Subscribe: Management & Events
This guide covers managing Subscriber behavior and reacting to runtime events, for both Web (JavaScript) and React Native.
Managing streams (React Native)
Detecting when a stream's video dimensions change
A stream's video dimensions can change if a stream published from a mobile device resizes (for example, due to device orientation changes) or if a screen-sharing source window is resized. When a stream's video dimensions change, the Session object dispatches a event, with set to .
Setting the preferred frame rate and resolution
When subscribing to a stream that uses the scalable video feature, you can set to to automatically manage Subscriber video resolution and optimize CPU/network usage. For advanced control, set the preferred frame rate and resolution for the subscribed stream:
- — Valid values are 30, 15, 7, and 1. Leave undefined to use the highest available frame rate.
- — Valid values are , , and . Leave undefined to use the highest available resolution.
Detecting when streams leave a session
When a remote stream leaves a session, the component dispatches a event:
The event includes details such as , , , , , and .
Custom rendering of subscribers
By default, renders child views for subscriber videos. You can also provide a render function to fully control rendering using :
Setting stream properties
Set global properties for all subscribers via the prop on , or set per‑stream properties via the prop.
Managing subscriber streams (JavaScript only)
Restricting the frame rate of a subscribed stream
You can also restrict the frame rate of a Subscriber's video stream. To restrict the frame rate of a subscriber, call the method of the Subscriber object, passing in :
Pass in and the frame rate of the video stream is not restricted:
When the frame rate is restricted, the Subscriber video frame will update once or less per second.
This feature is only available in sessions that use the Media Router (sessions with the media mode set to routed), not in sessions with the media mode set to relayed. In relayed sessions, calling this method has no effect.
Restricting the subscriber frame rate has the following benefits:
- It reduces CPU usage.
- It reduces the network bandwidth consumed by the app.
- It lets you subscribe to more streams simultaneously.
Reducing a subscriber's frame rate has no effect on the frame rate of the video in other clients.
Detecting when a subscriber's audio is blocked or unblocked
Some browsers automatically block audio playback, requiring a event before audio playback starts for subscribers. These browsers include Safari, Firefox 66+, and Chrome 71+.
The Subscriber object displays an audio playback button if audio playback is blocked. You can disable the Subscriber's default audio playback button and display your own UI element that the user will click to start audio playback.
See Displaying a custom UI element when Subscriber audio is blocked.
When the subscriber's audio is blocked, the Subscriber object dispatches a event, and it dispatches an event when the audio is unblocked:
Also, the Subscriber includes an which returns if the audio is blocked or if it is not.
Subscriber audio is unblocked when any of the following occurs:
- The user clicks the default Subscriber audio playback icon
- The OT.unblockAudio() method is called in response to an HTML element dispatching a event (if you have disabled the default audio playback icon)
- The local client gains access to the camera or microphone (for instance, in response to a successful call to ).
For more information, see this Mozilla article about autoplay in Firefox and this Google article about autoplay in Chrome.
Detecting when a subscriber's video is disabled
When the subscriber's video is disabled, the Subscriber object dispatches a event:
When the Media Router disables the video of a subscriber, you may want to adjust the user interface related to the subscriber.
The property of the event object defines the reason the video was disabled. This can be set to one of the following values:
- — The publisher stopped publishing video by calling .
- — The Media Router stopped sending video to the subscriber based on stream quality changes. This feature of the Media Router has a subscriber drop the video stream when connectivity degrades. (The subscriber continues to receive the audio stream, if there is one.)
Before sending this event, when the Subscriber's stream quality deteriorates to a level that is low enough that the video stream is at risk of being disabled, the Subscriber dispatches a event.
If connectivity improves to support video again, the Subscriber object dispatches a event, and the Subscriber resumes receiving video.
By default, the Subscriber displays a video disabled indicator when a event with this reason is dispatched and removes the indicator when the event occurs.
When you publish a stream, you can prevent it from having its video disabled due to stream quality. Set to in the options you pass into .
Getting Subscriber stats (Web)
The method of a Subscriber object provides you with information about the subscriber's stream, including the following:
- The total number of audio and video packets lost
- The total number of audio and video packets received
- The total number of audio and video bytes received
- The current average video frame rate
The following code logs loss ratios and bit rates every second:
To get statistics for a stream published by the local client, you must use a routed session and set the option to when subscribing:
To get more detailed stream statistics, use .
Setting the preferred frame rate and resolution (Web)
When subscribing to a stream that uses the scalable video feature, you can set to to automatically manage Subscriber video resolution. For advanced control, set and in the options you pass to (or use and after subscribing).