logo
Video Call
On this page

Using CDN for Live Streaming

2024-01-02

Feature Introduction

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 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 addresses, you can refer to CDN Stream Publishing Authentication to improve the security of your stream publishing usage.

Warning

When initiating relay to CDN or direct publishing to CDN, please note that CDN has requirements for audio and video formats. The publishing end 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 ZEGOCLOUD to ZEGO's own CDN or third-party CDN.

Relaying to CDN includes the following three methods:

Relay MethodDescription
Default Relay to CDNAll live streams published by users to ZEGOCLOUD using ZEGO Express SDK will be relayed to CDN, currently only supports ZEGO's own CDN.
Bypass Relay to CDNDevelopers customize and specify streams on ZEGOCLOUD to be relayed to CDN, supporting ZEGO's own CDN and third-party CDN.
Stream Mixing Relay to CDNIn stream 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 by playing stream URL addresses. However, because the direct publishing to CDN function does not pass 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:

FeatureDescriptionUsage Scenarios
Relaying to CDN (Recommended)Publishing stream first goes through ZEGOCLOUD, then is relayed to CDN by ZEGOCLOUD. Can use ZEGO's real-time audio and video services, suitable for scenarios requiring co-hosting interaction.Developers have business cooperation with third-party CDNs and want to use their 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 talent show live streaming, audio chat rooms, etc.
Direct Publishing to CDNPublishing stream does not pass through ZEGOCLOUD, directly pushes to CDN. Cannot use ZEGO's real-time audio and video services, 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, gaming live streaming, large classes, etc.

Prerequisites

Before using CDN live streaming, ensure that:

Warning

The CDN live streaming function 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 relevant source code, please see files in the "/ZegoExpressExample/Examples/AdvancedStreaming/StreamByCDN" directory.

Relaying to CDN

Note

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

Initialize and Login to Room

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

Start Publishing Stream

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

Start Relaying

After publishing stream successfully, call the addPublishCdnUrl interface to add dynamically relaying to CDN URLs, 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".

Note
  • If developers have the 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, when stopping relaying, they also need to call multiple times to stop all relayed streams.
  • After developers relay to multiple third-party CDNs, they can obtain state change notifications for each relayed stream from the list parameter of the CDN callback state notification onPublisherRelayCDNStateUpdate.
// After publishing stream successfully, start relaying to CDN

// Stream ID used when publishing stream
std::string streamID = "STREAM_ID";
// CDN address to be relayed, please fill in according to the actual URL, streamID is the stream name for publishing, can be customized
std::string URL = "rtmp://domain_name/ingest_point/streamID";
engine->addPublishCdnUrl(streamID, URL, [=](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 Callback State Notifications

If you need to listen to CDN callback state notifications, please refer to Monitor CDN Relay Status.

Stop Relaying

Call removePublishCdnUrl to delete the URL dynamically relayed 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 ZEGOCLOUD.

// Stream ID used when publishing stream
std::string streamID = "STREAM_ID";
// CDN address to stop relaying, please fill in according to the actual URL, streamID is the stream name for publishing
std::string URL = "rtmp://domain_name/ingest_point/streamID";
engine->removePublishCdnUrl(streamID, URL, [=](int errorCode){
    if(errorCode == 0)
    {
        // Stop relaying successful
    } else
    {
        // Stop relaying failed, possibly due to network reasons causing stop relaying request sending to fail
    }
});

Direct Publishing to CDN

Note

If you choose to use relaying to CDN function, you do not need to perform all 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 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 audio and video streams are published directly to CDN, they cannot be dynamically relayed to CDN through ZEGOCLOUD again.
  • If calling the enablePublishDirectToCDN interface results in error code 1000038, possible problems are: domain configuration error, media network exception, or media network link is empty, please contact ZEGO Technical Support.
ZegoCDNConfig config;
// URL needs to be filled in by developers according to actual situation, streamID is the stream name for publishing, can be customized
config.url = "rtmp://domain_name/ingest_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 "false" to disable the direct publishing to CDN function. Calling this interface during publishing stream will not affect the current publishing stream.

engine->stopPublishingStream();
ZegoCDNConfig cdnConfig;
engine->enablePublishDirectToCDN(false, &cdnConfig);

Audience Playing Stream

  • When the publishing end directly publishes to CDN, the playing end can directly play streams through streamID, please refer to "Playing Stream" in Quick Start - Implementation.

  • 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 playing method of passing in URL to play streams, and cannot play streams through stream ID. For URL playing operation steps, please refer to "1 Configure Stream Playing Parameters" and "2 Start Playing Stream" in Playing Stream by URL.

2024-01-02

Previous

Stream Mixing

Next

CDN Stream Publishing Authentication