Skip to main content

You are viewing Agora Docs forBetaproducts and features. Switch to Docs

How can I switch between the screen-sharing stream and the camera stream?

This article only applies to the Agora Web SDK 3.x and earlier versions.

This page introduces three solutions for switching between the screen-sharing stream and the stream captured by the camera in a screen-sharing scenario using the Agora Video SDK for Web.

Switching from the screen-sharing stream to the camera stream and vice versa are the same process. The following solutions take the first case as an example.

Solution 1: Create two streams

Create two Client objects on the sender, which correspond to the screen-sharing stream and the camera stream. See Share the Screen for details.

To switch from the screen-sharing stream to the camera stream, subscribe to the camera stream on the receiver.

We recommend this solution if your application scenario allows creating two streams at the same time.

Solution 2: Close the current stream and publish a new stream

  1. Call Client.unpublish and Stream.close in sequence to unpublish and close the current screen-sharing stream.
  2. Call AgoraRTC.createStream to create a new local video stream.
  3. Call Stream.init and Stream.publish to initialize and publish the stream.

This solution is reliable and works with multiple browsers and platforms, but it does not support dynamic switching. You may need to allow for a couple of seconds to switch streams.

Solution 3: Replace the current video track

Call Stream.replaceTrack to replace the video track in the local stream with the screen-sharing stream.

After replacing the video track with the screen-sharing stream, the encoding frame rate decreases to 5 fps.

This solution supports dynamic switching, but the replaceTrack method has the following limitations:

  • Supports only Chrome 65+, Safari and the latest Firefox browser.
  • Does not take effect on some mobile devices.
  • Does not support switching to a screen-sharing stream in dual-stream mode.
vundefined