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