Merge branch 't/gui_improvements'
[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 int d = conf.detailed_help_given;
43 const char **p = d? recv_args_info_detailed_help
44 : recv_args_info_help;
45
46 printf_or_die("%s\n\n", RECV_CMDLINE_PARSER_PACKAGE "-"
47 RECV_CMDLINE_PARSER_VERSION);
48 printf_or_die("%s\n\n", recv_args_info_usage);
49 for (; *p; p++)
50 printf_or_die("%s\n", *p);
51 print_receiver_helps(d);
52 exit(0);
53 }
54
55 static void *parse_config(int argc, char *argv[], int *receiver_num)
56 {
57 if (recv_cmdline_parser(argc, argv, &conf))
58 return NULL;
59 HANDLE_VERSION_FLAG("recv", conf);
60 if (conf.help_given || conf.detailed_help_given)
61 print_help_and_die();
62 loglevel = get_loglevel_by_name(conf.loglevel_arg);
63 return check_receiver_arg(conf.receiver_arg, receiver_num);
64 }
65
66 /**
67 * The main function of para_recv.
68 *
69 * \param argc number of arguments
70 * \param argv vector of arguments
71 *
72 * para_recv uses the specified receiver to receive an audio stream sent by
73 * para_server. The received data is written to stdout.
74 *
75 * \return \a EXIT_SUCCESS on success, \a EXIT_FAILURE on errors.
76 */
77 int main(int argc, char *argv[])
78 {
79 int ret, r_opened = 0, receiver_num;
80 struct receiver *r = NULL;
81 struct receiver_node rn;
82 struct stdout_task sot;
83 static struct sched s;
84
85 s.default_timeout.tv_sec = 1;
86 s.default_timeout.tv_usec = 0;
87
88 memset(&sot, 0, sizeof(struct stdout_task));
89 memset(&rn, 0, sizeof(struct receiver_node));
90 recv_init();
91 ret = -E_RECV_SYNTAX;
92 rn.conf = parse_config(argc, argv, &receiver_num);
93 if (!rn.conf) {
94 PARA_EMERG_LOG("parse failed\n");
95 goto out;
96 }
97 r = &receivers[receiver_num];
98 rn.receiver = r;
99 rn.btrn = btr_new_node(&(struct btr_node_description)
100 EMBRACE(.name = r->name));
101 ret = r->open(&rn);
102 if (ret < 0)
103 goto out;
104 r_opened = 1;
105
106 sot.btrn = btr_new_node(&(struct btr_node_description)
107 EMBRACE(.parent = rn.btrn, .name = "stdout"));
108 stdout_set_defaults(&sot);
109 register_task(&s, &sot.task);
110
111 rn.task.pre_select = r->pre_select;
112 rn.task.post_select = r->post_select;
113 sprintf(rn.task.status, "%s", r->name);
114 register_task(&s, &rn.task);
115
116 ret = schedule(&s);
117 out:
118 if (r_opened)
119 r->close(&rn);
120 btr_remove_node(&rn.btrn);
121 btr_remove_node(&sot.btrn);
122 if (rn.conf)
123 r->free_config(rn.conf);
124
125 if (ret < 0)
126 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
127 return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
128 }