Switching from CDN Playing Stream to Co-hosting Scenario
Feature Introduction
In co-hosting scenarios, since both CDN playing stream and CDN publishing stream have high latency, which will lead to poor interactive experience during the co-hosting process, it is recommended to use RTC playing stream and RTC publishing stream during the co-hosting process, and restore CDN playing stream after leaving the mic to improve the co-hosting experience. This article will introduce how to implement the co-hosting process under the complete CDN address playing stream method.
Comparison of Playing Stream Methods
| Playing Stream Method | Applicable Scenarios | Latency |
|---|---|---|
| Play stream from CDN | Viewers do not need to co-host with the anchor, interact with the anchor through barrage and other methods, and have low requirements for real-time viewing. | Latency exceeds 3 seconds |
| Play stream from RTC | Viewers need to co-host with the anchor, and have high requirements for real-time interaction and communication with the anchor. | Latency between co-hosting parties is below 300 milliseconds |
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
1 Viewer side switches playing stream method
When switching from CDN playing stream to co-hosting scenario, the viewer 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 anchor stream ID
[[ZegoExpressEngine sharedEngine] stopPlayingStream:@"anchorStreamID"];
//Switch to RTC playing stream
ZegoCanvas *playCanvas = [ZegoCanvas canvasWithView:self.remotePlayView];
ZegoPlayerConfig *playConfig = [[ZegoPlayerConfig alloc] init];
playConfig.resourceMode = ZegoStreamResourceModeOnlyRTC;
[[ZegoExpressEngine sharedEngine] startPlayingStream:self.playStreamIDTextField.text canvas:playCanvas config:playConfig];2 Viewer side starts preview and publishing stream
Pass in the viewer's stream ID, and start preview startPreview and publishing stream startPublishingStream, then the co-hosting is successful.
//Start preview
ZegoCanvas *previewCanvas = [ZegoCanvas canvasWithView:self.localPreviewView];
[[ZegoExpressEngine sharedEngine] startPreview:previewCanvas];
//Start publishing stream and pass in the viewer's stream ID
[[ZegoExpressEngine sharedEngine] startPublishingStream:@"pulishingStreamID"];3 Viewer side stops preview and publishing stream
If the co-hosting ends, you can call the stopPreview and stopPublishingStream interfaces on the viewer side to stop preview and publishing stream.
//Stop preview
[[ZegoExpressEngine sharedEngine] stopPreview];
//Stop publishing stream
[[ZegoExpressEngine sharedEngine] stopPublishingStream];4 Viewer side co-hosting ends
When the co-hosting ends, pass in the anchor's stream ID, and call the stopPlayingStream interface on the viewer 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
[[ZegoExpressEngine sharedEngine] stopPlayingStream:@"anchorStreamID"];
//Switch to CDN playing stream
ZegoCanvas *playCanvas = [ZegoCanvas canvasWithView:self.remotePlayView];
ZegoPlayerConfig *playerConfig = [[ZegoPlayerConfig alloc] init];
playerConfig.resourceMode = ZegoStreamResourceModeOnlyCDN;
ZegoCDNConfig *cdnConfig = [[ZegoCDNConfig alloc] init];
cdnConfig.url = "rtmp://xxxxxxxx"; // URL is the CDN playing stream address
cdnConfig.authParam = "xxx"; // If authentication is required, set authentication parameters. If authentication is not required, it can be not set (authentication parameters cannot contain "?" character)
playerConfig.cdnConfig = cdnConfig;
[[ZegoExpressEngine sharedEngine] startPlayingStream:self.streamID canvas:playCanvas config:playerConfig];5 Anchor side starts and stops playing stream
After the anchor side receives the viewer stream addition notification through the onRoomStreamUpdate callback in ZegoEventHandler, it can start RTC playing stream. When receiving the viewer stream deletion notification, it stops RTC playing stream.
- (void)onRoomStreamUpdate:(ZegoUpdateType)updateType streamList:(NSArray<ZegoStream *> *)streamList extendedData:(NSDictionary *)extendedData roomID:(NSString *)roomID {
if (updateType == ZegoUpdateTypeAdd) {
for (ZegoStream *stream in streamList) {
//Start RTC playing viewer stream
ZegoCanvas *playCanvas = [ZegoCanvas canvasWithView:self.remotePlayView];
ZegoPlayerConfig *playConfig = [[ZegoPlayerConfig alloc] init];
playConfig.resourceMode = ZegoStreamResourceModeOnlyRTC;
[[ZegoExpressEngine sharedEngine] startPlayingStream:stream.streamID canvas:playCanvas config:playConfig];
}
} else if (updateType == ZegoUpdateTypeDelete) {
for (ZegoStream *stream in streamList) {
//Stop RTC playing viewer stream
[[ZegoExpressEngine sharedEngine] stopPlayingStream:stream.streamID];
}
}
}