android_AudioSfDecoder.cpp revision ac18c1cd32408884d3960bd7aa56ba419c2ca68b
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
317f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi// keep in sync with the entries of kPcmDecodeMetadataKeys[] defined in android_AudioSfDecoder.h
327f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi#define ANDROID_KEY_INDEX_PCMFORMAT_NUMCHANNELS   0
337f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi#define ANDROID_KEY_INDEX_PCMFORMAT_SAMPLESPERSEC 1
347f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi#define ANDROID_KEY_INDEX_PCMFORMAT_BITSPERSAMPLE 2
357f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi#define ANDROID_KEY_INDEX_PCMFORMAT_CONTAINERSIZE 3
367f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi#define ANDROID_KEY_INDEX_PCMFORMAT_CHANNELMASK   4
377f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi#define ANDROID_KEY_INDEX_PCMFORMAT_ENDIANNESS    5
387f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
3913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi//--------------------------------------------------------------------------------------------------
4013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel TriviAudioSfDecoder::AudioSfDecoder(const AudioPlayback_Parameters* params) : GenericPlayer(params),
41b4fb100d7122d118d3da9d1d08ffacef68dd38b0Jean-Michel Trivi        mDataSource(0),
42b4fb100d7122d118d3da9d1d08ffacef68dd38b0Jean-Michel Trivi        mAudioSource(0),
4313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mBitrate(-1),
447f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        mChannelMask(ANDROID_UNKNOWN_CHANNELMASK),
4513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mDurationUsec(-1),
4613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mDecodeBuffer(NULL),
4713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mTimeDelta(-1),
4813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mSeekTimeMsec(0),
497f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        mLastDecodedPositionUs(-1),
507f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        mPcmFormatKeyCount(0),
517f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        mGetPcmFormatKeyCount(false)
5213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi{
5313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    SL_LOGV("AudioSfDecoder::AudioSfDecoder()");
5413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
5513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
5613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
5713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel TriviAudioSfDecoder::~AudioSfDecoder() {
5813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    SL_LOGV("AudioSfDecoder::~AudioSfDecoder()");
59b4fb100d7122d118d3da9d1d08ffacef68dd38b0Jean-Michel Trivi    if (mAudioSource != 0) {
60b4fb100d7122d118d3da9d1d08ffacef68dd38b0Jean-Michel Trivi        mAudioSource->stop();
61b4fb100d7122d118d3da9d1d08ffacef68dd38b0Jean-Michel Trivi    }
6213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
6313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
6413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
6513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi//--------------------------------------------------
6613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::play() {
6713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    SL_LOGV("AudioSfDecoder::play");
6813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
6913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    GenericPlayer::play();
7013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    (new AMessage(kWhatDecode, id()))->post();
7113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
7213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
7313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
7413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::startPrefetch_async() {
7513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    SL_LOGV("AudioSfDecoder::startPrefetch_async()");
7613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
7713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (wantPrefetch()) {
7813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGV("AudioSfDecoder::startPrefetch_async(): sending check cache msg");
7913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
8013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mStateFlags |= kFlagPreparing | kFlagBuffering;
8113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
8213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        (new AMessage(kWhatCheckCache, id()))->post();
8313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
8413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
8513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
8613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
8713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi//--------------------------------------------------
887f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Triviuint32_t AudioSfDecoder::getPcmFormatKeyCount() {
897f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    android::Mutex::Autolock autoLock(mGetPcmFormatLockSingleton);
907f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    mGetPcmFormatKeyCount = false;
917f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    (new AMessage(kWhatGetPcmFormat, id()))->post();
927f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    {
937f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        android::Mutex::Autolock autoLock(mGetPcmFormatLock);
947f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        while (!mGetPcmFormatKeyCount) {
957f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi            mGetPcmFormatCondition.wait(mGetPcmFormatLock);
967f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        }
977f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    }
987f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    mGetPcmFormatKeyCount = false;
997f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    return mPcmFormatKeyCount;
1007f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi}
1017f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
1027f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
1037f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi//--------------------------------------------------
1047f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivibool AudioSfDecoder::getPcmFormatKeySize(uint32_t index, uint32_t* pKeySize) {
1057f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    uint32_t keyCount = getPcmFormatKeyCount();
1067f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    if (index >= keyCount) {
1077f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        return false;
1087f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    } else {
1097f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        *pKeySize = strlen(kPcmDecodeMetadataKeys[index]) +1;
1107f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        return true;
1117f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    }
1127f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi}
1137f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
1147f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
1157f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi//--------------------------------------------------
1167f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivibool AudioSfDecoder::getPcmFormatKeyName(uint32_t index, uint32_t keySize, char* keyName) {
1177f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    uint32_t actualKeySize;
1187f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    if (!getPcmFormatKeySize(index, &actualKeySize)) {
1197f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        return false;
1207f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    }
1217f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    if (keySize < actualKeySize) {
1227f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        return false;
1237f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    }
1247f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    strncpy(keyName, kPcmDecodeMetadataKeys[index], actualKeySize);
1257f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    return true;
1267f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi}
1277f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
1287f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
1297f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi//--------------------------------------------------
1307f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivibool AudioSfDecoder::getPcmFormatValueSize(uint32_t index, uint32_t* pValueSize) {
1317f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    uint32_t keyCount = getPcmFormatKeyCount();
1327f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    if (index >= keyCount) {
1337f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        *pValueSize = 0;
1347f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        return false;
1357f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    } else {
1367f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        *pValueSize = sizeof(uint32_t);
1377f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        return true;
1387f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    }
1397f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi}
1407f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
1417f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
1427f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi//--------------------------------------------------
1437f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivibool AudioSfDecoder::getPcmFormatKeyValue(uint32_t index, uint32_t size, uint32_t* pValue) {
1447f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    uint32_t valueSize = 0;
1457f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    if (!getPcmFormatValueSize(index, &valueSize)) {
1467f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        return false;
1477f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    } else if (size == valueSize) {
1487f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        // this ensures we are accessing mPcmFormatValues with a valid size for that index
1497f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        return false;
1507f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    } else {
1517f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        *pValue = mPcmFormatValues[index];
1527f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        return true;
1537f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    }
1547f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi}
1557f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
1567f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
1577f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi//--------------------------------------------------
15813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi// Event handlers
15913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::onPrepare() {
160e9236d046fdb5cac0696c42e03443a2439188146Jean-Michel Trivi    SL_LOGD("AudioSfDecoder::onPrepare()");
16113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
1627f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_BITSPERSAMPLE] = SL_PCMSAMPLEFORMAT_FIXED_16;
1637f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_CONTAINERSIZE] = 16;
1647f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_ENDIANNESS] = SL_BYTEORDER_LITTLEENDIAN;
1657f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    // initialization with the default values
1667f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_NUMCHANNELS] = mChannelCount;
1677f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_SAMPLESPERSEC] = mSampleRateHz;
1687f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_CHANNELMASK] = mChannelMask;
1697f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
17013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    sp<DataSource> dataSource;
17113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
17213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    switch (mDataLocatorType) {
17313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
17413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    case kDataLocatorNone:
17513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGE("AudioSfDecoder::onPrepare: no data locator set");
17613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        notifyPrepared(MEDIA_ERROR_BASE);
17713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        return;
17813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
17913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    case kDataLocatorUri:
18093ac9bd4f7722c50dc9882ff74bade233860a940Jean-Michel Trivi        dataSource = DataSource::CreateFromURI(mDataLocator.uriRef);
18193ac9bd4f7722c50dc9882ff74bade233860a940Jean-Michel Trivi        if (dataSource == NULL) {
18293ac9bd4f7722c50dc9882ff74bade233860a940Jean-Michel Trivi            SL_LOGE("AudioSfDecoder::onPrepare(): Error opening %s", mDataLocator.uriRef);
18313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            notifyPrepared(MEDIA_ERROR_BASE);
18413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            return;
18513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
18613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        break;
18713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
18893ac9bd4f7722c50dc9882ff74bade233860a940Jean-Michel Trivi    case kDataLocatorFd:
18993ac9bd4f7722c50dc9882ff74bade233860a940Jean-Michel Trivi    {
19013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        dataSource = new FileSource(
19113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                mDataLocator.fdi.fd, mDataLocator.fdi.offset, mDataLocator.fdi.length);
19213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        status_t err = dataSource->initCheck();
19313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (err != OK) {
19413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            notifyPrepared(err);
19513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            return;
19613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
19793ac9bd4f7722c50dc9882ff74bade233860a940Jean-Michel Trivi        break;
19813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
19913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
20013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    default:
20113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        TRESPASS();
20213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
20313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
20413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    sp<MediaExtractor> extractor = MediaExtractor::Create(dataSource);
20513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (extractor == NULL) {
20613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGE("AudioSfDecoder::onPrepare: Could not instantiate extractor.");
20713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        notifyPrepared(ERROR_UNSUPPORTED);
20813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        return;
20913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
21013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
21113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    ssize_t audioTrackIndex = -1;
21213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    bool isRawAudio = false;
21313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    for (size_t i = 0; i < extractor->countTracks(); ++i) {
21413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        sp<MetaData> meta = extractor->getTrackMetaData(i);
21513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
21613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        const char *mime;
21713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        CHECK(meta->findCString(kKeyMIMEType, &mime));
21813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
21913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (!strncasecmp("audio/", mime, 6)) {
22013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            audioTrackIndex = i;
22113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
22213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_RAW, mime)) {
22313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                isRawAudio = true;
22413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            }
22513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            break;
22613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
22713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
22813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
22913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (audioTrackIndex < 0) {
23013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGE("AudioSfDecoder::onPrepare: Could not find a supported audio track.");
23113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        notifyPrepared(ERROR_UNSUPPORTED);
23213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        return;
23313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
23413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
23513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    sp<MediaSource> source = extractor->getTrack(audioTrackIndex);
23613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    sp<MetaData> meta = source->getFormat();
23713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
2387f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    bool hasChannelCount = meta->findInt32(kKeyChannelCount, &mChannelCount);
2397f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    if (hasChannelCount) {
2407f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_NUMCHANNELS] = mChannelCount;
2417f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    }
2427f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
24313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    off64_t size;
24413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    int64_t durationUs;
24513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (dataSource->getSize(&size) == OK
24613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            && meta->findInt64(kKeyDuration, &durationUs)) {
24713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mBitrate = size * 8000000ll / durationUs;  // in bits/sec
24813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mDurationUsec = durationUs;
2497f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        mDurationMsec = durationUs / 1000;
25013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    } else {
25113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mBitrate = -1;
25213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mDurationUsec = -1;
25313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
25413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
25513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (!isRawAudio) {
25613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        OMXClient client;
25713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        CHECK_EQ(client.connect(), (status_t)OK);
25813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
25913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        source = OMXCodec::Create(
26013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                client.interface(), meta, false /* createEncoder */,
26113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                source);
26213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
26313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (source == NULL) {
26413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            SL_LOGE("AudioSfDecoder::onPrepare: Could not instantiate decoder.");
26513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            notifyPrepared(ERROR_UNSUPPORTED);
26613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            return;
26713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
26813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
26913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        meta = source->getFormat();
27013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
27113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
27213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
27313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (source->start() != OK) {
27413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGE("AudioSfDecoder::onPrepare: Failed to start source/decoder.");
27513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        notifyPrepared(MEDIA_ERROR_BASE);
27613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        return;
27713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
27813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
27913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    mDataSource = dataSource;
28013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    mAudioSource = source;
28113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
2827f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    if (!hasChannelCount) {
2837f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        // even though the channel count was already queried above, there are issues with some
2847f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        // OMXCodecs (e.g. MP3 software decoder) not reporting the right count,
2857f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        // we trust the first reported value.
2867f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        CHECK(meta->findInt32(kKeyChannelCount, &mChannelCount));
2877f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_NUMCHANNELS] = mChannelCount;
2887f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    }
2897f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    int32_t sr;
2907f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    CHECK(meta->findInt32(kKeySampleRate, &sr));
2917f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    mSampleRateHz = (uint32_t) sr;
2927f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_SAMPLESPERSEC] = mSampleRateHz;
2937f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    // FIXME add code below once channel mask support is in, currently initialized to default
2947f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    //    if (meta->findInt32(kKeyChannelMask, &mChannelMask)) {
2957f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    //        mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_CHANNELMASK] = mChannelMask;
2967f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    //    }
29713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
29813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (!wantPrefetch()) {
29913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGV("AudioSfDecoder::onPrepare: no need to prefetch");
30013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        // doesn't need prefetching, notify good to go
30113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mCacheStatus = kStatusHigh;
30213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mCacheFill = 1000;
30313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        notifyStatus();
30413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        notifyCacheFill();
30513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
30613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
30713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    // at this point we have enough information about the source to create the sink that
30813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    // will consume the data
30913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    createAudioSink();
31013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
31113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    GenericPlayer::onPrepare();
312e9236d046fdb5cac0696c42e03443a2439188146Jean-Michel Trivi    SL_LOGD("AudioSfDecoder::onPrepare() done, mStateFlags=0x%x", mStateFlags);
31313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
31413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
31513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
31613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::onPause() {
317e9236d046fdb5cac0696c42e03443a2439188146Jean-Michel Trivi    SL_LOGD("AudioSfDecoder::onPause()");
31813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    GenericPlayer::onPause();
31913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    pauseAudioSink();
32013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
32113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
32213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
32313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::onPlay() {
324e9236d046fdb5cac0696c42e03443a2439188146Jean-Michel Trivi    SL_LOGD("AudioSfDecoder::onPlay()");
32513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    GenericPlayer::onPlay();
32613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    startAudioSink();
32713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
32813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
32913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
33013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::onSeek(const sp<AMessage> &msg) {
33113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    SL_LOGV("AudioSfDecoder::onSeek");
33213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    int64_t timeMsec;
33313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    CHECK(msg->findInt64(WHATPARAM_SEEK_SEEKTIME_MS, &timeMsec));
33413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
33513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    Mutex::Autolock _l(mSeekLock);
33613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    mStateFlags |= kFlagSeeking;
33713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    mSeekTimeMsec = timeMsec;
33813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    mTimeDelta = -1;
33913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    mLastDecodedPositionUs = -1;
34013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
34113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
34213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
34313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::onLoop(const sp<AMessage> &msg) {
34413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    SL_LOGV("AudioSfDecoder::onLoop");
34513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    int32_t loop;
34613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    CHECK(msg->findInt32(WHATPARAM_LOOP_LOOPING, &loop));
34713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
34813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (loop) {
34913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        //SL_LOGV("AudioSfDecoder::onLoop start looping");
35013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mStateFlags |= kFlagLooping;
35113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    } else {
35213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        //SL_LOGV("AudioSfDecoder::onLoop stop looping");
35313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mStateFlags &= ~kFlagLooping;
35413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
35513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
35613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
35713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
3587f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivivoid AudioSfDecoder::onGetPcmFormatKeyCount() {
3597f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    SL_LOGV("AudioSfDecoder::onGetPcmFormatKeyCount");
3607f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    {
3617f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        android::Mutex::Autolock autoLock(mGetPcmFormatLock);
3627f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
3637f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        if (!(mStateFlags & kFlagPrepared)) {
3647f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi            mPcmFormatKeyCount = 0;
3657f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        } else {
3667f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi            mPcmFormatKeyCount = NB_PCMMETADATA_KEYS;
3677f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        }
3687f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
3697f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        mGetPcmFormatKeyCount = true;
3707f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        mGetPcmFormatCondition.signal();
3717f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    }
3727f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
3737f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi}
3747f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
3757f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
37613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::onCheckCache(const sp<AMessage> &msg) {
37713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    //SL_LOGV("AudioSfDecoder::onCheckCache");
37813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    bool eos;
3794ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi    CacheStatus_t status = getCacheRemaining(&eos);
38013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
38113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (eos || status == kStatusHigh
38213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            || ((mStateFlags & kFlagPreparing) && (status >= kStatusEnough))) {
38313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (mStateFlags & kFlagPlaying) {
38413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            startAudioSink();
38513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
38613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mStateFlags &= ~kFlagBuffering;
38713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
38813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGV("AudioSfDecoder::onCheckCache: buffering done.");
38913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
39013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (mStateFlags & kFlagPreparing) {
39113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            //SL_LOGV("AudioSfDecoder::onCheckCache: preparation done.");
39213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            mStateFlags &= ~kFlagPreparing;
39313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
39413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
39513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mTimeDelta = -1;
39613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (mStateFlags & kFlagPlaying) {
39713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            (new AMessage(kWhatDecode, id()))->post();
39813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
39913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        return;
40013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
40113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
40213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    msg->post(100000);
40313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
40413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
40513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
40613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::onDecode() {
40713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    SL_LOGV("AudioSfDecoder::onDecode");
40813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
40913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    //-------------------------------- Need to buffer some more before decoding?
41013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    bool eos;
41113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (mDataSource == 0) {
41213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        // application set play state to paused which failed, then set play state to playing
41313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        return;
41413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
41513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (wantPrefetch()
41613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            && (getCacheRemaining(&eos) == kStatusLow)
41713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            && !eos) {
41813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGV("buffering more.");
41913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
42013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (mStateFlags & kFlagPlaying) {
42113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            pauseAudioSink();
42213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
42313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mStateFlags |= kFlagBuffering;
42413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        (new AMessage(kWhatCheckCache, id()))->post(100000);
42513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        return;
42613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
42713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
42813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (!(mStateFlags & (kFlagPlaying | kFlagBuffering | kFlagPreparing))) {
42913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        // don't decode if we're not buffering, prefetching or playing
43013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        //SL_LOGV("don't decode: not buffering, prefetching or playing");
43113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        return;
43213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
43313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
43413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    //-------------------------------- Decode
43513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    status_t err;
43613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    MediaSource::ReadOptions readOptions;
43713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (mStateFlags & kFlagSeeking) {
43813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        readOptions.setSeekTo(mSeekTimeMsec * 1000);
43913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
44013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
44113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    {
44213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        Mutex::Autolock _l(mDecodeBufferLock);
44313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (NULL != mDecodeBuffer) {
44413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            // the current decoded buffer hasn't been rendered, drop it
44513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            mDecodeBuffer->release();
44613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            mDecodeBuffer = NULL;
44713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
44813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        err = mAudioSource->read(&mDecodeBuffer, &readOptions);
44913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (err == OK) {
45013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            CHECK(mDecodeBuffer->meta_data()->findInt64(kKeyTime, &mLastDecodedPositionUs));
45113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
45213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
45313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
45413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    {
45513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        Mutex::Autolock _l(mSeekLock);
45613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (mStateFlags & kFlagSeeking) {
45713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            mStateFlags &= ~kFlagSeeking;
45813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
45913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
46013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
46113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    //-------------------------------- Handle return of decode
46213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (err != OK) {
46313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        bool continueDecoding = false;
46413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        switch(err) {
46513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            case ERROR_END_OF_STREAM:
46613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                if (0 < mDurationUsec) {
46713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                    mLastDecodedPositionUs = mDurationUsec;
46813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                }
46913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                // handle notification and looping at end of stream
47013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                if (mStateFlags & kFlagPlaying) {
47113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                    notify(PLAYEREVENT_ENDOFSTREAM, 1, true);
47213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                }
47313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                if (mStateFlags & kFlagLooping) {
47413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                    seek(0);
47513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                    // kick-off decoding again
47613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                    continueDecoding = true;
47713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                }
47813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                break;
47913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            case INFO_FORMAT_CHANGED:
480e9236d046fdb5cac0696c42e03443a2439188146Jean-Michel Trivi                SL_LOGD("MediaSource::read encountered INFO_FORMAT_CHANGED");
48113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                // reconfigure output
48213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                updateAudioSink();
48313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                continueDecoding = true;
48413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                break;
48513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            case INFO_DISCONTINUITY:
486e9236d046fdb5cac0696c42e03443a2439188146Jean-Michel Trivi                SL_LOGD("MediaSource::read encountered INFO_DISCONTINUITY");
48713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                continueDecoding = true;
48813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                break;
48913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            default:
49013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                SL_LOGE("MediaSource::read returned error %d", err);
49113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                break;
49213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
49313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (continueDecoding) {
49413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            if (NULL == mDecodeBuffer) {
49513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                (new AMessage(kWhatDecode, id()))->post();
49613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                return;
49713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            }
49813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        } else {
49913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            return;
50013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
50113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
50213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
50313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    //-------------------------------- Render
50413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    sp<AMessage> msg = new AMessage(kWhatRender, id());
50513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    msg->post();
50613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
50713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
50813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
50913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::onRender() {
51013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    //SL_LOGV("AudioSfDecoder::onRender");
51113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
51213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    Mutex::Autolock _l(mDecodeBufferLock);
51313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
51413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (NULL == mDecodeBuffer) {
51513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        // nothing to render, move along
51613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGV("AudioSfDecoder::onRender NULL buffer, exiting");
51713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        return;
51813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
51913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
52013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    mDecodeBuffer->release();
52113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    mDecodeBuffer = NULL;
52213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
52313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
52413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
52513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
52613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::onMessageReceived(const sp<AMessage> &msg) {
52713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    switch (msg->what()) {
52813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        case kWhatPrepare:
52913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            onPrepare();
53013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            break;
53113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
53213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        case kWhatDecode:
53313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            onDecode();
53413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            break;
53513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
53613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        case kWhatRender:
53713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            onRender();
53813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            break;
53913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
54013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        case kWhatCheckCache:
54113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            onCheckCache(msg);
54213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            break;
54313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
54413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        case kWhatNotif:
54513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            onNotify(msg);
54613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            break;
54713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
54813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        case kWhatPlay:
54913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            onPlay();
55013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            break;
55113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
55213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        case kWhatPause:
55313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            onPause();
55413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            break;
5557f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
5567f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        case kWhatGetPcmFormat:
5577f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi            onGetPcmFormatKeyCount();
5587f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi            break;
55913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi/*
56013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        case kWhatSeek:
56113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            onSeek(msg);
56213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            break;
56313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
56413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        case kWhatLoop:
56513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            onLoop(msg);
56613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            break;
56713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi*/
56813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        default:
56913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            GenericPlayer::onMessageReceived(msg);
57013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            break;
57113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
57213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
57313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
57413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi//--------------------------------------------------
57513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi// Prepared state, prefetch status notifications
57613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::notifyPrepared(status_t prepareRes) {
57713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    notify(PLAYEREVENT_PREPARED, (int32_t)prepareRes, true);
57813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
57913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
58013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
58113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
58213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::onNotify(const sp<AMessage> &msg) {
58313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (NULL == mNotifyClient) {
58413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        return;
58513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
58613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    int32_t val;
58713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (msg->findInt32(PLAYEREVENT_PREFETCHSTATUSCHANGE, &val)) {
58813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGV("\tASfPlayer notifying %s = %d", PLAYEREVENT_PREFETCHSTATUSCHANGE, val);
58937dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi        mNotifyClient(kEventPrefetchStatusChange, val, 0, mNotifyUser);
59013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
59113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    else if (msg->findInt32(PLAYEREVENT_PREFETCHFILLLEVELUPDATE, &val)) {
59213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGV("\tASfPlayer notifying %s = %d", PLAYEREVENT_PREFETCHFILLLEVELUPDATE, val);
59337dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi        mNotifyClient(kEventPrefetchFillLevelUpdate, val, 0, mNotifyUser);
59413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
59513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    else if (msg->findInt32(PLAYEREVENT_ENDOFSTREAM, &val)) {
59613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGV("\tASfPlayer notifying %s = %d", PLAYEREVENT_ENDOFSTREAM, val);
59737dc2fccf3f122b79ebd554de209d0a3c94ae161Jean-Michel Trivi        mNotifyClient(kEventEndOfStream, val, 0, mNotifyUser);
59813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
59913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    else {
60013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        GenericPlayer::onNotify(msg);
60113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
60213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
60313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
60413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
60513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi//--------------------------------------------------
60613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi// Private utility functions
60713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
60813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivibool AudioSfDecoder::wantPrefetch() {
609ac18c1cd32408884d3960bd7aa56ba419c2ca68bJean-Michel Trivi    if (mDataSource != 0) {
610ac18c1cd32408884d3960bd7aa56ba419c2ca68bJean-Michel Trivi        return (mDataSource->flags() & DataSource::kWantsPrefetching);
611ac18c1cd32408884d3960bd7aa56ba419c2ca68bJean-Michel Trivi    } else {
612ac18c1cd32408884d3960bd7aa56ba419c2ca68bJean-Michel Trivi        // happens if an improper data locator was passed, if the media extractor couldn't be
613ac18c1cd32408884d3960bd7aa56ba419c2ca68bJean-Michel Trivi        //  initialized, if there is no audio track in the media, if the OMX decoder couldn't be
614ac18c1cd32408884d3960bd7aa56ba419c2ca68bJean-Michel Trivi        //  instantiated, if the source couldn't be opened, or if the MediaSource
615ac18c1cd32408884d3960bd7aa56ba419c2ca68bJean-Michel Trivi        //  couldn't be started
616ac18c1cd32408884d3960bd7aa56ba419c2ca68bJean-Michel Trivi        SL_LOGV("AudioSfDecoder::wantPrefetch() tries to access NULL mDataSource");
617ac18c1cd32408884d3960bd7aa56ba419c2ca68bJean-Michel Trivi        return false;
618ac18c1cd32408884d3960bd7aa56ba419c2ca68bJean-Michel Trivi    }
61913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
62013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
62113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
62213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Triviint64_t AudioSfDecoder::getPositionUsec() {
62313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    Mutex::Autolock _l(mSeekLock);
62413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (mStateFlags & kFlagSeeking) {
62513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        return mSeekTimeMsec * 1000;
62613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    } else {
62713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (mLastDecodedPositionUs < 0) {
62813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            return 0;
62913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        } else {
63013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            return mLastDecodedPositionUs;
63113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
63213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
63313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
63413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
63513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
6364ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel TriviCacheStatus_t AudioSfDecoder::getCacheRemaining(bool *eos) {
63713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    sp<NuCachedSource2> cachedSource =
63813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        static_cast<NuCachedSource2 *>(mDataSource.get());
63913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
6404ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi    CacheStatus_t oldStatus = mCacheStatus;
64113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
64213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    status_t finalStatus;
64313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    size_t dataRemaining = cachedSource->approxDataRemaining(&finalStatus);
64413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    *eos = (finalStatus != OK);
64513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
64613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    CHECK_GE(mBitrate, 0);
64713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
64813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    int64_t dataRemainingUs = dataRemaining * 8000000ll / mBitrate;
64913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    //SL_LOGV("AudioSfDecoder::getCacheRemaining: approx %.2f secs remaining (eos=%d)",
65013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    //       dataRemainingUs / 1E6, *eos);
65113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
65213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (*eos) {
65313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        // data is buffered up to the end of the stream, it can't get any better than this
65413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mCacheStatus = kStatusHigh;
65513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mCacheFill = 1000;
65613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
65713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    } else {
65813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (mDurationUsec > 0) {
65913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            // known duration:
66013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
66113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            //   fill level is ratio of how much has been played + how much is
66213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            //   cached, divided by total duration
66313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            uint32_t currentPositionUsec = getPositionUsec();
66413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            mCacheFill = (int16_t) ((1000.0
66513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                    * (double)(currentPositionUsec + dataRemainingUs) / mDurationUsec));
66613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            //SL_LOGV("cacheFill = %d", mCacheFill);
66713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
66813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            //   cache status is evaluated against duration thresholds
6694ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi            if (dataRemainingUs > DURATION_CACHED_HIGH_MS*1000) {
67013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                mCacheStatus = kStatusHigh;
67113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                //LOGV("high");
6724ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi            } else if (dataRemainingUs > DURATION_CACHED_MED_MS*1000) {
67313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                //LOGV("enough");
67413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                mCacheStatus = kStatusEnough;
6754ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi            } else if (dataRemainingUs < DURATION_CACHED_LOW_MS*1000) {
67613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                //LOGV("low");
67713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                mCacheStatus = kStatusLow;
67813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            } else {
67913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                mCacheStatus = kStatusIntermediate;
68013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            }
68113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
68213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        } else {
68313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            // unknown duration:
68413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
68513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            //   cache status is evaluated against cache amount thresholds
68613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            //   (no duration so we don't have the bitrate either, could be derived from format?)
68713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            if (dataRemaining > SIZE_CACHED_HIGH_BYTES) {
68813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                mCacheStatus = kStatusHigh;
68913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            } else if (dataRemaining > SIZE_CACHED_MED_BYTES) {
69013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                mCacheStatus = kStatusEnough;
69113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            } else if (dataRemaining < SIZE_CACHED_LOW_BYTES) {
69213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                mCacheStatus = kStatusLow;
69313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            } else {
69413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                mCacheStatus = kStatusIntermediate;
69513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            }
69613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
69713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
69813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
69913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
70013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (oldStatus != mCacheStatus) {
70113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        notifyStatus();
70213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
70313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
70413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (abs(mCacheFill - mLastNotifiedCacheFill) > mCacheFillNotifThreshold) {
70513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        notifyCacheFill();
70613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
70713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
70813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    return mCacheStatus;
70913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
71013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
71113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi} // namespace android
712