Remove duplicate error code.
[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 static void vss_next_chunk_time(struct timeval *due)
147 {
148 struct timeval tmp;
149
150 tv_scale(mmd->chunks_sent, &mmd->afd.afhi.chunk_tv, &tmp);
151 tv_add(&tmp, &mmd->stream_start, due);
152 }
153
154 /*
155 * != NULL: timeout for next chunk
156 * NULL: nothing to do
157 */
158 static struct timeval *vss_compute_timeout(void)
159 {
160 static struct timeval the_timeout;
161 struct timeval now, next_chunk;
162
163 if (vss_next() && map) {
164 /* only sleep a bit, nec*/
165 the_timeout.tv_sec = 0;
166 the_timeout.tv_usec = 100;
167 return &the_timeout;
168 }
169 gettimeofday(&now, NULL);
170 if (chk_barrier("autoplay_delay", &now, &autoplay_barrier,
171 &the_timeout, 1) < 0)
172 return &the_timeout;
173 if (chk_barrier("eof", &now, &eof_barrier, &the_timeout, 1) < 0)
174 return &the_timeout;
175 if (chk_barrier("data send", &now, &data_send_barrier,
176 &the_timeout, 1) < 0)
177 return &the_timeout;
178 if (!vss_playing() || !map)
179 return NULL;
180 vss_next_chunk_time(&next_chunk);
181 if (chk_barrier("chunk", &now, &next_chunk, &the_timeout, 0) < 0)
182 return &the_timeout;
183 /* chunk is due or bof */
184 the_timeout.tv_sec = 0;
185 the_timeout.tv_usec = 0;
186 return &the_timeout;
187 }
188
189 static void vss_eof(void)
190 {
191 struct timeval now;
192 int i;
193 char *tmp;
194
195 if (!map) {
196 for (i = 0; senders[i].name; i++)
197 senders[i].shutdown_clients();
198 return;
199 }
200 if (mmd->new_vss_status_flags & VSS_NOMORE)
201 mmd->new_vss_status_flags = VSS_NEXT;
202 gettimeofday(&now, NULL);
203 tv_add(&mmd->afd.afhi.eof_tv, &now, &eof_barrier);
204 munmap(map, mmd->size);
205 map = NULL;
206 mmd->chunks_sent = 0;
207 mmd->offset = 0;
208 mmd->afd.afhi.seconds_total = 0;
209 free(mmd->afd.afhi.chunk_table);
210 mmd->afd.afhi.chunk_table = NULL;
211 tmp = make_message("%s:\n%s:\n%s:\n", status_item_list[SI_AUDIO_FILE_INFO],
212 status_item_list[SI_TAGINFO1], status_item_list[SI_TAGINFO2]);
213 strncpy(mmd->afd.afhi.info_string, tmp, sizeof(mmd->afd.afhi.info_string));
214 mmd->afd.afhi.info_string[sizeof(mmd->afd.afhi.info_string) - 1] = '\0';
215 make_empty_status_items(mmd->afd.verbose_ls_output);
216 free(tmp);
217 mmd->mtime = 0;
218 mmd->size = 0;
219 mmd->events++;
220 }
221
222 /**
223 * Get the header of the current audio file.
224 *
225 * \param header_len The length of the header is stored here.
226 *
227 * \return A pointer to a buffer containing the header, or NULL, if no audio
228 * file is selected or if the current audio format does not need special header
229 * treamtment.
230 *
231 */
232 char *vss_get_header(size_t *header_len)
233 {
234 if (!map || !mmd->afd.afhi.header_len)
235 return NULL;
236 *header_len = mmd->afd.afhi.header_len;
237 return map + mmd->afd.afhi.header_offset;
238 }
239
240 /**
241 * Get the list of all supported audio formats.
242 *
243 * \return Aa space separated list of all supported audio formats
244 * It is not allocated at runtime, i.e. there is no need to free
245 * the returned string in the caller.
246 */
247 const char *supported_audio_formats(void)
248 {
249 return SUPPORTED_AUDIO_FORMATS;
250 }
251
252 /**
253 * Get the chunk time of the current audio file.
254 *
255 * \return A pointer to a struct containing the chunk time, or NULL,
256 * if currently no audio file is selected.
257 */
258 struct timeval *vss_chunk_time(void)
259 {
260 if (!map)
261 return NULL;
262 return &mmd->afd.afhi.chunk_tv;
263 }
264
265 /** The possible states of the afs socket. See \ref afs_socket. */
266 enum afs_socket_status {
267 /** Socket is inactive. */
268 AFS_SOCKET_READY,
269 /** Socket fd was included in the write fd set for select(). */
270 AFS_SOCKET_CHECK_FOR_WRITE,
271 /** vss wrote a request to the socket and waits for afs to reply. */
272 AFS_SOCKET_AFD_PENDING
273 };
274
275 static enum afs_socket_status afsss;
276
277 /**
278 * Compute the timeout for para_server's main select-loop.
279 *
280 * This function gets called from para_server to determine the timeout value
281 * for its main select loop.
282 *
283 * \param rfds The set of file descriptors to be checked for reading.
284 * \param wfds The set of file descriptors to be checked for writing.
285 * \param max_fileno The highest-numbered file descriptor.
286 *
287 * Before the timeout is computed, the current vss status flags are evaluated
288 * and acted upon by calling appropriate functions from the lower layers.
289 * Possible actions include
290 *
291 * - request a new audio file from afs,
292 * - shutdown of all senders (stop/pause command),
293 * - reposition the stream (ff/jmp command).
294 *
295 * \return A pointer to a struct timeval containing the timeout for the next
296 * chunk of data to be sent, or NULL if we're not sending right now.
297 */
298 struct timeval *vss_preselect(fd_set *rfds, fd_set *wfds, int *max_fileno)
299 {
300 int i;
301 struct timeval *tv;
302
303 para_fd_set(afs_socket, rfds, max_fileno);
304 if (!map)
305 for (i = 0; senders[i].name; i++)
306 senders[i].shutdown_clients();
307 else {
308 if (vss_next()) {
309 vss_eof();
310 return vss_compute_timeout();
311 }
312 }
313 if (vss_paused() || vss_repos()) {
314 for (i = 0; senders[i].name; i++)
315 senders[i].shutdown_clients();
316 if (map) {
317 struct timeval now;
318 gettimeofday(&now, NULL);
319 if (!vss_paused() || mmd->chunks_sent)
320 tv_add(&mmd->afd.afhi.eof_tv, &now, &eof_barrier);
321 if (vss_repos())
322 tv_add(&now, &announce_tv, &data_send_barrier);
323 }
324 mmd->chunks_sent = 0;
325 }
326 if (vss_repos()) {
327 mmd->new_vss_status_flags &= ~(VSS_REPOS);
328 mmd->current_chunk = mmd->repos_request;
329 }
330 tv = vss_compute_timeout();
331 if (tv)
332 return tv;
333 if (!map && vss_playing() &&
334 !(mmd->new_vss_status_flags & VSS_NOMORE)) {
335 if (afsss == AFS_SOCKET_READY ||
336 afsss == AFS_SOCKET_CHECK_FOR_WRITE) {
337 PARA_DEBUG_LOG("ready and playing, but no audio file\n");
338 para_fd_set(afs_socket, wfds, max_fileno);
339 afsss = AFS_SOCKET_CHECK_FOR_WRITE;
340 }
341 }
342 return tv;
343 }
344
345 static int recv_afs_msg(int *fd, uint32_t *code, uint32_t *data)
346 {
347 char control[255], buf[8];
348 struct msghdr msg = {.msg_iov = NULL};
349 struct cmsghdr *cmsg;
350 struct iovec iov;
351 int ret = 0;
352
353 *fd = -1;
354 iov.iov_base = buf;
355 iov.iov_len = sizeof(buf);
356 msg.msg_iov = &iov;
357 msg.msg_iovlen = 1;
358 msg.msg_control = control;
359 msg.msg_controllen = sizeof(control);
360 memset(buf, 0, sizeof(buf));
361 ret = recvmsg(afs_socket, &msg, 0);
362 if (ret < 0)
363 return -ERRNO_TO_PARA_ERROR(errno);
364 afsss = AFS_SOCKET_READY;
365 if (iov.iov_len != sizeof(buf))
366 return -E_AFS_SHORT_READ;
367 *code = *(uint32_t*)buf;
368 *data = *(uint32_t*)(buf + 4);
369 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
370 if (cmsg->cmsg_level != SOL_SOCKET
371 || cmsg->cmsg_type != SCM_RIGHTS)
372 continue;
373 if ((cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int) != 1)
374 continue;
375 *fd = *(int *)CMSG_DATA(cmsg);
376 }
377 return 1;
378 }
379
380 static void recv_afs_result(void)
381 {
382 int ret, passed_fd, shmid;
383 uint32_t afs_code = 0, afs_data = 0;
384 struct stat statbuf;
385 struct timeval now;
386
387 mmd->afd.afhi.chunk_table = NULL;
388 ret = recv_afs_msg(&passed_fd, &afs_code, &afs_data);
389 if (ret < 0)
390 goto err;
391 PARA_DEBUG_LOG("fd: %d, code: %u, shmid: %u\n", passed_fd, afs_code,
392 afs_data);
393 ret = -E_NOFD;
394 if (afs_code != NEXT_AUDIO_FILE)
395 goto err;
396 if (passed_fd < 0)
397 goto err;
398 shmid = afs_data;
399 ret = load_afd(shmid, &mmd->afd);
400 if (ret < 0)
401 goto err;
402 shm_destroy(shmid);
403 ret = fstat(passed_fd, &statbuf);
404 if (ret < 0) {
405 PARA_ERROR_LOG("fstat error:\n");
406 ret = -ERRNO_TO_PARA_ERROR(errno);
407 goto err;
408 }
409 mmd->size = statbuf.st_size;
410 mmd->mtime = statbuf.st_mtime;
411 map = para_mmap(mmd->size, PROT_READ, MAP_PRIVATE,
412 passed_fd, 0);
413 close(passed_fd);
414 mmd->chunks_sent = 0;
415 mmd->current_chunk = 0;
416 mmd->offset = 0;
417 mmd->events++;
418 mmd->num_played++;
419 mmd->new_vss_status_flags &= (~VSS_NEXT);
420 gettimeofday(&now, NULL);
421 tv_add(&now, &announce_tv, &data_send_barrier);
422 return;
423 err:
424 free(mmd->afd.afhi.chunk_table);
425 if (passed_fd >= 0)
426 close(passed_fd);
427 PARA_ERROR_LOG("%s\n", para_strerror(-ret));
428 mmd->new_vss_status_flags = VSS_NEXT;
429 }
430
431 void vss_post_select(fd_set *rfds, fd_set *wfds)
432 {
433 int ret;
434
435 if (FD_ISSET(afs_socket, rfds))
436 recv_afs_result();
437 if (afsss != AFS_SOCKET_CHECK_FOR_WRITE || !FD_ISSET(afs_socket, wfds))
438 return;
439 PARA_NOTICE_LOG("requesting new fd from afs\n");
440 ret = send_buffer(afs_socket, "new");
441 afsss = AFS_SOCKET_AFD_PENDING;
442 }
443
444 static void get_chunk(long unsigned chunk_num, char **buf, size_t *len)
445 {
446 size_t pos = mmd->afd.afhi.chunk_table[chunk_num];
447 *buf = map + pos;
448 *len = mmd->afd.afhi.chunk_table[chunk_num + 1] - pos;
449 }
450
451 /**
452 * Get the data of the given chunk.
453 *
454 * \param chunk_num The number of the desired chunk.
455 * \param buf Chunk data.
456 * \param len Chunk length in bytes.
457 *
458 * \return Standard.
459 */
460 int vss_get_chunk(long unsigned chunk_num, char **buf, size_t *len)
461 {
462 if (!map || !vss_playing())
463 return -E_CHUNK;
464 if (chunk_num >= mmd->afd.afhi.chunks_total)
465 return -E_CHUNK;
466 get_chunk(chunk_num, buf, len);
467 return 1;
468 }
469
470 /**
471 * Main sending function.
472 *
473 * This function gets called from para_server as soon as the next chunk of data
474 * should be pushed out. It obtains a pointer to the data to be sent out as
475 * well as its length from mmd->afd.afhi. This information is then passed to
476 * each supported sender's send() function which is supposed to send out the data
477 * to all connected clients.
478 */
479 void vss_send_chunk(void)
480 {
481 int i;
482 struct timeval now, due;
483 char *buf;
484 size_t len;
485
486 if (!map || !vss_playing())
487 return;
488 gettimeofday(&now, NULL);
489 vss_next_chunk_time(&due);
490 if (tv_diff(&due, &now, NULL) > 0)
491 return;
492 if (chk_barrier("eof", &now, &eof_barrier, &due, 1) < 0)
493 return;
494 if (chk_barrier("data send", &now, &data_send_barrier,
495 &due, 1) < 0)
496 return;
497 mmd->new_vss_status_flags &= ~VSS_REPOS;
498 if (mmd->current_chunk >= mmd->afd.afhi.chunks_total) { /* eof */
499 mmd->new_vss_status_flags |= VSS_NEXT;
500 return vss_eof();
501 }
502 /*
503 * We call the send function also in case of empty chunks as they
504 * might have still some data queued which can be sent in this case.
505 */
506 if (!mmd->chunks_sent) {
507 struct timeval tmp;
508 gettimeofday(&mmd->stream_start, NULL);
509 tv_scale(mmd->current_chunk, &mmd->afd.afhi.chunk_tv, &tmp);
510 mmd->offset = tv2ms(&tmp);
511 mmd->events++;
512 }
513 get_chunk(mmd->current_chunk, &buf, &len);
514 for (i = 0; senders[i].name; i++)
515 senders[i].send(mmd->current_chunk, mmd->chunks_sent, buf, len);
516 mmd->new_vss_status_flags |= VSS_PLAYING;
517 mmd->chunks_sent++;
518 mmd->current_chunk++;
519 }