spxdec: Use read_u16() from portable_io.h.
[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 recv_init();
83 memset(&rn, 0, sizeof(struct receiver_node));
84 ret = check_receiver_arg(OPT_STRING_VAL(RECEIVER, lpr), &receiver_lpr);
85 if (ret < 0)
86 goto free_lpr;
87 cmd = lls_cmd(ret, recv_cmd_suite);
88 r = lls_user_data(cmd);
89 rn.receiver = r;
90 rn.lpr = receiver_lpr;
91 rn.btrn = btr_new_node(&(struct btr_node_description)
92 EMBRACE(.name = lls_command_name(cmd)));
93 ret = r->open(&rn);
94 if (ret < 0)
95 goto remove_btrn;
96 sot.btrn = btr_new_node(&(struct btr_node_description)
97 EMBRACE(.parent = rn.btrn, .name = "stdout"));
98 stdout_task_register(&sot, &s);
99
100 ti.name = lls_command_name(cmd);
101 ti.pre_select = r->pre_select;
102 ti.post_select = r->post_select;
103 ti.context = &rn;
104 rn.task = task_register(&ti, &s);
105
106 s.default_timeout.tv_sec = 1;
107 s.default_timeout.tv_usec = 0;
108 ret = schedule(&s);
109 sched_shutdown(&s);
110 r->close(&rn);
111 btr_remove_node(&sot.btrn);
112 remove_btrn:
113 btr_remove_node(&rn.btrn);
114 lls_free_parse_result(receiver_lpr, cmd);
115 free_lpr:
116 lls_free_parse_result(lpr, CMD_PTR);
117 out:
118 if (ret < 0) {
119 if (errctx)
120 PARA_ERROR_LOG("%s\n", errctx);
121 free(errctx);
122 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
123 }
124 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
125 }