]> git.tuebingen.mpg.de Git - paraslash.git/blob - vss.c
e12176d2df3ce08eeb457aa05d10a418206d59f8
[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/mman.h> /* mmap */
27 #include <sys/time.h> /* gettimeofday */
28 #include "server.cmdline.h"
29 #include "afs.h"
30 #include "vss.h"
31 #include "send.h"
32 #include "error.h"
33 #include "string.h"
34 #include "fd.h"
35
36 extern const char *status_item_list[];
37
38 static struct timeval announce_tv;
39 static struct timeval data_send_barrier;
40 static struct timeval eof_barrier;
41 static struct timeval autoplay_barrier;
42
43 extern struct misc_meta_data *mmd;
44 extern struct audio_file_selector selectors[];
45 extern struct sender senders[];
46 static char *inbuf;
47 static size_t inbuf_size;
48
49 static FILE *audio_file = NULL;
50 static char *map;
51
52 #if 1
53         void mp3_init(struct audio_format_handler *);
54 #endif
55
56 #ifdef HAVE_OGGVORBIS
57         void ogg_init(struct audio_format_handler *);
58 #endif
59 #ifdef HAVE_FAAD
60         void aac_afh_init(struct audio_format_handler *);
61 #endif
62
63 /**
64  * the list of supported  audio formats
65  */
66 static struct audio_format_handler afl[] = {
67 #if 1
68         {
69                 .name = "mp3",
70                 .init = mp3_init,
71         },
72 #endif
73 #ifdef HAVE_OGGVORBIS
74         {
75                 .name = "ogg",
76                 .init = ogg_init,
77         },
78 #endif
79 #ifdef HAVE_FAAD
80         {
81                 .name = "aac",
82                 .init = aac_afh_init,
83         },
84 #endif
85         {
86                 .name = NULL,
87         }
88 };
89
90 /** iterate over each supported audio format */
91 #define FOR_EACH_AUDIO_FORMAT(i) for (i = 0; afl[i].name; i++)
92
93
94
95 /**
96  * check if vss status flag \a P (playing) is set
97  *
98  * \return greater than zero if playing, zero otherwise.
99  *
100  */
101 unsigned int vss_playing(void)
102 {
103         return mmd->new_vss_status_flags & VSS_PLAYING;
104 }
105
106 /**
107  * check if \a N (next) status flag is set
108  *
109  * \return greater than zero if set, zero if not.
110  *
111  */
112 unsigned int vss_next(void)
113 {
114         return mmd->new_vss_status_flags & VSS_NEXT;
115 }
116
117 /**
118  * check if a reposition request is pending
119  *
120  * \return greater than zero if true, zero otherwise.
121  *
122  */
123 unsigned int vss_repos(void)
124 {
125         return mmd->new_vss_status_flags & VSS_REPOS;
126 }
127
128 /**
129  * check if the vss is currently paused
130  *
131  * \return greater than zero if paused, zero otherwise.
132  *
133  */
134 unsigned int vss_paused(void)
135 {
136         return !(mmd->new_vss_status_flags & VSS_NEXT)
137                 && !(mmd->new_vss_status_flags & VSS_PLAYING);
138 }
139
140 /**
141  * get the name of the given audio format
142  * \param i the audio format number
143  *
144  * This returns a pointer to statically allocated memory so it
145  * must not be freed by the caller.
146  */
147 const char *audio_format_name(int i)
148 {
149         return i >= 0?  afl[i].name : "(none)";
150 }
151
152 /**
153  * initialize the virtual streaming system
154  *
155  * Call the init functions of all supported audio format handlers and
156  * initialize all supported senders.
157  */
158 void vss_init(void)
159 {
160         int i;
161         char *hn = para_hostname(), *home = para_homedir();
162
163         PARA_DEBUG_LOG("supported audio formats: %s\n",
164                 SUPPORTED_AUDIO_FORMATS);
165         for (i = 0; afl[i].name; i++) {
166                 PARA_NOTICE_LOG("initializing %s handler\n",
167                         afl[i].name);
168                 afl[i].init(&afl[i]);
169         }
170         ms2tv(conf.announce_time_arg, &announce_tv);
171         PARA_INFO_LOG("announce timeval: %lums\n", tv2ms(&announce_tv));
172         for (i = 0; senders[i].name; i++) {
173                 PARA_NOTICE_LOG("initializing %s sender\n", senders[i].name);
174                 senders[i].init(&senders[i]);
175         }
176         free(hn);
177         free(home);
178         if (conf.autoplay_given) {
179                 struct timeval now, tmp;
180                 mmd->vss_status_flags |= VSS_PLAYING;
181                 mmd->new_vss_status_flags |= VSS_PLAYING;
182                 gettimeofday(&now, NULL);
183                 ms2tv(conf.autoplay_delay_arg, &tmp);
184                 tv_add(&now, &tmp, &autoplay_barrier);
185         }
186 }
187
188 static int get_file_info(int i)
189 {
190         return  afl[i].get_file_info(audio_file, map, mmd->size, &mmd->afi);
191 }
192
193 /**
194  * guess the audio format judging from filename
195  *
196  * \param name the filename
197  *
198  * \return This function returns -1 if it has no idea what kind of audio
199  * file this might be. Otherwise the (non-negative) number of the audio format
200  * is returned.
201  */
202 int guess_audio_format(const char *name)
203 {
204         int i,j, len = strlen(name);
205
206         FOR_EACH_AUDIO_FORMAT(i) {
207                 for (j = 0; afl[i].suffixes[j]; j++) {
208                         const char *p = afl[i].suffixes[j];
209                         int plen = strlen(p);
210                         if (len < plen + 1)
211                                 continue;
212                         if (name[len - plen - 1] != '.')
213                                 continue;
214                         if (strcasecmp(name + len - plen, p))
215                                 continue;
216 //                      PARA_DEBUG_LOG("might be %s\n", audio_format_name(i));
217                         return i;
218                 }
219         }
220         return -1;
221 }
222
223 static int get_audio_format(int omit)
224 {
225         int i;
226
227         FOR_EACH_AUDIO_FORMAT(i) {
228                 if (i == omit)
229                         continue;
230                 rewind(audio_file);
231                 if (get_file_info(i) > 0)
232                         return i;
233                 rewind(audio_file);
234         }
235         return -E_AUDIO_FORMAT;
236 }
237
238 /*
239  * upddate shared mem
240  */
241 static int update_mmd(void)
242 {
243         int i, fd = fileno(audio_file);
244         struct stat file_status;
245
246         if (fstat(fd, &file_status) == -1)
247                 return -E_FSTAT;
248         mmd->size = file_status.st_size;
249         mmd->mtime = file_status.st_mtime;
250         map = para_mmap(file_status.st_size, PROT_READ, MAP_PRIVATE, fd, 0);
251         i = guess_audio_format(mmd->filename);
252         if (i < 0 || get_file_info(i) < 0)
253                 i = get_audio_format(i);
254         if (i < 0)
255                 return i;
256         mmd->audio_format = i;
257         mmd->chunks_sent = 0;
258         mmd->current_chunk = 0;
259         mmd->offset = 0;
260         mmd->events++;
261         PARA_NOTICE_LOG("next audio file: %s\n", mmd->filename);
262         return 1;
263 }
264
265 static void vss_get_audio_file(void)
266 {
267         char **sl = selectors[mmd->selector_num].get_audio_file_list(10);
268         int i;
269
270         if (!sl)
271                 goto err_out;
272         for (i = 0; sl[i]; i++) {
273                 struct timeval now;
274                 PARA_INFO_LOG("trying %s\n", sl[i]);
275                 if (strlen(sl[i]) >= _POSIX_PATH_MAX)
276                         continue;
277                 audio_file = fopen(sl[i], "r");
278                 if (!audio_file)
279                         continue;
280                 strcpy(mmd->filename, sl[i]);
281                 if (update_mmd() < 0) {
282                         fclose(audio_file);
283                         audio_file = NULL;
284                         continue;
285                 }
286                 mmd->num_played++;
287                 if (selectors[mmd->selector_num].update_audio_file)
288                         selectors[mmd->selector_num].update_audio_file(sl[i]);
289                 PARA_DEBUG_LOG("%s", "success\n");
290                 mmd->new_vss_status_flags &= (~VSS_NEXT);
291                 gettimeofday(&now, NULL);
292                 tv_add(&now, &announce_tv, &data_send_barrier);
293
294                 goto free;
295         }
296         PARA_ERROR_LOG("%s", "no valid files found\n");
297 err_out:
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() || !audio_file)
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(struct audio_format_handler *af)
366 {
367         struct timeval now;
368         int i;
369         char *tmp;
370
371         if (!af || !audio_file) {
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         fclose(audio_file);
380         audio_file = NULL;
381         mmd->audio_format = -1;
382         af = NULL;
383         mmd->chunks_sent = 0;
384         mmd->offset = 0;
385         mmd->afi.seconds_total = 0;
386         free(mmd->afi.chunk_table);
387         mmd->afi.chunk_table = NULL;
388         free(mmd->afi.header);
389         mmd->afi.header = NULL;
390         tmp  = make_message("%s:\n%s:\n%s:\n", status_item_list[SI_AUDIO_INFO1],
391                 status_item_list[SI_AUDIO_INFO2], status_item_list[SI_AUDIO_INFO3]);
392         strcpy(mmd->afi.info_string, tmp);
393         free(tmp);
394         tmp  = make_message("%s:\n%s:\n%s:\n", status_item_list[SI_DBINFO1],
395                 status_item_list[SI_DBINFO2], status_item_list[SI_DBINFO3]);
396         strcpy(mmd->selector_info, tmp);
397         free(tmp);
398         mmd->filename[0] = '\0';
399         mmd->size = 0;
400         mmd->events++;
401 }
402
403 /**
404  * get the header and of the current audio file
405  *
406  * \param header_len the length of the header is stored here
407  *
408  * \return a pointer to a buffer containing the header, or NULL, if no audio
409  * file is selected or if the current audio format does not need special header
410  * treamtment.
411  *
412  */
413 char *vss_get_header(int *header_len)
414 {
415         if (mmd->audio_format < 0)
416                 return NULL;
417         *header_len = mmd->afi.header_len;
418         return mmd->afi.header;
419 }
420
421 /**
422  * get the list of all supported audio formats
423  *
424  * \return a space separated list of all supported audio formats
425  * It is not allocated at runtime, i.e. there is no need to free
426  * the returned string in the caller.
427  */
428 const char *supported_audio_formats(void)
429 {
430         return SUPPORTED_AUDIO_FORMATS;
431 }
432
433 /**
434  * get the chunk time of the current audio file
435  *
436  * \return a pointer to a struct containing the chunk time, or NULL,
437  * if currently no audio file is selected.
438  */
439 struct timeval *vss_chunk_time(void)
440 {
441         if (mmd->audio_format < 0)
442                 return NULL;
443         return &mmd->afi.chunk_tv;
444 }
445
446 /**
447  * compute the timeout for para_server's main select-loop
448  *
449  * This function gets called from para_server to determine the timeout value
450  * for its main select loop.
451  *
452  * Before the timeout is computed, the current vss status flags are evaluated
453  * and acted upon by calling appropriate functions from the lower layers.
454  * Possible actions include
455  *
456  *      - request a new file list from the current audio file selector
457  *      - shutdown of all senders (stop/pause command)
458  *      - reposition the stream (ff/jmp command)
459  *
460  * \return A pointer to a struct timeval containing the timeout for the next
461  * chunk of data to be sent, or NULL if we're not sending right now.
462  */
463 struct timeval *vss_preselect(void)
464 {
465         struct audio_format_handler *af = NULL;
466         int i, format;
467         struct timeval *ret;
468 again:
469         format = mmd->audio_format;
470         if (format >= 0)
471                 af = afl + format;
472         else
473                 for (i = 0; senders[i].name; i++)
474                         senders[i].shutdown_clients();
475         if (vss_next() && af) {
476                 vss_eof(af);
477                 return vss_compute_timeout();
478         }
479         if (vss_paused() || vss_repos()) {
480                 for (i = 0; senders[i].name; i++)
481                         senders[i].shutdown_clients();
482                 if (af) {
483                         struct timeval now;
484                         gettimeofday(&now, NULL);
485                         if (!vss_paused() || mmd->chunks_sent)
486                                 tv_add(&mmd->afi.eof_tv, &now, &eof_barrier);
487                         if (vss_repos())
488                                 tv_add(&now, &announce_tv, &data_send_barrier);
489                         if (mmd->new_vss_status_flags & VSS_NOMORE)
490                                 mmd->new_vss_status_flags = VSS_NEXT;
491                 }
492                 mmd->chunks_sent = 0;
493         }
494         if (vss_repos()) {
495                 mmd->new_vss_status_flags &= ~(VSS_REPOS);
496                 mmd->current_chunk = mmd->repos_request;
497         }
498         ret = vss_compute_timeout();
499         if (!ret && !audio_file && vss_playing() &&
500                         !(mmd->new_vss_status_flags & VSS_NOMORE)) {
501                 PARA_DEBUG_LOG("%s", "ready and playing, but no audio file\n");
502                 vss_get_audio_file();
503                 goto again;
504         }
505         return ret;
506 }
507
508 /**
509  * read a chunk of data from the current audio file
510  *
511  * \return The length of the chunk on success, zero on end of file, negative on
512  * errors.  Note: If the current chunk is of length zero, but the end of the
513  * file is not yet reached, this function returns -E_EMPTY_CHUNK.
514  */
515 ssize_t vss_read_chunk(void)
516 {
517         ssize_t len;
518         size_t pos;
519         int ret;
520         long unsigned cc = mmd->current_chunk;
521
522         if (cc >= mmd->afi.chunks_total) /* eof */
523                 return 0;
524         len = mmd->afi.chunk_table[cc + 1] - mmd->afi.chunk_table[cc];
525         if (!len) /* nothing to send for this run */
526                 return -E_EMPTY_CHUNK;
527         pos = mmd->afi.chunk_table[cc];
528         if (inbuf_size < len) {
529                 PARA_INFO_LOG("increasing inbuf for chunk #%lu/%lu to %zu bytes\n",
530                         cc, mmd->afi.chunks_total, len);
531                 inbuf = para_realloc(inbuf, len);
532                 inbuf_size = len;
533         }
534         ret = para_fseek(audio_file, pos, SEEK_SET);
535         if (ret < 0)
536                 return ret;
537         return para_fread(inbuf, len, 1, audio_file);
538 }
539
540 /**
541  * main sending function
542  *
543  * This function gets called from para_server as soon as the next chunk of
544  * data should be pushed out. It first calls the read_chunk() function of
545  * the current audio format handler to obtain a pointer to the data to be
546  * sent out as well as its length. This information is then passed to each
547  * supported sender's send() function which does the actual sending.
548  */
549 void vss_send_chunk(void)
550 {
551         int i;
552         struct audio_format_handler *af;
553         ssize_t ret;
554         struct timeval now, due;
555
556         if (mmd->audio_format < 0 || !audio_file || !vss_playing())
557                 return;
558         af = &afl[mmd->audio_format];
559         gettimeofday(&now, NULL);
560         vss_next_chunk_time(&due);
561         if (tv_diff(&due, &now, NULL) > 0)
562                 return;
563         if (chk_barrier("eof", &now, &eof_barrier, &due, 1) < 0)
564                 return;
565         if (chk_barrier("data send", &now, &data_send_barrier,
566                         &due, 1) < 0)
567                 return;
568         ret= vss_read_chunk();
569         mmd->new_vss_status_flags &= ~VSS_REPOS;
570         if (!ret || (ret < 0 && ret != -E_EMPTY_CHUNK)) {
571                 if (ret < 0) {
572                         mmd->new_vss_status_flags = VSS_NEXT;
573                         PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
574                 } else
575                         mmd->new_vss_status_flags |= VSS_NEXT;
576                 vss_eof(af);
577                 return;
578         }
579         /*
580          * We call the send function also in case of empty chunks as they
581          * might have still some data queued which can be sent in this case.
582          */
583         if (ret < 0)
584                 ret = 0;
585         if (!mmd->chunks_sent) {
586                 struct timeval tmp;
587                 gettimeofday(&mmd->stream_start, NULL);
588                 tv_scale(mmd->current_chunk, &mmd->afi.chunk_tv, &tmp);
589                 mmd->offset = tv2ms(&tmp);
590                 mmd->events++;
591         }
592         for (i = 0; senders[i].name; i++)
593                 senders[i].send(mmd->current_chunk, mmd->chunks_sent, inbuf, ret);
594         mmd->new_vss_status_flags |= VSS_PLAYING;
595         mmd->chunks_sent++;
596         mmd->current_chunk++;
597 }