Makefile.in: switch from git-tar-tree to git-archive
[paraslash.git] / afs.c
1 /*
2 * Copyright (C) 1997-2006 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 afs.c audio file sending functions
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 "db.h"
29 #include "afh.h"
30 #include "afs.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
46 static FILE *audio_file = NULL;
47
48 #if 1
49 void mp3_init(struct audio_format_handler *);
50 #endif
51
52 #ifdef HAVE_OGGVORBIS
53 void ogg_init(struct audio_format_handler *);
54 #endif
55 #ifdef HAVE_FAAD
56 void aac_afh_init(struct audio_format_handler *);
57 #endif
58
59 /**
60 * the list of supported audio formats
61 */
62 static struct audio_format_handler afl[] = {
63 #if 1
64 {
65 .name = "mp3",
66 .init = mp3_init,
67 },
68 #endif
69 #ifdef HAVE_OGGVORBIS
70 {
71 .name = "ogg",
72 .init = ogg_init,
73 },
74 #endif
75 #ifdef HAVE_FAAD
76 {
77 .name = "aac",
78 .init = aac_afh_init,
79 },
80 #endif
81 {
82 .name = NULL,
83 }
84 };
85 #define FOR_EACH_AUDIO_FORMAT(i) for (i = 0; afl[i].name; i++)
86
87 /**
88 * check if audio file sender is playing
89 *
90 * \return greater than zero if playing, zero otherwise.
91 *
92 */
93 unsigned int afs_playing(void)
94 {
95 return mmd->new_afs_status_flags & AFS_PLAYING;
96 }
97
98 /**
99 * check if 'next' flag is set afs_status_flags
100 *
101 * \return greater than zero if set, zero if not.
102 *
103 */
104 unsigned int afs_next(void)
105 {
106 return mmd->new_afs_status_flags & AFS_NEXT;
107 }
108
109 /**
110 * check if a reposition request is pending
111 *
112 * \return greater than zero if true, zero otherwise.
113 *
114 */
115 unsigned int afs_repos(void)
116 {
117 return mmd->new_afs_status_flags & AFS_REPOS;
118 }
119
120 /**
121 * check if audio file sender is paused
122 *
123 * \return greater than zero if paused, zero otherwise.
124 *
125 */
126 unsigned int afs_paused(void)
127 {
128 return !(mmd->new_afs_status_flags & AFS_NEXT)
129 && !(mmd->new_afs_status_flags & AFS_PLAYING);
130 }
131
132 /**
133 * get the name of the given audio format
134 * \param i the audio format number
135 *
136 * This returns a pointer to statically allocated memory so it
137 * must not be freed by the caller.
138 */
139 const char *audio_format_name(int i)
140 {
141 return i >= 0? afl[i].name : "(none)";
142 }
143
144 /**
145 * initialize the audio file sender
146 *
147 * Call the init functions of all supported audio format handlers and
148 * initialize all supported senders.
149 */
150 void afs_init(void)
151 {
152 int i;
153 char *hn = para_hostname(), *home = para_homedir();
154
155 PARA_DEBUG_LOG("supported audio formats: %s\n",
156 SUPPORTED_AUDIO_FORMATS);
157 for (i = 0; afl[i].name; i++) {
158 PARA_NOTICE_LOG("initializing %s handler\n",
159 afl[i].name);
160 afl[i].init(&afl[i]);
161 }
162 ms2tv(conf.announce_time_arg, &announce_tv);
163 PARA_INFO_LOG("announce timeval: %lums\n", tv2ms(&announce_tv));
164 for (i = 0; senders[i].name; i++) {
165 PARA_NOTICE_LOG("initializing %s sender\n", senders[i].name);
166 senders[i].init(&senders[i]);
167 }
168 free(hn);
169 free(home);
170 if (conf.autoplay_given) {
171 struct timeval now, tmp;
172 mmd->afs_status_flags |= AFS_PLAYING;
173 mmd->new_afs_status_flags |= AFS_PLAYING;
174 gettimeofday(&now, NULL);
175 ms2tv(conf.autoplay_delay_arg, &tmp);
176 tv_add(&now, &tmp, &autoplay_barrier);
177 }
178 }
179
180 static int get_file_info(int i)
181 {
182 return afl[i].get_file_info(audio_file, mmd->audio_file_info,
183 &mmd->chunks_total, &mmd->seconds_total);
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 || !afl[i].get_file_info)
222 continue;
223 rewind(audio_file);
224 if (get_file_info(i) > 0)
225 return i;
226 rewind(audio_file);
227 }
228 return -E_AUDIO_FORMAT;
229 }
230
231 /*
232 * upddate shared mem
233 */
234 static int update_mmd(void)
235 {
236 int i;
237 struct stat file_status;
238
239 i = guess_audio_format(mmd->filename);
240 if (i < 0 || get_file_info(i) < 0)
241 i = get_audio_format(i);
242 if (i < 0)
243 return i;
244 mmd->audio_format = i;
245 mmd->chunks_sent = 0;
246 mmd->current_chunk = 0;
247 mmd->offset = 0;
248 if (fstat(fileno(audio_file), &file_status) == -1)
249 return -E_FSTAT;
250 mmd->size = file_status.st_size;
251 mmd->mtime = file_status.st_mtime;
252 mmd->events++;
253 PARA_NOTICE_LOG("next audio file: %s\n", mmd->filename);
254 return 1;
255 }
256
257 static void get_song(void)
258 {
259 char **sl = selectors[mmd->selector_num].get_audio_file_list(10);
260 int i;
261
262 if (!sl)
263 goto err_out;
264 for (i = 0; sl[i]; i++) {
265 struct timeval now;
266 PARA_INFO_LOG("trying %s\n", sl[i]);
267 if (strlen(sl[i]) >= _POSIX_PATH_MAX)
268 continue;
269 audio_file = fopen(sl[i], "r");
270 if (!audio_file)
271 continue;
272 strcpy(mmd->filename, sl[i]);
273 if (update_mmd() < 0) {
274 fclose(audio_file);
275 audio_file = NULL;
276 continue;
277 }
278 mmd->num_played++;
279 if (selectors[mmd->selector_num].update_audio_file)
280 selectors[mmd->selector_num].update_audio_file(sl[i]);
281 PARA_DEBUG_LOG("%s", "success\n");
282 mmd->new_afs_status_flags &= (~AFS_NEXT);
283 gettimeofday(&now, NULL);
284 tv_add(&now, &announce_tv, &data_send_barrier);
285
286 goto free;
287 }
288 PARA_ERROR_LOG("%s", "no valid files found\n");
289 err_out:
290 mmd->new_afs_status_flags = AFS_NEXT;
291 free:
292 if (sl) {
293 for (i = 0; sl[i]; i++)
294 free(sl[i]);
295 free(sl);
296 }
297 }
298
299 static int chk_barrier(const char *bname, const struct timeval *now,
300 const struct timeval *barrier, struct timeval *diff,
301 int print_log)
302 {
303 long ms;
304
305 if (tv_diff(now, barrier, diff) > 0)
306 return 1;
307 ms = tv2ms(diff);
308 if (print_log && ms)
309 PARA_DEBUG_LOG("%s barrier: %lims left\n", bname, ms);
310 return -1;
311 }
312
313 static void afs_next_chunk_time(struct timeval *due)
314 {
315 struct timeval tmp;
316
317 tv_scale(mmd->chunks_sent, &afl[mmd->audio_format].chunk_tv, &tmp);
318 tv_add(&tmp, &mmd->stream_start, due);
319 }
320
321 /*
322 * != NULL: timeout for next chunk
323 * NULL: nothing to do
324 */
325 static struct timeval *afs_compute_timeout(void)
326 {
327 static struct timeval the_timeout;
328 struct timeval now, next_chunk;
329
330 if (afs_next() && mmd->audio_format >= 0) {
331 /* only sleep a bit, nec*/
332 the_timeout.tv_sec = 0;
333 the_timeout.tv_usec = 100;
334 return &the_timeout;
335 }
336 gettimeofday(&now, NULL);
337 if (chk_barrier("autoplay_delay", &now, &autoplay_barrier,
338 &the_timeout, 1) < 0)
339 return &the_timeout;
340 if (chk_barrier("eof", &now, &eof_barrier, &the_timeout, 1) < 0)
341 return &the_timeout;
342 if (chk_barrier("data send", &now, &data_send_barrier,
343 &the_timeout, 1) < 0)
344 return &the_timeout;
345 if (mmd->audio_format < 0 || !afs_playing() || !audio_file)
346 return NULL;
347 afs_next_chunk_time(&next_chunk);
348 if (chk_barrier(afl[mmd->audio_format].name, &now, &next_chunk,
349 &the_timeout, 0) < 0)
350 return &the_timeout;
351 /* chunk is due or bof */
352 the_timeout.tv_sec = 0;
353 the_timeout.tv_usec = 0;
354 return &the_timeout;
355 }
356
357 static void afs_eof(struct audio_format_handler *af)
358 {
359 struct timeval now;
360 int i;
361 char *tmp;
362
363 if (!af || !audio_file) {
364 for (i = 0; senders[i].name; i++)
365 senders[i].shutdown_clients();
366 return;
367 }
368 gettimeofday(&now, NULL);
369 tv_add(&af->eof_tv, &now, &eof_barrier);
370 af->close_audio_file();
371 audio_file = NULL;
372 mmd->audio_format = -1;
373 af = NULL;
374 mmd->chunks_sent = 0;
375 mmd->offset = 0;
376 mmd->seconds_total = 0;
377 tmp = make_message("%s:\n%s:\n%s:\n", status_item_list[SI_AUDIO_INFO1],
378 status_item_list[SI_AUDIO_INFO2], status_item_list[SI_AUDIO_INFO3]);
379 strcpy(mmd->audio_file_info, tmp);
380 free(tmp);
381 tmp = make_message("%s:\n%s:\n%s:\n", status_item_list[SI_DBINFO1],
382 status_item_list[SI_DBINFO2], status_item_list[SI_DBINFO3]);
383 strcpy(mmd->selector_info, tmp);
384 free(tmp);
385 mmd->filename[0] = '\0';
386 mmd->size = 0;
387 mmd->events++;
388 }
389
390 /**
391 * get the header and of the current audio file
392 *
393 * \param header_len the length of the header is stored here
394 *
395 * \return a pointer to a buffer containing the header, or NULL, if no audio
396 * file is selected or if the current audio format does not need special header
397 * treamtment.
398 *
399 */
400 char *afs_get_header(int *header_len)
401 {
402 *header_len = 0;
403 if (mmd->audio_format < 0)
404 return NULL;
405 if (!afl[mmd->audio_format].get_header_info)
406 return NULL;
407 return afl[mmd->audio_format].get_header_info(header_len);
408 }
409 const char *supported_audio_formats(void)
410 {
411 return SUPPORTED_AUDIO_FORMATS;
412 }
413
414 /**
415 * get the chunk time of the current audio file
416 *
417 * \return a pointer to a struct containing the chunk time, or NULL,
418 * if currently no audio file is selected.
419 */
420 struct timeval *afs_chunk_time(void)
421 {
422 if (mmd->audio_format < 0)
423 return NULL;
424 return &afl[mmd->audio_format].chunk_tv;
425 }
426
427 /**
428 * compute the timeout for para_server's main select-loop
429 *
430 * This function gets called from para_server to determine the timeout value
431 * for its main select loop.
432 *
433 * Before the timeout is computed, the current afs status flags are evaluated
434 * and acted upon by calling appropriate functions from the lower layers.
435 * Possible actions include
436 *
437 * - request a new file list from the current audio file selector
438 * - shutdown of all senders (stop/pause command)
439 * - reposition the stream (ff/jmp command)
440 *
441 * \return A pointer to a struct timeval containing the timeout for the next
442 * chunk of data to be sent, or NULL if we're not sending right now.
443 */
444 struct timeval *afs_preselect(void)
445 {
446 struct audio_format_handler *af = NULL;
447 int i, format;
448 struct timeval *ret;
449 again:
450 format = mmd->audio_format;
451 if (format >= 0)
452 af = afl + format;
453 else
454 for (i = 0; senders[i].name; i++)
455 senders[i].shutdown_clients();
456 if (afs_next() && af) {
457 afs_eof(af);
458 return afs_compute_timeout();
459 }
460 if (afs_paused() || afs_repos()) {
461 for (i = 0; senders[i].name; i++)
462 senders[i].shutdown_clients();
463 if (af) {
464 struct timeval now;
465 gettimeofday(&now, NULL);
466 if (!afs_paused() || mmd->chunks_sent)
467 tv_add(&af->eof_tv, &now, &eof_barrier);
468 if (afs_repos())
469 tv_add(&now, &announce_tv, &data_send_barrier);
470 if (mmd->new_afs_status_flags & AFS_NOMORE)
471 mmd->new_afs_status_flags = AFS_NEXT;
472 }
473 mmd->chunks_sent = 0;
474 }
475 if (af && afs_repos() && mmd->current_chunk != mmd->repos_request)
476 af->reposition_stream(mmd->repos_request);
477 if (afs_repos()) {
478 mmd->new_afs_status_flags &= ~(AFS_REPOS);
479 mmd->current_chunk = mmd->repos_request;
480 }
481 ret = afs_compute_timeout();
482 if (!ret && !audio_file && afs_playing() &&
483 !(mmd->new_afs_status_flags & AFS_NOMORE)) {
484 PARA_DEBUG_LOG("%s", "ready and playing, but no audio file\n");
485 get_song();
486 goto again;
487 }
488 return ret;
489 }
490
491 /**
492 * afs_send_chunk - paraslash's main sending function
493 *
494 * This function gets called from para_server as soon as the next chunk of
495 * data should be pushed out. It first calls the read_chunk() function of
496 * the current audio format handler to obtain a pointer to the data to be
497 * sent out as well as its length. This information is then passed to each
498 * supported sender's send() function which does the actual sending.
499 *
500 * Return value: Positive return value on success, zero on eof and negative
501 * on errors.
502 */
503
504 void afs_send_chunk(void)
505 {
506 int i;
507 struct audio_format_handler *af;
508 char *buf;
509 ssize_t ret;
510 struct timeval now, due;
511
512 if (mmd->audio_format < 0 || !audio_file || !afs_playing())
513 return;
514 af = &afl[mmd->audio_format];
515 gettimeofday(&now, NULL);
516 afs_next_chunk_time(&due);
517 if (tv_diff(&due, &now, NULL) > 0)
518 return;
519 if (chk_barrier("eof", &now, &eof_barrier, &due, 1) < 0)
520 return;
521 if (chk_barrier("data send", &now, &data_send_barrier,
522 &due, 1) < 0)
523 return;
524 buf = af->read_chunk(mmd->current_chunk, &ret);
525 mmd->new_afs_status_flags &= ~AFS_REPOS;
526 if (!buf) {
527 if (ret < 0)
528 mmd->new_afs_status_flags = AFS_NEXT;
529 else
530 mmd->new_afs_status_flags |= AFS_NEXT;
531 afs_eof(af);
532 return;
533 }
534 if (!mmd->chunks_sent) {
535 struct timeval tmp;
536 gettimeofday(&mmd->stream_start, NULL);
537 tv_scale(mmd->current_chunk, &af->chunk_tv, &tmp);
538 mmd->offset = tv2ms(&tmp);
539 mmd->events++;
540 }
541 for (i = 0; senders[i].name; i++)
542 senders[i].send(mmd->current_chunk, mmd->chunks_sent, buf, ret);
543 mmd->new_afs_status_flags |= AFS_PLAYING;
544 mmd->chunks_sent++;
545 mmd->current_chunk++;
546 }