Implement Screen Share from android Application to web users

Thanks a ton for sharing your experience and detailed code on using native WebRTC on Android. It’s super helpful!

I’ve found a way to switch between camera and screen sharing smoothly in the OpenVidu session. Here’s how I did it:
Updating the Session Class

  1. Replace the Video Track:

public void replaceVideoTrack(VideoTrack newVideoTrack) {
    PeerConnection peerConnection = localParticipant.getPeerConnection();

    if (peerConnection != null) {
        for (RtpSender sender : peerConnection.getSenders()) {
            if (sender.track() != null && sender.track().kind().equals("video")) {
                sender.setTrack(newVideoTrack, false);
                break;
            }
        }

        renegotiate();
    }
}

private void renegotiate() {
    MediaConstraints constraints = new MediaConstraints();
    createOfferForPublishing(constraints);
}

Integrating Screen Sharing Toggle
Updated handleToggleScreen to use the new method:

    private void handleToggleScreen(Boolean isScreenSharing, int resultCode, Intent data) {
        if (isScreenSharing) {
            PeerConnectionFactory peerConnectionFactory = this.session.getPeerConnectionFactory();

            videoCapturer = createScreenCapture(data, resultCode);

            if (videoCapturer != null) {

                DisplayMetrics displayMetrics = new DisplayMetrics();
                WindowManager windowsManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
                windowsManager.getDefaultDisplay().getMetrics(displayMetrics);
                int screenWidthPixels = displayMetrics.widthPixels;
                int screenHeightPixels = displayMetrics.heightPixels;

                eglBaseContext = EglBase.create().getEglBaseContext();
                surfaceTextureHelper = SurfaceTextureHelper.create("CaptureThread", eglBaseContext);

                VideoSource screenSource = peerConnectionFactory.createVideoSource(videoCapturer.isScreencast());
                videoCapturer.initialize(surfaceTextureHelper, context, screenSource.getCapturerObserver());
                videoCapturer.startCapture(screenWidthPixels, screenHeightPixels, 15);

                // Create new VideoTrack
                VideoTrack newVideoTrack = peerConnectionFactory.createVideoTrack("100", screenSource);
                newVideoTrack.addSink(localVideoView);
                newVideoTrack.setEnabled(true);

                // Update the local participant's video track reference
                setVideoTrack(newVideoTrack);

                // Replace the current video track in the peer connection
                session.replaceVideoTrack(newVideoTrack);

            }
        } else {
            startCamera();
            session.replaceVideoTrack(getVideoTrack());

        }
    }

Summary

  1. Add replaceVideoTrack Method: This method allows me to switch the video track in the peer connection.
  2. Update handleToggleScreen: This sets up the new video track and uses replaceVideoTrack.
  3. Renegotiate if Needed: Ensures the connection updates correctly.

With this setup, I can easily switch between camera and screen sharing in my OpenVidu session.

Thanks again for your help! This has made my project a lot better. If you have any more tips or things I should look out for, please let me know!