Direct to CDN
Feature Introduction
Direct to CDN (Content Delivery Network) refers to the process of pushing audio and video streams directly from the local client to the CDN. Users can watch directly through the playback stream URL address on a webpage or third-party player. However, since the direct to CDN function does not pass through the ZEGOCLOUD during network transmission, developers cannot use ZEGO's ultra-low latency audio and video services.
This feature 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 get the source code.
For related source code, please check the files in the "/ZegoExpressExample/CustomCdnPublish" directory.
Prerequisites
Before implementing the direct to CDN feature, please ensure:
- ZEGO Express SDK has been integrated in the project to implement basic Voice Call functions. For details, please refer to Quick Start - Integration and Quick Start - Implementation.
- A project has been created on ZEGOCLOUD Console, and a valid AppID and AppSign have been applied for.
Usage Steps
1 Initialization and Login Room
Please refer to Quick Start - Implementation for "Create Engine" and "Login Room".
2 Set SDK Direct to CDN
The direct to CDN feature must be set before "stream publishing".
-
API Prototype
/** * Whether to publish stream directly to CDN without passing through Zego real-time video cloud server * * This interface needs to be set before stream publishing. * After calling this interface to push audio and video streams directly 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 from the ZEGOCLOUD to the CDN. If audio and video streams are directly pushed to the CDN, they cannot be dynamically relayed to the CDN through the ZEGOCLOUD. * @param enable Whether to enable direct to CDN; true means enable direct to CDN; false means disable direct to CDN; default is false * @param config CDN configuration, if null, use Zego's background configuration */ public void enablePublishDirectToCDN(boolean enable, @Nullable ZegoCDNConfig config) -
Usage Example
ZegoCDNConfig config = new ZegoCDNConfig(); // URL needs to be filled in by the developer according to the actual situation config.url = "rtmp://xxxxxxxx"; engine.enablePublishDirectToCDN(true, config); engine.startPublishingStream("STREAM_ID");
3 Start Publishing Stream
Please refer to Quick Start - Implementation for "Publish Stream".
4 Listen for Publish Stream Status Notification
-
API Prototype
/** * Publish stream status callback * * After the stream is successfully published, you can get notifications of publish stream status changes through this callback interface. * Developers can roughly judge the user's publish stream network situation based on whether the state parameter is in [Requesting publish stream status]. * @param streamID Stream ID of the published stream * @param state Publish stream status * @param errorCode Error code corresponding to the publish stream status change. Please refer to the common error codes document [https://www.zegocloud.com/docs/4378.html] * @param extendedData Extended information. If using ZEGO's CDN content distribution 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 playback URLs of flv, rtmp, and hls protocols. If not using ZEGO's CDN, there is no need to pay attention to this parameter. */ public void onPublisherStateUpdate(String streamID, ZegoPublisherState state, int errorCode, JSONObject extendedData) -
Usage Example
engine.setEventHandler(new IZegoEventHandler(){ // Other overridden callbacks ... @Override public void onPublisherStateUpdate(String streamID, ZegoPublisherState state, int errorCode, JSONObject extendedData){ // Direct to CDN can also use this callback notification to listen for stream publishing results ... } // Other overridden callbacks ... });
5 Audience Play Stream
- When developers use ZEGO-configured CDN for direct publishing, they can play streams directly through streamID. Please refer to Quick Start - Implementation for "Play Stream".
- When developers use third-party CDN for direct publishing, they can play streams through URL. Please refer to Publish and Play Stream Advanced - Play Stream by URL.
API Reference List
| Method | Description |
|---|---|
| enablePublishDirectToCDN | Enable direct to CDN |
| startPublishingStream | Start publishing stream |
| stopPublishingStream | Stop publishing stream |
