logo
Video Call
Other Features
On this page

Using CDN for Live Streaming

2024-01-02

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

Warning

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

Relaying to CDN

Relaying to CDN refers to the process of pushing audio/video streams from ZEGO audio/video cloud 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 pushed by users to ZEGO audio/video cloud using ZEGO Express SDK will be relayed to CDN, currently only supporting ZEGO's own CDN.
Bypass Relay to CDNDevelopers can customize and specify streams on ZEGO real-time audio/video cloud to be relayed to CDN, supporting both ZEGO's own CDN and third-party CDN.
Mixed Stream Relay to CDNIn stream mixing scenarios, the output stream can also be relayed to CDN, supporting both ZEGO's own CDN and third-party CDN.

Direct Publishing to CDN

Direct publishing to CDN refers to the process of pushing audio/video streams directly from the local client to CDN. Users can directly watch from web pages or third-party players through the play stream URL address. However, since the direct publishing to CDN function does not go through ZEGO real-time audio/video cloud during network transmission, developers cannot use ZEGO's real-time audio/video services.

Function Comparison

The descriptions and usage scenarios of the two functions are as follows:

FunctionDescriptionUsage Scenario
Relaying to CDN (Recommended)The publishing stream first goes through ZEGO real-time audio/video cloud, then is relayed to CDN by ZEGO real-time audio/video cloud. You can use ZEGO's real-time audio/video services and can be used in scenarios that require mic-connected 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 mic-connected interaction. Suitable for business scenarios with mic-connected interaction requirements, such as showcase live streaming, audio chat rooms, etc.
Direct Publishing to CDNThe publishing stream does not go through ZEGO real-time audio/video cloud and is pushed directly to CDN. Unable to use ZEGO's real-time audio/video services, suitable for single live streaming scenarios (no interaction required).Suitable for situations where developers do not need to use real-time mic-connected 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 function is not enabled by default. Please enable it in the ZEGOCLOUD Console before using it (for activation steps, please refer to "CDN" in Project Management - Service Configuration), or contact ZEGO Technical Support to enable it.

Usage Steps

Relaying to CDN

Note

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

Initialize, Login Room, Start Publishing Stream

Please refer to "Create Engine", "Login Room", "Publish Stream" in Quick Start - Implementation Flow.

Start Relaying

After publishing stream successfully, call the addPublishCdnUrl interface to add the URL for dynamic relaying to CDN, so that the audio/video stream that has been successfully published to ZEGO real-time cloud can be dynamically relayed to third-party CDN.

Note
  • If developers need to relay to multiple third-party CDN vendors, they can call addPublishCdnUrl multiple times using the same stream ID (URLs need to be different).
  • If developers relay to multiple third-party CDNs, they also need to call multiple times to stop all relayed streams when stopping relaying.
  • If 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.
  • Call example:

    // After publishing stream successfully, start relaying to CDN
    
    // Stream ID used when publishing
    std::string streamID = "STREAM_ID";
    // CDN address to be relayed, please fill in according to the actual URL, streamID is the stream name of publishing, can be customized
    std::string URL = "rtmp://publish_domain/entry_point/streamID";
    
    engine->addPublishCdnUrl(streamID, URL, [](int errorCode) {
        if(errorCode == 0) {
            // Relaying successful
        } else {
            // Relaying failed, possibly due to network reasons causing the relaying request to fail
        }
    });

(Optional) Listen to CDN Callback Status Notifications

Developers can register onPublisherRelayCDNStateUpdate to get callbacks for adding/deleting relay to CDN URL status. After ZEGO RTC server relays the audio/video stream to CDN, if the CDN relay status changes, such as relay stopping or relay retry, this callback will be received.

Note

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

  • If abnormal, further locate the cause of the audio/video stream abnormality relayed to CDN based on the abnormal reason, and take corresponding disaster recovery strategies.
  • If the cause of the abnormality is not clear, you can contact ZEGO Technical Support to analyze the specific cause of the abnormality.
  • Call example:

    class MyEventHandler:public IZegoEventHandler{
    public:
        void onPublisherRelayCDNStateUpdate(const std::string& /*streamID*/, const std::vector<ZegoStreamRelayCDNInfo>& /*streamInfoList*/) {
            // After calling the dynamic relay interface successfully, when the CDN connection status changes, such as network interruption causing relay abnormalities, etc., the SDK will notify through this callback while retrying relaying
            // Please note, do not call any SDK interfaces in the SDK callback thread, you need to manually switch to another thread, otherwise a deadlock will occur
        }
    };
    auto handler=std::make_shared<MyEventHandler>();
    engine->setEventHandler(handler);

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 streamID exists.

  • Call example:

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

Direct Publishing to CDN

Note

If you choose to use the relaying to CDN function, you do not need to execute all steps in this section. Please refer to Relaying to CDN.

Start Direct Publishing to CDN

Before publishing stream, call the enablePublishDirectToCDN interface to push the audio/video stream 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/video streams to CDN from ZEGO real-time audio/video cloud. If the audio/video stream is pushed directly to CDN, it cannot be dynamically relayed to CDN through 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;
// URL needs to be filled in by the developer according to the actual situation, streamID is the stream name of publishing, can be customized
config.url = "rtmp://publish_domain/entry_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;
engine->enablePublishDirectToCDN(false, &cdnConfig);

Audience Playing Stream

  • When the publishing side directly publishes to CDN, the playing side can directly play streams through streamID. Please refer to "Play Stream" in Quick Start - Implementation Flow.

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

Previous

Small/Large Video Stream and Layered Encoding

Next

CDN Stream Publishing Authentication