1 /*
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/foundation/Mutexed.h>
31 #include <media/stagefright/CodecBase.h>
32 
33 #include "CCodecBuffers.h"
34 #include "InputSurfaceWrapper.h"
35 #include "PipelineWatcher.h"
36 
37 namespace android {
38 
39 class MemoryDealer;
40 
41 class CCodecCallback {
42 public:
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;
46     virtual void onOutputBuffersChanged() = 0;
47 };
48 
49 /**
50  * BufferChannelBase implementation for CCodec.
51  */
52 class CCodecBufferChannel
53     : public BufferChannelBase, public std::enable_shared_from_this<CCodecBufferChannel> {
54 public:
55     explicit CCodecBufferChannel(const std::shared_ptr<CCodecCallback> &callback);
56     virtual ~CCodecBufferChannel();
57 
58     // BufferChannelBase interface
59     void setCrypto(const sp<ICrypto> &crypto) override;
60     void setDescrambler(const sp<IDescrambler> &descrambler) override;
61 
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 attachBuffer(
74             const std::shared_ptr<C2Buffer> &c2Buffer,
75             const sp<MediaCodecBuffer> &buffer) override;
76     virtual status_t attachEncryptedBuffer(
77             const sp<hardware::HidlMemory> &memory,
78             bool secure,
79             const uint8_t *key,
80             const uint8_t *iv,
81             CryptoPlugin::Mode mode,
82             CryptoPlugin::Pattern pattern,
83             size_t offset,
84             const CryptoPlugin::SubSample *subSamples,
85             size_t numSubSamples,
86             const sp<MediaCodecBuffer> &buffer) override;
87     virtual status_t renderOutputBuffer(
88             const sp<MediaCodecBuffer> &buffer, int64_t timestampNs) override;
89     virtual status_t discardBuffer(const sp<MediaCodecBuffer> &buffer) override;
90     virtual void getInputBufferArray(Vector<sp<MediaCodecBuffer>> *array) override;
91     virtual void getOutputBufferArray(Vector<sp<MediaCodecBuffer>> *array) override;
92 
93     // Methods below are interface for CCodec to use.
94 
95     /**
96      * Set the component object for buffer processing.
97      */
98     void setComponent(const std::shared_ptr<Codec2Client::Component> &component);
99 
100     /**
101      * Set output graphic surface for rendering.
102      */
103     status_t setSurface(const sp<Surface> &surface);
104 
105     /**
106      * Set GraphicBufferSource object from which the component extracts input
107      * buffers.
108      */
109     status_t setInputSurface(const std::shared_ptr<InputSurfaceWrapper> &surface);
110 
111     /**
112      * Signal EOS to input surface.
113      */
114     status_t signalEndOfInputStream();
115 
116     /**
117      * Set parameters.
118      */
119     status_t setParameters(std::vector<std::unique_ptr<C2Param>> &params);
120 
121     /**
122      * Start queueing buffers to the component. This object should never queue
123      * buffers before this call has completed.
124      */
125     status_t start(
126             const sp<AMessage> &inputFormat,
127             const sp<AMessage> &outputFormat,
128             bool buffersBoundToCodec);
129 
130     /**
131      * Request initial input buffers to be filled by client.
132      */
133     status_t requestInitialInputBuffers();
134 
135     /**
136      * Stop queueing buffers to the component. This object should never queue
137      * buffers after this call, until start() is called.
138      */
139     void stop();
140 
141     /**
142      * Stop queueing buffers to the component and release all buffers.
143      */
144     void reset();
145 
146     /**
147      * Release all resources.
148      */
149     void release();
150 
151     void flush(const std::list<std::unique_ptr<C2Work>> &flushedWork);
152 
153     /**
154      * Notify input client about work done.
155      *
156      * @param workItems   finished work item.
157      * @param outputFormat new output format if it has changed, otherwise nullptr
158      * @param initData    new init data (CSD) if it has changed, otherwise nullptr
159      */
160     void onWorkDone(
161             std::unique_ptr<C2Work> work, const sp<AMessage> &outputFormat,
162             const C2StreamInitDataInfo::output *initData);
163 
164     /**
165      * Make an input buffer available for the client as it is no longer needed
166      * by the codec.
167      *
168      * @param frameIndex The index of input work
169      * @param arrayIndex The index of buffer in the input work buffers.
170      */
171     void onInputBufferDone(uint64_t frameIndex, size_t arrayIndex);
172 
173     PipelineWatcher::Clock::duration elapsed();
174 
175     enum MetaMode {
176         MODE_NONE,
177         MODE_ANW,
178     };
179 
180     void setMetaMode(MetaMode mode);
181 
182 private:
183     class QueueGuard;
184 
185     /**
186      * Special mutex-like object with the following properties:
187      *
188      * - At STOPPED state (initial, or after stop())
189      *   - QueueGuard object gets created at STOPPED state, and the client is
190      *     supposed to return immediately.
191      * - At RUNNING state (after start())
192      *   - Each QueueGuard object
193      */
194     class QueueSync {
195     public:
196         /**
197          * At construction the sync object is in STOPPED state.
198          */
QueueSync()199         inline QueueSync() {}
200         ~QueueSync() = default;
201 
202         /**
203          * Transition to RUNNING state when stopped. No-op if already in RUNNING
204          * state.
205          */
206         void start();
207 
208         /**
209          * At RUNNING state, wait until all QueueGuard object created during
210          * RUNNING state are destroyed, and then transition to STOPPED state.
211          * No-op if already in STOPPED state.
212          */
213         void stop();
214 
215     private:
216         Mutex mGuardLock;
217 
218         struct Counter {
CounterCounter219             inline Counter() : value(-1) {}
220             int32_t value;
221             Condition cond;
222         };
223         Mutexed<Counter> mCount;
224 
225         friend class CCodecBufferChannel::QueueGuard;
226     };
227 
228     class QueueGuard {
229     public:
230         QueueGuard(QueueSync &sync);
231         ~QueueGuard();
isRunning()232         inline bool isRunning() { return mRunning; }
233 
234     private:
235         QueueSync &mSync;
236         bool mRunning;
237     };
238 
239     void feedInputBufferIfAvailable();
240     void feedInputBufferIfAvailableInternal();
241     status_t queueInputBufferInternal(sp<MediaCodecBuffer> buffer);
242     bool handleWork(
243             std::unique_ptr<C2Work> work, const sp<AMessage> &outputFormat,
244             const C2StreamInitDataInfo::output *initData);
245     void sendOutputBuffers();
246     void ensureDecryptDestination(size_t size);
247     int32_t getHeapSeqNum(const sp<hardware::HidlMemory> &memory);
248 
249     QueueSync mSync;
250     sp<MemoryDealer> mDealer;
251     sp<IMemory> mDecryptDestination;
252     int32_t mHeapSeqNum;
253     std::map<wp<hardware::HidlMemory>, int32_t> mHeapSeqNumMap;
254 
255     std::shared_ptr<Codec2Client::Component> mComponent;
256     std::string mComponentName; ///< component name for debugging
257     const char *mName; ///< C-string version of component name
258     std::shared_ptr<CCodecCallback> mCCodecCallback;
259     std::shared_ptr<C2BlockPool> mInputAllocator;
260     QueueSync mQueueSync;
261     std::vector<std::unique_ptr<C2Param>> mParamsToBeSet;
262 
263     struct Input {
264         Input();
265 
266         std::unique_ptr<InputBuffers> buffers;
267         size_t numSlots;
268         FlexBuffersImpl extraBuffers;
269         size_t numExtraSlots;
270         uint32_t inputDelay;
271         uint32_t pipelineDelay;
272     };
273     Mutexed<Input> mInput;
274     struct Output {
275         std::unique_ptr<OutputBuffers> buffers;
276         size_t numSlots;
277         uint32_t outputDelay;
278     };
279     Mutexed<Output> mOutput;
280     Mutexed<std::list<sp<ABuffer>>> mFlushedConfigs;
281 
282     std::atomic_uint64_t mFrameIndex;
283     std::atomic_uint64_t mFirstValidFrameIndex;
284 
285     sp<MemoryDealer> makeMemoryDealer(size_t heapSize);
286 
287     struct OutputSurface {
288         sp<Surface> surface;
289         uint32_t generation;
290         int maxDequeueBuffers;
291     };
292     Mutexed<OutputSurface> mOutputSurface;
293 
294     struct BlockPools {
295         C2Allocator::id_t inputAllocatorId;
296         std::shared_ptr<C2BlockPool> inputPool;
297         C2Allocator::id_t outputAllocatorId;
298         C2BlockPool::local_id_t outputPoolId;
299         std::shared_ptr<Codec2Client::Configurable> outputPoolIntf;
300     };
301     Mutexed<BlockPools> mBlockPools;
302 
303     std::shared_ptr<InputSurfaceWrapper> mInputSurface;
304 
305     MetaMode mMetaMode;
306 
307     Mutexed<PipelineWatcher> mPipelineWatcher;
308 
309     std::atomic_bool mInputMetEos;
310     std::once_flag mRenderWarningFlag;
311 
312     sp<ICrypto> mCrypto;
313     sp<IDescrambler> mDescrambler;
314 
hasCryptoOrDescrambler()315     inline bool hasCryptoOrDescrambler() {
316         return mCrypto != nullptr || mDescrambler != nullptr;
317     }
318 };
319 
320 // Conversion of a c2_status_t value to a status_t value may depend on the
321 // operation that returns the c2_status_t value.
322 enum c2_operation_t {
323     C2_OPERATION_NONE,
324     C2_OPERATION_Component_connectToOmxInputSurface,
325     C2_OPERATION_Component_createBlockPool,
326     C2_OPERATION_Component_destroyBlockPool,
327     C2_OPERATION_Component_disconnectFromInputSurface,
328     C2_OPERATION_Component_drain,
329     C2_OPERATION_Component_flush,
330     C2_OPERATION_Component_queue,
331     C2_OPERATION_Component_release,
332     C2_OPERATION_Component_reset,
333     C2_OPERATION_Component_setOutputSurface,
334     C2_OPERATION_Component_start,
335     C2_OPERATION_Component_stop,
336     C2_OPERATION_ComponentStore_copyBuffer,
337     C2_OPERATION_ComponentStore_createComponent,
338     C2_OPERATION_ComponentStore_createInputSurface,
339     C2_OPERATION_ComponentStore_createInterface,
340     C2_OPERATION_Configurable_config,
341     C2_OPERATION_Configurable_query,
342     C2_OPERATION_Configurable_querySupportedParams,
343     C2_OPERATION_Configurable_querySupportedValues,
344     C2_OPERATION_InputSurface_connectToComponent,
345     C2_OPERATION_InputSurfaceConnection_disconnect,
346 };
347 
348 status_t toStatusT(c2_status_t c2s, c2_operation_t c2op = C2_OPERATION_NONE);
349 
350 }  // namespace android
351 
352 #endif  // CCODEC_BUFFER_CHANNEL_H_
353