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