Jump to content
  • 0

How to switch video track from Camera to Screen share and vice versa

Vijay EH


Hello everyone! We have been stuck here from a week at least trying to solve this. I'm using the webrtc-framework for android provided from official website of antmedia. I'm able to switch from front camera to back camera/vice versa. However I want to switch video track dynamically. I'm trying to create screen sharing video track and add it to peer connection like below.


public void SwitchVideoTrack() {

final EglBase.Context eglBaseContext = eglBase.getEglBaseContext();

PeerConnectionFactory peerConnectionFactory = peerConnectionClient.factory;


// create AudioSource

AudioSource audioSource = peerConnectionFactory.createAudioSource(new MediaConstraints());

this.audioTrack = peerConnectionFactory.createAudioTrack("AUDIO_TRACK_ID", audioSource);


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

// create VideoCapturer

videoCapturer = createScreenCapturer();


VideoSource videoSource = peerConnectionFactory.createVideoSource(videoCapturer.isScreencast());

localVideoTrack = peerConnectionFactory.createVideoTrack("VIDEO_TRACK_ID  ", videoSource);

videoCapturer.initialize(surfaceTextureHelper, context, videoSource.getCapturerObserver()); 

videoCapturer.startCapture(720, 1280, 30);



peerConnectionClient.localVideoSender.setTrack(localVideoTrack, true);


 Here localVideoSender is RtpSender.

public RtpSender localVideoSender;


As soon as I switch using this method, the stream stops and I get "Source error" in player where I'm streaming this video.

Please guide me to the right path if I'm missing anything else here. 



Link to comment
Share on other sites

  • Answers 2
  • Created
  • Last Reply

Top Posters For This Question

Top Posters For This Question

2 answers to this question

Recommended Posts

  • 0


Have a good day. 

The SDK has a switchCamera() function that switches front and rear cameras on the fly. We are doing our best, but it can sometimes take time to find resources for this kind of implementation. You can develop this feature according to switchCamera() codebase. If you still cannot figure it out, I can organize a meeting with our developer team.  How does it sound?

Best Regards,

Link to comment
Share on other sites


  • Create New...