blob: 6a24a1a005b5907f2927c7053ed0fa9b1546ae78 [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>
32#include <media/ICrypto.h>
33
34#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
59 virtual status_t queueInputBuffer(const sp<MediaCodecBuffer> &buffer) override;
60 virtual status_t queueSecureInputBuffer(
61 const sp<MediaCodecBuffer> &buffer,
62 bool secure,
63 const uint8_t *key,
64 const uint8_t *iv,
65 CryptoPlugin::Mode mode,
66 CryptoPlugin::Pattern pattern,
67 const CryptoPlugin::SubSample *subSamples,
68 size_t numSubSamples,
69 AString *errorDetailMsg) override;
70 virtual status_t renderOutputBuffer(
71 const sp<MediaCodecBuffer> &buffer, int64_t timestampNs) override;
72 virtual status_t discardBuffer(const sp<MediaCodecBuffer> &buffer) override;
73 virtual void getInputBufferArray(Vector<sp<MediaCodecBuffer>> *array) override;
74 virtual void getOutputBufferArray(Vector<sp<MediaCodecBuffer>> *array) override;
75
76 // Methods below are interface for CCodec to use.
77
78 /**
79 * Set the component object for buffer processing.
80 */
81 void setComponent(const std::shared_ptr<Codec2Client::Component> &component);
82
83 /**
84 * Set output graphic surface for rendering.
85 */
86 status_t setSurface(const sp<Surface> &surface);
87
88 /**
89 * Set GraphicBufferSource object from which the component extracts input
90 * buffers.
91 */
92 status_t setInputSurface(const std::shared_ptr<InputSurfaceWrapper> &surface);
93
94 /**
95 * Signal EOS to input surface.
96 */
97 status_t signalEndOfInputStream();
98
99 /**
100 * Set parameters.
101 */
102 status_t setParameters(std::vector<std::unique_ptr<C2Param>> &params);
103
104 /**
105 * Start queueing buffers to the component. This object should never queue
106 * buffers before this call has completed.
107 */
108 status_t start(const sp<AMessage> &inputFormat, const sp<AMessage> &outputFormat);
109
110 /**
111 * Request initial input buffers to be filled by client.
112 */
113 status_t requestInitialInputBuffers();
114
115 /**
116 * Stop queueing buffers to the component. This object should never queue
117 * buffers after this call, until start() is called.
118 */
119 void stop();
120
121 void flush(const std::list<std::unique_ptr<C2Work>> &flushedWork);
122
123 /**
124 * Notify input client about work done.
125 *
126 * @param workItems finished work item.
127 * @param outputFormat new output format if it has changed, otherwise nullptr
128 * @param initData new init data (CSD) if it has changed, otherwise nullptr
Pawin Vongmasa36653902018-11-15 00:10:25 -0800129 */
130 void onWorkDone(
131 std::unique_ptr<C2Work> work, const sp<AMessage> &outputFormat,
Wonsik Kimab34ed62019-01-31 15:28:46 -0800132 const C2StreamInitDataInfo::output *initData);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800133
134 /**
135 * Make an input buffer available for the client as it is no longer needed
136 * by the codec.
137 *
Wonsik Kimab34ed62019-01-31 15:28:46 -0800138 * @param frameIndex The index of input work
139 * @param arrayIndex The index of buffer in the input work buffers.
Pawin Vongmasa36653902018-11-15 00:10:25 -0800140 */
Wonsik Kimab34ed62019-01-31 15:28:46 -0800141 void onInputBufferDone(uint64_t frameIndex, size_t arrayIndex);
142
143 PipelineWatcher::Clock::duration elapsed();
Pawin Vongmasa36653902018-11-15 00:10:25 -0800144
145 enum MetaMode {
146 MODE_NONE,
147 MODE_ANW,
148 };
149
150 void setMetaMode(MetaMode mode);
151
152 // Internal classes
153 class Buffers;
154 class InputBuffers;
155 class OutputBuffers;
156
157private:
158 class QueueGuard;
159
160 /**
161 * Special mutex-like object with the following properties:
162 *
163 * - At STOPPED state (initial, or after stop())
164 * - QueueGuard object gets created at STOPPED state, and the client is
165 * supposed to return immediately.
166 * - At RUNNING state (after start())
167 * - Each QueueGuard object
168 */
169 class QueueSync {
170 public:
171 /**
172 * At construction the sync object is in STOPPED state.
173 */
174 inline QueueSync() {}
175 ~QueueSync() = default;
176
177 /**
178 * Transition to RUNNING state when stopped. No-op if already in RUNNING
179 * state.
180 */
181 void start();
182
183 /**
184 * At RUNNING state, wait until all QueueGuard object created during
185 * RUNNING state are destroyed, and then transition to STOPPED state.
186 * No-op if already in STOPPED state.
187 */
188 void stop();
189
190 private:
191 Mutex mGuardLock;
192
193 struct Counter {
194 inline Counter() : value(-1) {}
195 int32_t value;
196 Condition cond;
197 };
198 Mutexed<Counter> mCount;
199
200 friend class CCodecBufferChannel::QueueGuard;
201 };
202
203 class QueueGuard {
204 public:
205 QueueGuard(QueueSync &sync);
206 ~QueueGuard();
207 inline bool isRunning() { return mRunning; }
208
209 private:
210 QueueSync &mSync;
211 bool mRunning;
212 };
213
214 void feedInputBufferIfAvailable();
215 void feedInputBufferIfAvailableInternal();
216 status_t queueInputBufferInternal(const sp<MediaCodecBuffer> &buffer);
217 bool handleWork(
218 std::unique_ptr<C2Work> work, const sp<AMessage> &outputFormat,
219 const C2StreamInitDataInfo::output *initData);
220 void sendOutputBuffers();
221
222 QueueSync mSync;
223 sp<MemoryDealer> mDealer;
224 sp<IMemory> mDecryptDestination;
225 int32_t mHeapSeqNum;
226
227 std::shared_ptr<Codec2Client::Component> mComponent;
228 std::string mComponentName; ///< component name for debugging
229 const char *mName; ///< C-string version of component name
230 std::shared_ptr<CCodecCallback> mCCodecCallback;
231 std::shared_ptr<C2BlockPool> mInputAllocator;
232 QueueSync mQueueSync;
233 std::vector<std::unique_ptr<C2Param>> mParamsToBeSet;
234
Wonsik Kim078b58e2019-01-09 15:08:06 -0800235 size_t mNumInputSlots;
236 size_t mNumOutputSlots;
Wonsik Kim4fa4f2b2019-02-13 11:02:58 -0800237 size_t mDelay;
Wonsik Kim078b58e2019-01-09 15:08:06 -0800238
Pawin Vongmasa36653902018-11-15 00:10:25 -0800239 Mutexed<std::unique_ptr<InputBuffers>> mInputBuffers;
240 Mutexed<std::list<sp<ABuffer>>> mFlushedConfigs;
241 Mutexed<std::unique_ptr<OutputBuffers>> mOutputBuffers;
242
243 std::atomic_uint64_t mFrameIndex;
244 std::atomic_uint64_t mFirstValidFrameIndex;
245
246 sp<MemoryDealer> makeMemoryDealer(size_t heapSize);
247
248 struct OutputSurface {
249 sp<Surface> surface;
250 uint32_t generation;
Wonsik Kimf5e5c832019-02-21 11:36:05 -0800251 int maxDequeueBuffers;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800252 };
253 Mutexed<OutputSurface> mOutputSurface;
254
255 struct BlockPools {
256 C2Allocator::id_t inputAllocatorId;
257 std::shared_ptr<C2BlockPool> inputPool;
258 C2Allocator::id_t outputAllocatorId;
259 C2BlockPool::local_id_t outputPoolId;
260 std::shared_ptr<Codec2Client::Configurable> outputPoolIntf;
261 };
262 Mutexed<BlockPools> mBlockPools;
263
264 std::shared_ptr<InputSurfaceWrapper> mInputSurface;
265
266 MetaMode mMetaMode;
267
Wonsik Kimab34ed62019-01-31 15:28:46 -0800268 Mutexed<PipelineWatcher> mPipelineWatcher;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800269
270 class ReorderStash {
271 public:
272 struct Entry {
273 inline Entry() : buffer(nullptr), timestamp(0), flags(0), ordinal({0, 0, 0}) {}
274 inline Entry(
275 const std::shared_ptr<C2Buffer> &b,
276 int64_t t,
277 int32_t f,
278 const C2WorkOrdinalStruct &o)
279 : buffer(b), timestamp(t), flags(f), ordinal(o) {}
280 std::shared_ptr<C2Buffer> buffer;
281 int64_t timestamp;
282 int32_t flags;
283 C2WorkOrdinalStruct ordinal;
284 };
285
286 ReorderStash();
287
288 void clear();
Wonsik Kim6897f222019-01-30 13:29:24 -0800289 void flush();
Pawin Vongmasa36653902018-11-15 00:10:25 -0800290 void setDepth(uint32_t depth);
291 void setKey(C2Config::ordinal_key_t key);
292 bool pop(Entry *entry);
293 void emplace(
294 const std::shared_ptr<C2Buffer> &buffer,
295 int64_t timestamp,
296 int32_t flags,
297 const C2WorkOrdinalStruct &ordinal);
298 void defer(const Entry &entry);
299 bool hasPending() const;
300
301 private:
302 std::list<Entry> mPending;
303 std::list<Entry> mStash;
304 uint32_t mDepth;
305 C2Config::ordinal_key_t mKey;
306
307 bool less(const C2WorkOrdinalStruct &o1, const C2WorkOrdinalStruct &o2);
308 };
309 Mutexed<ReorderStash> mReorderStash;
310
311 std::atomic_bool mInputMetEos;
312
313 inline bool hasCryptoOrDescrambler() {
314 return mCrypto != nullptr || mDescrambler != nullptr;
315 }
316};
317
318// Conversion of a c2_status_t value to a status_t value may depend on the
319// operation that returns the c2_status_t value.
320enum c2_operation_t {
321 C2_OPERATION_NONE,
322 C2_OPERATION_Component_connectToOmxInputSurface,
323 C2_OPERATION_Component_createBlockPool,
324 C2_OPERATION_Component_destroyBlockPool,
325 C2_OPERATION_Component_disconnectFromInputSurface,
326 C2_OPERATION_Component_drain,
327 C2_OPERATION_Component_flush,
328 C2_OPERATION_Component_queue,
329 C2_OPERATION_Component_release,
330 C2_OPERATION_Component_reset,
331 C2_OPERATION_Component_setOutputSurface,
332 C2_OPERATION_Component_start,
333 C2_OPERATION_Component_stop,
334 C2_OPERATION_ComponentStore_copyBuffer,
335 C2_OPERATION_ComponentStore_createComponent,
336 C2_OPERATION_ComponentStore_createInputSurface,
337 C2_OPERATION_ComponentStore_createInterface,
338 C2_OPERATION_Configurable_config,
339 C2_OPERATION_Configurable_query,
340 C2_OPERATION_Configurable_querySupportedParams,
341 C2_OPERATION_Configurable_querySupportedValues,
342 C2_OPERATION_InputSurface_connectToComponent,
343 C2_OPERATION_InputSurfaceConnection_disconnect,
344};
345
346status_t toStatusT(c2_status_t c2s, c2_operation_t c2op = C2_OPERATION_NONE);
347
348} // namespace android
349
350#endif // CCODEC_BUFFER_CHANNEL_H_