android_AudioSfDecoder.h revision 7f5cc1afe49395fefaad9b2bbd728a45d1bfda6a
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#include <media/stagefright/DataSource.h>
18#include <media/stagefright/MediaSource.h>
19#include <media/stagefright/FileSource.h>
20#include <media/stagefright/MediaDefs.h>
21#include <media/stagefright/MediaExtractor.h>
22#include <media/stagefright/MetaData.h>
23#include <media/stagefright/OMXClient.h>
24#include <media/stagefright/OMXCodec.h>
25#include "NuCachedSource2.h"
26#include "NuHTTPDataSource.h"
27#include "ThrottledSource.h"
28
29#include "android_GenericPlayer.h"
30#include "OpenSLES_AndroidMetadata.h"
31
32//--------------------------------------------------------------------------------------------------
33namespace android {
34
35// to keep in sync with the ANDROID_KEY_INDEX_PCMFORMAT_* constants in android_AudioSfDecoder.cpp
36static const char* const kPcmDecodeMetadataKeys[] = {
37        ANDROID_KEY_PCMFORMAT_NUMCHANNELS, ANDROID_KEY_PCMFORMAT_SAMPLESPERSEC,
38        ANDROID_KEY_PCMFORMAT_BITSPERSAMPLE, ANDROID_KEY_PCMFORMAT_CONTAINERSIZE,
39        ANDROID_KEY_PCMFORMAT_CHANNELMASK, ANDROID_KEY_PCMFORMAT_ENDIANNESS };
40#define NB_PCMMETADATA_KEYS (sizeof(kPcmDecodeMetadataKeys)/sizeof(kPcmDecodeMetadataKeys[0]))
41
42class AudioSfDecoder : public GenericPlayer
43{
44public:
45
46    AudioSfDecoder(const AudioPlayback_Parameters* params);
47    virtual ~AudioSfDecoder();
48
49    // overridden from GenericPlayer
50    virtual void play();
51
52    void startPrefetch_async();
53
54    uint32_t getPcmFormatKeyCount();
55    bool     getPcmFormatKeySize(uint32_t index, uint32_t* pKeySize);
56    bool     getPcmFormatKeyName(uint32_t index, uint32_t keySize, char* keyName);
57    bool     getPcmFormatValueSize(uint32_t index, uint32_t* pValueSize);
58    bool     getPcmFormatKeyValue(uint32_t index, uint32_t size, uint32_t* pValue);
59
60protected:
61
62    enum {
63        kWhatDecode       = 'deco',
64        kWhatRender       = 'rend',
65        kWhatCheckCache   = 'cach',
66        kWhatGetPcmFormat = 'gpcm'
67    };
68
69    // Async event handlers (called from the AudioSfDecoder's event loop)
70    void onDecode();
71    void onCheckCache(const sp<AMessage> &msg);
72    virtual void onRender();
73
74    // Async event handlers (called from GenericPlayer's event loop)
75    virtual void onPrepare();
76    virtual void onPlay();
77    virtual void onPause();
78    virtual void onSeek(const sp<AMessage> &msg);
79    virtual void onLoop(const sp<AMessage> &msg);
80    virtual void onGetPcmFormatKeyCount();
81
82    // overridden from GenericPlayer
83    virtual void onNotify(const sp<AMessage> &msg);
84    virtual void onMessageReceived(const sp<AMessage> &msg);
85
86    // to be implemented by subclasses of AudioSfDecoder to do something with the audio samples
87    virtual void createAudioSink() = 0;
88    virtual void updateAudioSink() = 0;
89    virtual void startAudioSink() = 0;
90    virtual void pauseAudioSink() = 0;
91
92    sp<DataSource> mDataSource;
93    sp<MediaSource> mAudioSource;
94
95    // negative values indicate invalid value
96    int64_t mBitrate;  // in bits/sec
97    uint32_t mChannelMask;
98    int64_t mDurationUsec;
99
100    // buffer passed from decoder to renderer
101    MediaBuffer *mDecodeBuffer;
102    // mutex used to protect the decode buffer
103    Mutex       mDecodeBufferLock;
104
105private:
106
107    void notifyPrepared(status_t prepareRes);
108
109    int64_t mTimeDelta;
110    int64_t mSeekTimeMsec;
111    int64_t mLastDecodedPositionUs;
112
113    // mutex used for seek flag and seek time read/write
114    Mutex mSeekLock;
115
116    // informations that can be retrieved in the PCM format queries
117    //  these values are only written in the event loop
118    uint32_t mPcmFormatKeyCount;
119    uint32_t mPcmFormatValues[NB_PCMMETADATA_KEYS];
120    // for synchronous "get" calls on the PCM decode format:
121    //    prevents concurrent "getPcmFormat" calls
122    Mutex       mGetPcmFormatLockSingleton;
123    //    lock order 1/ mGetPcmFormatLockSingleton  2/ mGetPcmFormatLock
124    Mutex       mGetPcmFormatLock;
125    Condition   mGetPcmFormatCondition;
126    bool        mGetPcmFormatKeyCount;
127
128    bool wantPrefetch();
129    CacheStatus_t getCacheRemaining(bool *eos);
130    int64_t getPositionUsec();
131
132private:
133    DISALLOW_EVIL_CONSTRUCTORS(AudioSfDecoder);
134
135};
136
137} // namespace android
138