wma: Fix chunk_table computation.
[paraslash.git] / recv.c
1 /*
2  * Copyright (C) 2005-2009 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 #include <dirent.h>
12
13 #include "para.h"
14 #include "list.h"
15 #include "sched.h"
16 #include "ggo.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
24 /** The gengetopt args info struct. */
25 static struct recv_args_info conf;
26
27 static int loglevel;
28 /** Always log to stderr. */
29 INIT_STDERR_LOGGING(loglevel);
30
31 /** init array of error codes used by para_recv */
32 INIT_RECV_ERRLISTS;
33
34 __noreturn static void print_help_and_die(void)
35 {
36         int d = conf.detailed_help_given;
37         const char **p = d? recv_args_info_detailed_help
38                 : recv_args_info_help;
39
40         printf_or_die("%s\n\n", RECV_CMDLINE_PARSER_PACKAGE "-"
41                 RECV_CMDLINE_PARSER_VERSION);
42         printf_or_die("%s\n\n", recv_args_info_usage);
43         for (; *p; p++)
44                 printf_or_die("%s\n", *p);
45         print_receiver_helps(d);
46         exit(0);
47 }
48
49 static void *parse_config(int argc, char *argv[], int *receiver_num)
50 {
51         if (recv_cmdline_parser(argc, argv, &conf))
52                 return NULL;
53         HANDLE_VERSION_FLAG("recv", conf);
54         if (conf.help_given || conf.detailed_help_given)
55                 print_help_and_die();
56         loglevel = get_loglevel_by_name(conf.loglevel_arg);
57         return check_receiver_arg(conf.receiver_arg, receiver_num);
58 }
59
60 /**
61  * the main function of para_recv
62  *
63  * \param argc number of arguments
64  * \param argv vector of arguments
65  *
66  * para_recv uses the specified receiver to receive an audio stream sent by
67  * para_server. The received data is written to stdout.
68  *
69  * \return \a EXIT_SUCCESS on success, \a EXIT_FAILURE on errors.
70  */
71 int main(int argc, char *argv[])
72 {
73         int ret, r_opened = 0, receiver_num;
74         struct receiver *r = NULL;
75         struct receiver_node rn;
76         struct stdout_task sot;
77         static struct sched s;
78
79         s.default_timeout.tv_sec = 1;
80         s.default_timeout.tv_usec = 0;
81
82         memset(&sot, 0, sizeof(struct stdout_task));
83         memset(&rn, 0, sizeof(struct receiver_node));
84         recv_init();
85         ret = -E_RECV_SYNTAX;
86         rn.conf = parse_config(argc, argv, &receiver_num);
87         if (!rn.conf) {
88                 PARA_EMERG_LOG("parse failed\n");
89                 goto out;
90         }
91         r = &receivers[receiver_num];
92         rn.receiver = r;
93         ret = r->open(&rn);
94         if (ret < 0)
95                 goto out;
96         r_opened = 1;
97
98         stdout_set_defaults(&sot);
99         sot.bufp = &rn.buf;
100         sot.loaded = &rn.loaded;
101         sot.input_error = &rn.task.error;
102         register_task(&sot.task);
103
104         rn.task.pre_select = r->pre_select;
105         rn.task.post_select = r->post_select;
106         sprintf(rn.task.status, "receiver node");
107         register_task(&rn.task);
108
109         ret = schedule(&s);
110 out:
111         if (r_opened)
112                 r->close(&rn);
113         if (r)
114                 r->shutdown();
115         if (ret < 0)
116                 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
117         return ret < 0? EXIT_FAILURE : EXIT_SUCCESS;
118 }