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-to-CDN. Based on this feature, developers can connect RTC products and CDN live streaming products, making it convenient for users to directly watch and listen to live content 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 addresses, you can refer to CDN Stream Publishing Authentication to improve the security of your publishing usage.
When initiating relay-to-CDN or direct-to-CDN, please note that CDN has requirements for audio and video formats. The publishing end 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 and video streams from ZEGOCLOUD to ZEGO's own CDN or third-party CDN.

Relay-to-CDN includes the following three methods:
| Relay Method | Description |
|---|---|
| Default Relay-to-CDN | All live streams published by users using ZEGO Express SDK to ZEGOCLOUD will be relayed to CDN. Currently only supports ZEGO's own CDN. |
| Bypass Relay-to-CDN | Developers can customize and specify streams on ZEGOCLOUD to relay to CDN, supporting ZEGO's own CDN and third-party CDNs. |
| Mixing Relay-to-CDN | In mixing scenarios, output streams can also be relayed to CDN, supporting ZEGO's own CDN and third-party CDNs. |
Direct-to-CDN
Direct-to-CDN refers to the process of pushing audio and video streams directly from the local client to the CDN. Users can directly watch from web pages or third-party players by playing the stream URL address. However, since the direct-to-CDN function does not go through ZEGOCLOUD during network transmission, developers cannot use ZEGO's real-time audio and video services.
Feature Comparison
The descriptions and usage scenarios of the two functions are as follows:
| Feature | Description | Usage Scenarios |
|---|---|---|
| Relay-to-CDN (Recommended) | Publishing first goes through ZEGOCLOUD, then is relayed to CDN by ZEGOCLOUD. Can use real-time audio and video services provided by ZEGO, suitable for scenarios requiring co-hosting interaction. | Developers have business cooperation with third-party CDNs and want to use the original third-party CDN streaming media network distribution services while also using ZEGO Express SDK for real-time co-hosting interaction. Suitable for business scenarios with co-hosting interaction requirements, such as show live streaming, voice chat rooms, etc. |
| Direct-to-CDN | Publishing does not go through ZEGOCLOUD, directly pushes to CDN. Cannot use real-time audio and video services provided by ZEGO, suitable for single live streaming scenarios (no interaction). | Suitable for developers who 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, please ensure:
- You have created a project in the ZEGOCLOUD Console and applied for a valid AppID and AppSign. For details, please refer to Console - Project Information.
- You have integrated ZEGO Express SDK in your project and implemented basic audio and video publish/play stream functionality. For details, please refer to Quick Start - Integration and Quick Start - Implementation.
The CDN live streaming feature 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 check the files in the "/ZegoExpressExample/AdvancedStreaming/src/main/java/im/zego/advancedstreaming/streamByCdn" directory.
Relay-to-CDN
If you choose to use the direct-to-CDN feature, you do not need to perform all steps in this section. Please refer to Direct-to-CDN.
Initialize and Login to Room
Please refer to Quick Start - Implementation for "Create Engine" and "Login to Room".
Start Publishing Stream
Please refer to Quick Start - Implementation for "Publish Stream".
Start Relay-to-CDN
After successful publishing, call the addPublishCdnUrl interface to add a dynamically relayed CDN URL, which allows you to dynamically relay audio and video streams that have been successfully published to ZEGO real-time cloud to third-party CDNs. The supported relay address format is "rtmp".
- If developers 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 relay.
- 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 relay status callback onPublisherRelayCDNStateUpdate.
// After successful publishing, start relay to CDN
// Stream ID used when publishing
String streamID = "STREAM_ID";
// CDN address that needs to be relayed. Developers should fill in according to the actual URL. streamID is the stream name for publishing and can be customized
String URL = "rtmp://publishing domain name/access point/streamID";
engine.addPublishCdnUrl(streamID, URL, new IZegoPublisherUpdateCdnUrlCallback() {
@Override
public void onPublisherUpdateCdnUrlResult(int errorCode) {
if(errorCode == 0) {
// Relay successful
} else {
// Relay failed, possibly due to network reasons causing relay request sending to fail
}
}
});(Optional) Listen to CDN Relay Status Notifications
Add/Remove Relay CDN Address Status Callback
Developers can register onPublisherRelayCDNStateUpdate to get add/remove relay CDN address status callbacks. After ZEGO RTC server relays audio and video streams to CDN, if the CDN relay status changes, such as relay stopping or relay retrying, this callback will be received.
Developers can use this callback to determine whether the audio and video streams relayed to CDN are normal:
- If not normal, further locate the cause of the abnormal audio and video streams relayed to CDN based on the abnormal reason, and implement corresponding disaster recovery strategies.
- If you do not understand the cause of the abnormality, you can contact ZEGO Technical Support to analyze the specific cause of the abnormality.
engine.setEventHandler(new IZegoEventHandler() {
@Override
public void onPublisherRelayCDNStateUpdate(String streamID, ArrayList<ZegoStreamRelayCDNInfo> infoList) {
super.onPublisherRelayCDNStateUpdate(streamID, infoList);
}
}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 |
|---|---|
| ZEGO_STREAM_RELAY_CDN_STATE_NO_RELAY | No relay status, in this state before relay. If the relay process has continuous abnormalities, such as incorrect relay address, it will enter the no relay state. |
| ZEGO_STREAM_RELAY_CDN_STATE_RELAY_REQUESTING | Requesting relay status. After the relay operation is successfully executed, it will enter the requesting relay status. Usually this status is used for application interface display. If there is an interruption due to poor network quality, the SDK will internally retry and return to the requesting relay status. |
| ZEGO_STREAM_RELAY_CDN_STATE_RELAYING | Relaying status. Entering this status indicates that relay has succeeded. |
updateReason values are as follows:
| Enumeration Value | Description |
|---|---|
| ZEGO_STREAM_RELAY_CDN_UPDATE_REASON_NONE | None. |
| ZEGO_STREAM_RELAY_CDN_UPDATE_REASON_SERVER_ERROR | Server error. |
| ZEGO_STREAM_RELAY_CDN_UPDATE_REASON_HANDSHAKE_FAILED | Handshake failed. |
| ZEGO_STREAM_RELAY_CDN_UPDATE_REASON_ACCESS_POINT_ERROR | Access point error. |
| ZEGO_STREAM_RELAY_CDN_UPDATE_REASON_CREATE_STREAM_FAILED | Create stream failed. |
| ZEGO_STREAM_RELAY_CDN_UPDATE_REASON_BAD_NAME | Stream ID is invalid. |
| ZEGO_STREAM_RELAY_CDN_UPDATE_REASON_CDN_SERVER_DISCONNECTED | CDN server actively disconnected. |
| ZEGO_STREAM_RELAY_CDN_UPDATE_REASON_DISCONNECTED | Actively disconnected. |
| ZEGO_STREAM_RELAY_CDN_UPDATE_REASON_MIX_STREAM_ALL_INPUT_STREAM_CLOSED | All input stream sessions of the mixed stream are closed. |
| ZEGO_STREAM_RELAY_CDN_UPDATE_REASON_MIX_STREAM_ALL_INPUT_STREAM_NO_DATA | All input streams of the mixed stream have no data. |
| ZEGO_STREAM_RELAY_CDN_UPDATE_REASON_MIX_STREAM_SERVER_INTERNAL_ERROR | Mixing server internal error. |
Stop Relay
Call the removePublishCdnUrl interface to remove the dynamically relayed CDN URL. When calling the removePublishCdnUrl interface to stop relay, please ensure that the current stream streamID exists.
This interface will not stop the audio and video streams published to ZEGOCLOUD.
// Stream ID used when publishing
String streamID = "STREAM_ID";
// CDN address that needs to stop relay. Developers should fill in according to the actual URL. streamID is the stream name for publishing
String URL = "rtmp://publishing domain name/access point/streamID";
engine.removePublishCdnUrl(streamID, URL, new IZegoPublisherUpdateCdnUrlCallback() {
@Override
public void onPublisherUpdateCdnUrlResult(int errorCode) {
if(errorCode == 0) {
// Stop relay successful
} else {
// Stop relay failed, possibly due to network reasons causing stop relay request sending to fail
}
}
});Direct-to-CDN
If you choose to use the relay-to-CDN feature, you do not need to perform all steps in this section. Please refer to Relay-to-CDN.
Start Direct-to-CDN
Before publishing, call the enablePublishDirectToCDN interface to publish audio and 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 and video streams from ZEGOCLOUD to CDN. If audio and video streams are published directly to CDN, they cannot be dynamically relayed to CDN through ZEGOCLOUD.
- If calling the enablePublishDirectToCDN interface returns error code 1000038, possible problems include: domain name configuration error, media network exception, or media network connection is empty. Please contact ZEGO Technical Support.
ZegoCDNConfig config = new ZegoCDNConfig();
// URL needs to be filled in by developers according to actual conditions. streamID is the stream name for publishing and can be customized
config.url = "rtmp://publishing domain name/access point/streamID";
engine.enablePublishDirectToCDN(true, config);
engine.startPublishingStream("STREAM_ID");Stop Direct-to-CDN
To stop direct-to-CDN, call the stopPublishingStream interface to stop publishing.
After stopping publishing, if the next publishing does not need direct-to-CDN, you can call the enablePublishDirectToCDN interface and pass "false" to turn off the direct-to-CDN feature. Calling this interface during publishing will not affect the current publishing.
engine.stopPublishingStream();
ZegoCDNConfig config = new ZegoCDNConfig();
engine.enablePublishDirectToCDN(false, config);Audience Play Stream
-
When the publishing end publishes directly to CDN, the playing end can directly play the stream by streamID. Please refer to Quick Start - Implementation for "Play Stream".
-
After audio and video streams are successfully relayed to CDN, when developers want users to play streams from CDN, they need to use the custom play stream method with URL, and cannot play streams by stream ID. For URL play stream operation steps, please refer to "1 Configure Play Stream Parameters" and "2 Start Playing Stream" in Playing Stream by URL.
