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