blob: 82fec18b6e2da91586a1901f393c43539ab624d6 [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;
73 virtual status_t renderOutputBuffer(
74 const sp<MediaCodecBuffer> &buffer, int64_t timestampNs) override;
75 virtual status_t discardBuffer(const sp<MediaCodecBuffer> &buffer) override;
76 virtual void getInputBufferArray(Vector<sp<MediaCodecBuffer>> *array) override;
77 virtual void getOutputBufferArray(Vector<sp<MediaCodecBuffer>> *array) override;
78
79 // Methods below are interface for CCodec to use.
80
81 /**
82 * Set the component object for buffer processing.
83 */
84 void setComponent(const std::shared_ptr<Codec2Client::Component> &component);
85
86 /**
87 * Set output graphic surface for rendering.
88 */
89 status_t setSurface(const sp<Surface> &surface);
90
91 /**
92 * Set GraphicBufferSource object from which the component extracts input
93 * buffers.
94 */
95 status_t setInputSurface(const std::shared_ptr<InputSurfaceWrapper> &surface);
96
97 /**
98 * Signal EOS to input surface.
99 */
100 status_t signalEndOfInputStream();
101
102 /**
103 * Set parameters.
104 */
105 status_t setParameters(std::vector<std::unique_ptr<C2Param>> &params);
106
107 /**
108 * Start queueing buffers to the component. This object should never queue
109 * buffers before this call has completed.
110 */
111 status_t start(const sp<AMessage> &inputFormat, const sp<AMessage> &outputFormat);
112
113 /**
114 * Request initial input buffers to be filled by client.
115 */
116 status_t requestInitialInputBuffers();
117
118 /**
119 * Stop queueing buffers to the component. This object should never queue
120 * buffers after this call, until start() is called.
121 */
122 void stop();
123
124 void flush(const std::list<std::unique_ptr<C2Work>> &flushedWork);
125
126 /**
127 * Notify input client about work done.
128 *
129 * @param workItems finished work item.
130 * @param outputFormat new output format if it has changed, otherwise nullptr
131 * @param initData new init data (CSD) if it has changed, otherwise nullptr
Pawin Vongmasa36653902018-11-15 00:10:25 -0800132 */
133 void onWorkDone(
134 std::unique_ptr<C2Work> work, const sp<AMessage> &outputFormat,
Wonsik Kimab34ed62019-01-31 15:28:46 -0800135 const C2StreamInitDataInfo::output *initData);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800136
137 /**
138 * Make an input buffer available for the client as it is no longer needed
139 * by the codec.
140 *
Wonsik Kimab34ed62019-01-31 15:28:46 -0800141 * @param frameIndex The index of input work
142 * @param arrayIndex The index of buffer in the input work buffers.
Pawin Vongmasa36653902018-11-15 00:10:25 -0800143 */
Wonsik Kimab34ed62019-01-31 15:28:46 -0800144 void onInputBufferDone(uint64_t frameIndex, size_t arrayIndex);
145
146 PipelineWatcher::Clock::duration elapsed();
Pawin Vongmasa36653902018-11-15 00:10:25 -0800147
148 enum MetaMode {
149 MODE_NONE,
150 MODE_ANW,
151 };
152
153 void setMetaMode(MetaMode mode);
154
Pawin Vongmasa36653902018-11-15 00:10:25 -0800155private:
156 class QueueGuard;
157
158 /**
159 * Special mutex-like object with the following properties:
160 *
161 * - At STOPPED state (initial, or after stop())
162 * - QueueGuard object gets created at STOPPED state, and the client is
163 * supposed to return immediately.
164 * - At RUNNING state (after start())
165 * - Each QueueGuard object
166 */
167 class QueueSync {
168 public:
169 /**
170 * At construction the sync object is in STOPPED state.
171 */
172 inline QueueSync() {}
173 ~QueueSync() = default;
174
175 /**
176 * Transition to RUNNING state when stopped. No-op if already in RUNNING
177 * state.
178 */
179 void start();
180
181 /**
182 * At RUNNING state, wait until all QueueGuard object created during
183 * RUNNING state are destroyed, and then transition to STOPPED state.
184 * No-op if already in STOPPED state.
185 */
186 void stop();
187
188 private:
189 Mutex mGuardLock;
190
191 struct Counter {
192 inline Counter() : value(-1) {}
193 int32_t value;
194 Condition cond;
195 };
196 Mutexed<Counter> mCount;
197
198 friend class CCodecBufferChannel::QueueGuard;
199 };
200
201 class QueueGuard {
202 public:
203 QueueGuard(QueueSync &sync);
204 ~QueueGuard();
205 inline bool isRunning() { return mRunning; }
206
207 private:
208 QueueSync &mSync;
209 bool mRunning;
210 };
211
212 void feedInputBufferIfAvailable();
213 void feedInputBufferIfAvailableInternal();
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700214 status_t queueInputBufferInternal(sp<MediaCodecBuffer> buffer);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800215 bool handleWork(
216 std::unique_ptr<C2Work> work, const sp<AMessage> &outputFormat,
217 const C2StreamInitDataInfo::output *initData);
218 void sendOutputBuffers();
219
220 QueueSync mSync;
221 sp<MemoryDealer> mDealer;
222 sp<IMemory> mDecryptDestination;
223 int32_t mHeapSeqNum;
224
225 std::shared_ptr<Codec2Client::Component> mComponent;
226 std::string mComponentName; ///< component name for debugging
227 const char *mName; ///< C-string version of component name
228 std::shared_ptr<CCodecCallback> mCCodecCallback;
229 std::shared_ptr<C2BlockPool> mInputAllocator;
230 QueueSync mQueueSync;
231 std::vector<std::unique_ptr<C2Param>> mParamsToBeSet;
232
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700233 struct Input {
234 Input();
235
236 std::unique_ptr<InputBuffers> buffers;
237 size_t numSlots;
238 FlexBuffersImpl extraBuffers;
239 size_t numExtraSlots;
240 uint32_t inputDelay;
241 uint32_t pipelineDelay;
242 };
243 Mutexed<Input> mInput;
244 struct Output {
245 std::unique_ptr<OutputBuffers> buffers;
246 size_t numSlots;
247 uint32_t outputDelay;
248 };
249 Mutexed<Output> mOutput;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800250 Mutexed<std::list<sp<ABuffer>>> mFlushedConfigs;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800251
252 std::atomic_uint64_t mFrameIndex;
253 std::atomic_uint64_t mFirstValidFrameIndex;
254
255 sp<MemoryDealer> makeMemoryDealer(size_t heapSize);
256
257 struct OutputSurface {
258 sp<Surface> surface;
259 uint32_t generation;
Wonsik Kimf5e5c832019-02-21 11:36:05 -0800260 int maxDequeueBuffers;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800261 };
262 Mutexed<OutputSurface> mOutputSurface;
263
264 struct BlockPools {
265 C2Allocator::id_t inputAllocatorId;
266 std::shared_ptr<C2BlockPool> inputPool;
267 C2Allocator::id_t outputAllocatorId;
268 C2BlockPool::local_id_t outputPoolId;
269 std::shared_ptr<Codec2Client::Configurable> outputPoolIntf;
270 };
271 Mutexed<BlockPools> mBlockPools;
272
273 std::shared_ptr<InputSurfaceWrapper> mInputSurface;
274
275 MetaMode mMetaMode;
276
Wonsik Kimab34ed62019-01-31 15:28:46 -0800277 Mutexed<PipelineWatcher> mPipelineWatcher;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800278
279 class ReorderStash {
280 public:
281 struct Entry {
282 inline Entry() : buffer(nullptr), timestamp(0), flags(0), ordinal({0, 0, 0}) {}
283 inline Entry(
284 const std::shared_ptr<C2Buffer> &b,
285 int64_t t,
286 int32_t f,
287 const C2WorkOrdinalStruct &o)
288 : buffer(b), timestamp(t), flags(f), ordinal(o) {}
289 std::shared_ptr<C2Buffer> buffer;
290 int64_t timestamp;
291 int32_t flags;
292 C2WorkOrdinalStruct ordinal;
293 };
294
295 ReorderStash();
296
297 void clear();
Wonsik Kim6897f222019-01-30 13:29:24 -0800298 void flush();
Pawin Vongmasa36653902018-11-15 00:10:25 -0800299 void setDepth(uint32_t depth);
300 void setKey(C2Config::ordinal_key_t key);
301 bool pop(Entry *entry);
302 void emplace(
303 const std::shared_ptr<C2Buffer> &buffer,
304 int64_t timestamp,
305 int32_t flags,
306 const C2WorkOrdinalStruct &ordinal);
307 void defer(const Entry &entry);
308 bool hasPending() const;
Wonsik Kimf0e7d222019-06-28 12:33:16 -0700309 uint32_t depth() const { return mDepth; }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800310
311 private:
312 std::list<Entry> mPending;
313 std::list<Entry> mStash;
314 uint32_t mDepth;
315 C2Config::ordinal_key_t mKey;
316
317 bool less(const C2WorkOrdinalStruct &o1, const C2WorkOrdinalStruct &o2);
318 };
319 Mutexed<ReorderStash> mReorderStash;
320
321 std::atomic_bool mInputMetEos;
Wonsik Kimf7529dd2019-04-18 17:35:53 -0700322 std::once_flag mRenderWarningFlag;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800323
Wonsik Kim596187e2019-10-25 12:44:10 -0700324 sp<ICrypto> mCrypto;
325 sp<IDescrambler> mDescrambler;
326
Pawin Vongmasa36653902018-11-15 00:10:25 -0800327 inline bool hasCryptoOrDescrambler() {
328 return mCrypto != nullptr || mDescrambler != nullptr;
329 }
330};
331
332// Conversion of a c2_status_t value to a status_t value may depend on the
333// operation that returns the c2_status_t value.
334enum c2_operation_t {
335 C2_OPERATION_NONE,
336 C2_OPERATION_Component_connectToOmxInputSurface,
337 C2_OPERATION_Component_createBlockPool,
338 C2_OPERATION_Component_destroyBlockPool,
339 C2_OPERATION_Component_disconnectFromInputSurface,
340 C2_OPERATION_Component_drain,
341 C2_OPERATION_Component_flush,
342 C2_OPERATION_Component_queue,
343 C2_OPERATION_Component_release,
344 C2_OPERATION_Component_reset,
345 C2_OPERATION_Component_setOutputSurface,
346 C2_OPERATION_Component_start,
347 C2_OPERATION_Component_stop,
348 C2_OPERATION_ComponentStore_copyBuffer,
349 C2_OPERATION_ComponentStore_createComponent,
350 C2_OPERATION_ComponentStore_createInputSurface,
351 C2_OPERATION_ComponentStore_createInterface,
352 C2_OPERATION_Configurable_config,
353 C2_OPERATION_Configurable_query,
354 C2_OPERATION_Configurable_querySupportedParams,
355 C2_OPERATION_Configurable_querySupportedValues,
356 C2_OPERATION_InputSurface_connectToComponent,
357 C2_OPERATION_InputSurfaceConnection_disconnect,
358};
359
360status_t toStatusT(c2_status_t c2s, c2_operation_t c2op = C2_OPERATION_NONE);
361
362} // namespace android
363
364#endif // CCODEC_BUFFER_CHANNEL_H_