mp3dec: Only proceed decoding if at least 16K are available.
[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         static struct sched s;
66
67         s.default_timeout.tv_sec = 1;
68         s.default_timeout.tv_usec = 0;
69
70         memset(&sot, 0, sizeof(struct stdout_task));
71         memset(&rn, 0, sizeof(struct receiver_node));
72         for (ret = 0; receivers[ret].name; ret++)
73                 receivers[ret].init(&receivers[ret]);
74         ret = -E_RECV_SYNTAX;
75         rn.conf = parse_config(argc, argv, &receiver_num);
76         if (!rn.conf) {
77                 PARA_EMERG_LOG("parse failed\n");
78                 goto out;
79         }
80         r = &receivers[receiver_num];
81         rn.receiver = r;
82         ret = r->open(&rn);
83         if (ret < 0)
84                 goto out;
85         r_opened = 1;
86
87         stdout_set_defaults(&sot);
88         sot.buf = rn.buf;
89         sot.loaded = &rn.loaded;
90         sot.input_error = &rn.task.error;
91         register_task(&sot.task);
92
93         rn.task.pre_select = r->pre_select;
94         rn.task.post_select = r->post_select;
95         sprintf(rn.task.status, "receiver node");
96         register_task(&rn.task);
97
98         ret = schedule(&s);
99 out:
100         if (r_opened)
101                 r->close(&rn);
102         if (r)
103                 r->shutdown();
104         if (ret < 0)
105                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
106         return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
107 }