be more carful wrt. signed vs. unsigned argument passing
[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 streaming code of para_server which is independent
22 * of the current audio format, audio file selector and of the activated
23 * senders.
24 */
25
26 #include "server.h"
27 #include <sys/mman.h> /* mmap */
28 #include <sys/time.h> /* gettimeofday */
29 #include "server.cmdline.h"
30 #include "afs.h"
31 #include "vss.h"
32 #include "send.h"
33 #include "error.h"
34 #include "string.h"
35 #include "fd.h"
36
37 extern const char *status_item_list[];
38
39 static struct timeval announce_tv;
40 static struct timeval data_send_barrier;
41 static struct timeval eof_barrier;
42 static struct timeval autoplay_barrier;
43
44 extern struct misc_meta_data *mmd;
45 extern struct audio_file_selector selectors[];
46 extern struct sender senders[];
47
48 static int audio_file;
49 static char *map;
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 long unsigned announce_time = conf.announce_time_arg > 0?
162 conf.announce_time_arg : 300,
163 autoplay_delay = conf.autoplay_delay_arg > 0?
164 conf.autoplay_delay_arg : 0;
165
166 PARA_DEBUG_LOG("supported audio formats: %s\n",
167 SUPPORTED_AUDIO_FORMATS);
168 for (i = 0; afl[i].name; i++) {
169 PARA_NOTICE_LOG("initializing %s handler\n",
170 afl[i].name);
171 afl[i].init(&afl[i]);
172 }
173 ms2tv(announce_time, &announce_tv);
174 PARA_INFO_LOG("announce timeval: %lums\n", tv2ms(&announce_tv));
175 for (i = 0; senders[i].name; i++) {
176 PARA_NOTICE_LOG("initializing %s sender\n", senders[i].name);
177 senders[i].init(&senders[i]);
178 }
179 free(hn);
180 free(home);
181 if (conf.autoplay_given) {
182 struct timeval now, tmp;
183 mmd->vss_status_flags |= VSS_PLAYING;
184 mmd->new_vss_status_flags |= VSS_PLAYING;
185 gettimeofday(&now, NULL);
186 ms2tv(autoplay_delay, &tmp);
187 tv_add(&now, &tmp, &autoplay_barrier);
188 }
189 }
190
191 static int get_file_info(int i)
192 {
193 return afl[i].get_file_info(map, mmd->size, &mmd->afi);
194 }
195
196 /**
197 * guess the audio format judging from filename
198 *
199 * \param name the filename
200 *
201 * \return This function returns -1 if it has no idea what kind of audio
202 * file this might be. Otherwise the (non-negative) number of the audio format
203 * is returned.
204 */
205 int guess_audio_format(const char *name)
206 {
207 int i,j, len = strlen(name);
208
209 FOR_EACH_AUDIO_FORMAT(i) {
210 for (j = 0; afl[i].suffixes[j]; j++) {
211 const char *p = afl[i].suffixes[j];
212 int plen = strlen(p);
213 if (len < plen + 1)
214 continue;
215 if (name[len - plen - 1] != '.')
216 continue;
217 if (strcasecmp(name + len - plen, p))
218 continue;
219 // PARA_DEBUG_LOG("might be %s\n", audio_format_name(i));
220 return i;
221 }
222 }
223 return -1;
224 }
225
226 static int get_audio_format(int omit)
227 {
228 int i;
229
230 FOR_EACH_AUDIO_FORMAT(i) {
231 if (i == omit)
232 continue;
233 if (get_file_info(i) > 0)
234 return i;
235 }
236 return -E_AUDIO_FORMAT;
237 }
238
239 /*
240 * upddate shared mem
241 */
242 static int update_mmd(void)
243 {
244 int i;
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 mmd->events++;
256 return 1;
257 }
258
259 static void vss_get_audio_file(void)
260 {
261 char **sl = selectors[mmd->selector_num].get_audio_file_list(10);
262 int i;
263 struct stat file_status;
264
265 if (!sl)
266 goto err_out;
267 for (i = 0; sl[i]; i++) {
268 struct timeval now;
269 PARA_INFO_LOG("trying %s\n", sl[i]);
270 if (strlen(sl[i]) >= _POSIX_PATH_MAX)
271 continue;
272 audio_file = open(sl[i], O_RDONLY);
273 if (audio_file < 0)
274 continue;
275 if (fstat(audio_file, &file_status) == -1 ||
276 !file_status.st_size) {
277 close(audio_file);
278 continue;
279 }
280 mmd->size = file_status.st_size;
281 mmd->mtime = file_status.st_mtime;
282 map = para_mmap(mmd->size, PROT_READ, MAP_PRIVATE,
283 audio_file, 0);
284 strcpy(mmd->filename, sl[i]);
285 mmd->afi.header_len = 0; /* default: no header */
286 if (update_mmd() < 0) { /* invalid file */
287 close(audio_file);
288 munmap(map, mmd->size);
289 map = NULL;
290 continue;
291 }
292 mmd->num_played++;
293 if (selectors[mmd->selector_num].update_audio_file)
294 selectors[mmd->selector_num].update_audio_file(sl[i]);
295 PARA_NOTICE_LOG("next audio file: %s\n", mmd->filename);
296 mmd->new_vss_status_flags &= (~VSS_NEXT);
297 gettimeofday(&now, NULL);
298 tv_add(&now, &announce_tv, &data_send_barrier);
299 goto free;
300 }
301 err_out:
302 PARA_ERROR_LOG("%s", "no valid files found\n");
303 mmd->new_vss_status_flags = VSS_NEXT;
304 free:
305 if (sl) {
306 for (i = 0; sl[i]; i++)
307 free(sl[i]);
308 free(sl);
309 }
310 }
311
312 static int chk_barrier(const char *bname, const struct timeval *now,
313 const struct timeval *barrier, struct timeval *diff,
314 int print_log)
315 {
316 long ms;
317
318 if (tv_diff(now, barrier, diff) > 0)
319 return 1;
320 ms = tv2ms(diff);
321 if (print_log && ms)
322 PARA_DEBUG_LOG("%s barrier: %lims left\n", bname, ms);
323 return -1;
324 }
325
326 static void vss_next_chunk_time(struct timeval *due)
327 {
328 struct timeval tmp;
329
330 tv_scale(mmd->chunks_sent, &mmd->afi.chunk_tv, &tmp);
331 tv_add(&tmp, &mmd->stream_start, due);
332 }
333
334 /*
335 * != NULL: timeout for next chunk
336 * NULL: nothing to do
337 */
338 static struct timeval *vss_compute_timeout(void)
339 {
340 static struct timeval the_timeout;
341 struct timeval now, next_chunk;
342
343 if (vss_next() && mmd->audio_format >= 0) {
344 /* only sleep a bit, nec*/
345 the_timeout.tv_sec = 0;
346 the_timeout.tv_usec = 100;
347 return &the_timeout;
348 }
349 gettimeofday(&now, NULL);
350 if (chk_barrier("autoplay_delay", &now, &autoplay_barrier,
351 &the_timeout, 1) < 0)
352 return &the_timeout;
353 if (chk_barrier("eof", &now, &eof_barrier, &the_timeout, 1) < 0)
354 return &the_timeout;
355 if (chk_barrier("data send", &now, &data_send_barrier,
356 &the_timeout, 1) < 0)
357 return &the_timeout;
358 if (mmd->audio_format < 0 || !vss_playing() || !map)
359 return NULL;
360 vss_next_chunk_time(&next_chunk);
361 if (chk_barrier(afl[mmd->audio_format].name, &now, &next_chunk,
362 &the_timeout, 0) < 0)
363 return &the_timeout;
364 /* chunk is due or bof */
365 the_timeout.tv_sec = 0;
366 the_timeout.tv_usec = 0;
367 return &the_timeout;
368 }
369
370 static void vss_eof(void)
371 {
372 struct timeval now;
373 int i;
374 char *tmp;
375
376 if (!map) {
377 for (i = 0; senders[i].name; i++)
378 senders[i].shutdown_clients();
379 return;
380 }
381 gettimeofday(&now, NULL);
382 tv_add(&mmd->afi.eof_tv, &now, &eof_barrier);
383 munmap(map, mmd->size);
384 map = NULL;
385 close(audio_file);
386 mmd->audio_format = -1;
387 mmd->chunks_sent = 0;
388 mmd->offset = 0;
389 mmd->afi.seconds_total = 0;
390 free(mmd->afi.chunk_table);
391 mmd->afi.chunk_table = NULL;
392 tmp = make_message("%s:\n%s:\n%s:\n", status_item_list[SI_AUDIO_INFO1],
393 status_item_list[SI_AUDIO_INFO2], status_item_list[SI_AUDIO_INFO3]);
394 strcpy(mmd->afi.info_string, tmp);
395 free(tmp);
396 tmp = make_message("%s:\n%s:\n%s:\n", status_item_list[SI_DBINFO1],
397 status_item_list[SI_DBINFO2], status_item_list[SI_DBINFO3]);
398 strcpy(mmd->selector_info, tmp);
399 free(tmp);
400 mmd->filename[0] = '\0';
401 mmd->size = 0;
402 mmd->events++;
403 }
404
405 /**
406 * get the header and of the current audio file
407 *
408 * \param header_len the length of the header is stored here
409 *
410 * \return a pointer to a buffer containing the header, or NULL, if no audio
411 * file is selected or if the current audio format does not need special header
412 * treamtment.
413 *
414 */
415 char *vss_get_header(unsigned *header_len)
416 {
417 if (mmd->audio_format < 0 || !map || !mmd->afi.header_len)
418 return NULL;
419 *header_len = mmd->afi.header_len;
420 return map + mmd->afi.header_offset;
421 }
422
423 /**
424 * get the list of all supported audio formats
425 *
426 * \return a space separated list of all supported audio formats
427 * It is not allocated at runtime, i.e. there is no need to free
428 * the returned string in the caller.
429 */
430 const char *supported_audio_formats(void)
431 {
432 return SUPPORTED_AUDIO_FORMATS;
433 }
434
435 /**
436 * get the chunk time of the current audio file
437 *
438 * \return a pointer to a struct containing the chunk time, or NULL,
439 * if currently no audio file is selected.
440 */
441 struct timeval *vss_chunk_time(void)
442 {
443 if (mmd->audio_format < 0)
444 return NULL;
445 return &mmd->afi.chunk_tv;
446 }
447
448 /**
449 * compute the timeout for para_server's main select-loop
450 *
451 * This function gets called from para_server to determine the timeout value
452 * for its main select loop.
453 *
454 * Before the timeout is computed, the current vss status flags are evaluated
455 * and acted upon by calling appropriate functions from the lower layers.
456 * Possible actions include
457 *
458 * - request a new file list from the current audio file selector
459 * - shutdown of all senders (stop/pause command)
460 * - reposition the stream (ff/jmp command)
461 *
462 * \return A pointer to a struct timeval containing the timeout for the next
463 * chunk of data to be sent, or NULL if we're not sending right now.
464 */
465 struct timeval *vss_preselect(void)
466 {
467 struct audio_format_handler *af = NULL;
468 int i, format;
469 struct timeval *ret;
470 again:
471 format = mmd->audio_format;
472 if (format >= 0)
473 af = afl + format;
474 else
475 for (i = 0; senders[i].name; i++)
476 senders[i].shutdown_clients();
477 if (vss_next() && af) {
478 vss_eof();
479 return vss_compute_timeout();
480 }
481 if (vss_paused() || vss_repos()) {
482 for (i = 0; senders[i].name; i++)
483 senders[i].shutdown_clients();
484 if (af) {
485 struct timeval now;
486 gettimeofday(&now, NULL);
487 if (!vss_paused() || mmd->chunks_sent)
488 tv_add(&mmd->afi.eof_tv, &now, &eof_barrier);
489 if (vss_repos())
490 tv_add(&now, &announce_tv, &data_send_barrier);
491 if (mmd->new_vss_status_flags & VSS_NOMORE)
492 mmd->new_vss_status_flags = VSS_NEXT;
493 }
494 mmd->chunks_sent = 0;
495 }
496 if (vss_repos()) {
497 mmd->new_vss_status_flags &= ~(VSS_REPOS);
498 mmd->current_chunk = mmd->repos_request;
499 }
500 ret = vss_compute_timeout();
501 if (!ret && !map && vss_playing() &&
502 !(mmd->new_vss_status_flags & VSS_NOMORE)) {
503 PARA_DEBUG_LOG("%s", "ready and playing, but no audio file\n");
504 vss_get_audio_file();
505 goto again;
506 }
507 return ret;
508 }
509
510 /**
511 * main sending function
512 *
513 * This function gets called from para_server as soon as the next chunk of
514 * data should be pushed out. It first calls the read_chunk() function of
515 * the current audio format handler to obtain a pointer to the data to be
516 * sent out as well as its length. This information is then passed to each
517 * supported sender's send() function which does the actual sending.
518 */
519 void vss_send_chunk(void)
520 {
521 int i;
522 struct audio_format_handler *af;
523 ssize_t pos;
524 size_t len;
525 struct timeval now, due;
526
527 if (mmd->audio_format < 0 || !map || !vss_playing())
528 return;
529 af = &afl[mmd->audio_format];
530 gettimeofday(&now, NULL);
531 vss_next_chunk_time(&due);
532 if (tv_diff(&due, &now, NULL) > 0)
533 return;
534 if (chk_barrier("eof", &now, &eof_barrier, &due, 1) < 0)
535 return;
536 if (chk_barrier("data send", &now, &data_send_barrier,
537 &due, 1) < 0)
538 return;
539 mmd->new_vss_status_flags &= ~VSS_REPOS;
540 if (mmd->current_chunk >= mmd->afi.chunks_total) { /* eof */
541 mmd->new_vss_status_flags |= VSS_NEXT;
542 return vss_eof();
543 }
544 pos = mmd->afi.chunk_table[mmd->current_chunk];
545 len = mmd->afi.chunk_table[mmd->current_chunk + 1] - pos;
546 /*
547 * We call the send function also in case of empty chunks as they
548 * might have still some data queued which can be sent in this case.
549 */
550 if (!mmd->chunks_sent) {
551 struct timeval tmp;
552 gettimeofday(&mmd->stream_start, NULL);
553 tv_scale(mmd->current_chunk, &mmd->afi.chunk_tv, &tmp);
554 mmd->offset = tv2ms(&tmp);
555 mmd->events++;
556 }
557 for (i = 0; senders[i].name; i++)
558 senders[i].send(mmd->current_chunk, mmd->chunks_sent,
559 map + pos, len);
560 mmd->new_vss_status_flags |= VSS_PLAYING;
561 mmd->chunks_sent++;
562 mmd->current_chunk++;
563 }