audioc: Make all fds non-blocking.
[paraslash.git] / recv.c
1 /*
2  * Copyright (C) 2005-2008 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 <sys/types.h>
10 #include <dirent.h>
11
12 #include "para.h"
13 #include "list.h"
14 #include "sched.h"
15 #include "recv.h"
16 #include "recv.cmdline.h"
17 #include "fd.h"
18 #include "error.h"
19 #include "stdout.h"
20
21 /** the gengetopt args info struct */
22 struct recv_args_info conf;
23
24 /** always log to stderr */
25 INIT_STDERR_LOGGING(conf.loglevel_arg);
26
27 /** init array of error codes used by para_recv */
28 INIT_RECV_ERRLISTS;
29
30 static void *parse_config(int argc, char *argv[], int *receiver_num)
31 {
32         int i;
33
34         if (recv_cmdline_parser(argc, argv, &conf))
35                 return NULL;
36         HANDLE_VERSION_FLAG("recv", conf);
37         if (conf.list_receivers_given) {
38                 printf("available receivers: ");
39                 for (i = 0; receivers[i].name; i++)
40                         printf("%s%s", i? " " : "", receivers[i].name);
41                 printf("\nTry\n\tpara_recv -r '<receivername> -h'\n"
42                         "for help on <receivername>.\n");
43                 exit(EXIT_SUCCESS);
44         }
45         return check_receiver_arg(conf.receiver_arg, receiver_num);
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, r_opened = 0, receiver_num;
62         struct receiver *r = NULL;
63         struct receiver_node rn;
64         struct stdout_task sot;
65         struct sched s;
66
67         s.default_timeout.tv_sec = 1;
68         s.default_timeout.tv_usec = 0;
69
70         memset(&rn, 0, sizeof(struct receiver_node));
71         for (ret = 0; receivers[ret].name; ret++)
72                 receivers[ret].init(&receivers[ret]);
73         ret = -E_RECV_SYNTAX;
74         rn.conf = parse_config(argc, argv, &receiver_num);
75         if (!rn.conf) {
76                 PARA_EMERG_LOG("parse failed\n");
77                 goto out;
78         }
79         r = &receivers[receiver_num];
80         rn.receiver = r;
81         ret = r->open(&rn);
82         if (ret < 0)
83                 goto out;
84         r_opened = 1;
85
86         stdout_set_defaults(&sot);
87         sot.buf = rn.buf;
88         sot.loaded = &rn.loaded;
89         sot.input_error = &rn.task.error;
90         register_task(&sot.task);
91
92         rn.task.pre_select = r->pre_select;
93         rn.task.post_select = r->post_select;
94         sprintf(rn.task.status, "receiver node");
95         register_task(&rn.task);
96
97         ret = schedule(&s);
98 out:
99         if (r_opened)
100                 r->close(&rn);
101         if (r)
102                 r->shutdown();
103         if (ret < 0)
104                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
105         return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
106 }