2 * Copyright (C) 1997-2007 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file vss.c the virtual streaming system
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
17 #include <sys/mman.h> /* mmap */
18 #include <sys/time.h> /* gettimeofday */
19 #include "server.cmdline.h"
20 #include "afs_common.h"
27 extern const char *status_item_list
[];
29 static struct timeval announce_tv
;
30 static struct timeval data_send_barrier
;
31 static struct timeval eof_barrier
;
32 static struct timeval autoplay_barrier
;
34 extern struct misc_meta_data
*mmd
;
35 extern struct audio_file_selector selectors
[];
36 extern struct sender senders
[];
38 static int audio_file
;
42 void mp3_init(struct audio_format_handler
*);
46 void ogg_init(struct audio_format_handler
*);
49 void aac_afh_init(struct audio_format_handler
*);
53 * the list of supported audio formats
55 static struct audio_format_handler afl
[] = {
79 /** iterate over each supported audio format */
80 #define FOR_EACH_AUDIO_FORMAT(i) for (i = 0; afl[i].name; i++)
85 * check if vss status flag \a P (playing) is set
87 * \return greater than zero if playing, zero otherwise.
90 unsigned int vss_playing(void)
92 return mmd
->new_vss_status_flags
& VSS_PLAYING
;
96 * check if \a N (next) status flag is set
98 * \return greater than zero if set, zero if not.
101 unsigned int vss_next(void)
103 return mmd
->new_vss_status_flags
& VSS_NEXT
;
107 * check if a reposition request is pending
109 * \return greater than zero if true, zero otherwise.
112 unsigned int vss_repos(void)
114 return mmd
->new_vss_status_flags
& VSS_REPOS
;
118 * check if the vss is currently paused
120 * \return greater than zero if paused, zero otherwise.
123 unsigned int vss_paused(void)
125 return !(mmd
->new_vss_status_flags
& VSS_NEXT
)
126 && !(mmd
->new_vss_status_flags
& VSS_PLAYING
);
130 * get the name of the given audio format
131 * \param i the audio format number
133 * This returns a pointer to statically allocated memory so it
134 * must not be freed by the caller.
136 const char *audio_format_name(int i
)
138 return i
>= 0? afl
[i
].name
: "(none)";
142 * initialize the virtual streaming system
144 * Call the init functions of all supported audio format handlers and
145 * initialize all supported senders.
150 char *hn
= para_hostname(), *home
= para_homedir();
151 long unsigned announce_time
= conf
.announce_time_arg
> 0?
152 conf
.announce_time_arg
: 300,
153 autoplay_delay
= conf
.autoplay_delay_arg
> 0?
154 conf
.autoplay_delay_arg
: 0;
156 PARA_DEBUG_LOG("supported audio formats: %s\n",
157 SUPPORTED_AUDIO_FORMATS
);
158 for (i
= 0; afl
[i
].name
; i
++) {
159 PARA_NOTICE_LOG("initializing %s handler\n",
161 afl
[i
].init(&afl
[i
]);
163 ms2tv(announce_time
, &announce_tv
);
164 PARA_INFO_LOG("announce timeval: %lums\n", tv2ms(&announce_tv
));
165 for (i
= 0; senders
[i
].name
; i
++) {
166 PARA_NOTICE_LOG("initializing %s sender\n", senders
[i
].name
);
167 senders
[i
].init(&senders
[i
]);
171 if (conf
.autoplay_given
) {
172 struct timeval now
, tmp
;
173 mmd
->vss_status_flags
|= VSS_PLAYING
;
174 mmd
->new_vss_status_flags
|= VSS_PLAYING
;
175 gettimeofday(&now
, NULL
);
176 ms2tv(autoplay_delay
, &tmp
);
177 tv_add(&now
, &tmp
, &autoplay_barrier
);
181 static int get_file_info(int i
)
183 return afl
[i
].get_file_info(map
, mmd
->size
, &mmd
->afi
);
187 * guess the audio format judging from filename
189 * \param name the filename
191 * \return This function returns -1 if it has no idea what kind of audio
192 * file this might be. Otherwise the (non-negative) number of the audio format
195 int guess_audio_format(const char *name
)
197 int i
,j
, len
= strlen(name
);
199 FOR_EACH_AUDIO_FORMAT(i
) {
200 for (j
= 0; afl
[i
].suffixes
[j
]; j
++) {
201 const char *p
= afl
[i
].suffixes
[j
];
202 int plen
= strlen(p
);
205 if (name
[len
- plen
- 1] != '.')
207 if (strcasecmp(name
+ len
- plen
, p
))
209 // PARA_DEBUG_LOG("might be %s\n", audio_format_name(i));
216 static int get_audio_format(int omit
)
220 FOR_EACH_AUDIO_FORMAT(i
) {
223 if (get_file_info(i
) > 0)
226 return -E_AUDIO_FORMAT
;
232 static int update_mmd(void)
236 i
= guess_audio_format(mmd
->filename
);
237 if (i
< 0 || get_file_info(i
) < 0)
238 i
= get_audio_format(i
);
241 mmd
->audio_format
= i
;
242 mmd
->chunks_sent
= 0;
243 mmd
->current_chunk
= 0;
249 static void vss_get_audio_file(void)
251 char **sl
= selectors
[mmd
->selector_num
].get_audio_file_list(10);
253 struct stat file_status
;
257 for (i
= 0; sl
[i
]; i
++) {
259 PARA_INFO_LOG("trying %s\n", sl
[i
]);
260 if (strlen(sl
[i
]) >= _POSIX_PATH_MAX
)
262 audio_file
= open(sl
[i
], O_RDONLY
);
265 if (fstat(audio_file
, &file_status
) == -1 ||
266 !file_status
.st_size
) {
270 mmd
->size
= file_status
.st_size
;
271 mmd
->mtime
= file_status
.st_mtime
;
272 map
= para_mmap(mmd
->size
, PROT_READ
, MAP_PRIVATE
,
274 strcpy(mmd
->filename
, sl
[i
]);
275 mmd
->afi
.header_len
= 0; /* default: no header */
276 if (update_mmd() < 0) { /* invalid file */
278 munmap(map
, mmd
->size
);
283 if (selectors
[mmd
->selector_num
].update_audio_file
)
284 selectors
[mmd
->selector_num
].update_audio_file(sl
[i
]);
285 PARA_NOTICE_LOG("next audio file: %s\n", mmd
->filename
);
286 mmd
->new_vss_status_flags
&= (~VSS_NEXT
);
287 gettimeofday(&now
, NULL
);
288 tv_add(&now
, &announce_tv
, &data_send_barrier
);
292 PARA_ERROR_LOG("%s", "no valid files found\n");
293 mmd
->new_vss_status_flags
= VSS_NEXT
;
296 for (i
= 0; sl
[i
]; i
++)
302 static int chk_barrier(const char *bname
, const struct timeval
*now
,
303 const struct timeval
*barrier
, struct timeval
*diff
,
308 if (tv_diff(now
, barrier
, diff
) > 0)
312 PARA_DEBUG_LOG("%s barrier: %lims left\n", bname
, ms
);
316 static void vss_next_chunk_time(struct timeval
*due
)
320 tv_scale(mmd
->chunks_sent
, &mmd
->afi
.chunk_tv
, &tmp
);
321 tv_add(&tmp
, &mmd
->stream_start
, due
);
325 * != NULL: timeout for next chunk
326 * NULL: nothing to do
328 static struct timeval
*vss_compute_timeout(void)
330 static struct timeval the_timeout
;
331 struct timeval now
, next_chunk
;
333 if (vss_next() && mmd
->audio_format
>= 0) {
334 /* only sleep a bit, nec*/
335 the_timeout
.tv_sec
= 0;
336 the_timeout
.tv_usec
= 100;
339 gettimeofday(&now
, NULL
);
340 if (chk_barrier("autoplay_delay", &now
, &autoplay_barrier
,
341 &the_timeout
, 1) < 0)
343 if (chk_barrier("eof", &now
, &eof_barrier
, &the_timeout
, 1) < 0)
345 if (chk_barrier("data send", &now
, &data_send_barrier
,
346 &the_timeout
, 1) < 0)
348 if (mmd
->audio_format
< 0 || !vss_playing() || !map
)
350 vss_next_chunk_time(&next_chunk
);
351 if (chk_barrier(afl
[mmd
->audio_format
].name
, &now
, &next_chunk
,
352 &the_timeout
, 0) < 0)
354 /* chunk is due or bof */
355 the_timeout
.tv_sec
= 0;
356 the_timeout
.tv_usec
= 0;
360 static void vss_eof(void)
367 for (i
= 0; senders
[i
].name
; i
++)
368 senders
[i
].shutdown_clients();
371 gettimeofday(&now
, NULL
);
372 tv_add(&mmd
->afi
.eof_tv
, &now
, &eof_barrier
);
373 munmap(map
, mmd
->size
);
376 mmd
->audio_format
= -1;
377 mmd
->chunks_sent
= 0;
379 mmd
->afi
.seconds_total
= 0;
380 free(mmd
->afi
.chunk_table
);
381 mmd
->afi
.chunk_table
= NULL
;
382 tmp
= make_message("%s:\n%s:\n%s:\n", status_item_list
[SI_AUDIO_INFO1
],
383 status_item_list
[SI_AUDIO_INFO2
], status_item_list
[SI_AUDIO_INFO3
]);
384 strcpy(mmd
->afi
.info_string
, tmp
);
386 tmp
= make_message("%s:\n%s:\n%s:\n", status_item_list
[SI_DBINFO1
],
387 status_item_list
[SI_DBINFO2
], status_item_list
[SI_DBINFO3
]);
388 strcpy(mmd
->selector_info
, tmp
);
390 mmd
->filename
[0] = '\0';
396 * get the header and of the current audio file
398 * \param header_len the length of the header is stored here
400 * \return a pointer to a buffer containing the header, or NULL, if no audio
401 * file is selected or if the current audio format does not need special header
405 char *vss_get_header(size_t *header_len
)
407 if (mmd
->audio_format
< 0 || !map
|| !mmd
->afi
.header_len
)
409 *header_len
= mmd
->afi
.header_len
;
410 return map
+ mmd
->afi
.header_offset
;
414 * get the list of all supported audio formats
416 * \return a space separated list of all supported audio formats
417 * It is not allocated at runtime, i.e. there is no need to free
418 * the returned string in the caller.
420 const char *supported_audio_formats(void)
422 return SUPPORTED_AUDIO_FORMATS
;
426 * get the chunk time of the current audio file
428 * \return a pointer to a struct containing the chunk time, or NULL,
429 * if currently no audio file is selected.
431 struct timeval
*vss_chunk_time(void)
433 if (mmd
->audio_format
< 0)
435 return &mmd
->afi
.chunk_tv
;
439 * compute the timeout for para_server's main select-loop
441 * This function gets called from para_server to determine the timeout value
442 * for its main select loop.
444 * Before the timeout is computed, the current vss status flags are evaluated
445 * and acted upon by calling appropriate functions from the lower layers.
446 * Possible actions include
448 * - request a new file list from the current audio file selector
449 * - shutdown of all senders (stop/pause command)
450 * - reposition the stream (ff/jmp command)
452 * \return A pointer to a struct timeval containing the timeout for the next
453 * chunk of data to be sent, or NULL if we're not sending right now.
455 struct timeval
*vss_preselect(void)
457 struct audio_format_handler
*af
= NULL
;
461 format
= mmd
->audio_format
;
465 for (i
= 0; senders
[i
].name
; i
++)
466 senders
[i
].shutdown_clients();
467 if (vss_next() && af
) {
469 return vss_compute_timeout();
471 if (vss_paused() || vss_repos()) {
472 for (i
= 0; senders
[i
].name
; i
++)
473 senders
[i
].shutdown_clients();
476 gettimeofday(&now
, NULL
);
477 if (!vss_paused() || mmd
->chunks_sent
)
478 tv_add(&mmd
->afi
.eof_tv
, &now
, &eof_barrier
);
480 tv_add(&now
, &announce_tv
, &data_send_barrier
);
481 if (mmd
->new_vss_status_flags
& VSS_NOMORE
)
482 mmd
->new_vss_status_flags
= VSS_NEXT
;
484 mmd
->chunks_sent
= 0;
487 mmd
->new_vss_status_flags
&= ~(VSS_REPOS
);
488 mmd
->current_chunk
= mmd
->repos_request
;
490 ret
= vss_compute_timeout();
491 if (!ret
&& !map
&& vss_playing() &&
492 !(mmd
->new_vss_status_flags
& VSS_NOMORE
)) {
493 PARA_DEBUG_LOG("%s", "ready and playing, but no audio file\n");
494 vss_get_audio_file();
500 static void get_chunk(long unsigned chunk_num
, char **buf
, size_t *len
)
502 size_t pos
= mmd
->afi
.chunk_table
[chunk_num
];
504 *len
= mmd
->afi
.chunk_table
[chunk_num
+ 1] - pos
;
508 * Get the data of the given chunk.
510 * \param chunk_num The number of the desired chunk.
511 * \param buf Chunk data.
512 * \param len Chunk length in bytes.
514 * \return Positive on success, negative on errors.
516 int vss_get_chunk(long unsigned chunk_num
, char **buf
, size_t *len
)
518 if (mmd
->audio_format
< 0 || !map
|| !vss_playing())
520 if (chunk_num
>= mmd
->afi
.chunks_total
)
522 get_chunk(chunk_num
, buf
, len
);
527 * main sending function
529 * This function gets called from para_server as soon as the next chunk of
530 * data should be pushed out. It first calls the read_chunk() function of
531 * the current audio format handler to obtain a pointer to the data to be
532 * sent out as well as its length. This information is then passed to each
533 * supported sender's send() function which does the actual sending.
535 void vss_send_chunk(void)
538 struct audio_format_handler
*af
;
539 struct timeval now
, due
;
541 if (mmd
->audio_format
< 0 || !map
|| !vss_playing())
543 af
= &afl
[mmd
->audio_format
];
544 gettimeofday(&now
, NULL
);
545 vss_next_chunk_time(&due
);
546 if (tv_diff(&due
, &now
, NULL
) > 0)
548 if (chk_barrier("eof", &now
, &eof_barrier
, &due
, 1) < 0)
550 if (chk_barrier("data send", &now
, &data_send_barrier
,
553 mmd
->new_vss_status_flags
&= ~VSS_REPOS
;
554 if (mmd
->current_chunk
>= mmd
->afi
.chunks_total
) { /* eof */
555 mmd
->new_vss_status_flags
|= VSS_NEXT
;
559 * We call the send function also in case of empty chunks as they
560 * might have still some data queued which can be sent in this case.
562 if (!mmd
->chunks_sent
) {
564 gettimeofday(&mmd
->stream_start
, NULL
);
565 tv_scale(mmd
->current_chunk
, &mmd
->afi
.chunk_tv
, &tmp
);
566 mmd
->offset
= tv2ms(&tmp
);
569 for (i
= 0; senders
[i
].name
; i
++) {
572 get_chunk(mmd
->current_chunk
, &buf
, &len
);
573 senders
[i
].send(mmd
->current_chunk
, mmd
->chunks_sent
, buf
, len
);
575 mmd
->new_vss_status_flags
|= VSS_PLAYING
;
577 mmd
->current_chunk
++;