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/time.h> /* gettimeofday */
27 #include "server.cmdline.h"
35 extern const char *status_item_list
[];
37 static struct timeval announce_tv
;
38 static struct timeval data_send_barrier
;
39 static struct timeval eof_barrier
;
40 static struct timeval autoplay_barrier
;
42 extern struct misc_meta_data
*mmd
;
43 extern struct audio_file_selector selectors
[];
44 extern struct sender senders
[];
46 static FILE *audio_file
= NULL
;
49 void mp3_init(struct audio_format_handler
*);
53 void ogg_init(struct audio_format_handler
*);
56 void aac_afh_init(struct audio_format_handler
*);
60 * the list of supported audio formats
62 static struct audio_format_handler afl
[] = {
85 #define FOR_EACH_AUDIO_FORMAT(i) for (i = 0; afl[i].name; i++)
88 * check if vss status flag \a P (playing) is set
90 * \return greater than zero if playing, zero otherwise.
93 unsigned int vss_playing(void)
95 return mmd
->new_vss_status_flags
& VSS_PLAYING
;
99 * check if \a N (next) status flag is set
101 * \return greater than zero if set, zero if not.
104 unsigned int vss_next(void)
106 return mmd
->new_vss_status_flags
& VSS_NEXT
;
110 * check if a reposition request is pending
112 * \return greater than zero if true, zero otherwise.
115 unsigned int vss_repos(void)
117 return mmd
->new_vss_status_flags
& VSS_REPOS
;
121 * check if the vss is currently paused
123 * \return greater than zero if paused, zero otherwise.
126 unsigned int vss_paused(void)
128 return !(mmd
->new_vss_status_flags
& VSS_NEXT
)
129 && !(mmd
->new_vss_status_flags
& VSS_PLAYING
);
133 * get the name of the given audio format
134 * \param i the audio format number
136 * This returns a pointer to statically allocated memory so it
137 * must not be freed by the caller.
139 const char *audio_format_name(int i
)
141 return i
>= 0? afl
[i
].name
: "(none)";
145 * initialize the virtual streaming system
147 * Call the init functions of all supported audio format handlers and
148 * initialize all supported senders.
153 char *hn
= para_hostname(), *home
= para_homedir();
155 PARA_DEBUG_LOG("supported audio formats: %s\n",
156 SUPPORTED_AUDIO_FORMATS
);
157 for (i
= 0; afl
[i
].name
; i
++) {
158 PARA_NOTICE_LOG("initializing %s handler\n",
160 afl
[i
].init(&afl
[i
]);
162 ms2tv(conf
.announce_time_arg
, &announce_tv
);
163 PARA_INFO_LOG("announce timeval: %lums\n", tv2ms(&announce_tv
));
164 for (i
= 0; senders
[i
].name
; i
++) {
165 PARA_NOTICE_LOG("initializing %s sender\n", senders
[i
].name
);
166 senders
[i
].init(&senders
[i
]);
170 if (conf
.autoplay_given
) {
171 struct timeval now
, tmp
;
172 mmd
->vss_status_flags
|= VSS_PLAYING
;
173 mmd
->new_vss_status_flags
|= VSS_PLAYING
;
174 gettimeofday(&now
, NULL
);
175 ms2tv(conf
.autoplay_delay_arg
, &tmp
);
176 tv_add(&now
, &tmp
, &autoplay_barrier
);
180 static int get_file_info(int i
)
182 return afl
[i
].get_file_info(audio_file
, mmd
->audio_file_info
,
183 &mmd
->chunks_total
, &mmd
->seconds_total
);
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
) {
221 if (i
== omit
|| !afl
[i
].get_file_info
)
224 if (get_file_info(i
) > 0)
228 return -E_AUDIO_FORMAT
;
234 static int update_mmd(void)
237 struct stat file_status
;
239 i
= guess_audio_format(mmd
->filename
);
240 if (i
< 0 || get_file_info(i
) < 0)
241 i
= get_audio_format(i
);
244 mmd
->audio_format
= i
;
245 mmd
->chunks_sent
= 0;
246 mmd
->current_chunk
= 0;
248 if (fstat(fileno(audio_file
), &file_status
) == -1)
250 mmd
->size
= file_status
.st_size
;
251 mmd
->mtime
= file_status
.st_mtime
;
253 PARA_NOTICE_LOG("next audio file: %s\n", mmd
->filename
);
257 static void get_song(void)
259 char **sl
= selectors
[mmd
->selector_num
].get_audio_file_list(10);
264 for (i
= 0; sl
[i
]; i
++) {
266 PARA_INFO_LOG("trying %s\n", sl
[i
]);
267 if (strlen(sl
[i
]) >= _POSIX_PATH_MAX
)
269 audio_file
= fopen(sl
[i
], "r");
272 strcpy(mmd
->filename
, sl
[i
]);
273 if (update_mmd() < 0) {
279 if (selectors
[mmd
->selector_num
].update_audio_file
)
280 selectors
[mmd
->selector_num
].update_audio_file(sl
[i
]);
281 PARA_DEBUG_LOG("%s", "success\n");
282 mmd
->new_vss_status_flags
&= (~VSS_NEXT
);
283 gettimeofday(&now
, NULL
);
284 tv_add(&now
, &announce_tv
, &data_send_barrier
);
288 PARA_ERROR_LOG("%s", "no valid files found\n");
290 mmd
->new_vss_status_flags
= VSS_NEXT
;
293 for (i
= 0; sl
[i
]; i
++)
299 static int chk_barrier(const char *bname
, const struct timeval
*now
,
300 const struct timeval
*barrier
, struct timeval
*diff
,
305 if (tv_diff(now
, barrier
, diff
) > 0)
309 PARA_DEBUG_LOG("%s barrier: %lims left\n", bname
, ms
);
313 static void vss_next_chunk_time(struct timeval
*due
)
317 tv_scale(mmd
->chunks_sent
, &afl
[mmd
->audio_format
].chunk_tv
, &tmp
);
318 tv_add(&tmp
, &mmd
->stream_start
, due
);
322 * != NULL: timeout for next chunk
323 * NULL: nothing to do
325 static struct timeval
*vss_compute_timeout(void)
327 static struct timeval the_timeout
;
328 struct timeval now
, next_chunk
;
330 if (vss_next() && mmd
->audio_format
>= 0) {
331 /* only sleep a bit, nec*/
332 the_timeout
.tv_sec
= 0;
333 the_timeout
.tv_usec
= 100;
336 gettimeofday(&now
, NULL
);
337 if (chk_barrier("autoplay_delay", &now
, &autoplay_barrier
,
338 &the_timeout
, 1) < 0)
340 if (chk_barrier("eof", &now
, &eof_barrier
, &the_timeout
, 1) < 0)
342 if (chk_barrier("data send", &now
, &data_send_barrier
,
343 &the_timeout
, 1) < 0)
345 if (mmd
->audio_format
< 0 || !vss_playing() || !audio_file
)
347 vss_next_chunk_time(&next_chunk
);
348 if (chk_barrier(afl
[mmd
->audio_format
].name
, &now
, &next_chunk
,
349 &the_timeout
, 0) < 0)
351 /* chunk is due or bof */
352 the_timeout
.tv_sec
= 0;
353 the_timeout
.tv_usec
= 0;
357 static void vss_eof(struct audio_format_handler
*af
)
363 if (!af
|| !audio_file
) {
364 for (i
= 0; senders
[i
].name
; i
++)
365 senders
[i
].shutdown_clients();
368 gettimeofday(&now
, NULL
);
369 tv_add(&af
->eof_tv
, &now
, &eof_barrier
);
370 af
->close_audio_file();
372 mmd
->audio_format
= -1;
374 mmd
->chunks_sent
= 0;
376 mmd
->seconds_total
= 0;
377 tmp
= make_message("%s:\n%s:\n%s:\n", status_item_list
[SI_AUDIO_INFO1
],
378 status_item_list
[SI_AUDIO_INFO2
], status_item_list
[SI_AUDIO_INFO3
]);
379 strcpy(mmd
->audio_file_info
, tmp
);
381 tmp
= make_message("%s:\n%s:\n%s:\n", status_item_list
[SI_DBINFO1
],
382 status_item_list
[SI_DBINFO2
], status_item_list
[SI_DBINFO3
]);
383 strcpy(mmd
->selector_info
, tmp
);
385 mmd
->filename
[0] = '\0';
391 * get the header and of the current audio file
393 * \param header_len the length of the header is stored here
395 * \return a pointer to a buffer containing the header, or NULL, if no audio
396 * file is selected or if the current audio format does not need special header
400 char *vss_get_header(int *header_len
)
403 if (mmd
->audio_format
< 0)
405 if (!afl
[mmd
->audio_format
].get_header_info
)
407 return afl
[mmd
->audio_format
].get_header_info(header_len
);
409 const char *supported_audio_formats(void)
411 return SUPPORTED_AUDIO_FORMATS
;
415 * get the chunk time of the current audio file
417 * \return a pointer to a struct containing the chunk time, or NULL,
418 * if currently no audio file is selected.
420 struct timeval
*vss_chunk_time(void)
422 if (mmd
->audio_format
< 0)
424 return &afl
[mmd
->audio_format
].chunk_tv
;
428 * compute the timeout for para_server's main select-loop
430 * This function gets called from para_server to determine the timeout value
431 * for its main select loop.
433 * Before the timeout is computed, the current vss status flags are evaluated
434 * and acted upon by calling appropriate functions from the lower layers.
435 * Possible actions include
437 * - request a new file list from the current audio file selector
438 * - shutdown of all senders (stop/pause command)
439 * - reposition the stream (ff/jmp command)
441 * \return A pointer to a struct timeval containing the timeout for the next
442 * chunk of data to be sent, or NULL if we're not sending right now.
444 struct timeval
*vss_preselect(void)
446 struct audio_format_handler
*af
= NULL
;
450 format
= mmd
->audio_format
;
454 for (i
= 0; senders
[i
].name
; i
++)
455 senders
[i
].shutdown_clients();
456 if (vss_next() && af
) {
458 return vss_compute_timeout();
460 if (vss_paused() || vss_repos()) {
461 for (i
= 0; senders
[i
].name
; i
++)
462 senders
[i
].shutdown_clients();
465 gettimeofday(&now
, NULL
);
466 if (!vss_paused() || mmd
->chunks_sent
)
467 tv_add(&af
->eof_tv
, &now
, &eof_barrier
);
469 tv_add(&now
, &announce_tv
, &data_send_barrier
);
470 if (mmd
->new_vss_status_flags
& VSS_NOMORE
)
471 mmd
->new_vss_status_flags
= VSS_NEXT
;
473 mmd
->chunks_sent
= 0;
475 if (af
&& vss_repos() && mmd
->current_chunk
!= mmd
->repos_request
)
476 af
->reposition_stream(mmd
->repos_request
);
478 mmd
->new_vss_status_flags
&= ~(VSS_REPOS
);
479 mmd
->current_chunk
= mmd
->repos_request
;
481 ret
= vss_compute_timeout();
482 if (!ret
&& !audio_file
&& vss_playing() &&
483 !(mmd
->new_vss_status_flags
& VSS_NOMORE
)) {
484 PARA_DEBUG_LOG("%s", "ready and playing, but no audio file\n");
492 * main sending function
494 * This function gets called from para_server as soon as the next chunk of
495 * data should be pushed out. It first calls the read_chunk() function of
496 * the current audio format handler to obtain a pointer to the data to be
497 * sent out as well as its length. This information is then passed to each
498 * supported sender's send() function which does the actual sending.
500 * Return value: Positive return value on success, zero on eof and negative
504 void vss_send_chunk(void)
507 struct audio_format_handler
*af
;
510 struct timeval now
, due
;
512 if (mmd
->audio_format
< 0 || !audio_file
|| !vss_playing())
514 af
= &afl
[mmd
->audio_format
];
515 gettimeofday(&now
, NULL
);
516 vss_next_chunk_time(&due
);
517 if (tv_diff(&due
, &now
, NULL
) > 0)
519 if (chk_barrier("eof", &now
, &eof_barrier
, &due
, 1) < 0)
521 if (chk_barrier("data send", &now
, &data_send_barrier
,
524 buf
= af
->read_chunk(mmd
->current_chunk
, &ret
);
525 mmd
->new_vss_status_flags
&= ~VSS_REPOS
;
528 mmd
->new_vss_status_flags
= VSS_NEXT
;
530 mmd
->new_vss_status_flags
|= VSS_NEXT
;
534 if (!mmd
->chunks_sent
) {
536 gettimeofday(&mmd
->stream_start
, NULL
);
537 tv_scale(mmd
->current_chunk
, &af
->chunk_tv
, &tmp
);
538 mmd
->offset
= tv2ms(&tmp
);
541 for (i
= 0; senders
[i
].name
; i
++)
542 senders
[i
].send(mmd
->current_chunk
, mmd
->chunks_sent
, buf
, ret
);
543 mmd
->new_vss_status_flags
|= VSS_PLAYING
;
545 mmd
->current_chunk
++;