abebbfc2dfd80a0d2373f7c0431395c03656433f
[paraslash.git] / recv.c
1 /*
2  * Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>
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 #include <inttypes.h>
12 #include <lopsub.h>
13
14 #include "recv_cmd.lsg.h"
15 #include "para.h"
16 #include "list.h"
17 #include "sched.h"
18 #include "buffer_tree.h"
19 #include "recv.h"
20 #include "recv.cmdline.h"
21 #include "fd.h"
22 #include "string.h"
23 #include "error.h"
24 #include "stdout.h"
25 #include "version.h"
26
27 /** Array of error strings. */
28 DEFINE_PARA_ERRLIST;
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 __noreturn static void print_help_and_die(void)
38 {
39         bool d = conf.detailed_help_given;
40         if (d)
41                 recv_cmdline_parser_print_detailed_help();
42         else
43                 recv_cmdline_parser_print_help();
44         print_receiver_helps(d);
45         exit(EXIT_SUCCESS);
46 }
47
48 /**
49  * The main function of para_recv.
50  *
51  * \param argc number of arguments
52  * \param argv vector of arguments
53  *
54  * para_recv uses the specified receiver to receive an audio stream sent by
55  * para_server. The received data is written to stdout.
56  *
57  * \return \a EXIT_SUCCESS on success, \a EXIT_FAILURE on errors.
58  */
59 int main(int argc, char *argv[])
60 {
61         int ret;
62         bool r_opened = false;
63         const struct receiver *r = NULL;
64         struct receiver_node rn;
65         struct stdout_task sot = {.btrn = NULL};
66         static struct sched s;
67         struct task_info ti;
68         const struct lls_command *cmd;
69         struct lls_parse_result *receiver_lpr; /* receiver specific options */
70
71         recv_cmdline_parser(argc, argv, &conf);
72         loglevel = get_loglevel_by_name(conf.loglevel_arg);
73         version_handle_flag("recv", conf.version_given);
74         recv_init();
75         if (conf.help_given || conf.detailed_help_given)
76                 print_help_and_die();
77
78         memset(&rn, 0, sizeof(struct receiver_node));
79         ret = check_receiver_arg(conf.receiver_arg, &receiver_lpr);
80         if (ret < 0)
81                 goto out;
82         cmd = lls_cmd(ret, recv_cmd_suite);
83         r = lls_user_data(cmd);
84         rn.receiver = r;
85         rn.lpr = receiver_lpr;
86         rn.btrn = btr_new_node(&(struct btr_node_description)
87                 EMBRACE(.name = lls_command_name(cmd)));
88         ret = r->open(&rn);
89         if (ret < 0)
90                 goto free_receiver_lpr;
91         r_opened = true;
92
93         sot.btrn = btr_new_node(&(struct btr_node_description)
94                 EMBRACE(.parent = rn.btrn, .name = "stdout"));
95         stdout_task_register(&sot, &s);
96
97         ti.name = lls_command_name(cmd);
98         ti.pre_select = r->pre_select;
99         ti.post_select = r->post_select;
100         ti.context = &rn;
101         rn.task = task_register(&ti, &s);
102
103         s.default_timeout.tv_sec = 1;
104         s.default_timeout.tv_usec = 0;
105         ret = schedule(&s);
106         sched_shutdown(&s);
107         r->close(&rn);
108         btr_remove_node(&sot.btrn);
109         btr_remove_node(&rn.btrn);
110 free_receiver_lpr:
111         lls_free_parse_result(receiver_lpr, cmd);
112 out:
113         if (r_opened)
114                 r->close(&rn);
115         btr_remove_node(&rn.btrn);
116         btr_remove_node(&sot.btrn);
117
118         if (ret < 0)
119                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
120         return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
121 }