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