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