android_AudioSfDecoder.h revision bb832e853d4afb11b0a3287b2eb0cad87696d631
113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi/* 213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi * Copyright (C) 2011 The Android Open Source Project 313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi * 413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi * Licensed under the Apache License, Version 2.0 (the "License"); 513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi * you may not use this file except in compliance with the License. 613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi * You may obtain a copy of the License at 713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi * 813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi * http://www.apache.org/licenses/LICENSE-2.0 913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi * 1013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi * Unless required by applicable law or agreed to in writing, software 1113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi * distributed under the License is distributed on an "AS IS" BASIS, 1213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi * See the License for the specific language governing permissions and 1413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi * limitations under the License. 1513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi */ 1613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi 17bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi#ifndef AUDIO_SF_DECODER_H_ 18bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi#define AUDIO_SF_DECODER_H_ 19bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 204ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi#include <media/stagefright/DataSource.h> 214ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi#include <media/stagefright/MediaSource.h> 224ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi#include <media/stagefright/FileSource.h> 234ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi#include <media/stagefright/MediaDefs.h> 244ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi#include <media/stagefright/MediaExtractor.h> 254ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi#include <media/stagefright/MetaData.h> 264ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi#include <media/stagefright/OMXClient.h> 274ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi#include <media/stagefright/OMXCodec.h> 284ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi#include "NuCachedSource2.h" 294ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi#include "ThrottledSource.h" 304ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi 314ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi#include "android_GenericPlayer.h" 327f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi#include "OpenSLES_AndroidMetadata.h" 3313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi 3413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi//-------------------------------------------------------------------------------------------------- 3513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivinamespace android { 3613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi 37bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi// keep in sync with the entries of kPcmDecodeMetadataKeys[] 38bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi#define ANDROID_KEY_INDEX_PCMFORMAT_NUMCHANNELS 0 39bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi#define ANDROID_KEY_INDEX_PCMFORMAT_SAMPLESPERSEC 1 40bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi#define ANDROID_KEY_INDEX_PCMFORMAT_BITSPERSAMPLE 2 41bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi#define ANDROID_KEY_INDEX_PCMFORMAT_CONTAINERSIZE 3 42bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi#define ANDROID_KEY_INDEX_PCMFORMAT_CHANNELMASK 4 43bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi#define ANDROID_KEY_INDEX_PCMFORMAT_ENDIANNESS 5 44bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 457f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi// to keep in sync with the ANDROID_KEY_INDEX_PCMFORMAT_* constants in android_AudioSfDecoder.cpp 467f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivistatic const char* const kPcmDecodeMetadataKeys[] = { 477f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi ANDROID_KEY_PCMFORMAT_NUMCHANNELS, ANDROID_KEY_PCMFORMAT_SAMPLESPERSEC, 487f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi ANDROID_KEY_PCMFORMAT_BITSPERSAMPLE, ANDROID_KEY_PCMFORMAT_CONTAINERSIZE, 497f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi ANDROID_KEY_PCMFORMAT_CHANNELMASK, ANDROID_KEY_PCMFORMAT_ENDIANNESS }; 507f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi#define NB_PCMMETADATA_KEYS (sizeof(kPcmDecodeMetadataKeys)/sizeof(kPcmDecodeMetadataKeys[0])) 517f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi 5213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Triviclass AudioSfDecoder : public GenericPlayer 5313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi{ 5413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivipublic: 5513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi 5613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi AudioSfDecoder(const AudioPlayback_Parameters* params); 5713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi virtual ~AudioSfDecoder(); 5813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi 59e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi virtual void preDestroy(); 60e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi 6113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi // overridden from GenericPlayer 6213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi virtual void play(); 635050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi virtual void getPositionMsec(int* msec); //msec != NULL, ANDROID_UNKNOWN_TIME if unknown 6413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi 6513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi void startPrefetch_async(); 6613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi 677f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi uint32_t getPcmFormatKeyCount(); 687f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi bool getPcmFormatKeySize(uint32_t index, uint32_t* pKeySize); 697f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi bool getPcmFormatKeyName(uint32_t index, uint32_t keySize, char* keyName); 707f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi bool getPcmFormatValueSize(uint32_t index, uint32_t* pValueSize); 717f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi bool getPcmFormatKeyValue(uint32_t index, uint32_t size, uint32_t* pValue); 727f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi 7313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Triviprotected: 7413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi 7513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi enum { 767f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi kWhatDecode = 'deco', 777f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi kWhatRender = 'rend', 7854cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi kWhatCheckCache = 'cach' 7913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi }; 8013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi 8113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi // Async event handlers (called from the AudioSfDecoder's event loop) 8213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi void onDecode(); 8313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi void onCheckCache(const sp<AMessage> &msg); 8413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi virtual void onRender(); 8513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi 8613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi // Async event handlers (called from GenericPlayer's event loop) 8713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi virtual void onPrepare(); 8813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi virtual void onPlay(); 8913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi virtual void onPause(); 9013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi virtual void onSeek(const sp<AMessage> &msg); 9113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi virtual void onLoop(const sp<AMessage> &msg); 9213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi 9313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi // overridden from GenericPlayer 9413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi virtual void onNotify(const sp<AMessage> &msg); 9513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi virtual void onMessageReceived(const sp<AMessage> &msg); 9613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi 9713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi // to be implemented by subclasses of AudioSfDecoder to do something with the audio samples 9854cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi // (called from GenericPlayer's event loop) 9913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi virtual void createAudioSink() = 0; 10054cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi virtual void updateAudioSink() = 0; // called with mBufferSourceLock held 10113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi virtual void startAudioSink() = 0; 10213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi virtual void pauseAudioSink() = 0; 10313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi 104e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi sp<DataSource> mDataSource; // where the raw data comes from 105e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi sp<MediaSource> mAudioSource;// the decoder reading from the data source 106e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi // used to indicate mAudioSource was successfully started, but wasn't stopped 107e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi bool mAudioSourceStarted; 10813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi 10913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi // negative values indicate invalid value 11013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi int64_t mBitrate; // in bits/sec 1117f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi uint32_t mChannelMask; 1125050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi int64_t mDurationUsec; // ANDROID_UNKNOWN_TIME if unknown 11313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi 11413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi // buffer passed from decoder to renderer 11513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi MediaBuffer *mDecodeBuffer; 116e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi 117e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi // mutex used to protect the decode buffer, the audio source and its running state 118e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi Mutex mBufferSourceLock; 11913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi 12013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi void notifyPrepared(status_t prepareRes); 12113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi 12213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi int64_t mSeekTimeMsec; 1235050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi int64_t mLastDecodedPositionUs; // ANDROID_UNKNOWN_TIME if unknown 1245050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi // mutex used for seek flag, seek time (mSeekTimeMsec), 1255050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi // and last decoded position (mLastDecodedPositionUs) 1265050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi Mutex mTimeLock; 12713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi 1287f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi // informations that can be retrieved in the PCM format queries 1297f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi // these values are only written in the event loop 1307f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi uint32_t mPcmFormatKeyCount; 1317f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi uint32_t mPcmFormatValues[NB_PCMMETADATA_KEYS]; 13254cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi // protects mPcmFormatKeyCount and mPcmFormatValues 13354cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi Mutex mPcmFormatLock; 1347f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi 135bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Triviprivate: 13613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi bool wantPrefetch(); 1374ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi CacheStatus_t getCacheRemaining(bool *eos); 1385050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi int64_t getPositionUsec(); // ANDROID_UNKNOWN_TIME if unknown 13913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi 14054cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi // convenience function to update internal state when decoding parameters have changed, 14154cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi // called with a lock on mBufferSourceLock 14254cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi void hasNewDecodeParams(); 14354cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi 14413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Triviprivate: 14513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi DISALLOW_EVIL_CONSTRUCTORS(AudioSfDecoder); 14613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi 14713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi}; 14813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi 14913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi} // namespace android 150bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi 151bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi#endif // AUDIO_SF_DECODER_H_ 152