Fix com_nomore.
[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 static struct timeval announce_tv;
33 static struct timeval data_send_barrier;
34 static struct timeval eof_barrier;
35 static struct timeval autoplay_barrier;
36
37 extern struct misc_meta_data *mmd;
38 extern struct sender senders[];
39
40 static char *map;
41
42 /**
43 * Check if vss status flag \a P (playing) is set.
44 *
45 * \return Greater than zero if playing, zero otherwise.
46 *
47 */
48 unsigned int vss_playing(void)
49 {
50 return mmd->new_vss_status_flags & VSS_PLAYING;
51 }
52
53 /**
54 * Check if the \a N (next) status flag is set.
55 *
56 * \return Greater than zero if set, zero if not.
57 *
58 */
59 unsigned int vss_next(void)
60 {
61 return mmd->new_vss_status_flags & VSS_NEXT;
62 }
63
64 /**
65 * Check if a reposition request is pending.
66 *
67 * \return Greater than zero if true, zero otherwise.
68 *
69 */
70 unsigned int vss_repos(void)
71 {
72 return mmd->new_vss_status_flags & VSS_REPOS;
73 }
74
75 /**
76 * Check if the vss is currently paused.
77 *
78 * \return Greater than zero if paused, zero otherwise.
79 *
80 */
81 unsigned int vss_paused(void)
82 {
83 return !(mmd->new_vss_status_flags & VSS_NEXT)
84 && !(mmd->new_vss_status_flags & VSS_PLAYING);
85 }
86
87 /**
88 * Initialize the virtual streaming system.
89 *
90 * This also initializes all supported senders and starts streaming
91 * if the --autoplay command line flag was given.
92 */
93 void vss_init(void)
94 {
95 int i;
96 char *hn = para_hostname(), *home = para_homedir();
97 long unsigned announce_time = conf.announce_time_arg > 0?
98 conf.announce_time_arg : 300,
99 autoplay_delay = conf.autoplay_delay_arg > 0?
100 conf.autoplay_delay_arg : 0;
101 ms2tv(announce_time, &announce_tv);
102 PARA_INFO_LOG("announce timeval: %lums\n", tv2ms(&announce_tv));
103 for (i = 0; senders[i].name; i++) {
104 PARA_NOTICE_LOG("initializing %s sender\n", senders[i].name);
105 senders[i].init(&senders[i]);
106 }
107 free(hn);
108 free(home);
109 if (conf.autoplay_given) {
110 struct timeval now, tmp;
111 mmd->vss_status_flags |= VSS_PLAYING;
112 mmd->new_vss_status_flags |= VSS_PLAYING;
113 gettimeofday(&now, NULL);
114 ms2tv(autoplay_delay, &tmp);
115 tv_add(&now, &tmp, &autoplay_barrier);
116 }
117 }
118
119 static int chk_barrier(const char *bname, const struct timeval *now,
120 const struct timeval *barrier, struct timeval *diff,
121 int print_log)
122 {
123 long ms;
124
125 if (tv_diff(now, barrier, diff) > 0)
126 return 1;
127 ms = tv2ms(diff);
128 if (print_log && ms)
129 PARA_DEBUG_LOG("%s barrier: %lims left\n", bname, ms);
130 return -1;
131 }
132
133 static void vss_next_chunk_time(struct timeval *due)
134 {
135 struct timeval tmp;
136
137 tv_scale(mmd->chunks_sent, &mmd->afd.afhi.chunk_tv, &tmp);
138 tv_add(&tmp, &mmd->stream_start, due);
139 }
140
141 /*
142 * != NULL: timeout for next chunk
143 * NULL: nothing to do
144 */
145 static struct timeval *vss_compute_timeout(void)
146 {
147 static struct timeval the_timeout;
148 struct timeval now, next_chunk;
149
150 if (vss_next() && map) {
151 /* only sleep a bit, nec*/
152 the_timeout.tv_sec = 0;
153 the_timeout.tv_usec = 100;
154 return &the_timeout;
155 }
156 gettimeofday(&now, NULL);
157 if (chk_barrier("autoplay_delay", &now, &autoplay_barrier,
158 &the_timeout, 1) < 0)
159 return &the_timeout;
160 if (chk_barrier("eof", &now, &eof_barrier, &the_timeout, 1) < 0)
161 return &the_timeout;
162 if (chk_barrier("data send", &now, &data_send_barrier,
163 &the_timeout, 1) < 0)
164 return &the_timeout;
165 if (!vss_playing() || !map)
166 return NULL;
167 vss_next_chunk_time(&next_chunk);
168 if (chk_barrier("chunk", &now, &next_chunk, &the_timeout, 0) < 0)
169 return &the_timeout;
170 /* chunk is due or bof */
171 the_timeout.tv_sec = 0;
172 the_timeout.tv_usec = 0;
173 return &the_timeout;
174 }
175
176 static void vss_eof(void)
177 {
178 struct timeval now;
179 int i;
180 char *tmp;
181
182 if (!map) {
183 for (i = 0; senders[i].name; i++)
184 senders[i].shutdown_clients();
185 return;
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 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 Aa 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 * \param rfds The set of file descriptors to be checked for reading.
267 * \param wfds The set of file descriptors to be checked for writing.
268 * \param max_fileno The highest-numbered file descriptor.
269 *
270 * Before the timeout is computed, the current vss status flags are evaluated
271 * and acted upon by calling appropriate functions from the lower layers.
272 * Possible actions include
273 *
274 * - request a new audio file from afs,
275 * - shutdown of all senders (stop/pause command),
276 * - reposition the stream (ff/jmp command).
277 *
278 * \return A pointer to a struct timeval containing the timeout for the next
279 * chunk of data to be sent, or NULL if we're not sending right now.
280 */
281 struct timeval *vss_preselect(fd_set *rfds, fd_set *wfds, int *max_fileno)
282 {
283 int i;
284 struct timeval *tv;
285
286 para_fd_set(afs_socket, rfds, max_fileno);
287 if (!map)
288 for (i = 0; senders[i].name; i++)
289 senders[i].shutdown_clients();
290 else {
291 if (vss_next()) {
292 vss_eof();
293 return vss_compute_timeout();
294 }
295 }
296 if (vss_paused() || vss_repos()) {
297 for (i = 0; senders[i].name; i++)
298 senders[i].shutdown_clients();
299 if (map) {
300 struct timeval now;
301 gettimeofday(&now, NULL);
302 if (!vss_paused() || mmd->chunks_sent)
303 tv_add(&mmd->afd.afhi.eof_tv, &now, &eof_barrier);
304 if (vss_repos())
305 tv_add(&now, &announce_tv, &data_send_barrier);
306 }
307 mmd->chunks_sent = 0;
308 }
309 if (vss_repos()) {
310 mmd->new_vss_status_flags &= ~(VSS_REPOS);
311 mmd->current_chunk = mmd->repos_request;
312 }
313 tv = vss_compute_timeout();
314 if (tv)
315 return tv;
316 if (!map && vss_playing() &&
317 !(mmd->new_vss_status_flags & VSS_NOMORE)) {
318 if (afsss == AFS_SOCKET_READY ||
319 afsss == AFS_SOCKET_CHECK_FOR_WRITE) {
320 PARA_DEBUG_LOG("ready and playing, but no audio file\n");
321 para_fd_set(afs_socket, wfds, max_fileno);
322 afsss = AFS_SOCKET_CHECK_FOR_WRITE;
323 }
324 }
325 return tv;
326 }
327
328 static int recv_afs_msg(int *fd, uint32_t *code, uint32_t *data)
329 {
330 char control[255], buf[8];
331 struct msghdr msg = {.msg_iov = NULL};
332 struct cmsghdr *cmsg;
333 struct iovec iov;
334 int ret = 0;
335
336 iov.iov_base = buf;
337 iov.iov_len = sizeof(buf);
338 msg.msg_iov = &iov;
339 msg.msg_iovlen = 1;
340 msg.msg_control = control;
341 msg.msg_controllen = sizeof(control);
342 memset(buf, 0, sizeof(buf));
343 ret = recvmsg(afs_socket, &msg, 0);
344 if (ret < 0)
345 return -ERRNO_TO_PARA_ERROR(errno);
346 if (iov.iov_len != sizeof(buf))
347 return -E_SHORT_AFS_READ;
348 *code = *(uint32_t*)buf;
349 *data = *(uint32_t*)(buf + 4);
350 for (cmsg = CMSG_FIRSTHDR(&msg); cmsg; cmsg = CMSG_NXTHDR(&msg, cmsg)) {
351 if (cmsg->cmsg_level != SOL_SOCKET
352 || cmsg->cmsg_type != SCM_RIGHTS)
353 continue;
354 if ((cmsg->cmsg_len - CMSG_LEN(0)) / sizeof(int) != 1)
355 continue;
356 *fd = *(int *)CMSG_DATA(cmsg);
357 }
358 return 1;
359 }
360
361 static void recv_afs_result(void)
362 {
363 int ret, passed_fd = -1, shmid;
364 uint32_t afs_code = 0, afs_data = 0;
365 struct stat statbuf;
366 struct timeval now;
367
368 ret = recv_afs_msg(&passed_fd, &afs_code, &afs_data);
369 if (ret < 0)
370 goto err;
371 PARA_DEBUG_LOG("got the fd: %d, code: %u, shmid: %u\n",
372 passed_fd, afs_code, afs_data);
373 ret = -E_BAD_AFS_CODE;
374 if (afs_code != NEXT_AUDIO_FILE)
375 goto err;
376 afsss = AFS_SOCKET_READY;
377 shmid = afs_data;
378 ret = load_afd(shmid, &mmd->afd);
379 if (ret < 0)
380 goto err;
381 shm_destroy(shmid);
382 ret = fstat(passed_fd, &statbuf);
383 if (ret < 0) {
384 PARA_ERROR_LOG("fstat error:\n");
385 ret = -ERRNO_TO_PARA_ERROR(errno);
386 goto err;
387 }
388 mmd->size = statbuf.st_size;
389 mmd->mtime = statbuf.st_mtime;
390 map = para_mmap(mmd->size, PROT_READ, MAP_PRIVATE,
391 passed_fd, 0);
392 close(passed_fd);
393 mmd->chunks_sent = 0;
394 mmd->current_chunk = 0;
395 mmd->offset = 0;
396 mmd->events++;
397 mmd->num_played++;
398 mmd->new_vss_status_flags &= (~VSS_NEXT);
399 gettimeofday(&now, NULL);
400 tv_add(&now, &announce_tv, &data_send_barrier);
401 return;
402 err:
403 if (passed_fd >= 0)
404 close(passed_fd);
405 PARA_ERROR_LOG("%s\n", PARA_STRERROR(-ret));
406 }
407
408 void vss_post_select(fd_set *rfds, fd_set *wfds)
409 {
410 int ret;
411
412 if (FD_ISSET(afs_socket, rfds))
413 recv_afs_result();
414 if (afsss != AFS_SOCKET_CHECK_FOR_WRITE || !FD_ISSET(afs_socket, wfds))
415 return;
416 PARA_NOTICE_LOG("requesting new fd from afs\n");
417 ret = send_buffer(afs_socket, "new");
418 afsss = AFS_SOCKET_AFD_PENDING;
419 }
420
421 static void get_chunk(long unsigned chunk_num, char **buf, size_t *len)
422 {
423 size_t pos = mmd->afd.afhi.chunk_table[chunk_num];
424 *buf = map + pos;
425 *len = mmd->afd.afhi.chunk_table[chunk_num + 1] - pos;
426 }
427
428 /**
429 * Get the data of the given chunk.
430 *
431 * \param chunk_num The number of the desired chunk.
432 * \param buf Chunk data.
433 * \param len Chunk length in bytes.
434 *
435 * \return Standard.
436 */
437 int vss_get_chunk(long unsigned chunk_num, char **buf, size_t *len)
438 {
439 if (!map || !vss_playing())
440 return -E_CHUNK;
441 if (chunk_num >= mmd->afd.afhi.chunks_total)
442 return -E_CHUNK;
443 get_chunk(chunk_num, buf, len);
444 return 1;
445 }
446
447 /**
448 * Main sending function.
449 *
450 * This function gets called from para_server as soon as the next chunk of data
451 * should be pushed out. It obtains a pointer to the data to be sent out as
452 * well as its length from mmd->afd.afhi. This information is then passed to
453 * each supported sender's send() function which is supposed to send out the data
454 * to all connected clients.
455 */
456 void vss_send_chunk(void)
457 {
458 int i;
459 struct timeval now, due;
460 char *buf;
461 size_t len;
462
463 if (!map || !vss_playing())
464 return;
465 gettimeofday(&now, NULL);
466 vss_next_chunk_time(&due);
467 if (tv_diff(&due, &now, NULL) > 0)
468 return;
469 if (chk_barrier("eof", &now, &eof_barrier, &due, 1) < 0)
470 return;
471 if (chk_barrier("data send", &now, &data_send_barrier,
472 &due, 1) < 0)
473 return;
474 mmd->new_vss_status_flags &= ~VSS_REPOS;
475 if (mmd->current_chunk >= mmd->afd.afhi.chunks_total) { /* eof */
476 mmd->new_vss_status_flags |= VSS_NEXT;
477 return vss_eof();
478 }
479 /*
480 * We call the send function also in case of empty chunks as they
481 * might have still some data queued which can be sent in this case.
482 */
483 if (!mmd->chunks_sent) {
484 struct timeval tmp;
485 gettimeofday(&mmd->stream_start, NULL);
486 tv_scale(mmd->current_chunk, &mmd->afd.afhi.chunk_tv, &tmp);
487 mmd->offset = tv2ms(&tmp);
488 mmd->events++;
489 }
490 get_chunk(mmd->current_chunk, &buf, &len);
491 for (i = 0; senders[i].name; i++)
492 senders[i].send(mmd->current_chunk, mmd->chunks_sent, buf, len);
493 mmd->new_vss_status_flags |= VSS_PLAYING;
494 mmd->chunks_sent++;
495 mmd->current_chunk++;
496 }