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

Commit 28288fb0 authored by Wei Jia's avatar Wei Jia
Browse files

MediaPlayer2: use ANativeWindow to replace Surface and IGraphicBufferProducer

Test: MediaPlayer2 plays video files
Bug: 63934228
Change-Id: Id655aa19125cfc5554dbf36c223d0a27318ebb24
parent 80747152
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -246,6 +246,7 @@ cc_library_shared {
        "IStreamSource.cpp",
        "MediaUtils.cpp",
        "Metadata.cpp",
        "NdkWrapper.cpp",
    ],

    shared_libs: [
@@ -254,6 +255,8 @@ cc_library_shared {
        "libgui",
        "liblog",
        "libmediaextractor",
        "libmediandk",
        "libnativewindow",
        "libstagefright_foundation",
        "libui",
        "libutils",
@@ -261,6 +264,7 @@ cc_library_shared {

    export_shared_lib_headers: [
        "libbinder",
        "libmediandk",
    ],

    header_libs: [
@@ -328,6 +332,7 @@ cc_library_shared {
        "libmediandk",
        "libmediautils",
        "libmemunreachable",
        "libnativewindow",
        "libpowermanager",
        "libstagefright_httplive",
        "libstagefright_player2",
+18 −19
Original line number Diff line number Diff line
@@ -38,7 +38,6 @@
#include <binder/IServiceManager.h>
#include <binder/MemoryHeapBase.h>
#include <binder/MemoryBase.h>
#include <gui/Surface.h>
#include <utils/Errors.h>  // for status_t
#include <utils/String8.h>
#include <utils/SystemClock.h>
@@ -52,6 +51,8 @@
#include <media/Metadata.h>
#include <media/AudioTrack.h>
#include <media/MemoryLeakTrackUtil.h>
#include <media/NdkWrapper.h>

#include <media/stagefright/InterfaceUtils.h>
#include <media/stagefright/MediaCodecList.h>
#include <media/stagefright/MediaErrors.h>
@@ -737,9 +738,9 @@ status_t MediaPlayer2Manager::Client::setDataSource(
}

void MediaPlayer2Manager::Client::disconnectNativeWindow_l() {
    if (mConnectedWindow != NULL) {
    if (mConnectedWindow != NULL && mConnectedWindow->getANativeWindow() != NULL) {
        status_t err = nativeWindowDisconnect(
                mConnectedWindow.get(), "disconnectNativeWindow");
                mConnectedWindow->getANativeWindow(), "disconnectNativeWindow");

        if (err != OK) {
            ALOGW("nativeWindowDisconnect returned an error: %s (%d)",
@@ -750,21 +751,20 @@ void MediaPlayer2Manager::Client::disconnectNativeWindow_l() {
}

status_t MediaPlayer2Manager::Client::setVideoSurfaceTexture(
        const sp<IGraphicBufferProducer>& bufferProducer)
        const sp<ANativeWindowWrapper>& nww)
{
    ALOGV("[%d] setVideoSurfaceTexture(%p)", mConnId, bufferProducer.get());
    ALOGV("[%d] setVideoSurfaceTexture(%p)",
          mConnId,
          (nww == NULL ? NULL : nww->getANativeWindow()));
    sp<MediaPlayer2Base> p = getPlayer();
    if (p == 0) return UNKNOWN_ERROR;

    sp<IBinder> binder(IInterface::asBinder(bufferProducer));
    if (mConnectedWindowBinder == binder) {
    if (nww != NULL && nww->getANativeWindow() != NULL) {
        if (mConnectedWindow != NULL
            && mConnectedWindow->getANativeWindow() == nww->getANativeWindow()) {
            return OK;
        }

    sp<ANativeWindow> anw;
    if (bufferProducer != NULL) {
        anw = new Surface(bufferProducer, true /* controlledByApp */);
        status_t err = nativeWindowConnect(anw.get(), "setVideoSurfaceTexture");
        status_t err = nativeWindowConnect(nww->getANativeWindow(), "setVideoSurfaceTexture");

        if (err != OK) {
            ALOGE("setVideoSurfaceTexture failed: %d", err);
@@ -783,19 +783,18 @@ status_t MediaPlayer2Manager::Client::setVideoSurfaceTexture(
    // Note that we must set the player's new GraphicBufferProducer before
    // disconnecting the old one.  Otherwise queue/dequeue calls could be made
    // on the disconnected ANW, which may result in errors.
    status_t err = p->setVideoSurfaceTexture(bufferProducer);
    status_t err = p->setVideoSurfaceTexture(nww);

    mLock.lock();
    disconnectNativeWindow_l();

    if (err == OK) {
        mConnectedWindow = anw;
        mConnectedWindowBinder = binder;
        mConnectedWindow = nww;
        mLock.unlock();
    } else {
    } else if (nww != NULL) {
        mLock.unlock();
        status_t err = nativeWindowDisconnect(
                anw.get(), "disconnectNativeWindow");
                nww->getANativeWindow(), "disconnectNativeWindow");

        if (err != OK) {
            ALOGW("nativeWindowDisconnect returned an error: %s (%d)",
+5 −5
Original line number Diff line number Diff line
@@ -35,6 +35,7 @@

namespace android {

struct ANativeWindowWrapper;
struct AudioPlaybackRate;
class AudioTrack;
struct AVSyncSettings;
@@ -244,7 +245,7 @@ private:
        // MediaPlayer2Engine interface
        virtual void            disconnect();
        virtual status_t        setVideoSurfaceTexture(
                                        const sp<IGraphicBufferProducer>& bufferProducer);
                const sp<ANativeWindowWrapper>& nww) override;
        virtual status_t        setBufferingSettings(const BufferingSettings& buffering) override;
        virtual status_t        getBufferingSettings(
                                        BufferingSettings* buffering /* nonnull */) override;
@@ -375,8 +376,7 @@ private:
                    audio_session_t              mAudioSessionId;
                    audio_attributes_t *         mAudioAttributes;
                    uid_t                        mUid;
                    sp<ANativeWindow>            mConnectedWindow;
                    sp<IBinder>                  mConnectedWindowBinder;
                    sp<ANativeWindowWrapper>     mConnectedWindow;
                    struct sockaddr_in           mRetransmitEndpoint;
                    bool                         mRetransmitEndpointValid;
                    sp<Client>                   mNextClient;
+32 −6
Original line number Diff line number Diff line
@@ -17,9 +17,9 @@
//#define LOG_NDEBUG 0
#define LOG_TAG "NdkWrapper"

#include "NdkWrapper.h"
#include <media/NdkWrapper.h>

#include <gui/Surface.h>
#include <android/native_window.h>
#include <log/log.h>
#include <media/NdkMediaCodec.h>
#include <media/NdkMediaCrypto.h>
@@ -473,6 +473,31 @@ void AMediaFormatWrapper::setBuffer(const char* name, void* data, size_t size) {
}


//////////// ANativeWindowWrapper
ANativeWindowWrapper::ANativeWindowWrapper(ANativeWindow *aNativeWindow)
    : mANativeWindow(aNativeWindow) {
    if (aNativeWindow != NULL) {
        ANativeWindow_acquire(aNativeWindow);
    }
}

ANativeWindowWrapper::~ANativeWindowWrapper() {
    release();
}

status_t ANativeWindowWrapper::release() {
    if (mANativeWindow != NULL) {
        ANativeWindow_release(mANativeWindow);
        mANativeWindow = NULL;
    }
    return OK;
}

ANativeWindow *ANativeWindowWrapper::getANativeWindow() const {
    return mANativeWindow;
}


//////////// AMediaDrmWrapper
AMediaDrmWrapper::AMediaDrmWrapper(const uint8_t uuid[16]) {
    mAMediaDrm = AMediaDrm_createByUUID(uuid);
@@ -838,7 +863,7 @@ status_t AMediaCodecWrapper::getName(AString *outComponentName) const {

status_t AMediaCodecWrapper::configure(
    const sp<AMediaFormatWrapper> &format,
    const sp<Surface> &surface,
    const sp<ANativeWindowWrapper> &nww,
    const sp<AMediaCryptoWrapper> &crypto,
    uint32_t flags) {
    if (mAMediaCodec == NULL) {
@@ -848,7 +873,7 @@ status_t AMediaCodecWrapper::configure(
    media_status_t err = AMediaCodec_configure(
            mAMediaCodec,
            format->getAMediaFormat(),
            surface.get(),
            (nww == NULL ? NULL : nww->getANativeWindow()),
            crypto == NULL ? NULL : crypto->getAMediaCrypto(),
            flags);

@@ -969,12 +994,13 @@ status_t AMediaCodecWrapper::releaseOutputBuffer(size_t idx, bool render) {
        AMediaCodec_releaseOutputBuffer(mAMediaCodec, idx, render));
}

status_t AMediaCodecWrapper::setOutputSurface(const sp<Surface> &surface) {
status_t AMediaCodecWrapper::setOutputSurface(const sp<ANativeWindowWrapper> &nww) {
    if (mAMediaCodec == NULL) {
        return DEAD_OBJECT;
    }
    return translateErrorCode(
        AMediaCodec_setOutputSurface(mAMediaCodec, surface.get()));
        AMediaCodec_setOutputSurface(mAMediaCodec,
                                     (nww == NULL ? NULL : nww->getANativeWindow())));
}

status_t AMediaCodecWrapper::releaseOutputBufferAtTime(size_t idx, int64_t timestampNs) {
+1 −1
Original line number Diff line number Diff line
@@ -78,7 +78,7 @@ class TestPlayerStub : public MediaPlayer2Interface {

    // All the methods below wrap the mPlayer instance.
    virtual status_t setVideoSurfaceTexture(
            const android::sp<android::IGraphicBufferProducer>& st)  {
            const android::sp<android::ANativeWindowWrapper>& st)  {
        return mPlayer->setVideoSurfaceTexture(st);
    }
    virtual status_t prepare() {return mPlayer->prepare();}
Loading