kill the read_chunk handler of struct audio_format handler
[paraslash.git] / vss.c
1 /*
2 * Copyright (C) 1997-2007 Andre Noll <maan@systemlinux.org>
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
17 */
18
19 /** \file vss.c the virtual streaming system
20 *
21 * This contains the audio sending part of para_server which is independent of
22 * the current audio format, audio file selector and of the activated senders.
23 */
24
25 #include "server.h"
26 #include <sys/time.h> /* gettimeofday */
27 #include "server.cmdline.h"
28 #include "afs.h"
29 #include "afh.h"
30 #include "vss.h"
31 #include "send.h"
32 #include "error.h"
33 #include "string.h"
34 #include "fd.h"
35
36 extern const char *status_item_list[];
37
38 static struct timeval announce_tv;
39 static struct timeval data_send_barrier;
40 static struct timeval eof_barrier;
41 static struct timeval autoplay_barrier;
42
43 extern struct misc_meta_data *mmd;
44 extern struct audio_file_selector selectors[];
45 extern struct sender senders[];
46 static char *inbuf;
47 static size_t *chunk_table, inbuf_size;
48
49 static FILE *audio_file = NULL;
50
51 #if 1
52 void mp3_init(struct audio_format_handler *);
53 #endif
54
55 #ifdef HAVE_OGGVORBIS
56 void ogg_init(struct audio_format_handler *);
57 #endif
58 #ifdef HAVE_FAAD
59 void aac_afh_init(struct audio_format_handler *);
60 #endif
61
62 /**
63 * the list of supported audio formats
64 */
65 static struct audio_format_handler afl[] = {
66 #if 1
67 {
68 .name = "mp3",
69 .init = mp3_init,
70 },
71 #endif
72 #ifdef HAVE_OGGVORBIS
73 {
74 .name = "ogg",
75 .init = ogg_init,
76 },
77 #endif
78 #ifdef HAVE_FAAD
79 {
80 .name = "aac",
81 .init = aac_afh_init,
82 },
83 #endif
84 {
85 .name = NULL,
86 }
87 };
88
89 /** iterate over each supported audio format */
90 #define FOR_EACH_AUDIO_FORMAT(i) for (i = 0; afl[i].name; i++)
91
92
93
94 /**
95 * check if vss status flag \a P (playing) is set
96 *
97 * \return greater than zero if playing, zero otherwise.
98 *
99 */
100 unsigned int vss_playing(void)
101 {
102 return mmd->new_vss_status_flags & VSS_PLAYING;
103 }
104
105 /**
106 * check if \a N (next) status flag is set
107 *
108 * \return greater than zero if set, zero if not.
109 *
110 */
111 unsigned int vss_next(void)
112 {
113 return mmd->new_vss_status_flags & VSS_NEXT;
114 }
115
116 /**
117 * check if a reposition request is pending
118 *
119 * \return greater than zero if true, zero otherwise.
120 *
121 */
122 unsigned int vss_repos(void)
123 {
124 return mmd->new_vss_status_flags & VSS_REPOS;
125 }
126
127 /**
128 * check if the vss is currently paused
129 *
130 * \return greater than zero if paused, zero otherwise.
131 *
132 */
133 unsigned int vss_paused(void)
134 {
135 return !(mmd->new_vss_status_flags & VSS_NEXT)
136 && !(mmd->new_vss_status_flags & VSS_PLAYING);
137 }
138
139 /**
140 * get the name of the given audio format
141 * \param i the audio format number
142 *
143 * This returns a pointer to statically allocated memory so it
144 * must not be freed by the caller.
145 */
146 const char *audio_format_name(int i)
147 {
148 return i >= 0? afl[i].name : "(none)";
149 }
150
151 /**
152 * initialize the virtual streaming system
153 *
154 * Call the init functions of all supported audio format handlers and
155 * initialize all supported senders.
156 */
157 void vss_init(void)
158 {
159 int i;
160 char *hn = para_hostname(), *home = para_homedir();
161
162 PARA_DEBUG_LOG("supported audio formats: %s\n",
163 SUPPORTED_AUDIO_FORMATS);
164 for (i = 0; afl[i].name; i++) {
165 PARA_NOTICE_LOG("initializing %s handler\n",
166 afl[i].name);
167 afl[i].init(&afl[i]);
168 }
169 ms2tv(conf.announce_time_arg, &announce_tv);
170 PARA_INFO_LOG("announce timeval: %lums\n", tv2ms(&announce_tv));
171 for (i = 0; senders[i].name; i++) {
172 PARA_NOTICE_LOG("initializing %s sender\n", senders[i].name);
173 senders[i].init(&senders[i]);
174 }
175 free(hn);
176 free(home);
177 if (conf.autoplay_given) {
178 struct timeval now, tmp;
179 mmd->vss_status_flags |= VSS_PLAYING;
180 mmd->new_vss_status_flags |= VSS_PLAYING;
181 gettimeofday(&now, NULL);
182 ms2tv(conf.autoplay_delay_arg, &tmp);
183 tv_add(&now, &tmp, &autoplay_barrier);
184 }
185 }
186
187 static int get_file_info(int i)
188 {
189 return afl[i].get_file_info(audio_file, mmd->audio_file_info,
190 &mmd->chunks_total, &mmd->seconds_total, &chunk_table);
191 }
192
193 /**
194 * guess the audio format judging from filename
195 *
196 * \param name the filename
197 *
198 * \return This function returns -1 if it has no idea what kind of audio
199 * file this might be. Otherwise the (non-negative) number of the audio format
200 * is returned.
201 */
202 int guess_audio_format(const char *name)
203 {
204 int i,j, len = strlen(name);
205
206 FOR_EACH_AUDIO_FORMAT(i) {
207 for (j = 0; afl[i].suffixes[j]; j++) {
208 const char *p = afl[i].suffixes[j];
209 int plen = strlen(p);
210 if (len < plen + 1)
211 continue;
212 if (name[len - plen - 1] != '.')
213 continue;
214 if (strcasecmp(name + len - plen, p))
215 continue;
216 // PARA_DEBUG_LOG("might be %s\n", audio_format_name(i));
217 return i;
218 }
219 }
220 return -1;
221 }
222
223 static int get_audio_format(int omit)
224 {
225 int i;
226
227 FOR_EACH_AUDIO_FORMAT(i) {
228 if (i == omit)
229 continue;
230 rewind(audio_file);
231 if (get_file_info(i) > 0)
232 return i;
233 rewind(audio_file);
234 }
235 return -E_AUDIO_FORMAT;
236 }
237
238 /*
239 * upddate shared mem
240 */
241 static int update_mmd(void)
242 {
243 int i;
244 struct stat file_status;
245
246 i = guess_audio_format(mmd->filename);
247 if (i < 0 || get_file_info(i) < 0)
248 i = get_audio_format(i);
249 if (i < 0)
250 return i;
251 mmd->audio_format = i;
252 mmd->chunks_sent = 0;
253 mmd->current_chunk = 0;
254 mmd->offset = 0;
255 if (fstat(fileno(audio_file), &file_status) == -1)
256 return -E_FSTAT;
257 mmd->size = file_status.st_size;
258 mmd->mtime = file_status.st_mtime;
259 mmd->events++;
260 PARA_NOTICE_LOG("next audio file: %s\n", mmd->filename);
261 return 1;
262 }
263
264 static void get_song(void)
265 {
266 char **sl = selectors[mmd->selector_num].get_audio_file_list(10);
267 int i;
268
269 if (!sl)
270 goto err_out;
271 for (i = 0; sl[i]; i++) {
272 struct timeval now;
273 PARA_INFO_LOG("trying %s\n", sl[i]);
274 if (strlen(sl[i]) >= _POSIX_PATH_MAX)
275 continue;
276 audio_file = fopen(sl[i], "r");
277 if (!audio_file)
278 continue;
279 strcpy(mmd->filename, sl[i]);
280 if (update_mmd() < 0) {
281 fclose(audio_file);
282 audio_file = NULL;
283 continue;
284 }
285 mmd->num_played++;
286 if (selectors[mmd->selector_num].update_audio_file)
287 selectors[mmd->selector_num].update_audio_file(sl[i]);
288 PARA_DEBUG_LOG("%s", "success\n");
289 mmd->new_vss_status_flags &= (~VSS_NEXT);
290 gettimeofday(&now, NULL);
291 tv_add(&now, &announce_tv, &data_send_barrier);
292
293 goto free;
294 }
295 PARA_ERROR_LOG("%s", "no valid files found\n");
296 err_out:
297 mmd->new_vss_status_flags = VSS_NEXT;
298 free:
299 if (sl) {
300 for (i = 0; sl[i]; i++)
301 free(sl[i]);
302 free(sl);
303 }
304 }
305
306 static int chk_barrier(const char *bname, const struct timeval *now,
307 const struct timeval *barrier, struct timeval *diff,
308 int print_log)
309 {
310 long ms;
311
312 if (tv_diff(now, barrier, diff) > 0)
313 return 1;
314 ms = tv2ms(diff);
315 if (print_log && ms)
316 PARA_DEBUG_LOG("%s barrier: %lims left\n", bname, ms);
317 return -1;
318 }
319
320 static void vss_next_chunk_time(struct timeval *due)
321 {
322 struct timeval tmp;
323
324 tv_scale(mmd->chunks_sent, &afl[mmd->audio_format].chunk_tv, &tmp);
325 tv_add(&tmp, &mmd->stream_start, due);
326 }
327
328 /*
329 * != NULL: timeout for next chunk
330 * NULL: nothing to do
331 */
332 static struct timeval *vss_compute_timeout(void)
333 {
334 static struct timeval the_timeout;
335 struct timeval now, next_chunk;
336
337 if (vss_next() && mmd->audio_format >= 0) {
338 /* only sleep a bit, nec*/
339 the_timeout.tv_sec = 0;
340 the_timeout.tv_usec = 100;
341 return &the_timeout;
342 }
343 gettimeofday(&now, NULL);
344 if (chk_barrier("autoplay_delay", &now, &autoplay_barrier,
345 &the_timeout, 1) < 0)
346 return &the_timeout;
347 if (chk_barrier("eof", &now, &eof_barrier, &the_timeout, 1) < 0)
348 return &the_timeout;
349 if (chk_barrier("data send", &now, &data_send_barrier,
350 &the_timeout, 1) < 0)
351 return &the_timeout;
352 if (mmd->audio_format < 0 || !vss_playing() || !audio_file)
353 return NULL;
354 vss_next_chunk_time(&next_chunk);
355 if (chk_barrier(afl[mmd->audio_format].name, &now, &next_chunk,
356 &the_timeout, 0) < 0)
357 return &the_timeout;
358 /* chunk is due or bof */
359 the_timeout.tv_sec = 0;
360 the_timeout.tv_usec = 0;
361 return &the_timeout;
362 }
363
364 static void vss_eof(struct audio_format_handler *af)
365 {
366 struct timeval now;
367 int i;
368 char *tmp;
369
370 if (!af || !audio_file) {
371 for (i = 0; senders[i].name; i++)
372 senders[i].shutdown_clients();
373 return;
374 }
375 gettimeofday(&now, NULL);
376 tv_add(&af->eof_tv, &now, &eof_barrier);
377 af->close_audio_file();
378 audio_file = NULL;
379 mmd->audio_format = -1;
380 af = NULL;
381 mmd->chunks_sent = 0;
382 mmd->offset = 0;
383 mmd->seconds_total = 0;
384 tmp = make_message("%s:\n%s:\n%s:\n", status_item_list[SI_AUDIO_INFO1],
385 status_item_list[SI_AUDIO_INFO2], status_item_list[SI_AUDIO_INFO3]);
386 strcpy(mmd->audio_file_info, tmp);
387 free(tmp);
388 tmp = make_message("%s:\n%s:\n%s:\n", status_item_list[SI_DBINFO1],
389 status_item_list[SI_DBINFO2], status_item_list[SI_DBINFO3]);
390 strcpy(mmd->selector_info, tmp);
391 free(tmp);
392 mmd->filename[0] = '\0';
393 mmd->size = 0;
394 mmd->events++;
395 }
396
397 /**
398 * get the header and of the current audio file
399 *
400 * \param header_len the length of the header is stored here
401 *
402 * \return a pointer to a buffer containing the header, or NULL, if no audio
403 * file is selected or if the current audio format does not need special header
404 * treamtment.
405 *
406 */
407 char *vss_get_header(int *header_len)
408 {
409 *header_len = 0;
410 if (mmd->audio_format < 0)
411 return NULL;
412 if (!afl[mmd->audio_format].get_header_info)
413 return NULL;
414 return afl[mmd->audio_format].get_header_info(header_len);
415 }
416
417 /**
418 * get the list of all supported audio formats
419 *
420 * \return a space separated list of all supported audio formats
421 * It is not allocated at runtime, i.e. there is no need to free
422 * the returned string in the caller.
423 */
424 const char *supported_audio_formats(void)
425 {
426 return SUPPORTED_AUDIO_FORMATS;
427 }
428
429 /**
430 * get the chunk time of the current audio file
431 *
432 * \return a pointer to a struct containing the chunk time, or NULL,
433 * if currently no audio file is selected.
434 */
435 struct timeval *vss_chunk_time(void)
436 {
437 if (mmd->audio_format < 0)
438 return NULL;
439 return &afl[mmd->audio_format].chunk_tv;
440 }
441
442 /**
443 * compute the timeout for para_server's main select-loop
444 *
445 * This function gets called from para_server to determine the timeout value
446 * for its main select loop.
447 *
448 * Before the timeout is computed, the current vss status flags are evaluated
449 * and acted upon by calling appropriate functions from the lower layers.
450 * Possible actions include
451 *
452 * - request a new file list from the current audio file selector
453 * - shutdown of all senders (stop/pause command)
454 * - reposition the stream (ff/jmp command)
455 *
456 * \return A pointer to a struct timeval containing the timeout for the next
457 * chunk of data to be sent, or NULL if we're not sending right now.
458 */
459 struct timeval *vss_preselect(void)
460 {
461 struct audio_format_handler *af = NULL;
462 int i, format;
463 struct timeval *ret;
464 again:
465 format = mmd->audio_format;
466 if (format >= 0)
467 af = afl + format;
468 else
469 for (i = 0; senders[i].name; i++)
470 senders[i].shutdown_clients();
471 if (vss_next() && af) {
472 vss_eof(af);
473 return vss_compute_timeout();
474 }
475 if (vss_paused() || vss_repos()) {
476 for (i = 0; senders[i].name; i++)
477 senders[i].shutdown_clients();
478 if (af) {
479 struct timeval now;
480 gettimeofday(&now, NULL);
481 if (!vss_paused() || mmd->chunks_sent)
482 tv_add(&af->eof_tv, &now, &eof_barrier);
483 if (vss_repos())
484 tv_add(&now, &announce_tv, &data_send_barrier);
485 if (mmd->new_vss_status_flags & VSS_NOMORE)
486 mmd->new_vss_status_flags = VSS_NEXT;
487 }
488 mmd->chunks_sent = 0;
489 }
490 if (vss_repos()) {
491 mmd->new_vss_status_flags &= ~(VSS_REPOS);
492 mmd->current_chunk = mmd->repos_request;
493 }
494 ret = vss_compute_timeout();
495 if (!ret && !audio_file && vss_playing() &&
496 !(mmd->new_vss_status_flags & VSS_NOMORE)) {
497 PARA_DEBUG_LOG("%s", "ready and playing, but no audio file\n");
498 get_song();
499 goto again;
500 }
501 return ret;
502 }
503
504 static char *vss_read_chunk(long unsigned current_chunk, ssize_t *len)
505 {
506 int ret;
507 size_t pos;
508
509 *len = 0;
510 if (current_chunk >= mmd->chunks_total)
511 return NULL;
512 *len = chunk_table[current_chunk + 1] - chunk_table[current_chunk];
513 if (!*len) /* nothing to send for this run */
514 return inbuf;
515 pos = chunk_table[current_chunk];
516 if (inbuf_size < *len) {
517 PARA_INFO_LOG("increasing inbuf for chunk #%lu/%lu to %zd bytes\n",
518 current_chunk, mmd->chunks_total, *len);
519 inbuf = para_realloc(inbuf, *len);
520 inbuf_size = *len;
521 }
522 // PARA_DEBUG_LOG("reading chunk #%lu@%zd (%zd bytes)\n", current_chunk,
523 // pos, *len);
524 ret = fseek(audio_file, pos, SEEK_SET);
525 if (ret < 0)
526 return NULL;
527 ret = para_fread(inbuf, *len, 1, audio_file);
528 if (ret != *len)
529 return NULL;
530 return inbuf;
531 }
532
533 /**
534 * main sending function
535 *
536 * This function gets called from para_server as soon as the next chunk of
537 * data should be pushed out. It first calls the read_chunk() function of
538 * the current audio format handler to obtain a pointer to the data to be
539 * sent out as well as its length. This information is then passed to each
540 * supported sender's send() function which does the actual sending.
541 *
542 * Return value: Positive return value on success, zero on eof and negative
543 * on errors.
544 */
545 void vss_send_chunk(void)
546 {
547 int i;
548 struct audio_format_handler *af;
549 char *buf;
550 ssize_t ret;
551 struct timeval now, due;
552
553 if (mmd->audio_format < 0 || !audio_file || !vss_playing())
554 return;
555 af = &afl[mmd->audio_format];
556 gettimeofday(&now, NULL);
557 vss_next_chunk_time(&due);
558 if (tv_diff(&due, &now, NULL) > 0)
559 return;
560 if (chk_barrier("eof", &now, &eof_barrier, &due, 1) < 0)
561 return;
562 if (chk_barrier("data send", &now, &data_send_barrier,
563 &due, 1) < 0)
564 return;
565 buf = vss_read_chunk(mmd->current_chunk, &ret);
566 mmd->new_vss_status_flags &= ~VSS_REPOS;
567 if (!buf) {
568 if (ret < 0)
569 mmd->new_vss_status_flags = VSS_NEXT;
570 else
571 mmd->new_vss_status_flags |= VSS_NEXT;
572 vss_eof(af);
573 return;
574 }
575 if (!mmd->chunks_sent) {
576 struct timeval tmp;
577 gettimeofday(&mmd->stream_start, NULL);
578 tv_scale(mmd->current_chunk, &af->chunk_tv, &tmp);
579 mmd->offset = tv2ms(&tmp);
580 mmd->events++;
581 }
582 for (i = 0; senders[i].name; i++)
583 senders[i].send(mmd->current_chunk, mmd->chunks_sent, buf, ret);
584 mmd->new_vss_status_flags |= VSS_PLAYING;
585 mmd->chunks_sent++;
586 mmd->current_chunk++;
587 }