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