blob: b9e8d39e53efc7ec46574f95ee2be3d5b633948c [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;
48};
49
50/**
51 * BufferChannelBase implementation for CCodec.
52 */
53class CCodecBufferChannel
54 : public BufferChannelBase, public std::enable_shared_from_this<CCodecBufferChannel> {
55public:
56 explicit CCodecBufferChannel(const std::shared_ptr<CCodecCallback> &callback);
57 virtual ~CCodecBufferChannel();
58
59 // BufferChannelBase interface
Wonsik Kim596187e2019-10-25 12:44:10 -070060 void setCrypto(const sp<ICrypto> &crypto) override;
61 void setDescrambler(const sp<IDescrambler> &descrambler) override;
62
Pawin Vongmasa36653902018-11-15 00:10:25 -080063 virtual status_t queueInputBuffer(const sp<MediaCodecBuffer> &buffer) override;
64 virtual status_t queueSecureInputBuffer(
65 const sp<MediaCodecBuffer> &buffer,
66 bool secure,
67 const uint8_t *key,
68 const uint8_t *iv,
69 CryptoPlugin::Mode mode,
70 CryptoPlugin::Pattern pattern,
71 const CryptoPlugin::SubSample *subSamples,
72 size_t numSubSamples,
73 AString *errorDetailMsg) override;
Wonsik Kimfb7a7672019-12-27 17:13:33 -080074 virtual status_t attachBuffer(
75 const std::shared_ptr<C2Buffer> &c2Buffer,
76 const sp<MediaCodecBuffer> &buffer) override;
77 virtual status_t attachEncryptedBuffer(
78 const sp<hardware::HidlMemory> &memory,
79 bool secure,
80 const uint8_t *key,
81 const uint8_t *iv,
82 CryptoPlugin::Mode mode,
83 CryptoPlugin::Pattern pattern,
84 size_t offset,
85 const CryptoPlugin::SubSample *subSamples,
86 size_t numSubSamples,
87 const sp<MediaCodecBuffer> &buffer) override;
Pawin Vongmasa36653902018-11-15 00:10:25 -080088 virtual status_t renderOutputBuffer(
89 const sp<MediaCodecBuffer> &buffer, int64_t timestampNs) override;
90 virtual status_t discardBuffer(const sp<MediaCodecBuffer> &buffer) override;
91 virtual void getInputBufferArray(Vector<sp<MediaCodecBuffer>> *array) override;
92 virtual void getOutputBufferArray(Vector<sp<MediaCodecBuffer>> *array) override;
93
94 // Methods below are interface for CCodec to use.
95
96 /**
97 * Set the component object for buffer processing.
98 */
99 void setComponent(const std::shared_ptr<Codec2Client::Component> &component);
100
101 /**
102 * Set output graphic surface for rendering.
103 */
104 status_t setSurface(const sp<Surface> &surface);
105
106 /**
107 * Set GraphicBufferSource object from which the component extracts input
108 * buffers.
109 */
110 status_t setInputSurface(const std::shared_ptr<InputSurfaceWrapper> &surface);
111
112 /**
113 * Signal EOS to input surface.
114 */
115 status_t signalEndOfInputStream();
116
117 /**
118 * Set parameters.
119 */
120 status_t setParameters(std::vector<std::unique_ptr<C2Param>> &params);
121
122 /**
123 * Start queueing buffers to the component. This object should never queue
124 * buffers before this call has completed.
125 */
Wonsik Kimfb7a7672019-12-27 17:13:33 -0800126 status_t start(
127 const sp<AMessage> &inputFormat,
128 const sp<AMessage> &outputFormat,
129 bool buffersBoundToCodec);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800130
131 /**
132 * Request initial input buffers to be filled by client.
133 */
134 status_t requestInitialInputBuffers();
135
136 /**
137 * Stop queueing buffers to the component. This object should never queue
138 * buffers after this call, until start() is called.
139 */
140 void stop();
141
Wonsik Kim936a89c2020-05-08 16:07:50 -0700142 /**
143 * Stop queueing buffers to the component and release all buffers.
144 */
145 void reset();
146
147 /**
148 * Release all resources.
149 */
150 void release();
151
Pawin Vongmasa36653902018-11-15 00:10:25 -0800152 void flush(const std::list<std::unique_ptr<C2Work>> &flushedWork);
153
154 /**
155 * Notify input client about work done.
156 *
157 * @param workItems finished work item.
158 * @param outputFormat new output format if it has changed, otherwise nullptr
159 * @param initData new init data (CSD) if it has changed, otherwise nullptr
Pawin Vongmasa36653902018-11-15 00:10:25 -0800160 */
161 void onWorkDone(
162 std::unique_ptr<C2Work> work, const sp<AMessage> &outputFormat,
Wonsik Kimab34ed62019-01-31 15:28:46 -0800163 const C2StreamInitDataInfo::output *initData);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800164
165 /**
166 * Make an input buffer available for the client as it is no longer needed
167 * by the codec.
168 *
Wonsik Kimab34ed62019-01-31 15:28:46 -0800169 * @param frameIndex The index of input work
170 * @param arrayIndex The index of buffer in the input work buffers.
Pawin Vongmasa36653902018-11-15 00:10:25 -0800171 */
Wonsik Kimab34ed62019-01-31 15:28:46 -0800172 void onInputBufferDone(uint64_t frameIndex, size_t arrayIndex);
173
174 PipelineWatcher::Clock::duration elapsed();
Pawin Vongmasa36653902018-11-15 00:10:25 -0800175
176 enum MetaMode {
177 MODE_NONE,
178 MODE_ANW,
179 };
180
181 void setMetaMode(MetaMode mode);
182
Pawin Vongmasa36653902018-11-15 00:10:25 -0800183private:
184 class QueueGuard;
185
186 /**
187 * Special mutex-like object with the following properties:
188 *
189 * - At STOPPED state (initial, or after stop())
190 * - QueueGuard object gets created at STOPPED state, and the client is
191 * supposed to return immediately.
192 * - At RUNNING state (after start())
193 * - Each QueueGuard object
194 */
195 class QueueSync {
196 public:
197 /**
198 * At construction the sync object is in STOPPED state.
199 */
200 inline QueueSync() {}
201 ~QueueSync() = default;
202
203 /**
204 * Transition to RUNNING state when stopped. No-op if already in RUNNING
205 * state.
206 */
207 void start();
208
209 /**
210 * At RUNNING state, wait until all QueueGuard object created during
211 * RUNNING state are destroyed, and then transition to STOPPED state.
212 * No-op if already in STOPPED state.
213 */
214 void stop();
215
216 private:
217 Mutex mGuardLock;
218
219 struct Counter {
220 inline Counter() : value(-1) {}
221 int32_t value;
222 Condition cond;
223 };
224 Mutexed<Counter> mCount;
225
226 friend class CCodecBufferChannel::QueueGuard;
227 };
228
229 class QueueGuard {
230 public:
231 QueueGuard(QueueSync &sync);
232 ~QueueGuard();
233 inline bool isRunning() { return mRunning; }
234
235 private:
236 QueueSync &mSync;
237 bool mRunning;
238 };
239
240 void feedInputBufferIfAvailable();
241 void feedInputBufferIfAvailableInternal();
Sungtak Lee04b30352020-07-27 13:57:25 -0700242 status_t queueInputBufferInternal(sp<MediaCodecBuffer> buffer,
243 std::shared_ptr<C2LinearBlock> encryptedBlock = nullptr,
244 size_t blockSize = 0);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800245 bool handleWork(
246 std::unique_ptr<C2Work> work, const sp<AMessage> &outputFormat,
247 const C2StreamInitDataInfo::output *initData);
248 void sendOutputBuffers();
Wonsik Kimfb7a7672019-12-27 17:13:33 -0800249 void ensureDecryptDestination(size_t size);
250 int32_t getHeapSeqNum(const sp<hardware::HidlMemory> &memory);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800251
252 QueueSync mSync;
253 sp<MemoryDealer> mDealer;
254 sp<IMemory> mDecryptDestination;
255 int32_t mHeapSeqNum;
Wonsik Kimfb7a7672019-12-27 17:13:33 -0800256 std::map<wp<hardware::HidlMemory>, int32_t> mHeapSeqNumMap;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800257
258 std::shared_ptr<Codec2Client::Component> mComponent;
259 std::string mComponentName; ///< component name for debugging
260 const char *mName; ///< C-string version of component name
261 std::shared_ptr<CCodecCallback> mCCodecCallback;
262 std::shared_ptr<C2BlockPool> mInputAllocator;
263 QueueSync mQueueSync;
264 std::vector<std::unique_ptr<C2Param>> mParamsToBeSet;
265
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700266 struct Input {
267 Input();
268
269 std::unique_ptr<InputBuffers> buffers;
270 size_t numSlots;
271 FlexBuffersImpl extraBuffers;
272 size_t numExtraSlots;
273 uint32_t inputDelay;
274 uint32_t pipelineDelay;
Wonsik Kime1104ca2020-11-24 15:01:33 -0800275
276 FrameReassembler frameReassembler;
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700277 };
278 Mutexed<Input> mInput;
279 struct Output {
280 std::unique_ptr<OutputBuffers> buffers;
281 size_t numSlots;
282 uint32_t outputDelay;
283 };
284 Mutexed<Output> mOutput;
Wonsik Kim5ebfcb22021-01-05 18:58:15 -0800285 Mutexed<std::list<std::unique_ptr<C2Work>>> mFlushedConfigs;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800286
287 std::atomic_uint64_t mFrameIndex;
288 std::atomic_uint64_t mFirstValidFrameIndex;
289
290 sp<MemoryDealer> makeMemoryDealer(size_t heapSize);
291
292 struct OutputSurface {
293 sp<Surface> surface;
294 uint32_t generation;
Wonsik Kimf5e5c832019-02-21 11:36:05 -0800295 int maxDequeueBuffers;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800296 };
297 Mutexed<OutputSurface> mOutputSurface;
298
299 struct BlockPools {
300 C2Allocator::id_t inputAllocatorId;
301 std::shared_ptr<C2BlockPool> inputPool;
302 C2Allocator::id_t outputAllocatorId;
303 C2BlockPool::local_id_t outputPoolId;
304 std::shared_ptr<Codec2Client::Configurable> outputPoolIntf;
305 };
306 Mutexed<BlockPools> mBlockPools;
307
308 std::shared_ptr<InputSurfaceWrapper> mInputSurface;
309
310 MetaMode mMetaMode;
311
Wonsik Kimab34ed62019-01-31 15:28:46 -0800312 Mutexed<PipelineWatcher> mPipelineWatcher;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800313
Pawin Vongmasa36653902018-11-15 00:10:25 -0800314 std::atomic_bool mInputMetEos;
Wonsik Kimf7529dd2019-04-18 17:35:53 -0700315 std::once_flag mRenderWarningFlag;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800316
Wonsik Kim596187e2019-10-25 12:44:10 -0700317 sp<ICrypto> mCrypto;
318 sp<IDescrambler> mDescrambler;
319
Pawin Vongmasa36653902018-11-15 00:10:25 -0800320 inline bool hasCryptoOrDescrambler() {
321 return mCrypto != nullptr || mDescrambler != nullptr;
322 }
Sungtak Lee04b30352020-07-27 13:57:25 -0700323 std::atomic_bool mSendEncryptedInfoBuffer;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800324};
325
326// Conversion of a c2_status_t value to a status_t value may depend on the
327// operation that returns the c2_status_t value.
328enum c2_operation_t {
329 C2_OPERATION_NONE,
330 C2_OPERATION_Component_connectToOmxInputSurface,
331 C2_OPERATION_Component_createBlockPool,
332 C2_OPERATION_Component_destroyBlockPool,
333 C2_OPERATION_Component_disconnectFromInputSurface,
334 C2_OPERATION_Component_drain,
335 C2_OPERATION_Component_flush,
336 C2_OPERATION_Component_queue,
337 C2_OPERATION_Component_release,
338 C2_OPERATION_Component_reset,
339 C2_OPERATION_Component_setOutputSurface,
340 C2_OPERATION_Component_start,
341 C2_OPERATION_Component_stop,
342 C2_OPERATION_ComponentStore_copyBuffer,
343 C2_OPERATION_ComponentStore_createComponent,
344 C2_OPERATION_ComponentStore_createInputSurface,
345 C2_OPERATION_ComponentStore_createInterface,
346 C2_OPERATION_Configurable_config,
347 C2_OPERATION_Configurable_query,
348 C2_OPERATION_Configurable_querySupportedParams,
349 C2_OPERATION_Configurable_querySupportedValues,
350 C2_OPERATION_InputSurface_connectToComponent,
351 C2_OPERATION_InputSurfaceConnection_disconnect,
352};
353
354status_t toStatusT(c2_status_t c2s, c2_operation_t c2op = C2_OPERATION_NONE);
355
356} // namespace android
357
358#endif // CCODEC_BUFFER_CHANNEL_H_