blob: b3a5f4b2481a9442b6e5e51243fbe061b837d65b [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"
Wonsik Kime1104ca2020-11-24 15:01:33 -080034#include "FrameReassembler.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;
Guillaume Chelfi867d4dd2021-07-01 18:38:45 +020048 virtual void onFirstTunnelFrameReady() = 0;
Pawin Vongmasa36653902018-11-15 00:10:25 -080049};
50
51/**
52 * BufferChannelBase implementation for CCodec.
53 */
54class CCodecBufferChannel
55 : public BufferChannelBase, public std::enable_shared_from_this<CCodecBufferChannel> {
56public:
57 explicit CCodecBufferChannel(const std::shared_ptr<CCodecCallback> &callback);
58 virtual ~CCodecBufferChannel();
59
60 // BufferChannelBase interface
Wonsik Kim596187e2019-10-25 12:44:10 -070061 void setCrypto(const sp<ICrypto> &crypto) override;
62 void setDescrambler(const sp<IDescrambler> &descrambler) override;
63
Pawin Vongmasa36653902018-11-15 00:10:25 -080064 virtual status_t queueInputBuffer(const sp<MediaCodecBuffer> &buffer) override;
65 virtual status_t queueSecureInputBuffer(
66 const sp<MediaCodecBuffer> &buffer,
67 bool secure,
68 const uint8_t *key,
69 const uint8_t *iv,
70 CryptoPlugin::Mode mode,
71 CryptoPlugin::Pattern pattern,
72 const CryptoPlugin::SubSample *subSamples,
73 size_t numSubSamples,
74 AString *errorDetailMsg) override;
Wonsik Kimfb7a7672019-12-27 17:13:33 -080075 virtual status_t attachBuffer(
76 const std::shared_ptr<C2Buffer> &c2Buffer,
77 const sp<MediaCodecBuffer> &buffer) override;
78 virtual status_t attachEncryptedBuffer(
79 const sp<hardware::HidlMemory> &memory,
80 bool secure,
81 const uint8_t *key,
82 const uint8_t *iv,
83 CryptoPlugin::Mode mode,
84 CryptoPlugin::Pattern pattern,
85 size_t offset,
86 const CryptoPlugin::SubSample *subSamples,
87 size_t numSubSamples,
88 const sp<MediaCodecBuffer> &buffer) override;
Pawin Vongmasa36653902018-11-15 00:10:25 -080089 virtual status_t renderOutputBuffer(
90 const sp<MediaCodecBuffer> &buffer, int64_t timestampNs) override;
91 virtual status_t discardBuffer(const sp<MediaCodecBuffer> &buffer) override;
92 virtual void getInputBufferArray(Vector<sp<MediaCodecBuffer>> *array) override;
93 virtual void getOutputBufferArray(Vector<sp<MediaCodecBuffer>> *array) override;
94
95 // Methods below are interface for CCodec to use.
96
97 /**
98 * Set the component object for buffer processing.
99 */
100 void setComponent(const std::shared_ptr<Codec2Client::Component> &component);
101
102 /**
103 * Set output graphic surface for rendering.
104 */
105 status_t setSurface(const sp<Surface> &surface);
106
107 /**
108 * Set GraphicBufferSource object from which the component extracts input
109 * buffers.
110 */
111 status_t setInputSurface(const std::shared_ptr<InputSurfaceWrapper> &surface);
112
113 /**
114 * Signal EOS to input surface.
115 */
116 status_t signalEndOfInputStream();
117
118 /**
119 * Set parameters.
120 */
121 status_t setParameters(std::vector<std::unique_ptr<C2Param>> &params);
122
123 /**
124 * Start queueing buffers to the component. This object should never queue
125 * buffers before this call has completed.
126 */
Wonsik Kimfb7a7672019-12-27 17:13:33 -0800127 status_t start(
128 const sp<AMessage> &inputFormat,
129 const sp<AMessage> &outputFormat,
130 bool buffersBoundToCodec);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800131
132 /**
133 * Request initial input buffers to be filled by client.
134 */
135 status_t requestInitialInputBuffers();
136
137 /**
138 * Stop queueing buffers to the component. This object should never queue
139 * buffers after this call, until start() is called.
140 */
141 void stop();
142
Wonsik Kim936a89c2020-05-08 16:07:50 -0700143 /**
144 * Stop queueing buffers to the component and release all buffers.
145 */
146 void reset();
147
148 /**
149 * Release all resources.
150 */
151 void release();
152
Pawin Vongmasa36653902018-11-15 00:10:25 -0800153 void flush(const std::list<std::unique_ptr<C2Work>> &flushedWork);
154
155 /**
156 * Notify input client about work done.
157 *
158 * @param workItems finished work item.
159 * @param outputFormat new output format if it has changed, otherwise nullptr
160 * @param initData new init data (CSD) if it has changed, otherwise nullptr
Pawin Vongmasa36653902018-11-15 00:10:25 -0800161 */
162 void onWorkDone(
163 std::unique_ptr<C2Work> work, const sp<AMessage> &outputFormat,
Wonsik Kimab34ed62019-01-31 15:28:46 -0800164 const C2StreamInitDataInfo::output *initData);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800165
166 /**
167 * Make an input buffer available for the client as it is no longer needed
168 * by the codec.
169 *
Wonsik Kimab34ed62019-01-31 15:28:46 -0800170 * @param frameIndex The index of input work
171 * @param arrayIndex The index of buffer in the input work buffers.
Pawin Vongmasa36653902018-11-15 00:10:25 -0800172 */
Wonsik Kimab34ed62019-01-31 15:28:46 -0800173 void onInputBufferDone(uint64_t frameIndex, size_t arrayIndex);
174
175 PipelineWatcher::Clock::duration elapsed();
Pawin Vongmasa36653902018-11-15 00:10:25 -0800176
177 enum MetaMode {
178 MODE_NONE,
179 MODE_ANW,
180 };
181
182 void setMetaMode(MetaMode mode);
183
Guillaume Chelfi2d4c9db2022-03-18 13:43:49 +0100184 /**
185 * Push a blank buffer to the configured native output surface.
186 */
187 status_t pushBlankBufferToOutputSurface();
188
Pawin Vongmasa36653902018-11-15 00:10:25 -0800189private:
190 class QueueGuard;
191
192 /**
193 * Special mutex-like object with the following properties:
194 *
195 * - At STOPPED state (initial, or after stop())
196 * - QueueGuard object gets created at STOPPED state, and the client is
197 * supposed to return immediately.
198 * - At RUNNING state (after start())
199 * - Each QueueGuard object
200 */
201 class QueueSync {
202 public:
203 /**
204 * At construction the sync object is in STOPPED state.
205 */
206 inline QueueSync() {}
207 ~QueueSync() = default;
208
209 /**
210 * Transition to RUNNING state when stopped. No-op if already in RUNNING
211 * state.
212 */
213 void start();
214
215 /**
216 * At RUNNING state, wait until all QueueGuard object created during
217 * RUNNING state are destroyed, and then transition to STOPPED state.
218 * No-op if already in STOPPED state.
219 */
220 void stop();
221
222 private:
223 Mutex mGuardLock;
224
225 struct Counter {
226 inline Counter() : value(-1) {}
227 int32_t value;
228 Condition cond;
229 };
230 Mutexed<Counter> mCount;
231
232 friend class CCodecBufferChannel::QueueGuard;
233 };
234
235 class QueueGuard {
236 public:
237 QueueGuard(QueueSync &sync);
238 ~QueueGuard();
239 inline bool isRunning() { return mRunning; }
240
241 private:
242 QueueSync &mSync;
243 bool mRunning;
244 };
245
246 void feedInputBufferIfAvailable();
247 void feedInputBufferIfAvailableInternal();
Sungtak Lee04b30352020-07-27 13:57:25 -0700248 status_t queueInputBufferInternal(sp<MediaCodecBuffer> buffer,
249 std::shared_ptr<C2LinearBlock> encryptedBlock = nullptr,
250 size_t blockSize = 0);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800251 bool handleWork(
252 std::unique_ptr<C2Work> work, const sp<AMessage> &outputFormat,
253 const C2StreamInitDataInfo::output *initData);
254 void sendOutputBuffers();
Wonsik Kimfb7a7672019-12-27 17:13:33 -0800255 void ensureDecryptDestination(size_t size);
256 int32_t getHeapSeqNum(const sp<hardware::HidlMemory> &memory);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800257
258 QueueSync mSync;
259 sp<MemoryDealer> mDealer;
260 sp<IMemory> mDecryptDestination;
261 int32_t mHeapSeqNum;
Wonsik Kimfb7a7672019-12-27 17:13:33 -0800262 std::map<wp<hardware::HidlMemory>, int32_t> mHeapSeqNumMap;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800263
264 std::shared_ptr<Codec2Client::Component> mComponent;
265 std::string mComponentName; ///< component name for debugging
266 const char *mName; ///< C-string version of component name
267 std::shared_ptr<CCodecCallback> mCCodecCallback;
268 std::shared_ptr<C2BlockPool> mInputAllocator;
269 QueueSync mQueueSync;
270 std::vector<std::unique_ptr<C2Param>> mParamsToBeSet;
271
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700272 struct Input {
273 Input();
274
275 std::unique_ptr<InputBuffers> buffers;
276 size_t numSlots;
277 FlexBuffersImpl extraBuffers;
278 size_t numExtraSlots;
279 uint32_t inputDelay;
280 uint32_t pipelineDelay;
Wonsik Kim6b2c8be2021-09-28 05:11:04 -0700281 c2_cntr64_t lastFlushIndex;
Wonsik Kime1104ca2020-11-24 15:01:33 -0800282
283 FrameReassembler frameReassembler;
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700284 };
285 Mutexed<Input> mInput;
286 struct Output {
287 std::unique_ptr<OutputBuffers> buffers;
288 size_t numSlots;
289 uint32_t outputDelay;
290 };
291 Mutexed<Output> mOutput;
Wonsik Kim5ebfcb22021-01-05 18:58:15 -0800292 Mutexed<std::list<std::unique_ptr<C2Work>>> mFlushedConfigs;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800293
294 std::atomic_uint64_t mFrameIndex;
295 std::atomic_uint64_t mFirstValidFrameIndex;
296
297 sp<MemoryDealer> makeMemoryDealer(size_t heapSize);
298
299 struct OutputSurface {
300 sp<Surface> surface;
301 uint32_t generation;
Wonsik Kimf5e5c832019-02-21 11:36:05 -0800302 int maxDequeueBuffers;
Byeongjo Park25c3a3d2020-06-12 17:24:21 +0900303 std::map<uint64_t, int> rotation;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800304 };
305 Mutexed<OutputSurface> mOutputSurface;
306
307 struct BlockPools {
308 C2Allocator::id_t inputAllocatorId;
309 std::shared_ptr<C2BlockPool> inputPool;
310 C2Allocator::id_t outputAllocatorId;
311 C2BlockPool::local_id_t outputPoolId;
312 std::shared_ptr<Codec2Client::Configurable> outputPoolIntf;
313 };
314 Mutexed<BlockPools> mBlockPools;
315
316 std::shared_ptr<InputSurfaceWrapper> mInputSurface;
317
318 MetaMode mMetaMode;
319
Wonsik Kimab34ed62019-01-31 15:28:46 -0800320 Mutexed<PipelineWatcher> mPipelineWatcher;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800321
Pawin Vongmasa36653902018-11-15 00:10:25 -0800322 std::atomic_bool mInputMetEos;
Wonsik Kimf7529dd2019-04-18 17:35:53 -0700323 std::once_flag mRenderWarningFlag;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800324
Wonsik Kim596187e2019-10-25 12:44:10 -0700325 sp<ICrypto> mCrypto;
326 sp<IDescrambler> mDescrambler;
327
Pawin Vongmasa36653902018-11-15 00:10:25 -0800328 inline bool hasCryptoOrDescrambler() {
329 return mCrypto != nullptr || mDescrambler != nullptr;
330 }
Sungtak Lee04b30352020-07-27 13:57:25 -0700331 std::atomic_bool mSendEncryptedInfoBuffer;
Wonsik Kimec585c32021-10-01 01:11:00 -0700332
333 std::atomic_bool mTunneled;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800334};
335
336// Conversion of a c2_status_t value to a status_t value may depend on the
337// operation that returns the c2_status_t value.
338enum c2_operation_t {
339 C2_OPERATION_NONE,
340 C2_OPERATION_Component_connectToOmxInputSurface,
341 C2_OPERATION_Component_createBlockPool,
342 C2_OPERATION_Component_destroyBlockPool,
343 C2_OPERATION_Component_disconnectFromInputSurface,
344 C2_OPERATION_Component_drain,
345 C2_OPERATION_Component_flush,
346 C2_OPERATION_Component_queue,
347 C2_OPERATION_Component_release,
348 C2_OPERATION_Component_reset,
349 C2_OPERATION_Component_setOutputSurface,
350 C2_OPERATION_Component_start,
351 C2_OPERATION_Component_stop,
352 C2_OPERATION_ComponentStore_copyBuffer,
353 C2_OPERATION_ComponentStore_createComponent,
354 C2_OPERATION_ComponentStore_createInputSurface,
355 C2_OPERATION_ComponentStore_createInterface,
356 C2_OPERATION_Configurable_config,
357 C2_OPERATION_Configurable_query,
358 C2_OPERATION_Configurable_querySupportedParams,
359 C2_OPERATION_Configurable_querySupportedValues,
360 C2_OPERATION_InputSurface_connectToComponent,
361 C2_OPERATION_InputSurfaceConnection_disconnect,
362};
363
364status_t toStatusT(c2_status_t c2s, c2_operation_t c2op = C2_OPERATION_NONE);
365
366} // namespace android
367
368#endif // CCODEC_BUFFER_CHANNEL_H_