android_AudioSfDecoder.cpp revision b4393ef4ef3edb785746c37fd7b68950e85283ae
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
17bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-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),
33b4fb100d7122d118d3da9d1d08ffacef68dd38b0Jean-Michel Trivi        mDataSource(0),
34b4fb100d7122d118d3da9d1d08ffacef68dd38b0Jean-Michel Trivi        mAudioSource(0),
35e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        mAudioSourceStarted(false),
3613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mBitrate(-1),
375050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi        mDurationUsec(ANDROID_UNKNOWN_TIME),
3813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mDecodeBuffer(NULL),
3913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mSeekTimeMsec(0),
40ddaf8fec2c6362785f8f27e59e30bf6bfe858f3bGlenn Kasten        // play event logic depends on the initial time being zero not ANDROID_UNKNOWN_TIME
41ddaf8fec2c6362785f8f27e59e30bf6bfe858f3bGlenn Kasten        mLastDecodedPositionUs(0)
4213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi{
4354cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    SL_LOGD("AudioSfDecoder::AudioSfDecoder()");
4413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
4513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
4613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
4713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel TriviAudioSfDecoder::~AudioSfDecoder() {
4854cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    SL_LOGD("AudioSfDecoder::~AudioSfDecoder()");
49e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi}
50e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi
51e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi
52e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivivoid AudioSfDecoder::preDestroy() {
53e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    GenericPlayer::preDestroy();
54e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    SL_LOGD("AudioSfDecoder::preDestroy()");
55e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    {
56e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        Mutex::Autolock _l(mBufferSourceLock);
57e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi
58e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        if (NULL != mDecodeBuffer) {
59e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi            mDecodeBuffer->release();
60e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi            mDecodeBuffer = NULL;
61e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        }
62e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi
63e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        if ((mAudioSource != 0) && mAudioSourceStarted) {
64e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi            mAudioSource->stop();
65e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi            mAudioSourceStarted = false;
66e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        }
67b4fb100d7122d118d3da9d1d08ffacef68dd38b0Jean-Michel Trivi    }
6813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
6913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
7013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
7113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi//--------------------------------------------------
7213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::play() {
7354cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    SL_LOGD("AudioSfDecoder::play");
7413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
7513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    GenericPlayer::play();
7613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    (new AMessage(kWhatDecode, id()))->post();
7713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
7813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
7913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
805050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivivoid AudioSfDecoder::getPositionMsec(int* msec) {
815050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi    int64_t timeUsec = getPositionUsec();
825050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi    if (timeUsec == ANDROID_UNKNOWN_TIME) {
835050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi        *msec = ANDROID_UNKNOWN_TIME;
845050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi    } else {
855933f3d5e532aaac31ce0e6551c59f0197c0ae3cGlenn Kasten        *msec = timeUsec / 1000;
865050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi    }
875050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi}
885050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi
895050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi
9013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi//--------------------------------------------------
9191540f92d7f1bcda423859af6bd82df083c2afabGlenn Kastenuint32_t AudioSfDecoder::getPcmFormatKeyCount() const {
9291540f92d7f1bcda423859af6bd82df083c2afabGlenn Kasten    return NB_PCMMETADATA_KEYS;
937f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi}
947f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
957f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
967f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi//--------------------------------------------------
977f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivibool AudioSfDecoder::getPcmFormatKeySize(uint32_t index, uint32_t* pKeySize) {
9891540f92d7f1bcda423859af6bd82df083c2afabGlenn Kasten    if (index >= NB_PCMMETADATA_KEYS) {
997f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        return false;
1007f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    } else {
1017f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        *pKeySize = strlen(kPcmDecodeMetadataKeys[index]) +1;
1027f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        return true;
1037f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    }
1047f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi}
1057f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
1067f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
1077f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi//--------------------------------------------------
1087f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivibool AudioSfDecoder::getPcmFormatKeyName(uint32_t index, uint32_t keySize, char* keyName) {
1097f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    uint32_t actualKeySize;
1107f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    if (!getPcmFormatKeySize(index, &actualKeySize)) {
1117f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        return false;
1127f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    }
1137f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    if (keySize < actualKeySize) {
1147f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        return false;
1157f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    }
1167f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    strncpy(keyName, kPcmDecodeMetadataKeys[index], actualKeySize);
1177f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    return true;
1187f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi}
1197f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
1207f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
1217f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi//--------------------------------------------------
1227f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivibool AudioSfDecoder::getPcmFormatValueSize(uint32_t index, uint32_t* pValueSize) {
12391540f92d7f1bcda423859af6bd82df083c2afabGlenn Kasten    if (index >= NB_PCMMETADATA_KEYS) {
1247f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        *pValueSize = 0;
1257f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        return false;
1267f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    } else {
1277f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        *pValueSize = sizeof(uint32_t);
1287f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        return true;
1297f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    }
1307f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi}
1317f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
1327f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
1337f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi//--------------------------------------------------
1347f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivibool AudioSfDecoder::getPcmFormatKeyValue(uint32_t index, uint32_t size, uint32_t* pValue) {
1357f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    uint32_t valueSize = 0;
1367f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    if (!getPcmFormatValueSize(index, &valueSize)) {
1377f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        return false;
13813d02b645fc6e8ffe70a8bf8cc5f69f03558ae40Jean-Michel Trivi    } else if (size != valueSize) {
1397f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        // this ensures we are accessing mPcmFormatValues with a valid size for that index
14013d02b645fc6e8ffe70a8bf8cc5f69f03558ae40Jean-Michel Trivi        SL_LOGE("Error retrieving metadata value at index %d: using size of %d, should be %d",
14113d02b645fc6e8ffe70a8bf8cc5f69f03558ae40Jean-Michel Trivi                index, size, valueSize);
1427f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        return false;
1437f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    } else {
14491540f92d7f1bcda423859af6bd82df083c2afabGlenn Kasten        android::Mutex::Autolock autoLock(mPcmFormatLock);
1457f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        *pValue = mPcmFormatValues[index];
1467f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        return true;
1477f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    }
1487f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi}
1497f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
1507f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
1517f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi//--------------------------------------------------
15213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi// Event handlers
153e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi//  it is strictly verboten to call those methods outside of the event loop
154e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi
155e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi// Initializes the data and audio sources, and update the PCM format info
156e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi// post-condition: upon successful initialization based on the player data locator
157e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi//    GenericPlayer::onPrepare() was called
158e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi//    mDataSource != 0
159e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi//    mAudioSource != 0
160e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi//    mAudioSourceStarted == true
161e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten// All error returns from this method are via notifyPrepared(status) followed by "return".
16213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::onPrepare() {
163e9236d046fdb5cac0696c42e03443a2439188146Jean-Michel Trivi    SL_LOGD("AudioSfDecoder::onPrepare()");
164e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    Mutex::Autolock _l(mBufferSourceLock);
16513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
16691540f92d7f1bcda423859af6bd82df083c2afabGlenn Kasten    {
16791540f92d7f1bcda423859af6bd82df083c2afabGlenn Kasten    android::Mutex::Autolock autoLock(mPcmFormatLock);
168e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    // Initialize the PCM format info with the known parameters before the start of the decode
1697f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_BITSPERSAMPLE] = SL_PCMSAMPLEFORMAT_FIXED_16;
1707f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_CONTAINERSIZE] = 16;
1717f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_ENDIANNESS] = SL_BYTEORDER_LITTLEENDIAN;
172e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    //    initialization with the default values: they will be replaced by the actual values
173e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    //      once the decoder has figured them out
174b4393ef4ef3edb785746c37fd7b68950e85283aeGlenn Kasten    mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_NUMCHANNELS] = UNKNOWN_NUMCHANNELS;
175b4393ef4ef3edb785746c37fd7b68950e85283aeGlenn Kasten    mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_SAMPLERATE] = UNKNOWN_SAMPLERATE;
176b4393ef4ef3edb785746c37fd7b68950e85283aeGlenn Kasten    mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_CHANNELMASK] = UNKNOWN_CHANNELMASK;
17791540f92d7f1bcda423859af6bd82df083c2afabGlenn Kasten    }
1787f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
179e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    //---------------------------------
180e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    // Instantiate and initialize the data source for the decoder
18113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    sp<DataSource> dataSource;
18213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
18313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    switch (mDataLocatorType) {
18413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
18513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    case kDataLocatorNone:
18613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGE("AudioSfDecoder::onPrepare: no data locator set");
18713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        notifyPrepared(MEDIA_ERROR_BASE);
18813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        return;
18913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
19013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    case kDataLocatorUri:
19193ac9bd4f7722c50dc9882ff74bade233860a940Jean-Michel Trivi        dataSource = DataSource::CreateFromURI(mDataLocator.uriRef);
19293ac9bd4f7722c50dc9882ff74bade233860a940Jean-Michel Trivi        if (dataSource == NULL) {
19393ac9bd4f7722c50dc9882ff74bade233860a940Jean-Michel Trivi            SL_LOGE("AudioSfDecoder::onPrepare(): Error opening %s", mDataLocator.uriRef);
19413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            notifyPrepared(MEDIA_ERROR_BASE);
19513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            return;
19613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
19713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        break;
19813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
19993ac9bd4f7722c50dc9882ff74bade233860a940Jean-Michel Trivi    case kDataLocatorFd:
20093ac9bd4f7722c50dc9882ff74bade233860a940Jean-Michel Trivi    {
201833251ab9e5e59a6ea5ac325122cf3abdf7cd944Glenn Kasten        // As FileSource unconditionally takes ownership of the fd and closes it, then
202833251ab9e5e59a6ea5ac325122cf3abdf7cd944Glenn Kasten        // we have to make a dup for FileSource if the app wants to keep ownership itself
203833251ab9e5e59a6ea5ac325122cf3abdf7cd944Glenn Kasten        int fd = mDataLocator.fdi.fd;
204833251ab9e5e59a6ea5ac325122cf3abdf7cd944Glenn Kasten        if (mDataLocator.fdi.mCloseAfterUse) {
205833251ab9e5e59a6ea5ac325122cf3abdf7cd944Glenn Kasten            mDataLocator.fdi.mCloseAfterUse = false;
206833251ab9e5e59a6ea5ac325122cf3abdf7cd944Glenn Kasten        } else {
207833251ab9e5e59a6ea5ac325122cf3abdf7cd944Glenn Kasten            fd = ::dup(fd);
208833251ab9e5e59a6ea5ac325122cf3abdf7cd944Glenn Kasten        }
209833251ab9e5e59a6ea5ac325122cf3abdf7cd944Glenn Kasten        dataSource = new FileSource(fd, mDataLocator.fdi.offset, mDataLocator.fdi.length);
21013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        status_t err = dataSource->initCheck();
21113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (err != OK) {
21213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            notifyPrepared(err);
21313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            return;
21413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
21593ac9bd4f7722c50dc9882ff74bade233860a940Jean-Michel Trivi        break;
21613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
21713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
21813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    default:
21913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        TRESPASS();
22013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
22113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
222e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    //---------------------------------
223e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    // Instanciate and initialize the decoder attached to the data source
22413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    sp<MediaExtractor> extractor = MediaExtractor::Create(dataSource);
22513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (extractor == NULL) {
22613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGE("AudioSfDecoder::onPrepare: Could not instantiate extractor.");
22713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        notifyPrepared(ERROR_UNSUPPORTED);
22813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        return;
22913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
23013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
23113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    ssize_t audioTrackIndex = -1;
23213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    bool isRawAudio = false;
23313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    for (size_t i = 0; i < extractor->countTracks(); ++i) {
23413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        sp<MetaData> meta = extractor->getTrackMetaData(i);
23513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
23613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        const char *mime;
23713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        CHECK(meta->findCString(kKeyMIMEType, &mime));
23813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
23913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (!strncasecmp("audio/", mime, 6)) {
24013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            audioTrackIndex = i;
24113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
24213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_RAW, mime)) {
24313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                isRawAudio = true;
24413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            }
24513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            break;
24613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
24713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
24813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
24913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (audioTrackIndex < 0) {
25013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGE("AudioSfDecoder::onPrepare: Could not find a supported audio track.");
25113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        notifyPrepared(ERROR_UNSUPPORTED);
25213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        return;
25313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
25413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
25513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    sp<MediaSource> source = extractor->getTrack(audioTrackIndex);
25613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    sp<MetaData> meta = source->getFormat();
25713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
25854cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    // we can't trust the OMXCodec (if there is one) to issue a INFO_FORMAT_CHANGED so we want
25954cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    // to have some meaningful values as soon as possible.
260b4393ef4ef3edb785746c37fd7b68950e85283aeGlenn Kasten    int32_t channelCount;
261b4393ef4ef3edb785746c37fd7b68950e85283aeGlenn Kasten    bool hasChannelCount = meta->findInt32(kKeyChannelCount, &channelCount);
26254cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    int32_t sr;
26354cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    bool hasSampleRate = meta->findInt32(kKeySampleRate, &sr);
2647f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
26513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    off64_t size;
26613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    int64_t durationUs;
26713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (dataSource->getSize(&size) == OK
26813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            && meta->findInt64(kKeyDuration, &durationUs)) {
2695050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi        if (durationUs != 0) {
2705050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi            mBitrate = size * 8000000ll / durationUs;  // in bits/sec
2715050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi        } else {
2725050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi            mBitrate = -1;
2735050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi        }
27413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mDurationUsec = durationUs;
2757f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        mDurationMsec = durationUs / 1000;
27613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    } else {
27713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mBitrate = -1;
2785050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi        mDurationUsec = ANDROID_UNKNOWN_TIME;
2795050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi        mDurationMsec = ANDROID_UNKNOWN_TIME;
28013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
28113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
282e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    // the audio content is not raw PCM, so we need a decoder
28313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (!isRawAudio) {
28413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        OMXClient client;
28513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        CHECK_EQ(client.connect(), (status_t)OK);
28613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
28713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        source = OMXCodec::Create(
28813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                client.interface(), meta, false /* createEncoder */,
28913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                source);
29013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
29113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (source == NULL) {
29213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            SL_LOGE("AudioSfDecoder::onPrepare: Could not instantiate decoder.");
29313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            notifyPrepared(ERROR_UNSUPPORTED);
29413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            return;
29513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
29613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
29713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        meta = source->getFormat();
29813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
29913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
30013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
30113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (source->start() != OK) {
30213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGE("AudioSfDecoder::onPrepare: Failed to start source/decoder.");
30313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        notifyPrepared(MEDIA_ERROR_BASE);
30413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        return;
30513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
30613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
307e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    //---------------------------------
308e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    // The data source, and audio source (a decoder if required) are ready to be used
30913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    mDataSource = dataSource;
31013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    mAudioSource = source;
311e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    mAudioSourceStarted = true;
31213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
3137f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    if (!hasChannelCount) {
314b4393ef4ef3edb785746c37fd7b68950e85283aeGlenn Kasten        CHECK(meta->findInt32(kKeyChannelCount, &channelCount));
3157f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    }
31654cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi
31754cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    if (!hasSampleRate) {
31854cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi        CHECK(meta->findInt32(kKeySampleRate, &sr));
31954cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    }
3207f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    // FIXME add code below once channel mask support is in, currently initialized to default
32120d9a1229c7647dd2c6f1bece715080ec6202ecaGlenn Kasten    //       value computed from the channel count
32220d9a1229c7647dd2c6f1bece715080ec6202ecaGlenn Kasten    //    if (!hasChannelMask) {
323b4393ef4ef3edb785746c37fd7b68950e85283aeGlenn Kasten    //        CHECK(meta->findInt32(kKeyChannelMask, &channelMask));
3247f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    //    }
32513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
32613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (!wantPrefetch()) {
32713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGV("AudioSfDecoder::onPrepare: no need to prefetch");
32813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        // doesn't need prefetching, notify good to go
32913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mCacheStatus = kStatusHigh;
33013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mCacheFill = 1000;
33113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        notifyStatus();
33213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        notifyCacheFill();
33313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
33413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
33554cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    {
33654cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi        android::Mutex::Autolock autoLock(mPcmFormatLock);
337b4393ef4ef3edb785746c37fd7b68950e85283aeGlenn Kasten        mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_SAMPLERATE] = sr;
338b4393ef4ef3edb785746c37fd7b68950e85283aeGlenn Kasten        mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_NUMCHANNELS] = channelCount;
339b4393ef4ef3edb785746c37fd7b68950e85283aeGlenn Kasten        mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_CHANNELMASK] =
340b4393ef4ef3edb785746c37fd7b68950e85283aeGlenn Kasten                channelCountToMask(channelCount);
34154cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    }
34254cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi
34313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    // at this point we have enough information about the source to create the sink that
34413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    // will consume the data
34513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    createAudioSink();
34613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
347e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten    // signal successful completion of prepare
348e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten    mStateFlags |= kFlagPrepared;
349bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
35013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    GenericPlayer::onPrepare();
351e9236d046fdb5cac0696c42e03443a2439188146Jean-Michel Trivi    SL_LOGD("AudioSfDecoder::onPrepare() done, mStateFlags=0x%x", mStateFlags);
35213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
35313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
35413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
35513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::onPause() {
35654cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    SL_LOGV("AudioSfDecoder::onPause()");
35713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    GenericPlayer::onPause();
35813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    pauseAudioSink();
35913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
36013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
36113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
36213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::onPlay() {
36354cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    SL_LOGV("AudioSfDecoder::onPlay()");
36413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    GenericPlayer::onPlay();
36513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    startAudioSink();
36613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
36713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
36813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
36913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::onSeek(const sp<AMessage> &msg) {
37013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    SL_LOGV("AudioSfDecoder::onSeek");
37113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    int64_t timeMsec;
37213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    CHECK(msg->findInt64(WHATPARAM_SEEK_SEEKTIME_MS, &timeMsec));
37313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
3745050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi    Mutex::Autolock _l(mTimeLock);
37513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    mStateFlags |= kFlagSeeking;
37613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    mSeekTimeMsec = timeMsec;
377ddaf8fec2c6362785f8f27e59e30bf6bfe858f3bGlenn Kasten    // don't set mLastDecodedPositionUs to ANDROID_UNKNOWN_TIME; getPositionUsec
378ddaf8fec2c6362785f8f27e59e30bf6bfe858f3bGlenn Kasten    // ignores mLastDecodedPositionUs while seeking, and substitutes the seek goal instead
379ddaf8fec2c6362785f8f27e59e30bf6bfe858f3bGlenn Kasten
380ddaf8fec2c6362785f8f27e59e30bf6bfe858f3bGlenn Kasten    // nop for now
381ddaf8fec2c6362785f8f27e59e30bf6bfe858f3bGlenn Kasten    GenericPlayer::onSeek(msg);
38213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
38313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
38413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
38513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::onLoop(const sp<AMessage> &msg) {
38613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    SL_LOGV("AudioSfDecoder::onLoop");
38713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    int32_t loop;
38813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    CHECK(msg->findInt32(WHATPARAM_LOOP_LOOPING, &loop));
38913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
39013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (loop) {
39113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        //SL_LOGV("AudioSfDecoder::onLoop start looping");
39213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mStateFlags |= kFlagLooping;
39313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    } else {
39413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        //SL_LOGV("AudioSfDecoder::onLoop stop looping");
39513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mStateFlags &= ~kFlagLooping;
39613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
397ddaf8fec2c6362785f8f27e59e30bf6bfe858f3bGlenn Kasten
398ddaf8fec2c6362785f8f27e59e30bf6bfe858f3bGlenn Kasten    // nop for now
399ddaf8fec2c6362785f8f27e59e30bf6bfe858f3bGlenn Kasten    GenericPlayer::onLoop(msg);
40013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
40113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
40213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
40313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::onCheckCache(const sp<AMessage> &msg) {
40413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    //SL_LOGV("AudioSfDecoder::onCheckCache");
40513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    bool eos;
4064ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi    CacheStatus_t status = getCacheRemaining(&eos);
40713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
40813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (eos || status == kStatusHigh
40913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            || ((mStateFlags & kFlagPreparing) && (status >= kStatusEnough))) {
41013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (mStateFlags & kFlagPlaying) {
41113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            startAudioSink();
41213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
41313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mStateFlags &= ~kFlagBuffering;
41413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
41513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGV("AudioSfDecoder::onCheckCache: buffering done.");
41613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
41713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (mStateFlags & kFlagPreparing) {
41813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            //SL_LOGV("AudioSfDecoder::onCheckCache: preparation done.");
41913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            mStateFlags &= ~kFlagPreparing;
42013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
42113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
42213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (mStateFlags & kFlagPlaying) {
42313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            (new AMessage(kWhatDecode, id()))->post();
42413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
42513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        return;
42613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
42713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
42813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    msg->post(100000);
42913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
43013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
43113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
43213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::onDecode() {
43313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    SL_LOGV("AudioSfDecoder::onDecode");
43413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
43513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    //-------------------------------- Need to buffer some more before decoding?
43613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    bool eos;
43713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (mDataSource == 0) {
43813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        // application set play state to paused which failed, then set play state to playing
43913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        return;
44013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
441e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi
44213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (wantPrefetch()
44313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            && (getCacheRemaining(&eos) == kStatusLow)
44413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            && !eos) {
44513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGV("buffering more.");
44613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
44713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (mStateFlags & kFlagPlaying) {
44813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            pauseAudioSink();
44913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
45013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mStateFlags |= kFlagBuffering;
45113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        (new AMessage(kWhatCheckCache, id()))->post(100000);
45213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        return;
45313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
45413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
45513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (!(mStateFlags & (kFlagPlaying | kFlagBuffering | kFlagPreparing))) {
45613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        // don't decode if we're not buffering, prefetching or playing
45713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        //SL_LOGV("don't decode: not buffering, prefetching or playing");
45813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        return;
45913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
46013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
46113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    //-------------------------------- Decode
46213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    status_t err;
46313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    MediaSource::ReadOptions readOptions;
46413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (mStateFlags & kFlagSeeking) {
4655050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi        assert(mSeekTimeMsec != ANDROID_UNKNOWN_TIME);
46613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        readOptions.setSeekTo(mSeekTimeMsec * 1000);
46713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
46813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
4695050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi    int64_t timeUsec = ANDROID_UNKNOWN_TIME;
47013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    {
471e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        Mutex::Autolock _l(mBufferSourceLock);
472e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi
47313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (NULL != mDecodeBuffer) {
47413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            // the current decoded buffer hasn't been rendered, drop it
47513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            mDecodeBuffer->release();
47613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            mDecodeBuffer = NULL;
47713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
478e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        if(!mAudioSourceStarted) {
479e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi            return;
480e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        }
48113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        err = mAudioSource->read(&mDecodeBuffer, &readOptions);
48213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (err == OK) {
4835050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi            CHECK(mDecodeBuffer->meta_data()->findInt64(kKeyTime, &timeUsec));
48413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
48513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
48613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
48713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    {
4885050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi        Mutex::Autolock _l(mTimeLock);
48913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (mStateFlags & kFlagSeeking) {
49013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            mStateFlags &= ~kFlagSeeking;
4915050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi            mSeekTimeMsec = ANDROID_UNKNOWN_TIME;
4925050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi        }
4935050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi        if (timeUsec != ANDROID_UNKNOWN_TIME) {
4947349b2e742b2cedc6d149fac62ed661ad7d47decGlenn Kasten            // Note that though we've decoded this position, we haven't rendered it yet.
4957349b2e742b2cedc6d149fac62ed661ad7d47decGlenn Kasten            // So a GetPosition called after this point will observe the advanced position,
4967349b2e742b2cedc6d149fac62ed661ad7d47decGlenn Kasten            // even though the PCM may not have been supplied to the sink.  That's OK as
4977349b2e742b2cedc6d149fac62ed661ad7d47decGlenn Kasten            // we don't claim to provide frame-accurate (let alone sample-accurate) GetPosition.
4985050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi            mLastDecodedPositionUs = timeUsec;
49913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
50013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
50113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
50213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    //-------------------------------- Handle return of decode
50313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (err != OK) {
50413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        bool continueDecoding = false;
50513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        switch(err) {
50613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            case ERROR_END_OF_STREAM:
50713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                if (0 < mDurationUsec) {
5085050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi                    Mutex::Autolock _l(mTimeLock);
50913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                    mLastDecodedPositionUs = mDurationUsec;
51013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                }
51113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                // handle notification and looping at end of stream
51213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                if (mStateFlags & kFlagPlaying) {
51313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                    notify(PLAYEREVENT_ENDOFSTREAM, 1, true);
51413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                }
51513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                if (mStateFlags & kFlagLooping) {
51613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                    seek(0);
51713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                    // kick-off decoding again
51813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                    continueDecoding = true;
51913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                }
52013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                break;
52113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            case INFO_FORMAT_CHANGED:
522e9236d046fdb5cac0696c42e03443a2439188146Jean-Michel Trivi                SL_LOGD("MediaSource::read encountered INFO_FORMAT_CHANGED");
52313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                // reconfigure output
52454cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi                {
52554cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi                    Mutex::Autolock _l(mBufferSourceLock);
52654cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi                    hasNewDecodeParams();
52754cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi                }
52813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                continueDecoding = true;
52913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                break;
53013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            case INFO_DISCONTINUITY:
531e9236d046fdb5cac0696c42e03443a2439188146Jean-Michel Trivi                SL_LOGD("MediaSource::read encountered INFO_DISCONTINUITY");
53213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                continueDecoding = true;
53313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                break;
53413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            default:
53513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                SL_LOGE("MediaSource::read returned error %d", err);
53613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                break;
53713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
53813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (continueDecoding) {
53913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            if (NULL == mDecodeBuffer) {
54013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                (new AMessage(kWhatDecode, id()))->post();
54113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                return;
54213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            }
54313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        } else {
54413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            return;
54513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
54613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
54713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
54813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    //-------------------------------- Render
54913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    sp<AMessage> msg = new AMessage(kWhatRender, id());
55013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    msg->post();
55113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
55213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
55313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
55413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
55513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::onMessageReceived(const sp<AMessage> &msg) {
55613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    switch (msg->what()) {
55713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        case kWhatDecode:
55813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            onDecode();
55913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            break;
56013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
56113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        case kWhatRender:
56213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            onRender();
56313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            break;
56413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
56513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        case kWhatCheckCache:
56613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            onCheckCache(msg);
56713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            break;
56813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
56913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        default:
57013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            GenericPlayer::onMessageReceived(msg);
57113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            break;
57213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
57313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
57413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
57513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi//--------------------------------------------------
57613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi// Prepared state, prefetch status notifications
57713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::notifyPrepared(status_t prepareRes) {
578e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten    assert(!(mStateFlags & (kFlagPrepared | kFlagPreparedUnsuccessfully)));
579e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten    if (NO_ERROR == prepareRes) {
580e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten        // The "then" fork is not currently used, but is kept here to make it easier
581e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten        // to replace by a new signalPrepareCompletion(status) if we re-visit this later.
582e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten        mStateFlags |= kFlagPrepared;
583e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten    } else {
584e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten        mStateFlags |= kFlagPreparedUnsuccessfully;
585e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten    }
586e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten    // Do not call the superclass onPrepare to notify, because it uses a default error
587e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten    // status code but we can provide a more specific one.
588e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten    // GenericPlayer::onPrepare();
58913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    notify(PLAYEREVENT_PREPARED, (int32_t)prepareRes, true);
590e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten    SL_LOGD("AudioSfDecoder::onPrepare() done, mStateFlags=0x%x", mStateFlags);
59113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
59213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
59313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
59413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::onNotify(const sp<AMessage> &msg) {
595e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    notif_cbf_t notifyClient;
596e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    void*       notifyUser;
597e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    {
598e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        android::Mutex::Autolock autoLock(mNotifyClientLock);
599e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        if (NULL == mNotifyClient) {
600e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi            return;
601e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        } else {
602e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi            notifyClient = mNotifyClient;
603e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi            notifyUser   = mNotifyUser;
604e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        }
60513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
60613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    int32_t val;
60713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (msg->findInt32(PLAYEREVENT_PREFETCHSTATUSCHANGE, &val)) {
60813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGV("\tASfPlayer notifying %s = %d", PLAYEREVENT_PREFETCHSTATUSCHANGE, val);
609e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        notifyClient(kEventPrefetchStatusChange, val, 0, notifyUser);
61013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
61113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    else if (msg->findInt32(PLAYEREVENT_PREFETCHFILLLEVELUPDATE, &val)) {
61213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGV("\tASfPlayer notifying %s = %d", PLAYEREVENT_PREFETCHFILLLEVELUPDATE, val);
613e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        notifyClient(kEventPrefetchFillLevelUpdate, val, 0, notifyUser);
61413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
61513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    else if (msg->findInt32(PLAYEREVENT_ENDOFSTREAM, &val)) {
61613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGV("\tASfPlayer notifying %s = %d", PLAYEREVENT_ENDOFSTREAM, val);
617e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        notifyClient(kEventEndOfStream, val, 0, notifyUser);
61813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
61913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    else {
62013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        GenericPlayer::onNotify(msg);
62113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
62213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
62313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
62413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
62513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi//--------------------------------------------------
62613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi// Private utility functions
62713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
62813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivibool AudioSfDecoder::wantPrefetch() {
629ac18c1cd32408884d3960bd7aa56ba419c2ca68bJean-Michel Trivi    if (mDataSource != 0) {
630ac18c1cd32408884d3960bd7aa56ba419c2ca68bJean-Michel Trivi        return (mDataSource->flags() & DataSource::kWantsPrefetching);
631ac18c1cd32408884d3960bd7aa56ba419c2ca68bJean-Michel Trivi    } else {
632ac18c1cd32408884d3960bd7aa56ba419c2ca68bJean-Michel Trivi        // happens if an improper data locator was passed, if the media extractor couldn't be
633ac18c1cd32408884d3960bd7aa56ba419c2ca68bJean-Michel Trivi        //  initialized, if there is no audio track in the media, if the OMX decoder couldn't be
634ac18c1cd32408884d3960bd7aa56ba419c2ca68bJean-Michel Trivi        //  instantiated, if the source couldn't be opened, or if the MediaSource
635ac18c1cd32408884d3960bd7aa56ba419c2ca68bJean-Michel Trivi        //  couldn't be started
636ac18c1cd32408884d3960bd7aa56ba419c2ca68bJean-Michel Trivi        SL_LOGV("AudioSfDecoder::wantPrefetch() tries to access NULL mDataSource");
637ac18c1cd32408884d3960bd7aa56ba419c2ca68bJean-Michel Trivi        return false;
638ac18c1cd32408884d3960bd7aa56ba419c2ca68bJean-Michel Trivi    }
63913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
64013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
64113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
64213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Triviint64_t AudioSfDecoder::getPositionUsec() {
6435050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi    Mutex::Autolock _l(mTimeLock);
64413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (mStateFlags & kFlagSeeking) {
64513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        return mSeekTimeMsec * 1000;
64613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    } else {
647ddaf8fec2c6362785f8f27e59e30bf6bfe858f3bGlenn Kasten        return mLastDecodedPositionUs;
64813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
64913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
65013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
65113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
6524ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel TriviCacheStatus_t AudioSfDecoder::getCacheRemaining(bool *eos) {
65313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    sp<NuCachedSource2> cachedSource =
65413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        static_cast<NuCachedSource2 *>(mDataSource.get());
65513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
6564ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi    CacheStatus_t oldStatus = mCacheStatus;
65713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
65813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    status_t finalStatus;
65913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    size_t dataRemaining = cachedSource->approxDataRemaining(&finalStatus);
66013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    *eos = (finalStatus != OK);
66113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
66213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    CHECK_GE(mBitrate, 0);
66313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
66413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    int64_t dataRemainingUs = dataRemaining * 8000000ll / mBitrate;
66513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    //SL_LOGV("AudioSfDecoder::getCacheRemaining: approx %.2f secs remaining (eos=%d)",
66613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    //       dataRemainingUs / 1E6, *eos);
66713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
66813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (*eos) {
66913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        // data is buffered up to the end of the stream, it can't get any better than this
67013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mCacheStatus = kStatusHigh;
67113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mCacheFill = 1000;
67213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
67313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    } else {
67413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (mDurationUsec > 0) {
67513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            // known duration:
67613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
67713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            //   fill level is ratio of how much has been played + how much is
67813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            //   cached, divided by total duration
67913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            uint32_t currentPositionUsec = getPositionUsec();
6805050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi            if (currentPositionUsec == ANDROID_UNKNOWN_TIME) {
6815050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi                // if we don't know where we are, assume the worst for the fill ratio
6825050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi                currentPositionUsec = 0;
6835050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi            }
6845050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi            if (mDurationUsec > 0) {
6855050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi                mCacheFill = (int16_t) ((1000.0
6865050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi                        * (double)(currentPositionUsec + dataRemainingUs) / mDurationUsec));
6875050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi            } else {
6885050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi                mCacheFill = 0;
6895050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi            }
69013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            //SL_LOGV("cacheFill = %d", mCacheFill);
69113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
69213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            //   cache status is evaluated against duration thresholds
6934ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi            if (dataRemainingUs > DURATION_CACHED_HIGH_MS*1000) {
69413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                mCacheStatus = kStatusHigh;
69513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                //LOGV("high");
6964ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi            } else if (dataRemainingUs > DURATION_CACHED_MED_MS*1000) {
69713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                //LOGV("enough");
69813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                mCacheStatus = kStatusEnough;
6994ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi            } else if (dataRemainingUs < DURATION_CACHED_LOW_MS*1000) {
70013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                //LOGV("low");
70113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                mCacheStatus = kStatusLow;
70213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            } else {
70313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                mCacheStatus = kStatusIntermediate;
70413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            }
70513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
70613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        } else {
70713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            // unknown duration:
70813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
70913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            //   cache status is evaluated against cache amount thresholds
71013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            //   (no duration so we don't have the bitrate either, could be derived from format?)
71113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            if (dataRemaining > SIZE_CACHED_HIGH_BYTES) {
71213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                mCacheStatus = kStatusHigh;
71313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            } else if (dataRemaining > SIZE_CACHED_MED_BYTES) {
71413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                mCacheStatus = kStatusEnough;
71513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            } else if (dataRemaining < SIZE_CACHED_LOW_BYTES) {
71613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                mCacheStatus = kStatusLow;
71713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            } else {
71813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                mCacheStatus = kStatusIntermediate;
71913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            }
72013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
72113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
72213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
72313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
72413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (oldStatus != mCacheStatus) {
72513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        notifyStatus();
72613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
72713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
72813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (abs(mCacheFill - mLastNotifiedCacheFill) > mCacheFillNotifThreshold) {
72913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        notifyCacheFill();
73013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
73113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
73213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    return mCacheStatus;
73313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
73413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
73554cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi
73654cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivivoid AudioSfDecoder::hasNewDecodeParams() {
73754cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi
73854cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    if ((mAudioSource != 0) && mAudioSourceStarted) {
73954cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi        sp<MetaData> meta = mAudioSource->getFormat();
74054cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi
741b4393ef4ef3edb785746c37fd7b68950e85283aeGlenn Kasten        int32_t channelCount;
742b4393ef4ef3edb785746c37fd7b68950e85283aeGlenn Kasten        CHECK(meta->findInt32(kKeyChannelCount, &channelCount));
74354cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi        int32_t sr;
74454cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi        CHECK(meta->findInt32(kKeySampleRate, &sr));
74554cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi
74620d9a1229c7647dd2c6f1bece715080ec6202ecaGlenn Kasten        // FIXME similar to onPrepare()
74754cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi        {
74854cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi            android::Mutex::Autolock autoLock(mPcmFormatLock);
749b4393ef4ef3edb785746c37fd7b68950e85283aeGlenn Kasten            SL_LOGV("format changed: old sr=%d, channels=%d; new sr=%d, channels=%d",
750b4393ef4ef3edb785746c37fd7b68950e85283aeGlenn Kasten                    mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_SAMPLERATE],
751b4393ef4ef3edb785746c37fd7b68950e85283aeGlenn Kasten                    mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_NUMCHANNELS],
752b4393ef4ef3edb785746c37fd7b68950e85283aeGlenn Kasten                    sr, channelCount);
753b4393ef4ef3edb785746c37fd7b68950e85283aeGlenn Kasten            mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_NUMCHANNELS] = channelCount;
754b4393ef4ef3edb785746c37fd7b68950e85283aeGlenn Kasten            mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_SAMPLERATE] = sr;
755b4393ef4ef3edb785746c37fd7b68950e85283aeGlenn Kasten            mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_CHANNELMASK] =
756b4393ef4ef3edb785746c37fd7b68950e85283aeGlenn Kasten                    channelCountToMask(channelCount);
75754cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi        }
75854cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    }
75954cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi
76054cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    // alert users of those params
76154cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    updateAudioSink();
76254cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi}
76354cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi
76413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi} // namespace android
765