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

Commit dc31017e authored by Wonsik Kim's avatar Wonsik Kim
Browse files

CCodec: Episode VI --- Set Persistent Surface

- Implement CCodec::setInputSurface().
- Introduce InputSurfaceWrapper to handle both IInputSurface and
  IGraphicBufferProducer.
- Create C2OMXNode to wrap codec 2.0 component in IOMXNode, so that
  it can be used in IGraphicBufferPrdoucer::configure().

Test: setprop debug.stagefright.ccodec yes
Test: screenrecord --codec-name c2.google.avc.encoder /sdcard/record.mp4
Test: screenrecord --persistent-surface --codec-name c2.google.avc.encoder /sdcard/record.mp4
Change-Id: I7ea9f150ae06996f03a78645d6e748d265b975df
parent 0fa28dcc
Loading
Loading
Loading
Loading
+16 −2
Original line number Diff line number Diff line
@@ -50,6 +50,7 @@
#include <media/stagefright/MediaCodec.h>
#include <media/stagefright/MediaErrors.h>
#include <media/stagefright/MediaMuxer.h>
#include <media/stagefright/PersistentSurface.h>
#include <media/ICrypto.h>
#include <media/MediaCodecBuffer.h>

@@ -70,6 +71,7 @@ static const char* kMimeTypeAvc = "video/avc";
static bool gVerbose = false;           // chatty on stdout
static bool gRotate = false;            // rotate 90 degrees
static bool gMonotonicTime = false;     // use system monotonic time for timestamps
static bool gPersistentSurface = false; // use persistent surface
static enum {
    FORMAT_MP4, FORMAT_H264, FORMAT_FRAMES, FORMAT_RAW_FRAMES
} gOutputFormat = FORMAT_MP4;           // data format for output
@@ -199,10 +201,18 @@ static status_t prepareEncoder(float displayFps, sp<MediaCodec>* pCodec,

    ALOGV("Creating encoder input surface");
    sp<IGraphicBufferProducer> bufferProducer;
    if (gPersistentSurface) {
        sp<PersistentSurface> surface = MediaCodec::CreatePersistentInputSurface();
        bufferProducer = surface->getBufferProducer();
        err = codec->setInputSurface(surface);
    } else {
        err = codec->createInputSurface(&bufferProducer);
    }
    if (err != NO_ERROR) {
        fprintf(stderr,
            "ERROR: unable to create encoder input surface (err=%d)\n", err);
            "ERROR: unable to %s encoder input surface (err=%d)\n",
            gPersistentSurface ? "set" : "create",
            err);
        codec->release();
        return err;
    }
@@ -920,6 +930,7 @@ int main(int argc, char* const argv[]) {
        { "output-format",      required_argument,  NULL, 'o' },
        { "codec-name",         required_argument,  NULL, 'N' },
        { "monotonic-time",     no_argument,        NULL, 'm' },
        { "persistent-surface", no_argument,        NULL, 'p' },
        { NULL,                 0,                  NULL, 0 }
    };

@@ -1005,6 +1016,9 @@ int main(int argc, char* const argv[]) {
        case 'm':
            gMonotonicTime = true;
            break;
        case 'p':
            gPersistentSurface = true;
            break;
        default:
            if (ic != '?') {
                fprintf(stderr, "getopt_long returned unexpected value 0x%x\n", ic);
+1 −0
Original line number Diff line number Diff line
@@ -91,6 +91,7 @@ public:

private:
    friend struct OMXNodeInstance;
    friend struct C2OMXNode;

    // This is needed temporarily for OMX HIDL transition.
    friend inline bool (::android::hardware::media::omx::V1_0::implementation::
+1 −0
Original line number Diff line number Diff line
@@ -60,6 +60,7 @@ cc_library_shared {
        "AudioPresentationInfo.cpp",
        "AudioSource.cpp",
        "BufferImpl.cpp",
        "C2OMXNode.cpp",
        "CCodec.cpp",
        "CCodecBufferChannel.cpp",
        "CodecBase.cpp",
+281 −0
Original line number Diff line number Diff line
/*
 * Copyright 2018, The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#ifdef __LP64__
#define OMX_ANDROID_COMPILE_AS_32BIT_ON_64BIT_PLATFORMS
#endif

//#define LOG_NDEBUG 0
#define LOG_TAG "C2OMXNode"
#include <log/log.h>

#include <C2AllocatorGralloc.h>
#include <C2BlockInternal.h>
#include <C2Component.h>
#include <C2PlatformSupport.h>

#include <OMX_Component.h>
#include <OMX_Index.h>
#include <OMX_IndexExt.h>

#include <media/stagefright/omx/OMXUtils.h>
#include <media/stagefright/MediaErrors.h>
#include <ui/Fence.h>
#include <ui/GraphicBuffer.h>

#include "include/C2OMXNode.h"

namespace android {

namespace {

class Buffer2D : public C2Buffer {
public:
    explicit Buffer2D(C2ConstGraphicBlock block) : C2Buffer({ block }) {}
};

}  // namespace

C2OMXNode::C2OMXNode(const std::shared_ptr<C2Component> &comp) : mComp(comp) {}

status_t C2OMXNode::freeNode() {
    mComp.reset();
    return OK;
}

status_t C2OMXNode::sendCommand(OMX_COMMANDTYPE cmd, OMX_S32 param) {
    (void)cmd;
    (void)param;
    return ERROR_UNSUPPORTED;
}

status_t C2OMXNode::getParameter(OMX_INDEXTYPE index, void *params, size_t size) {
    status_t err = ERROR_UNSUPPORTED;
    switch ((uint32_t)index) {
        case OMX_IndexParamConsumerUsageBits: {
            // TODO: read from intf()
            OMX_U32 *usage = (OMX_U32 *)params;
            *usage = GRALLOC_USAGE_SW_READ_OFTEN;
            err = OK;
            break;
        }
        case OMX_IndexParamPortDefinition: {
            if (size < sizeof(OMX_PARAM_PORTDEFINITIONTYPE)) {
                return BAD_VALUE;
            }
            OMX_PARAM_PORTDEFINITIONTYPE *pDef = (OMX_PARAM_PORTDEFINITIONTYPE *)params;
            // TODO: read these from intf()
            pDef->nBufferCountActual = 16;
            pDef->eDomain = OMX_PortDomainVideo;
            pDef->format.video.nFrameWidth = 1080;
            pDef->format.video.nFrameHeight = 1920;
            err = OK;
            break;
        }
        default:
            break;
    }
    return err;
}

status_t C2OMXNode::setParameter(OMX_INDEXTYPE index, const void *params, size_t size) {
    (void)index;
    (void)params;
    (void)size;
    return ERROR_UNSUPPORTED;
}

status_t C2OMXNode::getConfig(OMX_INDEXTYPE index, void *config, size_t size) {
    (void)index;
    (void)config;
    (void)size;
    return ERROR_UNSUPPORTED;
}

status_t C2OMXNode::setConfig(OMX_INDEXTYPE index, const void *config, size_t size) {
    (void)index;
    (void)config;
    (void)size;
    return ERROR_UNSUPPORTED;
}

status_t C2OMXNode::setPortMode(OMX_U32 portIndex, IOMX::PortMode mode) {
    (void)portIndex;
    (void)mode;
    return ERROR_UNSUPPORTED;
}

status_t C2OMXNode::prepareForAdaptivePlayback(
        OMX_U32 portIndex, OMX_BOOL enable,
        OMX_U32 maxFrameWidth, OMX_U32 maxFrameHeight) {
    (void)portIndex;
    (void)enable;
    (void)maxFrameWidth;
    (void)maxFrameHeight;
    return ERROR_UNSUPPORTED;
}

status_t C2OMXNode::configureVideoTunnelMode(
        OMX_U32 portIndex, OMX_BOOL tunneled,
        OMX_U32 audioHwSync, native_handle_t **sidebandHandle) {
    (void)portIndex;
    (void)tunneled;
    (void)audioHwSync;
    *sidebandHandle = nullptr;
    return ERROR_UNSUPPORTED;
}

status_t C2OMXNode::getGraphicBufferUsage(OMX_U32 portIndex, OMX_U32* usage) {
    (void)portIndex;
    *usage = 0;
    return ERROR_UNSUPPORTED;
}

status_t C2OMXNode::setInputSurface(const sp<IOMXBufferSource> &bufferSource) {
    c2_status_t err = GetCodec2PlatformAllocatorStore()->fetchAllocator(
            C2PlatformAllocatorStore::GRALLOC,
            &mAllocator);
    if (err != OK) {
        return UNKNOWN_ERROR;
    }
    mBufferSource = bufferSource;
    return OK;
}

status_t C2OMXNode::allocateSecureBuffer(
        OMX_U32 portIndex, size_t size, buffer_id *buffer,
        void **bufferData, sp<NativeHandle> *nativeHandle) {
    (void)portIndex;
    (void)size;
    (void)nativeHandle;
    *buffer = 0;
    *bufferData = nullptr;
    return ERROR_UNSUPPORTED;
}

status_t C2OMXNode::useBuffer(
        OMX_U32 portIndex, const OMXBuffer &omxBuf, buffer_id *buffer) {
    (void)portIndex;
    (void)omxBuf;
    *buffer = 0;
    return ERROR_UNSUPPORTED;
}

status_t C2OMXNode::freeBuffer(OMX_U32 portIndex, buffer_id buffer) {
    (void)portIndex;
    (void)buffer;
    return ERROR_UNSUPPORTED;
}

status_t C2OMXNode::fillBuffer(
        buffer_id buffer, const OMXBuffer &omxBuf, int fenceFd) {
    (void)buffer;
    (void)omxBuf;
    (void)fenceFd;
    return ERROR_UNSUPPORTED;
}

status_t C2OMXNode::emptyBuffer(
        buffer_id buffer, const OMXBuffer &omxBuf,
        OMX_U32 flags, OMX_TICKS timestamp, int fenceFd) {
    // TODO: better fence handling
    if (fenceFd >= 0) {
        sp<Fence> fence = new Fence(fenceFd);
        fence->waitForever(LOG_TAG);
    }
    std::shared_ptr<C2Component> comp = mComp.lock();
    if (!comp) {
        return NO_INIT;
    }

    uint32_t c2Flags = 0;
    std::shared_ptr<C2GraphicBlock> block;

    C2Handle *handle = nullptr;
    if (omxBuf.mBufferType == OMXBuffer::kBufferTypeANWBuffer) {
        std::shared_ptr<C2GraphicAllocation> alloc;
        handle = WrapNativeCodec2GrallocHandle(
                native_handle_clone(omxBuf.mGraphicBuffer->handle),
                omxBuf.mGraphicBuffer->width,
                omxBuf.mGraphicBuffer->height,
                omxBuf.mGraphicBuffer->format,
                omxBuf.mGraphicBuffer->usage,
                omxBuf.mGraphicBuffer->stride);
        c2_status_t err = mAllocator->priorGraphicAllocation(handle, &alloc);
        if (err != OK) {
            return UNKNOWN_ERROR;
        }
        block = _C2BlockFactory::CreateGraphicBlock(alloc);
    } else if (flags & OMX_BUFFERFLAG_EOS) {
        c2Flags = C2FrameData::FLAG_END_OF_STREAM;
    } else {
        return BAD_VALUE;
    }

    std::unique_ptr<C2Work> work(new C2Work);
    work->input.flags = (C2FrameData::flags_t)c2Flags;
    work->input.ordinal.timestamp = timestamp;
    work->input.ordinal.frameIndex = mFrameIndex++;
    work->input.buffers.clear();
    if (block) {
        std::shared_ptr<C2Buffer> c2Buffer(
                // TODO: fence
                new Buffer2D(block->share(
                        C2Rect(block->width(), block->height()), ::android::C2Fence())),
                [handle, buffer, source = getSource()](C2Buffer *ptr) {
                    delete ptr;
                    native_handle_delete(handle);
                    // TODO: fence
                    (void)source->onInputBufferEmptied(buffer, -1);
                });
        work->input.buffers.push_back(c2Buffer);
    }
    work->worklets.clear();
    work->worklets.emplace_back(new C2Worklet);
    std::list<std::unique_ptr<C2Work>> items;
    items.push_back(std::move(work));

    c2_status_t err = comp->queue_nb(&items);
    if (err != C2_OK) {
        return UNKNOWN_ERROR;
    }

    return OK;
}

status_t C2OMXNode::getExtensionIndex(
        const char *parameterName, OMX_INDEXTYPE *index) {
    (void)parameterName;
    *index = OMX_IndexMax;
    return ERROR_UNSUPPORTED;
}

status_t C2OMXNode::dispatchMessage(const omx_message& msg) {
    if (msg.type != omx_message::EVENT) {
        return ERROR_UNSUPPORTED;
    }
    if (msg.u.event_data.event != OMX_EventDataSpaceChanged) {
        return ERROR_UNSUPPORTED;
    }
    // TODO: fill intf() with info inside |msg|.
    return OK;
}

sp<IOMXBufferSource> C2OMXNode::getSource() {
    return mBufferSource;
}

}  // namespace android
+90 −5
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@
#include <C2PlatformSupport.h>
#include <C2V4l2Support.h>

#include <android/IOMXBufferSource.h>
#include <gui/bufferqueue/1.0/H2BGraphicBufferProducer.h>
#include <gui/Surface.h>
#include <media/stagefright/codec2/1.0/InputSurface.h>
@@ -31,7 +32,9 @@
#include <media/stagefright/CCodec.h>
#include <media/stagefright/PersistentSurface.h>

#include "include/C2OMXNode.h"
#include "include/CCodecBufferChannel.h"
#include "include/InputSurfaceWrapper.h"

namespace android {

@@ -149,6 +152,76 @@ private:
    wp<CCodec> mCodec;
};

class C2InputSurfaceWrapper : public InputSurfaceWrapper {
public:
    explicit C2InputSurfaceWrapper(const sp<InputSurface> &surface) : mSurface(surface) {}
    ~C2InputSurfaceWrapper() override = default;

    status_t connect(const std::shared_ptr<C2Component> &comp) override {
        if (mConnection != nullptr) {
            return ALREADY_EXISTS;
        }
        mConnection = mSurface->connectToComponent(comp);
        return OK;
    }

    void disconnect() override {
        if (mConnection != nullptr) {
            mConnection->disconnect();
            mConnection.clear();
        }
    }

private:
    sp<InputSurface> mSurface;
    sp<InputSurfaceConnection> mConnection;
};

class GraphicBufferSourceWrapper : public InputSurfaceWrapper {
public:
    explicit GraphicBufferSourceWrapper(const sp<IGraphicBufferSource> &source) : mSource(source) {}
    ~GraphicBufferSourceWrapper() override = default;

    status_t connect(const std::shared_ptr<C2Component> &comp) override {
        // TODO: proper color aspect & dataspace
        android_dataspace dataSpace = HAL_DATASPACE_BT709;

        mNode = new C2OMXNode(comp);
        mSource->configure(mNode, dataSpace);

        // TODO: configure according to intf().

        sp<IOMXBufferSource> source = mNode->getSource();
        if (source == nullptr) {
            return NO_INIT;
        }
        constexpr size_t kNumSlots = 16;
        for (size_t i = 0; i < kNumSlots; ++i) {
            source->onInputBufferAdded(i);
        }
        source->onOmxExecuting();
        return OK;
    }

    void disconnect() override {
        if (mNode == nullptr) {
            return;
        }
        sp<IOMXBufferSource> source = mNode->getSource();
        if (source == nullptr) {
            ALOGD("GBSWrapper::disconnect: node is not configured with OMXBufferSource.");
            return;
        }
        source->onOmxIdle();
        source->onOmxLoaded();
        mNode.clear();
    }

private:
    sp<IGraphicBufferSource> mSource;
    sp<C2OMXNode> mNode;
};

}  // namespace

CCodec::CCodec()
@@ -314,7 +387,7 @@ void CCodec::createInputSurface() {
        return;
    }

    err = setupInputSurface(surface);
    err = setupInputSurface(std::make_shared<C2InputSurfaceWrapper>(surface));
    if (err != OK) {
        ALOGE("Failed to set up input surface: %d", err);
        mCallback->onInputSurfaceCreationFailed(err);
@@ -334,7 +407,7 @@ void CCodec::createInputSurface() {
            new BufferProducerWrapper(new H2BGraphicBufferProducer(surface)));
}

status_t CCodec::setupInputSurface(const sp<InputSurface> &surface) {
status_t CCodec::setupInputSurface(const std::shared_ptr<InputSurfaceWrapper> &surface) {
    status_t err = mChannel->setInputSurface(surface);
    if (err != OK) {
        return err;
@@ -351,10 +424,22 @@ void CCodec::initiateSetInputSurface(const sp<PersistentSurface> &surface) {
}

void CCodec::setInputSurface(const sp<PersistentSurface> &surface) {
    // TODO
    (void)surface;
    status_t err = setupInputSurface(std::make_shared<GraphicBufferSourceWrapper>(
            surface->getBufferSource()));
    if (err != OK) {
        ALOGE("Failed to set up input surface: %d", err);
        mCallback->onInputSurfaceDeclined(err);
        return;
    }

    mCallback->onInputSurfaceDeclined(ERROR_UNSUPPORTED);
    sp<AMessage> inputFormat;
    sp<AMessage> outputFormat;
    {
        Mutexed<Formats>::Locked formats(mFormats);
        inputFormat = formats->inputFormat;
        outputFormat = formats->outputFormat;
    }
    mCallback->onInputSurfaceAccepted(inputFormat, outputFormat);
}

void CCodec::initiateStart() {
Loading