Using CDN for Live Streaming
Feature Overview
ZEGO Express SDK supports publishing streams to CDN (Content Delivery Network), including two features: relay to CDN and direct publish to CDN. Based on this functionality, developers can connect RTC products with CDN live streaming products, making it convenient for users to directly watch and listen to live streaming content from web pages or third-party players. To ensure security, CDN authentication is enabled by default when publishing streams to CDN.
To prevent attackers from stealing or forging your publishing URL address, you can refer to CDN Stream Publishing Authentication to improve the security of your publishing stream usage.
When initiating relay to CDN or direct publish to CDN, please note that CDN has requirements for audio/video formats. The publishing side supports AAC and MP3 for audio, and H.264 and H.265 (requires CDN configuration) for video.
Relay to CDN
Relay to CDN refers to the process of pushing audio/video streams from the ZEGO audio/video cloud to ZEGO's own CDN or third-party CDNs.

Relay to CDN includes the following three methods:
| Relay Method | Description |
|---|---|
| Default relay to CDN | All live streams published by users to the ZEGO audio/video cloud using ZEGO Express SDK will be relayed to CDN. Currently, only ZEGO's own CDN is supported. |
| Bypass relay to CDN | Developers can customize and specify streams on the ZEGO real-time audio/video cloud to be relayed to CDN, supporting ZEGO's own CDN and third-party CDNs. |
| Mix stream relay to CDN | In mix stream scenarios, the output stream can also be relayed to CDN, supporting ZEGO's own CDN and third-party CDNs. |
Direct Publish to CDN
Direct publish to CDN refers to the process of pushing audio/video streams directly from the local client to the CDN, where users can directly watch from web pages or third-party players through the playing stream URL address. However, since the direct publish to CDN function does not go through the ZEGO real-time audio/video cloud during network transmission, developers cannot use ZEGO's real-time audio/video services.
Feature Comparison
The descriptions and usage scenarios of the two functions are as follows:
| Function | Description | Usage Scenario |
|---|---|---|
| Relay to CDN (Recommended) | The publishing stream first goes through the ZEGO real-time audio/video cloud, and then is relayed to CDN by the ZEGO real-time audio/video cloud. You can use the real-time audio/video services provided by ZEGO, and it can be used for scenarios that require co-hosting interaction. | Developers have business cooperation with third-party CDNs and want to use the original third-party CDN streaming media network's distribution service while also using ZEGO Express SDK for real-time co-hosting interaction. It is suitable for business scenarios with co-hosting interaction requirements, such as show live streaming, audio chat rooms, etc. |
| Direct publish to CDN | The publishing stream does not go through the ZEGO real-time audio/video cloud and is pushed directly to the CDN. You cannot use the real-time audio/video services provided by ZEGO, and it is suitable for single live streaming scenarios (no interaction). | Suitable for situations where developers do not need to use real-time co-hosting interaction and other services, such as e-commerce live streaming, game live streaming, large classes, etc. |
Prerequisites
Before using CDN for live streaming, ensure that:
- You have created a project in the ZEGOCLOUD Console and applied for a valid AppID and AppSign. For details, see Console - Project Information.
- You have integrated the ZEGO Express SDK into your project and implemented basic audio/video publishing and playing functionality. For details, see Quick Start - Integration and Quick Start - Implementation Flow.
CDN live streaming functionality is not enabled by default. Please enable it yourself in the ZEGOCLOUD Console before use (for enabling steps, please refer to "CDN" in Project Management - Service Configuration), or contact ZEGO technical support to enable it.
Example Source Code Download
Please refer to Download Example Source Code to get the source code.
For related source code, please view files in the "/ZegoExpressExample/Examples/AdvancedStreaming/StreamByCDN" directory.
Relay to CDN
If you choose to use the direct publish to CDN function, you do not need to perform all steps in this section. Please refer to Direct Publish to CDN.
1 Initialize and Log In to Room
Please refer to "Create Engine" and "Log In to Room" in Quick Start - Implementation Flow.
2 Start Publishing Stream
Please refer to "Publishing Stream" in Quick Start - Implementation Flow.
3 Start Relaying
After the stream is successfully published, call the addPublishCdnUrl interface to add a URL for dynamic relaying to CDN, so that the audio/video stream that has been successfully published to the ZEGO real-time cloud can be dynamically relayed to third-party CDNs. The supported relay address format is "rtmp".
- If developers need to relay to multiple third-party CDN vendors, they can use the same stream ID to call the addPublishCdnUrl interface multiple times (URLs need to be different).
- After developers relay to multiple third-party CDNs, they also need to call multiple times to stop all relayed streams when stopping relaying.
- After developers relay to multiple third-party CDNs, they can obtain status change notifications for each relayed stream from the list parameter of the CDN callback status notification onPublisherRelayCDNStateUpdate.
// After the stream is successfully published, start relaying to CDN
// Stream ID used when publishing
NSString *streamID = @"STREAM_ID";
// CDN address to relay, please fill in according to the actual URL, streamID is the stream name for publishing, can be customized
NSString *URL = @"rtmp://publish_domain/ingest_point/streamID";
[self.engine addPublishCDNURL:URL stream:streamID callback:^(int errorCode) {
// Get notification of whether relaying to CDN was successful
if(errorCode == 0) {
// Relay successful
} else {
// Relay failed, possibly due to network reasons causing the relay request to fail
}
}];4 (Optional) Listen to CDN Relay Status Notification
Add/Remove Relay CDN Address Status Callback
Developers can obtain the add/remove relay CDN address status callback by registering onPublisherRelayCDNStateUpdate. After the ZEGO RTC server relays the audio/video stream to CDN, if the CDN relay status changes, such as relay stopping or relay retrying, this callback will be received.
Developers can determine whether the audio/video stream relayed to CDN is normal based on this callback:
- If not normal, further locate the cause of the abnormal audio/video stream relayed to CDN based on the abnormal reason, and perform corresponding disaster recovery strategies.
- If you do not understand the cause of the abnormality, please contact ZEGO technical support to analyze the specific cause of the abnormality.
- (void)onPublisherRelayCDNStateUpdate:(NSArray<ZegoStreamRelayCDNInfo *> *)infoList streamID:(NSString *)streamID {
}Relay CDN Information Details
Relay CDN information ZegoStreamRelayCDNInfo includes the URL of the CDN publishing stream, relay status, reason for relay status change, and time when the status occurred. All parameters in ZegoStreamRelayCDNInfo are as follows:
| Parameter Name | Description |
|---|---|
| url | URL of the CDN publishing stream. |
| state | Relay status. |
| updateReason | Reason for relay status change. |
| stateTime | Time when the status occurred. |
Among them, state values are as follows:
| Enumeration Value | Description |
|---|---|
| ZegoStreamRelayCDNStateNoRelay | Not relaying state, in this state before relaying. If a continuous exception occurs during the relaying process, such as an incorrect relay address, it will enter the not relaying state. |
| ZegoStreamRelayCDNStateRelayRequesting | Requesting relay state, after the relay operation is successfully executed, it will enter the requesting relay state. Usually, this state is used for application interface display. If an interruption occurs due to poor network quality, the SDK will perform internal retry and will also return to the relaying state. |
| ZegoStreamRelayCDNStateRelaying | Relaying state, entering this state indicates that relaying was successful. |
updateReason values are as follows:
| Enumeration Value | Description |
|---|---|
| ZegoStreamRelayCDNUpdateReasonNone | None. |
| ZegoStreamRelayCDNUpdateReasonServerError | Server error. |
| ZegoStreamRelayCDNUpdateReasonHandshakeFailed | Handshake failed. |
| ZegoStreamRelayCDNUpdateReasonAccessPointError | Access point error. |
| ZegoStreamRelayCDNUpdateReasonCreateStreamFailed | Failed to create stream. |
| ZegoStreamRelayCDNUpdateReasonBadName | Stream ID is invalid. |
| ZegoStreamRelayCDNUpdateReasonCDNServerDisconnected | CDN server actively disconnected. |
| ZegoStreamRelayCDNUpdateReasonDisconnected | Actively disconnected. |
| ZegoStreamRelayCDNUpdateReasonMixStreamAllInputStreamClosed | All input stream sessions of the mix stream are closed. |
| ZegoStreamRelayCDNUpdateReasonMixStreamAllInputStreamNoData | All input streams of the mix stream have no data. |
| ZegoStreamRelayCDNUpdateReasonMixStreamServerInternalError | Mix stream server internal error. |
5 Stop Relaying
Call removePublishCdnUrl to remove the URL for dynamic relaying to CDN. When calling the removePublishCdnUrl interface to stop relaying, please ensure that the current stream streamID exists.
This interface will not stop the audio/video stream published to the ZEGO real-time audio/video cloud.
// Stream ID used when publishing
NSString *streamID = @"STREAM_ID";
// CDN address to stop relaying, please fill in according to the actual URL, streamID is the stream name for publishing
NSString *URL = @"rtmp://publish_domain/ingest_point/streamID";
[self.engine removePublishCDNURL:URL stream:streamID callback:^(int errorCode) {
// Get notification of whether stopping relaying to CDN was successful
if(errorCode == 0) {
// Stop relaying successful
} else {
// Stop relaying failed, possibly due to network reasons causing the stop relaying request to fail
}
}];Direct Publish to CDN
If you choose to use the relay to CDN function, you do not need to perform all steps in this section. Please refer to Relay to CDN.
1 Start Direct Publish to CDN
Call the enablePublishDirectToCDN interface before publishing to push audio/video streams directly to CDN.
- After calling the enablePublishDirectToCDN interface, calling addPublishCdnUrl and removePublishCdnUrl to dynamically relay to CDN will no longer take effect, because these two interfaces relay or stop relaying audio/video streams to CDN from the ZEGO real-time audio/video cloud. If audio/video streams are pushed directly to CDN, they cannot be dynamically relayed to CDN again through the ZEGO real-time audio/video cloud.
- If calling the enablePublishDirectToCDN interface results in error code 1000038, possible problems include: domain name configuration error, media network exception, or media network link is empty. Please contact ZEGO technical support.
ZegoCDNConfig *config = [[ZegoCDNConfig alloc] init];
// URL needs to be filled in by the developer according to the actual situation, streamID is the stream name for publishing, can be customized
config.URL = @"rtmp://publish_domain/ingest_point/streamID";
[self.engine enablePublishDirectToCDN:YES config:config];
[self.engine startPublishing:@"STREAM_ID"];2 Stop Direct Publish to CDN
To stop direct publish to CDN, call the stopPublishingStream interface to stop publishing.
After stopping publishing, if the next publishing does not need direct publish to CDN, you can call enablePublishDirectToCDN and pass the value "false" to disable the direct publish to CDN function. Calling this interface during publishing will not affect this publishing.
[[ZegoExpressEngine sharedEngine] stopPublishingStream];
ZegoCDNConfig *config = [[ZegoCDNConfig alloc] init];
[[ZegoExpressEngine sharedEngine] enablePublishDirectToCDN:NO config:config];Audience Playing Stream
-
When the publishing side publishes directly to CDN, the playing side can play the stream directly through streamID. Please refer to "Playing Stream" in Quick Start - Implementation Flow.
-
After the audio/video stream is successfully relayed to CDN, when developers want users to play from CDN, they need to use the custom playing method with URL passed in, rather than playing through stream ID. For URL playing operation steps, please refer to "1 Configure Playing Parameters" and "2 Start Playing Stream" in Playing Stream by URL.
