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