oss writer: Switch to the alternative post select method.
[paraslash.git] / write_common.c
1 /*
2  * Copyright (C) 2006-2013 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file write_common.c common functions of para_audiod and para_write */
8
9 #include <regex.h>
10
11 #include "para.h"
12 #include "string.h"
13 #include "list.h"
14 #include "sched.h"
15 #include "ggo.h"
16 #include "buffer_tree.h"
17 #include "write.h"
18 #include "error.h"
19
20 /** the array containing the names of all supported writers */
21 const char *writer_names[] ={WRITER_NAMES};
22
23 /** the array of supported writers */
24 struct writer writers[NUM_SUPPORTED_WRITERS] = {WRITER_ARRAY};
25
26 /**
27  * Call the init function of each supported paraslash writer.
28  */
29 void writer_init(void)
30 {
31         int i;
32
33         FOR_EACH_WRITER(i)
34                 writers[i].init(&writers[i]);
35 }
36 /**
37  * Check if given string is a valid command line for any writer.
38  *
39  * \param \wa String of the form writer_name:options.
40  * \param writer_num Contains the number of the writer upon success.
41  *
42  * This function checks whether \a wa starts with the name of a supported
43  * paraslash writer, optionally followed by a colon and any options for that
44  * writer.  If a valid writer name was found and further are present, the
45  * remaining part of \a wa is passed to that writer's config parser.
46  *
47  * \return On success, a pointer to the gengetopt args info struct is returned
48  * and \a writer_num contains the number of the writer. Otherwise this function
49  * prints an error message and calls exit().
50  */
51 void *check_writer_arg_or_die(const char *wa, int *writer_num)
52 {
53         int i, ret, argc;
54         const char *cmdline;
55         char **argv;
56         void *conf;
57
58         if (!wa || !*wa) {
59                 i = DEFAULT_WRITER;
60                 cmdline = NULL;
61                 goto check;
62         }
63         PARA_INFO_LOG("checking %s\n", wa);
64         FOR_EACH_WRITER(i) {
65                 const char *name = writer_names[i];
66                 size_t len = strlen(name);
67                 char c;
68
69                 if (strlen(wa) < len)
70                         continue;
71                 if (strncmp(name, wa, len))
72                         continue;
73                 c = wa[len];
74                 if (!c || c == ' ') {
75                         cmdline = c? wa + len + 1 : NULL;
76                         goto check;
77                 }
78         }
79         PARA_EMERG_LOG("invalid writer %s\n", wa);
80         exit(EXIT_FAILURE);
81 check:
82         ret = create_shifted_argv(cmdline, " \t", &argv);
83         if (ret < 0) {
84                 PARA_EMERG_LOG("%s: %s\n", wa, para_strerror(-ret));
85                 exit(EXIT_FAILURE);
86         }
87         argc = ret;
88         argv[0] = make_message("%s_write", writer_names[i]);
89         *writer_num = i;
90         conf = writers[i].parse_config_or_die(argc, argv);
91         free_argv(argv);
92         return conf;
93 }
94
95 /**
96  * Open a writer node and register the corresponding task.
97  *
98  * \param wn The writer node to open.
99  * \param parent The parent btr node (the source for the writer node).
100  * \param s The scheduler instance to register the task to.
101  *
102  * The configuration of the writer node stored in \p wn->conf must be
103  * initialized before this function may be called.
104  */
105 void register_writer_node(struct writer_node *wn, struct btr_node *parent,
106                 struct sched *s)
107 {
108         struct writer *w = writers + wn->writer_num;
109         char *name = make_message("%s writer", writer_names[wn->writer_num]);
110
111         wn->btrn = btr_new_node(&(struct btr_node_description)
112                 EMBRACE(.name = name, .parent = parent,
113                 .handler = w->execute, .context = wn));
114         strcpy(wn->task.status, name);
115         free(name);
116         wn->task.pre_select = w->pre_select;
117         if (w->new_post_select) {
118                 wn->task.new_post_select = w->new_post_select;
119                 wn->task.post_select = NULL;
120         } else {
121                 wn->task.new_post_select = NULL;
122                 wn->task.post_select = w->post_select;
123         }
124         register_task(s, &wn->task);
125 }
126
127 /**
128  * Print the help text of all writers to stdout.
129  *
130  * \param detailed Whether to print the detailed help text.
131  */
132 void print_writer_helps(int detailed)
133 {
134         int i;
135
136         printf_or_die("\nAvailable writers: \n\t");
137         FOR_EACH_WRITER(i)
138                 printf_or_die("%s%s", i? " " : "", writer_names[i]);
139         printf_or_die("\n\n");
140         FOR_EACH_WRITER(i) {
141                 struct writer *w = writers + i;
142
143                 if (!w->help.short_help)
144                         continue;
145                 printf_or_die("Options for %s:\n", writer_names[i]);
146                 ggo_print_help(&w->help, detailed);
147         }
148 }
149
150 static void get_btr_value(struct btr_node *btrn, const char *cmd,
151                 int32_t *result)
152 {
153         char *buf = NULL;
154         int ret = btr_exec_up(btrn, cmd, &buf);
155
156         if (ret < 0) {
157                 /*
158                  * This really should not happen. It means one of our parent
159                  * nodes died unexpectedly. Proceed with fingers crossed.
160                  */
161                 PARA_CRIT_LOG("cmd %s: %s\n", cmd, para_strerror(-ret));
162                 *result = 0;
163                 return;
164         }
165         ret = para_atoi32(buf, result);
166         assert(ret >= 0);
167         free(buf);
168 }
169
170 /**
171  * Ask parent btr nodes for the sample rate of the current stream.
172  *
173  * \param btrn Where to start the search.
174  * \param result Filled in by this function.
175  *
176  * This function is assumed to succeed and terminates on errors.
177  */
178 void get_btr_sample_rate(struct btr_node *btrn, int32_t *result)
179 {
180         get_btr_value(btrn, "sample_rate", result);
181 }
182
183 /**
184  * Ask parent btr nodes for the channel count of the current stream.
185  *
186  * \param btrn See \ref get_btr_sample_rate.
187  * \param result See \ref get_btr_sample_rate.
188  */
189 void get_btr_channels(struct btr_node *btrn, int32_t *result)
190 {
191         get_btr_value(btrn, "channels", result);
192 }
193
194 /**
195  * Ask parent btr nodes for the number of bits per sample and the byte sex.
196  *
197  * \param btrn See \ref get_btr_sample_rate.
198  * \param result Contains the sample format as an enum sample_format type.
199  */
200 void get_btr_sample_format(struct btr_node *btrn, int32_t *result)
201 {
202         get_btr_value(btrn, "sample_format", result);
203 }