Home | History | Annotate | Download | only in android
      1 /*
      2  * Copyright (C) 2011 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 #ifndef AUDIO_SF_DECODER_H_
     18 #define AUDIO_SF_DECODER_H_
     19 
     20 #include <media/DataSource.h>
     21 #include <media/MediaSource.h>
     22 #include <media/MediaExtractor.h>
     23 #include <media/stagefright/FileSource.h>
     24 #include <media/stagefright/MediaDefs.h>
     25 #include <media/stagefright/MetaData.h>
     26 #include "NuCachedSource2.h"
     27 #include "ThrottledSource.h"
     28 
     29 #include "android_GenericPlayer.h"
     30 
     31 //--------------------------------------------------------------------------------------------------
     32 namespace android {
     33 
     34 // keep in sync with the entries of kPcmDecodeMetadataKeys[]
     35 #define ANDROID_KEY_INDEX_PCMFORMAT_NUMCHANNELS   0
     36 #define ANDROID_KEY_INDEX_PCMFORMAT_SAMPLERATE    1
     37 #define ANDROID_KEY_INDEX_PCMFORMAT_BITSPERSAMPLE 2
     38 #define ANDROID_KEY_INDEX_PCMFORMAT_CONTAINERSIZE 3
     39 #define ANDROID_KEY_INDEX_PCMFORMAT_CHANNELMASK   4
     40 #define ANDROID_KEY_INDEX_PCMFORMAT_ENDIANNESS    5
     41 
     42 // to keep in sync with the ANDROID_KEY_INDEX_PCMFORMAT_* constants in android_AudioSfDecoder.cpp
     43 static const char* const kPcmDecodeMetadataKeys[] = {
     44         ANDROID_KEY_PCMFORMAT_NUMCHANNELS, ANDROID_KEY_PCMFORMAT_SAMPLERATE,
     45         ANDROID_KEY_PCMFORMAT_BITSPERSAMPLE, ANDROID_KEY_PCMFORMAT_CONTAINERSIZE,
     46         ANDROID_KEY_PCMFORMAT_CHANNELMASK, ANDROID_KEY_PCMFORMAT_ENDIANNESS };
     47 #define NB_PCMMETADATA_KEYS (sizeof(kPcmDecodeMetadataKeys)/sizeof(kPcmDecodeMetadataKeys[0]))
     48 
     49 // abstract base class for AudioToCbRenderer and it's subclasses
     50 class AudioSfDecoder : public GenericPlayer
     51 {
     52 public:
     53 
     54     explicit AudioSfDecoder(const AudioPlayback_Parameters* params);
     55     virtual ~AudioSfDecoder();
     56 
     57     virtual void preDestroy();
     58 
     59     // overridden from GenericPlayer
     60     virtual void play();
     61     virtual void getPositionMsec(int* msec); //msec != NULL, ANDROID_UNKNOWN_TIME if unknown
     62 
     63     uint32_t getPcmFormatKeyCount() const;
     64     bool     getPcmFormatKeySize(uint32_t index, uint32_t* pKeySize);
     65     bool     getPcmFormatKeyName(uint32_t index, uint32_t keySize, char* keyName);
     66     bool     getPcmFormatValueSize(uint32_t index, uint32_t* pValueSize);
     67     bool     getPcmFormatKeyValue(uint32_t index, uint32_t size, uint32_t* pValue);
     68 
     69 protected:
     70 
     71     enum {
     72         kWhatDecode       = 'deco',
     73         kWhatRender       = 'rend',
     74         kWhatCheckCache   = 'cach'
     75     };
     76 
     77     // Async event handlers (called from the AudioSfDecoder's event loop)
     78     void onDecode();
     79     void onCheckCache(const sp<AMessage> &msg);
     80     virtual void onRender() = 0;
     81 
     82     // Async event handlers (called from GenericPlayer's event loop)
     83     virtual void onPrepare();
     84     virtual void onPlay();
     85     virtual void onPause();
     86     virtual void onSeek(const sp<AMessage> &msg);
     87     virtual void onLoop(const sp<AMessage> &msg);
     88 
     89     // overridden from GenericPlayer
     90     virtual void onNotify(const sp<AMessage> &msg);
     91     virtual void onMessageReceived(const sp<AMessage> &msg);
     92 
     93     // to be implemented by subclasses of AudioSfDecoder to do something with the audio samples
     94     // (called from GenericPlayer's event loop)
     95     virtual void createAudioSink() = 0;
     96     virtual void updateAudioSink() = 0; // called with mBufferSourceLock held
     97     virtual void startAudioSink() = 0;
     98     virtual void pauseAudioSink() = 0;
     99 
    100     sp<DataSource>  mDataSource; // where the raw data comes from
    101     sp<MediaSource> mAudioSource;// the decoder reading from the data source
    102     // used to indicate mAudioSource was successfully started, but wasn't stopped
    103     bool            mAudioSourceStarted;
    104 
    105     // negative values indicate invalid value
    106     int64_t mBitrate;  // in bits/sec
    107     int64_t mDurationUsec; // ANDROID_UNKNOWN_TIME if unknown
    108 
    109     // buffer passed from decoder to renderer
    110     MediaBufferBase *mDecodeBuffer;
    111 
    112     // mutex used to protect the decode buffer, the audio source and its running state
    113     Mutex       mBufferSourceLock;
    114 
    115     void notifyPrepared(status_t prepareRes);
    116 
    117     int64_t mSeekTimeMsec;
    118     int64_t mLastDecodedPositionUs; // ANDROID_UNKNOWN_TIME if unknown
    119     // mutex used for seek flag, seek time (mSeekTimeMsec),
    120     //   and last decoded position (mLastDecodedPositionUs)
    121     Mutex mTimeLock;
    122 
    123     // informations that can be retrieved in the PCM format queries
    124     //  these values are only written in the event loop
    125     uint32_t mPcmFormatValues[NB_PCMMETADATA_KEYS];
    126     // protects mPcmFormatValues
    127     Mutex    mPcmFormatLock;
    128 
    129     virtual bool advancesPositionInRealTime() const { return false; }
    130 
    131 private:
    132     bool wantPrefetch();
    133     CacheStatus_t getCacheRemaining(bool *eos);
    134     int64_t getPositionUsec(); // ANDROID_UNKNOWN_TIME if unknown
    135 
    136     // convenience function to update internal state when decoding parameters have changed,
    137     // called with a lock on mBufferSourceLock
    138     void hasNewDecodeParams();
    139 
    140     static bool isSupportedCodec(const char* mime);
    141 
    142 private:
    143     DISALLOW_EVIL_CONSTRUCTORS(AudioSfDecoder);
    144 
    145 };
    146 
    147 } // namespace android
    148 
    149 #endif // AUDIO_SF_DECODER_H_
    150