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