Merge branch 'master' into next
[paraslash.git] / recv.c
1 /* Copyright (C) 2005 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
2
3 /** \file recv.c the stand-alone audio stream receiver */
4
5 #include <regex.h>
6 #include <sys/types.h>
7 #include <lopsub.h>
8
9 #include "recv_cmd.lsg.h"
10 #include "recv.lsg.h"
11 #include "para.h"
12 #include "list.h"
13 #include "sched.h"
14 #include "buffer_tree.h"
15 #include "recv.h"
16 #include "fd.h"
17 #include "string.h"
18 #include "error.h"
19 #include "stdout.h"
20 #include "version.h"
21
22 /** Array of error strings. */
23 DEFINE_PARA_ERRLIST;
24
25 #define CMD_PTR (lls_cmd(0, recv_suite))
26 #define OPT_RESULT(_name, _lpr) \
27 (lls_opt_result(LSG_RECV_PARA_RECV_OPT_ ## _name, lpr))
28 #define OPT_GIVEN(_name, _lpr) (lls_opt_given(OPT_RESULT(_name, _lpr)))
29 #define OPT_UINT32_VAL(_name, _lpr) (lls_uint32_val(0, OPT_RESULT(_name, _lpr)))
30 #define OPT_STRING_VAL(_name, _lpr) (lls_string_val(0, OPT_RESULT(_name, _lpr)))
31
32 static int loglevel;
33 /** Always log to stderr. */
34 INIT_STDERR_LOGGING(loglevel);
35
36 static void handle_help_flag(struct lls_parse_result *lpr)
37 {
38 char *help;
39
40 if (OPT_GIVEN(DETAILED_HELP, lpr))
41 help = lls_long_help(CMD_PTR);
42 else if (OPT_GIVEN(HELP, lpr))
43 help = lls_short_help(CMD_PTR);
44 else
45 return;
46 printf("%s\n", help);
47 free(help);
48 print_receiver_helps(OPT_GIVEN(DETAILED_HELP, lpr));
49 exit(EXIT_SUCCESS);
50 }
51
52 /**
53 * The main function of para_recv.
54 *
55 * \param argc number of arguments
56 * \param argv vector of arguments
57 *
58 * para_recv uses the specified receiver to receive an audio stream sent by
59 * para_server. The received data is written to stdout.
60 *
61 * \return \a EXIT_SUCCESS on success, \a EXIT_FAILURE on errors.
62 */
63 int main(int argc, char *argv[])
64 {
65 int ret;
66 const struct receiver *r = NULL;
67 struct receiver_node rn;
68 struct stdout_task sot = {.btrn = NULL};
69 static struct sched s;
70 struct task_info ti;
71 const struct lls_command *cmd;
72 struct lls_parse_result *lpr; /* command line */
73 struct lls_parse_result *receiver_lpr; /* receiver specific options */
74 char *errctx;
75
76 ret = lls(lls_parse(argc, argv, CMD_PTR, &lpr, &errctx));
77 if (ret < 0)
78 goto out;
79 loglevel = OPT_UINT32_VAL(LOGLEVEL, lpr);
80 version_handle_flag("recv", OPT_GIVEN(VERSION, lpr));
81 handle_help_flag(lpr);
82 memset(&rn, 0, sizeof(struct receiver_node));
83 ret = check_receiver_arg(OPT_STRING_VAL(RECEIVER, lpr), &receiver_lpr);
84 if (ret < 0)
85 goto free_lpr;
86 cmd = lls_cmd(ret, recv_cmd_suite);
87 r = lls_user_data(cmd);
88 rn.receiver = r;
89 rn.lpr = receiver_lpr;
90 rn.btrn = btr_new_node(&(struct btr_node_description)
91 EMBRACE(.name = lls_command_name(cmd)));
92 ret = r->open(&rn);
93 if (ret < 0)
94 goto remove_btrn;
95 sot.btrn = btr_new_node(&(struct btr_node_description)
96 EMBRACE(.parent = rn.btrn, .name = "stdout"));
97 stdout_task_register(&sot, &s);
98
99 ti.name = lls_command_name(cmd);
100 ti.pre_select = r->pre_select;
101 ti.post_select = r->post_select;
102 ti.context = &rn;
103 rn.task = task_register(&ti, &s);
104
105 s.default_timeout.tv_sec = 1;
106 s.default_timeout.tv_usec = 0;
107 ret = schedule(&s);
108 sched_shutdown(&s);
109 r->close(&rn);
110 btr_remove_node(&sot.btrn);
111 remove_btrn:
112 btr_remove_node(&rn.btrn);
113 lls_free_parse_result(receiver_lpr, cmd);
114 free_lpr:
115 lls_free_parse_result(lpr, CMD_PTR);
116 out:
117 if (ret < 0) {
118 if (errctx)
119 PARA_ERROR_LOG("%s\n", errctx);
120 free(errctx);
121 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
122 }
123 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
124 }