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