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