2 * Copyright (C) 1997-2006 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 afs.c audio file sending functions
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"
34 extern const char *status_item_list
[];
36 static struct timeval announce_tv
;
37 static struct timeval data_send_barrier
;
38 static struct timeval eof_barrier
;
40 extern struct misc_meta_data
*mmd
;
41 extern struct audio_file_selector selectors
[];
42 extern struct sender senders
[];
43 extern struct gengetopt_args_info conf
;
45 static FILE *audio_file
= NULL
;
48 void mp3_init(struct audio_format_handler
*);
52 void ogg_init(struct audio_format_handler
*);
55 void aac_afh_init(struct audio_format_handler
*);
59 * the list of supported audio formats
61 static struct audio_format_handler afl
[] = {
84 #define FOR_EACH_AUDIO_FORMAT(i) for (i = 0; afl[i].name; i++)
87 * check if audio file sender is playing
89 * \return greater than zero if playing, zero otherwise.
92 unsigned int afs_playing(void)
94 return mmd
->new_afs_status_flags
& AFS_PLAYING
;
98 * check if 'next' flag is set afs_status_flags
100 * \return greater than zero if set, zero if not.
103 unsigned int afs_next(void)
105 return mmd
->new_afs_status_flags
& AFS_NEXT
;
109 * check if a reposition request is pending
111 * \return greater than zero if true, zero otherwise.
114 unsigned int afs_repos(void)
116 return mmd
->new_afs_status_flags
& AFS_REPOS
;
120 * check if audio file sender is paused
122 * \return greater than zero if paused, zero otherwise.
125 unsigned int afs_paused(void)
127 return !(mmd
->new_afs_status_flags
& AFS_NEXT
)
128 && !(mmd
->new_afs_status_flags
& AFS_PLAYING
);
132 * get the name of the given audio format
133 * \param i the audio format number
135 * This returns a pointer to statically allocated memory so it
136 * must not be freed by the caller.
138 const char *audio_format_name(int i
)
140 return i
>= 0? afl
[i
].name
: "(none)";
144 * initialize the audio file sender
146 * Call the init functions of all supported audio format handlers and
147 * initialize all supported senders.
152 char *hn
= para_hostname(), *home
= para_homedir();
154 PARA_DEBUG_LOG("supported audio formats: %s\n",
155 SUPPORTED_AUDIO_FORMATS
);
156 for (i
= 0; afl
[i
].name
; i
++) {
157 PARA_NOTICE_LOG("initializing %s handler\n",
159 afl
[i
].init(&afl
[i
]);
161 ms2tv(conf
.announce_time_arg
, &announce_tv
);
162 PARA_INFO_LOG("announce timeval: %lums\n", tv2ms(&announce_tv
));
163 for (i
= 0; senders
[i
].name
; i
++) {
164 PARA_NOTICE_LOG("initializing %s sender\n", senders
[i
].name
);
165 senders
[i
].init(&senders
[i
]);
171 static int get_file_info(int i
)
173 return afl
[i
].get_file_info(audio_file
, mmd
->audio_file_info
,
174 &mmd
->chunks_total
, &mmd
->seconds_total
);
178 * guess the audio format judging from filename
180 * \param name the filename
182 * \return This function returns -1 if it has no idea what kind of audio
183 * file this might be. Otherwise the (non-negative) number of the audio format
186 int guess_audio_format(const char *name
)
188 int i
,j
, len
= strlen(name
);
190 FOR_EACH_AUDIO_FORMAT(i
) {
191 for (j
= 0; afl
[i
].suffixes
[j
]; j
++) {
192 const char *p
= afl
[i
].suffixes
[j
];
193 int plen
= strlen(p
);
196 if (name
[len
- plen
- 1] != '.')
198 if (strcasecmp(name
+ len
- plen
, p
))
200 PARA_DEBUG_LOG("might be %s\n", audio_format_name(i
));
207 static int get_audio_format(int omit
)
211 FOR_EACH_AUDIO_FORMAT(i
) {
212 if (i
== omit
|| !afl
[i
].get_file_info
)
215 if (get_file_info(i
) > 0)
219 return -E_AUDIO_FORMAT
;
225 static int update_mmd(void)
228 struct stat file_status
;
230 i
= guess_audio_format(mmd
->filename
);
231 if (i
< 0 || get_file_info(i
) < 0)
232 i
= get_audio_format(i
);
235 mmd
->audio_format
= i
;
236 mmd
->chunks_sent
= 0;
237 mmd
->current_chunk
= 0;
239 if (fstat(fileno(audio_file
), &file_status
) == -1)
241 mmd
->size
= file_status
.st_size
;
242 mmd
->mtime
= file_status
.st_mtime
;
244 PARA_NOTICE_LOG("next audio file: %s\n", mmd
->filename
);
248 static void get_song(void)
250 char **sl
= selectors
[mmd
->selector_num
].get_audio_file_list(10);
255 for (i
= 0; sl
[i
]; i
++) {
257 PARA_INFO_LOG("trying %s\n", sl
[i
]);
258 if (strlen(sl
[i
]) >= _POSIX_PATH_MAX
)
260 audio_file
= fopen(sl
[i
], "r");
263 strcpy(mmd
->filename
, sl
[i
]);
264 if (update_mmd() < 0) {
270 if (selectors
[mmd
->selector_num
].update_audio_file
)
271 selectors
[mmd
->selector_num
].update_audio_file(sl
[i
]);
272 PARA_DEBUG_LOG("%s", "success\n");
273 mmd
->new_afs_status_flags
&= (~AFS_NEXT
);
274 gettimeofday(&now
, NULL
);
275 tv_add(&now
, &announce_tv
, &data_send_barrier
);
279 PARA_ERROR_LOG("%s", "no valid files found\n");
281 mmd
->new_afs_status_flags
= AFS_NEXT
;
284 for (i
= 0; sl
[i
]; i
++)
290 static int chk_barrier(const char *bname
, const struct timeval
*now
,
291 const struct timeval
*barrier
, struct timeval
*diff
, int log
)
295 if (tv_diff(now
, barrier
, diff
) > 0)
299 PARA_DEBUG_LOG("%s barrier: %lims left\n", bname
, ms
);
303 static void afs_next_chunk_time(struct timeval
*due
)
307 tv_scale(mmd
->chunks_sent
, &afl
[mmd
->audio_format
].chunk_tv
, &tmp
);
308 tv_add(&tmp
, &mmd
->stream_start
, due
);
312 * != NULL: timeout for next chunk
313 * NULL: nothing to do
315 static struct timeval
*afs_compute_timeout(void)
317 static struct timeval the_timeout
;
318 struct timeval now
, next_chunk
;
320 if (afs_next() && mmd
->audio_format
>= 0) {
321 /* only sleep a bit, nec*/
322 the_timeout
.tv_sec
= 0;
323 the_timeout
.tv_usec
= 100;
326 gettimeofday(&now
, NULL
);
327 if (chk_barrier("eof", &now
, &eof_barrier
, &the_timeout
, 1) < 0)
329 if (chk_barrier("data send", &now
, &data_send_barrier
,
330 &the_timeout
, 1) < 0)
332 if (mmd
->audio_format
< 0 || !afs_playing() || !audio_file
)
334 afs_next_chunk_time(&next_chunk
);
335 if (chk_barrier(afl
[mmd
->audio_format
].name
, &now
, &next_chunk
,
336 &the_timeout
, 0) < 0)
338 /* chunk is due or bof */
339 the_timeout
.tv_sec
= 0;
340 the_timeout
.tv_usec
= 0;
344 static void afs_eof(struct audio_format_handler
*af
)
350 if (!af
|| !audio_file
) {
351 for (i
= 0; senders
[i
].name
; i
++)
352 senders
[i
].shutdown_clients();
355 gettimeofday(&now
, NULL
);
356 tv_add(&af
->eof_tv
, &now
, &eof_barrier
);
357 af
->close_audio_file();
359 mmd
->audio_format
= -1;
361 mmd
->chunks_sent
= 0;
363 mmd
->seconds_total
= 0;
364 tmp
= make_message("%s:\n%s:\n%s:\n", status_item_list
[SI_AUDIO_INFO1
],
365 status_item_list
[SI_AUDIO_INFO2
], status_item_list
[SI_AUDIO_INFO3
]);
366 strcpy(mmd
->audio_file_info
, tmp
);
368 tmp
= make_message("%s:\n%s:\n%s:\n", status_item_list
[SI_DBINFO1
],
369 status_item_list
[SI_DBINFO2
], status_item_list
[SI_DBINFO3
]);
370 strcpy(mmd
->selector_info
, tmp
);
372 mmd
->filename
[0] = '\0';
378 * get the header and of the current audio file
380 * \param header_len the length of the header is stored here
382 * \return a pointer to a buffer containing the header, or NULL, if no audio
383 * file is selected or if the current audio format does not need special header
387 char *afs_get_header(int *header_len
)
390 if (mmd
->audio_format
< 0)
392 if (!afl
[mmd
->audio_format
].get_header_info
)
394 return afl
[mmd
->audio_format
].get_header_info(header_len
);
398 * get the chunk time of the current audio file
400 * \return a pointer to a struct containing the chunk time, or NULL,
401 * if currently no audio file is selected.
403 struct timeval
*afs_chunk_time(void)
405 if (mmd
->audio_format
< 0)
407 return &afl
[mmd
->audio_format
].chunk_tv
;
411 * compute the timeout for para_server's main select-loop
413 * This function gets called from para_server to determine the timeout value
414 * for its main select loop.
416 * Before the timeout is computed, the current afs status flags are evaluated
417 * and acted upon by calling appropriate functions from the lower layers.
418 * Possible actions include
420 * - request a new file list from the current dabase tool (audio file change)
421 * - shutdown of all senders (stop/pause command)
422 * - repositioning of the stream (ff/jmp command)
424 * \return A pointer to a struct timeval containing the timeout for the next
425 * chunk of data to be sent, or NULL if we're not sending right now.
427 struct timeval
*afs_preselect(void)
429 struct audio_format_handler
*af
= NULL
;
433 format
= mmd
->audio_format
;
437 for (i
= 0; senders
[i
].name
; i
++)
438 senders
[i
].shutdown_clients();
439 if (afs_next() && af
) {
441 return afs_compute_timeout();
443 if (afs_paused() || afs_repos()) {
444 for (i
= 0; senders
[i
].name
; i
++)
445 senders
[i
].shutdown_clients();
448 gettimeofday(&now
, NULL
);
449 if (!afs_paused() || mmd
->chunks_sent
)
450 tv_add(&af
->eof_tv
, &now
, &eof_barrier
);
452 tv_add(&now
, &announce_tv
, &data_send_barrier
);
453 if (mmd
->new_afs_status_flags
& AFS_NOMORE
)
454 mmd
->new_afs_status_flags
= AFS_NEXT
;
456 mmd
->chunks_sent
= 0;
458 if (af
&& afs_repos() && mmd
->current_chunk
!= mmd
->repos_request
)
459 af
->reposition_stream(mmd
->repos_request
);
461 mmd
->new_afs_status_flags
&= ~(AFS_REPOS
);
462 mmd
->current_chunk
= mmd
->repos_request
;
464 ret
= afs_compute_timeout();
465 if (!ret
&& !audio_file
&& afs_playing() &&
466 !(mmd
->new_afs_status_flags
& AFS_NOMORE
)) {
467 PARA_DEBUG_LOG("%s", "ready and playing, but no audio file\n");
475 * afs_send_chunk - paraslash's main sending function
477 * This function gets called from para_server as soon as the next chunk of
478 * data should be pushed out. It first calls the read_chunk() function of
479 * the current audio format handler to obtain a pointer to the data to be
480 * sent out as well as its length. This information is then passed to each
481 * supported sender's send() function which does the actual sending.
483 * Return value: Positive return value on success, zero on eof and negative
487 void afs_send_chunk(void)
490 struct audio_format_handler
*af
;
493 struct timeval now
, due
;
495 if (mmd
->audio_format
< 0 || !audio_file
|| !afs_playing())
497 af
= &afl
[mmd
->audio_format
];
498 gettimeofday(&now
, NULL
);
499 afs_next_chunk_time(&due
);
500 if (tv_diff(&due
, &now
, NULL
) > 0)
502 if (chk_barrier("eof", &now
, &eof_barrier
, &due
, 1) < 0)
504 if (chk_barrier("data send", &now
, &data_send_barrier
,
507 buf
= af
->read_chunk(mmd
->current_chunk
, &ret
);
508 mmd
->new_afs_status_flags
&= ~AFS_REPOS
;
511 mmd
->new_afs_status_flags
= AFS_NEXT
;
513 mmd
->new_afs_status_flags
|= AFS_NEXT
;
517 if (!mmd
->chunks_sent
) {
519 gettimeofday(&mmd
->stream_start
, NULL
);
520 tv_scale(mmd
->current_chunk
, &af
->chunk_tv
, &tmp
);
521 mmd
->offset
= tv2ms(&tmp
);
524 for (i
= 0; senders
[i
].name
; i
++)
525 senders
[i
].send(mmd
->current_chunk
, mmd
->chunks_sent
, buf
, ret
);
526 mmd
->new_afs_status_flags
|= AFS_PLAYING
;
528 mmd
->current_chunk
++;