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