1 /*
2  * Copyright 2016, 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 A_BUFFER_CHANNEL_H_
18 
19 #define A_BUFFER_CHANNEL_H_
20 
21 #include <map>
22 #include <memory>
23 #include <mutex>
24 #include <vector>
25 
26 #include <media/openmax/OMX_Types.h>
27 #include <media/stagefright/CodecBase.h>
28 #include <mediadrm/ICrypto.h>
29 #include <media/IOMX.h>
30 
31 namespace android {
32 namespace hardware {
33 class HidlMemory;
34 };
35 using hardware::HidlMemory;
36 
37 /**
38  * BufferChannelBase implementation for ACodec.
39  */
40 class ACodecBufferChannel : public BufferChannelBase {
41 public:
42     struct BufferAndId {
43         sp<MediaCodecBuffer> mBuffer;
44         IOMX::buffer_id mBufferId;
45     };
46 
47     struct BufferInfo {
48         BufferInfo(
49                 const sp<MediaCodecBuffer> &buffer,
50                 IOMX::buffer_id bufferId,
51                 const sp<IMemory> &sharedEncryptedBuffer);
52 
53         BufferInfo() = delete;
54 
55         // Buffer facing MediaCodec and its clients.
56         const sp<MediaCodecBuffer> mClientBuffer;
57         // Buffer facing CodecBase.
58         const sp<MediaCodecBuffer> mCodecBuffer;
59         // OMX buffer ID.
60         const IOMX::buffer_id mBufferId;
61         // Encrypted buffer in case of secure input.
62         const sp<IMemory> mSharedEncryptedBuffer;
63     };
64 
65     ACodecBufferChannel(
66             const sp<AMessage> &inputBufferFilled, const sp<AMessage> &outputBufferDrained);
67     virtual ~ACodecBufferChannel();
68 
69     // BufferChannelBase interface
70     void setCrypto(const sp<ICrypto> &crypto) override;
71     void setDescrambler(const sp<IDescrambler> &descrambler) override;
72 
73     virtual status_t queueInputBuffer(const sp<MediaCodecBuffer> &buffer) override;
74     virtual status_t queueSecureInputBuffer(
75             const sp<MediaCodecBuffer> &buffer,
76             bool secure,
77             const uint8_t *key,
78             const uint8_t *iv,
79             CryptoPlugin::Mode mode,
80             CryptoPlugin::Pattern pattern,
81             const CryptoPlugin::SubSample *subSamples,
82             size_t numSubSamples,
83             AString *errorDetailMsg) override;
84     virtual status_t attachBuffer(
85             const std::shared_ptr<C2Buffer> &c2Buffer,
86             const sp<MediaCodecBuffer> &buffer) override;
87     virtual status_t attachEncryptedBuffer(
88             const sp<hardware::HidlMemory> &memory,
89             bool secure,
90             const uint8_t *key,
91             const uint8_t *iv,
92             CryptoPlugin::Mode mode,
93             CryptoPlugin::Pattern pattern,
94             size_t offset,
95             const CryptoPlugin::SubSample *subSamples,
96             size_t numSubSamples,
97             const sp<MediaCodecBuffer> &buffer) override;
98     virtual status_t renderOutputBuffer(
99             const sp<MediaCodecBuffer> &buffer, int64_t timestampNs) override;
100     virtual status_t discardBuffer(const sp<MediaCodecBuffer> &buffer) override;
101     virtual void getInputBufferArray(Vector<sp<MediaCodecBuffer>> *array) override;
102     virtual void getOutputBufferArray(Vector<sp<MediaCodecBuffer>> *array) override;
103 
104     // Methods below are interface for ACodec to use.
105 
106     /**
107      * Set input buffer array.
108      *
109      * @param array     Newly allocated buffers. Empty if buffers are
110      *                  deallocated.
111      */
112     void setInputBufferArray(const std::vector<BufferAndId> &array);
113     /**
114      * Set output buffer array.
115      *
116      * @param array     Newly allocated buffers. Empty if buffers are
117      *                  deallocated.
118      */
119     void setOutputBufferArray(const std::vector<BufferAndId> &array);
120     /**
121      * Request MediaCodec to fill the specified input buffer.
122      *
123      * @param bufferId  ID of the buffer, assigned by underlying component.
124      */
125     void fillThisBuffer(IOMX::buffer_id bufferID);
126     /**
127      * Request MediaCodec to drain the specified output buffer.
128      *
129      * @param bufferId  ID of the buffer, assigned by underlying component.
130      * @param omxFlags  flags associated with this buffer (e.g. EOS).
131      */
132     void drainThisBuffer(IOMX::buffer_id bufferID, OMX_U32 omxFlags);
133 
134 private:
135     int32_t getHeapSeqNum(const sp<HidlMemory> &memory);
136 
137     const sp<AMessage> mInputBufferFilled;
138     const sp<AMessage> mOutputBufferDrained;
139 
140     sp<MemoryDealer> mDealer;
141     sp<IMemory> mDecryptDestination;
142     int32_t mHeapSeqNum;
143     std::map<wp<HidlMemory>, int32_t> mHeapSeqNumMap;
144     sp<HidlMemory> mHidlMemory;
145 
146     // These should only be accessed via std::atomic_* functions.
147     //
148     // Note on thread safety: since the vector and BufferInfo are const, it's
149     // safe to read them at any thread once the shared_ptr object is atomically
150     // obtained. Inside BufferInfo, mBufferId and mSharedEncryptedBuffer are
151     // immutable objects. We write internal states of mClient/CodecBuffer when
152     // the caller has given up the reference, so that access is also safe.
153     std::shared_ptr<const std::vector<const BufferInfo>> mInputBuffers;
154     std::shared_ptr<const std::vector<const BufferInfo>> mOutputBuffers;
155 
156     sp<MemoryDealer> makeMemoryDealer(size_t heapSize);
157 
158     sp<ICrypto> mCrypto;
159     sp<IDescrambler> mDescrambler;
160 
hasCryptoOrDescrambler()161     bool hasCryptoOrDescrambler() {
162         return mCrypto != NULL || mDescrambler != NULL;
163     }
164 };
165 
166 }  // namespace android
167 
168 #endif  // A_BUFFER_CHANNEL_H_
169