]> git.tuebingen.mpg.de Git - paraslash.git/blob - vss.c
dbf8547c28e1bd091fac6f5a62e7f040e4bc8889
[paraslash.git] / vss.c
1 /*
2  * Copyright (C) 1997-2007 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file vss.c the virtual streaming system
8  *
9  * This contains the audio streaming code of para_server which is independent
10  * of the current audio format, audio file selector and of the activated
11  * senders.
12  */
13
14 #include <sys/mman.h> /* mmap */
15 #include <sys/time.h> /* gettimeofday */
16 #include <sys/types.h>
17 #include <dirent.h>
18
19 #include "para.h"
20 #include "error.h"
21 #include "string.h"
22 #include "afh.h"
23 #include "afs.h"
24 #include "server.h"
25 #include "net.h"
26 #include "server.cmdline.h"
27 #include "afs_common.h"
28 #include "vss.h"
29 #include "send.h"
30 #include "ipc.h"
31 #include "fd.h"
32
33 extern const char *status_item_list[];
34
35 static struct timeval announce_tv;
36 static struct timeval data_send_barrier;
37 static struct timeval eof_barrier;
38 static struct timeval autoplay_barrier;
39
40 extern struct misc_meta_data *mmd;
41 extern struct audio_file_selector selectors[];
42 extern struct sender senders[];
43
44 static int audio_file;
45 static char *map;
46
47 /* The mp3 audio format handler does not need any libs. */
48 void mp3_init(struct audio_format_handler *);
49
50 #ifdef HAVE_OGGVORBIS
51         void ogg_init(struct audio_format_handler *);
52 #endif
53 #ifdef HAVE_FAAD
54         void aac_afh_init(struct audio_format_handler *);
55 #endif
56
57 /**
58  * The list of supported  audio formats.
59  *
60  * We always define the full array of audio formats even if some audio formats
61  * were not compiled in. This is because for each audio file the number of its
62  * audio format is stored in the databse. We don't want that numbers to become
63  * stale just because the user installed a new version of paraslash that
64  * supports a different set of audio formats.
65  *
66  * It can still be easily detected whether an audio format is compiled in by
67  * checking if the init function pointer is not \p NULL.
68  */
69 static struct audio_format_handler afl[] = {
70         {
71                 .name = "mp3",
72                 .init = mp3_init,
73         },
74         {
75                 .name = "ogg",
76 #ifdef HAVE_OGGVORBIS
77                 .init = ogg_init,
78 #endif
79         },
80         {
81                 .name = "aac",
82 #ifdef HAVE_FAAD
83                 .init = aac_afh_init,
84 #endif
85         },
86         {
87                 .name = NULL,
88         }
89 };
90
91 static inline int next_audio_format(int format)
92 {
93         for (;;) {
94                 if (!afl[format].name)
95                         return format;
96                 format++;
97                 if (afl[format].init)
98                         return format;
99         }
100
101 }
102
103 /** Iterate over each supported audio format. */
104 #define FOR_EACH_AUDIO_FORMAT(i) for (i = 0; afl[i].name; i = next_audio_format(i))
105
106
107 /**
108  * check if vss status flag \a P (playing) is set
109  *
110  * \return greater than zero if playing, zero otherwise.
111  *
112  */
113 unsigned int vss_playing(void)
114 {
115         return mmd->new_vss_status_flags & VSS_PLAYING;
116 }
117
118 /**
119  * check if \a N (next) status flag is set
120  *
121  * \return greater than zero if set, zero if not.
122  *
123  */
124 unsigned int vss_next(void)
125 {
126         return mmd->new_vss_status_flags & VSS_NEXT;
127 }
128
129 /**
130  * check if a reposition request is pending
131  *
132  * \return greater than zero if true, zero otherwise.
133  *
134  */
135 unsigned int vss_repos(void)
136 {
137         return mmd->new_vss_status_flags & VSS_REPOS;
138 }
139
140 /**
141  * check if the vss is currently paused
142  *
143  * \return greater than zero if paused, zero otherwise.
144  *
145  */
146 unsigned int vss_paused(void)
147 {
148         return !(mmd->new_vss_status_flags & VSS_NEXT)
149                 && !(mmd->new_vss_status_flags & VSS_PLAYING);
150 }
151
152 /**
153  * Get the name of the given audio format.
154  *
155  * \param i The audio format number.
156  *
157  * This returns a pointer to statically allocated memory so it
158  * must not be freed by the caller.
159  */
160 const char *audio_format_name(int i)
161 {
162         //PARA_NOTICE_LOG("array size: %u¸ requested: %d\n", ARRAY_SIZE(afl), i);
163         assert(i < 0 || i < ARRAY_SIZE(afl) - 1);
164         return i >= 0?  afl[i].name : "(none)";
165 }
166
167 /**
168  * initialize the virtual streaming system
169  *
170  * Call the init functions of all supported audio format handlers and
171  * initialize all supported senders.
172  */
173 void vss_init(void)
174 {
175         int i;
176         char *hn = para_hostname(), *home = para_homedir();
177         long unsigned announce_time = conf.announce_time_arg > 0?
178                         conf.announce_time_arg : 300,
179                 autoplay_delay = conf.autoplay_delay_arg > 0?
180                         conf.autoplay_delay_arg : 0;
181
182         PARA_DEBUG_LOG("supported audio formats: %s\n",
183                 SUPPORTED_AUDIO_FORMATS);
184         FOR_EACH_AUDIO_FORMAT(i) {
185                 PARA_NOTICE_LOG("initializing %s handler\n",
186                         afl[i].name);
187                 afl[i].init(&afl[i]);
188         }
189         ms2tv(announce_time, &announce_tv);
190         PARA_INFO_LOG("announce timeval: %lums\n", tv2ms(&announce_tv));
191         for (i = 0; senders[i].name; i++) {
192                 PARA_NOTICE_LOG("initializing %s sender\n", senders[i].name);
193                 senders[i].init(&senders[i]);
194         }
195         free(hn);
196         free(home);
197         if (conf.autoplay_given) {
198                 struct timeval now, tmp;
199                 mmd->vss_status_flags |= VSS_PLAYING;
200                 mmd->new_vss_status_flags |= VSS_PLAYING;
201                 gettimeofday(&now, NULL);
202                 ms2tv(autoplay_delay, &tmp);
203                 tv_add(&now, &tmp, &autoplay_barrier);
204         }
205 }
206
207 static int get_file_info(int i)
208 {
209         return  afl[i].get_file_info(map, mmd->size, &mmd->afi);
210 }
211
212 /**
213  * guess the audio format judging from filename
214  *
215  * \param name the filename
216  *
217  * \return This function returns -1 if it has no idea what kind of audio
218  * file this might be. Otherwise the (non-negative) number of the audio format
219  * is returned.
220  */
221 int guess_audio_format(const char *name)
222 {
223         int i,j, len = strlen(name);
224
225         FOR_EACH_AUDIO_FORMAT(i) {
226                 for (j = 0; afl[i].suffixes[j]; j++) {
227                         const char *p = afl[i].suffixes[j];
228                         int plen = strlen(p);
229                         if (len < plen + 1)
230                                 continue;
231                         if (name[len - plen - 1] != '.')
232                                 continue;
233                         if (strcasecmp(name + len - plen, p))
234                                 continue;
235 //                      PARA_DEBUG_LOG("might be %s\n", audio_format_name(i));
236                         return i;
237                 }
238         }
239         return -E_BAD_AUDIO_FILE_SUFFIX;
240 }
241
242 static int get_audio_format(int omit)
243 {
244         int i;
245
246         FOR_EACH_AUDIO_FORMAT(i) {
247                 if (i == omit)
248                         continue;
249                 if (get_file_info(i) > 0)
250                         return i;
251         }
252         return -E_AUDIO_FORMAT;
253 }
254
255 /**
256  * Call get_file_info() to obtain an afhi structure.
257  *
258  * \param path The full path of the audio file.
259  * \param data Pointer to the contents of the (mapped) file.
260  * \param size The file size in bytes.
261  * \param afhi Result pointer.
262  *
263  * \return The number of the audio format on success, \p -E_AUDIO_FORMAT if no
264  * compiled in audio format handler is able to handler the file.
265  *
266  * This function tries to find an audio format handler that can interpret the
267  * file given by \a data and \a size.
268  *
269  * It first tries to determine the audio format from the filename given by \a
270  * path. If this doesn't work, all other audio format handlers are tried until
271  * one is found that can handle the file.
272  */
273 int compute_afhi(const char *path, char *data, size_t size,
274                 struct audio_format_info *afhi)
275 {
276         int ret, i, format = guess_audio_format(path);
277
278         if (format >= 0) {
279                 ret = afl[format].get_file_info(data, size, afhi);
280                 if (ret >= 0)
281                         return format;
282         }
283         FOR_EACH_AUDIO_FORMAT(i) {
284                 if (i == format) /* we already tried this one to no avail */
285                         continue;
286                 ret = afl[i].get_file_info(data, size, afhi);
287                 if (ret >= 0)
288                         return i;
289                 PARA_WARNING_LOG("%s\n", PARA_STRERROR(-ret));
290         }
291         return -E_AUDIO_FORMAT;
292 }
293
294 /*
295  * upddate shared mem
296  */
297 static int update_mmd(void)
298 {
299         int i;
300
301         i = guess_audio_format(mmd->filename);
302         if (i < 0 || get_file_info(i) < 0)
303                 i = get_audio_format(i);
304         if (i < 0)
305                 return i;
306         mmd->audio_format = i;
307         mmd->chunks_sent = 0;
308         mmd->current_chunk = 0;
309         mmd->offset = 0;
310         mmd->events++;
311         return 1;
312 }
313
314 static void vss_get_audio_file(void)
315 {
316         char **sl = selectors[mmd->selector_num].get_audio_file_list(10);
317         int i;
318         struct stat file_status;
319
320         if (!sl)
321                 goto err_out;
322         for (i = 0; sl[i]; i++) {
323                 struct timeval now;
324                 PARA_INFO_LOG("trying %s\n", sl[i]);
325                 if (strlen(sl[i]) >= _POSIX_PATH_MAX)
326                         continue;
327                 audio_file = open(sl[i], O_RDONLY);
328                 if (audio_file < 0)
329                         continue;
330                 if (fstat(audio_file, &file_status) == -1 ||
331                                 !file_status.st_size) {
332                         close(audio_file);
333                         continue;
334                 }
335                 mmd->size = file_status.st_size;
336                 mmd->mtime = file_status.st_mtime;
337                 map = para_mmap(mmd->size, PROT_READ, MAP_PRIVATE,
338                         audio_file, 0);
339                 strcpy(mmd->filename, sl[i]);
340                 mmd->afi.header_len = 0; /* default: no header */
341                 if (update_mmd() < 0) { /* invalid file */
342                         close(audio_file);
343                         munmap(map, mmd->size);
344                         map = NULL;
345                         continue;
346                 }
347                 mmd->num_played++;
348                 if (selectors[mmd->selector_num].update_audio_file)
349                         selectors[mmd->selector_num].update_audio_file(sl[i]);
350                 PARA_NOTICE_LOG("next audio file: %s\n", mmd->filename);
351                 mmd->new_vss_status_flags &= (~VSS_NEXT);
352                 gettimeofday(&now, NULL);
353                 tv_add(&now, &announce_tv, &data_send_barrier);
354                 goto free;
355         }
356 err_out:
357         PARA_ERROR_LOG("%s", "no valid files found\n");
358         mmd->new_vss_status_flags = VSS_NEXT;
359 free:
360         if (sl) {
361                 for (i = 0; sl[i]; i++)
362                         free(sl[i]);
363                 free(sl);
364         }
365 }
366
367 static int chk_barrier(const char *bname, const struct timeval *now,
368                 const struct timeval *barrier, struct timeval *diff,
369                 int print_log)
370 {
371         long ms;
372
373         if (tv_diff(now, barrier, diff) > 0)
374                 return 1;
375         ms = tv2ms(diff);
376         if (print_log && ms)
377                 PARA_DEBUG_LOG("%s barrier: %lims left\n", bname, ms);
378         return -1;
379 }
380
381 static void vss_next_chunk_time(struct timeval *due)
382 {
383         struct timeval tmp;
384
385         tv_scale(mmd->chunks_sent, &mmd->afi.chunk_tv, &tmp);
386         tv_add(&tmp, &mmd->stream_start, due);
387 }
388
389 /*
390  * != NULL: timeout for next chunk
391  * NULL: nothing to do
392  */
393 static struct timeval *vss_compute_timeout(void)
394 {
395         static struct timeval the_timeout;
396         struct timeval now, next_chunk;
397
398         if (vss_next() && mmd->audio_format >= 0) {
399                 /* only sleep a bit, nec*/
400                 the_timeout.tv_sec = 0;
401                 the_timeout.tv_usec = 100;
402                 return &the_timeout;
403         }
404         gettimeofday(&now, NULL);
405         if (chk_barrier("autoplay_delay", &now, &autoplay_barrier,
406                         &the_timeout, 1) < 0)
407                 return &the_timeout;
408         if (chk_barrier("eof", &now, &eof_barrier, &the_timeout, 1) < 0)
409                 return &the_timeout;
410         if (chk_barrier("data send", &now, &data_send_barrier,
411                         &the_timeout, 1) < 0)
412                 return &the_timeout;
413         if (mmd->audio_format < 0 || !vss_playing() || !map)
414                 return NULL;
415         vss_next_chunk_time(&next_chunk);
416         if (chk_barrier(afl[mmd->audio_format].name, &now, &next_chunk,
417                         &the_timeout, 0) < 0)
418                 return &the_timeout;
419         /* chunk is due or bof */
420         the_timeout.tv_sec = 0;
421         the_timeout.tv_usec = 0;
422         return &the_timeout;
423 }
424
425 static void vss_eof(void)
426 {
427         struct timeval now;
428         int i;
429         char *tmp;
430
431         if (!map) {
432                 for (i = 0; senders[i].name; i++)
433                         senders[i].shutdown_clients();
434                 return;
435         }
436         gettimeofday(&now, NULL);
437         tv_add(&mmd->afi.eof_tv, &now, &eof_barrier);
438         munmap(map, mmd->size);
439         map = NULL;
440         close(audio_file);
441         mmd->audio_format = -1;
442         mmd->chunks_sent = 0;
443         mmd->offset = 0;
444         mmd->afi.seconds_total = 0;
445         free(mmd->afi.chunk_table);
446         mmd->afi.chunk_table = NULL;
447         tmp  = make_message("%s:\n%s:\n%s:\n", status_item_list[SI_AUDIO_INFO1],
448                 status_item_list[SI_AUDIO_INFO2], status_item_list[SI_AUDIO_INFO3]);
449         strcpy(mmd->afi.info_string, tmp);
450         free(tmp);
451         tmp  = make_message("%s:\n%s:\n%s:\n", status_item_list[SI_DBINFO1],
452                 status_item_list[SI_DBINFO2], status_item_list[SI_DBINFO3]);
453         strcpy(mmd->selector_info, tmp);
454         free(tmp);
455         mmd->filename[0] = '\0';
456         mmd->size = 0;
457         mmd->events++;
458 }
459
460 /**
461  * Get the header of the current audio file.
462  *
463  * \param header_len the length of the header is stored here
464  *
465  * \return a pointer to a buffer containing the header, or NULL, if no audio
466  * file is selected or if the current audio format does not need special header
467  * treamtment.
468  *
469  */
470 char *vss_get_header(size_t *header_len)
471 {
472         if (mmd->audio_format < 0 || !map || !mmd->afi.header_len)
473                 return NULL;
474         *header_len = mmd->afi.header_len;
475         return map + mmd->afi.header_offset;
476 }
477
478 /**
479  * get the list of all supported audio formats
480  *
481  * \return a space separated list of all supported audio formats
482  * It is not allocated at runtime, i.e. there is no need to free
483  * the returned string in the caller.
484  */
485 const char *supported_audio_formats(void)
486 {
487         return SUPPORTED_AUDIO_FORMATS;
488 }
489
490 /**
491  * get the chunk time of the current audio file
492  *
493  * \return a pointer to a struct containing the chunk time, or NULL,
494  * if currently no audio file is selected.
495  */
496 struct timeval *vss_chunk_time(void)
497 {
498         if (mmd->audio_format < 0)
499                 return NULL;
500         return &mmd->afi.chunk_tv;
501 }
502
503 enum afs_socket_status {
504         AFS_SOCKET_READY,
505         AFS_SOCKET_CHECK_FOR_WRITE,
506         AFS_SOCKET_AFD_PENDING
507 };
508
509 static enum afs_socket_status afsss;
510
511 /**
512  * compute the timeout for para_server's main select-loop
513  *
514  * This function gets called from para_server to determine the timeout value
515  * for its main select loop.
516  *
517  * Before the timeout is computed, the current vss status flags are evaluated
518  * and acted upon by calling appropriate functions from the lower layers.
519  * Possible actions include
520  *
521  *      - request a new file list from the current audio file selector
522  *      - shutdown of all senders (stop/pause command)
523  *      - reposition the stream (ff/jmp command)
524  *
525  * \return A pointer to a struct timeval containing the timeout for the next
526  * chunk of data to be sent, or NULL if we're not sending right now.
527  */
528 struct timeval *vss_preselect(fd_set *rfds, fd_set *wfds, int *max_fileno)
529 {
530         struct audio_format_handler *af = NULL;
531         int i, format;
532         struct timeval *ret;
533
534         para_fd_set(afs_socket, rfds, max_fileno);
535
536 //again:
537         format = mmd->audio_format;
538         if (format >= 0)
539                 af = afl + format;
540         else
541                 for (i = 0; senders[i].name; i++)
542                         senders[i].shutdown_clients();
543         if (vss_next() && af) {
544                 vss_eof();
545                 return vss_compute_timeout();
546         }
547         if (vss_paused() || vss_repos()) {
548                 for (i = 0; senders[i].name; i++)
549                         senders[i].shutdown_clients();
550                 if (af) {
551                         struct timeval now;
552                         gettimeofday(&now, NULL);
553                         if (!vss_paused() || mmd->chunks_sent)
554                                 tv_add(&mmd->afi.eof_tv, &now, &eof_barrier);
555                         if (vss_repos())
556                                 tv_add(&now, &announce_tv, &data_send_barrier);
557                         if (mmd->new_vss_status_flags & VSS_NOMORE)
558                                 mmd->new_vss_status_flags = VSS_NEXT;
559                 }
560                 mmd->chunks_sent = 0;
561         }
562         if (vss_repos()) {
563                 mmd->new_vss_status_flags &= ~(VSS_REPOS);
564                 mmd->current_chunk = mmd->repos_request;
565         }
566         ret = vss_compute_timeout();
567         if (!ret && !map && vss_playing() &&
568                         !(mmd->new_vss_status_flags & VSS_NOMORE)) {
569                 PARA_DEBUG_LOG("%s", "ready and playing, but no audio file\n");
570                 //vss_get_audio_file();
571                 if (afsss == AFS_SOCKET_READY) {
572                         para_fd_set(afs_socket, wfds, max_fileno);
573                         afsss = AFS_SOCKET_CHECK_FOR_WRITE;
574                 }
575 //              goto again;
576         }
577         return ret;
578 }
579
580 static int recv_afs_msg(int *fd, uint32_t *code, uint32_t *data)
581 {
582         char control[255], buf[8];
583         struct msghdr msg = {.msg_iov = NULL};
584         struct cmsghdr *cmsg;
585         struct iovec iov;
586         int ret = 0;
587
588         iov.iov_base = buf;
589         iov.iov_len = sizeof(buf);
590         msg.msg_iov = &iov;
591         msg.msg_iovlen = 1;
592         msg.msg_control = control;
593         msg.msg_controllen = sizeof(control);
594         memset(buf, 0, sizeof(buf));
595         ret = recvmsg(afs_socket, &msg, 0);
596         if (ret < 0)
597                 return -ERRNO_TO_PARA_ERROR(errno);
598         if (iov.iov_len != sizeof(buf))
599                 return -E_SHORT_AFS_READ;
600         *code = *(uint32_t*)buf;
601         *data =  *(uint32_t*)(buf + 4);
602         cmsg = CMSG_FIRSTHDR(&msg);
603         for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
604                 if (cmsg->cmsg_level != SOL_SOCKET
605                         || cmsg->cmsg_type != SCM_RIGHTS)
606                 continue;
607                 if ((cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int) != 1)
608                 continue;
609                 *fd = *(int *)CMSG_DATA(cmsg);
610         }
611         return 1;
612 }
613
614 static void recv_afs_result(void)
615 {
616         int ret, passed_fd = -1, shmid;
617         uint32_t afs_code = 0, afs_data = 0;
618         struct stat statbuf;
619         struct timeval now;
620
621         ret = recv_afs_msg(&passed_fd, &afs_code, &afs_data);
622         if (ret < 0)
623                 goto err;
624         PARA_NOTICE_LOG("got the fd: %d, code: %u, shmid: %u\n",
625                 passed_fd, afs_code, afs_data);
626         ret = -E_BAD_AFS_CODE;
627         if (afs_code != NEXT_AUDIO_FILE)
628                 goto err;
629         afsss = AFS_SOCKET_READY;
630         shmid = afs_data;
631         ret = load_afd(shmid, &mmd->afd);
632         if (ret < 0)
633                 goto err;
634         shm_destroy(shmid);
635         PARA_NOTICE_LOG("next audio file: %s (%lu chunks)\n", mmd->afd.path,
636                 mmd->afd.afhi.chunks_total);
637         ret = fstat(passed_fd, &statbuf);
638         if (ret < 0) {
639                 ret = -ERRNO_TO_PARA_ERROR(errno);
640                 goto err;
641         }
642         mmd->size = statbuf.st_size;
643         mmd->mtime = statbuf.st_mtime;
644         map = para_mmap(mmd->size, PROT_READ, MAP_PRIVATE,
645                 passed_fd, 0);
646         strcpy(mmd->filename, mmd->afd.path); /* FIXME: check length */
647         mmd->afi.header_len = 0; /* default: no header */
648         mmd->audio_format = mmd->afd.afsi.audio_format_id;
649         mmd->chunks_sent = 0;
650         mmd->current_chunk = 0;
651         mmd->offset = 0;
652         mmd->events++;
653         mmd->num_played++;
654         mmd->afi = mmd->afd.afhi;
655         mmd->new_vss_status_flags &= (~VSS_NEXT);
656         gettimeofday(&now, NULL);
657         tv_add(&now, &announce_tv, &data_send_barrier);
658         return;
659 err:
660         if (passed_fd >= 0)
661                 close(passed_fd);
662         PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
663 }
664
665 void vss_post_select(fd_set *rfds, fd_set *wfds)
666 {
667         int ret;
668
669         if (FD_ISSET(afs_socket, rfds))
670                 recv_afs_result();
671         if (afsss != AFS_SOCKET_CHECK_FOR_WRITE || !FD_ISSET(afs_socket, wfds))
672                 return;
673         ret = send_buffer(afs_socket, "new");
674         afsss = AFS_SOCKET_AFD_PENDING;
675 }
676
677 static void get_chunk(long unsigned chunk_num, char **buf, size_t *len)
678 {
679         size_t pos = mmd->afi.chunk_table[chunk_num];
680         *buf = map + pos;
681         *len = mmd->afi.chunk_table[chunk_num + 1] - pos;
682 }
683
684 /**
685  * Get the data of the given chunk.
686  *
687  * \param chunk_num The number of the desired chunk.
688  * \param buf Chunk data.
689  * \param len Chunk length in bytes.
690  *
691  * \return Positive on success, negative on errors.
692  */
693 int vss_get_chunk(long unsigned chunk_num, char **buf, size_t *len)
694 {
695         if (mmd->audio_format < 0 || !map || !vss_playing())
696                 return -E_CHUNK;
697         if (chunk_num >= mmd->afi.chunks_total)
698                 return -E_CHUNK;
699         get_chunk(chunk_num, buf, len);
700         return 1;
701 }
702
703 /**
704  * main sending function
705  *
706  * This function gets called from para_server as soon as the next chunk of
707  * data should be pushed out. It first calls the read_chunk() function of
708  * the current audio format handler to obtain a pointer to the data to be
709  * sent out as well as its length. This information is then passed to each
710  * supported sender's send() function which does the actual sending.
711  */
712 void vss_send_chunk(void)
713 {
714         int i;
715         struct audio_format_handler *af;
716         struct timeval now, due;
717
718         if (mmd->audio_format < 0 || !map || !vss_playing())
719                 return;
720         af = &afl[mmd->audio_format];
721         gettimeofday(&now, NULL);
722         vss_next_chunk_time(&due);
723         if (tv_diff(&due, &now, NULL) > 0)
724                 return;
725         if (chk_barrier("eof", &now, &eof_barrier, &due, 1) < 0)
726                 return;
727         if (chk_barrier("data send", &now, &data_send_barrier,
728                         &due, 1) < 0)
729                 return;
730         mmd->new_vss_status_flags &= ~VSS_REPOS;
731         if (mmd->current_chunk >= mmd->afi.chunks_total) { /* eof */
732                 mmd->new_vss_status_flags |= VSS_NEXT;
733                 return vss_eof();
734         }
735         /*
736          * We call the send function also in case of empty chunks as they
737          * might have still some data queued which can be sent in this case.
738          */
739         if (!mmd->chunks_sent) {
740                 struct timeval tmp;
741                 gettimeofday(&mmd->stream_start, NULL);
742                 tv_scale(mmd->current_chunk, &mmd->afi.chunk_tv, &tmp);
743                 mmd->offset = tv2ms(&tmp);
744                 mmd->events++;
745         }
746         for (i = 0; senders[i].name; i++) {
747                 char *buf;
748                 size_t len;
749                 get_chunk(mmd->current_chunk, &buf, &len);
750                 senders[i].send(mmd->current_chunk, mmd->chunks_sent, buf, len);
751         }
752         mmd->new_vss_status_flags |= VSS_PLAYING;
753         mmd->chunks_sent++;
754         mmd->current_chunk++;
755 }