The ZEGO Express SDK provides the ability to publish streams to CDN (Content Delivery Network), including forwarding streams to CDN and direct publishing streams to CDN.
This ability enables users to receive and play the stream from the CDN via a playback URL using a browser or a third-party player.
For security, CDN authentication is enabled by default when publishing streams to CDN.
Forwarding streams to the CDN refers to forwarding streams from ZEGO's real-time streaming cloud to the ZEGO's CDN or third-party CDNs for large-scale distribution.
There are three methods for forwarding streams to the CDN:
This refers to forward streams from ZEGO's real-time streaming cloud to the ZEGO's CDN. To use this, contact the ZEGO team.
You can specify which CDN the streams to be forwarded to, the ZEGO's CDN, or third-party CDNs.
You can also forward the mixed output streams to the ZEGO's CDN, or third-party CDNs. For details, see Stream mixing.
Direct publishing streams to the CDN refers to the process of publishing audio and video streams directly from the client application to the designated CDN. Users can then play the streams directly from the CDN via a playback URL using a browser or a third-party player. However, as the streams are not transmitted over ZEGO's real-time streaming cloud, users will not be able to get the ultra-low latency streaming experience that ZEGO can provide with its optimized real-time network. The ZEGO's CDN or third-party CDN are supported.
The following are the descriptions and scenarios of the two features:
Feature | Description | Scenario |
---|---|---|
Forwarding streams to the CDN (recommended) | The streams are first published to the ZEGO real-time streaming cloud, and then the ZEGO real-time streaming cloud forwards the streams to CDN. | You are working with a third-party CDN and want to use the existing third-party CDN streaming media distribution service while using the ZEGO Express SDK for real-time co-hosting interaction service. It is suitable for business scenarios requiring continuous interaction, such as private live streaming, voice chatroom, etc. |
Direct publishing streams to the CDN | The stream is published directly to the CDN without going through the ZEGO real-time streaming cloud. | You are working with a third-party CDN for streaming media distribution service, but don't need to use real-time co-hosting interaction service. It is suitable for Livestream shopping, Livestream gaming, and large classes. |
Before you begin, make sure you complete the following steps:
Skip this session if you are direct publishing streams to the CDN.
After you publish a stream to ZEGO's real-time streaming cloud successfully, to forward a stream to ZEGO's CDN or third-party CDN, call the addPublishCdnUrl
method to start forwarding the stream to the specified CDN URL. RTMP streams are supported.
If you need to forward a stream to multiple third-party CDNs:
Call the addPublishCdnUrl
method multiple times with the corresponding CDN URLs and the same stream ID. (Each URL needs to be different.)
Accordingly, you need to call the removePublishCdnUrl
method multiple times to stop the stream forwarding for different CDN URLs.
You can obtain the status update of the stream forwarding to each URL from the infoList
parameter of the onPublisherRelayCDNStateUpdate
callback.
/** Start forwarding streams to the CDN after the stream publishing is successful. */
// The stream ID you used for stream publishing.
NSString *streamID = @"STREAM_ID";
// Starts forwarding a stream to the specified CDN URL, fill in the URL based on the actual situation.
NSString *URL = @"rtmp://xxxxxxxx";
[self.engine addPublishCDNURL:URL stream:streamID callback:^(int errorCode) {
// Obtain the notification that whether the forwarding operation is successful.
if(errorCode == 0) {
// Forwarding successful.
} else {
// Failed to forward, bacause the request is not sent due to network problems.
}
}];
To listen for the stream forwarding status change notifications, refer to the Streaming info monitoring.
To stop forwarding the stream to the specified CDN URL, call the removePublishCdnUrl
method.
Calling this method does not stop the streams from being published to the ZEGO real-time streaming cloud.
// The stream ID you used for stream publishing.
NSString *streamID = @"STREAM_ID";
// Stops forwarding a stream to the specified CDN URL, fill in the URL based on the actual situation.
NSString *URL = @"rtmp://xxxxxxxx";
[self.engine removePublishCDNURL:URL stream:streamID callback:^(int errorCode) {
// Obtain the notification that whether the stop forwarding operation is successful.
if(errorCode == 0) {
// Streams forwarding stopped successfully.
} else {
// Failed to stop forwarding streams to CDN, possibly because the request is not sent due to network error.
}
}];
Skip this session if you are forwarding streams to the CDN.
To direct publish streams to the CDN, call the enablePublishDirectToCDN
method.
You can not forward the streams to the CDN by calling these two methods (addPublishCdnUrl
and removePublishCdnUrl
) after you enabled the direct publishing to CDN feature (enablePublishDirectToCDN
).
ZegoCDNConfig *config = [[ZegoCDNConfig alloc] init];
// Fill in the URL based on the actual situation.
config.URL = @"rtmp://xxxxxxxx";
[self.engine enablePublishDirectToCDN:YES config:config];
[self.engine startPublishing:@"STREAM_ID"];
To stop direct publishing streams to CDN, call the stopPublishingStream
method.
After stream publishing to CDN stops, if you want to disable the direct publishing to CDN feature for the next time, do the following:
enablePublishDirectToCDN
method.false
value into the method.[[ZegoExpressEngine sharedEngine] stopPublishingStream];
ZegoCDNConfig *config = [[ZegoCDNConfig alloc] init];
[[ZegoExpressEngine sharedEngine] enablePublishDirectToCDN:NO config:config];