Debian Codegraph @

src/cfg_stream.c

raw symbols SCIP
1 /*
2 * Copyright (c) 2017, 2020 Moritz Grimm <[email protected]>
3 *
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
15 */
16
17 #ifdef HAVE_CONFIG_H
18 # include "config.h"
19 #endif /* HAVE_CONFIG_H */
20
21 #include <sys/queue.h>
22
23 #include <string.h>
24
25 #include "cfg_private.h"
26 #include "cfg_stream.h"
27 #include "xalloc.h"
28
29 struct cfg_stream {
30 TAILQ_ENTRY(cfg_stream) entry;
31 char *name;
32 char *mountpoint;
33 char *intake;
34 char *server;
35 int public;
36 enum cfg_stream_format format;
37 char *encoder;
38 char *stream_name;
39 char *stream_url;
40 char *stream_genre;
41 char *stream_description;
42 char *stream_quality;
43 char *stream_bitrate;
44 char *stream_samplerate;
45 char *stream_channels;
46 };
47
48 TAILQ_HEAD(cfg_stream_list, cfg_stream);
49
50 struct cfg_stream_list *
51 cfg_stream_list_create(void)
52 {
53 struct cfg_stream_list *sl;
54
55 sl = xcalloc(1UL, sizeof(*sl));
56 TAILQ_INIT(sl);
57
58 return (sl);
59 }
60
61 void
62 cfg_stream_list_destroy(cfg_stream_list_t *sl_p)
63 {
64 struct cfg_stream_list *sl = *sl_p;
65 struct cfg_stream *s;
66
67 if (!sl)
68 return;
69
70 while (NULL != (s = TAILQ_FIRST(sl))) {
71 TAILQ_REMOVE(sl, s, entry);
72 cfg_stream_destroy(&s);
73 }
74
75 xfree(sl);
76 *sl_p = NULL;
77 }
78
79 unsigned int
80 cfg_stream_list_nentries(struct cfg_stream_list *sl)
81 {
82 struct cfg_stream *s;
83 unsigned int n = 0;
84
85 TAILQ_FOREACH(s, sl, entry) {
86 n++;
87 }
88
89 return (n);
90 }
91
92 struct cfg_stream *
93 cfg_stream_list_find(struct cfg_stream_list *sl, const char *name)
94 {
95 struct cfg_stream *s;
96
97 TAILQ_FOREACH(s, sl, entry) {
98 if (0 == strcasecmp(s->name, name))
99 return (s);
100 }
101
102 return (NULL);
103 }
104
105 struct cfg_stream *
106 cfg_stream_list_get(struct cfg_stream_list *sl, const char *name)
107 {
108 struct cfg_stream *s;
109
110 s = cfg_stream_list_find(sl, name);
111 if (s)
112 return (s);
113 s = cfg_stream_create(name);
114 if (!s)
115 return (NULL);
116
117 TAILQ_INSERT_TAIL(sl, s, entry);
118
119 return (s);
120 }
121
122 void
123 cfg_stream_list_foreach(struct cfg_stream_list *sl,
124 void (*cb)(cfg_stream_t, void *), void *cb_arg)
125 {
126 struct cfg_stream *s;
127
128 TAILQ_FOREACH(s, sl, entry) {
129 cb(s, cb_arg);
130 }
131 }
132
133 struct cfg_stream *
134 cfg_stream_create(const char *name)
135 {
136 struct cfg_stream *s;
137
138 if (!name || !name[0])
139 return (NULL);
140
141 s = xcalloc(1UL, sizeof(*s));
142 s->name = xstrdup(name);
143
144 return (s);
145 }
146
147 void
148 cfg_stream_destroy(struct cfg_stream **s_p)
149 {
150 struct cfg_stream *s = *s_p;
151
152 xfree(s->name);
153 xfree(s->mountpoint);
154 xfree(s->intake);
155 xfree(s->server);
156 xfree(s->encoder);
157 xfree(s->stream_name);
158 xfree(s->stream_url);
159 xfree(s->stream_genre);
160 xfree(s->stream_description);
161 xfree(s->stream_quality);
162 xfree(s->stream_bitrate);
163 xfree(s->stream_samplerate);
164 xfree(s->stream_channels);
165 xfree(s);
166 *s_p = NULL;
167 }
168
169 int
170 cfg_stream_str2fmt(const char *str, enum cfg_stream_format *fmt_p)
171 {
172 if (0 == strcasecmp(str, CFG_SFMT_OGG)) {
173 *fmt_p = CFG_STREAM_OGG;
174 } else if (0 == strcasecmp(str, CFG_SFMT_MP3)) {
175 *fmt_p = CFG_STREAM_MP3;
176 } else if (0 == strcasecmp(str, CFG_SFMT_WEBM)) {
177 *fmt_p = CFG_STREAM_WEBM;
178 } else if (0 == strcasecmp(str, CFG_SFMT_MATROSKA)) {
179 *fmt_p = CFG_STREAM_MATROSKA;
180 } else
181 return (-1);
182 return (0);
183 }
184
185 const char *
186 cfg_stream_fmt2str(enum cfg_stream_format fmt)
187 {
188 switch (fmt) {
189 case CFG_STREAM_OGG:
190 return (CFG_SFMT_OGG);
191 case CFG_STREAM_MP3:
192 return (CFG_SFMT_MP3);
193 case CFG_STREAM_WEBM:
194 return (CFG_SFMT_WEBM);
195 case CFG_STREAM_MATROSKA:
196 return (CFG_SFMT_MATROSKA);
197 default:
198 return (NULL);
199 }
200 }
201
202
203 int
204 cfg_stream_set_name(struct cfg_stream *s, struct cfg_stream_list *sl,
205 const char *name, const char **errstrp)
206 {
207 struct cfg_stream *s2;
208
209 if (!name || !name[0]) {
210 if (errstrp)
211 *errstrp = "empty";
212 return (-1);
213 }
214
215 s2 = cfg_stream_list_find(sl, name);
216 if (s2 && s2 != s) {
217 if (errstrp)
218 *errstrp = "already exists";
219 return (-1);
220 }
221
222 SET_XSTRDUP(s->name, name, errstrp);
223
224 return (0);
225 }
226
227 int
228 cfg_stream_set_mountpoint(struct cfg_stream *s,
229 struct cfg_stream_list *not_used, const char *mountpoint,
230 const char **errstrp)
231 {
232 (void)not_used;
233 SET_XSTRDUP(s->mountpoint, mountpoint, errstrp);
234 return (0);
235 }
236
237 int
238 cfg_stream_set_intake(struct cfg_stream *s, struct cfg_stream_list *not_used,
239 const char *intake, const char **errstrp)
240 {
241 (void)not_used;
242 SET_XSTRDUP(s->intake, intake, errstrp);
243 return (0);
244 }
245
246 int
247 cfg_stream_set_server(struct cfg_stream *s, struct cfg_stream_list *not_used,
248 const char *server, const char **errstrp)
249 {
250 (void)not_used;
251 SET_XSTRDUP(s->server, server, errstrp);
252 return (0);
253 }
254
255 int
256 cfg_stream_set_public(struct cfg_stream *s, struct cfg_stream_list *not_used,
257 const char *public, const char **errstrp)
258 {
259 (void)not_used;
260 SET_BOOLEAN(s->public, public, errstrp);
261 return (0);
262 }
263
264 int
265 cfg_stream_set_format(struct cfg_stream *s, struct cfg_stream_list *not_used,
266 const char *fmt_str, const char **errstrp)
267 {
268 enum cfg_stream_format fmt;
269
270 (void)not_used;
271
272 if (!fmt_str || !fmt_str[0]) {
273 if (errstrp)
274 *errstrp = "empty";
275 return (-1);
276 }
277
278 if (0 > cfg_stream_str2fmt(fmt_str, &fmt)) {
279 if (errstrp)
280 *errstrp = "unsupported stream format";
281 return (-1);
282 }
283
284 s->format = fmt;
285
286 return (0);
287 }
288
289 int
290 cfg_stream_set_encoder(struct cfg_stream *s, struct cfg_stream_list *not_used,
291 const char *encoder, const char **errstrp)
292 {
293 (void)not_used;
294
295 if (NULL == encoder) {
296 if (s->encoder)
297 xfree(s->encoder);
298 s->encoder = NULL;
299 return (0);
300 }
301
302 SET_XSTRDUP(s->encoder, encoder, errstrp);
303 return (0);
304 }
305
306 int
307 cfg_stream_set_stream_name(struct cfg_stream *s,
308 struct cfg_stream_list *not_used, const char *stream_name,
309 const char **errstrp)
310 {
311 (void)not_used;
312 SET_XSTRDUP(s->stream_name, stream_name, errstrp);
313 return (0);
314 }
315
316 int
317 cfg_stream_set_stream_url(struct cfg_stream *s,
318 struct cfg_stream_list *not_used, const char *stream_url,
319 const char **errstrp)
320 {
321 (void)not_used;
322 SET_XSTRDUP(s->stream_url, stream_url, errstrp);
323 return (0);
324 }
325
326 int
327 cfg_stream_set_stream_genre(struct cfg_stream *s,
328 struct cfg_stream_list *not_used, const char *stream_genre,
329 const char **errstrp)
330 {
331 (void)not_used;
332 SET_XSTRDUP(s->stream_genre, stream_genre, errstrp);
333 return (0);
334 }
335
336 int
337 cfg_stream_set_stream_description(struct cfg_stream *s,
338 struct cfg_stream_list *not_used, const char *stream_description,
339 const char **errstrp)
340 {
341 (void)not_used;
342 SET_XSTRDUP(s->stream_description, stream_description, errstrp);
343 return (0);
344 }
345
346 int
347 cfg_stream_set_stream_quality(struct cfg_stream *s,
348 struct cfg_stream_list *not_used, const char *stream_quality,
349 const char **errstrp)
350 {
351 (void)not_used;
352 SET_XSTRDUP(s->stream_quality, stream_quality, errstrp);
353 return (0);
354 }
355
356 int
357 cfg_stream_set_stream_bitrate(struct cfg_stream *s,
358 struct cfg_stream_list *not_used, const char *stream_bitrate,
359 const char **errstrp)
360 {
361 (void)not_used;
362 SET_XSTRDUP(s->stream_bitrate, stream_bitrate, errstrp);
363 return (0);
364 }
365
366 int
367 cfg_stream_set_stream_samplerate(struct cfg_stream *s,
368 struct cfg_stream_list *not_used, const char *stream_samplerate,
369 const char **errstrp)
370 {
371 (void)not_used;
372 SET_XSTRDUP(s->stream_samplerate, stream_samplerate, errstrp);
373 return (0);
374 }
375
376 int
377 cfg_stream_set_stream_channels(struct cfg_stream *s,
378 struct cfg_stream_list *not_used, const char *stream_channels,
379 const char **errstrp)
380 {
381 (void)not_used;
382 SET_XSTRDUP(s->stream_channels, stream_channels, errstrp);
383 return (0);
384 }
385
386 int
387 cfg_stream_validate(struct cfg_stream *s, const char **errstrp)
388 {
389 if (CFG_STREAM_INVALID == cfg_stream_get_format(s)) {
390 if (NULL != errstrp)
391 *errstrp = "format missing or unsupported";
392 return (-1);
393 }
394
395 return (0);
396 }
397
398 const char *
399 cfg_stream_get_name(struct cfg_stream *s)
400 {
401 return (s->name);
402 }
403
404 const char *
405 cfg_stream_get_mountpoint(struct cfg_stream *s)
406 {
407 return (s->mountpoint);
408 }
409
410 const char *
411 cfg_stream_get_intake(struct cfg_stream *s)
412 {
413 return (s->intake);
414 }
415
416 const char *
417 cfg_stream_get_server(struct cfg_stream *s)
418 {
419 return (s->server);
420 }
421
422 int
423 cfg_stream_get_public(struct cfg_stream *s)
424 {
425 return (s->public);
426 }
427
428 enum cfg_stream_format
429 cfg_stream_get_format(struct cfg_stream *s)
430 {
431 return (s->format);
432 }
433
434 const char *
435 cfg_stream_get_format_str(struct cfg_stream *s)
436 {
437 return (cfg_stream_fmt2str(s->format));
438 }
439
440 const char *
441 cfg_stream_get_encoder(struct cfg_stream *s)
442 {
443 return (s->encoder);
444 }
445
446 const char *
447 cfg_stream_get_stream_name(struct cfg_stream *s)
448 {
449 return (s->stream_name);
450 }
451
452 const char *
453 cfg_stream_get_stream_url(struct cfg_stream *s)
454 {
455 return (s->stream_url);
456 }
457
458 const char *
459 cfg_stream_get_stream_genre(struct cfg_stream *s)
460 {
461 return (s->stream_genre);
462 }
463
464 const char *
465 cfg_stream_get_stream_description(struct cfg_stream *s)
466 {
467 return (s->stream_description);
468 }
469
470 const char *
471 cfg_stream_get_stream_quality(struct cfg_stream *s)
472 {
473 return (s->stream_quality);
474 }
475
476 const char *
477 cfg_stream_get_stream_bitrate(struct cfg_stream *s)
478 {
479 return (s->stream_bitrate);
480 }
481
482 const char *
483 cfg_stream_get_stream_samplerate(struct cfg_stream *s)
484 {
485 return (s->stream_samplerate);
486 }
487
488 const char *
489 cfg_stream_get_stream_channels(struct cfg_stream *s)
490 {
491 return (s->stream_channels);
492 }