ACodecBufferChannel.h revision aaa5e6400d7654ca1a7dcebeba36214ed060e3cb
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 33/** 34 * BufferChannelBase implementation for ACodec. 35 */ 36class ACodecBufferChannel : public BufferChannelBase { 37public: 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 * @return OK if no error. 91 */ 92 status_t setInputBufferArray(const std::vector<BufferAndId> &array); 93 /** 94 * Set output buffer array. 95 * 96 * @param array Newly allocated buffers. Empty if buffers are 97 * deallocated. 98 */ 99 void setOutputBufferArray(const std::vector<BufferAndId> &array); 100 /** 101 * Request MediaCodec to fill the specified input buffer. 102 * 103 * @param bufferId ID of the buffer, assigned by underlying component. 104 */ 105 void fillThisBuffer(IOMX::buffer_id bufferID); 106 /** 107 * Request MediaCodec to drain the specified output buffer. 108 * 109 * @param bufferId ID of the buffer, assigned by underlying component. 110 * @param omxFlags flags associated with this buffer (e.g. EOS). 111 */ 112 void drainThisBuffer(IOMX::buffer_id bufferID, OMX_U32 omxFlags); 113 114private: 115 const sp<AMessage> mInputBufferFilled; 116 const sp<AMessage> mOutputBufferDrained; 117 118 sp<MemoryDealer> mDealer; 119 sp<IMemory> mDecryptDestination; 120 int32_t mHeapSeqNum; 121 122 // These should only be accessed via std::atomic_* functions. 123 // 124 // Note on thread safety: since the vector and BufferInfo are const, it's 125 // safe to read them at any thread once the shared_ptr object is atomically 126 // obtained. Inside BufferInfo, mBufferId and mSharedEncryptedBuffer are 127 // immutable objects. We write internal states of mClient/CodecBuffer when 128 // the caller has given up the reference, so that access is also safe. 129 std::shared_ptr<const std::vector<const BufferInfo>> mInputBuffers; 130 std::shared_ptr<const std::vector<const BufferInfo>> mOutputBuffers; 131 132 sp<MemoryDealer> makeMemoryDealer(size_t heapSize); 133 134 bool hasCryptoOrDescrambler() { 135 return mCrypto != NULL || mDescrambler != NULL; 136 } 137}; 138 139} // namespace android 140 141#endif // A_BUFFER_CHANNEL_H_ 142