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

Relaying to CDN includes the following three ways:
| Relay Method | Description |
|---|---|
| Default relay to CDN | All live streams published by users using ZEGO Express SDK to ZEGO audio and video cloud will be relayed to CDN, currently only supporting ZEGO's own CDN. |
| Bypass relay to CDN | Developers customize and specify streams on ZEGOCLOUD to be relayed to CDN, supporting ZEGO's own CDN and third-party CDN. |
| Mixing relay to CDN | In mixing scenarios, output streams can also be relayed to CDN, supporting ZEGO's own CDN and third-party CDN. |
Direct Publishing to CDN
Direct publishing to CDN refers to the process of pushing audio and video streams directly from the local client to CDN. Users can directly watch from web pages or third-party players through the playing stream URL address. However, because the direct publishing to CDN function does not go through ZEGOCLOUD during network transmission, developers cannot use ZEGO's real-time audio and video services.
Function Comparison
The descriptions and usage scenarios of the two functions are as follows:
| Function | Description | Usage Scenarios |
|---|---|---|
| Relaying to CDN (Recommended) | The publishing stream first goes through ZEGOCLOUD, and then is relayed to CDN by ZEGOCLOUD. You can use the real-time audio and video services provided by ZEGO, which can be used in scenarios that require interactive co-hosting. | Developers have business cooperation with third-party CDN and want to use the original third-party CDN streaming media network distribution service while also using ZEGO Express SDK for real-time interactive co-hosting. Suitable for business scenarios with interactive co-hosting requirements, such as show live streaming, audio chat rooms, etc. |
| Direct Publishing to CDN | The publishing stream does not go through ZEGOCLOUD and is pushed directly to CDN. ZEGO's real-time audio and video services cannot be used, suitable for single live streaming scenarios (no interaction required). | Suitable for situations where developers do not need to use real-time interactive co-hosting and other services, such as e-commerce live streaming, game live streaming, large classes, etc. |
Prerequisites
Before using CDN live streaming, please ensure:
- You have created a project in ZEGOCLOUD Console and applied for valid AppID and AppSign. For details, please refer to Console - Project Information.
- You have integrated ZEGO Express SDK in the project and implemented basic audio and video publishing and playing functionality. For details, please refer to Quick Start - Integration and Quick Start - Implementation Flow.
CDN live streaming function is not enabled by default. Please enable it by yourself in ZEGOCLOUD Console before use (for activation steps, please refer to "CDN" in Project Management - Service Configuration), or contact ZEGO Technical Support to enable it.
Download Example Source Code
Please refer to Download Example Source Code to get the source code.
For related source code, please view files in the "Assets/ZegoExpressExample/Examples/AdvancedStreaming/StreamByCDN" directory.
Relaying to CDN
If you choose to use the direct publishing to CDN function, you do not need to perform all the steps in this section. Please refer to Direct Publishing to CDN.
Initialize and login room
Please refer to "Create Engine" and "Login Room" in Quick Start - Implementation Flow.
Start publishing stream
Please refer to "Publishing Stream" in Quick Start - Implementation Flow.
Start relaying
After the publishing stream is successful, call the AddPublishCdnUrl interface to add the URL for dynamic relaying to CDN, which can dynamically relay the audio and video stream that has been successfully published to ZEGO real-time cloud to a third-party CDN. The supported relay address format is "rtmp".
- If developers have the need to relay to multiple third-party CDN providers, they can call the AddPublishCdnUrl interface multiple times using the same stream ID (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. When running on WebGL platform, this callback status notification is not supported.
// After publishing stream is successful, start relaying to CDN
// Stream ID used when publishing stream
string streamID = "STREAM_ID";
// CDN address that needs to be relayed, please fill in according to the actual URL, streamID is the stream name of the publishing stream, can be customized
string URL = "rtmp://<Push domain name>/<Access point>/streamID";
engine.AddPublishCdnUrl(streamID, URL, (int errorCode)=>{
if(errorCode == 0)
{
// Relay successful
} else
{
// Relay failed, possibly due to network reasons causing the relay request to fail to send
}
});(Optional) Listen to CDN callback status notifications
Add/Remove relay CDN address status callback
Developers can obtain add/remove relay CDN address status callbacks by setting the OnPublisherRelayCDNStateUpdate delegate. After ZEGO RTC server relays the audio and video stream to CDN, if the CDN relay status changes, such as relay stopping or relay retrying, this callback will be received.
When running on WebGL platform, this callback status notification is not supported.
Developers can determine whether the audio and video stream relayed to CDN is normal based on this callback:
- If not normal, further locate the cause of the audio and video stream exception relayed to CDN based on the exception reason, and implement corresponding disaster recovery strategies.
- If you do not understand the cause of the exception, you can contact ZEGO technical personnel to analyze the specific cause of the exception.
public delegate void OnPublisherRelayCDNStateUpdate(string streamID, List<ZegoStreamRelayCDNInfo> infoList);
engine.OnPublisherRelayCDNStateUpdate = (string streamID, List<ZegoStreamRelayCDNInfo> infoList)=>{
};Relay CDN information details
Relay CDN information ZegoStreamRelayCDNInfo includes the URL of CDN publishing stream, relay status, reason for relay status change, and time when the status occurred. All parameters in ZegoStreamRelayCDNInfo are as follows:
When running on WebGL platform, there is no relay CDN information ZegoStreamRelayCDNInfo.
| Parameter Name | Description |
|---|---|
| url | URL of 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:
| Enum Value | Description |
|---|---|
| ZegoStreamRelayCDNState.NoRelay | No relay status, in this state before relaying. If the relay process continues to have exceptions, such as incorrect relay address, it will enter the no relay state. |
| ZegoStreamRelayCDNState.RelayRequesting | Requesting relay status. After the relay operation is successfully executed, it will enter the requesting relay status, usually this state is used for application interface display. If an interruption occurs due to poor network quality, the SDK will internally retry and will also return to the relaying status. |
| ZegoStreamRelayCDNState.Relaying | Relaying status, entering this state indicates that the relay was successful. |
updateReason values are as follows:
| Enum Value | Description |
|---|---|
| ZegoStreamRelayCDNUpdateReason.None | None. |
| ZegoStreamRelayCDNUpdateReason.ServerError | Server error. |
| ZegoStreamRelayCDNUpdateReason.HandshakeFailed | Handshake failed |
| ZegoStreamRelayCDNUpdateReason.AccessPointError | Access point error. |
| ZegoStreamRelayCDNUpdateReason.CreateStreamFailed | Create stream failed. |
| ZegoStreamRelayCDNUpdateReason.BadName | Stream ID is illegal. |
| ZegoStreamRelayCDNUpdateReason.CDNServerDisconnected | CDN server actively disconnected. |
| ZegoStreamRelayCDNUpdateReason.Disconnected | Actively disconnected. |
| ZegoStreamRelayCDNUpdateReason.MixStreamAllInputStreamClosed | All input stream sessions of mixing closed. |
| ZegoStreamRelayCDNUpdateReason.MixStreamAllInputStreamNoData | All input streams of mixing have no data. |
| ZegoStreamRelayCDNUpdateReason.MixStreamServerInternalError | Mixing server internal error. |
Stop relaying
Call RemovePublishCdnUrl to delete 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 and video stream published to ZEGOCLOUD.
// Stream ID used when publishing stream
string streamID = "STREAM_ID";
// CDN address that needs to stop relaying, please fill in according to the actual URL, streamID is the stream name of the publishing stream
string URL = "rtmp://<Push domain name>/<Access point>/streamID";
engine.RemovePublishCdnUrl(streamID, URL, (int errorCode)=>{
if(errorCode == 0)
{
// Stop relay successful
} else
{
// Stop relay failed, possibly due to network reasons causing the stop relay request to fail to send
}
});Direct Publishing to CDN
- Direct publishing to CDN is not supported when running on WebGL platform.
- If you choose to use the relaying to CDN function, you do not need to perform all the steps in this section. Please refer to Relaying to CDN.
Start direct publishing to CDN
Call the EnablePublishDirectToCDN interface before publishing stream to publish the audio and video stream 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 and video streams to CDN from ZEGOCLOUD. If the audio and video stream is published directly to CDN, it cannot be dynamically relayed to CDN through ZEGOCLOUD.
- If calling the EnablePublishDirectToCDN interface results in error code 1000038, possible problems include: incorrect domain configuration, media network exception, or empty media network link. Please contact ZEGO Technical Support.
ZegoCDNConfig config = new ZegoCDNConfig();
// URL needs to be filled in by developers according to actual situation, streamID is the stream name of the publishing stream, can be customized
config.url = "rtmp://<Push domain name>/<Access point>/streamID";
engine.EnablePublishDirectToCDN(true, config);
engine.StartPublishingStream("STREAM_ID");Stop direct publishing to CDN
To stop direct publishing to CDN, call the StopPublishingStream interface to stop publishing stream.
After stopping publishing stream, if the next publishing stream does not need direct publishing to CDN, you can call the EnablePublishDirectToCDN interface and pass the value "false" to turn off the direct publishing to CDN function. Calling this interface during publishing stream will not affect this publishing stream.
engine.StopPublishingStream();
ZegoCDNConfig cdnConfig = new ZegoCDNConfig();
engine.EnablePublishDirectToCDN(false, cdnConfig);Audience Playing Stream
-
When the publishing side directly publishes to CDN, the playing side can directly play stream through streamID. Please refer to "Playing Stream" in Quick Start - Implementation Flow.
-
After the audio and video stream is successfully relayed to CDN, when developers want users to play stream from CDN, they need to use the custom playing stream method with URL parameter, and cannot play stream through stream ID. For URL playing stream operation steps, please refer to "1 Configure playing stream parameters" and "2 Start playing stream" in Playing Stream by URL.
