Direct to CDN
Feature overview
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 play stream URL address from a web page or a 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.
Prerequisites
Before implementing the direct to CDN feature, ensure that:
- ZEGO Express SDK has been integrated into the project and basic Voice Call functions have been implemented. For details, please refer to Quick Start - Integration and Quick Start - Implementation.
- A project has been created in the ZEGOCLOUD Console and a valid AppID and AppSign have been applied.
Usage steps
Initialize and login room
Please refer to "Create Engine" and "Login Room" in Quick Start - Implementation.
Set SDK direct to CDN
The direct to CDN feature must be set before "publishing stream".
-
Interface prototype
/// Whether to publish stream directly to CDN without passing through ZEGO real-time video cloud server /// /// This interface needs to be set before publishing stream. /// After calling this interface to push audio and video streams directly to CDN, calling [addPublishCdnUrl] and [removePublishCdnUrl] to dynamically forward to CDN will no longer take effect, because these two interfaces are used to forward or stop forwarding audio and video streams to CDN from the ZEGOCLOUD. If the audio and video streams are pushed directly to CDN, they cannot be dynamically forwarded to CDN through the ZEGOCLOUD. /// /// @param enable Whether to enable direct to CDN; YES indicates enable direct to CDN; NO indicates disable direct to CDN; default is NO /// @param config CDN configuration. If nil, use Zego's background configuration - (void)enablePublishDirectToCDN:(BOOL)enable config:(nullable ZegoCDNConfig *)config; -
Usage example
ZegoCDNConfig *config = [[ZegoCDNConfig alloc] init]; // URL needs to be filled in by the developer according to the actual situation config.URL = @"rtmp://xxxxxxxx"; [self.engine enablePublishDirectToCDN:YES config:config]; [self.engine startPublishing:@"STREAM_ID"];
Start publishing stream
Please refer to "Publish stream" in Quick Start - Implementation.
Listen to publish stream status notification
-
Interface prototype
/// Publish stream state callback /// /// After the stream is successfully published, notifications of publish stream status changes can be obtained 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 state Publish stream state /// @param errorCode The error code corresponding to the publish stream status change. Please refer to the common error code document [https://www.zegocloud.com/article/4377.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 play stream URLs of flv, rtmp, and hls protocols. If not using ZEGO's CDN, you do not need to pay attention to this parameter. /// @param streamID Stream ID of the published stream - (void)onPublisherStateUpdate:(ZegoPublisherState)state errorCode:(int)errorCode extendedData:(nullable NSDictionary *)extendedData streamID:(NSString *)streamID; -
Usage example
@implementation xx - (void)onPublisherStateUpdate:(ZegoPublisherState)state errorCode:(int)errorCode extendedData:(nullable NSDictionary *)extendedData stream:(NSString *)streamID { /** After calling the publish stream interface successfully, when the publisher status changes, such as network interruption causing publish stream exceptions, etc., the SDK will notify through this callback while retrying to publish stream */ } @end [self.engine setEventHandler:self];
Audience play stream
- When developers use ZEGO-configured CDN for direct publishing, they can directly play streams through streamID. Please refer to "Play stream" in Quick Start - Implementation.
- 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 |
