blob: ae5767888a22b7948d5cbca807657720724475ce [file] [log] [blame]
Pawin Vongmasa36653902018-11-15 00:10:25 -08001/*
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 CCODEC_BUFFER_CHANNEL_H_
18
19#define CCODEC_BUFFER_CHANNEL_H_
20
21#include <map>
22#include <memory>
23#include <vector>
24
25#include <C2Buffer.h>
26#include <C2Component.h>
27#include <Codec2Mapper.h>
28
29#include <codec2/hidl/client.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080030#include <media/stagefright/foundation/Mutexed.h>
31#include <media/stagefright/CodecBase.h>
32#include <media/ICrypto.h>
33
Wonsik Kim469c8342019-04-11 16:46:09 -070034#include "CCodecBuffers.h"
Pawin Vongmasa36653902018-11-15 00:10:25 -080035#include "InputSurfaceWrapper.h"
Wonsik Kimab34ed62019-01-31 15:28:46 -080036#include "PipelineWatcher.h"
Pawin Vongmasa36653902018-11-15 00:10:25 -080037
38namespace android {
39
Wonsik Kim078b58e2019-01-09 15:08:06 -080040class MemoryDealer;
41
Pawin Vongmasa36653902018-11-15 00:10:25 -080042class CCodecCallback {
43public:
44 virtual ~CCodecCallback() = default;
45 virtual void onError(status_t err, enum ActionCode actionCode) = 0;
46 virtual void onOutputFramesRendered(int64_t mediaTimeUs, nsecs_t renderTimeNs) = 0;
Pawin Vongmasa36653902018-11-15 00:10:25 -080047 virtual void onOutputBuffersChanged() = 0;
48};
49
50/**
51 * BufferChannelBase implementation for CCodec.
52 */
53class CCodecBufferChannel
54 : public BufferChannelBase, public std::enable_shared_from_this<CCodecBufferChannel> {
55public:
56 explicit CCodecBufferChannel(const std::shared_ptr<CCodecCallback> &callback);
57 virtual ~CCodecBufferChannel();
58
59 // BufferChannelBase interface
60 virtual status_t queueInputBuffer(const sp<MediaCodecBuffer> &buffer) override;
61 virtual status_t queueSecureInputBuffer(
62 const sp<MediaCodecBuffer> &buffer,
63 bool secure,
64 const uint8_t *key,
65 const uint8_t *iv,
66 CryptoPlugin::Mode mode,
67 CryptoPlugin::Pattern pattern,
68 const CryptoPlugin::SubSample *subSamples,
69 size_t numSubSamples,
70 AString *errorDetailMsg) override;
71 virtual status_t renderOutputBuffer(
72 const sp<MediaCodecBuffer> &buffer, int64_t timestampNs) override;
73 virtual status_t discardBuffer(const sp<MediaCodecBuffer> &buffer) override;
74 virtual void getInputBufferArray(Vector<sp<MediaCodecBuffer>> *array) override;
75 virtual void getOutputBufferArray(Vector<sp<MediaCodecBuffer>> *array) override;
76
77 // Methods below are interface for CCodec to use.
78
79 /**
80 * Set the component object for buffer processing.
81 */
82 void setComponent(const std::shared_ptr<Codec2Client::Component> &component);
83
84 /**
85 * Set output graphic surface for rendering.
86 */
87 status_t setSurface(const sp<Surface> &surface);
88
89 /**
90 * Set GraphicBufferSource object from which the component extracts input
91 * buffers.
92 */
93 status_t setInputSurface(const std::shared_ptr<InputSurfaceWrapper> &surface);
94
95 /**
96 * Signal EOS to input surface.
97 */
98 status_t signalEndOfInputStream();
99
100 /**
101 * Set parameters.
102 */
103 status_t setParameters(std::vector<std::unique_ptr<C2Param>> &params);
104
105 /**
106 * Start queueing buffers to the component. This object should never queue
107 * buffers before this call has completed.
108 */
109 status_t start(const sp<AMessage> &inputFormat, const sp<AMessage> &outputFormat);
110
111 /**
112 * Request initial input buffers to be filled by client.
113 */
114 status_t requestInitialInputBuffers();
115
116 /**
117 * Stop queueing buffers to the component. This object should never queue
118 * buffers after this call, until start() is called.
119 */
120 void stop();
121
122 void flush(const std::list<std::unique_ptr<C2Work>> &flushedWork);
123
124 /**
125 * Notify input client about work done.
126 *
127 * @param workItems finished work item.
128 * @param outputFormat new output format if it has changed, otherwise nullptr
129 * @param initData new init data (CSD) if it has changed, otherwise nullptr
Pawin Vongmasa36653902018-11-15 00:10:25 -0800130 */
131 void onWorkDone(
132 std::unique_ptr<C2Work> work, const sp<AMessage> &outputFormat,
Wonsik Kimab34ed62019-01-31 15:28:46 -0800133 const C2StreamInitDataInfo::output *initData);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800134
135 /**
136 * Make an input buffer available for the client as it is no longer needed
137 * by the codec.
138 *
Wonsik Kimab34ed62019-01-31 15:28:46 -0800139 * @param frameIndex The index of input work
140 * @param arrayIndex The index of buffer in the input work buffers.
Pawin Vongmasa36653902018-11-15 00:10:25 -0800141 */
Wonsik Kimab34ed62019-01-31 15:28:46 -0800142 void onInputBufferDone(uint64_t frameIndex, size_t arrayIndex);
143
144 PipelineWatcher::Clock::duration elapsed();
Pawin Vongmasa36653902018-11-15 00:10:25 -0800145
146 enum MetaMode {
147 MODE_NONE,
148 MODE_ANW,
149 };
150
151 void setMetaMode(MetaMode mode);
152
Pawin Vongmasa36653902018-11-15 00:10:25 -0800153private:
154 class QueueGuard;
155
156 /**
157 * Special mutex-like object with the following properties:
158 *
159 * - At STOPPED state (initial, or after stop())
160 * - QueueGuard object gets created at STOPPED state, and the client is
161 * supposed to return immediately.
162 * - At RUNNING state (after start())
163 * - Each QueueGuard object
164 */
165 class QueueSync {
166 public:
167 /**
168 * At construction the sync object is in STOPPED state.
169 */
170 inline QueueSync() {}
171 ~QueueSync() = default;
172
173 /**
174 * Transition to RUNNING state when stopped. No-op if already in RUNNING
175 * state.
176 */
177 void start();
178
179 /**
180 * At RUNNING state, wait until all QueueGuard object created during
181 * RUNNING state are destroyed, and then transition to STOPPED state.
182 * No-op if already in STOPPED state.
183 */
184 void stop();
185
186 private:
187 Mutex mGuardLock;
188
189 struct Counter {
190 inline Counter() : value(-1) {}
191 int32_t value;
192 Condition cond;
193 };
194 Mutexed<Counter> mCount;
195
196 friend class CCodecBufferChannel::QueueGuard;
197 };
198
199 class QueueGuard {
200 public:
201 QueueGuard(QueueSync &sync);
202 ~QueueGuard();
203 inline bool isRunning() { return mRunning; }
204
205 private:
206 QueueSync &mSync;
207 bool mRunning;
208 };
209
210 void feedInputBufferIfAvailable();
211 void feedInputBufferIfAvailableInternal();
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700212 status_t queueInputBufferInternal(sp<MediaCodecBuffer> buffer);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800213 bool handleWork(
214 std::unique_ptr<C2Work> work, const sp<AMessage> &outputFormat,
215 const C2StreamInitDataInfo::output *initData);
216 void sendOutputBuffers();
217
218 QueueSync mSync;
219 sp<MemoryDealer> mDealer;
220 sp<IMemory> mDecryptDestination;
221 int32_t mHeapSeqNum;
222
223 std::shared_ptr<Codec2Client::Component> mComponent;
224 std::string mComponentName; ///< component name for debugging
225 const char *mName; ///< C-string version of component name
226 std::shared_ptr<CCodecCallback> mCCodecCallback;
227 std::shared_ptr<C2BlockPool> mInputAllocator;
228 QueueSync mQueueSync;
229 std::vector<std::unique_ptr<C2Param>> mParamsToBeSet;
230
Wonsik Kim4fa4f2b2019-02-13 11:02:58 -0800231 size_t mDelay;
Wonsik Kim078b58e2019-01-09 15:08:06 -0800232
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700233 struct Input {
234 Input();
235
236 std::unique_ptr<InputBuffers> buffers;
237 size_t numSlots;
238 FlexBuffersImpl extraBuffers;
239 size_t numExtraSlots;
240 uint32_t inputDelay;
241 uint32_t pipelineDelay;
242 };
243 Mutexed<Input> mInput;
244 struct Output {
245 std::unique_ptr<OutputBuffers> buffers;
246 size_t numSlots;
247 uint32_t outputDelay;
248 };
249 Mutexed<Output> mOutput;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800250 Mutexed<std::list<sp<ABuffer>>> mFlushedConfigs;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800251
252 std::atomic_uint64_t mFrameIndex;
253 std::atomic_uint64_t mFirstValidFrameIndex;
254
255 sp<MemoryDealer> makeMemoryDealer(size_t heapSize);
256
257 struct OutputSurface {
258 sp<Surface> surface;
259 uint32_t generation;
Wonsik Kimf5e5c832019-02-21 11:36:05 -0800260 int maxDequeueBuffers;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800261 };
262 Mutexed<OutputSurface> mOutputSurface;
263
264 struct BlockPools {
265 C2Allocator::id_t inputAllocatorId;
266 std::shared_ptr<C2BlockPool> inputPool;
267 C2Allocator::id_t outputAllocatorId;
268 C2BlockPool::local_id_t outputPoolId;
269 std::shared_ptr<Codec2Client::Configurable> outputPoolIntf;
270 };
271 Mutexed<BlockPools> mBlockPools;
272
273 std::shared_ptr<InputSurfaceWrapper> mInputSurface;
274
275 MetaMode mMetaMode;
276
Wonsik Kimab34ed62019-01-31 15:28:46 -0800277 Mutexed<PipelineWatcher> mPipelineWatcher;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800278
279 class ReorderStash {
280 public:
281 struct Entry {
282 inline Entry() : buffer(nullptr), timestamp(0), flags(0), ordinal({0, 0, 0}) {}
283 inline Entry(
284 const std::shared_ptr<C2Buffer> &b,
285 int64_t t,
286 int32_t f,
287 const C2WorkOrdinalStruct &o)
288 : buffer(b), timestamp(t), flags(f), ordinal(o) {}
289 std::shared_ptr<C2Buffer> buffer;
290 int64_t timestamp;
291 int32_t flags;
292 C2WorkOrdinalStruct ordinal;
293 };
294
295 ReorderStash();
296
297 void clear();
Wonsik Kim6897f222019-01-30 13:29:24 -0800298 void flush();
Pawin Vongmasa36653902018-11-15 00:10:25 -0800299 void setDepth(uint32_t depth);
300 void setKey(C2Config::ordinal_key_t key);
301 bool pop(Entry *entry);
302 void emplace(
303 const std::shared_ptr<C2Buffer> &buffer,
304 int64_t timestamp,
305 int32_t flags,
306 const C2WorkOrdinalStruct &ordinal);
307 void defer(const Entry &entry);
308 bool hasPending() const;
309
310 private:
311 std::list<Entry> mPending;
312 std::list<Entry> mStash;
313 uint32_t mDepth;
314 C2Config::ordinal_key_t mKey;
315
316 bool less(const C2WorkOrdinalStruct &o1, const C2WorkOrdinalStruct &o2);
317 };
318 Mutexed<ReorderStash> mReorderStash;
319
320 std::atomic_bool mInputMetEos;
Wonsik Kimf7529dd2019-04-18 17:35:53 -0700321 std::once_flag mRenderWarningFlag;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800322
323 inline bool hasCryptoOrDescrambler() {
324 return mCrypto != nullptr || mDescrambler != nullptr;
325 }
326};
327
328// Conversion of a c2_status_t value to a status_t value may depend on the
329// operation that returns the c2_status_t value.
330enum c2_operation_t {
331 C2_OPERATION_NONE,
332 C2_OPERATION_Component_connectToOmxInputSurface,
333 C2_OPERATION_Component_createBlockPool,
334 C2_OPERATION_Component_destroyBlockPool,
335 C2_OPERATION_Component_disconnectFromInputSurface,
336 C2_OPERATION_Component_drain,
337 C2_OPERATION_Component_flush,
338 C2_OPERATION_Component_queue,
339 C2_OPERATION_Component_release,
340 C2_OPERATION_Component_reset,
341 C2_OPERATION_Component_setOutputSurface,
342 C2_OPERATION_Component_start,
343 C2_OPERATION_Component_stop,
344 C2_OPERATION_ComponentStore_copyBuffer,
345 C2_OPERATION_ComponentStore_createComponent,
346 C2_OPERATION_ComponentStore_createInputSurface,
347 C2_OPERATION_ComponentStore_createInterface,
348 C2_OPERATION_Configurable_config,
349 C2_OPERATION_Configurable_query,
350 C2_OPERATION_Configurable_querySupportedParams,
351 C2_OPERATION_Configurable_querySupportedValues,
352 C2_OPERATION_InputSurface_connectToComponent,
353 C2_OPERATION_InputSurfaceConnection_disconnect,
354};
355
356status_t toStatusT(c2_status_t c2s, c2_operation_t c2op = C2_OPERATION_NONE);
357
358} // namespace android
359
360#endif // CCODEC_BUFFER_CHANNEL_H_