blob: f5e268aa900bb72f658c85be1af7b6de2ebaf8b8 [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
Brian Lindahl932bf602023-03-09 11:59:48 -070021#include <deque>
Pawin Vongmasa36653902018-11-15 00:10:25 -080022#include <map>
23#include <memory>
24#include <vector>
25
26#include <C2Buffer.h>
27#include <C2Component.h>
28#include <Codec2Mapper.h>
29
30#include <codec2/hidl/client.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080031#include <media/stagefright/foundation/Mutexed.h>
32#include <media/stagefright/CodecBase.h>
Pawin Vongmasa36653902018-11-15 00:10:25 -080033
Wonsik Kim469c8342019-04-11 16:46:09 -070034#include "CCodecBuffers.h"
Wonsik Kime1104ca2020-11-24 15:01:33 -080035#include "FrameReassembler.h"
Pawin Vongmasa36653902018-11-15 00:10:25 -080036#include "InputSurfaceWrapper.h"
Wonsik Kimab34ed62019-01-31 15:28:46 -080037#include "PipelineWatcher.h"
Pawin Vongmasa36653902018-11-15 00:10:25 -080038
39namespace android {
40
Wonsik Kim078b58e2019-01-09 15:08:06 -080041class MemoryDealer;
42
Pawin Vongmasa36653902018-11-15 00:10:25 -080043class CCodecCallback {
44public:
45 virtual ~CCodecCallback() = default;
46 virtual void onError(status_t err, enum ActionCode actionCode) = 0;
47 virtual void onOutputFramesRendered(int64_t mediaTimeUs, nsecs_t renderTimeNs) = 0;
Pawin Vongmasa36653902018-11-15 00:10:25 -080048 virtual void onOutputBuffersChanged() = 0;
Guillaume Chelfi867d4dd2021-07-01 18:38:45 +020049 virtual void onFirstTunnelFrameReady() = 0;
Pawin Vongmasa36653902018-11-15 00:10:25 -080050};
51
52/**
53 * BufferChannelBase implementation for CCodec.
54 */
55class CCodecBufferChannel
56 : public BufferChannelBase, public std::enable_shared_from_this<CCodecBufferChannel> {
57public:
58 explicit CCodecBufferChannel(const std::shared_ptr<CCodecCallback> &callback);
59 virtual ~CCodecBufferChannel();
60
61 // BufferChannelBase interface
Wonsik Kim596187e2019-10-25 12:44:10 -070062 void setCrypto(const sp<ICrypto> &crypto) override;
63 void setDescrambler(const sp<IDescrambler> &descrambler) override;
64
Brian Lindahlaab4eda2023-11-19 02:48:49 +000065 status_t queueInputBuffer(const sp<MediaCodecBuffer> &buffer) override;
66 status_t queueSecureInputBuffer(
Pawin Vongmasa36653902018-11-15 00:10:25 -080067 const sp<MediaCodecBuffer> &buffer,
68 bool secure,
69 const uint8_t *key,
70 const uint8_t *iv,
71 CryptoPlugin::Mode mode,
72 CryptoPlugin::Pattern pattern,
73 const CryptoPlugin::SubSample *subSamples,
74 size_t numSubSamples,
75 AString *errorDetailMsg) override;
Arun Johnson564f3a92024-02-01 19:09:45 +000076 status_t queueSecureInputBuffers(
77 const sp<MediaCodecBuffer> &buffer,
78 bool secure,
79 AString *errorDetailMsg) override;
Brian Lindahlaab4eda2023-11-19 02:48:49 +000080 status_t attachBuffer(
Wonsik Kimfb7a7672019-12-27 17:13:33 -080081 const std::shared_ptr<C2Buffer> &c2Buffer,
82 const sp<MediaCodecBuffer> &buffer) override;
Brian Lindahlaab4eda2023-11-19 02:48:49 +000083 status_t attachEncryptedBuffer(
Wonsik Kimfb7a7672019-12-27 17:13:33 -080084 const sp<hardware::HidlMemory> &memory,
85 bool secure,
86 const uint8_t *key,
87 const uint8_t *iv,
88 CryptoPlugin::Mode mode,
89 CryptoPlugin::Pattern pattern,
90 size_t offset,
91 const CryptoPlugin::SubSample *subSamples,
92 size_t numSubSamples,
Arun Johnson634d0802023-02-14 22:07:51 +000093 const sp<MediaCodecBuffer> &buffer,
94 AString* errorDetailMsg) override;
Arun Johnson564f3a92024-02-01 19:09:45 +000095 status_t attachEncryptedBuffers(
96 const sp<hardware::HidlMemory> &memory,
97 size_t offset,
98 const sp<MediaCodecBuffer> &buffer,
99 bool secure,
100 AString* errorDetailMsg) override;
Brian Lindahlaab4eda2023-11-19 02:48:49 +0000101 status_t renderOutputBuffer(
Pawin Vongmasa36653902018-11-15 00:10:25 -0800102 const sp<MediaCodecBuffer> &buffer, int64_t timestampNs) override;
Brian Lindahlaab4eda2023-11-19 02:48:49 +0000103 void pollForRenderedBuffers() override;
Sungtak Lee214ce612023-11-01 10:01:13 +0000104 void onBufferReleasedFromOutputSurface(uint32_t generation) override;
Sungtak Lee1720e4c2024-07-31 21:15:26 +0000105 void onBufferAttachedToOutputSurface(uint32_t generation) override;
Brian Lindahlaab4eda2023-11-19 02:48:49 +0000106 status_t discardBuffer(const sp<MediaCodecBuffer> &buffer) override;
107 void getInputBufferArray(Vector<sp<MediaCodecBuffer>> *array) override;
108 void getOutputBufferArray(Vector<sp<MediaCodecBuffer>> *array) override;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800109
110 // Methods below are interface for CCodec to use.
111
112 /**
113 * Set the component object for buffer processing.
114 */
115 void setComponent(const std::shared_ptr<Codec2Client::Component> &component);
116
117 /**
118 * Set output graphic surface for rendering.
119 */
Sungtak Lee214ce612023-11-01 10:01:13 +0000120 status_t setSurface(const sp<Surface> &surface, uint32_t generation, bool pushBlankBuffer);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800121
122 /**
123 * Set GraphicBufferSource object from which the component extracts input
124 * buffers.
125 */
126 status_t setInputSurface(const std::shared_ptr<InputSurfaceWrapper> &surface);
127
128 /**
129 * Signal EOS to input surface.
130 */
131 status_t signalEndOfInputStream();
132
133 /**
134 * Set parameters.
135 */
136 status_t setParameters(std::vector<std::unique_ptr<C2Param>> &params);
137
138 /**
139 * Start queueing buffers to the component. This object should never queue
140 * buffers before this call has completed.
141 */
Wonsik Kimfb7a7672019-12-27 17:13:33 -0800142 status_t start(
143 const sp<AMessage> &inputFormat,
144 const sp<AMessage> &outputFormat,
145 bool buffersBoundToCodec);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800146
147 /**
Wonsik Kim34b28b42022-05-20 15:49:32 -0700148 * Prepare initial input buffers to be filled by client.
149 *
150 * \param clientInputBuffers[out] pointer to slot index -> buffer map.
151 * On success, it contains prepared
152 * initial input buffers.
Pawin Vongmasa36653902018-11-15 00:10:25 -0800153 */
Wonsik Kim34b28b42022-05-20 15:49:32 -0700154 status_t prepareInitialInputBuffers(
Arun Johnson326166e2023-07-28 19:11:52 +0000155 std::map<size_t, sp<MediaCodecBuffer>> *clientInputBuffers,
156 bool retry = false);
Wonsik Kim34b28b42022-05-20 15:49:32 -0700157
158 /**
159 * Request initial input buffers as prepared in clientInputBuffers.
160 *
161 * \param clientInputBuffers[in] slot index -> buffer map with prepared
162 * initial input buffers.
163 */
164 status_t requestInitialInputBuffers(
165 std::map<size_t, sp<MediaCodecBuffer>> &&clientInputBuffers);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800166
167 /**
Sungtak Leed964e2e2022-07-30 08:43:58 +0000168 * Stop using buffers of the current output surface for other Codec
169 * instances to use the surface safely.
Sungtak Lee80c8e1e2023-01-26 11:03:14 +0000170 *
171 * \param pushBlankBuffer[in] push a blank buffer at the end if true
Sungtak Leed964e2e2022-07-30 08:43:58 +0000172 */
Sungtak Lee80c8e1e2023-01-26 11:03:14 +0000173 void stopUseOutputSurface(bool pushBlankBuffer);
Sungtak Leed964e2e2022-07-30 08:43:58 +0000174
175 /**
Pawin Vongmasa36653902018-11-15 00:10:25 -0800176 * Stop queueing buffers to the component. This object should never queue
177 * buffers after this call, until start() is called.
178 */
179 void stop();
180
Wonsik Kim936a89c2020-05-08 16:07:50 -0700181 /**
182 * Stop queueing buffers to the component and release all buffers.
183 */
184 void reset();
185
186 /**
187 * Release all resources.
188 */
189 void release();
190
Pawin Vongmasa36653902018-11-15 00:10:25 -0800191 void flush(const std::list<std::unique_ptr<C2Work>> &flushedWork);
192
193 /**
194 * Notify input client about work done.
195 *
196 * @param workItems finished work item.
197 * @param outputFormat new output format if it has changed, otherwise nullptr
198 * @param initData new init data (CSD) if it has changed, otherwise nullptr
Pawin Vongmasa36653902018-11-15 00:10:25 -0800199 */
200 void onWorkDone(
201 std::unique_ptr<C2Work> work, const sp<AMessage> &outputFormat,
Wonsik Kimab34ed62019-01-31 15:28:46 -0800202 const C2StreamInitDataInfo::output *initData);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800203
204 /**
205 * Make an input buffer available for the client as it is no longer needed
206 * by the codec.
207 *
Wonsik Kimab34ed62019-01-31 15:28:46 -0800208 * @param frameIndex The index of input work
209 * @param arrayIndex The index of buffer in the input work buffers.
Pawin Vongmasa36653902018-11-15 00:10:25 -0800210 */
Wonsik Kimab34ed62019-01-31 15:28:46 -0800211 void onInputBufferDone(uint64_t frameIndex, size_t arrayIndex);
212
213 PipelineWatcher::Clock::duration elapsed();
Pawin Vongmasa36653902018-11-15 00:10:25 -0800214
215 enum MetaMode {
216 MODE_NONE,
217 MODE_ANW,
218 };
219
220 void setMetaMode(MetaMode mode);
221
Songyue Han1e6769b2023-08-30 18:09:27 +0000222 /**
223 * get pixel format from output buffers.
224 *
225 * @return 0 if no valid pixel format found.
226 */
227 uint32_t getBuffersPixelFormat(bool isEncoder);
228
229 void resetBuffersPixelFormat(bool isEncoder);
230
Harish Mahendrakar38a99c22024-02-26 20:28:05 +0530231 /**
232 * Queue a C2 info buffer that will be sent to codec in the subsequent
233 * queueInputBuffer
234 *
235 * @param buffer C2 info buffer
236 */
237 void setInfoBuffer(const std::shared_ptr<C2InfoBuffer> &buffer);
238
Pawin Vongmasa36653902018-11-15 00:10:25 -0800239private:
Songyue Han1e6769b2023-08-30 18:09:27 +0000240 uint32_t getInputBuffersPixelFormat();
241
242 uint32_t getOutputBuffersPixelFormat();
243
Pawin Vongmasa36653902018-11-15 00:10:25 -0800244 class QueueGuard;
245
246 /**
247 * Special mutex-like object with the following properties:
248 *
249 * - At STOPPED state (initial, or after stop())
250 * - QueueGuard object gets created at STOPPED state, and the client is
251 * supposed to return immediately.
252 * - At RUNNING state (after start())
253 * - Each QueueGuard object
254 */
255 class QueueSync {
256 public:
257 /**
258 * At construction the sync object is in STOPPED state.
259 */
260 inline QueueSync() {}
261 ~QueueSync() = default;
262
263 /**
264 * Transition to RUNNING state when stopped. No-op if already in RUNNING
265 * state.
266 */
267 void start();
268
269 /**
270 * At RUNNING state, wait until all QueueGuard object created during
271 * RUNNING state are destroyed, and then transition to STOPPED state.
272 * No-op if already in STOPPED state.
273 */
274 void stop();
275
276 private:
277 Mutex mGuardLock;
278
279 struct Counter {
280 inline Counter() : value(-1) {}
281 int32_t value;
282 Condition cond;
283 };
284 Mutexed<Counter> mCount;
285
286 friend class CCodecBufferChannel::QueueGuard;
287 };
288
289 class QueueGuard {
290 public:
291 QueueGuard(QueueSync &sync);
292 ~QueueGuard();
293 inline bool isRunning() { return mRunning; }
294
295 private:
296 QueueSync &mSync;
297 bool mRunning;
298 };
299
Brian Lindahl932bf602023-03-09 11:59:48 -0700300 struct TrackedFrame {
301 uint64_t number;
302 int64_t mediaTimeUs;
303 int64_t desiredRenderTimeNs;
304 nsecs_t latchTime;
305 sp<Fence> presentFence;
306 };
307
Pawin Vongmasa36653902018-11-15 00:10:25 -0800308 void feedInputBufferIfAvailable();
309 void feedInputBufferIfAvailableInternal();
Sungtak Lee04b30352020-07-27 13:57:25 -0700310 status_t queueInputBufferInternal(sp<MediaCodecBuffer> buffer,
311 std::shared_ptr<C2LinearBlock> encryptedBlock = nullptr,
312 size_t blockSize = 0);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800313 bool handleWork(
314 std::unique_ptr<C2Work> work, const sp<AMessage> &outputFormat,
315 const C2StreamInitDataInfo::output *initData);
316 void sendOutputBuffers();
Wonsik Kimfb7a7672019-12-27 17:13:33 -0800317 void ensureDecryptDestination(size_t size);
318 int32_t getHeapSeqNum(const sp<hardware::HidlMemory> &memory);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800319
Brian Lindahl932bf602023-03-09 11:59:48 -0700320 void initializeFrameTrackingFor(ANativeWindow * window);
321 void trackReleasedFrame(const IGraphicBufferProducer::QueueBufferOutput& qbo,
322 int64_t mediaTimeUs, int64_t desiredRenderTimeNs);
323 void processRenderedFrames(const FrameEventHistoryDelta& delta);
324 int64_t getRenderTimeNs(const TrackedFrame& frame);
325
Pawin Vongmasa36653902018-11-15 00:10:25 -0800326 QueueSync mSync;
327 sp<MemoryDealer> mDealer;
328 sp<IMemory> mDecryptDestination;
329 int32_t mHeapSeqNum;
Wonsik Kimfb7a7672019-12-27 17:13:33 -0800330 std::map<wp<hardware::HidlMemory>, int32_t> mHeapSeqNumMap;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800331
332 std::shared_ptr<Codec2Client::Component> mComponent;
333 std::string mComponentName; ///< component name for debugging
334 const char *mName; ///< C-string version of component name
335 std::shared_ptr<CCodecCallback> mCCodecCallback;
336 std::shared_ptr<C2BlockPool> mInputAllocator;
337 QueueSync mQueueSync;
338 std::vector<std::unique_ptr<C2Param>> mParamsToBeSet;
Houxiang Daie74e5062022-05-19 15:32:54 +0800339 sp<AMessage> mOutputFormat;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800340
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700341 struct Input {
342 Input();
343
344 std::unique_ptr<InputBuffers> buffers;
345 size_t numSlots;
346 FlexBuffersImpl extraBuffers;
347 size_t numExtraSlots;
348 uint32_t inputDelay;
349 uint32_t pipelineDelay;
Wonsik Kim6b2c8be2021-09-28 05:11:04 -0700350 c2_cntr64_t lastFlushIndex;
Wonsik Kime1104ca2020-11-24 15:01:33 -0800351
352 FrameReassembler frameReassembler;
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700353 };
354 Mutexed<Input> mInput;
355 struct Output {
356 std::unique_ptr<OutputBuffers> buffers;
357 size_t numSlots;
358 uint32_t outputDelay;
Wonsik Kim3722abc2023-05-17 13:26:31 -0700359 // true iff the underlying block pool is bounded --- for example,
360 // a BufferQueue-based block pool would be bounded by the BufferQueue.
361 bool bounded;
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700362 };
363 Mutexed<Output> mOutput;
Wonsik Kim5ebfcb22021-01-05 18:58:15 -0800364 Mutexed<std::list<std::unique_ptr<C2Work>>> mFlushedConfigs;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800365
366 std::atomic_uint64_t mFrameIndex;
367 std::atomic_uint64_t mFirstValidFrameIndex;
368
369 sp<MemoryDealer> makeMemoryDealer(size_t heapSize);
370
Brian Lindahl932bf602023-03-09 11:59:48 -0700371 std::deque<TrackedFrame> mTrackedFrames;
Brian Lindahld7967a92023-08-10 10:12:49 -0600372 bool mAreRenderMetricsEnabled;
Brian Lindahl2048d492023-04-05 08:49:23 -0600373 bool mIsSurfaceToDisplay;
Brian Lindahl932bf602023-03-09 11:59:48 -0700374 bool mHasPresentFenceTimes;
375
Pawin Vongmasa36653902018-11-15 00:10:25 -0800376 struct OutputSurface {
377 sp<Surface> surface;
378 uint32_t generation;
Wonsik Kimf5e5c832019-02-21 11:36:05 -0800379 int maxDequeueBuffers;
Byeongjo Park25c3a3d2020-06-12 17:24:21 +0900380 std::map<uint64_t, int> rotation;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800381 };
382 Mutexed<OutputSurface> mOutputSurface;
Wonsik Kim3a692e62023-05-19 15:37:22 -0700383 int mRenderingDepth;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800384
385 struct BlockPools {
386 C2Allocator::id_t inputAllocatorId;
387 std::shared_ptr<C2BlockPool> inputPool;
388 C2Allocator::id_t outputAllocatorId;
389 C2BlockPool::local_id_t outputPoolId;
390 std::shared_ptr<Codec2Client::Configurable> outputPoolIntf;
391 };
392 Mutexed<BlockPools> mBlockPools;
393
Wonsik Kimd4ba8462024-07-12 11:05:48 -0700394 Mutexed<std::shared_ptr<InputSurfaceWrapper>> mInputSurface;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800395
396 MetaMode mMetaMode;
397
Wonsik Kimab34ed62019-01-31 15:28:46 -0800398 Mutexed<PipelineWatcher> mPipelineWatcher;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800399
Pawin Vongmasa36653902018-11-15 00:10:25 -0800400 std::atomic_bool mInputMetEos;
Wonsik Kimf7529dd2019-04-18 17:35:53 -0700401 std::once_flag mRenderWarningFlag;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800402
Wonsik Kim596187e2019-10-25 12:44:10 -0700403 sp<ICrypto> mCrypto;
404 sp<IDescrambler> mDescrambler;
405
Pawin Vongmasa36653902018-11-15 00:10:25 -0800406 inline bool hasCryptoOrDescrambler() {
407 return mCrypto != nullptr || mDescrambler != nullptr;
408 }
Sungtak Lee04b30352020-07-27 13:57:25 -0700409 std::atomic_bool mSendEncryptedInfoBuffer;
Wonsik Kimec585c32021-10-01 01:11:00 -0700410
411 std::atomic_bool mTunneled;
Harish Mahendrakar38a99c22024-02-26 20:28:05 +0530412
413 std::vector<std::shared_ptr<C2InfoBuffer>> mInfoBuffers;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800414};
415
416// Conversion of a c2_status_t value to a status_t value may depend on the
417// operation that returns the c2_status_t value.
418enum c2_operation_t {
419 C2_OPERATION_NONE,
420 C2_OPERATION_Component_connectToOmxInputSurface,
421 C2_OPERATION_Component_createBlockPool,
422 C2_OPERATION_Component_destroyBlockPool,
423 C2_OPERATION_Component_disconnectFromInputSurface,
424 C2_OPERATION_Component_drain,
425 C2_OPERATION_Component_flush,
426 C2_OPERATION_Component_queue,
427 C2_OPERATION_Component_release,
428 C2_OPERATION_Component_reset,
429 C2_OPERATION_Component_setOutputSurface,
430 C2_OPERATION_Component_start,
431 C2_OPERATION_Component_stop,
432 C2_OPERATION_ComponentStore_copyBuffer,
433 C2_OPERATION_ComponentStore_createComponent,
434 C2_OPERATION_ComponentStore_createInputSurface,
435 C2_OPERATION_ComponentStore_createInterface,
436 C2_OPERATION_Configurable_config,
437 C2_OPERATION_Configurable_query,
438 C2_OPERATION_Configurable_querySupportedParams,
439 C2_OPERATION_Configurable_querySupportedValues,
440 C2_OPERATION_InputSurface_connectToComponent,
441 C2_OPERATION_InputSurfaceConnection_disconnect,
442};
443
444status_t toStatusT(c2_status_t c2s, c2_operation_t c2op = C2_OPERATION_NONE);
445
446} // namespace android
447
448#endif // CCODEC_BUFFER_CHANNEL_H_