blob: 545ad70f60e06566070e71c788345087916ca0d3 [file] [log] [blame]
Pawin Vongmasa36653902018-11-15 00:10:25 -08001/*
2 * Copyright (C) 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 C_CODEC_H_
18#define C_CODEC_H_
19
20#include <chrono>
21#include <list>
22#include <memory>
23#include <set>
24
25#include <C2Component.h>
26#include <codec2/hidl/client.h>
27
28#include <android/native_window.h>
29#include <media/hardware/MetadataBufferType.h>
30#include <media/stagefright/foundation/Mutexed.h>
31#include <media/stagefright/CodecBase.h>
32#include <media/stagefright/FrameRenderTracker.h>
33#include <media/stagefright/MediaDefs.h>
34#include <media/stagefright/SkipCutBuffer.h>
35#include <utils/NativeHandle.h>
36#include <hardware/gralloc.h>
37#include <nativebase/nativebase.h>
38
39#include "CCodecConfig.h"
40
41namespace android {
42
43class CCodecBufferChannel;
44class InputSurfaceWrapper;
45struct MediaCodecInfo;
46
47class CCodec : public CodecBase {
48public:
49 CCodec();
50
51 virtual std::shared_ptr<BufferChannelBase> getBufferChannel() override;
52 virtual void initiateAllocateComponent(const sp<AMessage> &msg) override;
53 virtual void initiateConfigureComponent(const sp<AMessage> &msg) override;
54 virtual void initiateCreateInputSurface() override;
55 virtual void initiateSetInputSurface(const sp<PersistentSurface> &surface) override;
56 virtual void initiateStart() override;
57 virtual void initiateShutdown(bool keepComponentAllocated = false) override;
58
59 virtual status_t setSurface(const sp<Surface> &surface) override;
60
61 virtual void signalFlush() override;
62 virtual void signalResume() override;
63
64 virtual void signalSetParameters(const sp<AMessage> &params) override;
65 virtual void signalEndOfInputStream() override;
66 virtual void signalRequestIDRFrame() override;
67
68 void initiateReleaseIfStuck();
69 void onWorkDone(std::list<std::unique_ptr<C2Work>> &workItems,
70 size_t numDiscardedInputBuffers);
71 void onInputBufferDone(const std::shared_ptr<C2Buffer>& buffer);
72
73protected:
74 virtual ~CCodec();
75
76 virtual void onMessageReceived(const sp<AMessage> &msg) override;
77
78private:
79 typedef std::chrono::time_point<std::chrono::steady_clock> TimePoint;
80
81 status_t tryAndReportOnError(std::function<status_t()> job);
82
83 void initiateStop();
84 void initiateRelease(bool sendCallback = true);
85
86 void allocate(const sp<MediaCodecInfo> &codecInfo);
87 void configure(const sp<AMessage> &msg);
88 void start();
89 void stop();
90 void flush();
91 void release(bool sendCallback);
92
Lajos Molnar47118272019-01-31 16:28:04 -080093 /**
94 * Creates an input surface for the current device configuration compatible with CCodec.
95 * This could be backed by the C2 HAL or the OMX HAL.
96 */
97 static sp<PersistentSurface> CreateCompatibleInputSurface();
98
99 /// Creates an input surface to the OMX HAL
100 static sp<PersistentSurface> CreateOmxInputSurface();
101
102 /// handle a create input surface call
Pawin Vongmasa36653902018-11-15 00:10:25 -0800103 void createInputSurface();
104 void setInputSurface(const sp<PersistentSurface> &surface);
105 status_t setupInputSurface(const std::shared_ptr<InputSurfaceWrapper> &surface);
106 void setParameters(const sp<AMessage> &params);
107
108 void setDeadline(
109 const TimePoint &now,
110 const std::chrono::milliseconds &timeout,
111 const char *name);
112
113 void onWorkQueued(bool eos);
114 void subQueuedWorkCount(uint32_t count);
115
116 enum {
117 kWhatAllocate,
118 kWhatConfigure,
119 kWhatStart,
120 kWhatFlush,
121 kWhatStop,
122 kWhatRelease,
123 kWhatCreateInputSurface,
124 kWhatSetInputSurface,
125 kWhatSetParameters,
126
127 kWhatWorkDone,
128 kWhatWatch,
129 };
130
131 enum {
132 RELEASED,
133 ALLOCATED,
134 FLUSHED,
135 RUNNING,
136
137 ALLOCATING, // RELEASED -> ALLOCATED
138 STARTING, // ALLOCATED -> RUNNING
139 STOPPING, // RUNNING -> ALLOCATED
140 FLUSHING, // RUNNING -> FLUSHED
141 RESUMING, // FLUSHED -> RUNNING
142 RELEASING, // {ANY EXCEPT RELEASED} -> RELEASED
143 };
144
145 struct State {
146 inline State() : mState(RELEASED) {}
147 inline int get() const { return mState; }
148 inline void set(int newState) { mState = newState; }
149
150 std::shared_ptr<Codec2Client::Component> comp;
151 private:
152 int mState;
153 };
154
155 struct NamedTimePoint {
156 NamedTimePoint() : mTimePoint(TimePoint::max()), mName("") {}
157
158 inline void set(
159 const TimePoint &timePoint,
160 const char *name) {
161 mTimePoint = timePoint;
162 mName = name;
163 }
164
165 inline TimePoint get() const { return mTimePoint; }
166 inline const char *getName() const { return mName; }
167 private:
168 TimePoint mTimePoint;
169 const char *mName;
170 };
171
172 Mutexed<State> mState;
173 std::shared_ptr<CCodecBufferChannel> mChannel;
174
175 std::shared_ptr<Codec2Client> mClient;
176 std::shared_ptr<Codec2Client::Listener> mClientListener;
177 struct ClientListener;
178
179 Mutexed<NamedTimePoint> mDeadline;
180 std::atomic_int32_t mQueuedWorkCount;
181 Mutexed<NamedTimePoint> mQueueDeadline;
182 Mutexed<NamedTimePoint> mEosDeadline;
183 typedef CCodecConfig Config;
184 Mutexed<Config> mConfig;
185 Mutexed<std::list<std::unique_ptr<C2Work>>> mWorkDoneQueue;
186 Mutexed<std::list<size_t>> mNumDiscardedInputBuffersQueue;
187
188 friend class CCodecCallbackImpl;
189
190 DISALLOW_EVIL_CONSTRUCTORS(CCodec);
191};
192
193} // namespace android
194
195#endif // C_CODEC_H_