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