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 
24 namespace android {
25 
26 class MediaCodecBuffer;
27 
28 struct NuPlayer::Decoder : public DecoderBase {
29     Decoder(const sp<AMessage> &notify,
30             const sp<Source> &source,
31             pid_t pid,
32             uid_t uid,
33             const sp<Renderer> &renderer = NULL,
34             const sp<Surface> &surface = NULL,
35             const sp<CCDecoder> &ccDecoder = NULL);
36 
37     virtual sp<AMessage> getStats() const;
38 
39     // sets the output surface of video decoders.
40     virtual status_t setVideoSurface(const sp<Surface> &surface);
41 
42     virtual status_t releaseCrypto();
43 
44 protected:
45     virtual ~Decoder();
46 
47     virtual void onMessageReceived(const sp<AMessage> &msg);
48 
49     virtual void onConfigure(const sp<AMessage> &format);
50     virtual void onSetParameters(const sp<AMessage> &params);
51     virtual void onSetRenderer(const sp<Renderer> &renderer);
52     virtual void onResume(bool notifyComplete);
53     virtual void onFlush();
54     virtual void onShutdown(bool notifyComplete);
55     virtual bool doRequestBuffers();
56 
57 private:
58     enum {
59         kWhatCodecNotify         = 'cdcN',
60         kWhatRenderBuffer        = 'rndr',
61         kWhatSetVideoSurface     = 'sSur',
62         kWhatAudioOutputFormatChanged = 'aofc',
63         kWhatDrmReleaseCrypto    = 'rDrm',
64     };
65 
66     enum {
67         kMaxNumVideoTemporalLayers = 32,
68     };
69 
70     sp<Surface> mSurface;
71 
72     sp<Source> mSource;
73     sp<Renderer> mRenderer;
74     sp<CCDecoder> mCCDecoder;
75 
76     sp<AMessage> mInputFormat;
77     sp<AMessage> mOutputFormat;
78     sp<MediaCodec> mCodec;
79     sp<ALooper> mCodecLooper;
80 
81     List<sp<AMessage> > mPendingInputMessages;
82 
83     Vector<sp<MediaCodecBuffer> > mInputBuffers;
84     Vector<sp<MediaCodecBuffer> > mOutputBuffers;
85     Vector<sp<ABuffer> > mCSDsForCurrentFormat;
86     Vector<sp<ABuffer> > mCSDsToSubmit;
87     Vector<bool> mInputBufferIsDequeued;
88     Vector<MediaBuffer *> mMediaBuffers;
89     Vector<size_t> mDequeuedInputBuffers;
90 
91     const pid_t mPid;
92     const uid_t mUid;
93     int64_t mSkipRenderingUntilMediaTimeUs;
94     int64_t mNumFramesTotal;
95     int64_t mNumInputFramesDropped;
96     int64_t mNumOutputFramesDropped;
97     int32_t mVideoWidth;
98     int32_t mVideoHeight;
99     bool mIsAudio;
100     bool mIsVideoAVC;
101     bool mIsSecure;
102     bool mIsEncrypted;
103     bool mIsEncryptedObservedEarlier;
104     bool mFormatChangePending;
105     bool mTimeChangePending;
106     float mFrameRateTotal;
107     float mPlaybackSpeed;
108     int32_t mNumVideoTemporalLayerTotal;
109     int32_t mNumVideoTemporalLayerAllowed;
110     int32_t mCurrentMaxVideoTemporalLayerId;
111     float mVideoTemporalLayerAggregateFps[kMaxNumVideoTemporalLayers];
112 
113     bool mResumePending;
114     AString mComponentName;
115 
116     void handleError(int32_t err);
117     bool handleAnInputBuffer(size_t index);
118     bool handleAnOutputBuffer(
119             size_t index,
120             size_t offset,
121             size_t size,
122             int64_t timeUs,
123             int32_t flags);
124     void handleOutputFormatChange(const sp<AMessage> &format);
125 
126     void releaseAndResetMediaBuffers();
127     void requestCodecNotification();
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  // NUPLAYER_DECODER_H_
151