btr: splice-out fix.
[paraslash.git] / recv.c
1 /*
2  * Copyright (C) 2005-2013 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file recv.c the stand-alone audio stream receiver */
8
9 #include <regex.h>
10 #include <sys/types.h>
11
12 #include "para.h"
13 #include "list.h"
14 #include "sched.h"
15 #include "ggo.h"
16 #include "buffer_tree.h"
17 #include "recv.h"
18 #include "recv.cmdline.h"
19 #include "fd.h"
20 #include "string.h"
21 #include "error.h"
22 #include "stdout.h"
23 #include "version.h"
24
25 extern void afh_recv_init(struct receiver *r);
26 #undef AFH_RECEIVER
27 #define AFH_RECEIVER {.name = "afh", .init = afh_recv_init},
28 DEFINE_RECEIVER_ARRAY;
29
30 /** The gengetopt args info struct. */
31 static struct recv_args_info conf;
32
33 static int loglevel;
34 /** Always log to stderr. */
35 INIT_STDERR_LOGGING(loglevel);
36
37 /** init array of error codes used by para_recv */
38 INIT_RECV_ERRLISTS;
39
40 __noreturn static void print_help_and_die(void)
41 {
42         struct ggo_help h = DEFINE_GGO_HELP(recv);
43         bool d = conf.detailed_help_given;
44
45         ggo_print_help(&h, d? GPH_STANDARD_FLAGS_DETAILED : GPH_STANDARD_FLAGS);
46         print_receiver_helps(d? GPH_MODULE_FLAGS_DETAILED : GPH_MODULE_FLAGS);
47         exit(0);
48 }
49
50 /**
51  * The main function of para_recv.
52  *
53  * \param argc number of arguments
54  * \param argv vector of arguments
55  *
56  * para_recv uses the specified receiver to receive an audio stream sent by
57  * para_server. The received data is written to stdout.
58  *
59  * \return \a EXIT_SUCCESS on success, \a EXIT_FAILURE on errors.
60  */
61 int main(int argc, char *argv[])
62 {
63         int ret, r_opened = 0, receiver_num;
64         struct receiver *r = NULL;
65         struct receiver_node rn;
66         struct stdout_task sot;
67         static struct sched s;
68
69         recv_cmdline_parser(argc, argv, &conf);
70         loglevel = get_loglevel_by_name(conf.loglevel_arg);
71         version_handle_flag("recv", conf.version_given);
72         recv_init();
73         if (conf.help_given || conf.detailed_help_given)
74                 print_help_and_die();
75
76         memset(&rn, 0, sizeof(struct receiver_node));
77         rn.conf = check_receiver_arg(conf.receiver_arg, &receiver_num);
78         if (!rn.conf) {
79                 PARA_EMERG_LOG("invalid receiver specifier\n");
80                 ret = -E_RECV_SYNTAX;
81                 goto out;
82         }
83         r = &receivers[receiver_num];
84         rn.receiver = r;
85         rn.btrn = btr_new_node(&(struct btr_node_description)
86                 EMBRACE(.name = r->name));
87         ret = r->open(&rn);
88         if (ret < 0)
89                 goto out;
90         r_opened = 1;
91
92         memset(&sot, 0, sizeof(struct stdout_task));
93         sot.btrn = btr_new_node(&(struct btr_node_description)
94                 EMBRACE(.parent = rn.btrn, .name = "stdout"));
95         stdout_set_defaults(&sot);
96         register_task(&s, &sot.task);
97
98         rn.task.pre_select = r->pre_select;
99         rn.task.post_select = r->post_select;
100         sprintf(rn.task.status, "%s", r->name);
101         register_task(&s, &rn.task);
102
103         s.default_timeout.tv_sec = 1;
104         s.default_timeout.tv_usec = 0;
105         ret = schedule(&s);
106 out:
107         if (r_opened)
108                 r->close(&rn);
109         btr_remove_node(&rn.btrn);
110         btr_remove_node(&sot.btrn);
111         if (rn.conf)
112                 r->free_config(rn.conf);
113
114         if (ret < 0)
115                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
116         return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
117 }