blob: b7a8445f2b434e41b0c8032e410602ec5529d977 [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
Wonsik Kim469c8342019-04-11 16:46:09 -070036#include "CCodecBuffers.h"
Pawin Vongmasa36653902018-11-15 00:10:25 -080037#include "InputSurfaceWrapper.h"
Wonsik Kimab34ed62019-01-31 15:28:46 -080038#include "PipelineWatcher.h"
Pawin Vongmasa36653902018-11-15 00:10:25 -080039
40namespace android {
41
Wonsik Kim078b58e2019-01-09 15:08:06 -080042class MemoryDealer;
43
Pawin Vongmasa36653902018-11-15 00:10:25 -080044class CCodecCallback {
45public:
46 virtual ~CCodecCallback() = default;
47 virtual void onError(status_t err, enum ActionCode actionCode) = 0;
48 virtual void onOutputFramesRendered(int64_t mediaTimeUs, nsecs_t renderTimeNs) = 0;
Pawin Vongmasa36653902018-11-15 00:10:25 -080049 virtual void onOutputBuffersChanged() = 0;
50};
51
52/**
53 * BufferChannelBase implementation for CCodec.
54 */
55class CCodecBufferChannel
56 : public BufferChannelBase, public std::enable_shared_from_this<CCodecBufferChannel> {
57public:
58 explicit CCodecBufferChannel(const std::shared_ptr<CCodecCallback> &callback);
59 virtual ~CCodecBufferChannel();
60
61 // BufferChannelBase interface
62 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();
214 status_t queueInputBufferInternal(const sp<MediaCodecBuffer> &buffer);
215 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 Kim078b58e2019-01-09 15:08:06 -0800233 size_t mNumInputSlots;
234 size_t mNumOutputSlots;
Wonsik Kim4fa4f2b2019-02-13 11:02:58 -0800235 size_t mDelay;
Wonsik Kim078b58e2019-01-09 15:08:06 -0800236
Pawin Vongmasa36653902018-11-15 00:10:25 -0800237 Mutexed<std::unique_ptr<InputBuffers>> mInputBuffers;
238 Mutexed<std::list<sp<ABuffer>>> mFlushedConfigs;
239 Mutexed<std::unique_ptr<OutputBuffers>> mOutputBuffers;
240
241 std::atomic_uint64_t mFrameIndex;
242 std::atomic_uint64_t mFirstValidFrameIndex;
243
244 sp<MemoryDealer> makeMemoryDealer(size_t heapSize);
245
246 struct OutputSurface {
247 sp<Surface> surface;
248 uint32_t generation;
Wonsik Kimf5e5c832019-02-21 11:36:05 -0800249 int maxDequeueBuffers;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800250 };
251 Mutexed<OutputSurface> mOutputSurface;
252
253 struct BlockPools {
254 C2Allocator::id_t inputAllocatorId;
255 std::shared_ptr<C2BlockPool> inputPool;
256 C2Allocator::id_t outputAllocatorId;
257 C2BlockPool::local_id_t outputPoolId;
258 std::shared_ptr<Codec2Client::Configurable> outputPoolIntf;
259 };
260 Mutexed<BlockPools> mBlockPools;
261
262 std::shared_ptr<InputSurfaceWrapper> mInputSurface;
263
264 MetaMode mMetaMode;
265
Wonsik Kimab34ed62019-01-31 15:28:46 -0800266 Mutexed<PipelineWatcher> mPipelineWatcher;
Pawin Vongmasa36653902018-11-15 00:10:25 -0800267
268 class ReorderStash {
269 public:
270 struct Entry {
271 inline Entry() : buffer(nullptr), timestamp(0), flags(0), ordinal({0, 0, 0}) {}
272 inline Entry(
273 const std::shared_ptr<C2Buffer> &b,
274 int64_t t,
275 int32_t f,
276 const C2WorkOrdinalStruct &o)
277 : buffer(b), timestamp(t), flags(f), ordinal(o) {}
278 std::shared_ptr<C2Buffer> buffer;
279 int64_t timestamp;
280 int32_t flags;
281 C2WorkOrdinalStruct ordinal;
282 };
283
284 ReorderStash();
285
286 void clear();
Wonsik Kim6897f222019-01-30 13:29:24 -0800287 void flush();
Pawin Vongmasa36653902018-11-15 00:10:25 -0800288 void setDepth(uint32_t depth);
289 void setKey(C2Config::ordinal_key_t key);
290 bool pop(Entry *entry);
291 void emplace(
292 const std::shared_ptr<C2Buffer> &buffer,
293 int64_t timestamp,
294 int32_t flags,
295 const C2WorkOrdinalStruct &ordinal);
296 void defer(const Entry &entry);
297 bool hasPending() const;
298
299 private:
300 std::list<Entry> mPending;
301 std::list<Entry> mStash;
302 uint32_t mDepth;
303 C2Config::ordinal_key_t mKey;
304
305 bool less(const C2WorkOrdinalStruct &o1, const C2WorkOrdinalStruct &o2);
306 };
307 Mutexed<ReorderStash> mReorderStash;
308
309 std::atomic_bool mInputMetEos;
310
311 inline bool hasCryptoOrDescrambler() {
312 return mCrypto != nullptr || mDescrambler != nullptr;
313 }
314};
315
316// Conversion of a c2_status_t value to a status_t value may depend on the
317// operation that returns the c2_status_t value.
318enum c2_operation_t {
319 C2_OPERATION_NONE,
320 C2_OPERATION_Component_connectToOmxInputSurface,
321 C2_OPERATION_Component_createBlockPool,
322 C2_OPERATION_Component_destroyBlockPool,
323 C2_OPERATION_Component_disconnectFromInputSurface,
324 C2_OPERATION_Component_drain,
325 C2_OPERATION_Component_flush,
326 C2_OPERATION_Component_queue,
327 C2_OPERATION_Component_release,
328 C2_OPERATION_Component_reset,
329 C2_OPERATION_Component_setOutputSurface,
330 C2_OPERATION_Component_start,
331 C2_OPERATION_Component_stop,
332 C2_OPERATION_ComponentStore_copyBuffer,
333 C2_OPERATION_ComponentStore_createComponent,
334 C2_OPERATION_ComponentStore_createInputSurface,
335 C2_OPERATION_ComponentStore_createInterface,
336 C2_OPERATION_Configurable_config,
337 C2_OPERATION_Configurable_query,
338 C2_OPERATION_Configurable_querySupportedParams,
339 C2_OPERATION_Configurable_querySupportedValues,
340 C2_OPERATION_InputSurface_connectToComponent,
341 C2_OPERATION_InputSurfaceConnection_disconnect,
342};
343
344status_t toStatusT(c2_status_t c2s, c2_operation_t c2op = C2_OPERATION_NONE);
345
346} // namespace android
347
348#endif // CCODEC_BUFFER_CHANNEL_H_