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/stagefright/MediaSource.h>
22 
23 #include "android_GenericPlayer.h"
24 
25 //--------------------------------------------------------------------------------------------------
26 namespace android {
27 
28 // keep in sync with the entries of kPcmDecodeMetadataKeys[]
29 #define ANDROID_KEY_INDEX_PCMFORMAT_NUMCHANNELS   0
30 #define ANDROID_KEY_INDEX_PCMFORMAT_SAMPLERATE    1
31 #define ANDROID_KEY_INDEX_PCMFORMAT_BITSPERSAMPLE 2
32 #define ANDROID_KEY_INDEX_PCMFORMAT_CONTAINERSIZE 3
33 #define ANDROID_KEY_INDEX_PCMFORMAT_CHANNELMASK   4
34 #define ANDROID_KEY_INDEX_PCMFORMAT_ENDIANNESS    5
35 
36 // to keep in sync with the ANDROID_KEY_INDEX_PCMFORMAT_* constants in android_AudioSfDecoder.cpp
37 static const char* const kPcmDecodeMetadataKeys[] = {
38         ANDROID_KEY_PCMFORMAT_NUMCHANNELS, ANDROID_KEY_PCMFORMAT_SAMPLERATE,
39         ANDROID_KEY_PCMFORMAT_BITSPERSAMPLE, ANDROID_KEY_PCMFORMAT_CONTAINERSIZE,
40         ANDROID_KEY_PCMFORMAT_CHANNELMASK, ANDROID_KEY_PCMFORMAT_ENDIANNESS };
41 #define NB_PCMMETADATA_KEYS (sizeof(kPcmDecodeMetadataKeys)/sizeof(kPcmDecodeMetadataKeys[0]))
42 
43 // abstract base class for AudioToCbRenderer and it's subclasses
44 class AudioSfDecoder : public GenericPlayer
45 {
46 public:
47 
48     explicit AudioSfDecoder(const AudioPlayback_Parameters* params);
49     virtual ~AudioSfDecoder();
50 
51     virtual void preDestroy();
52 
53     // overridden from GenericPlayer
54     virtual void play();
55     virtual void getPositionMsec(int* msec); //msec != NULL, ANDROID_UNKNOWN_TIME if unknown
56 
57     uint32_t getPcmFormatKeyCount() const;
58     bool     getPcmFormatKeySize(uint32_t index, uint32_t* pKeySize);
59     bool     getPcmFormatKeyName(uint32_t index, uint32_t keySize, char* keyName);
60     bool     getPcmFormatValueSize(uint32_t index, uint32_t* pValueSize);
61     bool     getPcmFormatKeyValue(uint32_t index, uint32_t size, uint32_t* pValue);
62 
63 protected:
64 
65     enum {
66         kWhatDecode       = 'deco',
67         kWhatRender       = 'rend',
68         kWhatCheckCache   = 'cach'
69     };
70 
71     // Async event handlers (called from the AudioSfDecoder's event loop)
72     void onDecode();
73     void onCheckCache(const sp<AMessage> &msg);
74     virtual void onRender() = 0;
75 
76     // Async event handlers (called from GenericPlayer's event loop)
77     virtual void onPrepare();
78     virtual void onPlay();
79     virtual void onPause();
80     virtual void onSeek(const sp<AMessage> &msg);
81     virtual void onLoop(const sp<AMessage> &msg);
82 
83     // overridden from GenericPlayer
84     virtual void onNotify(const sp<AMessage> &msg);
85     virtual void onMessageReceived(const sp<AMessage> &msg);
86 
87     // to be implemented by subclasses of AudioSfDecoder to do something with the audio samples
88     // (called from GenericPlayer's event loop)
89     virtual void createAudioSink() = 0;
90     virtual void updateAudioSink() = 0; // called with mBufferSourceLock held
91     virtual void startAudioSink() = 0;
92     virtual void pauseAudioSink() = 0;
93 
94     sp<DataSource>  mDataSource; // where the raw data comes from
95     sp<MediaSource> mAudioSource;// the decoder reading from the data source
96     // used to indicate mAudioSource was successfully started, but wasn't stopped
97     bool            mAudioSourceStarted;
98 
99     // negative values indicate invalid value
100     int64_t mBitrate;  // in bits/sec
101     int64_t mDurationUsec; // ANDROID_UNKNOWN_TIME if unknown
102 
103     // buffer passed from decoder to renderer
104     MediaBufferBase *mDecodeBuffer;
105 
106     // mutex used to protect the decode buffer, the audio source and its running state
107     Mutex       mBufferSourceLock;
108 
109     void notifyPrepared(status_t prepareRes);
110 
111     int64_t mSeekTimeMsec;
112     int64_t mLastDecodedPositionUs; // ANDROID_UNKNOWN_TIME if unknown
113     // mutex used for seek flag, seek time (mSeekTimeMsec),
114     //   and last decoded position (mLastDecodedPositionUs)
115     Mutex mTimeLock;
116 
117     // informations that can be retrieved in the PCM format queries
118     //  these values are only written in the event loop
119     uint32_t mPcmFormatValues[NB_PCMMETADATA_KEYS];
120     // protects mPcmFormatValues
121     Mutex    mPcmFormatLock;
122 
advancesPositionInRealTime()123     virtual bool advancesPositionInRealTime() const { return false; }
124 
125 private:
126     bool wantPrefetch();
127     CacheStatus_t getCacheRemaining(bool *eos);
128     int64_t getPositionUsec(); // ANDROID_UNKNOWN_TIME if unknown
129 
130     // convenience function to update internal state when decoding parameters have changed,
131     // called with a lock on mBufferSourceLock
132     void hasNewDecodeParams();
133 
134     static bool isSupportedCodec(const char* mime);
135 
136 private:
137     DISALLOW_EVIL_CONSTRUCTORS(AudioSfDecoder);
138 
139 };
140 
141 } // namespace android
142 
143 #endif // AUDIO_SF_DECODER_H_
144