blob: 763eae917a2eb405bdb2925d9001d704e4eefc56 [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,
89 const sp<MediaCodecBuffer> &buffer) override;
Pawin Vongmasa36653902018-11-15 00:10:25 -080090 virtual status_t renderOutputBuffer(
91 const sp<MediaCodecBuffer> &buffer, int64_t timestampNs) override;
Brian Lindahl932bf602023-03-09 11:59:48 -070092 virtual void pollForRenderedBuffers() override;
Pawin Vongmasa36653902018-11-15 00:10:25 -080093 virtual status_t discardBuffer(const sp<MediaCodecBuffer> &buffer) override;
94 virtual void getInputBufferArray(Vector<sp<MediaCodecBuffer>> *array) override;
95 virtual void getOutputBufferArray(Vector<sp<MediaCodecBuffer>> *array) override;
96
97 // Methods below are interface for CCodec to use.
98
99 /**
100 * Set the component object for buffer processing.
101 */
102 void setComponent(const std::shared_ptr<Codec2Client::Component> &component);
103
104 /**
105 * Set output graphic surface for rendering.
106 */
Sungtak Lee80c8e1e2023-01-26 11:03:14 +0000107 status_t setSurface(const sp<Surface> &surface, bool pushBlankBuffer);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800108
109 /**
110 * Set GraphicBufferSource object from which the component extracts input
111 * buffers.
112 */
113 status_t setInputSurface(const std::shared_ptr<InputSurfaceWrapper> &surface);
114
115 /**
116 * Signal EOS to input surface.
117 */
118 status_t signalEndOfInputStream();
119
120 /**
121 * Set parameters.
122 */
123 status_t setParameters(std::vector<std::unique_ptr<C2Param>> &params);
124
125 /**
126 * Start queueing buffers to the component. This object should never queue
127 * buffers before this call has completed.
128 */
Wonsik Kimfb7a7672019-12-27 17:13:33 -0800129 status_t start(
130 const sp<AMessage> &inputFormat,
131 const sp<AMessage> &outputFormat,
132 bool buffersBoundToCodec);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800133
134 /**
Wonsik Kim34b28b42022-05-20 15:49:32 -0700135 * Prepare initial input buffers to be filled by client.
136 *
137 * \param clientInputBuffers[out] pointer to slot index -> buffer map.
138 * On success, it contains prepared
139 * initial input buffers.
Pawin Vongmasa36653902018-11-15 00:10:25 -0800140 */
Wonsik Kim34b28b42022-05-20 15:49:32 -0700141 status_t prepareInitialInputBuffers(
142 std::map<size_t, sp<MediaCodecBuffer>> *clientInputBuffers);
143
144 /**
145 * Request initial input buffers as prepared in clientInputBuffers.
146 *
147 * \param clientInputBuffers[in] slot index -> buffer map with prepared
148 * initial input buffers.
149 */
150 status_t requestInitialInputBuffers(
151 std::map<size_t, sp<MediaCodecBuffer>> &&clientInputBuffers);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800152
153 /**
Sungtak Leed964e2e2022-07-30 08:43:58 +0000154 * Stop using buffers of the current output surface for other Codec
155 * instances to use the surface safely.
Sungtak Lee80c8e1e2023-01-26 11:03:14 +0000156 *
157 * \param pushBlankBuffer[in] push a blank buffer at the end if true
Sungtak Leed964e2e2022-07-30 08:43:58 +0000158 */
Sungtak Lee80c8e1e2023-01-26 11:03:14 +0000159 void stopUseOutputSurface(bool pushBlankBuffer);
Sungtak Leed964e2e2022-07-30 08:43:58 +0000160
161 /**
Pawin Vongmasa36653902018-11-15 00:10:25 -0800162 * Stop queueing buffers to the component. This object should never queue
163 * buffers after this call, until start() is called.
164 */
165 void stop();
166
Wonsik Kim936a89c2020-05-08 16:07:50 -0700167 /**
168 * Stop queueing buffers to the component and release all buffers.
169 */
170 void reset();
171
172 /**
173 * Release all resources.
174 */
175 void release();
176
Pawin Vongmasa36653902018-11-15 00:10:25 -0800177 void flush(const std::list<std::unique_ptr<C2Work>> &flushedWork);
178
179 /**
180 * Notify input client about work done.
181 *
182 * @param workItems finished work item.
183 * @param outputFormat new output format if it has changed, otherwise nullptr
184 * @param initData new init data (CSD) if it has changed, otherwise nullptr
Pawin Vongmasa36653902018-11-15 00:10:25 -0800185 */
186 void onWorkDone(
187 std::unique_ptr<C2Work> work, const sp<AMessage> &outputFormat,
Wonsik Kimab34ed62019-01-31 15:28:46 -0800188 const C2StreamInitDataInfo::output *initData);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800189
190 /**
191 * Make an input buffer available for the client as it is no longer needed
192 * by the codec.
193 *
Wonsik Kimab34ed62019-01-31 15:28:46 -0800194 * @param frameIndex The index of input work
195 * @param arrayIndex The index of buffer in the input work buffers.
Pawin Vongmasa36653902018-11-15 00:10:25 -0800196 */
Wonsik Kimab34ed62019-01-31 15:28:46 -0800197 void onInputBufferDone(uint64_t frameIndex, size_t arrayIndex);
198
199 PipelineWatcher::Clock::duration elapsed();
Pawin Vongmasa36653902018-11-15 00:10:25 -0800200
201 enum MetaMode {
202 MODE_NONE,
203 MODE_ANW,
204 };
205
206 void setMetaMode(MetaMode mode);
207
Pawin Vongmasa36653902018-11-15 00:10:25 -0800208private:
209 class QueueGuard;
210
211 /**
212 * Special mutex-like object with the following properties:
213 *
214 * - At STOPPED state (initial, or after stop())
215 * - QueueGuard object gets created at STOPPED state, and the client is
216 * supposed to return immediately.
217 * - At RUNNING state (after start())
218 * - Each QueueGuard object
219 */
220 class QueueSync {
221 public:
222 /**
223 * At construction the sync object is in STOPPED state.
224 */
225 inline QueueSync() {}
226 ~QueueSync() = default;
227
228 /**
229 * Transition to RUNNING state when stopped. No-op if already in RUNNING
230 * state.
231 */
232 void start();
233
234 /**
235 * At RUNNING state, wait until all QueueGuard object created during
236 * RUNNING state are destroyed, and then transition to STOPPED state.
237 * No-op if already in STOPPED state.
238 */
239 void stop();
240
241 private:
242 Mutex mGuardLock;
243
244 struct Counter {
245 inline Counter() : value(-1) {}
246 int32_t value;
247 Condition cond;
248 };
249 Mutexed<Counter> mCount;
250
251 friend class CCodecBufferChannel::QueueGuard;
252 };
253
254 class QueueGuard {
255 public:
256 QueueGuard(QueueSync &sync);
257 ~QueueGuard();
258 inline bool isRunning() { return mRunning; }
259
260 private:
261 QueueSync &mSync;
262 bool mRunning;
263 };
264
Brian Lindahl932bf602023-03-09 11:59:48 -0700265 struct TrackedFrame {
266 uint64_t number;
267 int64_t mediaTimeUs;
268 int64_t desiredRenderTimeNs;
269 nsecs_t latchTime;
270 sp<Fence> presentFence;
271 };
272
Pawin Vongmasa36653902018-11-15 00:10:25 -0800273 void feedInputBufferIfAvailable();
274 void feedInputBufferIfAvailableInternal();
Sungtak Lee04b30352020-07-27 13:57:25 -0700275 status_t queueInputBufferInternal(sp<MediaCodecBuffer> buffer,
276 std::shared_ptr<C2LinearBlock> encryptedBlock = nullptr,
277 size_t blockSize = 0);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800278 bool handleWork(
279 std::unique_ptr<C2Work> work, const sp<AMessage> &outputFormat,
280 const C2StreamInitDataInfo::output *initData);
281 void sendOutputBuffers();
Wonsik Kimfb7a7672019-12-27 17:13:33 -0800282 void ensureDecryptDestination(size_t size);
283 int32_t getHeapSeqNum(const sp<hardware::HidlMemory> &memory);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800284
Brian Lindahl932bf602023-03-09 11:59:48 -0700285 void initializeFrameTrackingFor(ANativeWindow * window);
286 void trackReleasedFrame(const IGraphicBufferProducer::QueueBufferOutput& qbo,
287 int64_t mediaTimeUs, int64_t desiredRenderTimeNs);
288 void processRenderedFrames(const FrameEventHistoryDelta& delta);
289 int64_t getRenderTimeNs(const TrackedFrame& frame);
290
Pawin Vongmasa36653902018-11-15 00:10:25 -0800291 QueueSync mSync;
292 sp<MemoryDealer> mDealer;
293 sp<IMemory> mDecryptDestination;
294 int32_t mHeapSeqNum;
Wonsik Kimfb7a7672019-12-27 17:13:33 -0800295 std::map<wp<hardware::HidlMemory>, int32_t> mHeapSeqNumMap;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800296
297 std::shared_ptr<Codec2Client::Component> mComponent;
298 std::string mComponentName; ///< component name for debugging
299 const char *mName; ///< C-string version of component name
300 std::shared_ptr<CCodecCallback> mCCodecCallback;
301 std::shared_ptr<C2BlockPool> mInputAllocator;
302 QueueSync mQueueSync;
303 std::vector<std::unique_ptr<C2Param>> mParamsToBeSet;
304
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700305 struct Input {
306 Input();
307
308 std::unique_ptr<InputBuffers> buffers;
309 size_t numSlots;
310 FlexBuffersImpl extraBuffers;
311 size_t numExtraSlots;
312 uint32_t inputDelay;
313 uint32_t pipelineDelay;
Wonsik Kim6b2c8be2021-09-28 05:11:04 -0700314 c2_cntr64_t lastFlushIndex;
Wonsik Kime1104ca2020-11-24 15:01:33 -0800315
316 FrameReassembler frameReassembler;
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700317 };
318 Mutexed<Input> mInput;
319 struct Output {
320 std::unique_ptr<OutputBuffers> buffers;
321 size_t numSlots;
322 uint32_t outputDelay;
323 };
324 Mutexed<Output> mOutput;
Wonsik Kim5ebfcb22021-01-05 18:58:15 -0800325 Mutexed<std::list<std::unique_ptr<C2Work>>> mFlushedConfigs;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800326
327 std::atomic_uint64_t mFrameIndex;
328 std::atomic_uint64_t mFirstValidFrameIndex;
329
330 sp<MemoryDealer> makeMemoryDealer(size_t heapSize);
331
Brian Lindahl932bf602023-03-09 11:59:48 -0700332 std::deque<TrackedFrame> mTrackedFrames;
Brian Lindahle31a52d2023-04-05 08:49:23 -0600333 bool mIsSurfaceToDisplay;
Brian Lindahl932bf602023-03-09 11:59:48 -0700334 bool mHasPresentFenceTimes;
335
Pawin Vongmasa36653902018-11-15 00:10:25 -0800336 struct OutputSurface {
337 sp<Surface> surface;
338 uint32_t generation;
Wonsik Kimf5e5c832019-02-21 11:36:05 -0800339 int maxDequeueBuffers;
Byeongjo Park25c3a3d2020-06-12 17:24:21 +0900340 std::map<uint64_t, int> rotation;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800341 };
342 Mutexed<OutputSurface> mOutputSurface;
343
344 struct BlockPools {
345 C2Allocator::id_t inputAllocatorId;
346 std::shared_ptr<C2BlockPool> inputPool;
347 C2Allocator::id_t outputAllocatorId;
348 C2BlockPool::local_id_t outputPoolId;
349 std::shared_ptr<Codec2Client::Configurable> outputPoolIntf;
350 };
351 Mutexed<BlockPools> mBlockPools;
352
353 std::shared_ptr<InputSurfaceWrapper> mInputSurface;
354
355 MetaMode mMetaMode;
356
Wonsik Kimab34ed62019-01-31 15:28:46 -0800357 Mutexed<PipelineWatcher> mPipelineWatcher;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800358
Pawin Vongmasa36653902018-11-15 00:10:25 -0800359 std::atomic_bool mInputMetEos;
Wonsik Kimf7529dd2019-04-18 17:35:53 -0700360 std::once_flag mRenderWarningFlag;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800361
Wonsik Kim596187e2019-10-25 12:44:10 -0700362 sp<ICrypto> mCrypto;
363 sp<IDescrambler> mDescrambler;
364
Pawin Vongmasa36653902018-11-15 00:10:25 -0800365 inline bool hasCryptoOrDescrambler() {
366 return mCrypto != nullptr || mDescrambler != nullptr;
367 }
Sungtak Lee04b30352020-07-27 13:57:25 -0700368 std::atomic_bool mSendEncryptedInfoBuffer;
Wonsik Kimec585c32021-10-01 01:11:00 -0700369
370 std::atomic_bool mTunneled;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800371};
372
373// Conversion of a c2_status_t value to a status_t value may depend on the
374// operation that returns the c2_status_t value.
375enum c2_operation_t {
376 C2_OPERATION_NONE,
377 C2_OPERATION_Component_connectToOmxInputSurface,
378 C2_OPERATION_Component_createBlockPool,
379 C2_OPERATION_Component_destroyBlockPool,
380 C2_OPERATION_Component_disconnectFromInputSurface,
381 C2_OPERATION_Component_drain,
382 C2_OPERATION_Component_flush,
383 C2_OPERATION_Component_queue,
384 C2_OPERATION_Component_release,
385 C2_OPERATION_Component_reset,
386 C2_OPERATION_Component_setOutputSurface,
387 C2_OPERATION_Component_start,
388 C2_OPERATION_Component_stop,
389 C2_OPERATION_ComponentStore_copyBuffer,
390 C2_OPERATION_ComponentStore_createComponent,
391 C2_OPERATION_ComponentStore_createInputSurface,
392 C2_OPERATION_ComponentStore_createInterface,
393 C2_OPERATION_Configurable_config,
394 C2_OPERATION_Configurable_query,
395 C2_OPERATION_Configurable_querySupportedParams,
396 C2_OPERATION_Configurable_querySupportedValues,
397 C2_OPERATION_InputSurface_connectToComponent,
398 C2_OPERATION_InputSurfaceConnection_disconnect,
399};
400
401status_t toStatusT(c2_status_t c2s, c2_operation_t c2op = C2_OPERATION_NONE);
402
403} // namespace android
404
405#endif // CCODEC_BUFFER_CHANNEL_H_