简体   繁体   English

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

[英]Android unable to switch from camera to screen sharing webrtc in middle of communication

I'm working on android app and using webrtc with openvidu.我正在开发 android 应用程序并使用 webrtc 和 openvidu。 I got screen sharing and camera sharing working.我有屏幕共享和相机共享工作。 But only either of those works in one peerconnection.但只有其中一个在一个对等连接中有效。 I'm unable to switch between them.我无法在它们之间切换。 By default when connection is established, the camera will be shared and on click of a button I should be able to share screen without disconnecting existing connection.默认情况下,当连接建立时,相机将被共享,点击一个按钮,我应该能够在不断开现有连接的情况下共享屏幕。 However that is not happening in my case.但是,在我的情况下不会发生这种情况。 It doesn't publish the screen on click of button.它不会在单击按钮时发布屏幕。 It goes blank.它变成空白。

If anyone has worked on switching between camera to sharescreen and vice versa, please help me with this.如果有人致力于在相机和共享屏幕之间切换,反之亦然,请帮助我。 Thanks in advance.提前致谢。

    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;
        }

When I click button to share screen, I use below code:当我单击按钮共享屏幕时,我使用以下代码:

  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
                }
            }
        }
    }

References that I tried: How to share screen remotely in a video/audio call?我尝试过的参考资料: 如何在视频/音频通话中远程共享屏幕?

https://github.com/Jeffiano/ScreenShareRTC https://github.com/Jeffiano/ScreenShareRTC

https://chromium.googlesource.com/external/webrtc/+/b75f2541c942e2f35c3b7d7003ed17504176ced1/webrtc/api/android/java/src/org/webrtc/ScreenCapturerAndroid.java 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 WebRTC - 在通信过程中更改视频流

Ultimately I'm trying to find a way where I can addTrack/removeTrack in middle of communication.最终,我试图找到一种可以在通信过程中添加跟踪/删除跟踪的方法。 Thank you!谢谢!

I think you are using two video tracks.我认为您正在使用两个视频轨道。
Why don't you try with only one video track.为什么不尝试只使用一个视频轨道。
like for camera sharing, assign video track as像用于相机共享,将视频轨道分配为
mLiveSessionViewModel!!.localParticipant?.mediaStream.videotraks[0] = cameraVideoTrack mLiveSessionViewModel!!.localParticipant?.mediaStream.videotraks[0] = cameraVideoTrack
to switch to screen share, remove existing video track and add the respective videotrack切换到屏幕共享,删除现有的视频轨道并添加相应的视频轨道
mLiveSessionViewModel!!.localParticipant?.mediaStream.videotrakcs[0].removeTrack(cameraVideoTrack) mLiveSessionViewModel!!.localParticipant?.mediaStream.videotracks[0].addTrack(screenShareVideoTrack) mLiveSessionViewModel!!.localParticipant?.mediaStream.videotrakcs[0].removeTrack(cameraVideoTrack) mLiveSessionViewModel!!.localParticipant?.mediaStream.videotracks[0].addTrack(screenShareVideoTrack)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM