Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4811923e authored by Wonsik Kim's avatar Wonsik Kim
Browse files

MediaCodec refactoring part 1-b: secure buffers

- Define SecureBuffer to encapsulate secure buffers containing
decrypted data.
- ACodec and MediaCodec use SecureBuffer to communicate between
themselves.

Bug: 32133435
Test: Play an encrypted content in Play Movies
Change-Id: Ic70f7f78aad15bd91ab8ccd31c340bc7964ab1b3
parent 47677e3e
Loading
Loading
Loading
Loading
+1 −7
Original line number Diff line number Diff line
@@ -76,21 +76,15 @@ struct ACodec : public AHierarchicalStateMachine, public CodecBase {
        size_t countBuffers();
        IOMX::buffer_id bufferIDAt(size_t index) const;
        sp<MediaCodecBuffer> bufferAt(size_t index) const;
        sp<NativeHandle> handleAt(size_t index) const;
        sp<RefBase> memRefAt(size_t index) const;

    private:
        friend struct ACodec;

        Vector<IOMX::buffer_id> mBufferIDs;
        Vector<sp<MediaCodecBuffer>> mBuffers;
        Vector<sp<NativeHandle> > mHandles;
        Vector<sp<RefBase> > mMemRefs;

        PortDescription();
        void addBuffer(
                IOMX::buffer_id id, const sp<MediaCodecBuffer> &buffer,
                const sp<NativeHandle> &handle, const sp<RefBase> &memRef);
        void addBuffer(IOMX::buffer_id id, const sp<MediaCodecBuffer> &buffer);

        DISALLOW_EVIL_CONSTRUCTORS(PortDescription);
    };
+0 −2
Original line number Diff line number Diff line
@@ -90,8 +90,6 @@ struct CodecBase : public AHandler, /* static */ ColorUtils {
        virtual size_t countBuffers() = 0;
        virtual IOMX::buffer_id bufferIDAt(size_t index) const = 0;
        virtual sp<MediaCodecBuffer> bufferAt(size_t index) const = 0;
        virtual sp<NativeHandle> handleAt(size_t index) const { return NULL; };
        virtual sp<RefBase> memRefAt(size_t index) const { return NULL; }

    protected:
        PortDescription();
+1 −3
Original line number Diff line number Diff line
@@ -255,9 +255,7 @@ private:
    struct BufferInfo {
        uint32_t mBufferID;
        sp<MediaCodecBuffer> mData;
        sp<NativeHandle> mNativeHandle;
        sp<RefBase> mMemRef;
        sp<MediaCodecBuffer> mEncryptedData;
        sp<MediaCodecBuffer> mSecureData;
        sp<IMemory> mSharedEncryptedBuffer;
        sp<AMessage> mNotify;
        sp<AMessage> mFormat;
+6 −27
Original line number Diff line number Diff line
@@ -53,6 +53,7 @@

#include "include/avc_utils.h"
#include "include/DataConverter.h"
#include "include/SecureBuffer.h"
#include "include/SharedMemoryBuffer.h"
#include "omx/OMXUtils.h"

@@ -876,7 +877,6 @@ status_t ACodec::allocateBuffersOnPort(OMX_U32 portIndex) {
                info.mStatus = BufferInfo::OWNED_BY_US;
                info.mFenceFd = -1;
                info.mRenderInfo = NULL;
                info.mNativeHandle = NULL;

                if (portIndex == kPortIndexInput && (mFlags & kFlagIsSecure)) {
                    mem.clear();
@@ -887,19 +887,9 @@ status_t ACodec::allocateBuffersOnPort(OMX_U32 portIndex) {
                            portIndex, bufSize, &info.mBufferID,
                            &ptr, &native_handle);

                    // TRICKY: this representation is unorthodox, but ACodec requires
                    // an ABuffer with a proper size to validate range offsets and lengths.
                    // Since mData is never referenced for secure input, it is used to store
                    // either the pointer to the secure buffer, or the opaque handle as on
                    // some devices ptr is actually an opaque handle, not a pointer.

                    // TRICKY2: use native handle as the base of the ABuffer if received one,
                    // because Widevine source only receives these base addresses.
                    const native_handle_t *native_handle_ptr =
                        native_handle == NULL ? NULL : native_handle->handle();
                    info.mData = new MediaCodecBuffer(format,
                            new ABuffer(ptr != NULL ? ptr : (void *)native_handle_ptr, bufSize));
                    info.mNativeHandle = native_handle;
                    info.mData = (native_handle == NULL)
                            ? new SecureBuffer(format, ptr, bufSize)
                            : new SecureBuffer(format, native_handle, bufSize);
                    info.mCodecData = info.mData;
                } else {
                    err = mOMXNode->useBuffer(
@@ -948,7 +938,7 @@ status_t ACodec::allocateBuffersOnPort(OMX_U32 portIndex) {

    for (size_t i = 0; i < mBuffers[portIndex].size(); ++i) {
        const BufferInfo &info = mBuffers[portIndex][i];
        desc->addBuffer(info.mBufferID, info.mData, info.mNativeHandle, info.mMemRef);
        desc->addBuffer(info.mBufferID, info.mData);
    }

    notify->setObject("portDesc", desc);
@@ -5248,12 +5238,9 @@ ACodec::PortDescription::PortDescription() {
}

void ACodec::PortDescription::addBuffer(
        IOMX::buffer_id id, const sp<MediaCodecBuffer> &buffer,
        const sp<NativeHandle> &handle, const sp<RefBase> &memRef) {
        IOMX::buffer_id id, const sp<MediaCodecBuffer> &buffer) {
    mBufferIDs.push_back(id);
    mBuffers.push_back(buffer);
    mHandles.push_back(handle);
    mMemRefs.push_back(memRef);
}

size_t ACodec::PortDescription::countBuffers() {
@@ -5268,14 +5255,6 @@ sp<MediaCodecBuffer> ACodec::PortDescription::bufferAt(size_t index) const {
    return mBuffers.itemAt(index);
}

sp<NativeHandle> ACodec::PortDescription::handleAt(size_t index) const {
    return mHandles.itemAt(index);
}

sp<RefBase> ACodec::PortDescription::memRefAt(size_t index) const {
    return mMemRefs.itemAt(index);
}

////////////////////////////////////////////////////////////////////////////////

ACodec::BaseState::BaseState(ACodec *codec, const sp<AState> &parentState)
+24 −0
Original line number Diff line number Diff line
@@ -21,7 +21,10 @@
#include <binder/IMemory.h>
#include <media/stagefright/foundation/ABuffer.h>
#include <media/stagefright/foundation/AMessage.h>
#include <media/ICrypto.h>
#include <utils/NativeHandle.h>

#include "include/SecureBuffer.h"
#include "include/SharedMemoryBuffer.h"

namespace android {
@@ -31,4 +34,25 @@ SharedMemoryBuffer::SharedMemoryBuffer(const sp<AMessage> &format, const sp<IMem
      mMemory(mem) {
}

SecureBuffer::SecureBuffer(const sp<AMessage> &format, void *ptr, size_t size)
    : MediaCodecBuffer(format, new ABuffer(nullptr, size)),
      mPointer(ptr) {
}

SecureBuffer::SecureBuffer(
        const sp<AMessage> &format, const sp<NativeHandle> &handle, size_t size)
    : MediaCodecBuffer(format, new ABuffer(nullptr, size)),
      mPointer(nullptr),
      mHandle(handle) {
}

void *SecureBuffer::getDestinationPointer() {
    return (void *)(mHandle == nullptr ? mPointer : mHandle->handle());
}

ICrypto::DestinationType SecureBuffer::getDestinationType() {
    return mHandle == nullptr ? ICrypto::kDestinationTypeOpaqueHandle
                              : ICrypto::kDestinationTypeNativeHandle;
}

}  // namespace android
Loading