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
26struct NuPlayer::Decoder : public DecoderBase {
27    Decoder(const sp<AMessage> &notify,
28            const sp<Source> &source,
29            const sp<Renderer> &renderer = NULL,
30            const sp<NativeWindowWrapper> &nativeWindow = NULL,
31            const sp<CCDecoder> &ccDecoder = NULL);
32
33    virtual void getStats(
34            int64_t *mNumFramesTotal,
35            int64_t *mNumFramesDropped) const;
36
37protected:
38    virtual ~Decoder();
39
40    virtual void onMessageReceived(const sp<AMessage> &msg);
41
42    virtual void onConfigure(const sp<AMessage> &format);
43    virtual void onSetRenderer(const sp<Renderer> &renderer);
44    virtual void onGetInputBuffers(Vector<sp<ABuffer> > *dstBuffers);
45    virtual void onResume(bool notifyComplete);
46    virtual void onFlush(bool notifyComplete);
47    virtual void onShutdown(bool notifyComplete);
48    virtual void doRequestBuffers();
49
50private:
51    enum {
52        kWhatCodecNotify         = 'cdcN',
53        kWhatRenderBuffer        = 'rndr',
54    };
55
56    sp<NativeWindowWrapper> mNativeWindow;
57
58    sp<Source> mSource;
59    sp<Renderer> mRenderer;
60    sp<CCDecoder> mCCDecoder;
61
62    sp<AMessage> mInputFormat;
63    sp<AMessage> mOutputFormat;
64    sp<MediaCodec> mCodec;
65    sp<ALooper> mCodecLooper;
66
67    List<sp<AMessage> > mPendingInputMessages;
68
69    Vector<sp<ABuffer> > mInputBuffers;
70    Vector<sp<ABuffer> > mOutputBuffers;
71    Vector<sp<ABuffer> > mCSDsForCurrentFormat;
72    Vector<sp<ABuffer> > mCSDsToSubmit;
73    Vector<bool> mInputBufferIsDequeued;
74    Vector<MediaBuffer *> mMediaBuffers;
75    Vector<size_t> mDequeuedInputBuffers;
76
77    int64_t mSkipRenderingUntilMediaTimeUs;
78    int64_t mNumFramesTotal;
79    int64_t mNumFramesDropped;
80    bool mIsAudio;
81    bool mIsVideoAVC;
82    bool mIsSecure;
83    bool mFormatChangePending;
84
85    bool mPaused;
86    bool mResumePending;
87    AString mComponentName;
88
89    bool handleAnInputBuffer();
90    bool handleAnOutputBuffer();
91
92    void releaseAndResetMediaBuffers();
93    void requestCodecNotification();
94    bool isStaleReply(const sp<AMessage> &msg);
95
96    status_t fetchInputData(sp<AMessage> &reply);
97    bool onInputBufferFetched(const sp<AMessage> &msg);
98    void onRenderBuffer(const sp<AMessage> &msg);
99
100    bool supportsSeamlessFormatChange(const sp<AMessage> &to) const;
101    bool supportsSeamlessAudioFormatChange(const sp<AMessage> &targetFormat) const;
102    void rememberCodecSpecificData(const sp<AMessage> &format);
103
104    void notifyResumeCompleteIfNecessary();
105
106    DISALLOW_EVIL_CONSTRUCTORS(Decoder);
107};
108
109}  // namespace android
110
111#endif  // NUPLAYER_DECODER_H_
112