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