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 "NuCachedSource2.h"
274ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi#include "ThrottledSource.h"
284ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi
294ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi#include "android_GenericPlayer.h"
3013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
3113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi//--------------------------------------------------------------------------------------------------
3213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivinamespace android {
3313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
34bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi// keep in sync with the entries of kPcmDecodeMetadataKeys[]
35bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi#define ANDROID_KEY_INDEX_PCMFORMAT_NUMCHANNELS   0
360f92f48017588949daf7d24a339423e149bb2555Glenn Kasten#define ANDROID_KEY_INDEX_PCMFORMAT_SAMPLERATE    1
37bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi#define ANDROID_KEY_INDEX_PCMFORMAT_BITSPERSAMPLE 2
38bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi#define ANDROID_KEY_INDEX_PCMFORMAT_CONTAINERSIZE 3
39bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi#define ANDROID_KEY_INDEX_PCMFORMAT_CHANNELMASK   4
40bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi#define ANDROID_KEY_INDEX_PCMFORMAT_ENDIANNESS    5
41bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
427f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi// to keep in sync with the ANDROID_KEY_INDEX_PCMFORMAT_* constants in android_AudioSfDecoder.cpp
437f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivistatic const char* const kPcmDecodeMetadataKeys[] = {
440f92f48017588949daf7d24a339423e149bb2555Glenn Kasten        ANDROID_KEY_PCMFORMAT_NUMCHANNELS, ANDROID_KEY_PCMFORMAT_SAMPLERATE,
457f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        ANDROID_KEY_PCMFORMAT_BITSPERSAMPLE, ANDROID_KEY_PCMFORMAT_CONTAINERSIZE,
467f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        ANDROID_KEY_PCMFORMAT_CHANNELMASK, ANDROID_KEY_PCMFORMAT_ENDIANNESS };
477f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi#define NB_PCMMETADATA_KEYS (sizeof(kPcmDecodeMetadataKeys)/sizeof(kPcmDecodeMetadataKeys[0]))
487f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
497349b2e742b2cedc6d149fac62ed661ad7d47decGlenn Kasten// abstract base class for AudioToCbRenderer and it's subclasses
5013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Triviclass AudioSfDecoder : public GenericPlayer
5113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi{
5213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivipublic:
5313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
5413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    AudioSfDecoder(const AudioPlayback_Parameters* params);
5513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    virtual ~AudioSfDecoder();
5613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
57e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    virtual void preDestroy();
58e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi
5913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    // overridden from GenericPlayer
6013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    virtual void play();
615050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi    virtual void getPositionMsec(int* msec); //msec != NULL, ANDROID_UNKNOWN_TIME if unknown
6213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
6391540f92d7f1bcda423859af6bd82df083c2afabGlenn Kasten    uint32_t getPcmFormatKeyCount() const;
647f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    bool     getPcmFormatKeySize(uint32_t index, uint32_t* pKeySize);
657f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    bool     getPcmFormatKeyName(uint32_t index, uint32_t keySize, char* keyName);
667f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    bool     getPcmFormatValueSize(uint32_t index, uint32_t* pValueSize);
677f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    bool     getPcmFormatKeyValue(uint32_t index, uint32_t size, uint32_t* pValue);
687f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
6913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Triviprotected:
7013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
7113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    enum {
727f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        kWhatDecode       = 'deco',
737f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi        kWhatRender       = 'rend',
7454cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi        kWhatCheckCache   = 'cach'
7513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    };
7613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
7713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    // Async event handlers (called from the AudioSfDecoder's event loop)
7813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    void onDecode();
7913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    void onCheckCache(const sp<AMessage> &msg);
807349b2e742b2cedc6d149fac62ed661ad7d47decGlenn Kasten    virtual void onRender() = 0;
8113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
8213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    // Async event handlers (called from GenericPlayer's event loop)
8313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    virtual void onPrepare();
8413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    virtual void onPlay();
8513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    virtual void onPause();
8613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    virtual void onSeek(const sp<AMessage> &msg);
8713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    virtual void onLoop(const sp<AMessage> &msg);
8813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
8913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    // overridden from GenericPlayer
9013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    virtual void onNotify(const sp<AMessage> &msg);
9113837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    virtual void onMessageReceived(const sp<AMessage> &msg);
9213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
9313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    // to be implemented by subclasses of AudioSfDecoder to do something with the audio samples
9454cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    // (called from GenericPlayer's event loop)
9513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    virtual void createAudioSink() = 0;
9654cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    virtual void updateAudioSink() = 0; // called with mBufferSourceLock held
9713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    virtual void startAudioSink() = 0;
9813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    virtual void pauseAudioSink() = 0;
9913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
100e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    sp<DataSource>  mDataSource; // where the raw data comes from
1012cf0a2ee3672659f922bf0df4368b5a49148c5ffMarco Nelissen    sp<IMediaSource> mAudioSource;// the decoder reading from the data source
102e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    // used to indicate mAudioSource was successfully started, but wasn't stopped
103e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    bool            mAudioSourceStarted;
10413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
10513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    // negative values indicate invalid value
10613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    int64_t mBitrate;  // in bits/sec
1075050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi    int64_t mDurationUsec; // ANDROID_UNKNOWN_TIME if unknown
10813837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
10913837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    // buffer passed from decoder to renderer
11013837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    MediaBuffer *mDecodeBuffer;
111e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi
112e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    // mutex used to protect the decode buffer, the audio source and its running state
113e6ded5c61944a87fa9e472dec3a6929855d42aebJean-Michel Trivi    Mutex       mBufferSourceLock;
11413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
11513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    void notifyPrepared(status_t prepareRes);
11613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
11713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    int64_t mSeekTimeMsec;
1185050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi    int64_t mLastDecodedPositionUs; // ANDROID_UNKNOWN_TIME if unknown
1195050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi    // mutex used for seek flag, seek time (mSeekTimeMsec),
1205050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi    //   and last decoded position (mLastDecodedPositionUs)
1215050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi    Mutex mTimeLock;
12213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
1237f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    // informations that can be retrieved in the PCM format queries
1247f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    //  these values are only written in the event loop
1257f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi    uint32_t mPcmFormatValues[NB_PCMMETADATA_KEYS];
12691540f92d7f1bcda423859af6bd82df083c2afabGlenn Kasten    // protects mPcmFormatValues
12754cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    Mutex    mPcmFormatLock;
1287f5cc1afe49395fefaad9b2bbd728a45d1bfda6aJean-Michel Trivi
1297349b2e742b2cedc6d149fac62ed661ad7d47decGlenn Kasten    virtual bool advancesPositionInRealTime() const { return false; }
1307349b2e742b2cedc6d149fac62ed661ad7d47decGlenn Kasten
131bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Triviprivate:
13213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    bool wantPrefetch();
1334ee246c55533bdab8ab5fa0f0581744fe58e7c91Jean-Michel Trivi    CacheStatus_t getCacheRemaining(bool *eos);
1345050a75e342ce45794d56666cddde3d46472acc7Jean-Michel Trivi    int64_t getPositionUsec(); // ANDROID_UNKNOWN_TIME if unknown
13513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
13654cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    // convenience function to update internal state when decoding parameters have changed,
13754cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    // called with a lock on mBufferSourceLock
13854cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi    void hasNewDecodeParams();
13954cad4f35a090a06e655fcc9e072e1d38f9e7689Jean-Michel Trivi
1403597268c2bf4ff71521e3cbe522d7ee02c41f175Jean-Michel Trivi    static bool isSupportedCodec(const char* mime);
1413597268c2bf4ff71521e3cbe522d7ee02c41f175Jean-Michel Trivi
14213837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Triviprivate:
14313837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi    DISALLOW_EVIL_CONSTRUCTORS(AudioSfDecoder);
14413837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
14513837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi};
14613837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi
14713837cf3f7be0eb8b1a9552bd99a89f98c987720Jean-Michel Trivi} // namespace android
148bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi
149bb832e853d4afb11b0a3287b2eb0cad87696d631Jean-Michel Trivi#endif // AUDIO_SF_DECODER_H_
150