Android WebRTC 自定义远程和本地视图

Android WebRTC customize remote and local view

我正在 Android 项目中实现 webrtc,我基于这个示例 in github

此示例使用 libjingle 库。这是创建视频渲染视图的方式:

        // Create video renderers.
        VideoRendererGui.setView((GLSurfaceView)videoView, new Runnable() {
            @Override
            public void run() {
                createPeerConnectionFactory();
            }
        });
        remoteRender = VideoRendererGui.create(
                REMOTE_X, REMOTE_Y,
                REMOTE_WIDTH, REMOTE_HEIGHT, scalingType, false);
        localRender = VideoRendererGui.create(
                LOCAL_X_CONNECTING, LOCAL_Y_CONNECTING,
                LOCAL_WIDTH_CONNECTING, LOCAL_HEIGHT_CONNECTING, scalingType, true);

我的问题是如何自定义 remoteRenderlocalRender,以便我可以在 GLSurfaceView 中更改 position 及其宽度和高度

编辑:

我做了一个监听器,我试过这个:

    @Override
    public void onWidthHeightChange(int width, int height) {
        VideoRendererGui.update(remoteRender,
                REMOTE_X-width, REMOTE_X-height,
                REMOTE_WIDTH-width, REMOTE_HEIGHT-height, scalingType, false);
        if (iceConnected) {
            VideoRendererGui.update(localRender,
                    LOCAL_X_CONNECTED, LOCAL_Y_CONNECTED,
                    LOCAL_WIDTH_CONNECTED, LOCAL_HEIGHT_CONNECTED,
                    ScalingType.SCALE_ASPECT_FIT, true);
        } else {
            VideoRendererGui.update(localRender,
                    LOCAL_X_CONNECTING, LOCAL_Y_CONNECTING,
                    LOCAL_WIDTH_CONNECTING, LOCAL_HEIGHT_CONNECTING, scalingType, true);
        }
    }

当我将宽度和高度的值都设置为 150 时,出现以下错误:

08-21 14:34:01.621    7636-7636/org.appspot.apprtc E/AppRTCDemoActivity﹕ Fatal error: glUseProgram: GLES20 error: 1281
    java.lang.RuntimeException: glUseProgram: GLES20 error: 1281
            at org.webrtc.GlUtil.checkNoGLES2Error(GlUtil.java:48)
            at org.webrtc.GlShader.useProgram(GlShader.java:123)
            at org.webrtc.GlRectDrawer.drawOes(GlRectDrawer.java:132)
            at org.webrtc.VideoRendererGui$YuvImageRenderer.draw(VideoRendererGui.java:371)
            at org.webrtc.VideoRendererGui$YuvImageRenderer.access0(VideoRendererGui.java:131)
            at org.webrtc.VideoRendererGui.onDrawFrame(VideoRendererGui.java:722)
            at android.opengl.GLSurfaceView$GLThread.guardedRun(GLSurfaceView.java:1522)
            at android.opengl.GLSurfaceView$GLThread.run(GLSurfaceView.java:1239)

使用解决方案编辑 2:

当我查看 mattm 答案时,我明白他是对的。

当我搜索我的异常时,我发现它是从 libjingle 库中抛出的异常。我在此处 VideoRendererGui.java 的第 368、347 行找到了这段代码,我找到了问题的解决方案。

添加视图高度和宽度时,它必须在基于此代码的这些范围内:

  /**
   * Creates VideoRenderer.Callbacks with top left corner at (x, y) and
   * resolution (width, height). All parameters are in percentage of
   * screen resolution.
   */
  public static YuvImageRenderer create(
      int x, int y, int width, int height) {
    // Check display region parameters.
    if (x < 0 || x > 100 || y < 0 || y > 100 ||
        width < 0 || width > 100 || height < 0 || height > 100 ||
        x + width > 100 || y + height > 100) {
      throw new RuntimeException("Incorrect window parameters.");
    }

所以只要我遵循这些规则,调用方法 VideoRendererGui.update(... 就可以完美地工作

谢谢

就像更改字段一样简单

  • 坐标:
    REMOTE_XREMOTE_YLOCAL_X_CONNECTINGLOCAL_Y_CONNECTING
  • 或尺寸: REMOTE_WIDTHREMOTE_HEIGHTLOCAL_WIDTH_CONNECTINGLOCAL_HEIGHT_CONNECTING

如果要在创建对象后更改渲染,请调用 VideoRendererGui.update(remoteRender, ...)