logo
Video Call
On this page

Screen Sharing

2025-01-20

Function Overview

Screen sharing refers to sharing screen content as video with other users in the room during a video call or interactive live streaming. It supports sharing the entire screen, a specific window, or a browser tab. For example, in an education scenario, a teacher needs to display a PPT on their desktop to other students in the room to improve communication efficiency.

Download Sample Source Code

Please refer to Download Sample Source Code to get the source code.

For related source code, please check the files in the "src/Examples/Others/ScreenSharing" directory.

Prerequisites

Before using screen sharing, ensure that:

Warning
  1. Screen sharing currently only supports desktop browsers (such as Windows and macOS) including Google Chrome, Microsoft Edge, and Firefox, as well as Safari on macOS.

    • Plugin-based form: Supports screen sharing in Google Chrome browser version 65 and above.
    • Plugin-free form: Supports screen sharing in Google Chrome browser version 72 and above, Microsoft Edge browser version 80 and above, and Firefox and Safari browsers version 13 and above.
  2. If you are using macOS, please first open "System Preferences > Security & Privacy > Screen Recording" and add the browser you are using before you can share your screen.

Usage Steps

(Optional) Install Screen Sharing Plugin

Only Google Chrome browser supports plugin-based screen sharing. When the browser you are using is Google Chrome and the version is between 65 and 72, you need to perform the following operations to install the plugin:

  1. Click to download ZEGO Sharing Plugin and extract it.

  2. Open Google Chrome browser, click the "Customize and control Google Chrome" button in the upper right corner of the window, and select "More tools > Extensions".

  3. Turn on the "Developer mode" switch in the upper right corner, click "Load unpacked", and select the extracted "ZEGO Sharing Plugin" folder to complete the installation.

Warning
  • If the plugin is installed, the SDK will prioritize the plugin-based screen sharing, otherwise it will use the plugin-free form.

(Optional) Listen to Event Callbacks

Listen to the screenSharingEnded callback for stopping screen sharing by calling the on interface.

Warning

Only the publishing end can receive this callback notification, while the playing end cannot receive it.

zg.on('screenSharingEnded', (stream)=> {
    //do something
    console.warn('screen sharing end');

});

Create Screen Sharing Stream

Call the createZegoStream method to create a screen sharing stream.

The "screen" object contained in the "source" parameter of the createZegoStream interface can be used to configure the screen capture stream.

Set Audio Sharing

Warning
  • When setting up audio sharing, Google Chrome browser only supports capturing and publishing system speaker audio (such as music playing on the PC). Audio captured from external microphones needs to be published separately (such as voice from speaking through the microphone).
  • Some browsers cannot share all system speaker audio. For example, Google Chrome on macOS can only share tab audio, not audio from the entire screen or specified windows.

You can use the "audio" parameter under the "screen" object to set whether to share audio. The parameter values are as follows:

Parameter ValueDescription
falseDefault value, i.e., do not share audio.
trueShare audio. Different browsers have different ways to enable audio sharing, please refer to the following.

In Google Chrome browser, after setting "audio" to "true", you also need to check "Share audio" in the browser popup to send audio to remote users. When developers select different shared content, whether the "Share audio" option in the browser popup is displayed, please refer to the following:

Share content in Google ChromemacOSWindows
Entire screenNo "Share audio" option.

Has "Share audio" option.

When this option is checked, the entire computer screen audio (including other apps) will be sent to remote users, but does not include sound captured by the microphone.

WindowNo "Share audio" option.
Google Chrome tab

Has "Share audio" option.

When this option is checked, only the media sound playing in the selected tab (excluding muted media) will be sent to remote users.

Warning

Only when "Share audio" is checked can audio be sent to remote users, and the playing end will have sound.

In Microsoft Edge browser, after setting "audio" to "true", you also need to check "Share audio" in the browser popup to send audio to remote users. When developers select different shared content, whether the "Share audio" option in the browser popup is displayed, please refer to the following:

Share content in Microsoft EdgemacOSWindows
Entire screenNo "Share audio" option.

Has "Share audio" option.

When this option is checked, the entire computer screen audio (including other apps) will be sent to remote users, but does not include sound captured by the microphone.

WindowNo "Share audio" option.
Microsoft Edge tab

Has "Share audio" option.

When this option is checked, only the media sound playing in the selected tab (excluding muted media) will be sent to remote users.

Warning

Only when "Share audio" is checked can audio be sent to remote users, and the playing end will have sound.

In Firefox browser, when sharing the screen, after setting "audio" to "true":

  • In Windows, sharing audio from the screen (audio from browser webpages, other apps, etc.) is not supported, but sending audio captured by the microphone to remote users is supported.
  • In macOS, sharing audio from the screen (audio from browser webpages, other apps, etc.) and audio captured by the microphone is supported. Developers can choose to allow opening microphone permissions when sharing the screen in the browser to share audio.

Audio sharing is not supported.

Set Screen Sharing Video Quality

You can use the "videoQuality" parameter under the "screen" object to set the screen sharing video quality (resolution, frame rate, and bitrate). The parameter values and meanings are as follows:

ValueResolutionFrame Rate (fps)Bitrate (kbps)Application Scenario
1Actual shared resolution width × Actual shared resolution height20800"1" is a preset value with a higher frame rate, suitable for scenarios with higher requirements for video smoothness.
2Actual shared resolution width × Actual shared resolution height151500"2" is a preset value, suitable for scenarios that balance video smoothness and clarity.
3Actual shared resolution width × Actual shared resolution height52000"3" is a preset value with a higher bitrate, suitable for scenarios with higher requirements for video clarity.
4width × heightframeRatebitrate"4" is a custom value, suitable for scenarios where developers want to set the sharing area, frame rate, and bitrate according to their needs.
const screenStream = await zg.createZegoStream({
    videoBitrate: 1500,
    screen: {
        audio: document.getElementById('isScreenAudio').value == 'yes' ? true : false,
        video: {
            quality: 4,
            frameRate: 15,
            width: document.getElementById('screenWidth').value * 1 || screen.width,
            height:  document.getElementById('screenHeight').value* 1 || screen.height
        }
    },
});

Publish Screen Sharing Stream

Call the startPublishingStream method to publish the screen sharing stream created by createZegoStream to the ZEGOCLOUD real-time audio/video cloud. When publishing multiple streams simultaneously, it's recommended to only publish 1 audio stream to avoid echo.

const publisRes = zg.startPublishingStream(screenStreamId, screenStream);

Watch Remote Screen Sharing

After the screen sharing stream is published to the ZEGOCLOUD real-time audio/video cloud, other users can use the startPlayingStream interface to play the screen sharing stream.

Destroy Screen Sharing Stream

Stop Publishing

If you want to destroy the screen sharing stream, you need to first call the stopPublishingStream method to stop publishing, otherwise the remote video will freeze.

zg.stopPublishingStream(screenStreamId);

Destroy Screen Sharing Stream

After stopping publishing, you need to promptly call the destroyStream method to destroy the screen sharing stream data and release occupied resources.

zg.destroyStream(screenStream);

Previous

Common Video Configuration

Next

Basic Beauty