]> git.tuebingen.mpg.de Git - paraslash.git/blob - vss.c
3d54f16cc0e6911f26ed9500832540f0b164a273
[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 /**
48  * check if vss status flag \a P (playing) is set
49  *
50  * \return greater than zero if playing, zero otherwise.
51  *
52  */
53 unsigned int vss_playing(void)
54 {
55         return mmd->new_vss_status_flags & VSS_PLAYING;
56 }
57
58 /**
59  * check if \a N (next) status flag is set
60  *
61  * \return greater than zero if set, zero if not.
62  *
63  */
64 unsigned int vss_next(void)
65 {
66         return mmd->new_vss_status_flags & VSS_NEXT;
67 }
68
69 /**
70  * check if a reposition request is pending
71  *
72  * \return greater than zero if true, zero otherwise.
73  *
74  */
75 unsigned int vss_repos(void)
76 {
77         return mmd->new_vss_status_flags & VSS_REPOS;
78 }
79
80 /**
81  * check if the vss is currently paused
82  *
83  * \return greater than zero if paused, zero otherwise.
84  *
85  */
86 unsigned int vss_paused(void)
87 {
88         return !(mmd->new_vss_status_flags & VSS_NEXT)
89                 && !(mmd->new_vss_status_flags & VSS_PLAYING);
90 }
91
92 /**
93  * initialize the virtual streaming system
94  *
95  * Call the init functions of all supported audio format handlers and
96  * initialize all supported senders.
97  */
98 void vss_init(void)
99 {
100         int i;
101         char *hn = para_hostname(), *home = para_homedir();
102         long unsigned announce_time = conf.announce_time_arg > 0?
103                         conf.announce_time_arg : 300,
104                 autoplay_delay = conf.autoplay_delay_arg > 0?
105                         conf.autoplay_delay_arg : 0;
106         afh_init();
107         ms2tv(announce_time, &announce_tv);
108         PARA_INFO_LOG("announce timeval: %lums\n", tv2ms(&announce_tv));
109         for (i = 0; senders[i].name; i++) {
110                 PARA_NOTICE_LOG("initializing %s sender\n", senders[i].name);
111                 senders[i].init(&senders[i]);
112         }
113         free(hn);
114         free(home);
115         if (conf.autoplay_given) {
116                 struct timeval now, tmp;
117                 mmd->vss_status_flags |= VSS_PLAYING;
118                 mmd->new_vss_status_flags |= VSS_PLAYING;
119                 gettimeofday(&now, NULL);
120                 ms2tv(autoplay_delay, &tmp);
121                 tv_add(&now, &tmp, &autoplay_barrier);
122         }
123 }
124
125 static int chk_barrier(const char *bname, const struct timeval *now,
126                 const struct timeval *barrier, struct timeval *diff,
127                 int print_log)
128 {
129         long ms;
130
131         if (tv_diff(now, barrier, diff) > 0)
132                 return 1;
133         ms = tv2ms(diff);
134         if (print_log && ms)
135                 PARA_DEBUG_LOG("%s barrier: %lims left\n", bname, ms);
136         return -1;
137 }
138
139 static void vss_next_chunk_time(struct timeval *due)
140 {
141         struct timeval tmp;
142
143         tv_scale(mmd->chunks_sent, &mmd->afi.chunk_tv, &tmp);
144         tv_add(&tmp, &mmd->stream_start, due);
145 }
146
147 /*
148  * != NULL: timeout for next chunk
149  * NULL: nothing to do
150  */
151 static struct timeval *vss_compute_timeout(void)
152 {
153         static struct timeval the_timeout;
154         struct timeval now, next_chunk;
155
156         if (vss_next() && mmd->audio_format >= 0) {
157                 /* only sleep a bit, nec*/
158                 the_timeout.tv_sec = 0;
159                 the_timeout.tv_usec = 100;
160                 return &the_timeout;
161         }
162         gettimeofday(&now, NULL);
163         if (chk_barrier("autoplay_delay", &now, &autoplay_barrier,
164                         &the_timeout, 1) < 0)
165                 return &the_timeout;
166         if (chk_barrier("eof", &now, &eof_barrier, &the_timeout, 1) < 0)
167                 return &the_timeout;
168         if (chk_barrier("data send", &now, &data_send_barrier,
169                         &the_timeout, 1) < 0)
170                 return &the_timeout;
171         if (mmd->audio_format < 0 || !vss_playing() || !map)
172                 return NULL;
173         vss_next_chunk_time(&next_chunk);
174         if (chk_barrier(audio_format_name(mmd->audio_format), &now, &next_chunk,
175                         &the_timeout, 0) < 0)
176                 return &the_timeout;
177         /* chunk is due or bof */
178         the_timeout.tv_sec = 0;
179         the_timeout.tv_usec = 0;
180         return &the_timeout;
181 }
182
183 static void vss_eof(void)
184 {
185         struct timeval now;
186         int i;
187         char *tmp;
188
189         PARA_NOTICE_LOG("EOF\n");
190         if (!map) {
191                 for (i = 0; senders[i].name; i++)
192                         senders[i].shutdown_clients();
193                 return;
194         }
195         gettimeofday(&now, NULL);
196         tv_add(&mmd->afi.eof_tv, &now, &eof_barrier);
197         munmap(map, mmd->size);
198         map = NULL;
199         close(audio_file);
200         mmd->audio_format = -1;
201         mmd->chunks_sent = 0;
202         mmd->offset = 0;
203         mmd->afi.seconds_total = 0;
204         free(mmd->afi.chunk_table);
205         mmd->afi.chunk_table = NULL;
206         tmp  = make_message("%s:\n%s:\n%s:\n", status_item_list[SI_AUDIO_INFO1],
207                 status_item_list[SI_AUDIO_INFO2], status_item_list[SI_AUDIO_INFO3]);
208         strcpy(mmd->afi.info_string, tmp);
209         free(tmp);
210         tmp  = make_message("%s:\n%s:\n%s:\n", status_item_list[SI_DBINFO1],
211                 status_item_list[SI_DBINFO2], status_item_list[SI_DBINFO3]);
212         strcpy(mmd->selector_info, tmp);
213         free(tmp);
214         mmd->filename[0] = '\0';
215         mmd->size = 0;
216         mmd->events++;
217 }
218
219 /**
220  * Get the header of the current audio file.
221  *
222  * \param header_len the length of the header is stored here
223  *
224  * \return a pointer to a buffer containing the header, or NULL, if no audio
225  * file is selected or if the current audio format does not need special header
226  * treamtment.
227  *
228  */
229 char *vss_get_header(size_t *header_len)
230 {
231         if (mmd->audio_format < 0 || !map || !mmd->afi.header_len)
232                 return NULL;
233         *header_len = mmd->afi.header_len;
234         return map + mmd->afi.header_offset;
235 }
236
237 /**
238  * get the list of all supported audio formats
239  *
240  * \return a space separated list of all supported audio formats
241  * It is not allocated at runtime, i.e. there is no need to free
242  * the returned string in the caller.
243  */
244 const char *supported_audio_formats(void)
245 {
246         return SUPPORTED_AUDIO_FORMATS;
247 }
248
249 /**
250  * get the chunk time of the current audio file
251  *
252  * \return a pointer to a struct containing the chunk time, or NULL,
253  * if currently no audio file is selected.
254  */
255 struct timeval *vss_chunk_time(void)
256 {
257         if (mmd->audio_format < 0)
258                 return NULL;
259         return &mmd->afi.chunk_tv;
260 }
261
262 enum afs_socket_status {
263         AFS_SOCKET_READY,
264         AFS_SOCKET_CHECK_FOR_WRITE,
265         AFS_SOCKET_AFD_PENDING
266 };
267
268 static enum afs_socket_status afsss;
269
270 /**
271  * Compute the timeout for para_server's main select-loop.
272  *
273  * This function gets called from para_server to determine the timeout value
274  * for its main select loop.
275  *
276  * Before the timeout is computed, the current vss status flags are evaluated
277  * and acted upon by calling appropriate functions from the lower layers.
278  * Possible actions include
279  *
280  *      - request a new audio file from afs,
281  *      - shutdown of all senders (stop/pause command),
282  *      - reposition the stream (ff/jmp command).
283  *
284  * \return A pointer to a struct timeval containing the timeout for the next
285  * chunk of data to be sent, or NULL if we're not sending right now.
286  */
287 struct timeval *vss_preselect(fd_set *rfds, fd_set *wfds, int *max_fileno)
288 {
289         int i;
290         struct timeval *tv;
291
292         para_fd_set(afs_socket, rfds, max_fileno);
293         if (!map)
294                 for (i = 0; senders[i].name; i++)
295                         senders[i].shutdown_clients();
296         else {
297                 if (vss_next()) {
298                         vss_eof();
299                         return vss_compute_timeout();
300                 }
301         }
302         if (vss_paused() || vss_repos()) {
303                 for (i = 0; senders[i].name; i++)
304                         senders[i].shutdown_clients();
305                 if (map) {
306                         struct timeval now;
307                         gettimeofday(&now, NULL);
308                         if (!vss_paused() || mmd->chunks_sent)
309                                 tv_add(&mmd->afi.eof_tv, &now, &eof_barrier);
310                         if (vss_repos())
311                                 tv_add(&now, &announce_tv, &data_send_barrier);
312                         if (mmd->new_vss_status_flags & VSS_NOMORE)
313                                 mmd->new_vss_status_flags = VSS_NEXT;
314                 }
315                 mmd->chunks_sent = 0;
316         }
317         if (vss_repos()) {
318                 mmd->new_vss_status_flags &= ~(VSS_REPOS);
319                 mmd->current_chunk = mmd->repos_request;
320         }
321         tv = vss_compute_timeout();
322         if (tv)
323                 return tv;
324         if (!map && vss_playing() &&
325                         !(mmd->new_vss_status_flags & VSS_NOMORE)) {
326                 PARA_DEBUG_LOG("%s", "ready and playing, but no audio file\n");
327                 if (afsss == AFS_SOCKET_READY) {
328                         para_fd_set(afs_socket, wfds, max_fileno);
329                         afsss = AFS_SOCKET_CHECK_FOR_WRITE;
330                 }
331         }
332         return tv;
333 }
334
335 static int recv_afs_msg(int *fd, uint32_t *code, uint32_t *data)
336 {
337         char control[255], buf[8];
338         struct msghdr msg = {.msg_iov = NULL};
339         struct cmsghdr *cmsg;
340         struct iovec iov;
341         int ret = 0;
342
343         iov.iov_base = buf;
344         iov.iov_len = sizeof(buf);
345         msg.msg_iov = &iov;
346         msg.msg_iovlen = 1;
347         msg.msg_control = control;
348         msg.msg_controllen = sizeof(control);
349         memset(buf, 0, sizeof(buf));
350         ret = recvmsg(afs_socket, &msg, 0);
351         if (ret < 0)
352                 return -ERRNO_TO_PARA_ERROR(errno);
353         if (iov.iov_len != sizeof(buf))
354                 return -E_SHORT_AFS_READ;
355         *code = *(uint32_t*)buf;
356         *data =  *(uint32_t*)(buf + 4);
357         cmsg = CMSG_FIRSTHDR(&msg);
358         for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
359                 if (cmsg->cmsg_level != SOL_SOCKET
360                         || cmsg->cmsg_type != SCM_RIGHTS)
361                 continue;
362                 if ((cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int) != 1)
363                 continue;
364                 *fd = *(int *)CMSG_DATA(cmsg);
365         }
366         return 1;
367 }
368
369 static void recv_afs_result(void)
370 {
371         int ret, passed_fd = -1, shmid;
372         uint32_t afs_code = 0, afs_data = 0;
373         struct stat statbuf;
374         struct timeval now;
375
376         PARA_NOTICE_LOG("recv\n");
377         ret = recv_afs_msg(&passed_fd, &afs_code, &afs_data);
378         if (ret < 0)
379                 goto err;
380         PARA_NOTICE_LOG("got the fd: %d, code: %u, shmid: %u\n",
381                 passed_fd, afs_code, afs_data);
382         ret = -E_BAD_AFS_CODE;
383         if (afs_code != NEXT_AUDIO_FILE)
384                 goto err;
385         afsss = AFS_SOCKET_READY;
386         shmid = afs_data;
387         ret = load_afd(shmid, &mmd->afd);
388         if (ret < 0)
389                 goto err;
390         shm_destroy(shmid);
391         PARA_NOTICE_LOG("next audio file: %s (%lu chunks)\n", mmd->afd.path,
392                 mmd->afd.afhi.chunks_total);
393         ret = fstat(passed_fd, &statbuf);
394         if (ret < 0) {
395                 ret = -ERRNO_TO_PARA_ERROR(errno);
396                 goto err;
397         }
398         mmd->size = statbuf.st_size;
399         mmd->mtime = statbuf.st_mtime;
400         map = para_mmap(mmd->size, PROT_READ, MAP_PRIVATE,
401                 passed_fd, 0);
402         strcpy(mmd->filename, mmd->afd.path); /* FIXME: check length */
403         mmd->afi.header_len = 0; /* default: no header */
404         mmd->audio_format = mmd->afd.afsi.audio_format_id;
405         mmd->chunks_sent = 0;
406         mmd->current_chunk = 0;
407         mmd->offset = 0;
408         mmd->events++;
409         mmd->num_played++;
410         mmd->afi = mmd->afd.afhi;
411         mmd->new_vss_status_flags &= (~VSS_NEXT);
412         gettimeofday(&now, NULL);
413         tv_add(&now, &announce_tv, &data_send_barrier);
414         return;
415 err:
416         if (passed_fd >= 0)
417                 close(passed_fd);
418         PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
419 }
420
421 void vss_post_select(fd_set *rfds, fd_set *wfds)
422 {
423         int ret;
424
425         if (FD_ISSET(afs_socket, rfds))
426                 recv_afs_result();
427         if (afsss != AFS_SOCKET_CHECK_FOR_WRITE || !FD_ISSET(afs_socket, wfds))
428                 return;
429         PARA_NOTICE_LOG("requesting new socket\n");
430         ret = send_buffer(afs_socket, "new");
431         afsss = AFS_SOCKET_AFD_PENDING;
432 }
433
434 static void get_chunk(long unsigned chunk_num, char **buf, size_t *len)
435 {
436         size_t pos = mmd->afi.chunk_table[chunk_num];
437         *buf = map + pos;
438         *len = mmd->afi.chunk_table[chunk_num + 1] - pos;
439
440         if (chunk_num + 5 > mmd->afd.afhi.chunks_total)
441                 PARA_NOTICE_LOG("chunk %lu/%lu\n, len: %zu\n", chunk_num,
442                         mmd->afd.afhi.chunks_total, *len);
443 }
444
445 /**
446  * Get the data of the given chunk.
447  *
448  * \param chunk_num The number of the desired chunk.
449  * \param buf Chunk data.
450  * \param len Chunk length in bytes.
451  *
452  * \return Positive on success, negative on errors.
453  */
454 int vss_get_chunk(long unsigned chunk_num, char **buf, size_t *len)
455 {
456         if (mmd->audio_format < 0 || !map || !vss_playing())
457                 return -E_CHUNK;
458         if (chunk_num >= mmd->afi.chunks_total)
459                 return -E_CHUNK;
460         get_chunk(chunk_num, buf, len);
461         return 1;
462 }
463
464 /**
465  * main sending function
466  *
467  * This function gets called from para_server as soon as the next chunk of
468  * data should be pushed out. It first calls the read_chunk() function of
469  * the current audio format handler to obtain a pointer to the data to be
470  * sent out as well as its length. This information is then passed to each
471  * supported sender's send() function which does the actual sending.
472  */
473 void vss_send_chunk(void)
474 {
475         int i;
476         struct timeval now, due;
477         char *buf;
478         size_t len;
479
480         if (mmd->audio_format < 0 || !map || !vss_playing())
481                 return;
482         gettimeofday(&now, NULL);
483         vss_next_chunk_time(&due);
484         if (tv_diff(&due, &now, NULL) > 0)
485                 return;
486         if (chk_barrier("eof", &now, &eof_barrier, &due, 1) < 0)
487                 return;
488         if (chk_barrier("data send", &now, &data_send_barrier,
489                         &due, 1) < 0)
490                 return;
491         mmd->new_vss_status_flags &= ~VSS_REPOS;
492         if (mmd->current_chunk >= mmd->afi.chunks_total) { /* eof */
493                 mmd->new_vss_status_flags |= VSS_NEXT;
494                 return vss_eof();
495         }
496         /*
497          * We call the send function also in case of empty chunks as they
498          * might have still some data queued which can be sent in this case.
499          */
500         if (!mmd->chunks_sent) {
501                 struct timeval tmp;
502                 gettimeofday(&mmd->stream_start, NULL);
503                 tv_scale(mmd->current_chunk, &mmd->afi.chunk_tv, &tmp);
504                 mmd->offset = tv2ms(&tmp);
505                 mmd->events++;
506         }
507         get_chunk(mmd->current_chunk, &buf, &len);
508         for (i = 0; senders[i].name; i++)
509                 senders[i].send(mmd->current_chunk, mmd->chunks_sent, buf, len);
510         mmd->new_vss_status_flags |= VSS_PLAYING;
511         mmd->chunks_sent++;
512         mmd->current_chunk++;
513 }