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