android_AudioSfDecoder.h revision e6ded5c61944a87fa9e472dec3a6929855d42aeb
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
174ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi#include <media/stagefright/DataSource.h>
184ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi#include <media/stagefright/MediaSource.h>
194ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi#include <media/stagefright/FileSource.h>
204ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi#include <media/stagefright/MediaDefs.h>
214ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi#include <media/stagefright/MediaExtractor.h>
224ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi#include <media/stagefright/MetaData.h>
234ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi#include <media/stagefright/OMXClient.h>
244ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi#include <media/stagefright/OMXCodec.h>
254ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi#include "NuCachedSource2.h"
264ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi#include "NuHTTPDataSource.h"
274ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi#include "ThrottledSource.h"
284ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi
294ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi#include "android_GenericPlayer.h"
307f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi#include "OpenSLES_AndroidMetadata.h"
3113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
3213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi//--------------------------------------------------------------------------------------------------
3313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivinamespace android {
3413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
357f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi// to keep in sync with the ANDROID_KEY_INDEX_PCMFORMAT_* constants in android_AudioSfDecoder.cpp
367f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivistatic const char* const kPcmDecodeMetadataKeys[] = {
377f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        ANDROID_KEY_PCMFORMAT_NUMCHANNELS, ANDROID_KEY_PCMFORMAT_SAMPLESPERSEC,
387f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        ANDROID_KEY_PCMFORMAT_BITSPERSAMPLE, ANDROID_KEY_PCMFORMAT_CONTAINERSIZE,
397f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        ANDROID_KEY_PCMFORMAT_CHANNELMASK, ANDROID_KEY_PCMFORMAT_ENDIANNESS };
407f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi#define NB_PCMMETADATA_KEYS (sizeof(kPcmDecodeMetadataKeys)/sizeof(kPcmDecodeMetadataKeys[0]))
417f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
4213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Triviclass AudioSfDecoder : public GenericPlayer
4313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi{
4413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivipublic:
4513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
4613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    AudioSfDecoder(const AudioPlayback_Parameters* params);
4713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    virtual ~AudioSfDecoder();
4813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
49e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    virtual void preDestroy();
50e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi
5113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    // overridden from GenericPlayer
5213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    virtual void play();
5313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
5413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    void startPrefetch_async();
5513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
567f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    uint32_t getPcmFormatKeyCount();
577f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    bool     getPcmFormatKeySize(uint32_t index, uint32_t* pKeySize);
587f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    bool     getPcmFormatKeyName(uint32_t index, uint32_t keySize, char* keyName);
597f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    bool     getPcmFormatValueSize(uint32_t index, uint32_t* pValueSize);
607f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    bool     getPcmFormatKeyValue(uint32_t index, uint32_t size, uint32_t* pValue);
617f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
6213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Triviprotected:
6313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
6413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    enum {
657f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        kWhatDecode       = 'deco',
667f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        kWhatRender       = 'rend',
677f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        kWhatCheckCache   = 'cach',
687f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        kWhatGetPcmFormat = 'gpcm'
6913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    };
7013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
7113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    // Async event handlers (called from the AudioSfDecoder's event loop)
7213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    void onDecode();
7313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    void onCheckCache(const sp<AMessage> &msg);
7413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    virtual void onRender();
7513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
7613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    // Async event handlers (called from GenericPlayer's event loop)
7713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    virtual void onPrepare();
7813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    virtual void onPlay();
7913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    virtual void onPause();
8013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    virtual void onSeek(const sp<AMessage> &msg);
8113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    virtual void onLoop(const sp<AMessage> &msg);
827f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    virtual void onGetPcmFormatKeyCount();
8313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
8413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    // overridden from GenericPlayer
8513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    virtual void onNotify(const sp<AMessage> &msg);
8613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    virtual void onMessageReceived(const sp<AMessage> &msg);
8713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
8813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    // to be implemented by subclasses of AudioSfDecoder to do something with the audio samples
8913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    virtual void createAudioSink() = 0;
9013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    virtual void updateAudioSink() = 0;
9113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    virtual void startAudioSink() = 0;
9213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    virtual void pauseAudioSink() = 0;
9313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
94e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    sp<DataSource>  mDataSource; // where the raw data comes from
95e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    sp<MediaSource> mAudioSource;// the decoder reading from the data source
96e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    // used to indicate mAudioSource was successfully started, but wasn't stopped
97e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    bool            mAudioSourceStarted;
9813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
9913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    // negative values indicate invalid value
10013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    int64_t mBitrate;  // in bits/sec
1017f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    uint32_t mChannelMask;
10213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    int64_t mDurationUsec;
10313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
10413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    // buffer passed from decoder to renderer
10513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    MediaBuffer *mDecodeBuffer;
106e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi
107e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    // mutex used to protect the decode buffer, the audio source and its running state
108e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    Mutex       mBufferSourceLock;
10913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
11013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Triviprivate:
11113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
11213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    void notifyPrepared(status_t prepareRes);
11313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
11413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    int64_t mTimeDelta;
11513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    int64_t mSeekTimeMsec;
11613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    int64_t mLastDecodedPositionUs;
11713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
11813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    // mutex used for seek flag and seek time read/write
11913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    Mutex mSeekLock;
12013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
1217f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    // informations that can be retrieved in the PCM format queries
1227f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    //  these values are only written in the event loop
1237f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    uint32_t mPcmFormatKeyCount;
1247f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    uint32_t mPcmFormatValues[NB_PCMMETADATA_KEYS];
1257f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    // for synchronous "get" calls on the PCM decode format:
1267f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    //    prevents concurrent "getPcmFormat" calls
1277f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    Mutex       mGetPcmFormatLockSingleton;
1287f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    //    lock order 1/ mGetPcmFormatLockSingleton  2/ mGetPcmFormatLock
1297f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    Mutex       mGetPcmFormatLock;
1307f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    Condition   mGetPcmFormatCondition;
1317f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    bool        mGetPcmFormatKeyCount;
1327f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
13313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    bool wantPrefetch();
1344ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi    CacheStatus_t getCacheRemaining(bool *eos);
13513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    int64_t getPositionUsec();
13613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
13713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Triviprivate:
13813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    DISALLOW_EVIL_CONSTRUCTORS(AudioSfDecoder);
13913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
14013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi};
14113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
14213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi} // namespace android
143