android_AudioSfDecoder.h revision 7f5cc1afe49395fefaad9b2bbd728a45d1bfda6a
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
4913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    // overridden from GenericPlayer
5013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    virtual void play();
5113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
5213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    void startPrefetch_async();
5313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
547f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    uint32_t getPcmFormatKeyCount();
557f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    bool     getPcmFormatKeySize(uint32_t index, uint32_t* pKeySize);
567f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    bool     getPcmFormatKeyName(uint32_t index, uint32_t keySize, char* keyName);
577f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    bool     getPcmFormatValueSize(uint32_t index, uint32_t* pValueSize);
587f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    bool     getPcmFormatKeyValue(uint32_t index, uint32_t size, uint32_t* pValue);
597f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
6013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Triviprotected:
6113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
6213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    enum {
637f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        kWhatDecode       = 'deco',
647f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        kWhatRender       = 'rend',
657f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        kWhatCheckCache   = 'cach',
667f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        kWhatGetPcmFormat = 'gpcm'
6713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    };
6813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
6913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    // Async event handlers (called from the AudioSfDecoder's event loop)
7013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    void onDecode();
7113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    void onCheckCache(const sp<AMessage> &msg);
7213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    virtual void onRender();
7313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
7413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    // Async event handlers (called from GenericPlayer's event loop)
7513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    virtual void onPrepare();
7613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    virtual void onPlay();
7713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    virtual void onPause();
7813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    virtual void onSeek(const sp<AMessage> &msg);
7913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    virtual void onLoop(const sp<AMessage> &msg);
807f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    virtual void onGetPcmFormatKeyCount();
8113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
8213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    // overridden from GenericPlayer
8313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    virtual void onNotify(const sp<AMessage> &msg);
8413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    virtual void onMessageReceived(const sp<AMessage> &msg);
8513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
8613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    // to be implemented by subclasses of AudioSfDecoder to do something with the audio samples
8713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    virtual void createAudioSink() = 0;
8813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    virtual void updateAudioSink() = 0;
8913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    virtual void startAudioSink() = 0;
9013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    virtual void pauseAudioSink() = 0;
9113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
9213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    sp<DataSource> mDataSource;
9313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    sp<MediaSource> mAudioSource;
9413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
9513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    // negative values indicate invalid value
9613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    int64_t mBitrate;  // in bits/sec
977f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    uint32_t mChannelMask;
9813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    int64_t mDurationUsec;
9913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
10013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    // buffer passed from decoder to renderer
10113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    MediaBuffer *mDecodeBuffer;
10213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    // mutex used to protect the decode buffer
10313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    Mutex       mDecodeBufferLock;
10413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
10513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Triviprivate:
10613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
10713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    void notifyPrepared(status_t prepareRes);
10813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
10913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    int64_t mTimeDelta;
11013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    int64_t mSeekTimeMsec;
11113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    int64_t mLastDecodedPositionUs;
11213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
11313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    // mutex used for seek flag and seek time read/write
11413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    Mutex mSeekLock;
11513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
1167f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    // informations that can be retrieved in the PCM format queries
1177f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    //  these values are only written in the event loop
1187f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    uint32_t mPcmFormatKeyCount;
1197f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    uint32_t mPcmFormatValues[NB_PCMMETADATA_KEYS];
1207f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    // for synchronous "get" calls on the PCM decode format:
1217f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    //    prevents concurrent "getPcmFormat" calls
1227f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    Mutex       mGetPcmFormatLockSingleton;
1237f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    //    lock order 1/ mGetPcmFormatLockSingleton  2/ mGetPcmFormatLock
1247f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    Mutex       mGetPcmFormatLock;
1257f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    Condition   mGetPcmFormatCondition;
1267f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    bool        mGetPcmFormatKeyCount;
1277f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
12813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    bool wantPrefetch();
1294ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi    CacheStatus_t getCacheRemaining(bool *eos);
13013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    int64_t getPositionUsec();
13113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
13213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Triviprivate:
13313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    DISALLOW_EVIL_CONSTRUCTORS(AudioSfDecoder);
13413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
13513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi};
13613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
13713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi} // namespace android
138