Switching from CDN Stream Playing to Co-hosting Scenario
Introduction
In the co-hosting scenario, since both CDN stream playing and CDN stream publishing have high latency, the interactive experience during the co-hosting process will be very poor. Therefore, it is recommended to use RTC stream playing and RTC stream publishing during the co-hosting process, and restore CDN stream playing after getting off the mic to improve the co-hosting experience. This document will introduce how to implement the co-hosting process under the complete CDN address stream playing method.
Comparison of Stream Playing Methods
| Stream Playing Method | Applicable Scenario | Latency Situation |
|---|---|---|
| Play stream from CDN | Audiences do not need to co-host with the host, and interact with the host through bullet comments and other methods, with low requirements for viewing real-time performance. | Latency exceeds 3 seconds |
| Play stream from RTC | Audiences need to co-host with the host, and have high requirements for real-time interaction and communication with the host. | Latency between both co-hosting parties is lower than 300 ms |
Prerequisites
To switch from CDN stream playing to co-hosting scenario, please ensure that the current audio and video stream has implemented Playing Stream by URL.
Implementation Steps
Audience switches stream playing method
When switching from CDN stream playing to co-hosting scenario, the audience needs to call stopPlayingStream to stop the current CDN stream playing, and call startPlayingStream to switch to RTC stream playing.
//Stop the current CDN stream playing, pass in the corresponding host stream ID
engine->stopPlayingStream(anchorStreamID);
//Switch to RTC stream playing
ZegoCanvas canvas(ZegoUtilHelper::GetView(ui->frame_Play));
ZegoPlayerConfig config;
config.resourceMode = ZEGO_STREAM_RESOURCE_MODE_ONLY_RTC;
engine->startPlayingStream(m_currentPlayStreamId, &canvas, config);Audience starts preview and publishes stream
Pass in the audience's stream ID, and start preview startPreview and publish stream startPublishingStream, and the co-hosting will be successful.
//Start preview
ZegoCanvas canvas(ZegoUtilHelper::GetView(ui->frame_Preview));
engine->startPreview(&canvas);
//Start publishing stream, and pass in the audience's stream ID
engine->startPublishingStream(pulishingStreamID);Audience stops preview and publishing stream
If the co-hosting ends, the audience can call the stopPreview and stopPublishingStream interfaces to stop preview and publishing stream.
//Stop preview
engine->stopPreview();
//Stop publishing stream
engine->stopPublishingStream();Audience co-hosting ends
When co-hosting ends, pass in the host's stream ID, and call the stopPlayingStream interface on the audience side to stop RTC stream playing, switch back to CDN stream playing, and set CDN parameters through ZegoViewMode. For details, please refer to Playing Stream by URL.
//Stop the current RTC stream playing
engine->stopPlayingStream(anchorStreamID);
//Switch to CDN stream playing
ZegoCanvas canvas(ZegoUtilHelper::GetView(ui_->frame_View3));
ZegoPlayerConfig config;
ZegoCDNConfig cdn_config;
cdn_config.url = "rtmp://xxxxxxxx"; // URL is the CDN stream playing address
cdn_config.authParam = "xxx"; // If authentication is required, set the authentication parameter; if authentication is not required, it can be not set (authentication parameter cannot contain "?" character)
engine_->startPlayingStream(anchorStreamID, &canvas, config);Host starts and stops stream playing
The host receives the audience stream addition notification through the onRoomStreamUpdate callback in IZegoEventHandler, and can start RTC stream playing. When receiving the audience stream deletion notification, stop RTC stream playing.
void onRoomStreamUpdate(const std::string &roomID, ZegoUpdateType updateType, const std::vector<ZegoStream> &streamList, const std::string& extendData) {
for_each(streamList.begin(), streamList.end(), [&](ZegoStream stream){
if(updateType == ZEGO_UPDATE_TYPE_ADD){
// RTC stream playing
ZegoCanvas canvas(ZegoUtilHelper::GetView(ui->frame_Play));
ZegoPlayerConfig config;
config.resourceMode = ZEGO_STREAM_RESOURCE_MODE_ONLY_RTC;
engine->startPlayingStream(stream.streamID, &canvas, config);
}
if(updateType == ZEGO_UPDATE_TYPE_DELETE){
engine->stopPlayingStream(stream.streamID);
}
});
}