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