Direct to CDN
Function Overview
Direct to CDN (Content Delivery Network) refers to the process of pushing audio and video streams directly from a local client to a CDN. Users can watch directly from a web page or a third-party player through a playing stream URL address. However, since the direct to CDN function does not pass through the ZEGOCLOUD real-time audio and video cloud during network transmission, developers cannot use ZEGOCLOUD's ultra-low latency audio and video service.
This function is generally used by developers who have audio and video distribution service cooperation with third-party CDNs.
Example Source Code Download
Please refer to Download Example Source Code to obtain the source code.
For related source code, please view the files in the "/ZegoExpressExample/src/CDNAbout" directory.
Prerequisites
Before implementing the direct to CDN function, please ensure:
- The ZEGO Express SDK has been integrated into the project and basic real-time voice functions have been implemented. For details, please refer to Quick Start - Integration and Quick Start - Implementation Process.
- A project has been created in the ZEGOCLOUD Console, and a valid AppID and AppSign have been applied for.
Usage Steps
Initialization and Logging In to Room
Please refer to "Creating Engine" and "Logging In to Room" in Quick Start - Implementation Process.
Set Up SDK Direct to CDN
The direct to CDN function must be set "before publishing stream".
-
Interface prototype
/** * Whether to publish stream directly to CDN without passing through the ZEGO real-time video cloud server, supporting setting up other publishing streams * * This interface needs to be set before publishing stream. * After calling this interface to directly push audio and video streams to the CDN, calling [addPublishCdnUrl] and [removePublishCdnUrl] to dynamically relay to the CDN will no longer take effect, because these two interfaces relay or stop relaying audio and video streams to the CDN from the ZEGO real-time video cloud. If the audio and video stream is directly pushed to the CDN, it cannot be dynamically relayed to the CDN through the ZEGO real-time video cloud. * * @param enable Whether to enable direct to CDN; true indicates enabling direct to CDN; false indicates not enabling direct to CDN; the default is false * @param config CDN configuration; if it is nullptr, the ZEGO backend configuration will be used */ virtual void enablePublishDirectToCDN(bool enable, ZegoCDNConfig* config); -
Usage example
ZegoCDNConfig config; // The URL needs to be filled in by the developer according to the actual situation config.URL = "rtmp://xxxxxxxx"; engine->enablePublishDirectToCDN(true, &config); engine->startPublishing("STREAM_ID")
Start Publishing Stream
Please refer to "Publishing Stream" in Quick Start - Implementation Process.
Listen to Publishing Status Notifications
-
Interface prototype
/** * Publishing state callback * * After the stream is successfully published, notifications of publishing state changes can be obtained through this callback interface. * Developers can roughly judge the user's publishing network situation based on whether the state parameter is in [requesting publishing state]. * * @param streamID The stream ID of the published stream * @param state The publishing state * @param errorCode The error code corresponding to the publishing state change. Please refer to the common error code document [https://www.zegocloud.com/docs/4380.html] * @param extendedData Extended information. If using ZEGO's CDN content delivery network, after the stream is successfully published, the keys of the content of this parameter are flv_url_list, rtmp_url_list, hls_url_list. These correspond to the playing URLs for the flv, rtmp, and hls protocols. If not using ZEGO's CDN, this parameter does not need to be concerned with. */ virtual void onPublisherStateUpdate(const std::string& /*streamID*/, ZegoPublisherState /*state*/, int /*errorCode*/, const std::string& /*extendedData*/); -
Usage example
class MyEventHandler:public IZegoEventHandler{ public: void onPublisherRelayCDNStateUpdate(const std::string& /*streamID*/, const std::vector<ZegoStreamRelayCDNInfo>& /*streamInfoList*/) { /** After the dynamic relaying interface is called successfully, when the CDN connection state changes, such as network interruption causing relaying abnormalities, etc., the SDK will notify through this callback while retrying relaying */ } }; auto handler = std::make_shared<MyEventHandler>(); engine->setEventHandler(handler);
Audience Playing Stream
- When developers use ZEGO's configured CDN for direct publishing, they can play streams directly through streamID. Please refer to "Playing Stream" in Quick Start - Implementation Process.
- When developers use a third-party CDN for direct publishing, they can play streams through URL. Please refer to Publishing and Playing Stream Advanced - Playing Stream by URL.
API Reference List
| Method | Description |
|---|---|
| enablePublishDirectToCDN | Enable direct to CDN |
| startPublishingStream | Start publishing stream |
| stopPublishingStream | Stop publishing stream |
