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