安卓通过Parcel传递Surface到native层(Mediaplayer.java)

使用MediaPlayer中的setParameter或者invoke方法(需要反射),伪代码示例:

应用层Java伪代码:

public Parcel newRequest() {
    Parcel parcel = Parcel.obtain();
    parcel.writeInterfaceToken(IMEDIA_PLAYER);
    return parcel;
}

public void setSurface(Surface surface) {
     Parcel request= newRequest();
     Parcel reply = Parcel.obtain();
     request.writeInt(KEY_ID); 
     surface.writeToParcel(request, 0);
     invokeAPI (request,  reply);
}
 
private int invokeAPI(Parcel request, Parcel reply) {
        int ret = -1;
        try {
            Class<?> clsMediaPlayer = Class.forName("android.media.MediaPlayer");
            Method getInvoke = clsMediaPlayer.getMethod("invoke",Parcel.class, Parcel.class);
            getInvoke.setAccessible(true);
            getInvoke.invoke(mMediaPlayer, request , reply);
            ret = reply.readInt();
            Log.d(TAG,"invokeExtend reply =" + ret);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } finally {
            return ret;
        }
}

本地层MediaPlayerService中读取:

#include <gui/view/Surface.h>
#include <gui/IGraphicBufferProducer.h>

status_t MediaPlayerService::invoke(const Parcel &request, Parcel *reply) {
    if (reply == NULL) {
        return BAD_VALUE;
    }
 
    int32_t key;
    status_t ret = request.readInt32(&key);
    if (ret != OK) {
        return ret;
    }
 
    switch (key) {
        case KEY_ID :
        {
            android::view::Surface surfaceShim;
            surfaceShim.readFromParcel(&request, /*nameAlreadyRead*/false);
            if (surfaceShim.graphicBufferProducer != nullptr) {
                sp<IGraphicBufferProducer> graphicBufferProducer = surfaceShim.graphicBufferProducer;
            /*如此可以获取到IGraphicBufferProducer */
                return OK;
            } 
            return BAD_VALUE;
        }
        default:
        {
            return INVALID_OPERATION;
        }
    }
}

备注:

surfaceShim.readFromParcel( &request, /*nameAlreadyRead*/false);  //第二个参数要设置为false

因为parcel中默认添加了一个name字段,这里是第一次读取这个parcel,设置false后surfaceShim.readFromParcel函数会去取这个name字段,不影响后读取parcel其他字段。

参考:

Android11\frameworks\native\libs\gui\view\Surface.cpp

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
android.os.BadParcelableException: ClassNotFoundException when unmarshalling: at android.os.Parcel.readParcelableCreator(Parcel.java:3364) at android.os.Parcel.readParcelable(Parcel.java:3272) at android.view.InsetsSourceControl.<init>(InsetsSourceControl.java:71) at android.view.InsetsSourceControl$1.createFromParcel(InsetsSourceControl.java:116) at android.view.InsetsSourceControl$1.createFromParcel(InsetsSourceControl.java:114) at android.os.Parcel.readTypedObject(Parcel.java:3119) at android.os.Parcel.readTypedArray(Parcel.java:3089) at android.view.IWindowSession$Stub$Proxy.relayout(IWindowSession.java:1739) at android.view.ViewRootImpl.relayoutWindow(ViewRootImpl.java:7702) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2816) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:2133) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:8439) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1089) at android.view.Choreographer.doCallbacks(Choreographer.java:907) at android.view.Choreographer.doFrame(Choreographer.java:842) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1074) at android.os.Handler.handleCallback(Handler.java:967) at android.os.Handler.dispatchMessage(Handler.java:104) at android.os.Looper.loop(Looper.java:243) at android.app.ActivityThread.main(ActivityThread.java:8021) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:591) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1164)
06-10

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值