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

Commit 85a53633 authored by Praveen Chavan's avatar Praveen Chavan Committed by Lajos Molnar
Browse files

NdkMediaCodec: implement createPersistenInputSurface and setInputSurface

Expose createPersistentInputSurface() and setInputSurface via
NDK-mediaCodec to enable native encoder apps to record from a
persistent input surface.

Bug: 32746065
Change-Id: Ia152f43dacfe376a89c550ecbeaf6d4016ec07b5
parent 19431587
Loading
Loading
Loading
Loading
+33 −1
Original line number Original line Diff line number Diff line
@@ -190,7 +190,39 @@ media_status_t AMediaCodec_releaseOutputBufferAtTime(
 *
 *
 * For more details, see the Java documentation for MediaCodec.createInputSurface.
 * For more details, see the Java documentation for MediaCodec.createInputSurface.
 */
 */
media_status_t AMediaCodec_createInputSurface(AMediaCodec*, ANativeWindow** surface);
media_status_t AMediaCodec_createInputSurface(
        AMediaCodec *mData, ANativeWindow **surface);

/**
 * Creates a persistent Surface that can be used as the input to encoder
 *
 * Persistent surface can be reused by MediaCodec instances and can be set
 * on a new instance via AMediaCodec_setInputSurface().
 * A persistent surface can be connected to at most one instance of MediaCodec
 * at any point in time.
 *
 * The application is responsible for releasing the surface by calling
 * ANativeWindow_release() when done.
 *
 * For more details, see the Java documentation for MediaCodec.createPersistentInputSurface.
 */
media_status_t AMediaCodec_createPersistentInputSurface(
        ANativeWindow **surface);

/**
 * Set a persistent-surface that can be used as the input to encoder, in place of input buffers
 *
 * The surface provided *must* be a persistent surface created via
 * AMediaCodec_createPersistentInputSurface()
 * This can only be called after the codec has been configured by calling
 * AMediaCodec_configure(..); and before AMediaCodec_start() has been called.
 *
 * For more details, see the Java documentation for MediaCodec.setInputSurface.
 */
media_status_t AMediaCodec_setInputSurface(
        AMediaCodec *mData, ANativeWindow *surface);




typedef enum {
typedef enum {
    AMEDIACODECRYPTOINFO_MODE_CLEAR = 0,
    AMEDIACODECRYPTOINFO_MODE_CLEAR = 0,
+1 −0
Original line number Original line Diff line number Diff line
@@ -39,6 +39,7 @@ LOCAL_C_INCLUDES := \
    frameworks/base/media/jni \
    frameworks/base/media/jni \
    frameworks/av/include/ndk \
    frameworks/av/include/ndk \
    frameworks/native/include \
    frameworks/native/include \
    frameworks/native/include/media/openmax \
    system/media/camera/include \
    system/media/camera/include \
    $(call include-path-for, libhardware)/hardware \
    $(call include-path-for, libhardware)/hardware \


+54 −0
Original line number Original line Diff line number Diff line
@@ -31,6 +31,7 @@
#include <media/stagefright/foundation/ALooper.h>
#include <media/stagefright/foundation/ALooper.h>
#include <media/stagefright/foundation/AMessage.h>
#include <media/stagefright/foundation/AMessage.h>


#include <media/stagefright/PersistentSurface.h>
#include <media/stagefright/MediaCodec.h>
#include <media/stagefright/MediaCodec.h>
#include <media/stagefright/MediaErrors.h>
#include <media/stagefright/MediaErrors.h>
#include <media/MediaCodecBuffer.h>
#include <media/MediaCodecBuffer.h>
@@ -55,6 +56,18 @@ enum {
    kWhatStopActivityNotifications,
    kWhatStopActivityNotifications,
};
};


struct AMediaCodecPersistentSurface : public Surface {
    sp<PersistentSurface> mPersistentSurface;
    AMediaCodecPersistentSurface(
            const sp<IGraphicBufferProducer>& igbp,
            const sp<PersistentSurface>& ps)
            : Surface(igbp) {
        mPersistentSurface = ps;
    }
    virtual ~AMediaCodecPersistentSurface() {
        //mPersistentSurface ref will be let go off here
    }
};


class CodecHandler: public AHandler {
class CodecHandler: public AHandler {
private:
private:
@@ -396,6 +409,47 @@ media_status_t AMediaCodec_createInputSurface(AMediaCodec *mData, ANativeWindow
    return AMEDIA_OK;
    return AMEDIA_OK;
}
}


EXPORT
media_status_t AMediaCodec_createPersistentInputSurface(ANativeWindow **surface) {
    if (surface == NULL) {
        return AMEDIA_ERROR_INVALID_PARAMETER;
    }
    *surface = NULL;

    sp<PersistentSurface> ps = MediaCodec::CreatePersistentInputSurface();
    if (ps == NULL) {
        return AMEDIA_ERROR_UNKNOWN;
    }

    sp<IGraphicBufferProducer> igbp = ps->getBufferProducer();
    if (igbp == NULL) {
        return AMEDIA_ERROR_UNKNOWN;
    }

    *surface = new AMediaCodecPersistentSurface(igbp, ps);
    ANativeWindow_acquire(*surface);

    return AMEDIA_OK;
}

EXPORT
media_status_t AMediaCodec_setInputSurface(
        AMediaCodec *mData, ANativeWindow *surface) {

    if (surface == NULL || mData == NULL) {
        return AMEDIA_ERROR_INVALID_PARAMETER;
    }

    AMediaCodecPersistentSurface *aMediaPersistentSurface =
            static_cast<AMediaCodecPersistentSurface *>(surface);
    if (aMediaPersistentSurface->mPersistentSurface == NULL) {
        return AMEDIA_ERROR_INVALID_PARAMETER;
    }

    return translate_error(mData->mCodec->setInputSurface(
            aMediaPersistentSurface->mPersistentSurface));
}

//EXPORT
//EXPORT
media_status_t AMediaCodec_setNotificationCallback(AMediaCodec *mData, OnCodecEvent callback,
media_status_t AMediaCodec_setNotificationCallback(AMediaCodec *mData, OnCodecEvent callback,
        void *userdata) {
        void *userdata) {