blob: 02632110064db594524706f94e971502813af71a [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>
Pawin Vongmasa36653902018-11-15 00:10:25 -080032
Wonsik Kim469c8342019-04-11 16:46:09 -070033#include "CCodecBuffers.h"
Pawin Vongmasa36653902018-11-15 00:10:25 -080034#include "InputSurfaceWrapper.h"
Wonsik Kimab34ed62019-01-31 15:28:46 -080035#include "PipelineWatcher.h"
Pawin Vongmasa36653902018-11-15 00:10:25 -080036
37namespace android {
38
Wonsik Kim078b58e2019-01-09 15:08:06 -080039class MemoryDealer;
40
Pawin Vongmasa36653902018-11-15 00:10:25 -080041class CCodecCallback {
42public:
43 virtual ~CCodecCallback() = default;
44 virtual void onError(status_t err, enum ActionCode actionCode) = 0;
45 virtual void onOutputFramesRendered(int64_t mediaTimeUs, nsecs_t renderTimeNs) = 0;
Pawin Vongmasa36653902018-11-15 00:10:25 -080046 virtual void onOutputBuffersChanged() = 0;
47};
48
49/**
50 * BufferChannelBase implementation for CCodec.
51 */
52class CCodecBufferChannel
53 : public BufferChannelBase, public std::enable_shared_from_this<CCodecBufferChannel> {
54public:
55 explicit CCodecBufferChannel(const std::shared_ptr<CCodecCallback> &callback);
56 virtual ~CCodecBufferChannel();
57
58 // BufferChannelBase interface
Wonsik Kim596187e2019-10-25 12:44:10 -070059 void setCrypto(const sp<ICrypto> &crypto) override;
60 void setDescrambler(const sp<IDescrambler> &descrambler) override;
61
Pawin Vongmasa36653902018-11-15 00:10:25 -080062 virtual status_t queueInputBuffer(const sp<MediaCodecBuffer> &buffer) override;
63 virtual status_t queueSecureInputBuffer(
64 const sp<MediaCodecBuffer> &buffer,
65 bool secure,
66 const uint8_t *key,
67 const uint8_t *iv,
68 CryptoPlugin::Mode mode,
69 CryptoPlugin::Pattern pattern,
70 const CryptoPlugin::SubSample *subSamples,
71 size_t numSubSamples,
72 AString *errorDetailMsg) override;
Wonsik Kimfb7a7672019-12-27 17:13:33 -080073 virtual status_t attachBuffer(
74 const std::shared_ptr<C2Buffer> &c2Buffer,
75 const sp<MediaCodecBuffer> &buffer) override;
76 virtual status_t attachEncryptedBuffer(
77 const sp<hardware::HidlMemory> &memory,
78 bool secure,
79 const uint8_t *key,
80 const uint8_t *iv,
81 CryptoPlugin::Mode mode,
82 CryptoPlugin::Pattern pattern,
83 size_t offset,
84 const CryptoPlugin::SubSample *subSamples,
85 size_t numSubSamples,
86 const sp<MediaCodecBuffer> &buffer) override;
Pawin Vongmasa36653902018-11-15 00:10:25 -080087 virtual status_t renderOutputBuffer(
88 const sp<MediaCodecBuffer> &buffer, int64_t timestampNs) override;
89 virtual status_t discardBuffer(const sp<MediaCodecBuffer> &buffer) override;
90 virtual void getInputBufferArray(Vector<sp<MediaCodecBuffer>> *array) override;
91 virtual void getOutputBufferArray(Vector<sp<MediaCodecBuffer>> *array) override;
92
93 // Methods below are interface for CCodec to use.
94
95 /**
96 * Set the component object for buffer processing.
97 */
98 void setComponent(const std::shared_ptr<Codec2Client::Component> &component);
99
100 /**
101 * Set output graphic surface for rendering.
102 */
103 status_t setSurface(const sp<Surface> &surface);
104
105 /**
106 * Set GraphicBufferSource object from which the component extracts input
107 * buffers.
108 */
109 status_t setInputSurface(const std::shared_ptr<InputSurfaceWrapper> &surface);
110
111 /**
112 * Signal EOS to input surface.
113 */
114 status_t signalEndOfInputStream();
115
116 /**
117 * Set parameters.
118 */
119 status_t setParameters(std::vector<std::unique_ptr<C2Param>> &params);
120
121 /**
122 * Start queueing buffers to the component. This object should never queue
123 * buffers before this call has completed.
124 */
Wonsik Kimfb7a7672019-12-27 17:13:33 -0800125 status_t start(
126 const sp<AMessage> &inputFormat,
127 const sp<AMessage> &outputFormat,
128 bool buffersBoundToCodec);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800129
130 /**
131 * Request initial input buffers to be filled by client.
132 */
133 status_t requestInitialInputBuffers();
134
135 /**
136 * Stop queueing buffers to the component. This object should never queue
137 * buffers after this call, until start() is called.
138 */
139 void stop();
140
141 void flush(const std::list<std::unique_ptr<C2Work>> &flushedWork);
142
143 /**
144 * Notify input client about work done.
145 *
146 * @param workItems finished work item.
147 * @param outputFormat new output format if it has changed, otherwise nullptr
148 * @param initData new init data (CSD) if it has changed, otherwise nullptr
Pawin Vongmasa36653902018-11-15 00:10:25 -0800149 */
150 void onWorkDone(
151 std::unique_ptr<C2Work> work, const sp<AMessage> &outputFormat,
Wonsik Kimab34ed62019-01-31 15:28:46 -0800152 const C2StreamInitDataInfo::output *initData);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800153
154 /**
155 * Make an input buffer available for the client as it is no longer needed
156 * by the codec.
157 *
Wonsik Kimab34ed62019-01-31 15:28:46 -0800158 * @param frameIndex The index of input work
159 * @param arrayIndex The index of buffer in the input work buffers.
Pawin Vongmasa36653902018-11-15 00:10:25 -0800160 */
Wonsik Kimab34ed62019-01-31 15:28:46 -0800161 void onInputBufferDone(uint64_t frameIndex, size_t arrayIndex);
162
163 PipelineWatcher::Clock::duration elapsed();
Pawin Vongmasa36653902018-11-15 00:10:25 -0800164
165 enum MetaMode {
166 MODE_NONE,
167 MODE_ANW,
168 };
169
170 void setMetaMode(MetaMode mode);
171
Pawin Vongmasa36653902018-11-15 00:10:25 -0800172private:
173 class QueueGuard;
174
175 /**
176 * Special mutex-like object with the following properties:
177 *
178 * - At STOPPED state (initial, or after stop())
179 * - QueueGuard object gets created at STOPPED state, and the client is
180 * supposed to return immediately.
181 * - At RUNNING state (after start())
182 * - Each QueueGuard object
183 */
184 class QueueSync {
185 public:
186 /**
187 * At construction the sync object is in STOPPED state.
188 */
189 inline QueueSync() {}
190 ~QueueSync() = default;
191
192 /**
193 * Transition to RUNNING state when stopped. No-op if already in RUNNING
194 * state.
195 */
196 void start();
197
198 /**
199 * At RUNNING state, wait until all QueueGuard object created during
200 * RUNNING state are destroyed, and then transition to STOPPED state.
201 * No-op if already in STOPPED state.
202 */
203 void stop();
204
205 private:
206 Mutex mGuardLock;
207
208 struct Counter {
209 inline Counter() : value(-1) {}
210 int32_t value;
211 Condition cond;
212 };
213 Mutexed<Counter> mCount;
214
215 friend class CCodecBufferChannel::QueueGuard;
216 };
217
218 class QueueGuard {
219 public:
220 QueueGuard(QueueSync &sync);
221 ~QueueGuard();
222 inline bool isRunning() { return mRunning; }
223
224 private:
225 QueueSync &mSync;
226 bool mRunning;
227 };
228
229 void feedInputBufferIfAvailable();
230 void feedInputBufferIfAvailableInternal();
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700231 status_t queueInputBufferInternal(sp<MediaCodecBuffer> buffer);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800232 bool handleWork(
233 std::unique_ptr<C2Work> work, const sp<AMessage> &outputFormat,
234 const C2StreamInitDataInfo::output *initData);
235 void sendOutputBuffers();
Wonsik Kimfb7a7672019-12-27 17:13:33 -0800236 void ensureDecryptDestination(size_t size);
237 int32_t getHeapSeqNum(const sp<hardware::HidlMemory> &memory);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800238
239 QueueSync mSync;
240 sp<MemoryDealer> mDealer;
241 sp<IMemory> mDecryptDestination;
242 int32_t mHeapSeqNum;
Wonsik Kimfb7a7672019-12-27 17:13:33 -0800243 std::map<wp<hardware::HidlMemory>, int32_t> mHeapSeqNumMap;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800244
245 std::shared_ptr<Codec2Client::Component> mComponent;
246 std::string mComponentName; ///< component name for debugging
247 const char *mName; ///< C-string version of component name
248 std::shared_ptr<CCodecCallback> mCCodecCallback;
249 std::shared_ptr<C2BlockPool> mInputAllocator;
250 QueueSync mQueueSync;
251 std::vector<std::unique_ptr<C2Param>> mParamsToBeSet;
252
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700253 struct Input {
254 Input();
255
256 std::unique_ptr<InputBuffers> buffers;
257 size_t numSlots;
258 FlexBuffersImpl extraBuffers;
259 size_t numExtraSlots;
260 uint32_t inputDelay;
261 uint32_t pipelineDelay;
262 };
263 Mutexed<Input> mInput;
264 struct Output {
265 std::unique_ptr<OutputBuffers> buffers;
266 size_t numSlots;
267 uint32_t outputDelay;
268 };
269 Mutexed<Output> mOutput;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800270 Mutexed<std::list<sp<ABuffer>>> mFlushedConfigs;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800271
272 std::atomic_uint64_t mFrameIndex;
273 std::atomic_uint64_t mFirstValidFrameIndex;
274
275 sp<MemoryDealer> makeMemoryDealer(size_t heapSize);
276
277 struct OutputSurface {
278 sp<Surface> surface;
279 uint32_t generation;
Wonsik Kimf5e5c832019-02-21 11:36:05 -0800280 int maxDequeueBuffers;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800281 };
282 Mutexed<OutputSurface> mOutputSurface;
283
284 struct BlockPools {
285 C2Allocator::id_t inputAllocatorId;
286 std::shared_ptr<C2BlockPool> inputPool;
287 C2Allocator::id_t outputAllocatorId;
288 C2BlockPool::local_id_t outputPoolId;
289 std::shared_ptr<Codec2Client::Configurable> outputPoolIntf;
290 };
291 Mutexed<BlockPools> mBlockPools;
292
293 std::shared_ptr<InputSurfaceWrapper> mInputSurface;
294
295 MetaMode mMetaMode;
296
Wonsik Kimab34ed62019-01-31 15:28:46 -0800297 Mutexed<PipelineWatcher> mPipelineWatcher;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800298
Wonsik Kima4e049d2020-04-28 19:42:23 +0000299 class ReorderStash {
300 public:
301 struct Entry {
302 inline Entry() : buffer(nullptr), timestamp(0), flags(0), ordinal({0, 0, 0}) {}
303 inline Entry(
304 const std::shared_ptr<C2Buffer> &b,
305 int64_t t,
306 int32_t f,
307 const C2WorkOrdinalStruct &o)
308 : buffer(b), timestamp(t), flags(f), ordinal(o) {}
309 std::shared_ptr<C2Buffer> buffer;
310 int64_t timestamp;
311 int32_t flags;
312 C2WorkOrdinalStruct ordinal;
313 };
314
315 ReorderStash();
316
317 void clear();
318 void flush();
319 void setDepth(uint32_t depth);
320 void setKey(C2Config::ordinal_key_t key);
321 bool pop(Entry *entry);
322 void emplace(
323 const std::shared_ptr<C2Buffer> &buffer,
324 int64_t timestamp,
325 int32_t flags,
326 const C2WorkOrdinalStruct &ordinal);
327 void defer(const Entry &entry);
328 bool hasPending() const;
329 uint32_t depth() const { return mDepth; }
330
331 private:
332 std::list<Entry> mPending;
333 std::list<Entry> mStash;
334 uint32_t mDepth;
335 C2Config::ordinal_key_t mKey;
336
337 bool less(const C2WorkOrdinalStruct &o1, const C2WorkOrdinalStruct &o2);
338 };
339 Mutexed<ReorderStash> mReorderStash;
340
Pawin Vongmasa36653902018-11-15 00:10:25 -0800341 std::atomic_bool mInputMetEos;
Wonsik Kimf7529dd2019-04-18 17:35:53 -0700342 std::once_flag mRenderWarningFlag;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800343
Wonsik Kim596187e2019-10-25 12:44:10 -0700344 sp<ICrypto> mCrypto;
345 sp<IDescrambler> mDescrambler;
346
Pawin Vongmasa36653902018-11-15 00:10:25 -0800347 inline bool hasCryptoOrDescrambler() {
348 return mCrypto != nullptr || mDescrambler != nullptr;
349 }
350};
351
352// Conversion of a c2_status_t value to a status_t value may depend on the
353// operation that returns the c2_status_t value.
354enum c2_operation_t {
355 C2_OPERATION_NONE,
356 C2_OPERATION_Component_connectToOmxInputSurface,
357 C2_OPERATION_Component_createBlockPool,
358 C2_OPERATION_Component_destroyBlockPool,
359 C2_OPERATION_Component_disconnectFromInputSurface,
360 C2_OPERATION_Component_drain,
361 C2_OPERATION_Component_flush,
362 C2_OPERATION_Component_queue,
363 C2_OPERATION_Component_release,
364 C2_OPERATION_Component_reset,
365 C2_OPERATION_Component_setOutputSurface,
366 C2_OPERATION_Component_start,
367 C2_OPERATION_Component_stop,
368 C2_OPERATION_ComponentStore_copyBuffer,
369 C2_OPERATION_ComponentStore_createComponent,
370 C2_OPERATION_ComponentStore_createInputSurface,
371 C2_OPERATION_ComponentStore_createInterface,
372 C2_OPERATION_Configurable_config,
373 C2_OPERATION_Configurable_query,
374 C2_OPERATION_Configurable_querySupportedParams,
375 C2_OPERATION_Configurable_querySupportedValues,
376 C2_OPERATION_InputSurface_connectToComponent,
377 C2_OPERATION_InputSurfaceConnection_disconnect,
378};
379
380status_t toStatusT(c2_status_t c2s, c2_operation_t c2op = C2_OPERATION_NONE);
381
382} // namespace android
383
384#endif // CCODEC_BUFFER_CHANNEL_H_