Switching from CDN Playing Stream to Co-hosting Scenario
Feature Overview
In the co-hosting scenario, since both CDN playing stream and CDN publishing stream have high latency, the interaction experience during the co-hosting process will be very poor. Therefore, it is recommended to use RTC playing stream and RTC publishing stream during the co-hosting process, and then restore CDN playing stream after getting off the mic to improve the co-hosting experience. This article will introduce how to implement the co-hosting process in the complete CDN address playing stream method.
Comparison of Playing Stream Methods
| Playing Stream Method | Applicable Scenario | Latency |
|---|---|---|
| Playing stream from CDN | The audience does not need to co-host with the host, and interacts with the host through barrage and other methods. The requirement for real-time viewing is not high. | Latency exceeds 3 seconds |
| Playing stream from RTC | The audience needs to co-host with the host, and has high requirements for real-time interaction with the host. | Latency between both co-hosting parties is lower than 300 ms |
Prerequisites
If you need to switch from CDN playing stream to co-hosting scenario, please ensure that the current audio and video stream has implemented Playing Stream by URL.
Implementation Steps
Audience side switches playing stream method
When switching from CDN playing stream to co-hosting scenario, the audience side needs to call stopPlayingStream to stop the current CDN playing stream, and call startPlayingStream to switch to RTC playing stream.
//Stop the current CDN playing stream, pass in the corresponding host stream ID
engine.stopPlayingStream(anchorStreamID);
//Switch to RTC playing stream
ZegoCanvas zegoCanvas = new ZegoCanvas(view);
zegoCanvas.viewMode = ZegoViewMode.ASPECT_FILL;
ZegoPlayerConfig playerConfig = new ZegoPlayerConfig();
playerConfig.resourceMode = ZegoStreamResourceMode.ONLY_RTC;
engine.startPlayingStream(anchorStreamID, zegoCanvas, playerConfig);Audience side starts preview and publishing stream
Pass in the stream ID of the audience side, and start preview startPreview and publishing stream startPublishingStream, then the co-hosting can be successful.
//Start preview
ZegoCanvas previewCanvas = new ZegoCanvas(view);
previewCanvas.viewMode = ZegoViewMode.ASPECT_FILL;
engine.startPreview(previewCanvas);
//Start publishing stream, and pass in the stream ID of the audience side
engine.startPublishingStream(pulishingStreamID);Audience side stops preview and publishing stream
If the co-hosting ends, the audience side can call the stopPreview and stopPublishingStream interfaces to stop preview and publishing stream.
//Stop preview
engine.stopPreview();
//Stop publishing stream
engine.stopPublishingStream();Audience side co-hosting ends
After the co-hosting ends, pass in the host's stream ID, and call the stopPlayingStream interface on the audience side to stop RTC playing stream, switch back to CDN playing stream, and set CDN parameters through ZegoViewMode. For details, please refer to Playing Stream by URL.
//Stop the current RTC playing stream
engine.stopPlayingStream(anchorStreamID);
//Switch to CDN playing stream
ZegoCanvas zegoCanvas = new ZegoCanvas(view);
zegoCanvas.viewMode = ZegoViewMode.ASPECT_FILL;
ZegoPlayerConfig playerConfig = new ZegoPlayerConfig();
playerConfig.cdnConfig = new ZegoCDNConfig(); // Set CDN parameters
playerConfig.cdnConfig.url = "rtmp://xxxxxxxx"; // URL is the CDN playing stream address
playerConfig.cdnConfig.authParam = "xxx"; // If authentication is required, set the authentication parameter. If authentication is not required, it can be not set (The authentication parameter cannot contain the "?" character)
engine.startPlayingStream(anchorStreamID, zegoCanvas, playerConfig);Host side starts and stops playing stream
The host side receives the notification of the addition of audience streams through the onRoomStreamUpdate callback in IZegoEventHandler, and can start RTC playing stream. When receiving the notification of the deletion of audience streams, stop RTC playing stream.
private final IZegoEventHandler eventHandler = new IZegoEventHandler() {
@Override
public void onRoomStreamUpdate(String roomID, ZegoUpdateType updateType, ArrayList<ZegoStream> streamList, JSONObject extendedData) {
super.onRoomStreamUpdate(roomID, updateType, streamList, extendedData);
if (ZegoUpdateType.ADD == updateType) {
for (ZegoStream stream : streamList) {
//Start RTC playing audience stream
ZegoCanvas zegoCanvas = new ZegoCanvas(view);
zegoCanvas.viewMode = ZegoViewMode.ASPECT_FILL;
ZegoPlayerConfig playerConfig = new ZegoPlayerConfig();
playerConfig.resourceMode = ZegoStreamResourceMode.ONLY_RTC;
engine.startPlayingStream(stream.streamID, zegoCanvas, playerConfig);
}
} else if (ZegoUpdateType.DELETE == updateType) {
for (ZegoStream stream : streamList) {
//Stop RTC playing audience stream
engine.stopPlayingStream(stream.streamID);
}
}
}
};