blob: 046c5c3c49c9b808de9ed9e3e9da4cbbc66905f2 [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"
Pawin Vongmasa36653902018-11-15 00:10:25 -080034#include "InputSurfaceWrapper.h"
Wonsik Kimab34ed62019-01-31 15:28:46 -080035#include "PipelineWatcher.h"
Pawin Vongmasa36653902018-11-15 00:10:25 -080036
37namespace android {
38
Wonsik Kim078b58e2019-01-09 15:08:06 -080039class MemoryDealer;
40
Pawin Vongmasa36653902018-11-15 00:10:25 -080041class CCodecCallback {
42public:
43 virtual ~CCodecCallback() = default;
44 virtual void onError(status_t err, enum ActionCode actionCode) = 0;
45 virtual void onOutputFramesRendered(int64_t mediaTimeUs, nsecs_t renderTimeNs) = 0;
Pawin Vongmasa36653902018-11-15 00:10:25 -080046 virtual void onOutputBuffersChanged() = 0;
47};
48
49/**
50 * BufferChannelBase implementation for CCodec.
51 */
52class CCodecBufferChannel
53 : public BufferChannelBase, public std::enable_shared_from_this<CCodecBufferChannel> {
54public:
55 explicit CCodecBufferChannel(const std::shared_ptr<CCodecCallback> &callback);
56 virtual ~CCodecBufferChannel();
57
58 // BufferChannelBase interface
Wonsik Kim596187e2019-10-25 12:44:10 -070059 void setCrypto(const sp<ICrypto> &crypto) override;
60 void setDescrambler(const sp<IDescrambler> &descrambler) override;
61
Pawin Vongmasa36653902018-11-15 00:10:25 -080062 virtual status_t queueInputBuffer(const sp<MediaCodecBuffer> &buffer) override;
63 virtual status_t queueSecureInputBuffer(
64 const sp<MediaCodecBuffer> &buffer,
65 bool secure,
66 const uint8_t *key,
67 const uint8_t *iv,
68 CryptoPlugin::Mode mode,
69 CryptoPlugin::Pattern pattern,
70 const CryptoPlugin::SubSample *subSamples,
71 size_t numSubSamples,
72 AString *errorDetailMsg) override;
Wonsik Kimfb7a7672019-12-27 17:13:33 -080073 virtual status_t attachBuffer(
74 const std::shared_ptr<C2Buffer> &c2Buffer,
75 const sp<MediaCodecBuffer> &buffer) override;
76 virtual status_t attachEncryptedBuffer(
77 const sp<hardware::HidlMemory> &memory,
78 bool secure,
79 const uint8_t *key,
80 const uint8_t *iv,
81 CryptoPlugin::Mode mode,
82 CryptoPlugin::Pattern pattern,
83 size_t offset,
84 const CryptoPlugin::SubSample *subSamples,
85 size_t numSubSamples,
86 const sp<MediaCodecBuffer> &buffer) override;
Pawin Vongmasa36653902018-11-15 00:10:25 -080087 virtual status_t renderOutputBuffer(
88 const sp<MediaCodecBuffer> &buffer, int64_t timestampNs) override;
89 virtual status_t discardBuffer(const sp<MediaCodecBuffer> &buffer) override;
90 virtual void getInputBufferArray(Vector<sp<MediaCodecBuffer>> *array) override;
91 virtual void getOutputBufferArray(Vector<sp<MediaCodecBuffer>> *array) override;
92
93 // Methods below are interface for CCodec to use.
94
95 /**
96 * Set the component object for buffer processing.
97 */
98 void setComponent(const std::shared_ptr<Codec2Client::Component> &component);
99
100 /**
101 * Set output graphic surface for rendering.
102 */
103 status_t setSurface(const sp<Surface> &surface);
104
105 /**
106 * Set GraphicBufferSource object from which the component extracts input
107 * buffers.
108 */
109 status_t setInputSurface(const std::shared_ptr<InputSurfaceWrapper> &surface);
110
111 /**
112 * Signal EOS to input surface.
113 */
114 status_t signalEndOfInputStream();
115
116 /**
117 * Set parameters.
118 */
119 status_t setParameters(std::vector<std::unique_ptr<C2Param>> &params);
120
121 /**
122 * Start queueing buffers to the component. This object should never queue
123 * buffers before this call has completed.
124 */
Wonsik Kimfb7a7672019-12-27 17:13:33 -0800125 status_t start(
126 const sp<AMessage> &inputFormat,
127 const sp<AMessage> &outputFormat,
128 bool buffersBoundToCodec);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800129
130 /**
131 * Request initial input buffers to be filled by client.
132 */
133 status_t requestInitialInputBuffers();
134
135 /**
136 * Stop queueing buffers to the component. This object should never queue
137 * buffers after this call, until start() is called.
138 */
139 void stop();
140
Wonsik Kim936a89c2020-05-08 16:07:50 -0700141 /**
142 * Stop queueing buffers to the component and release all buffers.
143 */
144 void reset();
145
146 /**
147 * Release all resources.
148 */
149 void release();
150
Pawin Vongmasa36653902018-11-15 00:10:25 -0800151 void flush(const std::list<std::unique_ptr<C2Work>> &flushedWork);
152
153 /**
154 * Notify input client about work done.
155 *
156 * @param workItems finished work item.
157 * @param outputFormat new output format if it has changed, otherwise nullptr
158 * @param initData new init data (CSD) if it has changed, otherwise nullptr
Pawin Vongmasa36653902018-11-15 00:10:25 -0800159 */
160 void onWorkDone(
161 std::unique_ptr<C2Work> work, const sp<AMessage> &outputFormat,
Wonsik Kimab34ed62019-01-31 15:28:46 -0800162 const C2StreamInitDataInfo::output *initData);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800163
164 /**
165 * Make an input buffer available for the client as it is no longer needed
166 * by the codec.
167 *
Wonsik Kimab34ed62019-01-31 15:28:46 -0800168 * @param frameIndex The index of input work
169 * @param arrayIndex The index of buffer in the input work buffers.
Pawin Vongmasa36653902018-11-15 00:10:25 -0800170 */
Wonsik Kimab34ed62019-01-31 15:28:46 -0800171 void onInputBufferDone(uint64_t frameIndex, size_t arrayIndex);
172
173 PipelineWatcher::Clock::duration elapsed();
Pawin Vongmasa36653902018-11-15 00:10:25 -0800174
175 enum MetaMode {
176 MODE_NONE,
177 MODE_ANW,
178 };
179
180 void setMetaMode(MetaMode mode);
181
Pawin Vongmasa36653902018-11-15 00:10:25 -0800182private:
183 class QueueGuard;
184
185 /**
186 * Special mutex-like object with the following properties:
187 *
188 * - At STOPPED state (initial, or after stop())
189 * - QueueGuard object gets created at STOPPED state, and the client is
190 * supposed to return immediately.
191 * - At RUNNING state (after start())
192 * - Each QueueGuard object
193 */
194 class QueueSync {
195 public:
196 /**
197 * At construction the sync object is in STOPPED state.
198 */
199 inline QueueSync() {}
200 ~QueueSync() = default;
201
202 /**
203 * Transition to RUNNING state when stopped. No-op if already in RUNNING
204 * state.
205 */
206 void start();
207
208 /**
209 * At RUNNING state, wait until all QueueGuard object created during
210 * RUNNING state are destroyed, and then transition to STOPPED state.
211 * No-op if already in STOPPED state.
212 */
213 void stop();
214
215 private:
216 Mutex mGuardLock;
217
218 struct Counter {
219 inline Counter() : value(-1) {}
220 int32_t value;
221 Condition cond;
222 };
223 Mutexed<Counter> mCount;
224
225 friend class CCodecBufferChannel::QueueGuard;
226 };
227
228 class QueueGuard {
229 public:
230 QueueGuard(QueueSync &sync);
231 ~QueueGuard();
232 inline bool isRunning() { return mRunning; }
233
234 private:
235 QueueSync &mSync;
236 bool mRunning;
237 };
238
239 void feedInputBufferIfAvailable();
240 void feedInputBufferIfAvailableInternal();
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700241 status_t queueInputBufferInternal(sp<MediaCodecBuffer> buffer);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800242 bool handleWork(
243 std::unique_ptr<C2Work> work, const sp<AMessage> &outputFormat,
244 const C2StreamInitDataInfo::output *initData);
245 void sendOutputBuffers();
Wonsik Kimfb7a7672019-12-27 17:13:33 -0800246 void ensureDecryptDestination(size_t size);
247 int32_t getHeapSeqNum(const sp<hardware::HidlMemory> &memory);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800248
249 QueueSync mSync;
250 sp<MemoryDealer> mDealer;
251 sp<IMemory> mDecryptDestination;
252 int32_t mHeapSeqNum;
Wonsik Kimfb7a7672019-12-27 17:13:33 -0800253 std::map<wp<hardware::HidlMemory>, int32_t> mHeapSeqNumMap;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800254
255 std::shared_ptr<Codec2Client::Component> mComponent;
256 std::string mComponentName; ///< component name for debugging
257 const char *mName; ///< C-string version of component name
258 std::shared_ptr<CCodecCallback> mCCodecCallback;
259 std::shared_ptr<C2BlockPool> mInputAllocator;
260 QueueSync mQueueSync;
261 std::vector<std::unique_ptr<C2Param>> mParamsToBeSet;
262
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700263 struct Input {
264 Input();
265
266 std::unique_ptr<InputBuffers> buffers;
267 size_t numSlots;
268 FlexBuffersImpl extraBuffers;
269 size_t numExtraSlots;
270 uint32_t inputDelay;
271 uint32_t pipelineDelay;
272 };
273 Mutexed<Input> mInput;
274 struct Output {
275 std::unique_ptr<OutputBuffers> buffers;
276 size_t numSlots;
277 uint32_t outputDelay;
278 };
279 Mutexed<Output> mOutput;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800280 Mutexed<std::list<sp<ABuffer>>> mFlushedConfigs;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800281
282 std::atomic_uint64_t mFrameIndex;
283 std::atomic_uint64_t mFirstValidFrameIndex;
284
285 sp<MemoryDealer> makeMemoryDealer(size_t heapSize);
286
287 struct OutputSurface {
288 sp<Surface> surface;
289 uint32_t generation;
Wonsik Kimf5e5c832019-02-21 11:36:05 -0800290 int maxDequeueBuffers;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800291 };
292 Mutexed<OutputSurface> mOutputSurface;
293
294 struct BlockPools {
295 C2Allocator::id_t inputAllocatorId;
296 std::shared_ptr<C2BlockPool> inputPool;
297 C2Allocator::id_t outputAllocatorId;
298 C2BlockPool::local_id_t outputPoolId;
299 std::shared_ptr<Codec2Client::Configurable> outputPoolIntf;
300 };
301 Mutexed<BlockPools> mBlockPools;
302
303 std::shared_ptr<InputSurfaceWrapper> mInputSurface;
304
305 MetaMode mMetaMode;
306
Wonsik Kimab34ed62019-01-31 15:28:46 -0800307 Mutexed<PipelineWatcher> mPipelineWatcher;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800308
Pawin Vongmasa36653902018-11-15 00:10:25 -0800309 std::atomic_bool mInputMetEos;
Wonsik Kimf7529dd2019-04-18 17:35:53 -0700310 std::once_flag mRenderWarningFlag;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800311
Wonsik Kim596187e2019-10-25 12:44:10 -0700312 sp<ICrypto> mCrypto;
313 sp<IDescrambler> mDescrambler;
314
Pawin Vongmasa36653902018-11-15 00:10:25 -0800315 inline bool hasCryptoOrDescrambler() {
316 return mCrypto != nullptr || mDescrambler != nullptr;
317 }
318};
319
320// Conversion of a c2_status_t value to a status_t value may depend on the
321// operation that returns the c2_status_t value.
322enum c2_operation_t {
323 C2_OPERATION_NONE,
324 C2_OPERATION_Component_connectToOmxInputSurface,
325 C2_OPERATION_Component_createBlockPool,
326 C2_OPERATION_Component_destroyBlockPool,
327 C2_OPERATION_Component_disconnectFromInputSurface,
328 C2_OPERATION_Component_drain,
329 C2_OPERATION_Component_flush,
330 C2_OPERATION_Component_queue,
331 C2_OPERATION_Component_release,
332 C2_OPERATION_Component_reset,
333 C2_OPERATION_Component_setOutputSurface,
334 C2_OPERATION_Component_start,
335 C2_OPERATION_Component_stop,
336 C2_OPERATION_ComponentStore_copyBuffer,
337 C2_OPERATION_ComponentStore_createComponent,
338 C2_OPERATION_ComponentStore_createInputSurface,
339 C2_OPERATION_ComponentStore_createInterface,
340 C2_OPERATION_Configurable_config,
341 C2_OPERATION_Configurable_query,
342 C2_OPERATION_Configurable_querySupportedParams,
343 C2_OPERATION_Configurable_querySupportedValues,
344 C2_OPERATION_InputSurface_connectToComponent,
345 C2_OPERATION_InputSurfaceConnection_disconnect,
346};
347
348status_t toStatusT(c2_status_t c2s, c2_operation_t c2op = C2_OPERATION_NONE);
349
350} // namespace android
351
352#endif // CCODEC_BUFFER_CHANNEL_H_