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
22 * of the current audio format, database tool and of the activated senders.
25 #include <sys/time.h> /* gettimeofday */
26 #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 dblist
[];
42 extern struct sender senders
[];
43 extern struct gengetopt_args_info conf
;
45 static FILE *audio_file
= NULL
;
48 void mp3_init(void *);
52 void ogg_init(void *);
56 * the list of supported audio formats
58 struct audio_format afl
[] = {
78 * check if audio file sender is playing
80 * \return greater than zero if playing, zero otherwise.
83 unsigned int afs_playing(void)
85 return mmd
->new_afs_status_flags
& AFS_PLAYING
;
89 * check if 'next' flag is set afs_status_flags
91 * \return greater than zero if set, zero if not.
94 unsigned int afs_next(void)
96 return mmd
->new_afs_status_flags
& AFS_NEXT
;
100 * check if a reposition request is pending
102 * \return greater than zero if true, zero otherwise.
105 unsigned int afs_repos(void)
107 return mmd
->new_afs_status_flags
& AFS_REPOS
;
111 * check if audio file sender is paused
113 * \return greater than zero if paused, zero otherwise.
116 unsigned int afs_paused(void)
118 return !(mmd
->new_afs_status_flags
& AFS_NEXT
)
119 && !(mmd
->new_afs_status_flags
& AFS_PLAYING
);
123 * get the name of the given audio format
124 * \param i the audio format number
126 * This returns a pointer to statically allocated memory so it
127 * must not be freed by the caller.
129 const char *audio_format_name(int i
)
131 return i
>= 0? afl
[i
].name
: "(none)";
135 * initialize the audio file sender
137 * Call the init functions of all supported audio format handlers and
138 * initialize all supported senders.
143 char *hn
= para_hostname(), *home
= para_homedir();
145 PARA_DEBUG_LOG("supported audio formats: %s\n",
146 SUPPORTED_AUDIO_FORMATS
);
147 for (i
= 0; afl
[i
].name
; i
++) {
148 PARA_NOTICE_LOG("initializing %s handler\n",
150 afl
[i
].init(&afl
[i
]);
152 ms2tv(conf
.announce_time_arg
, &announce_tv
);
153 PARA_INFO_LOG("announce timeval: %lu:%lu\n", announce_tv
.tv_sec
, announce_tv
.tv_usec
);
154 for (i
= 0; senders
[i
].name
; i
++) {
155 PARA_NOTICE_LOG("initializing %s sender\n", senders
[i
].name
);
156 senders
[i
].init(&senders
[i
]);
162 static int get_file_info(int i
)
164 return afl
[i
].get_file_info(audio_file
, mmd
->audio_file_info
,
165 &mmd
->chunks_total
, &mmd
->seconds_total
);
169 * guess the audio format judging from filename
170 * \param name the filename
172 * \return This function returns -1 if it has no idea what kind of audio
173 * file this might be. Otherwise the (non-negative) number of the audio format
176 static int guess_audio_format(const char *name
)
179 int i
, len1
= strlen(name
), len2
;
181 for (i
= 0; afl
[i
].name
; i
++) {
182 len2
= strlen(afl
[i
].name
);
185 if (!strncasecmp(name
+ (len1
- len2
), afl
[i
].name
, len2
)) {
186 PARA_DEBUG_LOG("might be %s\n", afl
[i
].name
);
193 static int get_audio_format(int omit
)
197 for (i
= 0; afl
[i
].name
; i
++) {
198 if (i
== omit
|| !afl
[i
].get_file_info
)
201 if (get_file_info(i
) > 0)
205 return -E_AUDIO_FORMAT
;
211 static int update_mmd(void)
214 struct stat file_status
;
216 i
= guess_audio_format(mmd
->filename
);
217 if (i
< 0 || get_file_info(i
) < 0)
218 i
= get_audio_format(i
);
221 mmd
->audio_format
= i
;
222 mmd
->chunks_sent
= 0;
223 mmd
->current_chunk
= 0;
225 if (fstat(fileno(audio_file
), &file_status
) == -1)
227 mmd
->size
= file_status
.st_size
;
228 mmd
->mtime
= file_status
.st_mtime
;
230 PARA_NOTICE_LOG("next audio file: %s\n", mmd
->filename
);
234 static void get_song(void)
236 char **sl
= dblist
[mmd
->dbt_num
].get_audio_file_list(10);
241 for (i
= 0; sl
[i
]; i
++) {
243 PARA_INFO_LOG("trying %s\n", sl
[i
]);
244 if (strlen(sl
[i
]) >= _POSIX_PATH_MAX
)
246 audio_file
= fopen(sl
[i
], "r");
249 strcpy(mmd
->filename
, sl
[i
]);
250 if (update_mmd() < 0) {
256 if (dblist
[mmd
->dbt_num
].update_audio_file
)
257 dblist
[mmd
->dbt_num
].update_audio_file(sl
[i
]);
258 PARA_DEBUG_LOG("%s", "success\n");
259 mmd
->new_afs_status_flags
&= (~AFS_NEXT
);
260 gettimeofday(&now
, NULL
);
261 tv_add(&now
, &announce_tv
, &data_send_barrier
);
265 PARA_ERROR_LOG("%s", "no valid files found\n");
267 mmd
->new_afs_status_flags
= AFS_NEXT
;
270 for (i
= 0; sl
[i
]; i
++)
276 static int chk_barrier(const char *bname
, const struct timeval
*now
,
277 const struct timeval
*barrier
, struct timeval
*diff
, int log
)
281 if (tv_diff(now
, barrier
, diff
) > 0)
285 PARA_DEBUG_LOG("%s barrier: %lims left\n", bname
, ms
);
289 static void afs_next_chunk_time(struct timeval
*due
)
293 tv_scale(mmd
->chunks_sent
, &afl
[mmd
->audio_format
].chunk_tv
, &tmp
);
294 tv_add(&tmp
, &mmd
->stream_start
, due
);
298 * != NULL: timeout for next chunk
299 * NULL: nothing to do
301 static struct timeval
*afs_compute_timeout(void)
303 static struct timeval the_timeout
;
304 struct timeval now
, next_chunk
;
306 if (afs_next() && mmd
->audio_format
>= 0) {
307 /* only sleep a bit, nec*/
308 the_timeout
.tv_sec
= 0;
309 the_timeout
.tv_usec
= 100;
312 gettimeofday(&now
, NULL
);
313 if (chk_barrier("eof", &now
, &eof_barrier
, &the_timeout
, 1) < 0)
315 if (chk_barrier("data send", &now
, &data_send_barrier
,
316 &the_timeout
, 1) < 0)
318 if (mmd
->audio_format
< 0 || !afs_playing() || !audio_file
)
320 afs_next_chunk_time(&next_chunk
);
321 if (chk_barrier(afl
[mmd
->audio_format
].name
, &now
, &next_chunk
,
322 &the_timeout
, 0) < 0)
324 /* chunk is due or bof */
325 the_timeout
.tv_sec
= 0;
326 the_timeout
.tv_usec
= 0;
330 static void afs_eof(struct audio_format
*af
)
336 if (!af
|| !audio_file
) {
337 for (i
= 0; senders
[i
].name
; i
++)
338 senders
[i
].shutdown_clients();
341 gettimeofday(&now
, NULL
);
342 tv_add(&af
->eof_tv
, &now
, &eof_barrier
);
343 af
->close_audio_file();
345 mmd
->audio_format
= -1;
347 mmd
->chunks_sent
= 0;
349 mmd
->seconds_total
= 0;
350 tmp
= make_message("%s:\n%s:\n%s:\n", status_item_list
[SI_AUDIO_INFO1
],
351 status_item_list
[SI_AUDIO_INFO2
], status_item_list
[SI_AUDIO_INFO3
]);
352 strcpy(mmd
->audio_file_info
, tmp
);
354 tmp
= make_message("%s:\n%s:\n%s:\n", status_item_list
[SI_DBINFO1
],
355 status_item_list
[SI_DBINFO2
], status_item_list
[SI_DBINFO3
]);
356 strcpy(mmd
->dbinfo
, tmp
);
358 mmd
->filename
[0] = '\0';
364 * compute the timeout for para_server's main select-loop
366 * This function gets called from para_server to determine the timeout value
367 * for its main select loop.
369 * Before the timeout is computed, the current afs status flags are evaluated
370 * and acted upon by calling appropriate functions from the lower layers.
371 * Possible actions include
373 * - request a new file list from the current dabase tool (audio file change)
374 * - shutdown of all senders (stop/pause command)
375 * - repositioning of the stream (ff/jmp command)
377 * \return A pointer to a struct timeval containing the timeout for the next
378 * chunk of data to be sent, or NULL if we're not sending right now.
380 struct timeval
*afs_preselect(void)
382 struct audio_format
*af
= NULL
;
386 format
= mmd
->audio_format
;
390 for (i
= 0; senders
[i
].name
; i
++)
391 senders
[i
].shutdown_clients();
392 if (afs_next() && af
) {
394 return afs_compute_timeout();
396 if (afs_paused() || afs_repos()) {
397 for (i
= 0; senders
[i
].name
; i
++)
398 senders
[i
].shutdown_clients();
401 if (!afs_paused() || mmd
->chunks_sent
) {
402 gettimeofday(&now
, NULL
);
403 tv_add(&af
->eof_tv
, &now
, &eof_barrier
);
406 tv_add(&now
, &announce_tv
, &data_send_barrier
);
407 if (mmd
->new_afs_status_flags
& AFS_NOMORE
)
408 mmd
->new_afs_status_flags
= AFS_NEXT
;
410 mmd
->chunks_sent
= 0;
412 if (af
&& afs_repos() && mmd
->current_chunk
!= mmd
->repos_request
)
413 af
->reposition_stream(mmd
->repos_request
);
415 mmd
->new_afs_status_flags
&= ~(AFS_REPOS
);
416 mmd
->current_chunk
= mmd
->repos_request
;
418 ret
= afs_compute_timeout();
419 if (!ret
&& !audio_file
&& afs_playing() &&
420 !(mmd
->new_afs_status_flags
& AFS_NOMORE
)) {
421 PARA_DEBUG_LOG("%s", "ready and playing, but no audio file\n");
429 * afs_send_chunk - paraslash's main sending function
431 * This function gets called from para_server as soon as the next chunk of
432 * data should be pushed out. It first calls the read_chunk() function of
433 * the current audio format handler to obtain a pointer to the data to be
434 * sent out as well as its length. This information is then passed to each
435 * supported sender's send() function which does the actual sending.
437 * Return value: Positive return value on success, zero on eof and negative
441 void afs_send_chunk(void)
444 struct audio_format
*af
;
447 struct timeval now
, due
;
449 if (mmd
->audio_format
< 0 || !audio_file
|| !afs_playing())
451 af
= &afl
[mmd
->audio_format
];
452 gettimeofday(&now
, NULL
);
453 afs_next_chunk_time(&due
);
454 if (tv_diff(&due
, &now
, NULL
) > 0)
456 buf
= af
->read_chunk(mmd
->current_chunk
, &ret
);
457 mmd
->new_afs_status_flags
&= ~(AFS_NEXT
| AFS_REPOS
);
460 mmd
->new_afs_status_flags
= AFS_NEXT
;
462 mmd
->new_afs_status_flags
|= AFS_NEXT
;
466 if (!mmd
->chunks_sent
) {
468 gettimeofday(&mmd
->stream_start
, NULL
);
469 tv_scale(mmd
->current_chunk
, &af
->chunk_tv
, &tmp
);
470 mmd
->offset
= tv2ms(&tmp
);
473 for (i
= 0; senders
[i
].name
; i
++)
474 senders
[i
].send(af
, mmd
->current_chunk
,
475 mmd
->chunks_sent
, buf
, ret
);
476 mmd
->new_afs_status_flags
|= AFS_PLAYING
;
478 mmd
->current_chunk
++;