Exemplo n.º 1
0
/* Seek to a nonbuffered part of a handle by rebuffering the data. */
static void rebuffer_handle(int handle_id, size_t newpos)
{
    struct memory_handle *h = find_handle(handle_id);
    if (!h)
        return;

    /* When seeking foward off of the buffer, if it is a short seek don't
       rebuffer the whole track, just read enough to satisfy */
    if (newpos > h->offset && newpos - h->offset < BUFFERING_DEFAULT_FILECHUNK)
    {
        LOGFQUEUE("buffering >| Q_BUFFER_HANDLE %d", handle_id);
        queue_send(&buffering_queue, Q_BUFFER_HANDLE, handle_id);
        h->ridx = h->data + newpos;
        return;
    }

    h->offset = newpos;

    /* Reset the handle to its new offset */
    LOGFQUEUE("buffering >| Q_RESET_HANDLE %d", handle_id);
    queue_send(&buffering_queue, Q_RESET_HANDLE, handle_id);

    uintptr_t next = ringbuf_offset(h->next);
    if (ringbuf_sub(next, h->data) < h->filesize - newpos)
    {
        /* There isn't enough space to rebuffer all of the track from its new
           offset, so we ask the user to free some */
        DEBUGF("%s(): space is needed\n", __func__);
        send_event(BUFFER_EVENT_REBUFFER, &handle_id);
    }

    /* Now we ask for a rebuffer */
    LOGFQUEUE("buffering >| Q_BUFFER_HANDLE %d", handle_id);
    queue_send(&buffering_queue, Q_BUFFER_HANDLE, handle_id);
}
Exemplo n.º 2
0
/* Close the handle. Return true for success and false for failure */
bool bufclose(int handle_id)
{
    logf("bufclose(%d)", handle_id);

    LOGFQUEUE("buffering >| Q_CLOSE_HANDLE %d", handle_id);
    return queue_send(&buffering_queue, Q_CLOSE_HANDLE, handle_id);
}
Exemplo n.º 3
0
/* Load a decoder or encoder and set the format type */
bool codec_load(int hid, int cod_spec)
{
    struct codec_load_info parm = { hid, cod_spec };

    LOGFQUEUE("audio >| codec Q_CODEC_LOAD: %d, %d", hid, cod_spec);
    return codec_queue_send(Q_CODEC_LOAD, (intptr_t)&parm) != 0;
}
Exemplo n.º 4
0
/* Tell is voice is still in a playing state */
bool mp3_is_playing(void)
{
    /* TODO: Implement a timeout or state query function for event objects */
    LOGFQUEUE("mp3 >| voice Q_VOICE_STATE");
    int state = queue_send(&voice_queue, Q_VOICE_STATE, 0);
    return state != TSTATE_STOPPED;
}
Exemplo n.º 5
0
static enum codec_command_action
codec_get_command_callback(intptr_t *param)
{
    yield();

    if (LIKELY(queue_empty(&codec_queue)))
        return CODEC_ACTION_NULL; /* As you were */

    /* Process the message - return requested action and data (if any should
       be expected) */
    while (1)
    {
        enum codec_command_action action = CODEC_ACTION_NULL;
        struct queue_event ev;
        queue_wait(&codec_queue, &ev);

        switch (ev.id)
        {
        case Q_CODEC_RUN:   /* Already running */
            LOGFQUEUE("codec < Q_CODEC_RUN");
            break;

        case Q_CODEC_PAUSE: /* Stay here and wait */
            LOGFQUEUE("codec < Q_CODEC_PAUSE");
            codec_queue_ack(Q_CODEC_PAUSE);
            continue;

        case Q_CODEC_SEEK:  /* Audio wants codec to seek */
            LOGFQUEUE("codec < Q_CODEC_SEEK %ld", ev.data);
            *param = ev.data;
            action = CODEC_ACTION_SEEK_TIME;
            break;

        case Q_CODEC_STOP:  /* Must only return 0 in main loop */
            LOGFQUEUE("codec < Q_CODEC_STOP");
            action = CODEC_ACTION_HALT;
            break;

        default:            /* This is in error in this context. */
            ev.id = Q_NULL;
            logf("codec bad req %ld (%s)", ev.id, __func__);
        }

        codec_queue_ack(ev.id);
        return action;
    }
}
Exemplo n.º 6
0
static bool codec_load_next_track(void)
{
    intptr_t result = Q_CODEC_REQUEST_FAILED;

    audio_set_prev_elapsed(thistrack_id3->elapsed);

#ifdef AB_REPEAT_ENABLE
    ab_end_of_track_report();
#endif

    logf("Request new track");

    if (ci.new_track == 0)
    {
        ci.new_track++;
        automatic_skip = true;
    }

    if (!ci.stop_codec)
    {
        trigger_cpu_boost();
        LOGFQUEUE("codec >| audio Q_AUDIO_CHECK_NEW_TRACK");
        result = queue_send(&audio_queue, Q_AUDIO_CHECK_NEW_TRACK, 0);
    }

    switch (result)
    {
        case Q_CODEC_REQUEST_COMPLETE:
            LOGFQUEUE("codec |< Q_CODEC_REQUEST_COMPLETE");
            pcmbuf_start_track_change(automatic_skip);
            return true;

        case Q_CODEC_REQUEST_FAILED:
            LOGFQUEUE("codec |< Q_CODEC_REQUEST_FAILED");
            ci.new_track = 0;
            ci.stop_codec = true;
            codec_requested_stop = true;
            return false;

        default:
            LOGFQUEUE("codec |< default");
            ci.stop_codec = true;
            codec_requested_stop = true;
            return false;
    }
}
Exemplo n.º 7
0
static void NORETURN_ATTR audio_thread(void)
{
    struct queue_event ev;
    ev.id = Q_NULL; /* something not in switch below */

    pcm_postinit();

    while (1)
    {
        switch (ev.id)
        {
        /* Starts the playback engine branch */
        case Q_AUDIO_PLAY:
            LOGFQUEUE("audio < Q_AUDIO_PLAY");
            audio_playback_handler(&ev);
            continue;

        /* Playback has to handle these, even if not playing */
        case Q_AUDIO_REMAKE_AUDIO_BUFFER:
#ifdef HAVE_DISK_STORAGE
        case Q_AUDIO_UPDATE_WATERMARK:
#endif
            audio_playback_handler(&ev);
            break;

#ifdef AUDIO_HAVE_RECORDING
        /* Starts the recording engine branch */
        case Q_AUDIO_INIT_RECORDING:
            LOGFQUEUE("audio < Q_AUDIO_INIT_RECORDING");
            audio_recording_handler(&ev);
            continue;
#endif

        /* All return upon USB */
        case SYS_USB_CONNECTED:
            LOGFQUEUE("audio < SYS_USB_CONNECTED");
            voice_stop();
            usb_acknowledge(SYS_USB_CONNECTED_ACK);
            usb_wait_for_disconnect(&audio_queue);
            break;
        }

        queue_wait(&audio_queue, &ev);
    }
}
Exemplo n.º 8
0
/* Stop current voice clip from playing */
void mp3_play_stop(void)
{
    if(!audio_is_thread_ready())
       return;

    mutex_lock(&voice_mutex); /* Sync against voice_stop */
    LOGFQUEUE("mp3 >| voice Q_VOICE_STOP: 1");
    queue_send(&voice_queue, Q_VOICE_STOP, 1);

    mutex_unlock(&voice_mutex);
}
Exemplo n.º 9
0
/* Borrow the codec thread and return the ID */
void codec_thread_do_callback(void (*fn)(void), unsigned int *id)
{
    /* Set id before telling thread to call something; it may be
     * needed before this function returns. */
    if (id != NULL)
        *id = codec_thread_id;

    /* Codec thread will signal just before entering callback */
    LOGFQUEUE("codec >| Q_CODEC_DO_CALLBACK");
    queue_send(&codec_queue, Q_CODEC_DO_CALLBACK, (intptr_t)fn);
}
/* Stop any current clip and start playing a new one */
void mp3_play_data(const unsigned char* start, int size,
                   pcm_play_callback_type get_more)
{
    if (get_more != NULL && start != NULL && (ssize_t)size > 0)
    {
        struct voice_info voice_clip =
        {
            .get_more = get_more,
            .start    = (unsigned char *)start,
            .size     = size,
        };

        LOGFQUEUE("mp3 >| voice Q_VOICE_PLAY");
        queue_send(&voice_queue, Q_VOICE_PLAY, (intptr_t)&voice_clip);
    }
}

/* Stop current voice clip from playing */
void mp3_play_stop(void)
{
    LOGFQUEUE("mp3 >| voice Q_VOICE_STOP");
    queue_send(&voice_queue, Q_VOICE_STOP, 0);
}
Exemplo n.º 11
0
/* Poll the state of the codec queue. Returns < 0 if the message is urgent
   and any state should exit, > 0 if it's a run message (and it was
   scrubbed), 0 if message was ignored. */
static int codec_check_queue__have_msg(void)
{
    struct queue_event ev;

    queue_peek(&codec_queue, &ev);

    /* Seek, pause or stop? Just peek and return if so. Codec
       must handle the command after returing. Inserts will not
       be allowed until it complies. */
    switch (ev.id)
    {
    case Q_CODEC_SEEK:
        LOGFQUEUE("codec - Q_CODEC_SEEK", ev.id);
        return -1;
    case Q_CODEC_PAUSE:
        LOGFQUEUE("codec - Q_CODEC_PAUSE", ev.id);
        return -1;
    case Q_CODEC_STOP:
        LOGFQUEUE("codec - Q_CODEC_STOP", ev.id);
        return -1;
    }

    /* This is in error in this context unless it's "go, go, go!" */
    queue_wait(&codec_queue, &ev);

    if (ev.id == Q_CODEC_RUN)
    {
        logf("codec < Q_CODEC_RUN: already running!");
        codec_queue_ack(Q_CODEC_RUN);
        return 1;
    }

    /* Ignore it */
    logf("codec < bad req %ld (%s)", ev.id, __func__);
    codec_queue_ack(Q_NULL);
    return 0;
}
Exemplo n.º 12
0
static void codec_seek_complete_callback(void)
{
    logf("seek_complete");

    /* Clear DSP */
    dsp_configure(ci.dsp, DSP_FLUSH, 0);

    /* Post notification to audio thread */
    LOGFQUEUE("audio > Q_AUDIO_CODEC_SEEK_COMPLETE");
    audio_queue_post(Q_AUDIO_CODEC_SEEK_COMPLETE, 0);

    /* Wait for urgent or go message */
    do
    {
        queue_wait(&codec_queue, NULL);
    }
    while (codec_check_queue__have_msg() == 0);
}
Exemplo n.º 13
0
/* Codec thread function */
static void NORETURN_ATTR codec_thread(void)
{
    struct queue_event ev;

    while (1)
    {
        cancel_cpu_boost();

        queue_wait(&codec_queue, &ev);

        switch (ev.id)
        {
        case Q_CODEC_LOAD:
            LOGFQUEUE("codec < Q_CODEC_LOAD");
            load_codec((const struct codec_load_info *)ev.data);
            break;

        case Q_CODEC_RUN:
            LOGFQUEUE("codec < Q_CODEC_RUN");
            run_codec();
            break;

        case Q_CODEC_PAUSE:
            LOGFQUEUE("codec < Q_CODEC_PAUSE");
            break;

        case Q_CODEC_SEEK:
            LOGFQUEUE("codec < Q_CODEC_SEEK: %lu", (unsigned long)ev.data);
            seek_codec(ev.data);
            break;

        case Q_CODEC_UNLOAD:
            LOGFQUEUE("codec < Q_CODEC_UNLOAD");
            unload_codec();
            break;

        case Q_CODEC_DO_CALLBACK:
            LOGFQUEUE("codec < Q_CODEC_DO_CALLBACK");
            do_callback((void (*)(void))ev.data);
            break;

        default:
            LOGFQUEUE("codec < default : %ld", ev.id);
        }
    }
}
Exemplo n.º 14
0
/* This function is meant to be used by the buffer request functions to
   ensure the codec is no longer active */
void voice_stop(void)
{
    mutex_lock(&voice_mutex);

    /* Stop the output and current clip */
    mp3_play_stop();

    /* Careful if using sync objects in talk.c - make sure locking order is
     * observed with one or the other always granted first */

    /* Unqueue all future clips */
    talk_force_shutup();

    /* Wait for any final queue_post to be processed */
    LOGFQUEUE("mp3 >| voice Q_VOICE_NULL");
    queue_send(&voice_queue, Q_VOICE_NULL, 0);

    mutex_unlock(&voice_mutex);
} /* voice_stop */
Exemplo n.º 15
0
/* Handle Q_CODEC_RUN */
static void run_codec(void)
{
    bool const encoder = type_is_encoder(codec_type);
    int status;

    if (codec_type == AFMT_UNKNOWN)
    {
        logf("no codec to run");
        return;
    }

    codec_queue_ack(Q_CODEC_RUN);

    trigger_cpu_boost();

    if (!encoder)
    {
        /* This will be either the initial buffered offset or where it left off
           if it remained buffered and we're skipping back to it and it is best
           to have ci.curpos in sync with the handle's read position - it's the
           codec's responsibility to ensure it has the correct positions -
           playback is sorta dumb and only has a vague idea about what to
           buffer based upon what metadata has to say */
        ci.curpos = bufftell(ci.audio_hid);

        /* Pin the codec's audio data in place */
        buf_pin_handle(ci.audio_hid, true);
    }

    status = codec_run_proc();

    if (!encoder)
    {
        /* Codec is done with it - let it move */
        buf_pin_handle(ci.audio_hid, false);

        /* Notify audio that we're done for better or worse - advise of the
           status */
        LOGFQUEUE("codec > audio Q_AUDIO_CODEC_COMPLETE: %d", status);
        audio_queue_post(Q_AUDIO_CODEC_COMPLETE, status);
    }
}
Exemplo n.º 16
0
/* Stop any current clip and start playing a new one */
void mp3_play_data(const unsigned char* start, int size,
                   pcm_more_callback_type get_more)
{
    /* Shared struct to get data to the thread - once it replies, it has
     * safely cached it in its own private data */
    static struct voice_info voice_clip SHAREDBSS_ATTR;

    if (get_more != NULL && start != NULL && (ssize_t)size > 0)
    {
        mutex_lock(&voice_mutex);

        voice_clip.get_more = get_more;
        voice_clip.start    = (unsigned char *)start;
        voice_clip.size     = size;
        LOGFQUEUE("mp3 >| voice Q_VOICE_PLAY");
        queue_send(&voice_queue, Q_VOICE_PLAY, (intptr_t)&voice_clip);

        mutex_unlock(&voice_mutex);
    }
}
/* Voice thread message processing */
static enum voice_state voice_message(struct voice_thread_data *td)
{
    if (quiet_counter > 0)
        queue_wait_w_tmo(&voice_queue, &td->ev, HZ/10);
    else
        queue_wait(&voice_queue, &td->ev);

    switch (td->ev.id)
    {
    case Q_VOICE_PLAY:
        LOGFQUEUE("voice < Q_VOICE_PLAY");
        if (quiet_counter == 0)
        {
            /* Boost CPU now */
            trigger_cpu_boost();
        }
        else
        {
            /* Stop any clip still playing */
            voice_stop_playback();
        }

        quiet_counter = QUIET_COUNT;

        /* Copy the clip info */
        td->vi = *(struct voice_info *)td->ev.data;

        /* Be sure audio buffer is initialized */
        audio_restore_playback(AUDIO_WANT_VOICE);

        /* We need nothing more from the sending thread - let it run */
        queue_reply(&voice_queue, 1);

        /* Make audio play more softly and set delay to return to normal
           playback level */
        pcmbuf_soft_mode(true);

        /* Clean-start the decoder */
        td->st = speex_decoder_init(&speex_wb_mode);

        /* Make bit buffer use our own buffer */
        speex_bits_set_bit_buffer(&td->bits, td->vi.start, td->vi.size);
        speex_decoder_ctl(td->st, SPEEX_GET_LOOKAHEAD, &td->lookahead);

        return VOICE_STATE_DECODE;

    case SYS_TIMEOUT:
        if (voice_unplayed_frames())
        {
            /* Waiting for PCM to finish */
            break;
        }

        /* Drop through and stop the first time after clip runs out */
        if (quiet_counter-- != QUIET_COUNT)
        {
            if (quiet_counter <= 0)
                pcmbuf_soft_mode(false);

            break;
        }

        /* Fall-through */
    case Q_VOICE_STOP:
        LOGFQUEUE("voice < Q_VOICE_STOP");
        cancel_cpu_boost();
        voice_stop_playback();
        break;

    /* No default: no other message ids are sent */
    }

    return VOICE_STATE_MESSAGE;
}
Exemplo n.º 18
0
/** CODEC THREAD */
static void codec_thread(void)
{
    struct queue_event ev;
    int status;

    while (1) {
        status = 0;
        
#ifdef HAVE_CROSSFADE
        if (!pcmbuf_is_crossfade_active())
#endif
        {
            cancel_cpu_boost();
        }
            
        queue_wait(&codec_queue, &ev);
        codec_requested_stop = false;

        switch (ev.id) {
            case Q_CODEC_LOAD_DISK:
                LOGFQUEUE("codec < Q_CODEC_LOAD_DISK");
                queue_reply(&codec_queue, 1);
                audio_codec_loaded = true;
                ci.stop_codec = false;
                status = codec_load_file((const char *)ev.data, &ci);
                LOGFQUEUE("codec_load_file %s %d\n", (const char *)ev.data, status);
                break;

            case Q_CODEC_LOAD:
                LOGFQUEUE("codec < Q_CODEC_LOAD");
                if (*get_codec_hid() < 0) {
                    logf("Codec slot is empty!");
                    /* Wait for the pcm buffer to go empty */
                    while (pcm_is_playing())
                        yield();
                    /* This must be set to prevent an infinite loop */
                    ci.stop_codec = true;
                    LOGFQUEUE("codec > codec Q_AUDIO_PLAY");
                    queue_post(&codec_queue, Q_AUDIO_PLAY, 0);
                    break;
                }

                audio_codec_loaded = true;
                ci.stop_codec = false;
                status = codec_load_buf(*get_codec_hid(), &ci);
                LOGFQUEUE("codec_load_buf %d\n", status);
                break;

            case Q_CODEC_DO_CALLBACK:
                LOGFQUEUE("codec < Q_CODEC_DO_CALLBACK");
                queue_reply(&codec_queue, 1);
                if ((void*)ev.data != NULL)
                {
                    cpucache_invalidate();
                    ((void (*)(void))ev.data)();
                    cpucache_flush();
                }
                break;

#ifdef AUDIO_HAVE_RECORDING
            case Q_ENCODER_LOAD_DISK:
                LOGFQUEUE("codec < Q_ENCODER_LOAD_DISK");
                audio_codec_loaded = false; /* Not audio codec! */
                logf("loading encoder");
                ci.stop_encoder = false;
                status = codec_load_file((const char *)ev.data, &ci);
                logf("encoder stopped");
                break;
#endif /* AUDIO_HAVE_RECORDING */

            default:
                LOGFQUEUE("codec < default");
        }

        if (audio_codec_loaded)
        {
            if (ci.stop_codec)
            {
                status = CODEC_OK;
                if (!(audio_status() & AUDIO_STATUS_PLAY))
                    pcmbuf_play_stop();

            }
            audio_codec_loaded = false;
        }

        switch (ev.id) {
            case Q_CODEC_LOAD_DISK:
            case Q_CODEC_LOAD:
                LOGFQUEUE("codec < Q_CODEC_LOAD");
                if (audio_status() & AUDIO_STATUS_PLAY)
                {
                    if (ci.new_track || status != CODEC_OK)
                    {
                        if (!ci.new_track)
                        {
                            logf("Codec failure, %d %d", ci.new_track, status);
                            splash(HZ*2, "Codec failure");
                        }

                        if (!codec_load_next_track())
                        {
                            LOGFQUEUE("codec > audio Q_AUDIO_STOP");
                            /* End of playlist */
                            queue_post(&audio_queue, Q_AUDIO_STOP, 0);
                            break;
                        }
                    }
                    else
                    {
                        logf("Codec finished");
                        if (ci.stop_codec)
                        {
                            /* Wait for the audio to stop playing before
                             * triggering the WPS exit */
                            while(pcm_is_playing())
                            {
                                /* There has been one too many struct pointer swaps by now
                                 * so even though it says othertrack_id3, its the correct one! */
                                othertrack_id3->elapsed =
                                    othertrack_id3->length - pcmbuf_get_latency();
                                sleep(1);
                            }

                            if (codec_requested_stop)
                            {
                                LOGFQUEUE("codec > audio Q_AUDIO_STOP");
                                queue_post(&audio_queue, Q_AUDIO_STOP, 0);
                            }
                            break;
                        }
                    }

                    if (*get_codec_hid() >= 0)
                    {
                        LOGFQUEUE("codec > codec Q_CODEC_LOAD");
                        queue_post(&codec_queue, Q_CODEC_LOAD, 0);
                    }
                    else
                    {
                        const char *codec_fn =
                            get_codec_filename(thistrack_id3->codectype);
                        if (codec_fn)
                        {
                            LOGFQUEUE("codec > codec Q_CODEC_LOAD_DISK");
                            queue_post(&codec_queue, Q_CODEC_LOAD_DISK,
                                       (intptr_t)codec_fn);
                        }
                    }
                }
                break;

#ifdef AUDIO_HAVE_RECORDING
            case Q_ENCODER_LOAD_DISK:
                LOGFQUEUE("codec < Q_ENCODER_LOAD_DISK");

                if (status == CODEC_OK)
                    break;

                logf("Encoder failure");
                splash(HZ*2, "Encoder failure");

                if (ci.enc_codec_loaded < 0)
                    break;

                logf("Encoder failed to load");
                ci.enc_codec_loaded = -1;
                break;
#endif /* AUDIO_HAVE_RECORDING */

            default:
                LOGFQUEUE("codec < default");

        } /* end switch */
    }
}
Exemplo n.º 19
0
/* Voice thread message processing */
static void voice_message(struct voice_thread_data *td)
{
    while (1)
    {
        switch (td->ev.id)
        {
        case Q_VOICE_PLAY:
            LOGFQUEUE("voice < Q_VOICE_PLAY");
            /* Put up a block for completion signal */
            voice_done = false;

            /* Copy the clip info */
            td->vi = *(struct voice_info *)td->ev.data;

            /* Be sure audio buffer is initialized */
            audio_restore_playback(AUDIO_WANT_VOICE);

            /* We need nothing more from the sending thread - let it run */
            queue_reply(&voice_queue, 1);

            if (td->state == TSTATE_STOPPED)
            {
                /* Boost CPU now */
                trigger_cpu_boost();
            }
            else if (!playback_is_playing())
            {
                /* Just voice, stop any clip still playing */
                pcmbuf_play_stop();
            }

            /* Clean-start the decoder */
            td->st = speex_decoder_init(&speex_wb_mode);

            /* Make bit buffer use our own buffer */
            speex_bits_set_bit_buffer(&td->bits, td->vi.start, td->vi.size);
            speex_decoder_ctl(td->st, SPEEX_GET_LOOKAHEAD, &td->lookahead);

            td->state = TSTATE_DECODE;
            return;

        case Q_VOICE_STOP:
            LOGFQUEUE("voice < Q_VOICE_STOP: %ld", td->ev.data);

            if (td->ev.data != 0 && !playback_is_playing())
            {
                /* If not playing, it's just voice so stop pcm playback */
                pcmbuf_play_stop();
            }

            /* Cancel boost */
            cancel_cpu_boost();

            td->state = TSTATE_STOPPED;
            voice_done = true;
            break;

        case Q_VOICE_STATE:
            LOGFQUEUE("voice < Q_VOICE_STATE");
            queue_reply(&voice_queue, td->state);

            if (td->state == TSTATE_STOPPED)
                break; /* Not in a playback state */

            return;

        default:
            /* Default messages get a reply and thread continues with no
             * state transition */
            LOGFQUEUE("voice < default");

            if (td->state == TSTATE_STOPPED)
                break;  /* Not in playback state */

            queue_reply(&voice_queue, 0);
            return;
        }

        queue_wait(&voice_queue, &td->ev);
    }
}
Exemplo n.º 20
0
void buf_set_base_handle(int handle_id)
{
    LOGFQUEUE("buffering > Q_BASE_HANDLE %d", handle_id);
    queue_post(&buffering_queue, Q_BASE_HANDLE, handle_id);
}
Exemplo n.º 21
0
void buf_request_buffer_handle(int handle_id)
{
    LOGFQUEUE("buffering >| Q_START_FILL %d",handle_id);
    queue_send(&buffering_queue, Q_START_FILL, handle_id);
}
Exemplo n.º 22
0
/* Stop codec if running - codec stays resident if loaded */
void codec_stop(void)
{
    /* Wait until it's in the main loop */
    LOGFQUEUE("audio >| codec Q_CODEC_STOP");
    while (codec_queue_send(Q_CODEC_STOP, 0) != Q_NULL);
}
Exemplo n.º 23
0
/* Call the codec's exit routine and close all references */
void codec_unload(void)
{
    codec_stop();
    LOGFQUEUE("audio >| codec Q_CODEC_UNLOAD");
    codec_queue_send(Q_CODEC_UNLOAD, 0);
}
Exemplo n.º 24
0
/* Pause the codec and make it wait for further instructions inside the
   command callback */
bool codec_pause(void)
{
    LOGFQUEUE("audio >| codec Q_CODEC_PAUSE");
    return codec_queue_send(Q_CODEC_PAUSE, 0) != Q_NULL;
}
Exemplo n.º 25
0
/* Instruct the codec to seek to the specified time (should be properly
   paused or stopped first to avoid possible buffering deadlock) */
void codec_seek(long time)
{
    LOGFQUEUE("audio > codec Q_CODEC_SEEK: %ld", time);
    codec_queue_send(Q_CODEC_SEEK, time);
}
Exemplo n.º 26
0
/* Begin decoding the current file */
void codec_go(void)
{
    LOGFQUEUE("audio >| codec Q_CODEC_RUN");
    codec_queue_send(Q_CODEC_RUN, 0);
}
Exemplo n.º 27
0
void buffering_thread(void)
{
    bool filling = false;
    struct queue_event ev;

    while (true)
    {
        if (!filling) {
            cancel_cpu_boost();
        }

        queue_wait_w_tmo(&buffering_queue, &ev, filling ? 5 : HZ/2);

        switch (ev.id)
        {
            case Q_START_FILL:
                LOGFQUEUE("buffering < Q_START_FILL %d", (int)ev.data);
                /* Call buffer callbacks here because this is one of two ways
                 * to begin a full buffer fill */
                send_event(BUFFER_EVENT_BUFFER_LOW, 0);
                shrink_buffer();
                queue_reply(&buffering_queue, 1);
                filling |= buffer_handle((int)ev.data);
                break;

            case Q_BUFFER_HANDLE:
                LOGFQUEUE("buffering < Q_BUFFER_HANDLE %d", (int)ev.data);
                queue_reply(&buffering_queue, 1);
                buffer_handle((int)ev.data);
                break;

            case Q_RESET_HANDLE:
                LOGFQUEUE("buffering < Q_RESET_HANDLE %d", (int)ev.data);
                queue_reply(&buffering_queue, 1);
                reset_handle((int)ev.data);
                break;

            case Q_CLOSE_HANDLE:
                LOGFQUEUE("buffering < Q_CLOSE_HANDLE %d", (int)ev.data);
                queue_reply(&buffering_queue, close_handle((int)ev.data));
                break;

            case Q_HANDLE_ADDED:
                LOGFQUEUE("buffering < Q_HANDLE_ADDED %d", (int)ev.data);
                /* A handle was added: the disk is spinning, so we can fill */
                filling = true;
                break;

            case Q_BASE_HANDLE:
                LOGFQUEUE("buffering < Q_BASE_HANDLE %d", (int)ev.data);
                base_handle_id = (int)ev.data;
                break;

#ifndef SIMULATOR
            case SYS_USB_CONNECTED:
                LOGFQUEUE("buffering < SYS_USB_CONNECTED");
                usb_acknowledge(SYS_USB_CONNECTED_ACK);
                usb_wait_for_disconnect(&buffering_queue);
                break;
#endif

            case SYS_TIMEOUT:
                LOGFQUEUE_SYS_TIMEOUT("buffering < SYS_TIMEOUT");
                break;
        }

        update_data_counters();

        /* If the buffer is low, call the callbacks to get new data */
        if (num_handles > 0 && data_counters.useful <= conf_watermark)
            send_event(BUFFER_EVENT_BUFFER_LOW, 0);

#if 0
        /* TODO: This needs to be fixed to use the idle callback, disable it
         * for simplicity until its done right */
#if MEM > 8
        /* If the disk is spinning, take advantage by filling the buffer */
        else if (storage_disk_is_active() && queue_empty(&buffering_queue))
        {
            if (num_handles > 0 && data_counters.useful <= high_watermark)
                send_event(BUFFER_EVENT_BUFFER_LOW, 0);

            if (data_counters.remaining > 0 && BUF_USED <= high_watermark)
            {
                /* This is a new fill, shrink the buffer up first */
                if (!filling)
                    shrink_buffer();
                filling = fill_buffer();
                update_data_counters();
            }
        }
#endif
#endif

        if (queue_empty(&buffering_queue)) {
            if (filling) {
                if (data_counters.remaining > 0 && BUF_USED < buffer_len)
                    filling = fill_buffer();
                else if (data_counters.remaining == 0)
                    filling = false;
            }
            else if (ev.id == SYS_TIMEOUT)
            {
                if (data_counters.remaining > 0 &&
                    data_counters.useful <= conf_watermark) {
                    shrink_buffer();
                    filling = fill_buffer();
                }
            }
        }
    }
}
Exemplo n.º 28
0
/* Reserve space in the buffer for a file.
   filename: name of the file to open
   offset: offset at which to start buffering the file, useful when the first
           (offset-1) bytes of the file aren't needed.
   type: one of the data types supported (audio, image, cuesheet, others
   user_data: user data passed possibly passed in subcalls specific to a
              data_type (only used for image (albumart) buffering so far )
   return value: <0 if the file cannot be opened, or one file already
   queued to be opened, otherwise the handle for the file in the buffer
*/
int bufopen(const char *file, size_t offset, enum data_type type,
            void *user_data)
{
#ifndef HAVE_ALBUMART
    /* currently only used for aa loading */
    (void)user_data;
#endif
    if (type == TYPE_ID3)
    {
        /* ID3 case: allocate space, init the handle and return. */

        struct memory_handle *h = add_handle(sizeof(struct mp3entry), false, true);
        if (!h)
            return ERR_BUFFER_FULL;

        h->fd = -1;
        h->filesize = sizeof(struct mp3entry);
        h->filerem = sizeof(struct mp3entry);
        h->offset = 0;
        h->data = buf_widx;
        h->ridx = buf_widx;
        h->widx = buf_widx;
        h->available = 0;
        h->type = type;
        strlcpy(h->path, file, MAX_PATH);

        buf_widx += sizeof(struct mp3entry);  /* safe because the handle
                                                 can't wrap */

        /* Inform the buffering thread that we added a handle */
        LOGFQUEUE("buffering > Q_HANDLE_ADDED %d", h->id);
        queue_post(&buffering_queue, Q_HANDLE_ADDED, h->id);

        return h->id;
    }

    /* Other cases: there is a little more work. */

    int fd = open(file, O_RDONLY);
    if (fd < 0)
        return ERR_FILE_ERROR;

    size_t size = filesize(fd);
    bool can_wrap = type==TYPE_PACKET_AUDIO || type==TYPE_CODEC;

    size_t adjusted_offset = offset;
    if (adjusted_offset > size)
        adjusted_offset = 0;

    /* Reserve extra space because alignment can move data forward */
    size_t padded_size = STORAGE_PAD(size-adjusted_offset);
    struct memory_handle *h = add_handle(padded_size, can_wrap, false);
    if (!h)
    {
        DEBUGF("%s(): failed to add handle\n", __func__);
        close(fd);
        return ERR_BUFFER_FULL;
    }

    strlcpy(h->path, file, MAX_PATH);
    h->offset = adjusted_offset;

    /* Don't bother to storage align bitmaps because they are not
     * loaded directly into the buffer.
     */
    if (type != TYPE_BITMAP)
    {
        size_t alignment_pad;
        
        /* Remember where data area starts, for use by reset_handle */
        h->start = buf_widx;

        /* Align to desired storage alignment */
        alignment_pad = STORAGE_OVERLAP(adjusted_offset - (size_t)(&buffer[buf_widx]));
        buf_widx = ringbuf_add(buf_widx, alignment_pad);
    }

    h->ridx = buf_widx;
    h->widx = buf_widx;
    h->data = buf_widx;
    h->available = 0;
    h->filerem = 0;
    h->type = type;

#ifdef HAVE_ALBUMART
    if (type == TYPE_BITMAP)
    {
        /* Bitmap file: we load the data instead of the file */
        int rc;
        mutex_lock(&llist_mod_mutex); /* Lock because load_bitmap yields */
        rc = load_image(fd, file, (struct dim*)user_data);
        mutex_unlock(&llist_mod_mutex);
        if (rc <= 0)
        {
            rm_handle(h);
            close(fd);
            return ERR_FILE_ERROR;
        }
        h->filerem = 0;
        h->filesize = rc;
        h->available = rc;
        h->widx = buf_widx + rc; /* safe because the data doesn't wrap */
        buf_widx += rc;  /* safe too */
    }
    else
#endif
    {
        h->filerem = size - adjusted_offset;
        h->filesize = size;
        h->available = 0;
        h->widx = buf_widx;
    }

    if (type == TYPE_CUESHEET) {
        h->fd = fd;
        /* Immediately start buffering those */
        LOGFQUEUE("buffering >| Q_BUFFER_HANDLE %d", h->id);
        queue_send(&buffering_queue, Q_BUFFER_HANDLE, h->id);
    } else {
        /* Other types will get buffered in the course of normal operations */
        h->fd = -1;
        close(fd);

        /* Inform the buffering thread that we added a handle */
        LOGFQUEUE("buffering > Q_HANDLE_ADDED %d", h->id);
        queue_post(&buffering_queue, Q_HANDLE_ADDED, h->id);
    }

    logf("bufopen: new hdl %d", h->id);
    return h->id;
}