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