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