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