blob: c0fa138949d7e68c0808733265f669f0f080c5c7 [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
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
Pawin Vongmasa36653902018-11-15 00:10:25 -0800152private:
153 class QueueGuard;
154
155 /**
156 * Special mutex-like object with the following properties:
157 *
158 * - At STOPPED state (initial, or after stop())
159 * - QueueGuard object gets created at STOPPED state, and the client is
160 * supposed to return immediately.
161 * - At RUNNING state (after start())
162 * - Each QueueGuard object
163 */
164 class QueueSync {
165 public:
166 /**
167 * At construction the sync object is in STOPPED state.
168 */
169 inline QueueSync() {}
170 ~QueueSync() = default;
171
172 /**
173 * Transition to RUNNING state when stopped. No-op if already in RUNNING
174 * state.
175 */
176 void start();
177
178 /**
179 * At RUNNING state, wait until all QueueGuard object created during
180 * RUNNING state are destroyed, and then transition to STOPPED state.
181 * No-op if already in STOPPED state.
182 */
183 void stop();
184
185 private:
186 Mutex mGuardLock;
187
188 struct Counter {
189 inline Counter() : value(-1) {}
190 int32_t value;
191 Condition cond;
192 };
193 Mutexed<Counter> mCount;
194
195 friend class CCodecBufferChannel::QueueGuard;
196 };
197
198 class QueueGuard {
199 public:
200 QueueGuard(QueueSync &sync);
201 ~QueueGuard();
202 inline bool isRunning() { return mRunning; }
203
204 private:
205 QueueSync &mSync;
206 bool mRunning;
207 };
208
209 void feedInputBufferIfAvailable();
210 void feedInputBufferIfAvailableInternal();
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700211 status_t queueInputBufferInternal(sp<MediaCodecBuffer> buffer);
Pawin Vongmasa36653902018-11-15 00:10:25 -0800212 bool handleWork(
213 std::unique_ptr<C2Work> work, const sp<AMessage> &outputFormat,
214 const C2StreamInitDataInfo::output *initData);
215 void sendOutputBuffers();
216
217 QueueSync mSync;
218 sp<MemoryDealer> mDealer;
219 sp<IMemory> mDecryptDestination;
220 int32_t mHeapSeqNum;
221
222 std::shared_ptr<Codec2Client::Component> mComponent;
223 std::string mComponentName; ///< component name for debugging
224 const char *mName; ///< C-string version of component name
225 std::shared_ptr<CCodecCallback> mCCodecCallback;
226 std::shared_ptr<C2BlockPool> mInputAllocator;
227 QueueSync mQueueSync;
228 std::vector<std::unique_ptr<C2Param>> mParamsToBeSet;
229
Wonsik Kim5ecf3832019-04-18 10:28:58 -0700230 struct Input {
231 Input();
232
233 std::unique_ptr<InputBuffers> buffers;
234 size_t numSlots;
235 FlexBuffersImpl extraBuffers;
236 size_t numExtraSlots;
237 uint32_t inputDelay;
238 uint32_t pipelineDelay;
239 };
240 Mutexed<Input> mInput;
241 struct Output {
242 std::unique_ptr<OutputBuffers> buffers;
243 size_t numSlots;
244 uint32_t outputDelay;
245 };
246 Mutexed<Output> mOutput;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800247 Mutexed<std::list<sp<ABuffer>>> mFlushedConfigs;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800248
249 std::atomic_uint64_t mFrameIndex;
250 std::atomic_uint64_t mFirstValidFrameIndex;
251
252 sp<MemoryDealer> makeMemoryDealer(size_t heapSize);
253
254 struct OutputSurface {
255 sp<Surface> surface;
256 uint32_t generation;
Wonsik Kimf5e5c832019-02-21 11:36:05 -0800257 int maxDequeueBuffers;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800258 };
259 Mutexed<OutputSurface> mOutputSurface;
260
261 struct BlockPools {
262 C2Allocator::id_t inputAllocatorId;
263 std::shared_ptr<C2BlockPool> inputPool;
264 C2Allocator::id_t outputAllocatorId;
265 C2BlockPool::local_id_t outputPoolId;
266 std::shared_ptr<Codec2Client::Configurable> outputPoolIntf;
267 };
268 Mutexed<BlockPools> mBlockPools;
269
270 std::shared_ptr<InputSurfaceWrapper> mInputSurface;
271
272 MetaMode mMetaMode;
273
Wonsik Kimab34ed62019-01-31 15:28:46 -0800274 Mutexed<PipelineWatcher> mPipelineWatcher;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800275
276 class ReorderStash {
277 public:
278 struct Entry {
279 inline Entry() : buffer(nullptr), timestamp(0), flags(0), ordinal({0, 0, 0}) {}
280 inline Entry(
281 const std::shared_ptr<C2Buffer> &b,
282 int64_t t,
283 int32_t f,
284 const C2WorkOrdinalStruct &o)
285 : buffer(b), timestamp(t), flags(f), ordinal(o) {}
286 std::shared_ptr<C2Buffer> buffer;
287 int64_t timestamp;
288 int32_t flags;
289 C2WorkOrdinalStruct ordinal;
290 };
291
292 ReorderStash();
293
294 void clear();
Wonsik Kim6897f222019-01-30 13:29:24 -0800295 void flush();
Pawin Vongmasa36653902018-11-15 00:10:25 -0800296 void setDepth(uint32_t depth);
297 void setKey(C2Config::ordinal_key_t key);
298 bool pop(Entry *entry);
299 void emplace(
300 const std::shared_ptr<C2Buffer> &buffer,
301 int64_t timestamp,
302 int32_t flags,
303 const C2WorkOrdinalStruct &ordinal);
304 void defer(const Entry &entry);
305 bool hasPending() const;
Wonsik Kimf0e7d222019-06-28 12:33:16 -0700306 uint32_t depth() const { return mDepth; }
Pawin Vongmasa36653902018-11-15 00:10:25 -0800307
308 private:
309 std::list<Entry> mPending;
310 std::list<Entry> mStash;
311 uint32_t mDepth;
312 C2Config::ordinal_key_t mKey;
313
314 bool less(const C2WorkOrdinalStruct &o1, const C2WorkOrdinalStruct &o2);
315 };
316 Mutexed<ReorderStash> mReorderStash;
317
318 std::atomic_bool mInputMetEos;
Wonsik Kimf7529dd2019-04-18 17:35:53 -0700319 std::once_flag mRenderWarningFlag;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800320
321 inline bool hasCryptoOrDescrambler() {
322 return mCrypto != nullptr || mDescrambler != nullptr;
323 }
324};
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_