logo
On this page

Using CDN for Live Streaming

2026-03-05

Overview

Warning

CDN live streaming is not currently supported on Web.

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 functionality, 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 stream publishing usage.

Warning

When initiating relay or direct publishing to CDN, please note that CDN has requirements for audio and video formats. For publishing, audio supports AAC and MP3, and video supports H.264 and H.265 (requires CDN configuration).

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 CDNs.

Relaying to CDN includes the following three methods:

Relay MethodDescription
Default Relay to CDNAll live streams published by users using ZEGO Express SDK to ZEGO audio and video cloud will be relayed to CDN. Currently, only ZEGO's own CDN is supported.
Bypass Relay to CDNDevelopers can customize and specify streams on the ZEGOCLOUD to be relayed to CDN, supporting ZEGO's own CDN and third-party CDNs.
Stream Mixing Relay to CDNIn stream mixing scenarios, output streams can also be relayed to CDN, supporting ZEGO's own CDN and third-party CDNs.

Direct Publishing to CDN

Direct publishing to CDN refers to the process of pushing audio and video streams directly from the local client to the CDN. Users can watch directly from web pages or third-party players through the Play stream URL. However, since the direct CDN publishing function does not go through the 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:

FeatureDescriptionUsage Scenario
Relay to CDN (Recommended)Stream publishing first goes through ZEGOCLOUD, then is relayed to CDN by ZEGOCLOUD. You can use ZEGO's real-time audio and video services and can be used for scenarios that require interactive co-streaming.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-streaming interaction. Suitable for business scenarios with co-streaming interaction requirements, such as show live streaming, voice chat rooms, etc.
Direct Publishing to CDNStream publishing does not go through ZEGOCLOUD, but is pushed directly to CDN. Cannot use ZEGO's real-time audio and video services, suitable for single live streaming scenarios (no interaction required).Suitable for situations where developers do not need to use real-time co-streaming interaction and other services, such as e-commerce live streaming, gaming live streaming, large classes, etc.

Prerequisites

Before using CDN for live streaming, please ensure:

Warning

CDN live streaming is not enabled by default. Please enable it in the ZEGOCLOUD Console before using (for enabling steps, please refer to "CDN" in Project Management - Service Configuration), or contact ZEGOCLOUD Technical Support to enable it.

Demo Source Code Download

Please refer to Download Demo Source Code to obtain the source code.

For related source code, please check the files in the "lib/topics/StreamAdvanced/stream_by_cdn" directory.

Relaying to CDN

Note

If you choose to use the direct CDN publishing function, you do not need to perform all the steps in this section. Please refer to Direct Publishing to CDN.

Initialization and Login Room

Please refer to "Create Engine" and "Login Room" in Quick Start - Implementation.

Start Publishing Stream

Please refer to "Stream Publishing" in Quick Start - Implementation.

Start Relaying

After stream publishing is successful, call the addPublishCdnUrl interface to add the URL for dynamic relaying to CDN. You can dynamically relay the audio and video streams that have been successfully published to the ZEGO real-time cloud to third-party CDNs. The supported relay address format is "rtmp".

Note
  • If developers need to relay to multiple third-party CDN providers, 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 stream publishing is successful, start relaying to CDN

// Stream ID used when publishing
String streamID = "STREAM_ID";
// CDN address to be relayed, please fill in the actual URL, streamID is the stream name for publishing, can be customized
String URL = "rtmp://domain-name ingest-point/streamID";
ZegoExpressEngine.instance.addPublishCdnUrl(streamID, URL).then((ZegoPublisherUpdateCdnUrlResult result) {
    if(result.errorCode == 0) {
        // Relay successful
    } else {
        // Relay failed, possibly due to network reasons causing the relay request to fail
    }
});

(Optional) Listen for CDN Relay Status Notifications

Add/Remove Relay CDN Address Status Callback

Developers can register onPublisherRelayCDNStateUpdate to get the add/remove relay CDN address status callback. After the ZEGO RTC server relays the audio and video stream to the CDN, if the CDN relay status changes, such as relay stopping or relay retrying, this callback will be received.

Note

Developers can use this callback to determine whether the relayed CDN audio and video stream is normal:

  • If not normal, further locate the cause of the abnormal relayed CDN audio and video stream based on the abnormal reason, and implement corresponding disaster recovery strategies.
  • If the cause of the abnormality is not understood, please contact ZEGOCLOUD Technical Support to analyze the specific cause of the abnormality.
ZegoExpressEngine.onPublisherRelayCDNStateUpdate =
 (String streamID, List<ZegoStreamRelayCDNInfo> infoList) {
};

Relay CDN Information Details

Relay CDN information ZegoStreamRelayCDNInfo includes the URL of the CDN stream publishing, relay status, reason for relay status change, and time when the status occurred. All parameters in ZegoStreamRelayCDNInfo are as follows:

Parameter NameDescription
urlThe URL of the CDN stream publishing.
stateRelay status.
updateReasonReason for relay status change.
stateTimeTime when the status occurred.

Among them, the values for state are as follows:

Enumeration ValueDescription
ZegoStreamRelayCDNState.NoRelayNo relay status, in this state before relaying. If the relay process has continuous abnormalities, such as incorrect relay address, it will enter the no relay status.
ZegoStreamRelayCDNState.RelayRequestingRequesting 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 perform internal retry and will also return to the relaying status.
ZegoStreamRelayCDNState.RelayingRelaying status, entering this status indicates that the relay was successful.

The values for updateReason are as follows:

Enumeration ValueDescription
ZegoStreamRelayCDNUpdateReason.NoneNone.
ZegoStreamRelayCDNUpdateReason.ServerErrorServer error.
ZegoStreamRelayCDNUpdateReason.HandshakeFailedHandshake failed.
ZegoStreamRelayCDNUpdateReason.AccessPointErrorAccess point error.
ZegoStreamRelayCDNUpdateReason.CreateStreamFailedFailed to create stream.
ZegoStreamRelayCDNUpdateReason.BadNameStream ID is invalid.
ZegoStreamRelayCDNUpdateReason.CDNServerDisconnectedCDN server actively disconnected.
ZegoStreamRelayCDNUpdateReason.DisconnectedActively disconnected.
ZegoStreamRelayCDNUpdateReason.MixStreamAllInputStreamClosedAll input stream sessions for stream mixing are closed.
ZegoStreamRelayCDNUpdateReason.MixStreamAllInputStreamNoDataAll input streams for stream mixing have no data.
ZegoStreamRelayCDNUpdateReason.MixStreamServerInternalErrorStream mixing server internal error.

Stop Relaying

Call the removePublishCdnUrl interface 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.

Warning

This interface will not stop the audio and video streams published to the ZEGOCLOUD.


// Stream ID used when publishing
String streamID = "STREAM_ID";
// CDN address to stop relaying, please fill in the actual URL, streamID is the stream name for publishing
String URL = "rtmp://domain-name ingest-point/streamID";
ZegoExpressEngine.instance.removePublishCdnUrl(streamID, URL).then((ZegoPublisherUpdateCdnUrlResult result) {
    if(result.errorCode == 0) {
        // Stop relay successful
    } else {
        // Stop relay failed, possibly due to network reasons causing the stop relay request to fail
    }
});

Direct Publishing to CDN

Note

If you choose to use the relay 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

Before publishing stream, call the enablePublishDirectToCDN interface to publish audio and video streams directly to CDN.

Warning
  • After calling the enablePublishDirectToCDN interface, calling addPublishCdnUrl and removePublishCdnUrl for dynamic relaying 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 results in error code 1000038, possible problems include: incorrect domain configuration, media network exception, or empty media network link. Please contact ZEGOCLOUD Technical Support.
// URL needs to be filled in by the developer based on the actual situation, streamID is the stream name for publishing, can be customized
String url = "rtmp://domain-name ingest-point/streamID";
var config = new ZegoCDNConfig(url, "");

ZegoExpressEngine.instance.enablePublishDirectToCDN(true, config: config);
ZegoExpressEngine.instance.startPublishingStream("STREAM_ID");

Stop Direct Publishing to CDN

If you need to stop direct publishing to CDN, call the stopPublishingStream interface to stop publishing.

After stopping publishing, if the next publishing does not need direct CDN publishing, you can call the enablePublishDirectToCDN interface and pass the value "false" to turn off the direct CDN publishing function. Calling this interface during publishing will not affect this publishing.

ZegoExpressEngine.instance.stopPublishingStream();
// URL needs to be filled in by the developer based on the actual situation, streamID is the stream name for publishing, can be customized
String url = "rtmp://domain-name ingest-point/streamID";
var config = new ZegoCDNConfig(url, "");
ZegoExpressEngine.instance.enablePublishDirectToCDN(false, config: config);

Audience Playing Stream

  • When the publisher publishes directly to CDN, the playing side can play the stream directly through streamID. Please refer to "Playing Stream" in Quick Start - Implementation.

  • After the audio and video stream is successfully relayed to CDN, when developers want users to play from CDN, they need to use the custom URL playing method instead of playing through stream ID. For URL playing operation steps, please refer to "1 Configure Playing Parameters" and "2 Start Playing" in Playing Stream by URL.

Previous

AI Voice Changer

Next

CDN Publishing Authentication