This topic describes how to subscribe to the minor stream to improve user experience.
What are major and minor streams
Dual streams refer to pushing two camera streams simultaneously: the default high resolution stream and a low resolution stream. The subscriber can choose to subscribe to the low resolution stream based on actual needs to reduce bandwidth and performance pressure, thereby improving user experience.
Publish the minor stream
Check the environment
You can verify whether the current environment supports dual streams. Do not enable this feature if it is not supported.
// Static method
const result = AliRtcEngine.isDualVideoStreamSupported();
if (!result) {
console.log('Your browser does not support opening dual video stream.');
}Enable dual streams
Call the setEnableDualVideoStream method to enable the minor stream and set its parameters. This method can be called before or after publishing.
// Prerequisite: You need to create an engine instance first
// Enable the minor stream and set the required resolution and frame rate
// The aspect ratio of the resolution should be consistent with that of the major stream to avoid cropping
aliRtcEngine.setEnableDualVideoStream(true, {
width: 320,
height: 180,
frameRate: 200,
});
// If you need to adjust the minor stream resolution, simply call the method again
aliRtcEngine.setEnableDualVideoStream(true, {
width: 240,
height: 180,
frameRate: 200,
});Listen for minor stream status changes
If you need to handle minor stream status changes, you can listen for the dualStreamPublishStateChanged event.
aliRtcEngine.on('dualStreamPublishStateChanged', (oldState, newState, interval, channelId) => {
// Both oldState and newState are of the AliRtcSubscribeState type. The values are 0 (initialization), 1 (unpublished), 2 (being published), and 3 (published)
// interval specifies the time elapsed for the change between two states, in milliseconds
console.log(`Channel ${channelId} minor stream publish status changed from ${oldState} to ${newState}`);
});Subscribe to the minor stream
aliyun-rtc-sdk pulls the major stream by default. If you need to pull the minor stream, call the setRemoteDefaultVideoStreamType method to set it to the minor stream.
// Supports values 1 (major stream) and 2 (minor stream)
aliRtcEngine.setRemoteDefaultVideoStreamType(2);If the remote user has not published a minor stream, the major stream will be pulled automatically. Refer to the following table for the stream types subscribed during the instance process:
Default stream type set | Has the remote user published a minor stream | Stream type actually subscribed |
Major stream | No | Major stream |
Major stream | Yes | Major stream |
Minor stream | No | Major stream |
Minor stream | Yes | Minor stream |