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