- mmd->num_played++;
- if (selectors[mmd->selector_num].update_audio_file)
- selectors[mmd->selector_num].update_audio_file(sl[i]);
- PARA_DEBUG_LOG("%s", "success\n");
- mmd->new_afs_status_flags &= (~AFS_NEXT);
- gettimeofday(&now, NULL);
- tv_add(&now, &announce_tv, &data_send_barrier);
-
- goto free;
- }
- PARA_ERROR_LOG("%s", "no valid files found\n");
-err_out:
- mmd->new_afs_status_flags = AFS_NEXT;
-free:
- if (sl) {
- for (i = 0; sl[i]; i++)
- free(sl[i]);
- free(sl);
- }
-}
-
-static int chk_barrier(const char *bname, const struct timeval *now,
- const struct timeval *barrier, struct timeval *diff, int log)
-{
- long ms;
-
- if (tv_diff(now, barrier, diff) > 0)
- return 1;
- ms = tv2ms(diff);
- if (log && ms)
- PARA_DEBUG_LOG("%s barrier: %lims left\n", bname, ms);
- return -1;
-}
-
-static void afs_next_chunk_time(struct timeval *due)
-{
- struct timeval tmp;
-
- tv_scale(mmd->chunks_sent, &afl[mmd->audio_format].chunk_tv, &tmp);
- tv_add(&tmp, &mmd->stream_start, due);
-}
-
-/*
- * != NULL: timeout for next chunk
- * NULL: nothing to do
- */
-static struct timeval *afs_compute_timeout(void)
-{
- static struct timeval the_timeout;
- struct timeval now, next_chunk;
-
- if (afs_next() && mmd->audio_format >= 0) {
- /* only sleep a bit, nec*/
- the_timeout.tv_sec = 0;
- the_timeout.tv_usec = 100;
- return &the_timeout;
- }
- gettimeofday(&now, NULL);
- if (chk_barrier("eof", &now, &eof_barrier, &the_timeout, 1) < 0)
- return &the_timeout;
- if (chk_barrier("data send", &now, &data_send_barrier,
- &the_timeout, 1) < 0)
- return &the_timeout;
- if (mmd->audio_format < 0 || !afs_playing() || !audio_file)
- return NULL;
- afs_next_chunk_time(&next_chunk);
- if (chk_barrier(afl[mmd->audio_format].name, &now, &next_chunk,
- &the_timeout, 0) < 0)
- return &the_timeout;
- /* chunk is due or bof */
- the_timeout.tv_sec = 0;
- the_timeout.tv_usec = 0;
- return &the_timeout;
-}
-
-static void afs_eof(struct audio_format_handler *af)
-{
- struct timeval now;
- int i;
- char *tmp;
-
- if (!af || !audio_file) {
- for (i = 0; senders[i].name; i++)
- senders[i].shutdown_clients();
- return;
- }
- gettimeofday(&now, NULL);
- tv_add(&af->eof_tv, &now, &eof_barrier);
- af->close_audio_file();
- audio_file = NULL;
- mmd->audio_format = -1;
- af = NULL;
- mmd->chunks_sent = 0;
- mmd->offset = 0;
- mmd->seconds_total = 0;
- tmp = make_message("%s:\n%s:\n%s:\n", status_item_list[SI_AUDIO_INFO1],
- status_item_list[SI_AUDIO_INFO2], status_item_list[SI_AUDIO_INFO3]);
- strcpy(mmd->audio_file_info, tmp);
- free(tmp);
- tmp = make_message("%s:\n%s:\n%s:\n", status_item_list[SI_DBINFO1],
- status_item_list[SI_DBINFO2], status_item_list[SI_DBINFO3]);
- strcpy(mmd->selector_info, tmp);
- free(tmp);
- mmd->filename[0] = '\0';
- mmd->size = 0;
- mmd->events++;
-}
-
-/**
- * get the header and of the current audio file
- *
- * \param header_len the length of the header is stored here
- *
- * \return a pointer to a buffer containing the header, or NULL, if no audio
- * file is selected or if the current audio format does not need special header
- * treamtment.
- *
- */
-char *afs_get_header(int *header_len)
-{
- *header_len = 0;
- if (mmd->audio_format < 0)
- return NULL;
- if (!afl[mmd->audio_format].get_header_info)
- return NULL;
- return afl[mmd->audio_format].get_header_info(header_len);
-}
-
-/**
- * get the chunk time of the current audio file
- *
- * \return a pointer to a struct containing the chunk time, or NULL,
- * if currently no audio file is selected.
- */
-struct timeval *afs_chunk_time(void)
-{
- if (mmd->audio_format < 0)
- return NULL;
- return &afl[mmd->audio_format].chunk_tv;
-}
-
-/**
- * compute the timeout for para_server's main select-loop
- *
- * This function gets called from para_server to determine the timeout value
- * for its main select loop.
- *
- * Before the timeout is computed, the current afs status flags are evaluated
- * and acted upon by calling appropriate functions from the lower layers.
- * Possible actions include
- *
- * - request a new file list from the current dabase tool (audio file change)
- * - shutdown of all senders (stop/pause command)
- * - repositioning of the stream (ff/jmp command)
- *
- * \return A pointer to a struct timeval containing the timeout for the next
- * chunk of data to be sent, or NULL if we're not sending right now.
- */
-struct timeval *afs_preselect(void)
-{
- struct audio_format_handler *af = NULL;
- int i, format;
- struct timeval *ret;
-again:
- format = mmd->audio_format;
- if (format >= 0)
- af = afl + format;
- else
- for (i = 0; senders[i].name; i++)
- senders[i].shutdown_clients();
- if (afs_next() && af) {
- afs_eof(af);
- return afs_compute_timeout();
- }
- if (afs_paused() || afs_repos()) {
- for (i = 0; senders[i].name; i++)
- senders[i].shutdown_clients();
- if (af) {
- struct timeval now;
- gettimeofday(&now, NULL);
- if (!afs_paused() || mmd->chunks_sent)
- tv_add(&af->eof_tv, &now, &eof_barrier);
- if (afs_repos())
- tv_add(&now, &announce_tv, &data_send_barrier);
- if (mmd->new_afs_status_flags & AFS_NOMORE)
- mmd->new_afs_status_flags = AFS_NEXT;
- }
- mmd->chunks_sent = 0;
- }
- if (af && afs_repos() && mmd->current_chunk != mmd->repos_request)
- af->reposition_stream(mmd->repos_request);
- if (afs_repos()) {
- mmd->new_afs_status_flags &= ~(AFS_REPOS);
- mmd->current_chunk = mmd->repos_request;
- }
- ret = afs_compute_timeout();
- if (!ret && !audio_file && afs_playing() &&
- !(mmd->new_afs_status_flags & AFS_NOMORE)) {
- PARA_DEBUG_LOG("%s", "ready and playing, but no audio file\n");
- get_song();
- goto again;
- }
- return ret;
-}
-
-/**
- * afs_send_chunk - paraslash's main sending function
- *
- * This function gets called from para_server as soon as the next chunk of
- * data should be pushed out. It first calls the read_chunk() function of
- * the current audio format handler to obtain a pointer to the data to be
- * sent out as well as its length. This information is then passed to each
- * supported sender's send() function which does the actual sending.
- *
- * Return value: Positive return value on success, zero on eof and negative
- * on errors.
- */
-
-void afs_send_chunk(void)
-{
- int i;
- struct audio_format_handler *af;
- char *buf;
- ssize_t ret;
- struct timeval now, due;
-
- if (mmd->audio_format < 0 || !audio_file || !afs_playing())
- return;
- af = &afl[mmd->audio_format];
- gettimeofday(&now, NULL);
- afs_next_chunk_time(&due);
- if (tv_diff(&due, &now, NULL) > 0)
- return;
- if (chk_barrier("eof", &now, &eof_barrier, &due, 1) < 0)
- return;
- if (chk_barrier("data send", &now, &data_send_barrier,
- &due, 1) < 0)
- return;
- buf = af->read_chunk(mmd->current_chunk, &ret);
- mmd->new_afs_status_flags &= ~AFS_REPOS;
- if (!buf) {