recv.h: Cosmetics.
[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 char *tmp;
190
191 mmd->stream_start = *now;
192 if (!vsst->map)
193 return;
194 if (mmd->new_vss_status_flags & VSS_NOMORE)
195 mmd->new_vss_status_flags = VSS_NEXT;
196 tv_add(&mmd->afd.afhi.eof_tv, now, &vsst->eof_barrier);
197 para_munmap(vsst->map, mmd->size);
198 vsst->map = NULL;
199 mmd->chunks_sent = 0;
200 mmd->offset = 0;
201 mmd->afd.afhi.seconds_total = 0;
202 mmd->afd.afhi.chunk_tv.tv_sec = 0;
203 mmd->afd.afhi.chunk_tv.tv_usec = 0;
204 free(mmd->afd.afhi.chunk_table);
205 mmd->afd.afhi.chunk_table = NULL;
206 tmp = make_message("%s:\n%s:\n%s:\n", status_item_list[SI_AUDIO_FILE_INFO],
207 status_item_list[SI_TAGINFO1], status_item_list[SI_TAGINFO2]);
208 strncpy(mmd->afd.afhi.info_string, tmp, sizeof(mmd->afd.afhi.info_string));
209 mmd->afd.afhi.info_string[sizeof(mmd->afd.afhi.info_string) - 1] = '\0';
210 make_empty_status_items(mmd->afd.verbose_ls_output);
211 free(tmp);
212 mmd->mtime = 0;
213 mmd->size = 0;
214 mmd->events++;
215 }
216
217 /**
218 * Get the list of all supported audio formats.
219 *
220 * \return Aa space separated list of all supported audio formats
221 * It is not allocated at runtime, i.e. there is no need to free
222 * the returned string in the caller.
223 */
224 const char *supported_audio_formats(void)
225 {
226 return SUPPORTED_AUDIO_FORMATS;
227 }
228
229 /**
230 * Get the chunk time of the current audio file.
231 *
232 * \return A pointer to a struct containing the chunk time, or NULL,
233 * if currently no audio file is selected.
234 */
235 struct timeval *vss_chunk_time(void)
236 {
237 if (mmd->afd.afhi.chunk_tv.tv_sec == 0 &&
238 mmd->afd.afhi.chunk_tv.tv_usec == 0)
239 return NULL;
240 return &mmd->afd.afhi.chunk_tv;
241 }
242
243 static int need_to_request_new_audio_file(struct vss_task *vsst)
244 {
245 if (vsst->map) /* have audio file */
246 return 0;
247 if (!vss_playing()) /* don't need one */
248 return 0;
249 if (mmd->new_vss_status_flags & VSS_NOMORE)
250 return 0;
251 if (vsst->afsss == AFS_SOCKET_AFD_PENDING) /* already requested one */
252 return 0;
253 return 1;
254 }
255
256 /**
257 * Compute the timeout for para_server's main select-loop.
258 *
259 * This function gets called from para_server to determine the timeout value
260 * for its main select loop.
261 *
262 * \param s Pointer to the server scheduler.
263 * \param t Pointer to the vss task structure.
264 *
265 * Before the timeout is computed, the current vss status flags are evaluated
266 * and acted upon by calling appropriate functions from the lower layers.
267 * Possible actions include
268 *
269 * - request a new audio file from afs,
270 * - shutdown of all senders (stop/pause command),
271 * - reposition the stream (ff/jmp command).
272 */
273 static void vss_pre_select(struct sched *s, struct task *t)
274 {
275 int i;
276 struct timeval *tv, diff;
277 struct vss_task *vsst = container_of(t, struct vss_task, task);
278
279 if (!vsst->map || vss_next() || vss_paused() || vss_repos())
280 for (i = 0; senders[i].name; i++)
281 senders[i].shutdown_clients();
282 if (vss_next())
283 vss_eof(vsst);
284 else if (vss_paused()) {
285 if (mmd->chunks_sent)
286 tv_add(&mmd->afd.afhi.eof_tv, now, &vsst->eof_barrier);
287 mmd->chunks_sent = 0;
288 } else if (vss_repos()) {
289 tv_add(now, &vsst->announce_tv, &vsst->data_send_barrier);
290 tv_add(&mmd->afd.afhi.eof_tv, now, &vsst->eof_barrier);
291 mmd->chunks_sent = 0;
292 mmd->current_chunk = mmd->repos_request;
293 mmd->new_vss_status_flags &= ~VSS_REPOS;
294 }
295
296 if (need_to_request_new_audio_file(vsst)) {
297 PARA_DEBUG_LOG("ready and playing, but no audio file\n");
298 para_fd_set(vsst->afs_socket, &s->wfds, &s->max_fileno);
299 vsst->afsss = AFS_SOCKET_CHECK_FOR_WRITE;
300 } else
301 para_fd_set(vsst->afs_socket, &s->rfds, &s->max_fileno);
302 for (i = 0; senders[i].name; i++) {
303 if (!senders[i].pre_select)
304 continue;
305 senders[i].pre_select(&s->max_fileno, &s->rfds, &s->wfds);
306 }
307 tv = vss_compute_timeout(vsst);
308 if (tv && tv_diff(tv, &s->timeout, &diff) < 0)
309 s->timeout = *tv;
310 }
311
312 static int recv_afs_msg(int afs_socket, int *fd, uint32_t *code, uint32_t *data)
313 {
314 char control[255], buf[8];
315 struct msghdr msg = {.msg_iov = NULL};
316 struct cmsghdr *cmsg;
317 struct iovec iov;
318 int ret = 0;
319
320 *fd = -1;
321 iov.iov_base = buf;
322 iov.iov_len = sizeof(buf);
323 msg.msg_iov = &iov;
324 msg.msg_iovlen = 1;
325 msg.msg_control = control;
326 msg.msg_controllen = sizeof(control);
327 memset(buf, 0, sizeof(buf));
328 ret = recvmsg(afs_socket, &msg, 0);
329 if (ret < 0)
330 return -ERRNO_TO_PARA_ERROR(errno);
331 if (iov.iov_len != sizeof(buf))
332 return -E_AFS_SHORT_READ;
333 *code = *(uint32_t*)buf;
334 *data = *(uint32_t*)(buf + 4);
335 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
336 if (cmsg->cmsg_level != SOL_SOCKET
337 || cmsg->cmsg_type != SCM_RIGHTS)
338 continue;
339 if ((cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int) != 1)
340 continue;
341 *fd = *(int *)CMSG_DATA(cmsg);
342 }
343 return 1;
344 }
345
346 static void recv_afs_result(struct vss_task *vsst)
347 {
348 int ret, passed_fd, shmid;
349 uint32_t afs_code = 0, afs_data = 0;
350 struct stat statbuf;
351
352 vsst->afsss = AFS_SOCKET_READY;
353 mmd->afd.afhi.chunk_table = NULL;
354 ret = recv_afs_msg(vsst->afs_socket, &passed_fd, &afs_code, &afs_data);
355 if (ret < 0)
356 goto err;
357 PARA_DEBUG_LOG("fd: %d, code: %u, shmid: %u\n", passed_fd, afs_code,
358 afs_data);
359 ret = -E_NOFD;
360 if (afs_code != NEXT_AUDIO_FILE)
361 goto err;
362 if (passed_fd < 0)
363 goto err;
364 shmid = afs_data;
365 ret = load_afd(shmid, &mmd->afd);
366 if (ret < 0)
367 goto err;
368 shm_destroy(shmid);
369 ret = fstat(passed_fd, &statbuf);
370 if (ret < 0) {
371 PARA_ERROR_LOG("fstat error:\n");
372 ret = -ERRNO_TO_PARA_ERROR(errno);
373 goto err;
374 }
375 mmd->size = statbuf.st_size;
376 mmd->mtime = statbuf.st_mtime;
377 vsst->map = para_mmap(mmd->size, PROT_READ, MAP_PRIVATE,
378 passed_fd, 0);
379 close(passed_fd);
380 mmd->chunks_sent = 0;
381 mmd->current_chunk = 0;
382 mmd->offset = 0;
383 mmd->events++;
384 mmd->num_played++;
385 mmd->new_vss_status_flags &= (~VSS_NEXT);
386 tv_add(now, &vsst->announce_tv, &vsst->data_send_barrier);
387 afh_get_header(&mmd->afd.afhi, vsst->map, &vsst->header_buf,
388 &vsst->header_len);
389 return;
390 err:
391 free(mmd->afd.afhi.chunk_table);
392 if (passed_fd >= 0)
393 close(passed_fd);
394 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
395 mmd->new_vss_status_flags = VSS_NEXT;
396 }
397
398 /**
399 * Main sending function.
400 *
401 * This function gets called from para_server as soon as the next chunk of data
402 * should be pushed out. It obtains a pointer to the data to be sent out as
403 * well as its length from mmd->afd.afhi. This information is then passed to
404 * each supported sender's send() function which is supposed to send out the data
405 * to all connected clients.
406 */
407 static void vss_send_chunk(struct vss_task *vsst)
408 {
409 int i;
410 struct timeval due;
411 const char *buf;
412 size_t len;
413
414 if (!vsst->map || !vss_playing())
415 return;
416 compute_chunk_time(mmd->chunks_sent, &mmd->afd.afhi.chunk_tv,
417 &mmd->stream_start, &due);
418 if (tv_diff(&due, now, NULL) > 0)
419 return;
420 if (chk_barrier("eof", &vsst->eof_barrier, &due, 1) < 0)
421 return;
422 if (chk_barrier("data send", &vsst->data_send_barrier,
423 &due, 1) < 0)
424 return;
425 mmd->new_vss_status_flags &= ~VSS_REPOS;
426 if (mmd->current_chunk >= mmd->afd.afhi.chunks_total) { /* eof */
427 mmd->new_vss_status_flags |= VSS_NEXT;
428 return;
429 }
430 /*
431 * We call the send function also in case of empty chunks as they
432 * might have still some data queued which can be sent in this case.
433 */
434 if (!mmd->chunks_sent) {
435 struct timeval tmp;
436 mmd->stream_start = *now;
437 tv_scale(mmd->current_chunk, &mmd->afd.afhi.chunk_tv, &tmp);
438 mmd->offset = tv2ms(&tmp);
439 mmd->events++;
440 }
441 afh_get_chunk(mmd->current_chunk, &mmd->afd.afhi, vsst->map, &buf, &len);
442 for (i = 0; senders[i].name; i++)
443 senders[i].send(mmd->current_chunk, mmd->chunks_sent, buf, len,
444 vsst->header_buf, vsst->header_len);
445 mmd->new_vss_status_flags |= VSS_PLAYING;
446 mmd->chunks_sent++;
447 mmd->current_chunk++;
448 }
449
450 static void vss_post_select(struct sched *s, struct task *t)
451 {
452 int ret, i;
453 struct vss_task *vsst = container_of(t, struct vss_task, task);
454
455 if (mmd->sender_cmd_data.cmd_num >= 0) {
456 int num = mmd->sender_cmd_data.cmd_num,
457 sender_num = mmd->sender_cmd_data.sender_num;
458
459 if (senders[sender_num].client_cmds[num])
460 senders[sender_num].client_cmds[num](&mmd->sender_cmd_data);
461 mmd->sender_cmd_data.cmd_num = -1;
462 }
463 if (vsst->afsss != AFS_SOCKET_CHECK_FOR_WRITE) {
464 if (FD_ISSET(vsst->afs_socket, &s->rfds))
465 recv_afs_result(vsst);
466 } else if (FD_ISSET(vsst->afs_socket, &s->wfds)) {
467 PARA_NOTICE_LOG("requesting new fd from afs\n");
468 ret = send_buffer(vsst->afs_socket, "new");
469 vsst->afsss = AFS_SOCKET_AFD_PENDING;
470 }
471 for (i = 0; senders[i].name; i++) {
472 if (!senders[i].post_select)
473 continue;
474 senders[i].post_select(&s->rfds, &s->wfds);
475 }
476 vss_send_chunk(vsst);
477 }
478
479 /**
480 * Initialize the virtual streaming system task.
481 *
482 * \param afs_socket The fd for communication with afs.
483 *
484 * This also initializes all supported senders and starts streaming
485 * if the --autoplay command line flag was given.
486 */
487 void init_vss_task(int afs_socket)
488 {
489 static struct vss_task vss_task_struct, *vsst = &vss_task_struct;
490 int i;
491 char *hn = para_hostname(), *home = para_homedir();
492 long unsigned announce_time = conf.announce_time_arg > 0?
493 conf.announce_time_arg : 300,
494 autoplay_delay = conf.autoplay_delay_arg > 0?
495 conf.autoplay_delay_arg : 0;
496
497 vsst->afs_socket = afs_socket;
498 vsst->task.pre_select = vss_pre_select;
499 vsst->task.post_select = vss_post_select;
500 ms2tv(announce_time, &vsst->announce_tv);
501 PARA_INFO_LOG("announce timeval: %lums\n", tv2ms(&vsst->announce_tv));
502 for (i = 0; senders[i].name; i++) {
503 PARA_NOTICE_LOG("initializing %s sender\n", senders[i].name);
504 senders[i].init(&senders[i]);
505 }
506 free(hn);
507 free(home);
508 mmd->sender_cmd_data.cmd_num = -1;
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 }
516 register_task(&vsst->task);
517 }