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