General concepts of screen sharing
You can publish a stream that uses a video view of your screen (instead of a camera) as the source.
A client connected to the session can subscribe to the stream (and view it) as they would subscribe to a stream that uses a camera as the source.
Publishing a stream with a screen-sharing source
To publish a screen-sharing stream, set the videoSource property of the OTPublisher properties prop to "screen":
<OTPublisher
properties={{
videoSource: 'screen',
}}
/>
The screen-sharing stream will capture video from the entire view of application. For this reason, you may want to disable subscribers in the local client when publishing a stream-sharing stream (to prevent them from appearing in the published stream).
The videoSource property of the options parameter of the OT.initPublisher() method defines the video source for the stream to be published. For screen-sharing, set this property to "screen". In older browser versions, you could also pass in "application" or "window" to specify a type of screen-sharing source. However, in current browsers that support screen sharing, passing in any of these values results in the same behavior — the browser displays a dialog box in which the end user selects the screen-sharing source (which can be the entire screen or a specific window). In Electron, screen sharing captures the entire screen, without prompting the user for permission.
The following code shows how to publish a stream that uses screen sharing as the source:
<div id="publisher"></div>
<div id="screen-preview"></div>
<script>
OT.checkScreenSharingCapability(function(response) {
if(!response.supported || response.extensionRegistered === false) {
// This browser does not support screen sharing.
} else if (response.extensionInstalled === false) {
// Prompt to install the extension.
} else {
// Screen sharing is available. Publish the screen.
var publisher = OT.initPublisher('screen-preview',
{videoSource: 'screen'},
function(error) {
if (error) {
// Look at error.message to see what went wrong.
} else {
session.publish(publisher, function(error) {
if (error) {
// Look error.message to see what went wrong.
}
});
}
}
);
}
});
</script>
Upon error, the completion handler for the OT.initPublisher() method can be passed an error object with one of the following error codes:
- 1550 — "Screen sharing is not supported."
- 1551 — "Screen sharing requires a type extension, but there is no extension registered for type."
- 1552 — "Screen sharing requires a type extension, but it is not installed."
When you publish screen-sharing stream, the following default values are set for the options parameter of the OT.initPublisher() method:
maxResolution—{ width: 1920, height: 1920 }mirror—falsefitMode—"contain"publishAudio—false
Additionally, subscribers to the resulting stream also default to using the "contain" fitMode setting.
For information on maxResolution and fitMode, see the next two sections.
By default, scalable video is disabled for screen-sharing streams. You can enable scalable video for screen-sharing streams by setting the scalableScreenshare option for the OT.initPublisher() method. Note: scalable video for screen-sharing streams is a beta feature.
To use the device's screen, instead of a camera, as the video source, use the PublisherKit class to use a custom video capturer. For a code sample, see the screen-sharing activity in the samples directory of the SDK.
Call the setPublisherVideoType(PublisherKit.PublisherKitVideoType type) property of the Publisher object and pass in PublisherKit.PublisherKitVideoTypePublisherKitVideoTypeScreen. This flags the published stream as having a screen-sharing video source (instead of a camera).
Before you publish a screen-sharing stream that uses the screen video type in a routed session, you should call the setAudioFallbackEnabled(boolean enabled) method for the PublisherKit object and pass in false. This disables the audio-only fallback feature, so that the video does not drop out in subscribers. See The Vonage Video Media Router and media modes.
By default, scalable video is disabled for screen-sharing streams. You can enable scalable video for screen-sharing streams using the PublisherKit.Builder.scalableScreenshare() method.
To use the device's screen, instead of a camera, as the video source, use the PublisherKit class to use a custom video capturer. For a code sample, see the Screen-Sharing project in the samples directory of the SDK.
Set the OTPublisherKit.videoType property to OTPublisherKitVideoTypeScreen (defined in OTPublisherKitVideoType). This flags the published stream as having a screen-sharing video source (instead of a camera).
Before you publish a screen-sharing stream that uses the screen video type in a session that uses the Vonage Video Media Server, you should set the OTPublisherKit.audioFallbackEnabled property to NO (false in swift). This disables the audio-only fallback feature, so that the video does not drop out in subscribers. See The Vonage Video Media Router and media modes.
By default, scalable video is disabled for screen-sharing streams. You can enable scalable video for screen-sharing streams using the OTPublisherKitSettings.scalableScreenshare property. Note: scalable video for screen-sharing streams is a beta feature.
To use the device's screen, instead of a camera, as the video source, use the PublisherKit class to use a custom video capturer. For a code sample, see the Screen-Sharing project in the samples directory of the SDK.
Set the OTPublisherKit.videoType property to OTPublisherKitVideoTypeScreen (defined in OTPublisherKitVideoType). This flags the published stream as having a screen-sharing video source (instead of a camera).
Before you publish a screen-sharing stream that uses the screen video type in a session that uses the Vonage Video Media Server, you should set the OTPublisherKit.audioFallbackEnabled property to NO (false in swift). This disables the audio-only fallback feature, so that the video does not drop out in subscribers. See The Vonage Video Media Router and media modes.
By default, scalable video is disabled for screen-sharing streams. You can enable scalable video for screen-sharing streams using the OTPublisherKitSettings.scalableScreenshare property. Note: scalable video for screen-sharing streams is a beta feature.
To use the device's screen, instead of a camera, as the video source, you will need to implement a custom video capturer.
Here is an example that defines a custom video capturer to implement screen sharing (using the screen instead of a camera as the video source):
Set the VideoSourceType property of the Publisher object to OpenTok.VideoSourceType.Screen. This flags the published stream as having a screen-sharing video source (instead of a camera).
By default, scalable video is disabled for screen-sharing streams. You can enable scalable video for screen-sharing streams using the Publisher.Builder.ScalableScreenshare property.
Note: scalable video for screen-sharing streams is a beta feature.
Determining the video type ("screen" or "camera") for a stream
You can subscribe to a stream that uses a screen-sharing video source in the same way that you subscribe to a stream that uses a camera as the source. See Subscribing to streams.
You can detect that a stream is a screen-sharing stream, by checking the videoType property of the event object in the OTSession streamCreated event. For a screen-sharing stream, this property is set to "screen":
<OTSession
applicationId="the API key"
sessionId="the session ID"
token="the token"
eventHandlers={{
streamCreated: event => {
console.log(event.videoType, 'stream created', event.streamId);
}
}}>
{ /* ... */ }
</OTSession>
The OTSession object dispatches a streamPropertyChanged event when the dimensions of another client's stream changes. This can happen when the publishing client resizes a window being shared (such as a browser window in an app using the Vonage Video web client SDK). The changedProperty property of the event object is set to "videoDimensions". The stream.height and stream.width property of the event object are the new width and height of the video.
<OTSession
applicationId="the API key"
sessionId="the session ID"
token="the token"
eventHandlers={{
streamPropertyChanged: event => {
console.log(event.stream.streamId, 'stream property', event.changedProperty, 'changed:', event.stream.height, event.stream.width);
}
}}>
{ /* ... */ }
</OTSession>
You may use the video dimensions and videoType details for streams to adjust the size or dimensions of their corresponding subscribers in the UI. For details on customizing the layout of subscribers in the UI, see Subscribing to streams.
The Stream object contains a videoType property. This can be set to one of the following values
"camera"— a standard video stream that uses a camera as the video source"screen"— a screen sharing video stream"custom"— a stream published by a web client using an HTML VideoTrack element as the video sourceundefined— a stream is voice-only (see the Voice-only guide)
This property can change if a stream published from a mobile device changes from a camera to a screen-sharing video type. When the video type changes, the Session object dispatches a streamPropertyChanged event.
The following code subscribes to streams and adds them to different HTML DIV container elements, based on the video type:
<div id="people"></div>
<div id="screens"></div>
<script>
session.on('streamCreated', function(event) {
var subOptions = {insertMode: 'append'};
if(event.stream.videoType === 'screen') {
session.subscribe(event.stream, 'screens', subOptions);
} else {
session.subscribe(event.stream, 'people', subOptions);
}
});
</script>
The Stream object contains a videoType property. This can be set to one of the following values, defined in the Stream.StreamVideoType enum:
StreamVideoTypeCamera— a standard video stream that uses a camera as the video sourceStreamVideoTypeScreen— a screen sharing video streamStreamVideoTypeCustom— a stream published by a web client using an HTML VideoTrack element as the video source.
The Stream object contains a videoType property. This can be set to one of the following values, defined in the OTStreamVideoType enum:
OTStreamVideoTypeCamera— a standard video stream that uses a camera as the video sourceOTStreamVideoTypeScreen— a screen sharing video streamOTStreamVideoTypeCustom— a stream published by a web client using an HTML VideoTrack element as the video source
The Stream object contains a videoType property. This can be set to one of the following values, defined in the OTStreamVideoType enum:
OTStreamVideoTypeCamera— a standard video stream that uses a camera as the video sourceOTStreamVideoTypeScreen— a screen sharing video streamOTStreamVideoTypeCustom— a stream published by a web client using an HTML VideoTrack element as the video source
The Stream object contains a VideoSourceType property. This can be set to one of the following values, defined in the OpenTok.VideoSourceType enum:
Camera— a standard video stream that uses a camera as the video sourceScreen— a screen sharing video streamCustom— a stream published by a web client using an HTML VideoTrack element as the video source.
Subscribing to screen-sharing streams
The Stream object contains a videoType property. This can be set to one of the following values
"camera"— a standard video stream that uses a camera as the video source"screen"— a screen sharing video stream"custom"— a stream published by a web client using an HTML VideoTrack element as the video sourceundefined— a stream is voice-only (see the Voice-only guide)
This property can change if a stream published from a mobile device changes from a camera to a screen-sharing video type. When the video type changes, the Session object dispatches a streamPropertyChanged event.
The following code subscribes to streams and adds them to different HTML DIV container elements, based on the video type:
<div id="people"></div>
<div id="screens"></div>
<script>
session.on('streamCreated', function(event) {
var subOptions = {insertMode: 'append'};
if(event.stream.videoType === 'screen') {
session.subscribe(event.stream, 'screens', subOptions);
} else {
session.subscribe(event.stream, 'people', subOptions);
}
});
</script>
You can subscribe to a stream that uses a screen-sharing video source in the same way that you subscribe to a stream that uses a camera as the source. See Subscribing to streams.
You can subscribe to a stream that uses a screen-sharing video source in the same way that you subscribe to a stream that uses a camera as the source. See Subscribing to streams in Swift.
You can subscribe to a stream that uses a screen-sharing video source in the same way that you subscribe to a stream that uses a camera as the source. See Subscribing to streams in Objective-C.
You can subscribe to a stream that uses a screen-sharing video source in the same way that you subscribe to a stream that uses a camera as the source. See Subscribing to streams.