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