Enable dual-stream mode

更新时间:
复制 MD 格式

Dual-stream mode publishes a high-resolution and a low-resolution camera stream simultaneously, allowing subscribers to reduce bandwidth usage and system load.

What is dual-stream mode?

Dual-stream mode publishes two camera streams at the same time: a default high-resolution stream and a low-resolution stream. Subscribers can switch to the low-resolution stream to reduce bandwidth consumption and improve performance.

Publish the low-res stream

Check environment support

Before you enable dual-stream mode, verify that the current environment supports it.

// Static method
const result = AliRtcEngine.isDualVideoStreamSupported();
if (!result) {
  console.log('Your browser does not support opening dual video stream.');
}

Enable dual-stream mode

Call setEnableDualVideoStream to enable the low-resolution stream and configure its parameters. You can call this method before or after you start publishing.

// Prerequisite: An engine instance must be created first.
// Enable the low-res stream and set the required resolution and frame rate.
// The aspect ratio should be consistent with that of the high-res stream to avoid cropping.
aliRtcEngine.setEnableDualVideoStream(true, {
  width: 320,
  height: 180,
  frameRate: 200,
});

// If you need to adjust the parameters, call the method again.
aliRtcEngine.setEnableDualVideoStream(true, {
  width: 240,
  height: 180,
  frameRate: 200,
});

Listen for low-res stream state changes

Listen for the dualStreamPublishStateChanged event to track low-resolution stream state changes.

aliRtcEngine.on('dualStreamPublishStateChanged', (oldState, newState, interval, channelId) => {
  // Both oldState and newState are of the AliRtcSubscribeState type. Valid values: 0 (Initializing), 1 (Unpublished), 2 (Publishing), and 3 (Published).
  // interval is the time elapsed between the two states, in milliseconds.
  console.log(`The low-res stream publish state in channel ${channelId} changed from ${oldState} to ${newState}`);
});

Subscribe to the low-res stream

By default, aliyun-rtc-sdk subscribes to the high-resolution stream. To subscribe to the low-resolution stream instead, call setRemoteDefaultVideoStreamType.

// Accepts 1 (high-res stream) or 2 (low-res stream)
aliRtcEngine.setRemoteDefaultVideoStreamType(2);

If the remote user has not published a low-resolution stream, the SDK automatically falls back to the high-resolution stream. The following table describes the actual stream type received in each scenario:

Default stream type

Low-res stream published?

Actual stream subscribed

High-res stream

No

High-res stream

High-res stream

Yes

High-res stream

Low-res stream

No

High-res stream

Low-res stream

Yes

Low-res stream