android_AudioSfDecoder.cpp revision 13837cf3f7be0eb8b1a9552bd99a89f98c987720
113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi/*
213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi * Copyright (C) 2011 The Android Open Source Project
313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi *
413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi * Licensed under the Apache License, Version 2.0 (the "License");
513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi * you may not use this file except in compliance with the License.
613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi * You may obtain a copy of the License at
713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi *
813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi *      http://www.apache.org/licenses/LICENSE-2.0
913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi *
1013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi * Unless required by applicable law or agreed to in writing, software
1113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi * distributed under the License is distributed on an "AS IS" BASIS,
1213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi * See the License for the specific language governing permissions and
1413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi * limitations under the License.
1513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi */
1613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
1713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi#define USE_LOG SLAndroidLogLevel_Verbose
1813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
1913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi#include "sles_allinclusive.h"
2013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
2113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivinamespace android {
2213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
2313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi//--------------------------------------------------------------------------------------------------
2413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel TriviAudioSfDecoder::AudioSfDecoder(const AudioPlayback_Parameters* params) : GenericPlayer(params),
2513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mBitrate(-1),
2613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mNumChannels(1),
2713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mSampleRateHz(0),
2813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mDurationUsec(-1),
2913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mDecodeBuffer(NULL),
3013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mCacheStatus(kStatusEmpty),
3113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mCacheFill(0),
3213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mLastNotifiedCacheFill(0),
3313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mCacheFillNotifThreshold(100),
3413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mTimeDelta(-1),
3513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mSeekTimeMsec(0),
3613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mLastDecodedPositionUs(-1)
3713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi{
3813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    SL_LOGV("AudioSfDecoder::AudioSfDecoder()");
3913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
4013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
4113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
4213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
4313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel TriviAudioSfDecoder::~AudioSfDecoder() {
4413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    SL_LOGV("AudioSfDecoder::~AudioSfDecoder()");
4513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
4613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
4713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
4813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
4913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi//--------------------------------------------------
5013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::play() {
5113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    SL_LOGV("AudioSfDecoder::play");
5213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
5313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    GenericPlayer::play();
5413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    (new AMessage(kWhatDecode, id()))->post();
5513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
5613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
5713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
5813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::startPrefetch_async() {
5913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    SL_LOGV("AudioSfDecoder::startPrefetch_async()");
6013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
6113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (wantPrefetch()) {
6213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGV("AudioSfDecoder::startPrefetch_async(): sending check cache msg");
6313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
6413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mStateFlags |= kFlagPreparing | kFlagBuffering;
6513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
6613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        (new AMessage(kWhatCheckCache, id()))->post();
6713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
6813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
6913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
7013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
7113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi//--------------------------------------------------
7213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi// Event handlers
7313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::onPrepare() {
7413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    SL_LOGI("AudioSfDecoder::onPrepare()");
7513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
7613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    sp<DataSource> dataSource;
7713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
7813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    switch (mDataLocatorType) {
7913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
8013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    case kDataLocatorNone:
8113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGE("AudioSfDecoder::onPrepare: no data locator set");
8213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        notifyPrepared(MEDIA_ERROR_BASE);
8313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        return;
8413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
8513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    case kDataLocatorUri:
8613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (!strncasecmp(mDataLocator.uriRef, "http://", 7)) {
8713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            SL_LOGE("AudioSfDecoder::onPrepare: http source not supported yet");
8813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            notifyPrepared(MEDIA_ERROR_BASE);
8913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            return;
9013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        } else {
9113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            dataSource = DataSource::CreateFromURI(mDataLocator.uriRef);
9213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
9313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        break;
9413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
9513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    case kDataLocatorFd: {
9613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        dataSource = new FileSource(
9713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                mDataLocator.fdi.fd, mDataLocator.fdi.offset, mDataLocator.fdi.length);
9813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        status_t err = dataSource->initCheck();
9913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (err != OK) {
10013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            notifyPrepared(err);
10113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            return;
10213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
10313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
10413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    break;
10513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
10613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    default:
10713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        TRESPASS();
10813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
10913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
11013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    sp<MediaExtractor> extractor = MediaExtractor::Create(dataSource);
11113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (extractor == NULL) {
11213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGE("AudioSfDecoder::onPrepare: Could not instantiate extractor.");
11313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        notifyPrepared(ERROR_UNSUPPORTED);
11413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        return;
11513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
11613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
11713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    ssize_t audioTrackIndex = -1;
11813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    bool isRawAudio = false;
11913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    for (size_t i = 0; i < extractor->countTracks(); ++i) {
12013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        sp<MetaData> meta = extractor->getTrackMetaData(i);
12113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
12213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        const char *mime;
12313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        CHECK(meta->findCString(kKeyMIMEType, &mime));
12413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
12513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (!strncasecmp("audio/", mime, 6)) {
12613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            audioTrackIndex = i;
12713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
12813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_RAW, mime)) {
12913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                isRawAudio = true;
13013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            }
13113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            break;
13213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
13313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
13413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
13513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (audioTrackIndex < 0) {
13613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGE("AudioSfDecoder::onPrepare: Could not find a supported audio track.");
13713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        notifyPrepared(ERROR_UNSUPPORTED);
13813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        return;
13913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
14013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
14113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    sp<MediaSource> source = extractor->getTrack(audioTrackIndex);
14213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    sp<MetaData> meta = source->getFormat();
14313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
14413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    off64_t size;
14513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    int64_t durationUs;
14613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (dataSource->getSize(&size) == OK
14713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            && meta->findInt64(kKeyDuration, &durationUs)) {
14813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mBitrate = size * 8000000ll / durationUs;  // in bits/sec
14913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mDurationUsec = durationUs;
15013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    } else {
15113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mBitrate = -1;
15213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mDurationUsec = -1;
15313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
15413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
15513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (!isRawAudio) {
15613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        OMXClient client;
15713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        CHECK_EQ(client.connect(), (status_t)OK);
15813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
15913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        source = OMXCodec::Create(
16013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                client.interface(), meta, false /* createEncoder */,
16113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                source);
16213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
16313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (source == NULL) {
16413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            SL_LOGE("AudioSfDecoder::onPrepare: Could not instantiate decoder.");
16513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            notifyPrepared(ERROR_UNSUPPORTED);
16613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            return;
16713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
16813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
16913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        meta = source->getFormat();
17013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
17113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
17213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
17313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (source->start() != OK) {
17413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGE("AudioSfDecoder::onPrepare: Failed to start source/decoder.");
17513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        notifyPrepared(MEDIA_ERROR_BASE);
17613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        return;
17713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
17813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
17913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    mDataSource = dataSource;
18013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    mAudioSource = source;
18113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
18213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    CHECK(meta->findInt32(kKeyChannelCount, &mNumChannels));
18313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    CHECK(meta->findInt32(kKeySampleRate, &mSampleRateHz));
18413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
18513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (!wantPrefetch()) {
18613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGV("AudioSfDecoder::onPrepare: no need to prefetch");
18713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        // doesn't need prefetching, notify good to go
18813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mCacheStatus = kStatusHigh;
18913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mCacheFill = 1000;
19013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        notifyStatus();
19113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        notifyCacheFill();
19213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
19313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
19413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    // at this point we have enough information about the source to create the sink that
19513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    // will consume the data
19613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    createAudioSink();
19713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
19813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    GenericPlayer::onPrepare();
19913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    SL_LOGI("AudioSfDecoder::onPrepare() done, mStateFlags=0x%x", mStateFlags);
20013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
20113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
20213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
20313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::onPause() {
20413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    SL_LOGI("AudioSfDecoder::onPause()");
20513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    GenericPlayer::onPause();
20613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    pauseAudioSink();
20713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
20813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
20913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
21013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::onPlay() {
21113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    SL_LOGI("AudioSfDecoder::onPlay()");
21213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    GenericPlayer::onPlay();
21313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    startAudioSink();
21413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
21513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
21613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
21713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::onSeek(const sp<AMessage> &msg) {
21813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    SL_LOGV("AudioSfDecoder::onSeek");
21913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    int64_t timeMsec;
22013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    CHECK(msg->findInt64(WHATPARAM_SEEK_SEEKTIME_MS, &timeMsec));
22113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
22213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    Mutex::Autolock _l(mSeekLock);
22313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    mStateFlags |= kFlagSeeking;
22413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    mSeekTimeMsec = timeMsec;
22513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    mTimeDelta = -1;
22613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    mLastDecodedPositionUs = -1;
22713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
22813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
22913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
23013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::onLoop(const sp<AMessage> &msg) {
23113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    SL_LOGV("AudioSfDecoder::onLoop");
23213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    int32_t loop;
23313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    CHECK(msg->findInt32(WHATPARAM_LOOP_LOOPING, &loop));
23413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
23513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (loop) {
23613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        //SL_LOGV("AudioSfDecoder::onLoop start looping");
23713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mStateFlags |= kFlagLooping;
23813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    } else {
23913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        //SL_LOGV("AudioSfDecoder::onLoop stop looping");
24013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mStateFlags &= ~kFlagLooping;
24113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
24213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
24313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
24413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
24513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::onCheckCache(const sp<AMessage> &msg) {
24613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    //SL_LOGV("AudioSfDecoder::onCheckCache");
24713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    bool eos;
24813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    CacheStatus status = getCacheRemaining(&eos);
24913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
25013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (eos || status == kStatusHigh
25113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            || ((mStateFlags & kFlagPreparing) && (status >= kStatusEnough))) {
25213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (mStateFlags & kFlagPlaying) {
25313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            startAudioSink();
25413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
25513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mStateFlags &= ~kFlagBuffering;
25613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
25713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGV("AudioSfDecoder::onCheckCache: buffering done.");
25813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
25913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (mStateFlags & kFlagPreparing) {
26013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            //SL_LOGV("AudioSfDecoder::onCheckCache: preparation done.");
26113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            mStateFlags &= ~kFlagPreparing;
26213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
26313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
26413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mTimeDelta = -1;
26513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (mStateFlags & kFlagPlaying) {
26613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            (new AMessage(kWhatDecode, id()))->post();
26713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
26813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        return;
26913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
27013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
27113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    msg->post(100000);
27213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
27313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
27413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
27513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::onDecode() {
27613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    SL_LOGV("AudioSfDecoder::onDecode");
27713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
27813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    //-------------------------------- Need to buffer some more before decoding?
27913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    bool eos;
28013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (mDataSource == 0) {
28113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        // application set play state to paused which failed, then set play state to playing
28213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        return;
28313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
28413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (wantPrefetch()
28513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            && (getCacheRemaining(&eos) == kStatusLow)
28613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            && !eos) {
28713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGV("buffering more.");
28813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
28913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (mStateFlags & kFlagPlaying) {
29013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            pauseAudioSink();
29113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
29213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mStateFlags |= kFlagBuffering;
29313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        (new AMessage(kWhatCheckCache, id()))->post(100000);
29413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        return;
29513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
29613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
29713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (!(mStateFlags & (kFlagPlaying | kFlagBuffering | kFlagPreparing))) {
29813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        // don't decode if we're not buffering, prefetching or playing
29913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        //SL_LOGV("don't decode: not buffering, prefetching or playing");
30013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        return;
30113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
30213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
30313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    //-------------------------------- Decode
30413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    status_t err;
30513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    MediaSource::ReadOptions readOptions;
30613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (mStateFlags & kFlagSeeking) {
30713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        readOptions.setSeekTo(mSeekTimeMsec * 1000);
30813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
30913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
31013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    {
31113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        Mutex::Autolock _l(mDecodeBufferLock);
31213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (NULL != mDecodeBuffer) {
31313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            // the current decoded buffer hasn't been rendered, drop it
31413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            mDecodeBuffer->release();
31513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            mDecodeBuffer = NULL;
31613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
31713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        err = mAudioSource->read(&mDecodeBuffer, &readOptions);
31813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (err == OK) {
31913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            CHECK(mDecodeBuffer->meta_data()->findInt64(kKeyTime, &mLastDecodedPositionUs));
32013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
32113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
32213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
32313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    {
32413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        Mutex::Autolock _l(mSeekLock);
32513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (mStateFlags & kFlagSeeking) {
32613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            mStateFlags &= ~kFlagSeeking;
32713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
32813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
32913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
33013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    //-------------------------------- Handle return of decode
33113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (err != OK) {
33213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        bool continueDecoding = false;
33313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        switch(err) {
33413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            case ERROR_END_OF_STREAM:
33513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                if (0 < mDurationUsec) {
33613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                    mLastDecodedPositionUs = mDurationUsec;
33713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                }
33813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                // handle notification and looping at end of stream
33913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                if (mStateFlags & kFlagPlaying) {
34013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                    notify(PLAYEREVENT_ENDOFSTREAM, 1, true);
34113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                }
34213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                if (mStateFlags & kFlagLooping) {
34313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                    seek(0);
34413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                    // kick-off decoding again
34513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                    continueDecoding = true;
34613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                }
34713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                break;
34813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            case INFO_FORMAT_CHANGED:
34913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                SL_LOGI("MediaSource::read encountered INFO_FORMAT_CHANGED");
35013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                // reconfigure output
35113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                updateAudioSink();
35213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                continueDecoding = true;
35313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                break;
35413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            case INFO_DISCONTINUITY:
35513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                SL_LOGI("MediaSource::read encountered INFO_DISCONTINUITY");
35613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                continueDecoding = true;
35713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                break;
35813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            default:
35913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                SL_LOGE("MediaSource::read returned error %d", err);
36013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                break;
36113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
36213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (continueDecoding) {
36313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            if (NULL == mDecodeBuffer) {
36413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                (new AMessage(kWhatDecode, id()))->post();
36513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                return;
36613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            }
36713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        } else {
36813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            return;
36913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
37013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
37113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
37213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    //-------------------------------- Render
37313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    sp<AMessage> msg = new AMessage(kWhatRender, id());
37413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    msg->post();
37513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
37613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
37713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
37813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::onRender() {
37913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    //SL_LOGV("AudioSfDecoder::onRender");
38013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
38113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    Mutex::Autolock _l(mDecodeBufferLock);
38213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
38313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (NULL == mDecodeBuffer) {
38413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        // nothing to render, move along
38513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGV("AudioSfDecoder::onRender NULL buffer, exiting");
38613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        return;
38713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
38813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
38913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    mDecodeBuffer->release();
39013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    mDecodeBuffer = NULL;
39113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
39213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
39313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
39413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
39513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::onMessageReceived(const sp<AMessage> &msg) {
39613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    switch (msg->what()) {
39713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        case kWhatPrepare:
39813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            onPrepare();
39913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            break;
40013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
40113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        case kWhatDecode:
40213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            onDecode();
40313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            break;
40413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
40513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        case kWhatRender:
40613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            onRender();
40713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            break;
40813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
40913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        case kWhatCheckCache:
41013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            onCheckCache(msg);
41113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            break;
41213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
41313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        case kWhatNotif:
41413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            onNotify(msg);
41513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            break;
41613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
41713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        case kWhatPlay:
41813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            onPlay();
41913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            break;
42013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
42113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        case kWhatPause:
42213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            onPause();
42313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            break;
42413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi/*
42513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        case kWhatSeek:
42613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            onSeek(msg);
42713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            break;
42813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
42913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        case kWhatLoop:
43013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            onLoop(msg);
43113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            break;
43213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi*/
43313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        default:
43413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            GenericPlayer::onMessageReceived(msg);
43513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            break;
43613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
43713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
43813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
43913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi//--------------------------------------------------
44013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi// Prepared state, prefetch status notifications
44113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::notifyPrepared(status_t prepareRes) {
44213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    notify(PLAYEREVENT_PREPARED, (int32_t)prepareRes, true);
44313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
44413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
44513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
44613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::notifyStatus() {
44713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    notify(PLAYEREVENT_PREFETCHSTATUSCHANGE, (int32_t)mCacheStatus, true);
44813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
44913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
45013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::notifyCacheFill() {
45113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    mLastNotifiedCacheFill = mCacheFill;
45213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    notify(PLAYEREVENT_PREFETCHFILLLEVELUPDATE, (int32_t)mLastNotifiedCacheFill, true);
45313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
45413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
45513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::onNotify(const sp<AMessage> &msg) {
45613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (NULL == mNotifyClient) {
45713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        return;
45813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
45913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    int32_t val;
46013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (msg->findInt32(PLAYEREVENT_PREFETCHSTATUSCHANGE, &val)) {
46113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGV("\tASfPlayer notifying %s = %d", PLAYEREVENT_PREFETCHSTATUSCHANGE, val);
46213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mNotifyClient(kEventPrefetchStatusChange, val, mNotifyUser);
46313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
46413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    else if (msg->findInt32(PLAYEREVENT_PREFETCHFILLLEVELUPDATE, &val)) {
46513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGV("\tASfPlayer notifying %s = %d", PLAYEREVENT_PREFETCHFILLLEVELUPDATE, val);
46613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mNotifyClient(kEventPrefetchFillLevelUpdate, val, mNotifyUser);
46713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
46813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    else if (msg->findInt32(PLAYEREVENT_ENDOFSTREAM, &val)) {
46913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGV("\tASfPlayer notifying %s = %d", PLAYEREVENT_ENDOFSTREAM, val);
47013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mNotifyClient(kEventEndOfStream, val, mNotifyUser);
47113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
47213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    else {
47313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        GenericPlayer::onNotify(msg);
47413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
47513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
47613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
47713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
47813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi//--------------------------------------------------
47913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi// Private utility functions
48013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
48113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivibool AudioSfDecoder::wantPrefetch() {
48213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    return (mDataSource->flags() & DataSource::kWantsPrefetching);
48313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
48413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
48513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
48613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Triviint64_t AudioSfDecoder::getPositionUsec() {
48713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    Mutex::Autolock _l(mSeekLock);
48813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (mStateFlags & kFlagSeeking) {
48913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        return mSeekTimeMsec * 1000;
49013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    } else {
49113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (mLastDecodedPositionUs < 0) {
49213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            return 0;
49313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        } else {
49413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            return mLastDecodedPositionUs;
49513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
49613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
49713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
49813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
49913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
50013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel TriviAudioSfDecoder::CacheStatus AudioSfDecoder::getCacheRemaining(bool *eos) {
50113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    sp<NuCachedSource2> cachedSource =
50213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        static_cast<NuCachedSource2 *>(mDataSource.get());
50313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
50413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    CacheStatus oldStatus = mCacheStatus;
50513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
50613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    status_t finalStatus;
50713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    size_t dataRemaining = cachedSource->approxDataRemaining(&finalStatus);
50813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    *eos = (finalStatus != OK);
50913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
51013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    CHECK_GE(mBitrate, 0);
51113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
51213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    int64_t dataRemainingUs = dataRemaining * 8000000ll / mBitrate;
51313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    //SL_LOGV("AudioSfDecoder::getCacheRemaining: approx %.2f secs remaining (eos=%d)",
51413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    //       dataRemainingUs / 1E6, *eos);
51513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
51613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (*eos) {
51713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        // data is buffered up to the end of the stream, it can't get any better than this
51813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mCacheStatus = kStatusHigh;
51913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mCacheFill = 1000;
52013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
52113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    } else {
52213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (mDurationUsec > 0) {
52313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            // known duration:
52413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
52513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            //   fill level is ratio of how much has been played + how much is
52613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            //   cached, divided by total duration
52713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            uint32_t currentPositionUsec = getPositionUsec();
52813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            mCacheFill = (int16_t) ((1000.0
52913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                    * (double)(currentPositionUsec + dataRemainingUs) / mDurationUsec));
53013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            //SL_LOGV("cacheFill = %d", mCacheFill);
53113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
53213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            //   cache status is evaluated against duration thresholds
53313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            if (dataRemainingUs > DURATION_CACHED_HIGH_US) {
53413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                mCacheStatus = kStatusHigh;
53513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                //LOGV("high");
53613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            } else if (dataRemainingUs > DURATION_CACHED_MED_US) {
53713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                //LOGV("enough");
53813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                mCacheStatus = kStatusEnough;
53913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            } else if (dataRemainingUs < DURATION_CACHED_LOW_US) {
54013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                //LOGV("low");
54113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                mCacheStatus = kStatusLow;
54213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            } else {
54313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                mCacheStatus = kStatusIntermediate;
54413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            }
54513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
54613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        } else {
54713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            // unknown duration:
54813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
54913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            //   cache status is evaluated against cache amount thresholds
55013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            //   (no duration so we don't have the bitrate either, could be derived from format?)
55113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            if (dataRemaining > SIZE_CACHED_HIGH_BYTES) {
55213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                mCacheStatus = kStatusHigh;
55313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            } else if (dataRemaining > SIZE_CACHED_MED_BYTES) {
55413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                mCacheStatus = kStatusEnough;
55513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            } else if (dataRemaining < SIZE_CACHED_LOW_BYTES) {
55613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                mCacheStatus = kStatusLow;
55713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            } else {
55813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                mCacheStatus = kStatusIntermediate;
55913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            }
56013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
56113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
56213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
56313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
56413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (oldStatus != mCacheStatus) {
56513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        notifyStatus();
56613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
56713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
56813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (abs(mCacheFill - mLastNotifiedCacheFill) > mCacheFillNotifThreshold) {
56913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        notifyCacheFill();
57013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
57113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
57213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    return mCacheStatus;
57313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
57413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
57513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi} // namespace android
576