Screen sharing enables the host of a video call or interactive live streaming to display what is on their screen to other users in the channel. This function has many obvious advantages for communicating information, particularly in the following scenarios:
In order to enable screen sharing on the web client, you need to call createStream to create a screen-sharing stream. You do this by setting the relevant properties when you create the stream. The property settings of a stream differ between browsers (as shown in Implementation). After you call createStream, the web browser pops up a window to let the end user select which screens to share and then shares the selected screens.
The following are general guidelines:
createClient once to create one client object. Set video as false, and screen as true when calling createStream.createClient twice to create two client objects:video as true and screen as false when calling createStream.video as false and screen as true when calling createStream.As of v3.2.0, the Web SDK supports setting the transmission optimization strategy by using optimizationMode. For a screen-sharing stream, the default value of the optimizationMode property is "detail" and the SDK prioritizes clarity. This strategy is applicable to scenarios where the shared content is text or images. If the shared content is a movie or video clip, Agora recommends setting optimizationMode as "motion". The SDK prioritizes smoothness.
This section introduces how to share the screen on Chrome, Edge, Firefox, and Electron.
Before you begin, ensure that you understand how to start a video call or start interactive video streaming.
The Web SDK supports screen sharing on Chrome 58 or later. There are two ways to enable screen sharing on Chrome: with or without using the Google Chrome Extension for Screen Sharing provided by Agora.
Screen sharing without the extension requires the Agora Web SDK v2.6 or later, and Chrome 72 or later. Otherwise, you have to use the Google Chrome extension to share the screen.
To enable screen sharing without the extension, set video as false, and screen as true when calling createStream to create a screen-sharing stream.
// Check if the browser supports screen sharing without an extension.
Number.tem = ua.match(/(Chrome(?=\/))\/?(\d+)/i);
if(parseInt(tem[2]) >= 72 && navigator.mediaDevices.getDisplayMedia ) {
// Create the stream for screen sharing.
screenStream = AgoraRTC.createStream({
streamID: uid,
audio: false,
video: false,
screen: true,
});
}
Perform the following steps to share the screen on Chrome using the extension:
Add the Google Chrome Extension for Screen Sharing provided by Agora and get an extension ID.
Set video as false, screen as true, and extensionId as the string you get in the previous step when calling createStream to create a screen-sharing stream.
screenStream = AgoraRTC.createStream({
streamID: uid,
audio: false,
video: false,
screen: true,
//chrome extension ID
extensionId: 'minllpmhdgpndnkomcoccfekfegnlikg'
});
The Web SDK supports screen sharing on Edge 80 or later on Windows 10+. To enable screen sharing on Edge, set video as false, and screen as true when calling createStream to create a screen-sharing stream.
screenStream = AgoraRTC.createStream({
streamID: uid,
audio: false,
video: false,
screen: true,
});
The Web SDK supports screen sharing on Firefox 56 or later. To enable screen sharing on Firefox, in addition to setting video as false, and screen as true when calling createStream to create a screen-sharing stream, you also need to set the mediaSource property to specify the screen-sharing mode:
screen: Shares the whole screen.application: Shares all windows of an application.window: Shares a specific window of an application.Firefox on Windows does not support the application mode.
screenStream = AgoraRTC.createStream({
streamID: uid,
audio: false,
video: false,
screen: true,
mediaSource: 'screen' // 'screen', 'application', 'window'
});
To enable screen sharing on Electron, you need to draw the UI for your users to select which screen or window to share.
For quick integration, perform the following steps to use the default UI provided by Agora:
Call AgoraRTC.createStream, and set video as false, and screen as true to create a screen-sharing stream.
localStream = AgoraRTC.createStream({
streamID: UID,
audio: false,
video: false,
screen: true
});
Call localStream.init to initialize the stream.
localStream.init(function(stream) {})
The SDK provides a default UI for screen source selection, as the following figure shows:
If you want to customize the UI, perform the following steps:
Call AgoraRTC.getScreenSources to get sources of the screens to share.
AgoraRTC.getScreenSources(function(err, sources) {
console.log(sources)
})
The sources parameter is an array of the source object. A source object contains the following properties of the screen source:
id: The sourceId.name: The name of the screen source.thumbnail: The thumbnail of the screen source.Based on the properties of source, draw the UI (by HTML and CSS) for the end user selecting the screen source to share.
The following figure shows the properties' corresponding elements on the UI for screen source selection:
Get the sourceId of the source selected by the end user.
Set sourceId when creating the screen-sharing stream.
localStream = AgoraRTC.createStream({
streamID: UID,
audio: false,
video: false,
screen: true,
sourceId: sourceId
});
localStream.init(function(stream) {})
- The
getScreenSourcesmethod is a wrapper ofdesktopCapturer.getSourcesprovided by Electron. See desktopCapturer.- Only Electron requires the
sourceIdparameter.
As of v3.0.0, the Web SDK supports sharing the local audio playback when sharing a screen on Windows Chrome 74 or later. To share the audio, set the screen and screenAudio properties as true and the audio property as false when calling createStream to create a screen-sharing stream.
screenStream = AgoraRTC.createStream({
streamID: uid,
audio: false,
video: false,
screen: true,
screenAudio: true
});
audio and screenAudio properties are different:
audio controls whether the stream contains the audio captured by the local audio input device.screenAudio controls whether the stream contains the local audio playback.audio as false in the screen-sharing stream. If you set both audio and screenAudio as true, the stream contains the local audio playback only.The following sample code shows the complete steps to enable screen sharing.
isFirefox and isCompatibleChrome to determine the browser type, and you need to define the functions. See the code in common.js for an example.//TODO: Fill in your App ID.
var appID = "<yourAppID>";
var channel = "screen_video";
var channelKey = null;
AgoraRTC.Logger.setLogLevel(AgoraRTC.Logger.INFO);
var screenClient = AgoraRTC.createClient({
mode: 'rtc',
codec: 'vp8'
});
screenClient.init(appID, function() {
screenClient.join(channelKey, channel, null, function(uid) {
// Create the stream for screen sharing.
const streamSpec = {
streamID: uid,
audio: false,
video: false,
screen: true
}
// Set relevant properties according to the browser.
// Note that you need to implement isFirefox and isCompatibleChrome.
if (isFirefox()) {
streamSpec.mediaSource = 'window';
} else if (!isCompatibleChrome()) {
streamSpec.extensionId = 'minllpmhdgpndnkomcoccfekfegnlikg';
}
screenStream = AgoraRTC.createStream(streamSpec);
// Initialize the stream.
screenStream.init(function() {
// Play the stream.
screenStream.play('Screen');
// Publish the stream.
screenClient.publish(screenStream);
}, function(err) {
console.log(err);
});
}, function(err) {
console.log(err);
})
});
Additionally, we provide an open-source GitHub sample project. You can try it online and refer to the code in rtc-client.js and index.js.
One client only sends one stream. If you want to enable both screen sharing and video capturing on one host, you need to create two clients:
// Create the client to send the screen-sharing stream.
var screenClient = AgoraRTC.createClient({mode: 'rtc', codec: 'vp8'});
screenClient.init(key, function() {
screenClient.join(channelKey, channel, null, function(uid) {
// Create the screen-sharing stream, screenStream.
...
screenClient.publish(screenStream);
}
}
// Create the client to send the video stream.
var videoClient = AgoraRTC.createClient({mode: 'rtc', codec: 'vp8'});
videoClient.init(key, function() {
videoClient.join(channelKey, channel, null, function(uid) {
// Create the video stream, videoStream.
...
videoClient.publish(videoStream);
}
}
Billing issues: Two clients of a host subscribing to each other incurs extra charges, as shown in the following image:
Agora recommends that you save the returned uid when each client joins the channel and handle the subscription as follows:
stream-added event occurs, first check if the joined uid belongs to a local stream. If yes, do not subscribe to the stream.var localStreams = [];
...
screenClient.join(channelKey, channel, null, function(uid) {
// Save the uid of the local stream.
localStreams.push(uid);
}
...
videoClient.on('stream-added', function(evt) {
var stream = evt.stream;
var uid = stream.getId()
// When the 'stream-added' event occurs, check if the stream is a local uid.
if(!localStreams.includes(uid)) {
console.log('subscribe stream: ' + uid);
// Subscribe to the stream.
videoClient.subscribe(stream);
}
})
The default video profile for screen sharing is a resolution of 1920 × 1080 (width × height) and a frame rate of 5 fps. To use a different profile, call Stream.setScreenProfile to set the screen profile, as in the following example:
// After creating a stream for screen sharing
screenStream.setScreenProfile("720p_1");
The SDK supports the following screen profiles:
| Screen profile | Resolution (width × height) | Frame rate |
|---|---|---|
480p_1 |
640 × 480 | 5 fps |
480p_2 |
640 × 480 | 30 fps |
720p_1 |
1280 × 720 | 5 fps |
720p_2 |
1280 × 720 | 30 fps |
1080p_1 |
1920 × 1080 | 5 fps |
1080p_2 |
1920 × 1080 | 30 fps |
Stream.init) the screen-sharing stream.video property as false when creating a screen-sharing stream.audio property as true for only one local stream.When implementing the screen sharing, you can also refer to the following articles: