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