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