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
220c7d40a5f11f6930e6c4b2551a14f41dd721f936Jean-Michel Trivi#include <binder/IServiceManager.h>
234ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi#include <media/stagefright/foundation/ADebug.h>
244ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi
254ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi
264ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi#define SIZE_CACHED_HIGH_BYTES 1000000
274ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi#define SIZE_CACHED_MED_BYTES   700000
284ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi#define SIZE_CACHED_LOW_BYTES   400000
294ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi
3013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivinamespace android {
3113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
3213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi//--------------------------------------------------------------------------------------------------
3313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel TriviAudioSfDecoder::AudioSfDecoder(const AudioPlayback_Parameters* params) : GenericPlayer(params),
34b4fb100d7122d118d3da9d1d08ffacef68dd38b0Jean-Michel Trivi        mDataSource(0),
35b4fb100d7122d118d3da9d1d08ffacef68dd38b0Jean-Michel Trivi        mAudioSource(0),
36e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        mAudioSourceStarted(false),
3713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mBitrate(-1),
385050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi        mDurationUsec(ANDROID_UNKNOWN_TIME),
3913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mDecodeBuffer(NULL),
4013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mSeekTimeMsec(0),
41ddaf8fec2c6362785f8f27e59e30bf6bfe858f3bGlenn Kasten        // play event logic depends on the initial time being zero not ANDROID_UNKNOWN_TIME
42ddaf8fec2c6362785f8f27e59e30bf6bfe858f3bGlenn Kasten        mLastDecodedPositionUs(0)
4313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi{
4454cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    SL_LOGD("AudioSfDecoder::AudioSfDecoder()");
4513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
4613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
4713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
4813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel TriviAudioSfDecoder::~AudioSfDecoder() {
4954cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    SL_LOGD("AudioSfDecoder::~AudioSfDecoder()");
50e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi}
51e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi
52e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi
53e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivivoid AudioSfDecoder::preDestroy() {
54e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    GenericPlayer::preDestroy();
55e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    SL_LOGD("AudioSfDecoder::preDestroy()");
56e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    {
57e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        Mutex::Autolock _l(mBufferSourceLock);
58e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi
59e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        if (NULL != mDecodeBuffer) {
60e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi            mDecodeBuffer->release();
61e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi            mDecodeBuffer = NULL;
62e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        }
63e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi
64e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        if ((mAudioSource != 0) && mAudioSourceStarted) {
65e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi            mAudioSource->stop();
66e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi            mAudioSourceStarted = false;
67e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        }
68b4fb100d7122d118d3da9d1d08ffacef68dd38b0Jean-Michel Trivi    }
6913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
7013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
7113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
7213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi//--------------------------------------------------
7313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::play() {
7454cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    SL_LOGD("AudioSfDecoder::play");
7513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
7613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    GenericPlayer::play();
7713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    (new AMessage(kWhatDecode, id()))->post();
7813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
7913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
8013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
815050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivivoid AudioSfDecoder::getPositionMsec(int* msec) {
825050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi    int64_t timeUsec = getPositionUsec();
835050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi    if (timeUsec == ANDROID_UNKNOWN_TIME) {
845050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi        *msec = ANDROID_UNKNOWN_TIME;
855050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi    } else {
865933f3d5e532aaac31ce0e6551c59f0197c0ae3cGlenn Kasten        *msec = timeUsec / 1000;
875050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi    }
885050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi}
895050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi
905050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi
9113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi//--------------------------------------------------
9291540f92d7f1bcda423859af6bd82df083c2afabGlenn Kastenuint32_t AudioSfDecoder::getPcmFormatKeyCount() const {
9391540f92d7f1bcda423859af6bd82df083c2afabGlenn Kasten    return NB_PCMMETADATA_KEYS;
947f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi}
957f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
967f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
977f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi//--------------------------------------------------
987f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivibool AudioSfDecoder::getPcmFormatKeySize(uint32_t index, uint32_t* pKeySize) {
9991540f92d7f1bcda423859af6bd82df083c2afabGlenn Kasten    if (index >= NB_PCMMETADATA_KEYS) {
1007f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        return false;
1017f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    } else {
1027f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        *pKeySize = strlen(kPcmDecodeMetadataKeys[index]) +1;
1037f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        return true;
1047f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    }
1057f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi}
1067f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
1077f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
1087f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi//--------------------------------------------------
1097f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivibool AudioSfDecoder::getPcmFormatKeyName(uint32_t index, uint32_t keySize, char* keyName) {
1107f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    uint32_t actualKeySize;
1117f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    if (!getPcmFormatKeySize(index, &actualKeySize)) {
1127f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        return false;
1137f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    }
1147f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    if (keySize < actualKeySize) {
1157f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        return false;
1167f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    }
1177f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    strncpy(keyName, kPcmDecodeMetadataKeys[index], actualKeySize);
1187f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    return true;
1197f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi}
1207f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
1217f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
1227f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi//--------------------------------------------------
1237f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivibool AudioSfDecoder::getPcmFormatValueSize(uint32_t index, uint32_t* pValueSize) {
12491540f92d7f1bcda423859af6bd82df083c2afabGlenn Kasten    if (index >= NB_PCMMETADATA_KEYS) {
1257f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        *pValueSize = 0;
1267f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        return false;
1277f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    } else {
1287f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        *pValueSize = sizeof(uint32_t);
1297f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        return true;
1307f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    }
1317f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi}
1327f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
1337f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
1347f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi//--------------------------------------------------
1357f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivibool AudioSfDecoder::getPcmFormatKeyValue(uint32_t index, uint32_t size, uint32_t* pValue) {
1367f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    uint32_t valueSize = 0;
1377f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    if (!getPcmFormatValueSize(index, &valueSize)) {
1387f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        return false;
13913d02b645fc6e8ffe70a8bf8cc5f69f03558ae40Jean-Michel Trivi    } else if (size != valueSize) {
1407f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        // this ensures we are accessing mPcmFormatValues with a valid size for that index
14113d02b645fc6e8ffe70a8bf8cc5f69f03558ae40Jean-Michel Trivi        SL_LOGE("Error retrieving metadata value at index %d: using size of %d, should be %d",
14213d02b645fc6e8ffe70a8bf8cc5f69f03558ae40Jean-Michel Trivi                index, size, valueSize);
1437f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        return false;
1447f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    } else {
14591540f92d7f1bcda423859af6bd82df083c2afabGlenn Kasten        android::Mutex::Autolock autoLock(mPcmFormatLock);
1467f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        *pValue = mPcmFormatValues[index];
1477f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        return true;
1487f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    }
1497f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi}
1507f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
1517f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
1527f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi//--------------------------------------------------
15313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi// Event handlers
154e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi//  it is strictly verboten to call those methods outside of the event loop
155e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi
156e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi// Initializes the data and audio sources, and update the PCM format info
157e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi// post-condition: upon successful initialization based on the player data locator
158e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi//    GenericPlayer::onPrepare() was called
159e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi//    mDataSource != 0
160e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi//    mAudioSource != 0
161e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi//    mAudioSourceStarted == true
162e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten// All error returns from this method are via notifyPrepared(status) followed by "return".
16313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::onPrepare() {
164e9236d046fdb5cac0696c42e03443a2439188146Jean-Michel Trivi    SL_LOGD("AudioSfDecoder::onPrepare()");
165e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    Mutex::Autolock _l(mBufferSourceLock);
16613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
16791540f92d7f1bcda423859af6bd82df083c2afabGlenn Kasten    {
16891540f92d7f1bcda423859af6bd82df083c2afabGlenn Kasten    android::Mutex::Autolock autoLock(mPcmFormatLock);
169e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    // Initialize the PCM format info with the known parameters before the start of the decode
1707f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_BITSPERSAMPLE] = SL_PCMSAMPLEFORMAT_FIXED_16;
1717f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_CONTAINERSIZE] = 16;
1727f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_ENDIANNESS] = SL_BYTEORDER_LITTLEENDIAN;
173e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    //    initialization with the default values: they will be replaced by the actual values
174e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    //      once the decoder has figured them out
175b4393ef4ef3edb785746c37fd7b68950e85283aeGlenn Kasten    mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_NUMCHANNELS] = UNKNOWN_NUMCHANNELS;
176b4393ef4ef3edb785746c37fd7b68950e85283aeGlenn Kasten    mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_SAMPLERATE] = UNKNOWN_SAMPLERATE;
177b4393ef4ef3edb785746c37fd7b68950e85283aeGlenn Kasten    mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_CHANNELMASK] = UNKNOWN_CHANNELMASK;
17891540f92d7f1bcda423859af6bd82df083c2afabGlenn Kasten    }
1797f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
180e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    //---------------------------------
181e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    // Instantiate and initialize the data source for the decoder
18213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    sp<DataSource> dataSource;
18313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
18413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    switch (mDataLocatorType) {
18513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
18613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    case kDataLocatorNone:
18713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGE("AudioSfDecoder::onPrepare: no data locator set");
18813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        notifyPrepared(MEDIA_ERROR_BASE);
18913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        return;
19013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
19113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    case kDataLocatorUri:
19293ac9bd4f7722c50dc9882ff74bade233860a940Jean-Michel Trivi        dataSource = DataSource::CreateFromURI(mDataLocator.uriRef);
19393ac9bd4f7722c50dc9882ff74bade233860a940Jean-Michel Trivi        if (dataSource == NULL) {
19493ac9bd4f7722c50dc9882ff74bade233860a940Jean-Michel Trivi            SL_LOGE("AudioSfDecoder::onPrepare(): Error opening %s", mDataLocator.uriRef);
19513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            notifyPrepared(MEDIA_ERROR_BASE);
19613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            return;
19713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
19813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        break;
19913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
20093ac9bd4f7722c50dc9882ff74bade233860a940Jean-Michel Trivi    case kDataLocatorFd:
20193ac9bd4f7722c50dc9882ff74bade233860a940Jean-Michel Trivi    {
202833251ab9e5e59a6ea5ac325122cf3abdf7cd944Glenn Kasten        // As FileSource unconditionally takes ownership of the fd and closes it, then
203833251ab9e5e59a6ea5ac325122cf3abdf7cd944Glenn Kasten        // we have to make a dup for FileSource if the app wants to keep ownership itself
204833251ab9e5e59a6ea5ac325122cf3abdf7cd944Glenn Kasten        int fd = mDataLocator.fdi.fd;
205833251ab9e5e59a6ea5ac325122cf3abdf7cd944Glenn Kasten        if (mDataLocator.fdi.mCloseAfterUse) {
206833251ab9e5e59a6ea5ac325122cf3abdf7cd944Glenn Kasten            mDataLocator.fdi.mCloseAfterUse = false;
207833251ab9e5e59a6ea5ac325122cf3abdf7cd944Glenn Kasten        } else {
208833251ab9e5e59a6ea5ac325122cf3abdf7cd944Glenn Kasten            fd = ::dup(fd);
209833251ab9e5e59a6ea5ac325122cf3abdf7cd944Glenn Kasten        }
210833251ab9e5e59a6ea5ac325122cf3abdf7cd944Glenn Kasten        dataSource = new FileSource(fd, mDataLocator.fdi.offset, mDataLocator.fdi.length);
21113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        status_t err = dataSource->initCheck();
21213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (err != OK) {
21313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            notifyPrepared(err);
21413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            return;
21513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
21693ac9bd4f7722c50dc9882ff74bade233860a940Jean-Michel Trivi        break;
21713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
21813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
219e2e8fa36bd7448b59fbcdf141e0b6d21e5401d91Glenn Kasten    // AndroidBufferQueue data source is handled by a subclass,
220e2e8fa36bd7448b59fbcdf141e0b6d21e5401d91Glenn Kasten    // which does not call up to this method.  Hence, the missing case.
22113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    default:
22213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        TRESPASS();
22313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
22413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
225e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    //---------------------------------
226e2e8fa36bd7448b59fbcdf141e0b6d21e5401d91Glenn Kasten    // Instantiate and initialize the decoder attached to the data source
22713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    sp<MediaExtractor> extractor = MediaExtractor::Create(dataSource);
22813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (extractor == NULL) {
22913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGE("AudioSfDecoder::onPrepare: Could not instantiate extractor.");
23013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        notifyPrepared(ERROR_UNSUPPORTED);
23113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        return;
23213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
23313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
23413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    ssize_t audioTrackIndex = -1;
23513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    bool isRawAudio = false;
23613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    for (size_t i = 0; i < extractor->countTracks(); ++i) {
23713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        sp<MetaData> meta = extractor->getTrackMetaData(i);
23813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
23913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        const char *mime;
24013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        CHECK(meta->findCString(kKeyMIMEType, &mime));
24113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
24213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (!strncasecmp("audio/", mime, 6)) {
2433597268c2bf4ff71521e3cbe522d7ee02c41f175Jean-Michel Trivi            if (isSupportedCodec(mime)) {
2443597268c2bf4ff71521e3cbe522d7ee02c41f175Jean-Michel Trivi                audioTrackIndex = i;
24513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
2463597268c2bf4ff71521e3cbe522d7ee02c41f175Jean-Michel Trivi                if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_RAW, mime)) {
2473597268c2bf4ff71521e3cbe522d7ee02c41f175Jean-Michel Trivi                    isRawAudio = true;
2483597268c2bf4ff71521e3cbe522d7ee02c41f175Jean-Michel Trivi                }
2493597268c2bf4ff71521e3cbe522d7ee02c41f175Jean-Michel Trivi                break;
25013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            }
25113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
25213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
25313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
25413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (audioTrackIndex < 0) {
25513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGE("AudioSfDecoder::onPrepare: Could not find a supported audio track.");
25613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        notifyPrepared(ERROR_UNSUPPORTED);
25713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        return;
25813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
25913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
26013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    sp<MediaSource> source = extractor->getTrack(audioTrackIndex);
26113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    sp<MetaData> meta = source->getFormat();
26213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
26354cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    // we can't trust the OMXCodec (if there is one) to issue a INFO_FORMAT_CHANGED so we want
26454cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    // to have some meaningful values as soon as possible.
265b4393ef4ef3edb785746c37fd7b68950e85283aeGlenn Kasten    int32_t channelCount;
266b4393ef4ef3edb785746c37fd7b68950e85283aeGlenn Kasten    bool hasChannelCount = meta->findInt32(kKeyChannelCount, &channelCount);
26754cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    int32_t sr;
26854cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    bool hasSampleRate = meta->findInt32(kKeySampleRate, &sr);
2697f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
2701fa5c3206d06bbebdea2dc92f378ce6b8a211e23Glenn Kasten    // first compute the duration
27113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    off64_t size;
27213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    int64_t durationUs;
2731fa5c3206d06bbebdea2dc92f378ce6b8a211e23Glenn Kasten    int32_t durationMsec;
27413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (dataSource->getSize(&size) == OK
27513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            && meta->findInt64(kKeyDuration, &durationUs)) {
2765050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi        if (durationUs != 0) {
2775050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi            mBitrate = size * 8000000ll / durationUs;  // in bits/sec
2785050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi        } else {
2795050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi            mBitrate = -1;
2805050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi        }
28113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mDurationUsec = durationUs;
2821fa5c3206d06bbebdea2dc92f378ce6b8a211e23Glenn Kasten        durationMsec = durationUs / 1000;
28313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    } else {
28413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mBitrate = -1;
2855050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi        mDurationUsec = ANDROID_UNKNOWN_TIME;
2861fa5c3206d06bbebdea2dc92f378ce6b8a211e23Glenn Kasten        durationMsec = ANDROID_UNKNOWN_TIME;
2871fa5c3206d06bbebdea2dc92f378ce6b8a211e23Glenn Kasten    }
2881fa5c3206d06bbebdea2dc92f378ce6b8a211e23Glenn Kasten
2891fa5c3206d06bbebdea2dc92f378ce6b8a211e23Glenn Kasten    // then assign the duration under the settings lock
2901fa5c3206d06bbebdea2dc92f378ce6b8a211e23Glenn Kasten    {
2911fa5c3206d06bbebdea2dc92f378ce6b8a211e23Glenn Kasten        Mutex::Autolock _l(mSettingsLock);
2921fa5c3206d06bbebdea2dc92f378ce6b8a211e23Glenn Kasten        mDurationMsec = durationMsec;
29313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
29413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
295e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    // the audio content is not raw PCM, so we need a decoder
29613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (!isRawAudio) {
29713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        OMXClient client;
29813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        CHECK_EQ(client.connect(), (status_t)OK);
29913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
30013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        source = OMXCodec::Create(
30113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                client.interface(), meta, false /* createEncoder */,
30213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                source);
30313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
30413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (source == NULL) {
30513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            SL_LOGE("AudioSfDecoder::onPrepare: Could not instantiate decoder.");
30613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            notifyPrepared(ERROR_UNSUPPORTED);
30713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            return;
30813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
30913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
31013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        meta = source->getFormat();
31113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
31213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
31313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
31413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (source->start() != OK) {
31513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGE("AudioSfDecoder::onPrepare: Failed to start source/decoder.");
31613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        notifyPrepared(MEDIA_ERROR_BASE);
31713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        return;
31813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
31913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
320e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    //---------------------------------
321e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    // The data source, and audio source (a decoder if required) are ready to be used
32213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    mDataSource = dataSource;
32313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    mAudioSource = source;
324e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    mAudioSourceStarted = true;
32513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
3267f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    if (!hasChannelCount) {
327b4393ef4ef3edb785746c37fd7b68950e85283aeGlenn Kasten        CHECK(meta->findInt32(kKeyChannelCount, &channelCount));
3287f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    }
32954cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi
33054cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    if (!hasSampleRate) {
33154cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi        CHECK(meta->findInt32(kKeySampleRate, &sr));
33254cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    }
3337f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    // FIXME add code below once channel mask support is in, currently initialized to default
33420d9a1229c7647dd2c6f1bece715080ec6202ecaGlenn Kasten    //       value computed from the channel count
33520d9a1229c7647dd2c6f1bece715080ec6202ecaGlenn Kasten    //    if (!hasChannelMask) {
336b4393ef4ef3edb785746c37fd7b68950e85283aeGlenn Kasten    //        CHECK(meta->findInt32(kKeyChannelMask, &channelMask));
3377f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    //    }
33813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
33913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (!wantPrefetch()) {
34013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGV("AudioSfDecoder::onPrepare: no need to prefetch");
34113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        // doesn't need prefetching, notify good to go
34213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mCacheStatus = kStatusHigh;
34313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mCacheFill = 1000;
34413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        notifyStatus();
34513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        notifyCacheFill();
34613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
34713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
34854cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    {
34954cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi        android::Mutex::Autolock autoLock(mPcmFormatLock);
350b4393ef4ef3edb785746c37fd7b68950e85283aeGlenn Kasten        mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_SAMPLERATE] = sr;
351b4393ef4ef3edb785746c37fd7b68950e85283aeGlenn Kasten        mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_NUMCHANNELS] = channelCount;
352b4393ef4ef3edb785746c37fd7b68950e85283aeGlenn Kasten        mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_CHANNELMASK] =
353b4393ef4ef3edb785746c37fd7b68950e85283aeGlenn Kasten                channelCountToMask(channelCount);
35454cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    }
35554cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi
35613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    // at this point we have enough information about the source to create the sink that
35713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    // will consume the data
35813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    createAudioSink();
35913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
360e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten    // signal successful completion of prepare
361e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten    mStateFlags |= kFlagPrepared;
362bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
36313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    GenericPlayer::onPrepare();
364e9236d046fdb5cac0696c42e03443a2439188146Jean-Michel Trivi    SL_LOGD("AudioSfDecoder::onPrepare() done, mStateFlags=0x%x", mStateFlags);
36513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
36613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
36713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
36813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::onPause() {
36954cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    SL_LOGV("AudioSfDecoder::onPause()");
37013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    GenericPlayer::onPause();
37113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    pauseAudioSink();
37213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
37313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
37413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
37513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::onPlay() {
37654cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    SL_LOGV("AudioSfDecoder::onPlay()");
37713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    GenericPlayer::onPlay();
37813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    startAudioSink();
37913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
38013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
38113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
38213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::onSeek(const sp<AMessage> &msg) {
38313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    SL_LOGV("AudioSfDecoder::onSeek");
38413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    int64_t timeMsec;
38513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    CHECK(msg->findInt64(WHATPARAM_SEEK_SEEKTIME_MS, &timeMsec));
38613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
3875050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi    Mutex::Autolock _l(mTimeLock);
38813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    mStateFlags |= kFlagSeeking;
38913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    mSeekTimeMsec = timeMsec;
390ddaf8fec2c6362785f8f27e59e30bf6bfe858f3bGlenn Kasten    // don't set mLastDecodedPositionUs to ANDROID_UNKNOWN_TIME; getPositionUsec
391ddaf8fec2c6362785f8f27e59e30bf6bfe858f3bGlenn Kasten    // ignores mLastDecodedPositionUs while seeking, and substitutes the seek goal instead
392ddaf8fec2c6362785f8f27e59e30bf6bfe858f3bGlenn Kasten
393ddaf8fec2c6362785f8f27e59e30bf6bfe858f3bGlenn Kasten    // nop for now
394ddaf8fec2c6362785f8f27e59e30bf6bfe858f3bGlenn Kasten    GenericPlayer::onSeek(msg);
39513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
39613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
39713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
39813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::onLoop(const sp<AMessage> &msg) {
39913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    SL_LOGV("AudioSfDecoder::onLoop");
40013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    int32_t loop;
40113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    CHECK(msg->findInt32(WHATPARAM_LOOP_LOOPING, &loop));
40213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
40313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (loop) {
40413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        //SL_LOGV("AudioSfDecoder::onLoop start looping");
40513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mStateFlags |= kFlagLooping;
40613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    } else {
40713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        //SL_LOGV("AudioSfDecoder::onLoop stop looping");
40813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mStateFlags &= ~kFlagLooping;
40913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
410ddaf8fec2c6362785f8f27e59e30bf6bfe858f3bGlenn Kasten
411ddaf8fec2c6362785f8f27e59e30bf6bfe858f3bGlenn Kasten    // nop for now
412ddaf8fec2c6362785f8f27e59e30bf6bfe858f3bGlenn Kasten    GenericPlayer::onLoop(msg);
41313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
41413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
41513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
41613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::onCheckCache(const sp<AMessage> &msg) {
41713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    //SL_LOGV("AudioSfDecoder::onCheckCache");
41813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    bool eos;
4194ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi    CacheStatus_t status = getCacheRemaining(&eos);
42013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
42113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (eos || status == kStatusHigh
42213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            || ((mStateFlags & kFlagPreparing) && (status >= kStatusEnough))) {
42313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (mStateFlags & kFlagPlaying) {
42413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            startAudioSink();
42513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
42613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mStateFlags &= ~kFlagBuffering;
42713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
42813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGV("AudioSfDecoder::onCheckCache: buffering done.");
42913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
43013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (mStateFlags & kFlagPreparing) {
43113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            //SL_LOGV("AudioSfDecoder::onCheckCache: preparation done.");
43213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            mStateFlags &= ~kFlagPreparing;
43313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
43413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
43513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (mStateFlags & kFlagPlaying) {
43613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            (new AMessage(kWhatDecode, id()))->post();
43713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
43813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        return;
43913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
44013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
44113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    msg->post(100000);
44213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
44313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
44413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
44513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::onDecode() {
44613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    SL_LOGV("AudioSfDecoder::onDecode");
44713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
44813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    //-------------------------------- Need to buffer some more before decoding?
44913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    bool eos;
45013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (mDataSource == 0) {
45113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        // application set play state to paused which failed, then set play state to playing
45213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        return;
45313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
454e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi
45513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (wantPrefetch()
45613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            && (getCacheRemaining(&eos) == kStatusLow)
45713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            && !eos) {
45813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGV("buffering more.");
45913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
46013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (mStateFlags & kFlagPlaying) {
46113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            pauseAudioSink();
46213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
46313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mStateFlags |= kFlagBuffering;
46413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        (new AMessage(kWhatCheckCache, id()))->post(100000);
46513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        return;
46613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
46713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
46813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (!(mStateFlags & (kFlagPlaying | kFlagBuffering | kFlagPreparing))) {
46913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        // don't decode if we're not buffering, prefetch