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