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