首页 > 解决方案 > android antmedia webrtc 将摄像头切换到屏幕共享,反之亦然

问题描述

我正在使用 Antmedia 官网提供的 webrtc-android-framework 模块。我能够建立连接,并且可以毫无问题地看到另一端发布的视频。但是我无法从相机切换到屏幕共享。我正在使用下面的代码从相机捕捉切换到屏幕共享。

public void MakeScreenCaptureReady() {
            final EglBase.Context eglBaseContext = eglBase.getEglBaseContext();
            PeerConnectionFactory peerConnectionFactory = peerConnectionClient.factory;
    
            // create AudioSource
            AudioSource audioSource = peerConnectionFactory.createAudioSource(new MediaConstraints());
            this.audioTrack = peerConnectionFactory.createAudioTrack("101", audioSource);
    
            surfaceTextureHelper = SurfaceTextureHelper.create("CaptureThread", eglBaseContext);
            // create VideoCapturer
            videoCapturer = createScreenCapturer();
    
    
            VideoSource videoSource = 
            peerConnectionFactory.createVideoSource(videoCapturer.isScreencast());
            localVideoTrack = peerConnectionFactory.createVideoTrack("118", videoSource);
            videoCapturer.initialize(surfaceTextureHelper, context, videoSource.getCapturerObserver()); 
            videoCapturer.startCapture(720, 1280, 30);
    
            peerConnectionClient.setLocalVideoTrack(localVideoTrack);
            peerConnectionClient.localVideoSender.setTrack(localVideoTrack, true); //true for taking ownership and replacing the existing track
    }

它会将屏幕共享视频缓冲 2-3 秒,并source error在订阅者端停止投掷。服务器上基本上没有可用的块来进一步缓冲。在点击上述代码之前,我已经获得了屏幕共享所需的权限。

startActivityForResult(
                    mMediaProjectionManager!!.createScreenCaptureIntent(),
                    SCREEN_RECORD_REQUEST_CODE
            )

这是我用来在 onActivityResult 上调用上述方法的以下代码:

        intent.putExtra(CallActivity.EXTRA_SCREENCAPTURE, true)
        webRTCClient.setMediaProjectionParams(resultCode, data)
        webRTCClient.MakeScreenCaptureReady()

如何实现相机和截屏的切换?任何帮助深表感谢。谢谢!

标签: androidkotlinwebrtcant-media-serverwebrtc-android

解决方案


推荐阅读