NuPlayerDecoder.h revision 7e34bf5af26f8752d4786d3098740cdf51e2438f
1/*
2 * Copyright 2014 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#define NUPLAYER_DECODER_H_
19
20#include "NuPlayer.h"
21
22#include "NuPlayerDecoderBase.h"
23
24namespace android {
25
26class MediaCodecBuffer;
27
28struct NuPlayer::Decoder : public DecoderBase {
29    Decoder(const sp<AMessage> &notify,
30            const sp<Source> &source,
31            pid_t pid,
32            const sp<Renderer> &renderer = NULL,
33            const sp<Surface> &surface = NULL,
34            const sp<CCDecoder> &ccDecoder = NULL);
35
36    virtual sp<AMessage> getStats() const;
37
38    // sets the output surface of video decoders.
39    virtual status_t setVideoSurface(const sp<Surface> &surface);
40
41protected:
42    virtual ~Decoder();
43
44    virtual void onMessageReceived(const sp<AMessage> &msg);
45
46    virtual void onConfigure(const sp<AMessage> &format);
47    virtual void onSetParameters(const sp<AMessage> &params);
48    virtual void onSetRenderer(const sp<Renderer> &renderer);
49    virtual void onGetInputBuffers(Vector<sp<MediaCodecBuffer> > *dstBuffers);
50    virtual void onResume(bool notifyComplete);
51    virtual void onFlush();
52    virtual void onShutdown(bool notifyComplete);
53    virtual bool doRequestBuffers();
54
55private:
56    enum {
57        kWhatCodecNotify         = 'cdcN',
58        kWhatRenderBuffer        = 'rndr',
59        kWhatSetVideoSurface     = 'sSur'
60    };
61
62    enum {
63        kMaxNumVideoTemporalLayers = 32,
64    };
65
66    sp<Surface> mSurface;
67
68    sp<Source> mSource;
69    sp<Renderer> mRenderer;
70    sp<CCDecoder> mCCDecoder;
71
72    sp<AMessage> mInputFormat;
73    sp<AMessage> mOutputFormat;
74    sp<MediaCodec> mCodec;
75    sp<ALooper> mCodecLooper;
76
77    List<sp<AMessage> > mPendingInputMessages;
78
79    Vector<sp<MediaCodecBuffer> > mInputBuffers;
80    Vector<sp<MediaCodecBuffer> > mOutputBuffers;
81    Vector<sp<ABuffer> > mCSDsForCurrentFormat;
82    Vector<sp<ABuffer> > mCSDsToSubmit;
83    Vector<bool> mInputBufferIsDequeued;
84    Vector<MediaBuffer *> mMediaBuffers;
85    Vector<size_t> mDequeuedInputBuffers;
86
87    const pid_t mPid;
88    int64_t mSkipRenderingUntilMediaTimeUs;
89    int64_t mNumFramesTotal;
90    int64_t mNumInputFramesDropped;
91    int64_t mNumOutputFramesDropped;
92    int32_t mVideoWidth;
93    int32_t mVideoHeight;
94    bool mIsAudio;
95    bool mIsVideoAVC;
96    bool mIsSecure;
97    bool mFormatChangePending;
98    bool mTimeChangePending;
99    float mFrameRateTotal;
100    float mPlaybackSpeed;
101    int32_t mNumVideoTemporalLayerTotal;
102    int32_t mNumVideoTemporalLayerAllowed;
103    int32_t mCurrentMaxVideoTemporalLayerId;
104    float mVideoTemporalLayerAggregateFps[kMaxNumVideoTemporalLayers];
105
106    bool mResumePending;
107    AString mComponentName;
108
109    void handleError(int32_t err);
110    bool handleAnInputBuffer(size_t index);
111    bool handleAnOutputBuffer(
112            size_t index,
113            size_t offset,
114            size_t size,
115            int64_t timeUs,
116            int32_t flags);
117    void handleOutputFormatChange(const sp<AMessage> &format);
118
119    void releaseAndResetMediaBuffers();
120    void requestCodecNotification();
121    bool isStaleReply(const sp<AMessage> &msg);
122
123    void doFlush(bool notifyComplete);
124    status_t fetchInputData(sp<AMessage> &reply);
125    bool onInputBufferFetched(const sp<AMessage> &msg);
126    void onRenderBuffer(const sp<AMessage> &msg);
127
128    bool supportsSeamlessFormatChange(const sp<AMessage> &to) const;
129    bool supportsSeamlessAudioFormatChange(const sp<AMessage> &targetFormat) const;
130    void rememberCodecSpecificData(const sp<AMessage> &format);
131    bool isDiscontinuityPending() const;
132    void finishHandleDiscontinuity(bool flushOnTimeChange);
133
134    void notifyResumeCompleteIfNecessary();
135
136    DISALLOW_EVIL_CONSTRUCTORS(Decoder);
137};
138
139}  // namespace android
140
141#endif  // NUPLAYER_DECODER_H_
142