NuPlayerDecoder.h revision 2245fc625910e47d1ba3c339e205c21ab58a47ad
1/*
2 * Copyright (C) 2010 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 NUPLAYER_DECODER_H_
18
19#define NUPLAYER_DECODER_H_
20
21#include "NuPlayer.h"
22
23#include <media/stagefright/foundation/AHandler.h>
24
25namespace android {
26
27struct ABuffer;
28struct MediaCodec;
29struct MediaBuffer;
30
31struct NuPlayer::Decoder : public AHandler {
32    Decoder(const sp<AMessage> &notify,
33            const sp<NativeWindowWrapper> &nativeWindow = NULL);
34
35    virtual void configure(const sp<AMessage> &format);
36    virtual void init();
37
38    status_t getInputBuffers(Vector<sp<ABuffer> > *dstBuffers) const;
39    virtual void signalFlush(const sp<AMessage> &format = NULL);
40    virtual void signalUpdateFormat(const sp<AMessage> &format);
41    virtual void signalResume();
42    virtual void initiateShutdown();
43
44    virtual bool supportsSeamlessFormatChange(const sp<AMessage> &to) const;
45
46    enum {
47        kWhatFillThisBuffer      = 'flTB',
48        kWhatDrainThisBuffer     = 'drTB',
49        kWhatOutputFormatChanged = 'fmtC',
50        kWhatFlushCompleted      = 'flsC',
51        kWhatShutdownCompleted   = 'shDC',
52        kWhatEOS                 = 'eos ',
53        kWhatError               = 'err ',
54    };
55
56protected:
57
58    virtual ~Decoder();
59
60    virtual void onMessageReceived(const sp<AMessage> &msg);
61
62private:
63    enum {
64        kWhatCodecNotify        = 'cdcN',
65        kWhatConfigure          = 'conf',
66        kWhatGetInputBuffers    = 'gInB',
67        kWhatInputBufferFilled  = 'inpF',
68        kWhatRenderBuffer       = 'rndr',
69        kWhatFlush              = 'flus',
70        kWhatShutdown           = 'shuD',
71        kWhatUpdateFormat       = 'uFmt',
72    };
73
74    sp<AMessage> mNotify;
75    sp<NativeWindowWrapper> mNativeWindow;
76
77    sp<AMessage> mInputFormat;
78    sp<AMessage> mOutputFormat;
79    sp<MediaCodec> mCodec;
80    sp<ALooper> mCodecLooper;
81    sp<ALooper> mDecoderLooper;
82
83    List<sp<AMessage> > mPendingInputMessages;
84
85    Vector<sp<ABuffer> > mInputBuffers;
86    Vector<sp<ABuffer> > mOutputBuffers;
87    Vector<sp<ABuffer> > mCSDsForCurrentFormat;
88    Vector<sp<ABuffer> > mCSDsToSubmit;
89    Vector<bool> mInputBufferIsDequeued;
90    Vector<MediaBuffer *> mMediaBuffers;
91
92    void handleError(int32_t err);
93    bool handleAnInputBuffer();
94    bool handleAnOutputBuffer();
95
96    void releaseAndResetMediaBuffers();
97    void requestCodecNotification();
98    bool isStaleReply(const sp<AMessage> &msg);
99
100    void onConfigure(const sp<AMessage> &format);
101    void onFlush();
102    void onResume();
103    bool onInputBufferFilled(const sp<AMessage> &msg);
104    void onRenderBuffer(const sp<AMessage> &msg);
105    void onShutdown();
106
107    int32_t mBufferGeneration;
108    bool mPaused;
109    AString mComponentName;
110
111    bool supportsSeamlessAudioFormatChange(const sp<AMessage> &targetFormat) const;
112    void rememberCodecSpecificData(const sp<AMessage> &format);
113
114    DISALLOW_EVIL_CONSTRUCTORS(Decoder);
115};
116
117struct NuPlayer::CCDecoder : public RefBase {
118    enum {
119        kWhatClosedCaptionData,
120        kWhatTrackAdded,
121    };
122
123    CCDecoder(const sp<AMessage> &notify);
124
125    size_t getTrackCount() const;
126    sp<AMessage> getTrackInfo(size_t index) const;
127    status_t selectTrack(size_t index, bool select);
128    bool isSelected() const;
129    void decode(const sp<ABuffer> &accessUnit);
130    void display(int64_t timeUs);
131    void flush();
132
133private:
134    sp<AMessage> mNotify;
135    KeyedVector<int64_t, sp<ABuffer> > mCCMap;
136    size_t mCurrentChannel;
137    int32_t mSelectedTrack;
138    int32_t mTrackIndices[4];
139    Vector<size_t> mFoundChannels;
140
141    bool isTrackValid(size_t index) const;
142    int32_t getTrackIndex(size_t channel) const;
143    bool extractFromSEI(const sp<ABuffer> &accessUnit);
144    sp<ABuffer> filterCCBuf(const sp<ABuffer> &ccBuf, size_t index);
145
146    DISALLOW_EVIL_CONSTRUCTORS(CCDecoder);
147};
148
149}  // namespace android
150
151#endif  // NUPLAYER_DECODER_H_
152