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"
21ff25010cb77455a46357d6dd012631a2599d7bf4Glenn Kasten#include "android/channels.h"
2213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
230c7d40a5f11f6930e6c4b2551a14f41dd721f936Jean-Michel Trivi#include <binder/IServiceManager.h>
24fb8035480852914d326eb4c2074060df32382926Andreas Huber#include <media/IMediaHTTPService.h>
254ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi#include <media/stagefright/foundation/ADebug.h>
26d67d2393c62827a91f84b1eed83a789387cff032Lajos Molnar#include <media/stagefright/MediaBuffer.h>
27040cca5f36511d632c355b5008ebb09d28fd6402Lajos Molnar#include <media/stagefright/SimpleDecodingSource.h>
284ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi
294ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi
304ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi#define SIZE_CACHED_HIGH_BYTES 1000000
314ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi#define SIZE_CACHED_MED_BYTES   700000
324ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi#define SIZE_CACHED_LOW_BYTES   400000
334ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi
3413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivinamespace android {
3513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
3613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi//--------------------------------------------------------------------------------------------------
3713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel TriviAudioSfDecoder::AudioSfDecoder(const AudioPlayback_Parameters* params) : GenericPlayer(params),
38b4fb100d7122d118d3da9d1d08ffacef68dd38b0Jean-Michel Trivi        mDataSource(0),
39b4fb100d7122d118d3da9d1d08ffacef68dd38b0Jean-Michel Trivi        mAudioSource(0),
40e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        mAudioSourceStarted(false),
4113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mBitrate(-1),
425050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi        mDurationUsec(ANDROID_UNKNOWN_TIME),
4313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mDecodeBuffer(NULL),
4413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mSeekTimeMsec(0),
45ddaf8fec2c6362785f8f27e59e30bf6bfe858f3bGlenn Kasten        // play event logic depends on the initial time being zero not ANDROID_UNKNOWN_TIME
46ddaf8fec2c6362785f8f27e59e30bf6bfe858f3bGlenn Kasten        mLastDecodedPositionUs(0)
4713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi{
4854cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    SL_LOGD("AudioSfDecoder::AudioSfDecoder()");
4913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
5013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
5113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
5213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel TriviAudioSfDecoder::~AudioSfDecoder() {
5354cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    SL_LOGD("AudioSfDecoder::~AudioSfDecoder()");
54e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi}
55e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi
56e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi
57e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivivoid AudioSfDecoder::preDestroy() {
58e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    GenericPlayer::preDestroy();
59e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    SL_LOGD("AudioSfDecoder::preDestroy()");
60e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    {
61e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        Mutex::Autolock _l(mBufferSourceLock);
62e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi
63e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        if (NULL != mDecodeBuffer) {
64e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi            mDecodeBuffer->release();
65e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi            mDecodeBuffer = NULL;
66e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        }
67e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi
68e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        if ((mAudioSource != 0) && mAudioSourceStarted) {
69e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi            mAudioSource->stop();
70e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi            mAudioSourceStarted = false;
71e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        }
72b4fb100d7122d118d3da9d1d08ffacef68dd38b0Jean-Michel Trivi    }
7313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
7413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
7513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
7613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi//--------------------------------------------------
7713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::play() {
7854cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    SL_LOGD("AudioSfDecoder::play");
7913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
8013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    GenericPlayer::play();
8152e35b2c9f25450452ab422c46737a7fb5b67380Lajos Molnar    (new AMessage(kWhatDecode, this))->post();
8213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
8313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
8413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
855050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivivoid AudioSfDecoder::getPositionMsec(int* msec) {
865050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi    int64_t timeUsec = getPositionUsec();
875050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi    if (timeUsec == ANDROID_UNKNOWN_TIME) {
885050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi        *msec = ANDROID_UNKNOWN_TIME;
895050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi    } else {
905933f3d5e532aaac31ce0e6551c59f0197c0ae3cGlenn Kasten        *msec = timeUsec / 1000;
915050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi    }
925050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi}
935050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi
945050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi
9513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi//--------------------------------------------------
9691540f92d7f1bcda423859af6bd82df083c2afabGlenn Kastenuint32_t AudioSfDecoder::getPcmFormatKeyCount() const {
9791540f92d7f1bcda423859af6bd82df083c2afabGlenn Kasten    return NB_PCMMETADATA_KEYS;
987f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi}
997f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
1007f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
1017f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi//--------------------------------------------------
1027f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivibool AudioSfDecoder::getPcmFormatKeySize(uint32_t index, uint32_t* pKeySize) {
10391540f92d7f1bcda423859af6bd82df083c2afabGlenn Kasten    if (index >= NB_PCMMETADATA_KEYS) {
1047f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        return false;
1057f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    } else {
1067f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        *pKeySize = strlen(kPcmDecodeMetadataKeys[index]) +1;
1077f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        return true;
1087f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    }
1097f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi}
1107f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
1117f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
1127f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi//--------------------------------------------------
1137f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivibool AudioSfDecoder::getPcmFormatKeyName(uint32_t index, uint32_t keySize, char* keyName) {
1147f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    uint32_t actualKeySize;
1157f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    if (!getPcmFormatKeySize(index, &actualKeySize)) {
1167f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        return false;
1177f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    }
1187f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    if (keySize < actualKeySize) {
1197f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        return false;
1207f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    }
1217f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    strncpy(keyName, kPcmDecodeMetadataKeys[index], actualKeySize);
1227f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    return true;
1237f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi}
1247f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
1257f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
1267f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi//--------------------------------------------------
1277f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivibool AudioSfDecoder::getPcmFormatValueSize(uint32_t index, uint32_t* pValueSize) {
12891540f92d7f1bcda423859af6bd82df083c2afabGlenn Kasten    if (index >= NB_PCMMETADATA_KEYS) {
1297f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        *pValueSize = 0;
1307f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        return false;
1317f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    } else {
1327f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        *pValueSize = sizeof(uint32_t);
1337f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        return true;
1347f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    }
1357f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi}
1367f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
1377f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
1387f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi//--------------------------------------------------
1397f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivibool AudioSfDecoder::getPcmFormatKeyValue(uint32_t index, uint32_t size, uint32_t* pValue) {
1407f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    uint32_t valueSize = 0;
1417f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    if (!getPcmFormatValueSize(index, &valueSize)) {
1427f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        return false;
14313d02b645fc6e8ffe70a8bf8cc5f69f03558ae40Jean-Michel Trivi    } else if (size != valueSize) {
1447f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        // this ensures we are accessing mPcmFormatValues with a valid size for that index
14513d02b645fc6e8ffe70a8bf8cc5f69f03558ae40Jean-Michel Trivi        SL_LOGE("Error retrieving metadata value at index %d: using size of %d, should be %d",
14613d02b645fc6e8ffe70a8bf8cc5f69f03558ae40Jean-Michel Trivi                index, size, valueSize);
1477f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        return false;
1487f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    } else {
14991540f92d7f1bcda423859af6bd82df083c2afabGlenn Kasten        android::Mutex::Autolock autoLock(mPcmFormatLock);
1507f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        *pValue = mPcmFormatValues[index];
1517f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        return true;
1527f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    }
1537f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi}
1547f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
1557f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
1567f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi//--------------------------------------------------
15713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi// Event handlers
158e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi//  it is strictly verboten to call those methods outside of the event loop
159e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi
160e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi// Initializes the data and audio sources, and update the PCM format info
161e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi// post-condition: upon successful initialization based on the player data locator
162e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi//    GenericPlayer::onPrepare() was called
163e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi//    mDataSource != 0
164e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi//    mAudioSource != 0
165e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi//    mAudioSourceStarted == true
166e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten// All error returns from this method are via notifyPrepared(status) followed by "return".
16713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::onPrepare() {
168e9236d046fdb5cac0696c42e03443a2439188146Jean-Michel Trivi    SL_LOGD("AudioSfDecoder::onPrepare()");
169e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    Mutex::Autolock _l(mBufferSourceLock);
17013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
17191540f92d7f1bcda423859af6bd82df083c2afabGlenn Kasten    {
17291540f92d7f1bcda423859af6bd82df083c2afabGlenn Kasten    android::Mutex::Autolock autoLock(mPcmFormatLock);
173e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    // Initialize the PCM format info with the known parameters before the start of the decode
1747f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_BITSPERSAMPLE] = SL_PCMSAMPLEFORMAT_FIXED_16;
1757f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_CONTAINERSIZE] = 16;
1767f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_ENDIANNESS] = SL_BYTEORDER_LITTLEENDIAN;
177e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    //    initialization with the default values: they will be replaced by the actual values
178e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    //      once the decoder has figured them out
179b4393ef4ef3edb785746c37fd7b68950e85283aeGlenn Kasten    mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_NUMCHANNELS] = UNKNOWN_NUMCHANNELS;
180b4393ef4ef3edb785746c37fd7b68950e85283aeGlenn Kasten    mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_SAMPLERATE] = UNKNOWN_SAMPLERATE;
1814e8fe8a60c3aa8085918f15f281e0979682aefdcPaul McLean    mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_CHANNELMASK] = SL_ANDROID_UNKNOWN_CHANNELMASK;
18291540f92d7f1bcda423859af6bd82df083c2afabGlenn Kasten    }
1837f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
184e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    //---------------------------------
185e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    // Instantiate and initialize the data source for the decoder
18613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    sp<DataSource> dataSource;
18713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
18813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    switch (mDataLocatorType) {
18913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
19013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    case kDataLocatorNone:
19113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGE("AudioSfDecoder::onPrepare: no data locator set");
19213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        notifyPrepared(MEDIA_ERROR_BASE);
19313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        return;
19413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
19513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    case kDataLocatorUri:
196fb8035480852914d326eb4c2074060df32382926Andreas Huber        dataSource = DataSource::CreateFromURI(
197fb8035480852914d326eb4c2074060df32382926Andreas Huber                NULL /* XXX httpService */, mDataLocator.uriRef);
19893ac9bd4f7722c50dc9882ff74bade233860a940Jean-Michel Trivi        if (dataSource == NULL) {
19993ac9bd4f7722c50dc9882ff74bade233860a940Jean-Michel Trivi            SL_LOGE("AudioSfDecoder::onPrepare(): Error opening %s", mDataLocator.uriRef);
20013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            notifyPrepared(MEDIA_ERROR_BASE);
20113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            return;
20213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
20313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        break;
20413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
20593ac9bd4f7722c50dc9882ff74bade233860a940Jean-Michel Trivi    case kDataLocatorFd:
20693ac9bd4f7722c50dc9882ff74bade233860a940Jean-Michel Trivi    {
207833251ab9e5e59a6ea5ac325122cf3abdf7cd944Glenn Kasten        // As FileSource unconditionally takes ownership of the fd and closes it, then
208833251ab9e5e59a6ea5ac325122cf3abdf7cd944Glenn Kasten        // we have to make a dup for FileSource if the app wants to keep ownership itself
209833251ab9e5e59a6ea5ac325122cf3abdf7cd944Glenn Kasten        int fd = mDataLocator.fdi.fd;
210833251ab9e5e59a6ea5ac325122cf3abdf7cd944Glenn Kasten        if (mDataLocator.fdi.mCloseAfterUse) {
211833251ab9e5e59a6ea5ac325122cf3abdf7cd944Glenn Kasten            mDataLocator.fdi.mCloseAfterUse = false;
212833251ab9e5e59a6ea5ac325122cf3abdf7cd944Glenn Kasten        } else {
213833251ab9e5e59a6ea5ac325122cf3abdf7cd944Glenn Kasten            fd = ::dup(fd);
214833251ab9e5e59a6ea5ac325122cf3abdf7cd944Glenn Kasten        }
215833251ab9e5e59a6ea5ac325122cf3abdf7cd944Glenn Kasten        dataSource = new FileSource(fd, mDataLocator.fdi.offset, mDataLocator.fdi.length);
21613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        status_t err = dataSource->initCheck();
21713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (err != OK) {
21813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            notifyPrepared(err);
21913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            return;
22013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
22193ac9bd4f7722c50dc9882ff74bade233860a940Jean-Michel Trivi        break;
22213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
22313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
224e2e8fa36bd7448b59fbcdf141e0b6d21e5401d91Glenn Kasten    // AndroidBufferQueue data source is handled by a subclass,
225e2e8fa36bd7448b59fbcdf141e0b6d21e5401d91Glenn Kasten    // which does not call up to this method.  Hence, the missing case.
22613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    default:
22713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        TRESPASS();
22813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
22913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
230e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    //---------------------------------
231e2e8fa36bd7448b59fbcdf141e0b6d21e5401d91Glenn Kasten    // Instantiate and initialize the decoder attached to the data source
2322cf0a2ee3672659f922bf0df4368b5a49148c5ffMarco Nelissen    sp<IMediaExtractor> extractor = MediaExtractor::Create(dataSource);
23313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (extractor == NULL) {
23413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGE("AudioSfDecoder::onPrepare: Could not instantiate extractor.");
23513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        notifyPrepared(ERROR_UNSUPPORTED);
23613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        return;
23713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
23813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
23913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    ssize_t audioTrackIndex = -1;
24013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    bool isRawAudio = false;
24113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    for (size_t i = 0; i < extractor->countTracks(); ++i) {
24213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        sp<MetaData> meta = extractor->getTrackMetaData(i);
24313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
24413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        const char *mime;
24513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        CHECK(meta->findCString(kKeyMIMEType, &mime));
24613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
24713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (!strncasecmp("audio/", mime, 6)) {
2483597268c2bf4ff71521e3cbe522d7ee02c41f175Jean-Michel Trivi            if (isSupportedCodec(mime)) {
2493597268c2bf4ff71521e3cbe522d7ee02c41f175Jean-Michel Trivi                audioTrackIndex = i;
25013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
2513597268c2bf4ff71521e3cbe522d7ee02c41f175Jean-Michel Trivi                if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_RAW, mime)) {
2523597268c2bf4ff71521e3cbe522d7ee02c41f175Jean-Michel Trivi                    isRawAudio = true;
2533597268c2bf4ff71521e3cbe522d7ee02c41f175Jean-Michel Trivi                }
2543597268c2bf4ff71521e3cbe522d7ee02c41f175Jean-Michel Trivi                break;
25513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            }
25613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
25713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
25813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
25913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (audioTrackIndex < 0) {
26013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGE("AudioSfDecoder::onPrepare: Could not find a supported audio track.");
26113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        notifyPrepared(ERROR_UNSUPPORTED);
26213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        return;
26313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
26413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
2652cf0a2ee3672659f922bf0df4368b5a49148c5ffMarco Nelissen    sp<IMediaSource> source = extractor->getTrack(audioTrackIndex);
26613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    sp<MetaData> meta = source->getFormat();
26713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
26854cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    // we can't trust the OMXCodec (if there is one) to issue a INFO_FORMAT_CHANGED so we want
26954cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    // to have some meaningful values as soon as possible.
270b4393ef4ef3edb785746c37fd7b68950e85283aeGlenn Kasten    int32_t channelCount;
271b4393ef4ef3edb785746c37fd7b68950e85283aeGlenn Kasten    bool hasChannelCount = meta->findInt32(kKeyChannelCount, &channelCount);
27254cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    int32_t sr;
27354cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    bool hasSampleRate = meta->findInt32(kKeySampleRate, &sr);
2747f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
2751fa5c3206d06bbebdea2dc92f378ce6b8a211e23Glenn Kasten    // first compute the duration
27613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    off64_t size;
27713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    int64_t durationUs;
2781fa5c3206d06bbebdea2dc92f378ce6b8a211e23Glenn Kasten    int32_t durationMsec;
27913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (dataSource->getSize(&size) == OK
28013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            && meta->findInt64(kKeyDuration, &durationUs)) {
2815050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi        if (durationUs != 0) {
2825050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi            mBitrate = size * 8000000ll / durationUs;  // in bits/sec
2835050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi        } else {
2845050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi            mBitrate = -1;
2855050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi        }
28613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mDurationUsec = durationUs;
2871fa5c3206d06bbebdea2dc92f378ce6b8a211e23Glenn Kasten        durationMsec = durationUs / 1000;
28813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    } else {
28913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mBitrate = -1;
2905050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi        mDurationUsec = ANDROID_UNKNOWN_TIME;
2911fa5c3206d06bbebdea2dc92f378ce6b8a211e23Glenn Kasten        durationMsec = ANDROID_UNKNOWN_TIME;
2921fa5c3206d06bbebdea2dc92f378ce6b8a211e23Glenn Kasten    }
2931fa5c3206d06bbebdea2dc92f378ce6b8a211e23Glenn Kasten
2941fa5c3206d06bbebdea2dc92f378ce6b8a211e23Glenn Kasten    // then assign the duration under the settings lock
2951fa5c3206d06bbebdea2dc92f378ce6b8a211e23Glenn Kasten    {
2961fa5c3206d06bbebdea2dc92f378ce6b8a211e23Glenn Kasten        Mutex::Autolock _l(mSettingsLock);
2971fa5c3206d06bbebdea2dc92f378ce6b8a211e23Glenn Kasten        mDurationMsec = durationMsec;
29813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
29913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
300e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    // the audio content is not raw PCM, so we need a decoder
30113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (!isRawAudio) {
302040cca5f36511d632c355b5008ebb09d28fd6402Lajos Molnar        source = SimpleDecodingSource::Create(source);
30313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (source == NULL) {
30413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            SL_LOGE("AudioSfDecoder::onPrepare: Could not instantiate decoder.");
30513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            notifyPrepared(ERROR_UNSUPPORTED);
30613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            return;
30713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
30813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
30913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        meta = source->getFormat();
31013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
31113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
31213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
31313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (source->start() != OK) {
31413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGE("AudioSfDecoder::onPrepare: Failed to start source/decoder.");
31513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        notifyPrepared(MEDIA_ERROR_BASE);
31613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        return;
31713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
31813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
319e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    //---------------------------------
320e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    // The data source, and audio source (a decoder if required) are ready to be used
32113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    mDataSource = dataSource;
32213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    mAudioSource = source;
323e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    mAudioSourceStarted = true;
32413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
3257f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    if (!hasChannelCount) {
326b4393ef4ef3edb785746c37fd7b68950e85283aeGlenn Kasten        CHECK(meta->findInt32(kKeyChannelCount, &channelCount));
3277f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    }
32854cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi
32954cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    if (!hasSampleRate) {
33054cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi        CHECK(meta->findInt32(kKeySampleRate, &sr));
33154cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    }
3327f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    // FIXME add code below once channel mask support is in, currently initialized to default
33320d9a1229c7647dd2c6f1bece715080ec6202ecaGlenn Kasten    //       value computed from the channel count
33420d9a1229c7647dd2c6f1bece715080ec6202ecaGlenn Kasten    //    if (!hasChannelMask) {
335b4393ef4ef3edb785746c37fd7b68950e85283aeGlenn Kasten    //        CHECK(meta->findInt32(kKeyChannelMask, &channelMask));
3367f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    //    }
33713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
33813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (!wantPrefetch()) {
33913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGV("AudioSfDecoder::onPrepare: no need to prefetch");
34013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        // doesn't need prefetching, notify good to go
34113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mCacheStatus = kStatusHigh;
34213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mCacheFill = 1000;
34313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        notifyStatus();
34413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        notifyCacheFill();
34513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
34613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
34754cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    {
34854cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi        android::Mutex::Autolock autoLock(mPcmFormatLock);
349b4393ef4ef3edb785746c37fd7b68950e85283aeGlenn Kasten        mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_SAMPLERATE] = sr;
350b4393ef4ef3edb785746c37fd7b68950e85283aeGlenn Kasten        mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_NUMCHANNELS] = channelCount;
351b4393ef4ef3edb785746c37fd7b68950e85283aeGlenn Kasten        mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_CHANNELMASK] =
3524e8fe8a60c3aa8085918f15f281e0979682aefdcPaul McLean                sles_channel_out_mask_from_count(channelCount);
35354cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    }
35454cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi
35513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    // at this point we have enough information about the source to create the sink that
35613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    // will consume the data
35713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    createAudioSink();
35813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
359e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten    // signal successful completion of prepare
360e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten    mStateFlags |= kFlagPrepared;
361bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
36213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    GenericPlayer::onPrepare();
363e9236d046fdb5cac0696c42e03443a2439188146Jean-Michel Trivi    SL_LOGD("AudioSfDecoder::onPrepare() done, mStateFlags=0x%x", mStateFlags);
36413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
36513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
36613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
36713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::onPause() {
36854cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    SL_LOGV("AudioSfDecoder::onPause()");
36913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    GenericPlayer::onPause();
37013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    pauseAudioSink();
37113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
37213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
37313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
37413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::onPlay() {
37554cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    SL_LOGV("AudioSfDecoder::onPlay()");
37613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    GenericPlayer::onPlay();
37713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    startAudioSink();
37813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
37913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
38013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
38113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::onSeek(const sp<AMessage> &msg) {
38213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    SL_LOGV("AudioSfDecoder::onSeek");
38313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    int64_t timeMsec;
38413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    CHECK(msg->findInt64(WHATPARAM_SEEK_SEEKTIME_MS, &timeMsec));
38513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
3865050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi    Mutex::Autolock _l(mTimeLock);
38713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    mStateFlags |= kFlagSeeking;
38813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    mSeekTimeMsec = timeMsec;
389ddaf8fec2c6362785f8f27e59e30bf6bfe858f3bGlenn Kasten    // don't set mLastDecodedPositionUs to ANDROID_UNKNOWN_TIME; getPositionUsec
390ddaf8fec2c6362785f8f27e59e30bf6bfe858f3bGlenn Kasten    // ignores mLastDecodedPositionUs while seeking, and substitutes the seek goal instead
391ddaf8fec2c6362785f8f27e59e30bf6bfe858f3bGlenn Kasten
392ddaf8fec2c6362785f8f27e59e30bf6bfe858f3bGlenn Kasten    // nop for now
393ddaf8fec2c6362785f8f27e59e30bf6bfe858f3bGlenn Kasten    GenericPlayer::onSeek(msg);
39413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
39513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
39613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
39713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::onLoop(const sp<AMessage> &msg) {
39813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    SL_LOGV("AudioSfDecoder::onLoop");
39913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    int32_t loop;
40013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    CHECK(msg->findInt32(WHATPARAM_LOOP_LOOPING, &loop));
40113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
40213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (loop) {
40313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        //SL_LOGV("AudioSfDecoder::onLoop start looping");
40413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mStateFlags |= kFlagLooping;
40513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    } else {
40613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        //SL_LOGV("AudioSfDecoder::onLoop stop looping");
40713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mStateFlags &= ~kFlagLooping;
40813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
409ddaf8fec2c6362785f8f27e59e30bf6bfe858f3bGlenn Kasten
410ddaf8fec2c6362785f8f27e59e30bf6bfe858f3bGlenn Kasten    // nop for now
411ddaf8fec2c6362785f8f27e59e30bf6bfe858f3bGlenn Kasten    GenericPlayer::onLoop(msg);
41213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
41313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
41413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
41513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::onCheckCache(const sp<AMessage> &msg) {
41613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    //SL_LOGV("AudioSfDecoder::onCheckCache");
41713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    bool eos;
4184ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi    CacheStatus_t status = getCacheRemaining(&eos);
41913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
42013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (eos || status == kStatusHigh
42113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            || ((mStateFlags & kFlagPreparing) && (status >= kStatusEnough))) {
42213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (mStateFlags & kFlagPlaying) {
42313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            startAudioSink();
42413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
42513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mStateFlags &= ~kFlagBuffering;
42613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
42713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGV("AudioSfDecoder::onCheckCache: buffering done.");
42813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
42913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (mStateFlags & kFlagPreparing) {
43013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            //SL_LOGV("AudioSfDecoder::onCheckCache: preparation done.");
43113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            mStateFlags &= ~kFlagPreparing;
43213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
43313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
43413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (mStateFlags & kFlagPlaying) {
43552e35b2c9f25450452ab422c46737a7fb5b67380Lajos Molnar            (new AMessage(kWhatDecode, this))->post();
43613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
43713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        return;
43813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
43913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
44013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    msg->post(100000);
44113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
44213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
44313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
44413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::onDecode() {
44513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    SL_LOGV("AudioSfDecoder::onDecode");
44613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
44713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    //-------------------------------- Need to buffer some more before decoding?
44813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    bool eos;
44913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (mDataSource == 0) {
45013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        // application set play state to paused which failed, then set play state to playing
45113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        return;
45213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
453e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi
45413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (wantPrefetch()
45513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            && (getCacheRemaining(&eos) == kStatusLow)
45613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            && !eos) {
45713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGV("buffering more.");
45813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
45913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (mStateFlags & kFlagPlaying) {
46013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            pauseAudioSink();
46113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
46213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mStateFlags |= kFlagBuffering;
46352e35b2c9f25450452ab422c46737a7fb5b67380Lajos Molnar        (new AMessage(kWhatCheckCache, this))->post(100000);
46413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        return;
46513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
46613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
46713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (!(mStateFlags & (kFlagPlaying | kFlagBuffering | kFlagPreparing))) {
46813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        // don't decode if we're not buffering, prefetching or playing
46913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        //SL_LOGV("don't decode: not buffering, prefetching or playing");
47013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        return;
47113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
47213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
47313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    //-------------------------------- Decode
47413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    status_t err;
47513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    MediaSource::ReadOptions readOptions;
47613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (mStateFlags & kFlagSeeking) {
4775050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi        assert(mSeekTimeMsec != ANDROID_UNKNOWN_TIME);
47813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        readOptions.setSeekTo(mSeekTimeMsec * 1000);
47913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
48013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
4815050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi    int64_t timeUsec = ANDROID_UNKNOWN_TIME;
48213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    {
483e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        Mutex::Autolock _l(mBufferSourceLock);
484e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi
48513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (NULL != mDecodeBuffer) {
48613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            // the current decoded buffer hasn't been rendered, drop it
48713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            mDecodeBuffer->release();
48813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            mDecodeBuffer = NULL;
48913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
490ca426f63e9c900ecbd28f8e3037aaf47ef739dd4Glenn Kasten        if (!mAudioSourceStarted) {
491e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi            return;
492e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        }
49313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        err = mAudioSource->read(&mDecodeBuffer, &readOptions);
49413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (err == OK) {
495209c05d9104db8b77ef0846ee8eb3b161bf44031Glenn Kasten            // FIXME workaround apparent bug in AAC decoder: kKeyTime is 3 frames old if length is 0
496209c05d9104db8b77ef0846ee8eb3b161bf44031Glenn Kasten            if (mDecodeBuffer->range_length() == 0) {
497209c05d9104db8b77ef0846ee8eb3b161bf44031Glenn Kasten                timeUsec = ANDROID_UNKNOWN_TIME;
498209c05d9104db8b77ef0846ee8eb3b161bf44031Glenn Kasten            } else {
499209c05d9104db8b77ef0846ee8eb3b161bf44031Glenn Kasten                CHECK(mDecodeBuffer->meta_data()->findInt64(kKeyTime, &timeUsec));
500209c05d9104db8b77ef0846ee8eb3b161bf44031Glenn Kasten            }
501e2e8fa36bd7448b59fbcdf141e0b6d21e5401d91Glenn Kasten        } else {
502e2e8fa36bd7448b59fbcdf141e0b6d21e5401d91Glenn Kasten            // errors are handled below
50313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
50413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
50513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
50613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    {
5075050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi        Mutex::Autolock _l(mTimeLock);
50813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (mStateFlags & kFlagSeeking) {
50913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            mStateFlags &= ~kFlagSeeking;
5105050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi            mSeekTimeMsec = ANDROID_UNKNOWN_TIME;
5115050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi        }
5125050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi        if (timeUsec != ANDROID_UNKNOWN_TIME) {
5137349b2e742b2cedc6d149fac62ed661ad7d47decGlenn Kasten            // Note that though we've decoded this position, we haven't rendered it yet.
5147349b2e742b2cedc6d149fac62ed661ad7d47decGlenn Kasten            // So a GetPosition called after this point will observe the advanced position,
5157349b2e742b2cedc6d149fac62ed661ad7d47decGlenn Kasten            // even though the PCM may not have been supplied to the sink.  That's OK as
516e2e8fa36bd7448b59fbcdf141e0b6d21e5401d91Glenn Kasten            // we don't claim to provide AAC frame-accurate (let alone sample-accurate) GetPosition.
5175050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi            mLastDecodedPositionUs = timeUsec;
51813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
51913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
52013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
52113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    //-------------------------------- Handle return of decode
52213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (err != OK) {
52313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        bool continueDecoding = false;
524ac28eca1df49f581d952ffbda5d3019f7e3b7be6Glenn Kasten        switch (err) {
52513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            case ERROR_END_OF_STREAM:
52613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                if (0 < mDurationUsec) {
5275050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi                    Mutex::Autolock _l(mTimeLock);
52813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                    mLastDecodedPositionUs = mDurationUsec;
52913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                }
53013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                // handle notification and looping at end of stream
53113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                if (mStateFlags & kFlagPlaying) {
532e2e8fa36bd7448b59fbcdf141e0b6d21e5401d91Glenn Kasten                    notify(PLAYEREVENT_ENDOFSTREAM, 1, true /*async*/);
53313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                }
53413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                if (mStateFlags & kFlagLooping) {
53513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                    seek(0);
53613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                    // kick-off decoding again
53713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                    continueDecoding = true;
53813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                }
53913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                break;
54013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            case INFO_FORMAT_CHANGED:
541e9236d046fdb5cac0696c42e03443a2439188146Jean-Michel Trivi                SL_LOGD("MediaSource::read encountered INFO_FORMAT_CHANGED");
54213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                // reconfigure output
54354cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi                {
54454cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi                    Mutex::Autolock _l(mBufferSourceLock);
54554cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi                    hasNewDecodeParams();
54654cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi                }
54713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                continueDecoding = true;
54813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                break;
54913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            case INFO_DISCONTINUITY:
550e9236d046fdb5cac0696c42e03443a2439188146Jean-Michel Trivi                SL_LOGD("MediaSource::read encountered INFO_DISCONTINUITY");
55113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                continueDecoding = true;
55213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                break;
55313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            default:
55413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                SL_LOGE("MediaSource::read returned error %d", err);
55513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                break;
55613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
55713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (continueDecoding) {
55813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            if (NULL == mDecodeBuffer) {
55952e35b2c9f25450452ab422c46737a7fb5b67380Lajos Molnar                (new AMessage(kWhatDecode, this))->post();
56013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                return;
56113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            }
56213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        } else {
56313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            return;
56413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
56513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
56613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
56713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    //-------------------------------- Render
56852e35b2c9f25450452ab422c46737a7fb5b67380Lajos Molnar    sp<AMessage> msg = new AMessage(kWhatRender, this);
56913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    msg->post();
57013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
57113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
57213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
57313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
57413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::onMessageReceived(const sp<AMessage> &msg) {
57513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    switch (msg->what()) {
57613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        case kWhatDecode:
57713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            onDecode();
57813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            break;
57913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
58013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        case kWhatRender:
58113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            onRender();
58213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            break;
58313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
58413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        case kWhatCheckCache:
58513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            onCheckCache(msg);
58613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            break;
58713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
58813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        default:
58913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            GenericPlayer::onMessageReceived(msg);
59013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            break;
59113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
59213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
59313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
59413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi//--------------------------------------------------
59513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi// Prepared state, prefetch status notifications
59613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::notifyPrepared(status_t prepareRes) {
597e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten    assert(!(mStateFlags & (kFlagPrepared | kFlagPreparedUnsuccessfully)));
598e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten    if (NO_ERROR == prepareRes) {
599e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten        // The "then" fork is not currently used, but is kept here to make it easier
600e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten        // to replace by a new signalPrepareCompletion(status) if we re-visit this later.
601e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten        mStateFlags |= kFlagPrepared;
602e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten    } else {
603e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten        mStateFlags |= kFlagPreparedUnsuccessfully;
604e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten    }
605e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten    // Do not call the superclass onPrepare to notify, because it uses a default error
606e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten    // status code but we can provide a more specific one.
607e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten    // GenericPlayer::onPrepare();
608e2e8fa36bd7448b59fbcdf141e0b6d21e5401d91Glenn Kasten    notify(PLAYEREVENT_PREPARED, (int32_t)prepareRes, true /*async*/);
609e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten    SL_LOGD("AudioSfDecoder::onPrepare() done, mStateFlags=0x%x", mStateFlags);
61013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
61113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
61213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
61313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::onNotify(const sp<AMessage> &msg) {
614e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    notif_cbf_t notifyClient;
615e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    void*       notifyUser;
616e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    {
617e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        android::Mutex::Autolock autoLock(mNotifyClientLock);
618e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        if (NULL == mNotifyClient) {
619e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi            return;
620e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        } else {
621e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi            notifyClient = mNotifyClient;
622e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi            notifyUser   = mNotifyUser;
623e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        }
62413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
62513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    int32_t val;
62613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (msg->findInt32(PLAYEREVENT_PREFETCHSTATUSCHANGE, &val)) {
62713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGV("\tASfPlayer notifying %s = %d", PLAYEREVENT_PREFETCHSTATUSCHANGE, val);
628e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        notifyClient(kEventPrefetchStatusChange, val, 0, notifyUser);
62913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
63013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    else if (msg->findInt32(PLAYEREVENT_PREFETCHFILLLEVELUPDATE, &val)) {
63113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGV("\tASfPlayer notifying %s = %d", PLAYEREVENT_PREFETCHFILLLEVELUPDATE, val);
632e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        notifyClient(kEventPrefetchFillLevelUpdate, val, 0, notifyUser);
63313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
63413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    else if (msg->findInt32(PLAYEREVENT_ENDOFSTREAM, &val)) {
63513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGV("\tASfPlayer notifying %s = %d", PLAYEREVENT_ENDOFSTREAM, val);
636e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        notifyClient(kEventEndOfStream, val, 0, notifyUser);
63713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
63813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    else {
63913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        GenericPlayer::onNotify(msg);
64013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
64113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
64213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
64313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
64413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi//--------------------------------------------------
64513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi// Private utility functions
64613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
64713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivibool AudioSfDecoder::wantPrefetch() {
648ac18c1cd32408884d3960bd7aa56ba419c2ca68bJean-Michel Trivi    if (mDataSource != 0) {
649ac18c1cd32408884d3960bd7aa56ba419c2ca68bJean-Michel Trivi        return (mDataSource->flags() & DataSource::kWantsPrefetching);
650ac18c1cd32408884d3960bd7aa56ba419c2ca68bJean-Michel Trivi    } else {
651ac18c1cd32408884d3960bd7aa56ba419c2ca68bJean-Michel Trivi        // happens if an improper data locator was passed, if the media extractor couldn't be
652ac18c1cd32408884d3960bd7aa56ba419c2ca68bJean-Michel Trivi        //  initialized, if there is no audio track in the media, if the OMX decoder couldn't be
653ac18c1cd32408884d3960bd7aa56ba419c2ca68bJean-Michel Trivi        //  instantiated, if the source couldn't be opened, or if the MediaSource
654ac18c1cd32408884d3960bd7aa56ba419c2ca68bJean-Michel Trivi        //  couldn't be started
655ac18c1cd32408884d3960bd7aa56ba419c2ca68bJean-Michel Trivi        SL_LOGV("AudioSfDecoder::wantPrefetch() tries to access NULL mDataSource");
656ac18c1cd32408884d3960bd7aa56ba419c2ca68bJean-Michel Trivi        return false;
657ac18c1cd32408884d3960bd7aa56ba419c2ca68bJean-Michel Trivi    }
65813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
65913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
66013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
66113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Triviint64_t AudioSfDecoder::getPositionUsec() {
6625050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi    Mutex::Autolock _l(mTimeLock);
66313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (mStateFlags & kFlagSeeking) {
66413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        return mSeekTimeMsec * 1000;
66513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    } else {
666ddaf8fec2c6362785f8f27e59e30bf6bfe858f3bGlenn Kasten        return mLastDecodedPositionUs;
66713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
66813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
66913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
67013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
6714ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel TriviCacheStatus_t AudioSfDecoder::getCacheRemaining(bool *eos) {
67213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    sp<NuCachedSource2> cachedSource =
67313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        static_cast<NuCachedSource2 *>(mDataSource.get());
67413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
6754ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi    CacheStatus_t oldStatus = mCacheStatus;
67613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
67713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    status_t finalStatus;
67813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    size_t dataRemaining = cachedSource->approxDataRemaining(&finalStatus);
67913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    *eos = (finalStatus != OK);
68013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
68113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    CHECK_GE(mBitrate, 0);
68213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
68313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    int64_t dataRemainingUs = dataRemaining * 8000000ll / mBitrate;
68413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    //SL_LOGV("AudioSfDecoder::getCacheRemaining: approx %.2f secs remaining (eos=%d)",
68513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    //       dataRemainingUs / 1E6, *eos);
68613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
68713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (*eos) {
68813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        // data is buffered up to the end of the stream, it can't get any better than this
68913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mCacheStatus = kStatusHigh;
69013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mCacheFill = 1000;
69113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
69213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    } else {
69313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (mDurationUsec > 0) {
69413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            // known duration:
69513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
69613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            //   fill level is ratio of how much has been played + how much is
69713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            //   cached, divided by total duration
698a93915739311e228a59d93ecd93a5665299953abGlenn Kasten            int64_t currentPositionUsec = getPositionUsec();
6995050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi            if (currentPositionUsec == ANDROID_UNKNOWN_TIME) {
7005050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi                // if we don't know where we are, assume the worst for the fill ratio
7015050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi                currentPositionUsec = 0;
7025050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi            }
7035050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi            if (mDurationUsec > 0) {
7045050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi                mCacheFill = (int16_t) ((1000.0
7055050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi                        * (double)(currentPositionUsec + dataRemainingUs) / mDurationUsec));
7065050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi            } else {
7075050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi                mCacheFill = 0;
7085050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi            }
70913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            //SL_LOGV("cacheFill = %d", mCacheFill);
71013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
71113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            //   cache status is evaluated against duration thresholds
7124ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi            if (dataRemainingUs > DURATION_CACHED_HIGH_MS*1000) {
71313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                mCacheStatus = kStatusHigh;
714de7c7da8460de9fb1e8739978f25e1463e2e1666Steve Block                //ALOGV("high");
7154ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi            } else if (dataRemainingUs > DURATION_CACHED_MED_MS*1000) {
716de7c7da8460de9fb1e8739978f25e1463e2e1666Steve Block                //ALOGV("enough");
71713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                mCacheStatus = kStatusEnough;
7184ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi            } else if (dataRemainingUs < DURATION_CACHED_LOW_MS*1000) {
719de7c7da8460de9fb1e8739978f25e1463e2e1666Steve Block                //ALOGV("low");
72013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                mCacheStatus = kStatusLow;
72113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            } else {
72213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                mCacheStatus = kStatusIntermediate;
72313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            }
72413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
72513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        } else {
72613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            // unknown duration:
72713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
72813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            //   cache status is evaluated against cache amount thresholds
72913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            //   (no duration so we don't have the bitrate either, could be derived from format?)
73013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            if (dataRemaining > SIZE_CACHED_HIGH_BYTES) {
73113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                mCacheStatus = kStatusHigh;
73213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            } else if (dataRemaining > SIZE_CACHED_MED_BYTES) {
73313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                mCacheStatus = kStatusEnough;
73413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            } else if (dataRemaining < SIZE_CACHED_LOW_BYTES) {
73513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                mCacheStatus = kStatusLow;
73613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            } else {
73713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                mCacheStatus = kStatusIntermediate;
73813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            }
73913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
74013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
74113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
74213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
74313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (oldStatus != mCacheStatus) {
74413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        notifyStatus();
74513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
74613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
74713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (abs(mCacheFill - mLastNotifiedCacheFill) > mCacheFillNotifThreshold) {
74813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        notifyCacheFill();
74913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
75013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
75113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    return mCacheStatus;
75213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
75313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
75454cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi
75554cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivivoid AudioSfDecoder::hasNewDecodeParams() {
75654cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi
75754cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    if ((mAudioSource != 0) && mAudioSourceStarted) {
75854cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi        sp<MetaData> meta = mAudioSource->getFormat();
75954cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi
760b4393ef4ef3edb785746c37fd7b68950e85283aeGlenn Kasten        int32_t channelCount;
761b4393ef4ef3edb785746c37fd7b68950e85283aeGlenn Kasten        CHECK(meta->findInt32(kKeyChannelCount, &channelCount));
76254cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi        int32_t sr;
76354cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi        CHECK(meta->findInt32(kKeySampleRate, &sr));
76454cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi
76520d9a1229c7647dd2c6f1bece715080ec6202ecaGlenn Kasten        // FIXME similar to onPrepare()
76654cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi        {
76754cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi            android::Mutex::Autolock autoLock(mPcmFormatLock);
768b4393ef4ef3edb785746c37fd7b68950e85283aeGlenn Kasten            SL_LOGV("format changed: old sr=%d, channels=%d; new sr=%d, channels=%d",
769b4393ef4ef3edb785746c37fd7b68950e85283aeGlenn Kasten                    mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_SAMPLERATE],
770b4393ef4ef3edb785746c37fd7b68950e85283aeGlenn Kasten                    mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_NUMCHANNELS],
771b4393ef4ef3edb785746c37fd7b68950e85283aeGlenn Kasten                    sr, channelCount);
772b4393ef4ef3edb785746c37fd7b68950e85283aeGlenn Kasten            mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_NUMCHANNELS] = channelCount;
773b4393ef4ef3edb785746c37fd7b68950e85283aeGlenn Kasten            mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_SAMPLERATE] = sr;
774b4393ef4ef3edb785746c37fd7b68950e85283aeGlenn Kasten            mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_CHANNELMASK] =
7754e8fe8a60c3aa8085918f15f281e0979682aefdcPaul McLean                    sles_channel_out_mask_from_count(channelCount);
77654cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi        }
777e2e8fa36bd7448b59fbcdf141e0b6d21e5401d91Glenn Kasten        // there's no need to do a notify of PLAYEREVENT_CHANNEL_COUNT,
778e2e8fa36bd7448b59fbcdf141e0b6d21e5401d91Glenn Kasten        // because the only listener is for volume updates, and decoders don't support that
77954cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    }
78054cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi
78154cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    // alert users of those params
78254cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    updateAudioSink();
78354cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi}
78454cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi
7850c7d40a5f11f6930e6c4b2551a14f41dd721f936Jean-Michel Trivistatic const char* const kPlaybackOnlyCodecs[] = { MEDIA_MIMETYPE_AUDIO_AMR_NB,
7863597268c2bf4ff71521e3cbe522d7ee02c41f175Jean-Michel Trivi        MEDIA_MIMETYPE_AUDIO_AMR_WB };
7870c7d40a5f11f6930e6c4b2551a14f41dd721f936Jean-Michel Trivi#define NB_PLAYBACK_ONLY_CODECS (sizeof(kPlaybackOnlyCodecs)/sizeof(kPlaybackOnlyCodecs[0]))
7883597268c2bf4ff71521e3cbe522d7ee02c41f175Jean-Michel Trivi
7893597268c2bf4ff71521e3cbe522d7ee02c41f175Jean-Michel Trivibool AudioSfDecoder::isSupportedCodec(const char* mime) {
7900c7d40a5f11f6930e6c4b2551a14f41dd721f936Jean-Michel Trivi    bool codecRequiresPermission = false;
7910c7d40a5f11f6930e6c4b2551a14f41dd721f936Jean-Michel Trivi    for (unsigned int i = 0 ; i < NB_PLAYBACK_ONLY_CODECS ; i++) {
7920c7d40a5f11f6930e6c4b2551a14f41dd721f936Jean-Michel Trivi        if (!strcasecmp(mime, kPlaybackOnlyCodecs[i])) {
7930c7d40a5f11f6930e6c4b2551a14f41dd721f936Jean-Michel Trivi            codecRequiresPermission = true;
7940c7d40a5f11f6930e6c4b2551a14f41dd721f936Jean-Michel Trivi            break;
7953597268c2bf4ff71521e3cbe522d7ee02c41f175Jean-Michel Trivi        }
7963597268c2bf4ff71521e3cbe522d7ee02c41f175Jean-Michel Trivi    }
7970c7d40a5f11f6930e6c4b2551a14f41dd721f936Jean-Michel Trivi    if (codecRequiresPermission) {
7980c7d40a5f11f6930e6c4b2551a14f41dd721f936Jean-Michel Trivi        // verify only the system can decode, for playback only
7990c7d40a5f11f6930e6c4b2551a14f41dd721f936Jean-Michel Trivi        return checkCallingPermission(
8000c7d40a5f11f6930e6c4b2551a14f41dd721f936Jean-Michel Trivi                String16("android.permission.ALLOW_ANY_CODEC_FOR_PLAYBACK"));
8010c7d40a5f11f6930e6c4b2551a14f41dd721f936Jean-Michel Trivi    } else {
8020c7d40a5f11f6930e6c4b2551a14f41dd721f936Jean-Michel Trivi        return true;
8030c7d40a5f11f6930e6c4b2551a14f41dd721f936Jean-Michel Trivi    }
8043597268c2bf4ff71521e3cbe522d7ee02c41f175Jean-Michel Trivi}
8053597268c2bf4ff71521e3cbe522d7ee02c41f175Jean-Michel Trivi
80613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi} // namespace android
807