2 * Copyright (C) 1997-2007 Andre Noll <maan@systemlinux.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
19 /** \file vss.c the virtual streaming system
21 * This contains the audio sending part of para_server which is independent of
22 * the current audio format, audio file selector and of the activated senders.
26 #include <sys/mman.h> /* mmap */
27 #include <sys/time.h> /* gettimeofday */
28 #include "server.cmdline.h"
36 extern const char *status_item_list
[];
38 static struct timeval announce_tv
;
39 static struct timeval data_send_barrier
;
40 static struct timeval eof_barrier
;
41 static struct timeval autoplay_barrier
;
43 extern struct misc_meta_data
*mmd
;
44 extern struct audio_file_selector selectors
[];
45 extern struct sender senders
[];
47 static FILE *audio_file
= NULL
;
51 void mp3_init(struct audio_format_handler
*);
55 void ogg_init(struct audio_format_handler
*);
58 void aac_afh_init(struct audio_format_handler
*);
62 * the list of supported audio formats
64 static struct audio_format_handler afl
[] = {
88 /** iterate over each supported audio format */
89 #define FOR_EACH_AUDIO_FORMAT(i) for (i = 0; afl[i].name; i++)
94 * check if vss status flag \a P (playing) is set
96 * \return greater than zero if playing, zero otherwise.
99 unsigned int vss_playing(void)
101 return mmd
->new_vss_status_flags
& VSS_PLAYING
;
105 * check if \a N (next) status flag is set
107 * \return greater than zero if set, zero if not.
110 unsigned int vss_next(void)
112 return mmd
->new_vss_status_flags
& VSS_NEXT
;
116 * check if a reposition request is pending
118 * \return greater than zero if true, zero otherwise.
121 unsigned int vss_repos(void)
123 return mmd
->new_vss_status_flags
& VSS_REPOS
;
127 * check if the vss is currently paused
129 * \return greater than zero if paused, zero otherwise.
132 unsigned int vss_paused(void)
134 return !(mmd
->new_vss_status_flags
& VSS_NEXT
)
135 && !(mmd
->new_vss_status_flags
& VSS_PLAYING
);
139 * get the name of the given audio format
140 * \param i the audio format number
142 * This returns a pointer to statically allocated memory so it
143 * must not be freed by the caller.
145 const char *audio_format_name(int i
)
147 return i
>= 0? afl
[i
].name
: "(none)";
151 * initialize the virtual streaming system
153 * Call the init functions of all supported audio format handlers and
154 * initialize all supported senders.
159 char *hn
= para_hostname(), *home
= para_homedir();
161 PARA_DEBUG_LOG("supported audio formats: %s\n",
162 SUPPORTED_AUDIO_FORMATS
);
163 for (i
= 0; afl
[i
].name
; i
++) {
164 PARA_NOTICE_LOG("initializing %s handler\n",
166 afl
[i
].init(&afl
[i
]);
168 ms2tv(conf
.announce_time_arg
, &announce_tv
);
169 PARA_INFO_LOG("announce timeval: %lums\n", tv2ms(&announce_tv
));
170 for (i
= 0; senders
[i
].name
; i
++) {
171 PARA_NOTICE_LOG("initializing %s sender\n", senders
[i
].name
);
172 senders
[i
].init(&senders
[i
]);
176 if (conf
.autoplay_given
) {
177 struct timeval now
, tmp
;
178 mmd
->vss_status_flags
|= VSS_PLAYING
;
179 mmd
->new_vss_status_flags
|= VSS_PLAYING
;
180 gettimeofday(&now
, NULL
);
181 ms2tv(conf
.autoplay_delay_arg
, &tmp
);
182 tv_add(&now
, &tmp
, &autoplay_barrier
);
186 static int get_file_info(int i
)
188 return afl
[i
].get_file_info(audio_file
, map
, mmd
->size
, &mmd
->afi
);
192 * guess the audio format judging from filename
194 * \param name the filename
196 * \return This function returns -1 if it has no idea what kind of audio
197 * file this might be. Otherwise the (non-negative) number of the audio format
200 int guess_audio_format(const char *name
)
202 int i
,j
, len
= strlen(name
);
204 FOR_EACH_AUDIO_FORMAT(i
) {
205 for (j
= 0; afl
[i
].suffixes
[j
]; j
++) {
206 const char *p
= afl
[i
].suffixes
[j
];
207 int plen
= strlen(p
);
210 if (name
[len
- plen
- 1] != '.')
212 if (strcasecmp(name
+ len
- plen
, p
))
214 // PARA_DEBUG_LOG("might be %s\n", audio_format_name(i));
221 static int get_audio_format(int omit
)
225 FOR_EACH_AUDIO_FORMAT(i
) {
229 if (get_file_info(i
) > 0)
233 return -E_AUDIO_FORMAT
;
239 static int update_mmd(void)
241 int i
, fd
= fileno(audio_file
);
242 struct stat file_status
;
244 if (fstat(fd
, &file_status
) == -1)
246 mmd
->size
= file_status
.st_size
;
247 mmd
->mtime
= file_status
.st_mtime
;
248 map
= para_mmap(file_status
.st_size
, PROT_READ
, MAP_PRIVATE
, fd
, 0);
249 i
= guess_audio_format(mmd
->filename
);
250 if (i
< 0 || get_file_info(i
) < 0)
251 i
= get_audio_format(i
);
254 mmd
->audio_format
= i
;
255 mmd
->chunks_sent
= 0;
256 mmd
->current_chunk
= 0;
259 PARA_NOTICE_LOG("next audio file: %s\n", mmd
->filename
);
263 static void vss_get_audio_file(void)
265 char **sl
= selectors
[mmd
->selector_num
].get_audio_file_list(10);
270 for (i
= 0; sl
[i
]; i
++) {
272 PARA_INFO_LOG("trying %s\n", sl
[i
]);
273 if (strlen(sl
[i
]) >= _POSIX_PATH_MAX
)
275 audio_file
= fopen(sl
[i
], "r");
278 strcpy(mmd
->filename
, sl
[i
]);
279 if (update_mmd() < 0) {
285 if (selectors
[mmd
->selector_num
].update_audio_file
)
286 selectors
[mmd
->selector_num
].update_audio_file(sl
[i
]);
287 PARA_DEBUG_LOG("%s", "success\n");
288 mmd
->new_vss_status_flags
&= (~VSS_NEXT
);
289 gettimeofday(&now
, NULL
);
290 tv_add(&now
, &announce_tv
, &data_send_barrier
);
294 PARA_ERROR_LOG("%s", "no valid files found\n");
296 mmd
->new_vss_status_flags
= VSS_NEXT
;
299 for (i
= 0; sl
[i
]; i
++)
305 static int chk_barrier(const char *bname
, const struct timeval
*now
,
306 const struct timeval
*barrier
, struct timeval
*diff
,
311 if (tv_diff(now
, barrier
, diff
) > 0)
315 PARA_DEBUG_LOG("%s barrier: %lims left\n", bname
, ms
);
319 static void vss_next_chunk_time(struct timeval
*due
)
323 tv_scale(mmd
->chunks_sent
, &mmd
->afi
.chunk_tv
, &tmp
);
324 tv_add(&tmp
, &mmd
->stream_start
, due
);
328 * != NULL: timeout for next chunk
329 * NULL: nothing to do
331 static struct timeval
*vss_compute_timeout(void)
333 static struct timeval the_timeout
;
334 struct timeval now
, next_chunk
;
336 if (vss_next() && mmd
->audio_format
>= 0) {
337 /* only sleep a bit, nec*/
338 the_timeout
.tv_sec
= 0;
339 the_timeout
.tv_usec
= 100;
342 gettimeofday(&now
, NULL
);
343 if (chk_barrier("autoplay_delay", &now
, &autoplay_barrier
,
344 &the_timeout
, 1) < 0)
346 if (chk_barrier("eof", &now
, &eof_barrier
, &the_timeout
, 1) < 0)
348 if (chk_barrier("data send", &now
, &data_send_barrier
,
349 &the_timeout
, 1) < 0)
351 if (mmd
->audio_format
< 0 || !vss_playing() || !audio_file
)
353 vss_next_chunk_time(&next_chunk
);
354 if (chk_barrier(afl
[mmd
->audio_format
].name
, &now
, &next_chunk
,
355 &the_timeout
, 0) < 0)
357 /* chunk is due or bof */
358 the_timeout
.tv_sec
= 0;
359 the_timeout
.tv_usec
= 0;
363 static void vss_eof(struct audio_format_handler
*af
)
369 if (!af
|| !audio_file
) {
370 for (i
= 0; senders
[i
].name
; i
++)
371 senders
[i
].shutdown_clients();
374 gettimeofday(&now
, NULL
);
375 tv_add(&mmd
->afi
.eof_tv
, &now
, &eof_barrier
);
376 munmap(map
, mmd
->size
);
379 mmd
->audio_format
= -1;
381 mmd
->chunks_sent
= 0;
383 mmd
->afi
.seconds_total
= 0;
384 free(mmd
->afi
.chunk_table
);
385 mmd
->afi
.chunk_table
= NULL
;
386 free(mmd
->afi
.header
);
387 mmd
->afi
.header
= NULL
;
388 tmp
= make_message("%s:\n%s:\n%s:\n", status_item_list
[SI_AUDIO_INFO1
],
389 status_item_list
[SI_AUDIO_INFO2
], status_item_list
[SI_AUDIO_INFO3
]);
390 strcpy(mmd
->afi
.info_string
, tmp
);
392 tmp
= make_message("%s:\n%s:\n%s:\n", status_item_list
[SI_DBINFO1
],
393 status_item_list
[SI_DBINFO2
], status_item_list
[SI_DBINFO3
]);
394 strcpy(mmd
->selector_info
, tmp
);
396 mmd
->filename
[0] = '\0';
402 * get the header and of the current audio file
404 * \param header_len the length of the header is stored here
406 * \return a pointer to a buffer containing the header, or NULL, if no audio
407 * file is selected or if the current audio format does not need special header
411 char *vss_get_header(int *header_len
)
413 if (mmd
->audio_format
< 0)
415 *header_len
= mmd
->afi
.header_len
;
416 return mmd
->afi
.header
;
420 * get the list of all supported audio formats
422 * \return a space separated list of all supported audio formats
423 * It is not allocated at runtime, i.e. there is no need to free
424 * the returned string in the caller.
426 const char *supported_audio_formats(void)
428 return SUPPORTED_AUDIO_FORMATS
;
432 * get the chunk time of the current audio file
434 * \return a pointer to a struct containing the chunk time, or NULL,
435 * if currently no audio file is selected.
437 struct timeval
*vss_chunk_time(void)
439 if (mmd
->audio_format
< 0)
441 return &mmd
->afi
.chunk_tv
;
445 * compute the timeout for para_server's main select-loop
447 * This function gets called from para_server to determine the timeout value
448 * for its main select loop.
450 * Before the timeout is computed, the current vss status flags are evaluated
451 * and acted upon by calling appropriate functions from the lower layers.
452 * Possible actions include
454 * - request a new file list from the current audio file selector
455 * - shutdown of all senders (stop/pause command)
456 * - reposition the stream (ff/jmp command)
458 * \return A pointer to a struct timeval containing the timeout for the next
459 * chunk of data to be sent, or NULL if we're not sending right now.
461 struct timeval
*vss_preselect(void)
463 struct audio_format_handler
*af
= NULL
;
467 format
= mmd
->audio_format
;
471 for (i
= 0; senders
[i
].name
; i
++)
472 senders
[i
].shutdown_clients();
473 if (vss_next() && af
) {
475 return vss_compute_timeout();
477 if (vss_paused() || vss_repos()) {
478 for (i
= 0; senders
[i
].name
; i
++)
479 senders
[i
].shutdown_clients();
482 gettimeofday(&now
, NULL
);
483 if (!vss_paused() || mmd
->chunks_sent
)
484 tv_add(&mmd
->afi
.eof_tv
, &now
, &eof_barrier
);
486 tv_add(&now
, &announce_tv
, &data_send_barrier
);
487 if (mmd
->new_vss_status_flags
& VSS_NOMORE
)
488 mmd
->new_vss_status_flags
= VSS_NEXT
;
490 mmd
->chunks_sent
= 0;
493 mmd
->new_vss_status_flags
&= ~(VSS_REPOS
);
494 mmd
->current_chunk
= mmd
->repos_request
;
496 ret
= vss_compute_timeout();
497 if (!ret
&& !audio_file
&& vss_playing() &&
498 !(mmd
->new_vss_status_flags
& VSS_NOMORE
)) {
499 PARA_DEBUG_LOG("%s", "ready and playing, but no audio file\n");
500 vss_get_audio_file();
507 * main sending function
509 * This function gets called from para_server as soon as the next chunk of
510 * data should be pushed out. It first calls the read_chunk() function of
511 * the current audio format handler to obtain a pointer to the data to be
512 * sent out as well as its length. This information is then passed to each
513 * supported sender's send() function which does the actual sending.
515 void vss_send_chunk(void)
518 struct audio_format_handler
*af
;
520 struct timeval now
, due
;
522 if (mmd
->audio_format
< 0 || !audio_file
|| !vss_playing())
524 af
= &afl
[mmd
->audio_format
];
525 gettimeofday(&now
, NULL
);
526 vss_next_chunk_time(&due
);
527 if (tv_diff(&due
, &now
, NULL
) > 0)
529 if (chk_barrier("eof", &now
, &eof_barrier
, &due
, 1) < 0)
531 if (chk_barrier("data send", &now
, &data_send_barrier
,
534 mmd
->new_vss_status_flags
&= ~VSS_REPOS
;
535 if (mmd
->current_chunk
>= mmd
->afi
.chunks_total
) { /* eof */
536 mmd
->new_vss_status_flags
|= VSS_NEXT
;
539 pos
= mmd
->afi
.chunk_table
[mmd
->current_chunk
];
540 len
= mmd
->afi
.chunk_table
[mmd
->current_chunk
+ 1] - pos
;
542 * We call the send function also in case of empty chunks as they
543 * might have still some data queued which can be sent in this case.
545 if (!mmd
->chunks_sent
) {
547 gettimeofday(&mmd
->stream_start
, NULL
);
548 tv_scale(mmd
->current_chunk
, &mmd
->afi
.chunk_tv
, &tmp
);
549 mmd
->offset
= tv2ms(&tmp
);
552 for (i
= 0; senders
[i
].name
; i
++)
553 senders
[i
].send(mmd
->current_chunk
, mmd
->chunks_sent
,
555 mmd
->new_vss_status_flags
|= VSS_PLAYING
;
557 mmd
->current_chunk
++;