Android无法在通信过程中从相机切换到屏幕共享webrtc

Posted

技术标签:

【中文标题】Android无法在通信过程中从相机切换到屏幕共享webrtc【英文标题】:Android unable to switch from camera to screen sharing webrtc in middle of communication 【发布时间】:2020-11-25 04:38:08 【问题描述】:

我正在开发 android 应用程序并将 webrtc 与 openvidu 一起使用。我得到了屏幕共享和相机共享工作。但只有其中任何一种适用于一个对等连接。我无法在它们之间切换。默认情况下,当建立连接时,相机将被共享,单击按钮我应该能够在不断开现有连接的情况下共享屏幕。但是,在我的情况下并没有发生这种情况。它不会在单击按钮时发布屏幕。一片空白。

如果有人在相机和共享屏幕之间切换,反之亦然,请帮助我。 提前致谢。

    public VideoTrack MakeCameraSettingsReady() 
            final EglBase.Context eglBaseContext = EglBase.create().getEglBaseContext();
            PeerConnectionFactory peerConnectionFactory = this.sessionManager.getPeerConnectionFactory();
    
            // create Audiosource
            AudioSource audioSource = peerConnectionFactory.createAudioSource(new MediaConstraints());
            this.audioTrack = peerConnectionFactory.createAudioTrack("101", audioSource);
    
            surfaceTextureHelper = SurfaceTextureHelper.create("CaptureThread", eglBaseContext);
            // create VideoCapturer
            videoCapturer = createVideoCapturer();
    
            //MediaConstraints constraints = new MediaConstraints();
    
            VideoSource videoSource = peerConnectionFactory.createVideoSource(videoCapturer.isScreencast());
            localVideoTrack = peerConnectionFactory.createVideoTrack("100", videoSource);
            videoCapturer.initialize(surfaceTextureHelper, context, videoSource.getCapturerObserver());
            videoCapturer.startCapture(720, 1280, 30);
    
            return localVideoTrack;
        
    
        public VideoTrack MakeScreenCaptureReady() 
            final EglBase.Context eglBaseContext = EglBase.create().getEglBaseContext();
            PeerConnectionFactory peerConnectionFactory = this.sessionManager.getPeerConnectionFactory();
    
            // create AudioSource
            AudioSource audioSource = peerConnectionFactory.createAudioSource(new MediaConstraints());
            this.audioTrack = peerConnectionFactory.createAudioTrack("101", audioSource);
    
            surfaceTextureHelper = SurfaceTextureHelper.create("CaptureThread", eglBaseContext);
            // create VideoCapturer
            screenCapturerAndroid = (ScreenCapturerAndroid) createScreenCapturer();
    
            //MediaConstraints constraints = new MediaConstraints();
    
            VideoSource videoSource = peerConnectionFactory.createVideoSource(screenCapturerAndroid.isScreencast());
            localVideoTrack = peerConnectionFactory.createVideoTrack("100", videoSource);
            screenCapturerAndroid.initialize(surfaceTextureHelper, context, videoSource.getCapturerObserver());
            screenCapturerAndroid.startCapture(720, 1280, 30);

            return localVideoTrack;
        

当我点击按钮分享屏幕时,我使用以下代码:

  private fun stopCameraShare()
            videoCapturerAndroid?.stopCapture()
            localRenderer.dispose()
            localVideoView.release()
            localVideoView.clearImage()
            stream?.removeTrack(localVideoTrack)
            localVideoTrack.dispose()
        

    private fun shareScreen()
            stopCameraShare()
            val mediaProjectionManager = activity!!.getSystemService(Context.MEDIA_PROJECTION_SERVICE) as MediaProjectionManager
            startActivityForResult(mediaProjectionManager.createScreenCaptureIntent(), 29)
        

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) 
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == SCREEN_RECORD_REQUEST_CODE) 
            if (resultCode == RESULT_OK) 
                //Start screen recording
                mResultCode = resultCode
                mResultData = data

                runOnUiThread 
                    screenCaptureVideoTrack = mLiveSessionViewModel!!.localParticipant?.MakeScreenCaptureReady()
                    mLiveSessionViewModel!!.localParticipant?.screenCaptureVideoTrack = screenCaptureVideoTrack
                
            
        
    

我尝试过的参考资料: How to share screen remotely in a video/audio call?

https://github.com/Jeffiano/ScreenShareRTC

https://chromium.googlesource.com/external/webrtc/+/b75f2541c942e2f35c3b7d7003ed17504176ced1/webrtc/api/android/java/src/org/webrtc/ScreenCapturerAndroid.java

WebRTC - change video stream in the middle of communication

最终,我试图找到一种可以在通信过程中添加跟踪/删除跟踪的方法。谢谢!

【问题讨论】:

【参考方案1】:

我认为您正在使用两个视频轨道。 您为什么不尝试只使用一个视频轨道。 像相机共享一样,将视频轨道分配为 mLiveSessionViewModel!!.localParticipant?.mediaStream.videotraks[0] = cameraVideoTrack 切换到屏幕共享,删除现有视频轨道并添加相应的视频轨道 mLiveSessionViewModel!!.localParticipant?.mediaStream.videotrakcs[0].removeTrack(cameraVideoTrack) mLiveSessionViewModel!!.localParticipant?.mediaStream.videotracks[0].addTrack(screenShareVideoTrack)

【讨论】:

以上是关于Android无法在通信过程中从相机切换到屏幕共享webrtc的主要内容,如果未能解决你的问题,请参考以下文章

在 JavaScript 和 ActionScript 之间共享数组引用

在android webrtc中切换相机(后/前)

如何在片段中从相机捕获图像,

在 iOS 中从浏览器访问相机胶卷

切换相机后音频/视频不同步

如何在我的 Xcode Swift 项目的不同导航堆栈中从一个视图控制器屏幕切换到另一个?