blob: f60b6fa1211e81991baf57baf676507356f92825 [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
Pawin Vongmasa36653902018-11-15 00:10:25 -080065 virtual status_t queueInputBuffer(const sp<MediaCodecBuffer> &buffer) override;
66 virtual status_t queueSecureInputBuffer(
67 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;
Wonsik Kimfb7a7672019-12-27 17:13:33 -080076 virtual status_t attachBuffer(
77 const std::shared_ptr<C2Buffer> &c2Buffer,
78 const sp<MediaCodecBuffer> &buffer) override;
79 virtual status_t attachEncryptedBuffer(
80 const sp<hardware::HidlMemory> &memory,
81 bool secure,
82 const uint8_t *key,
83 const uint8_t *iv,
84 CryptoPlugin::Mode mode,
85 CryptoPlugin::Pattern pattern,
86 size_t offset,
87 const CryptoPlugin::SubSample *subSamples,
88 size_t numSubSamples,
Arun Johnson634d0802023-02-14 22:07:51 +000089 const sp<MediaCodecBuffer> &buffer,
90 AString* errorDetailMsg) override;
Pawin Vongmasa36653902018-11-15 00:10:25 -080091 virtual status_t renderOutputBuffer(
92 const sp<MediaCodecBuffer> &buffer, int64_t timestampNs) override;
Brian Lindahl932bf602023-03-09 11:59:48 -070093 virtual void pollForRenderedBuffers() override;
Pawin Vongmasa36653902018-11-15 00:10:25 -080094 virtual status_t discardBuffer(const sp<MediaCodecBuffer> &buffer) override;
95 virtual void getInputBufferArray(Vector<sp<MediaCodecBuffer>> *array) override;
96 virtual void getOutputBufferArray(Vector<sp<MediaCodecBuffer>> *array) override;
97
98 // Methods below are interface for CCodec to use.
99
100 /**
101 * Set the component object for buffer processing.
102 */
103 void setComponent(const std::shared_ptr<Codec2Client::Component> &component);
104
105 /**
106 * Set output graphic surface for rendering.
107 */
Sungtak Lee80c8e1e2023-01-26 11:03:14 +0000108 status_t setSurface(const sp<Surface> &surface, bool pushBlankBuffer);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800109
110 /**
111 * Set GraphicBufferSource object from which the component extracts input
112 * buffers.
113 */
114 status_t setInputSurface(const std::shared_ptr<InputSurfaceWrapper> &surface);
115
116 /**
117 * Signal EOS to input surface.
118 */
119 status_t signalEndOfInputStream();
120
121 /**
122 * Set parameters.
123 */
124 status_t setParameters(std::vector<std::unique_ptr<C2Param>> &params);
125
126 /**
127 * Start queueing buffers to the component. This object should never queue
128 * buffers before this call has completed.
129 */
Wonsik Kimfb7a7672019-12-27 17:13:33 -0800130 status_t start(
131 const sp<AMessage> &inputFormat,
132 const sp<AMessage> &outputFormat,
133 bool buffersBoundToCodec);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800134
135 /**
Wonsik Kim34b28b42022-05-20 15:49:32 -0700136 * Prepare initial input buffers to be filled by client.
137 *
138 * \param clientInputBuffers[out] pointer to slot index -> buffer map.
139 * On success, it contains prepared
140 * initial input buffers.
Pawin Vongmasa36653902018-11-15 00:10:25 -0800141 */
Wonsik Kim34b28b42022-05-20 15:49:32 -0700142 status_t prepareInitialInputBuffers(
Arun Johnson326166e2023-07-28 19:11:52 +0000143 std::map<size_t, sp<MediaCodecBuffer>> *clientInputBuffers,
144 bool retry = false);
Wonsik Kim34b28b42022-05-20 15:49:32 -0700145
146 /**
147 * Request initial input buffers as prepared in clientInputBuffers.
148 *
149 * \param clientInputBuffers[in] slot index -> buffer map with prepared
150 * initial input buffers.
151 */
152 status_t requestInitialInputBuffers(
153 std::map<size_t, sp<MediaCodecBuffer>> &&clientInputBuffers);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800154
155 /**
Sungtak Leed964e2e2022-07-30 08:43:58 +0000156 * Stop using buffers of the current output surface for other Codec
157 * instances to use the surface safely.
Sungtak Lee80c8e1e2023-01-26 11:03:14 +0000158 *
159 * \param pushBlankBuffer[in] push a blank buffer at the end if true
Sungtak Leed964e2e2022-07-30 08:43:58 +0000160 */
Sungtak Lee80c8e1e2023-01-26 11:03:14 +0000161 void stopUseOutputSurface(bool pushBlankBuffer);
Sungtak Leed964e2e2022-07-30 08:43:58 +0000162
163 /**
Pawin Vongmasa36653902018-11-15 00:10:25 -0800164 * Stop queueing buffers to the component. This object should never queue
165 * buffers after this call, until start() is called.
166 */
167 void stop();
168
Wonsik Kim936a89c2020-05-08 16:07:50 -0700169 /**
170 * Stop queueing buffers to the component and release all buffers.
171 */
172 void reset();
173
174 /**
175 * Release all resources.
176 */
177 void release();
178
Pawin Vongmasa36653902018-11-15 00:10:25 -0800179 void flush(const std::list<std::unique_ptr<C2Work>> &flushedWork);
180
181 /**
182 * Notify input client about work done.
183 *
184 * @param workItems finished work item.
185 * @param outputFormat new output format if it has changed, otherwise nullptr
186 * @param initData new init data (CSD) if it has changed, otherwise nullptr
Pawin Vongmasa36653902018-11-15 00:10:25 -0800187 */
188 void onWorkDone(
189 std::unique_ptr<C2Work> work, const sp<AMessage> &outputFormat,
Wonsik Kimab34ed62019-01-31 15:28:46 -0800190 const C2StreamInitDataInfo::output *initData);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800191
192 /**
193 * Make an input buffer available for the client as it is no longer needed
194 * by the codec.
195 *
Wonsik Kimab34ed62019-01-31 15:28:46 -0800196 * @param frameIndex The index of input work
197 * @param arrayIndex The index of buffer in the input work buffers.
Pawin Vongmasa36653902018-11-15 00:10:25 -0800198 */
Wonsik Kimab34ed62019-01-31 15:28:46 -0800199 void onInputBufferDone(uint64_t frameIndex, size_t arrayIndex);
200
201 PipelineWatcher::Clock::duration elapsed();
Pawin Vongmasa36653902018-11-15 00:10:25 -0800202
203 enum MetaMode {
204 MODE_NONE,
205 MODE_ANW,
206 };
207
208 void setMetaMode(MetaMode mode);
209
Pawin Vongmasa36653902018-11-15 00:10:25 -0800210private:
211 class QueueGuard;
212
213 /**
214 * Special mutex-like object with the following properties:
215 *
216 * - At STOPPED state (initial, or after stop())
217 * - QueueGuard object gets created at STOPPED state, and the client is
218 * supposed to return immediately.
219 * - At RUNNING state (after start())
220 * - Each QueueGuard object
221 */
222 class QueueSync {
223 public:
224 /**
225 * At construction the sync object is in STOPPED state.
226 */
227 inline QueueSync() {}
228 ~QueueSync() = default;
229
230 /**
231 * Transition to RUNNING state when stopped. No-op if already in RUNNING
232 * state.
233 */
234 void start();
235
236 /**
237 * At RUNNING state, wait until all QueueGuard object created during
238 * RUNNING state are destroyed, and then transition to STOPPED state.
239 * No-op if already in STOPPED state.
240 */
241 void stop();
242
243 private:
244 Mutex mGuardLock;
245
246 struct Counter {
247 inline Counter() : value(-1) {}
248 int32_t value;
249 Condition cond;
250 };
251 Mutexed<Counter> mCount;
252
253 friend class CCodecBufferChannel::QueueGuard;
254 };
255
256 class QueueGuard {
257 public:
258 QueueGuard(QueueSync &sync);
259 ~QueueGuard();
260 inline bool isRunning() { return mRunning; }
261
262 private:
263 QueueSync &mSync;
264 bool mRunning;
265 };
266
Brian Lindahl932bf602023-03-09 11:59:48 -0700267 struct TrackedFrame {
268 uint64_t number;
269 int64_t mediaTimeUs;
270 int64_t desiredRenderTimeNs;
271 nsecs_t latchTime;
272 sp<Fence> presentFence;
273 };
274
Pawin Vongmasa36653902018-11-15 00:10:25 -0800275 void feedInputBufferIfAvailable();
276 void feedInputBufferIfAvailableInternal();
Sungtak Lee04b30352020-07-27 13:57:25 -0700277 status_t queueInputBufferInternal(sp<MediaCodecBuffer> buffer,
278 std::shared_ptr<C2LinearBlock> encryptedBlock = nullptr,
279 size_t blockSize = 0);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800280 bool handleWork(
281 std::unique_ptr<C2Work> work, const sp<AMessage> &outputFormat,
282 const C2StreamInitDataInfo::output *initData);
283 void sendOutputBuffers();
Wonsik Kimfb7a7672019-12-27 17:13:33 -0800284 void ensureDecryptDestination(size_t size);
285 int32_t getHeapSeqNum(const sp<hardware::HidlMemory> &memory);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800286
Brian Lindahl932bf602023-03-09 11:59:48 -0700287 void initializeFrameTrackingFor(ANativeWindow * window);
288 void trackReleasedFrame(const IGraphicBufferProducer::QueueBufferOutput& qbo,
289 int64_t mediaTimeUs, int64_t desiredRenderTimeNs);
290 void processRenderedFrames(const FrameEventHistoryDelta& delta);
291 int64_t getRenderTimeNs(const TrackedFrame& frame);
292
Pawin Vongmasa36653902018-11-15 00:10:25 -0800293 QueueSync mSync;
294 sp<MemoryDealer> mDealer;
295 sp<IMemory> mDecryptDestination;
296 int32_t mHeapSeqNum;
Wonsik Kimfb7a7672019-12-27 17:13:33 -0800297 std::map<wp<hardware::HidlMemory>, int32_t> mHeapSeqNumMap;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800298
299 std::shared_ptr<Codec2Client::Component> mComponent;
300 std::string mComponentName; ///< component name for debugging
301 const char *mName; ///< C-string version of component name
302 std::shared_ptr<CCodecCallback> mCCodecCallback;
303 std::shared_ptr<C2BlockPool> mInputAllocator;
304 QueueSync mQueueSync;
305 std::vector<std::unique_ptr<C2Param>> mParamsToBeSet;
306
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700307 struct Input {
308 Input();
309
310 std::unique_ptr<InputBuffers> buffers;
311 size_t numSlots;
312 FlexBuffersImpl extraBuffers;
313 size_t numExtraSlots;
314 uint32_t inputDelay;
315 uint32_t pipelineDelay;
Wonsik Kim6b2c8be2021-09-28 05:11:04 -0700316 c2_cntr64_t lastFlushIndex;
Wonsik Kime1104ca2020-11-24 15:01:33 -0800317
318 FrameReassembler frameReassembler;
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700319 };
320 Mutexed<Input> mInput;
321 struct Output {
322 std::unique_ptr<OutputBuffers> buffers;
323 size_t numSlots;
324 uint32_t outputDelay;
Wonsik Kim3722abc2023-05-17 13:26:31 -0700325 // true iff the underlying block pool is bounded --- for example,
326 // a BufferQueue-based block pool would be bounded by the BufferQueue.
327 bool bounded;
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700328 };
329 Mutexed<Output> mOutput;
Wonsik Kim5ebfcb22021-01-05 18:58:15 -0800330 Mutexed<std::list<std::unique_ptr<C2Work>>> mFlushedConfigs;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800331
332 std::atomic_uint64_t mFrameIndex;
333 std::atomic_uint64_t mFirstValidFrameIndex;
334
335 sp<MemoryDealer> makeMemoryDealer(size_t heapSize);
336
Brian Lindahl932bf602023-03-09 11:59:48 -0700337 std::deque<TrackedFrame> mTrackedFrames;
Brian Lindahld7967a92023-08-10 10:12:49 -0600338 bool mAreRenderMetricsEnabled;
Brian Lindahl2048d492023-04-05 08:49:23 -0600339 bool mIsSurfaceToDisplay;
Brian Lindahl932bf602023-03-09 11:59:48 -0700340 bool mHasPresentFenceTimes;
341
Pawin Vongmasa36653902018-11-15 00:10:25 -0800342 struct OutputSurface {
343 sp<Surface> surface;
344 uint32_t generation;
Wonsik Kimf5e5c832019-02-21 11:36:05 -0800345 int maxDequeueBuffers;
Byeongjo Park25c3a3d2020-06-12 17:24:21 +0900346 std::map<uint64_t, int> rotation;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800347 };
348 Mutexed<OutputSurface> mOutputSurface;
Wonsik Kim3a692e62023-05-19 15:37:22 -0700349 int mRenderingDepth;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800350
351 struct BlockPools {
352 C2Allocator::id_t inputAllocatorId;
353 std::shared_ptr<C2BlockPool> inputPool;
354 C2Allocator::id_t outputAllocatorId;
355 C2BlockPool::local_id_t outputPoolId;
356 std::shared_ptr<Codec2Client::Configurable> outputPoolIntf;
357 };
358 Mutexed<BlockPools> mBlockPools;
359
360 std::shared_ptr<InputSurfaceWrapper> mInputSurface;
361
362 MetaMode mMetaMode;
363
Wonsik Kimab34ed62019-01-31 15:28:46 -0800364 Mutexed<PipelineWatcher> mPipelineWatcher;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800365
Pawin Vongmasa36653902018-11-15 00:10:25 -0800366 std::atomic_bool mInputMetEos;
Wonsik Kimf7529dd2019-04-18 17:35:53 -0700367 std::once_flag mRenderWarningFlag;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800368
Wonsik Kim596187e2019-10-25 12:44:10 -0700369 sp<ICrypto> mCrypto;
370 sp<IDescrambler> mDescrambler;
371
Pawin Vongmasa36653902018-11-15 00:10:25 -0800372 inline bool hasCryptoOrDescrambler() {
373 return mCrypto != nullptr || mDescrambler != nullptr;
374 }
Sungtak Lee04b30352020-07-27 13:57:25 -0700375 std::atomic_bool mSendEncryptedInfoBuffer;
Wonsik Kimec585c32021-10-01 01:11:00 -0700376
377 std::atomic_bool mTunneled;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800378};
379
380// Conversion of a c2_status_t value to a status_t value may depend on the
381// operation that returns the c2_status_t value.
382enum c2_operation_t {
383 C2_OPERATION_NONE,
384 C2_OPERATION_Component_connectToOmxInputSurface,
385 C2_OPERATION_Component_createBlockPool,
386 C2_OPERATION_Component_destroyBlockPool,
387 C2_OPERATION_Component_disconnectFromInputSurface,
388 C2_OPERATION_Component_drain,
389 C2_OPERATION_Component_flush,
390 C2_OPERATION_Component_queue,
391 C2_OPERATION_Component_release,
392 C2_OPERATION_Component_reset,
393 C2_OPERATION_Component_setOutputSurface,
394 C2_OPERATION_Component_start,
395 C2_OPERATION_Component_stop,
396 C2_OPERATION_ComponentStore_copyBuffer,
397 C2_OPERATION_ComponentStore_createComponent,
398 C2_OPERATION_ComponentStore_createInputSurface,
399 C2_OPERATION_ComponentStore_createInterface,
400 C2_OPERATION_Configurable_config,
401 C2_OPERATION_Configurable_query,
402 C2_OPERATION_Configurable_querySupportedParams,
403 C2_OPERATION_Configurable_querySupportedValues,
404 C2_OPERATION_InputSurface_connectToComponent,
405 C2_OPERATION_InputSurfaceConnection_disconnect,
406};
407
408status_t toStatusT(c2_status_t c2s, c2_operation_t c2op = C2_OPERATION_NONE);
409
410} // namespace android
411
412#endif // CCODEC_BUFFER_CHANNEL_H_