android_AudioSfDecoder.cpp revision 5933f3d5e532aaac31ce0e6551c59f0197c0ae3c
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
1754cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi//#define USE_LOG SLAndroidLogLevel_Debug
1813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
1913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi#include "sles_allinclusive.h"
202b06e20ae32388f6e1dfd088d9773c34e6b1cb45Jean-Michel Trivi#include "android/android_AudioSfDecoder.h"
2113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
224ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi#include <media/stagefright/foundation/ADebug.h>
234ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi
244ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi
254ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi#define SIZE_CACHED_HIGH_BYTES 1000000
264ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi#define SIZE_CACHED_MED_BYTES   700000
274ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi#define SIZE_CACHED_LOW_BYTES   400000
284ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi
2913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivinamespace android {
3013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
317f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi// keep in sync with the entries of kPcmDecodeMetadataKeys[] defined in android_AudioSfDecoder.h
327f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi#define ANDROID_KEY_INDEX_PCMFORMAT_NUMCHANNELS   0
337f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi#define ANDROID_KEY_INDEX_PCMFORMAT_SAMPLESPERSEC 1
347f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi#define ANDROID_KEY_INDEX_PCMFORMAT_BITSPERSAMPLE 2
357f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi#define ANDROID_KEY_INDEX_PCMFORMAT_CONTAINERSIZE 3
367f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi#define ANDROID_KEY_INDEX_PCMFORMAT_CHANNELMASK   4
377f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi#define ANDROID_KEY_INDEX_PCMFORMAT_ENDIANNESS    5
387f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
3913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi//--------------------------------------------------------------------------------------------------
4013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel TriviAudioSfDecoder::AudioSfDecoder(const AudioPlayback_Parameters* params) : GenericPlayer(params),
41b4fb100d7122d118d3da9d1d08ffacef68dd38b0Jean-Michel Trivi        mDataSource(0),
42b4fb100d7122d118d3da9d1d08ffacef68dd38b0Jean-Michel Trivi        mAudioSource(0),
43e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        mAudioSourceStarted(false),
4413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mBitrate(-1),
451a9c2615d0933d183fcb1b9e34ec8f0da2a85153Glenn Kasten        mChannelMask(UNKNOWN_CHANNELMASK),
465050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi        mDurationUsec(ANDROID_UNKNOWN_TIME),
4713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mDecodeBuffer(NULL),
4813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mSeekTimeMsec(0),
495050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi        mLastDecodedPositionUs(ANDROID_UNKNOWN_TIME),
5054cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi        mPcmFormatKeyCount(0)
5113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi{
5254cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    SL_LOGD("AudioSfDecoder::AudioSfDecoder()");
5313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
5413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
5513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
5613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel TriviAudioSfDecoder::~AudioSfDecoder() {
5754cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    SL_LOGD("AudioSfDecoder::~AudioSfDecoder()");
58e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi}
59e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi
60e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi
61e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivivoid AudioSfDecoder::preDestroy() {
62e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    GenericPlayer::preDestroy();
63e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    SL_LOGD("AudioSfDecoder::preDestroy()");
64e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    {
65e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        Mutex::Autolock _l(mBufferSourceLock);
66e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi
67e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        if (NULL != mDecodeBuffer) {
68e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi            mDecodeBuffer->release();
69e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi            mDecodeBuffer = NULL;
70e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        }
71e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi
72e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        if ((mAudioSource != 0) && mAudioSourceStarted) {
73e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi            mAudioSource->stop();
74e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi            mAudioSourceStarted = false;
75e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        }
76b4fb100d7122d118d3da9d1d08ffacef68dd38b0Jean-Michel Trivi    }
7713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
7813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
7913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
8013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi//--------------------------------------------------
8113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::play() {
8254cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    SL_LOGD("AudioSfDecoder::play");
8313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
8413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    GenericPlayer::play();
8513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    (new AMessage(kWhatDecode, id()))->post();
8613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
8713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
8813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
895050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivivoid AudioSfDecoder::getPositionMsec(int* msec) {
905050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi    int64_t timeUsec = getPositionUsec();
915050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi    if (timeUsec == ANDROID_UNKNOWN_TIME) {
925050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi        *msec = ANDROID_UNKNOWN_TIME;
935050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi    } else {
945933f3d5e532aaac31ce0e6551c59f0197c0ae3cGlenn Kasten        *msec = timeUsec / 1000;
955050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi    }
965050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi}
975050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi
985050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi
9913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::startPrefetch_async() {
10013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    SL_LOGV("AudioSfDecoder::startPrefetch_async()");
10113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
10213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (wantPrefetch()) {
10313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGV("AudioSfDecoder::startPrefetch_async(): sending check cache msg");
10413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
10513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mStateFlags |= kFlagPreparing | kFlagBuffering;
10613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
10713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        (new AMessage(kWhatCheckCache, id()))->post();
10813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
10913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
11013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
11113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
11213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi//--------------------------------------------------
1137f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Triviuint32_t AudioSfDecoder::getPcmFormatKeyCount() {
11454cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    android::Mutex::Autolock autoLock(mPcmFormatLock);
1157f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    return mPcmFormatKeyCount;
1167f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi}
1177f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
1187f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
1197f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi//--------------------------------------------------
1207f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivibool AudioSfDecoder::getPcmFormatKeySize(uint32_t index, uint32_t* pKeySize) {
1217f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    uint32_t keyCount = getPcmFormatKeyCount();
1227f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    if (index >= keyCount) {
1237f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        return false;
1247f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    } else {
1257f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        *pKeySize = strlen(kPcmDecodeMetadataKeys[index]) +1;
1267f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        return true;
1277f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    }
1287f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi}
1297f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
1307f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
1317f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi//--------------------------------------------------
1327f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivibool AudioSfDecoder::getPcmFormatKeyName(uint32_t index, uint32_t keySize, char* keyName) {
1337f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    uint32_t actualKeySize;
1347f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    if (!getPcmFormatKeySize(index, &actualKeySize)) {
1357f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        return false;
1367f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    }
1377f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    if (keySize < actualKeySize) {
1387f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        return false;
1397f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    }
1407f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    strncpy(keyName, kPcmDecodeMetadataKeys[index], actualKeySize);
1417f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    return true;
1427f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi}
1437f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
1447f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
1457f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi//--------------------------------------------------
1467f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivibool AudioSfDecoder::getPcmFormatValueSize(uint32_t index, uint32_t* pValueSize) {
1477f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    uint32_t keyCount = getPcmFormatKeyCount();
1487f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    if (index >= keyCount) {
1497f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        *pValueSize = 0;
1507f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        return false;
1517f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    } else {
1527f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        *pValueSize = sizeof(uint32_t);
1537f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        return true;
1547f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    }
1557f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi}
1567f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
1577f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
1587f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi//--------------------------------------------------
1597f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivibool AudioSfDecoder::getPcmFormatKeyValue(uint32_t index, uint32_t size, uint32_t* pValue) {
1607f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    uint32_t valueSize = 0;
1617f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    if (!getPcmFormatValueSize(index, &valueSize)) {
1627f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        return false;
16313d02b645fc6e8ffe70a8bf8cc5f69f03558ae40Jean-Michel Trivi    } else if (size != valueSize) {
1647f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        // this ensures we are accessing mPcmFormatValues with a valid size for that index
16513d02b645fc6e8ffe70a8bf8cc5f69f03558ae40Jean-Michel Trivi        SL_LOGE("Error retrieving metadata value at index %d: using size of %d, should be %d",
16613d02b645fc6e8ffe70a8bf8cc5f69f03558ae40Jean-Michel Trivi                index, size, valueSize);
1677f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        return false;
1687f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    } else {
1697f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        *pValue = mPcmFormatValues[index];
1707f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        return true;
1717f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    }
1727f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi}
1737f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
1747f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
1757f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi//--------------------------------------------------
17613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi// Event handlers
177e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi//  it is strictly verboten to call those methods outside of the event loop
178e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi
179e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi// Initializes the data and audio sources, and update the PCM format info
180e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi// post-condition: upon successful initialization based on the player data locator
181e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi//    GenericPlayer::onPrepare() was called
182e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi//    mDataSource != 0
183e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi//    mAudioSource != 0
184e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi//    mAudioSourceStarted == true
185e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten// All error returns from this method are via notifyPrepared(status) followed by "return".
18613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::onPrepare() {
187e9236d046fdb5cac0696c42e03443a2439188146Jean-Michel Trivi    SL_LOGD("AudioSfDecoder::onPrepare()");
188e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    Mutex::Autolock _l(mBufferSourceLock);
18913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
190e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    // Initialize the PCM format info with the known parameters before the start of the decode
1917f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_BITSPERSAMPLE] = SL_PCMSAMPLEFORMAT_FIXED_16;
1927f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_CONTAINERSIZE] = 16;
1937f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_ENDIANNESS] = SL_BYTEORDER_LITTLEENDIAN;
194e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    //    initialization with the default values: they will be replaced by the actual values
195e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    //      once the decoder has figured them out
1967f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_NUMCHANNELS] = mChannelCount;
1977f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_SAMPLESPERSEC] = mSampleRateHz;
1987f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_CHANNELMASK] = mChannelMask;
1997f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
200e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    //---------------------------------
201e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    // Instantiate and initialize the data source for the decoder
20213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    sp<DataSource> dataSource;
20313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
20413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    switch (mDataLocatorType) {
20513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
20613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    case kDataLocatorNone:
20713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGE("AudioSfDecoder::onPrepare: no data locator set");
20813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        notifyPrepared(MEDIA_ERROR_BASE);
20913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        return;
21013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
21113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    case kDataLocatorUri:
21293ac9bd4f7722c50dc9882ff74bade233860a940Jean-Michel Trivi        dataSource = DataSource::CreateFromURI(mDataLocator.uriRef);
21393ac9bd4f7722c50dc9882ff74bade233860a940Jean-Michel Trivi        if (dataSource == NULL) {
21493ac9bd4f7722c50dc9882ff74bade233860a940Jean-Michel Trivi            SL_LOGE("AudioSfDecoder::onPrepare(): Error opening %s", mDataLocator.uriRef);
21513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            notifyPrepared(MEDIA_ERROR_BASE);
21613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            return;
21713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
21813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        break;
21913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
22093ac9bd4f7722c50dc9882ff74bade233860a940Jean-Michel Trivi    case kDataLocatorFd:
22193ac9bd4f7722c50dc9882ff74bade233860a940Jean-Michel Trivi    {
22213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        dataSource = new FileSource(
22313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                mDataLocator.fdi.fd, mDataLocator.fdi.offset, mDataLocator.fdi.length);
22413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        status_t err = dataSource->initCheck();
22513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (err != OK) {
22613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            notifyPrepared(err);
22713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            return;
22813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
22993ac9bd4f7722c50dc9882ff74bade233860a940Jean-Michel Trivi        break;
23013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
23113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
23213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    default:
23313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        TRESPASS();
23413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
23513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
236e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    //---------------------------------
237e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    // Instanciate and initialize the decoder attached to the data source
23813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    sp<MediaExtractor> extractor = MediaExtractor::Create(dataSource);
23913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (extractor == NULL) {
24013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGE("AudioSfDecoder::onPrepare: Could not instantiate extractor.");
24113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        notifyPrepared(ERROR_UNSUPPORTED);
24213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        return;
24313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
24413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
24513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    ssize_t audioTrackIndex = -1;
24613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    bool isRawAudio = false;
24713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    for (size_t i = 0; i < extractor->countTracks(); ++i) {
24813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        sp<MetaData> meta = extractor->getTrackMetaData(i);
24913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
25013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        const char *mime;
25113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        CHECK(meta->findCString(kKeyMIMEType, &mime));
25213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
25313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (!strncasecmp("audio/", mime, 6)) {
25413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            audioTrackIndex = i;
25513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
25613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            if (!strcasecmp(MEDIA_MIMETYPE_AUDIO_RAW, mime)) {
25713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                isRawAudio = true;
25813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            }
25913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            break;
26013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
26113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
26213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
26313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (audioTrackIndex < 0) {
26413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGE("AudioSfDecoder::onPrepare: Could not find a supported audio track.");
26513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        notifyPrepared(ERROR_UNSUPPORTED);
26613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        return;
26713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
26813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
26913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    sp<MediaSource> source = extractor->getTrack(audioTrackIndex);
27013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    sp<MetaData> meta = source->getFormat();
27113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
27254cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    // we can't trust the OMXCodec (if there is one) to issue a INFO_FORMAT_CHANGED so we want
27354cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    // to have some meaningful values as soon as possible.
2747f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    bool hasChannelCount = meta->findInt32(kKeyChannelCount, &mChannelCount);
27554cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    int32_t sr;
27654cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    bool hasSampleRate = meta->findInt32(kKeySampleRate, &sr);
27754cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    if (hasSampleRate) {
27854cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi        mSampleRateHz = (uint32_t) sr;
2797f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    }
2807f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
28113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    off64_t size;
28213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    int64_t durationUs;
28313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (dataSource->getSize(&size) == OK
28413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            && meta->findInt64(kKeyDuration, &durationUs)) {
2855050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi        if (durationUs != 0) {
2865050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi            mBitrate = size * 8000000ll / durationUs;  // in bits/sec
2875050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi        } else {
2885050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi            mBitrate = -1;
2895050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi        }
29013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mDurationUsec = durationUs;
2917f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        mDurationMsec = durationUs / 1000;
29213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    } else {
29313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mBitrate = -1;
2945050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi        mDurationUsec = ANDROID_UNKNOWN_TIME;
2955050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi        mDurationMsec = ANDROID_UNKNOWN_TIME;
29613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
29713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
298e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    // the audio content is not raw PCM, so we need a decoder
29913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (!isRawAudio) {
30013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        OMXClient client;
30113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        CHECK_EQ(client.connect(), (status_t)OK);
30213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
30313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        source = OMXCodec::Create(
30413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                client.interface(), meta, false /* createEncoder */,
30513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                source);
30613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
30713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (source == NULL) {
30813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            SL_LOGE("AudioSfDecoder::onPrepare: Could not instantiate decoder.");
30913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            notifyPrepared(ERROR_UNSUPPORTED);
31013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            return;
31113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
31213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
31313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        meta = source->getFormat();
31413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
31513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
31613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
31713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (source->start() != OK) {
31813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGE("AudioSfDecoder::onPrepare: Failed to start source/decoder.");
31913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        notifyPrepared(MEDIA_ERROR_BASE);
32013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        return;
32113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
32213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
323e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    //---------------------------------
324e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    // The data source, and audio source (a decoder if required) are ready to be used
32513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    mDataSource = dataSource;
32613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    mAudioSource = source;
327e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    mAudioSourceStarted = true;
32813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
3297f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    if (!hasChannelCount) {
3307f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        CHECK(meta->findInt32(kKeyChannelCount, &mChannelCount));
3317f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    }
33254cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi
33354cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    if (!hasSampleRate) {
33454cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi        CHECK(meta->findInt32(kKeySampleRate, &sr));
33554cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi        mSampleRateHz = (uint32_t) sr;
33654cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    }
3377f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    // FIXME add code below once channel mask support is in, currently initialized to default
3387f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    //    if (meta->findInt32(kKeyChannelMask, &mChannelMask)) {
3397f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    //        mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_CHANNELMASK] = mChannelMask;
3407f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    //    }
34113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
34213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (!wantPrefetch()) {
34313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGV("AudioSfDecoder::onPrepare: no need to prefetch");
34413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        // doesn't need prefetching, notify good to go
34513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mCacheStatus = kStatusHigh;
34613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mCacheFill = 1000;
34713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        notifyStatus();
34813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        notifyCacheFill();
34913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
35013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
35154cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    {
35254cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi        android::Mutex::Autolock autoLock(mPcmFormatLock);
35354cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi        mPcmFormatKeyCount = NB_PCMMETADATA_KEYS;
35454cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi        mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_SAMPLESPERSEC] = mSampleRateHz;
35554cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi        mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_NUMCHANNELS] = mChannelCount;
35654cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    }
35754cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi
35813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    // at this point we have enough information about the source to create the sink that
35913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    // will consume the data
36013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    createAudioSink();
36113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
362e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten    // signal successful completion of prepare
363e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten    mStateFlags |= kFlagPrepared;
36413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    GenericPlayer::onPrepare();
365e9236d046fdb5cac0696c42e03443a2439188146Jean-Michel Trivi    SL_LOGD("AudioSfDecoder::onPrepare() done, mStateFlags=0x%x", mStateFlags);
36613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
36713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
36813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
36913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::onPause() {
37054cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    SL_LOGV("AudioSfDecoder::onPause()");
37113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    GenericPlayer::onPause();
37213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    pauseAudioSink();
37313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
37413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
37513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
37613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::onPlay() {
37754cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    SL_LOGV("AudioSfDecoder::onPlay()");
37813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    GenericPlayer::onPlay();
37913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    startAudioSink();
38013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
38113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
38213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
38313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::onSeek(const sp<AMessage> &msg) {
38413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    SL_LOGV("AudioSfDecoder::onSeek");
38513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    int64_t timeMsec;
38613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    CHECK(msg->findInt64(WHATPARAM_SEEK_SEEKTIME_MS, &timeMsec));
38713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
3885050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi    Mutex::Autolock _l(mTimeLock);
38913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    mStateFlags |= kFlagSeeking;
39013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    mSeekTimeMsec = timeMsec;
3915050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi    mLastDecodedPositionUs = ANDROID_UNKNOWN_TIME;
39213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
39313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
39413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
39513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::onLoop(const sp<AMessage> &msg) {
39613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    SL_LOGV("AudioSfDecoder::onLoop");
39713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    int32_t loop;
39813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    CHECK(msg->findInt32(WHATPARAM_LOOP_LOOPING, &loop));
39913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
40013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (loop) {
40113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        //SL_LOGV("AudioSfDecoder::onLoop start looping");
40213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mStateFlags |= kFlagLooping;
40313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    } else {
40413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        //SL_LOGV("AudioSfDecoder::onLoop stop looping");
40513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mStateFlags &= ~kFlagLooping;
40613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
40713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
40813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
40913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
41013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::onCheckCache(const sp<AMessage> &msg) {
41113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    //SL_LOGV("AudioSfDecoder::onCheckCache");
41213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    bool eos;
4134ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi    CacheStatus_t status = getCacheRemaining(&eos);
41413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
41513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (eos || status == kStatusHigh
41613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            || ((mStateFlags & kFlagPreparing) && (status >= kStatusEnough))) {
41713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (mStateFlags & kFlagPlaying) {
41813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            startAudioSink();
41913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
42013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mStateFlags &= ~kFlagBuffering;
42113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
42213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGV("AudioSfDecoder::onCheckCache: buffering done.");
42313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
42413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (mStateFlags & kFlagPreparing) {
42513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            //SL_LOGV("AudioSfDecoder::onCheckCache: preparation done.");
42613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            mStateFlags &= ~kFlagPreparing;
42713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
42813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
42913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (mStateFlags & kFlagPlaying) {
43013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            (new AMessage(kWhatDecode, id()))->post();
43113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
43213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        return;
43313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
43413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
43513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    msg->post(100000);
43613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
43713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
43813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
43913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::onDecode() {
44013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    SL_LOGV("AudioSfDecoder::onDecode");
44113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
44213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    //-------------------------------- Need to buffer some more before decoding?
44313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    bool eos;
44413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (mDataSource == 0) {
44513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        // application set play state to paused which failed, then set play state to playing
44613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        return;
44713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
448e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi
44913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (wantPrefetch()
45013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            && (getCacheRemaining(&eos) == kStatusLow)
45113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            && !eos) {
45213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGV("buffering more.");
45313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
45413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (mStateFlags & kFlagPlaying) {
45513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            pauseAudioSink();
45613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
45713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mStateFlags |= kFlagBuffering;
45813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        (new AMessage(kWhatCheckCache, id()))->post(100000);
45913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        return;
46013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
46113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
46213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (!(mStateFlags & (kFlagPlaying | kFlagBuffering | kFlagPreparing))) {
46313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        // don't decode if we're not buffering, prefetching or playing
46413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        //SL_LOGV("don't decode: not buffering, prefetching or playing");
46513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        return;
46613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
46713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
46813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    //-------------------------------- Decode
46913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    status_t err;
47013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    MediaSource::ReadOptions readOptions;
47113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (mStateFlags & kFlagSeeking) {
4725050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi        assert(mSeekTimeMsec != ANDROID_UNKNOWN_TIME);
47313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        readOptions.setSeekTo(mSeekTimeMsec * 1000);
47413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
47513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
4765050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi    int64_t timeUsec = ANDROID_UNKNOWN_TIME;
47713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    {
478e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        Mutex::Autolock _l(mBufferSourceLock);
479e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi
48013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (NULL != mDecodeBuffer) {
48113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            // the current decoded buffer hasn't been rendered, drop it
48213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            mDecodeBuffer->release();
48313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            mDecodeBuffer = NULL;
48413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
485e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        if(!mAudioSourceStarted) {
486e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi            return;
487e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        }
48813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        err = mAudioSource->read(&mDecodeBuffer, &readOptions);
48913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (err == OK) {
4905050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi            CHECK(mDecodeBuffer->meta_data()->findInt64(kKeyTime, &timeUsec));
49113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
49213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
49313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
49413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    {
4955050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi        Mutex::Autolock _l(mTimeLock);
49613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (mStateFlags & kFlagSeeking) {
49713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            mStateFlags &= ~kFlagSeeking;
4985050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi            mSeekTimeMsec = ANDROID_UNKNOWN_TIME;
4995050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi        }
5005050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi        if (timeUsec != ANDROID_UNKNOWN_TIME) {
5015050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi            mLastDecodedPositionUs = timeUsec;
50213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
50313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
50413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
50513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    //-------------------------------- Handle return of decode
50613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (err != OK) {
50713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        bool continueDecoding = false;
50813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        switch(err) {
50913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            case ERROR_END_OF_STREAM:
51013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                if (0 < mDurationUsec) {
5115050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi                    Mutex::Autolock _l(mTimeLock);
51213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                    mLastDecodedPositionUs = mDurationUsec;
51313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                }
51413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                // handle notification and looping at end of stream
51513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                if (mStateFlags & kFlagPlaying) {
51613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                    notify(PLAYEREVENT_ENDOFSTREAM, 1, true);
51713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                }
51813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                if (mStateFlags & kFlagLooping) {
51913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                    seek(0);
52013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                    // kick-off decoding again
52113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                    continueDecoding = true;
52213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                }
52313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                break;
52413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            case INFO_FORMAT_CHANGED:
525e9236d046fdb5cac0696c42e03443a2439188146Jean-Michel Trivi                SL_LOGD("MediaSource::read encountered INFO_FORMAT_CHANGED");
52613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                // reconfigure output
52754cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi                {
52854cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi                    Mutex::Autolock _l(mBufferSourceLock);
52954cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi                    hasNewDecodeParams();
53054cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi                }
53113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                continueDecoding = true;
53213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                break;
53313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            case INFO_DISCONTINUITY:
534e9236d046fdb5cac0696c42e03443a2439188146Jean-Michel Trivi                SL_LOGD("MediaSource::read encountered INFO_DISCONTINUITY");
53513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                continueDecoding = true;
53613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                break;
53713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            default:
53813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                SL_LOGE("MediaSource::read returned error %d", err);
53913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                break;
54013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
54113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (continueDecoding) {
54213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            if (NULL == mDecodeBuffer) {
54313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                (new AMessage(kWhatDecode, id()))->post();
54413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                return;
54513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            }
54613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        } else {
54713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            return;
54813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
54913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
55013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
55113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    //-------------------------------- Render
55213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    sp<AMessage> msg = new AMessage(kWhatRender, id());
55313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    msg->post();
55413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
55513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
55613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
55713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::onRender() {
55813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    //SL_LOGV("AudioSfDecoder::onRender");
55913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
560e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    Mutex::Autolock _l(mBufferSourceLock);
56113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
56213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (NULL == mDecodeBuffer) {
56313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        // nothing to render, move along
56413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGV("AudioSfDecoder::onRender NULL buffer, exiting");
56513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        return;
56613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
56713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
56813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    mDecodeBuffer->release();
56913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    mDecodeBuffer = NULL;
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 kWhatPrepare:
57713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            onPrepare();
57813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            break;
57913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
58013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        case kWhatDecode:
58113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            onDecode();
58213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            break;
58313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
58413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        case kWhatRender:
58513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            onRender();
58613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            break;
58713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
58813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        case kWhatCheckCache:
58913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            onCheckCache(msg);
59013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            break;
59113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
59213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        case kWhatNotif:
59313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            onNotify(msg);
59413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            break;
59513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
59613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        case kWhatPlay:
59713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            onPlay();
59813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            break;
59913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
60013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        case kWhatPause:
60113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            onPause();
60213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            break;
6037f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
60413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi/*
60513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        case kWhatSeek:
60613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            onSeek(msg);
60713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            break;
60813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
60913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        case kWhatLoop:
61013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            onLoop(msg);
61113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            break;
61213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi*/
61313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        default:
61413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            GenericPlayer::onMessageReceived(msg);
61513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            break;
61613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
61713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
61813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
61913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi//--------------------------------------------------
62013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi// Prepared state, prefetch status notifications
62113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::notifyPrepared(status_t prepareRes) {
622e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten    assert(!(mStateFlags & (kFlagPrepared | kFlagPreparedUnsuccessfully)));
623e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten    if (NO_ERROR == prepareRes) {
624e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten        // The "then" fork is not currently used, but is kept here to make it easier
625e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten        // to replace by a new signalPrepareCompletion(status) if we re-visit this later.
626e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten        mStateFlags |= kFlagPrepared;
627e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten    } else {
628e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten        mStateFlags |= kFlagPreparedUnsuccessfully;
629e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten    }
630e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten    // Do not call the superclass onPrepare to notify, because it uses a default error
631e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten    // status code but we can provide a more specific one.
632e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten    // GenericPlayer::onPrepare();
63313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    notify(PLAYEREVENT_PREPARED, (int32_t)prepareRes, true);
634e878c470cf58c8654d613ab2449468b44a90d6e5Glenn Kasten    SL_LOGD("AudioSfDecoder::onPrepare() done, mStateFlags=0x%x", mStateFlags);
63513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
63613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
63713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
63813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivivoid AudioSfDecoder::onNotify(const sp<AMessage> &msg) {
639e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    notif_cbf_t notifyClient;
640e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    void*       notifyUser;
641e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    {
642e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        android::Mutex::Autolock autoLock(mNotifyClientLock);
643e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        if (NULL == mNotifyClient) {
644e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi            return;
645e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        } else {
646e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi            notifyClient = mNotifyClient;
647e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi            notifyUser   = mNotifyUser;
648e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        }
64913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
65013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    int32_t val;
65113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (msg->findInt32(PLAYEREVENT_PREFETCHSTATUSCHANGE, &val)) {
65213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGV("\tASfPlayer notifying %s = %d", PLAYEREVENT_PREFETCHSTATUSCHANGE, val);
653e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        notifyClient(kEventPrefetchStatusChange, val, 0, notifyUser);
65413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
65513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    else if (msg->findInt32(PLAYEREVENT_PREFETCHFILLLEVELUPDATE, &val)) {
65613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGV("\tASfPlayer notifying %s = %d", PLAYEREVENT_PREFETCHFILLLEVELUPDATE, val);
657e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        notifyClient(kEventPrefetchFillLevelUpdate, val, 0, notifyUser);
65813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
65913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    else if (msg->findInt32(PLAYEREVENT_ENDOFSTREAM, &val)) {
66013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        SL_LOGV("\tASfPlayer notifying %s = %d", PLAYEREVENT_ENDOFSTREAM, val);
661e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi        notifyClient(kEventEndOfStream, val, 0, notifyUser);
66213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
66313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    else {
66413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        GenericPlayer::onNotify(msg);
66513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
66613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
66713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
66813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
66913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi//--------------------------------------------------
67013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi// Private utility functions
67113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
67213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivibool AudioSfDecoder::wantPrefetch() {
673ac18c1cd32408884d3960bd7aa56ba419c2ca68bJean-Michel Trivi    if (mDataSource != 0) {
674ac18c1cd32408884d3960bd7aa56ba419c2ca68bJean-Michel Trivi        return (mDataSource->flags() & DataSource::kWantsPrefetching);
675ac18c1cd32408884d3960bd7aa56ba419c2ca68bJean-Michel Trivi    } else {
676ac18c1cd32408884d3960bd7aa56ba419c2ca68bJean-Michel Trivi        // happens if an improper data locator was passed, if the media extractor couldn't be
677ac18c1cd32408884d3960bd7aa56ba419c2ca68bJean-Michel Trivi        //  initialized, if there is no audio track in the media, if the OMX decoder couldn't be
678ac18c1cd32408884d3960bd7aa56ba419c2ca68bJean-Michel Trivi        //  instantiated, if the source couldn't be opened, or if the MediaSource
679ac18c1cd32408884d3960bd7aa56ba419c2ca68bJean-Michel Trivi        //  couldn't be started
680ac18c1cd32408884d3960bd7aa56ba419c2ca68bJean-Michel Trivi        SL_LOGV("AudioSfDecoder::wantPrefetch() tries to access NULL mDataSource");
681ac18c1cd32408884d3960bd7aa56ba419c2ca68bJean-Michel Trivi        return false;
682ac18c1cd32408884d3960bd7aa56ba419c2ca68bJean-Michel Trivi    }
68313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
68413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
68513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
68613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Triviint64_t AudioSfDecoder::getPositionUsec() {
6875050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi    Mutex::Autolock _l(mTimeLock);
68813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (mStateFlags & kFlagSeeking) {
68913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        return mSeekTimeMsec * 1000;
69013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    } else {
69113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (mLastDecodedPositionUs < 0) {
6925050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi            return ANDROID_UNKNOWN_TIME;
69313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        } else {
69413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            return mLastDecodedPositionUs;
69513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
69613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
69713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
69813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
69913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
7004ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel TriviCacheStatus_t AudioSfDecoder::getCacheRemaining(bool *eos) {
70113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    sp<NuCachedSource2> cachedSource =
70213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        static_cast<NuCachedSource2 *>(mDataSource.get());
70313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
7044ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi    CacheStatus_t oldStatus = mCacheStatus;
70513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
70613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    status_t finalStatus;
70713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    size_t dataRemaining = cachedSource->approxDataRemaining(&finalStatus);
70813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    *eos = (finalStatus != OK);
70913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
71013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    CHECK_GE(mBitrate, 0);
71113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
71213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    int64_t dataRemainingUs = dataRemaining * 8000000ll / mBitrate;
71313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    //SL_LOGV("AudioSfDecoder::getCacheRemaining: approx %.2f secs remaining (eos=%d)",
71413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    //       dataRemainingUs / 1E6, *eos);
71513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
71613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (*eos) {
71713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        // data is buffered up to the end of the stream, it can't get any better than this
71813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mCacheStatus = kStatusHigh;
71913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        mCacheFill = 1000;
72013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
72113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    } else {
72213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        if (mDurationUsec > 0) {
72313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            // known duration:
72413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
72513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            //   fill level is ratio of how much has been played + how much is
72613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            //   cached, divided by total duration
72713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            uint32_t currentPositionUsec = getPositionUsec();
7285050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi            if (currentPositionUsec == ANDROID_UNKNOWN_TIME) {
7295050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi                // if we don't know where we are, assume the worst for the fill ratio
7305050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi                currentPositionUsec = 0;
7315050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi            }
7325050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi            if (mDurationUsec > 0) {
7335050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi                mCacheFill = (int16_t) ((1000.0
7345050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi                        * (double)(currentPositionUsec + dataRemainingUs) / mDurationUsec));
7355050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi            } else {
7365050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi                mCacheFill = 0;
7375050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi            }
73813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            //SL_LOGV("cacheFill = %d", mCacheFill);
73913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
74013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            //   cache status is evaluated against duration thresholds
7414ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi            if (dataRemainingUs > DURATION_CACHED_HIGH_MS*1000) {
74213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                mCacheStatus = kStatusHigh;
74313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                //LOGV("high");
7444ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi            } else if (dataRemainingUs > DURATION_CACHED_MED_MS*1000) {
74513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                //LOGV("enough");
74613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                mCacheStatus = kStatusEnough;
7474ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi            } else if (dataRemainingUs < DURATION_CACHED_LOW_MS*1000) {
74813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                //LOGV("low");
74913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                mCacheStatus = kStatusLow;
75013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            } else {
75113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                mCacheStatus = kStatusIntermediate;
75213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            }
75313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
75413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        } else {
75513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            // unknown duration:
75613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
75713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            //   cache status is evaluated against cache amount thresholds
75813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            //   (no duration so we don't have the bitrate either, could be derived from format?)
75913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            if (dataRemaining > SIZE_CACHED_HIGH_BYTES) {
76013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                mCacheStatus = kStatusHigh;
76113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            } else if (dataRemaining > SIZE_CACHED_MED_BYTES) {
76213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                mCacheStatus = kStatusEnough;
76313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            } else if (dataRemaining < SIZE_CACHED_LOW_BYTES) {
76413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                mCacheStatus = kStatusLow;
76513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            } else {
76613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi                mCacheStatus = kStatusIntermediate;
76713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi            }
76813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        }
76913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
77013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
77113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
77213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (oldStatus != mCacheStatus) {
77313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        notifyStatus();
77413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
77513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
77613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    if (abs(mCacheFill - mLastNotifiedCacheFill) > mCacheFillNotifThreshold) {
77713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi        notifyCacheFill();
77813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    }
77913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
78013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    return mCacheStatus;
78113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}
78213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
78354cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi
78454cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivivoid AudioSfDecoder::hasNewDecodeParams() {
78554cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi
78654cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    if ((mAudioSource != 0) && mAudioSourceStarted) {
78754cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi        sp<MetaData> meta = mAudioSource->getFormat();
78854cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi
78954cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi        CHECK(meta->findInt32(kKeyChannelCount, &mChannelCount));
79054cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi
79154cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi        SL_LOGV("old sample rate = %d", mSampleRateHz);
79254cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi        int32_t sr;
79354cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi        CHECK(meta->findInt32(kKeySampleRate, &sr));
79454cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi        mSampleRateHz = (uint32_t) sr;
79554cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi        SL_LOGV("found new sample rate = %d", mSampleRateHz);
79654cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi
79754cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi        {
79854cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi            android::Mutex::Autolock autoLock(mPcmFormatLock);
79954cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi            mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_NUMCHANNELS] = mChannelCount;
80054cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi            mPcmFormatValues[ANDROID_KEY_INDEX_PCMFORMAT_SAMPLESPERSEC] = mSampleRateHz;
80154cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi        }
80254cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    }
80354cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi
80454cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    // alert users of those params
80554cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    updateAudioSink();
80654cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi}
80754cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi
80813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi} // namespace android
809