Documentation
Low_Latency_Live Live Streaming
Documentation
Demo APP
SDK Center
API Center
FAQ
Code Market
Console
Sign Up
Log In
中文站 English
  • Documentation
  • Live Streaming
  • Develop your app
  • Live Streaming
  • CDN
  • Publishing streams via CDN

Publishing streams via CDN

Last updated:2024-01-29 23:17

Introduction

The ZEGO Express SDK provides the ability to publish streams to CDN (Content Delivery Network), including forwarding streams to CDN and direct publishing streams to CDN.

This ability enables users to receive and play the stream from the CDN via a playback URL using a browser or a third-party player.

For security, CDN authentication is enabled by default when publishing streams to CDN.

Forward a stream to the CDN

Forwarding streams to the CDN refers to forwarding streams from ZEGO's real-time streaming cloud to the ZEGO's CDN or third-party CDNs for large-scale distribution.

There are three methods for forwarding streams to the CDN:

  • Forwarding streams to the ZEGO's CDN (Default)

This refers to forward streams from ZEGO's real-time streaming cloud to the ZEGO's CDN. To use this, contact the ZEGO team.

  • Forwarding streams to the CDN

You can specify which CDN the streams to be forwarded to, the ZEGO's CDN, or third-party CDNs.

  • Forwarding mixed streams to the CDN

You can also forward the mixed output streams to the ZEGO's CDN, or third-party CDNs.

Direct publishing a stream to the CDN

Direct publishing streams to the CDN refers to the process of publishing audio and video streams directly from the client application to the designated CDN. Users can then play the streams directly from the CDN via a playback URL using a browser or a third-party player. However, as the streams are not transmitted over ZEGO's real-time streaming cloud, users will not be able to get the ultra-low latency streaming experience that ZEGO can provide with its optimized real-time network. The ZEGO's CDN or third-party CDN are supported.

/Pics/Common/ZegoExpressEngine/direct_publishing_en.png

Feature comparison

The following are the descriptions and scenarios of the two features:

Feature Description Scenario
Forwarding streams to the CDN (recommended) The streams are first published to the ZEGO real-time streaming cloud, and then the ZEGO real-time streaming cloud forwards the streams to CDN. You are working with a third-party CDN and want to use the existing third-party CDN streaming media distribution service while using the ZEGO Express SDK for real-time co-hosting interaction service. It is suitable for business scenarios requiring continuous interaction, such as private live streaming, voice chatroom, etc.
Direct publishing streams to the CDN The stream is published directly to the CDN without going through the ZEGO real-time streaming cloud. You are working with a third-party CDN for streaming media distribution service, but don't need to use real-time co-hosting interaction service. It is suitable for Livestream shopping, Livestream gaming, and large classes.

Prerequisites

Before you begin, make sure you complete the following steps:

  • A project has been created in ZEGOCLOUD Console and applied for a valid AppID and AppSign. For details, please refer to Console - How to view project information .
  • ZEGO Express SDK has been integrated into the project to implement basic real-time audio and video functions. For details, please refer to Integrate the SDK and Implement a basic video call.

Forward a stream to the CDN

Skip this session if you are direct publishing streams to the CDN.

After you publish a stream to ZEGO's real-time streaming cloud successfully, to forward a stream to ZEGO's CDN or third-party CDN, call the addPublishCdnUrl method to start forwarding the stream to the specified CDN URL. RTMP streams are supported.

If you need to forward a stream to multiple third-party CDNs:

  • Call the addPublishCdnUrl method multiple times with the corresponding CDN URLs and the same stream ID. (Each URL needs to be different.)

  • Accordingly, you need to call the removePublishCdnUrl method multiple times to stop the stream forwarding for different CDN URLs.

  • You can obtain the status update of the stream forwarding to each URL from the infoList parameter of the onPublisherRelayCDNStateUpdate callback.

/** Start forwarding streams to the CDN after the stream publishing is successful. */

// The stream ID you used for stream publishing.
NSString *streamID = @"STREAM_ID";
// Starts forwarding a stream to the specified CDN URL, fill in the URL based on the actual situation.
NSString *URL = @"rtmp://xxxxxxxx";
[self.engine addPublishCDNURL:URL stream:streamID callback:^(int errorCode) {
    // Obtain the notification that whether the forwarding operation is successful.
    if(errorCode == 0) {
        // Forwarding successful.
    } else {
        // Failed to forward, bacause the request is not sent due to network problems.
    }
}];

Optional: Listen for the stream forwarding status update notifications

To listen for the stream forwarding status change notifications, refer to the Streaming info monitoring.

Stop forwarding the stream to the CDN

To stop forwarding the stream to the specified CDN URL, call the removePublishCdnUrl method.

Calling this method does not stop the streams from being published to the ZEGO real-time streaming cloud.

// The stream ID you used for stream publishing.
NSString *streamID = @"STREAM_ID";
// Stops forwarding a stream to the specified CDN URL, fill in the URL based on the actual situation.
NSString *URL = @"rtmp://xxxxxxxx";
[self.engine removePublishCDNURL:URL stream:streamID callback:^(int errorCode) {
    // Obtain the notification that whether the stop forwarding operation is successful.
    if(errorCode == 0) {
        // Streams forwarding stopped successfully.
    } else {
        // Failed to stop forwarding streams to CDN, possibly because the request is not sent due to network error.
    }
}];

Direct publishing a stream to the CDN

Skip this session if you are forwarding streams to the CDN.

To direct publish streams to the CDN, call the enablePublishDirectToCDN method.

You can not forward the streams to the CDN by calling these two methods (addPublishCdnUrl and removePublishCdnUrl) after you enabled the direct publishing to CDN feature (enablePublishDirectToCDN).

  • Because these two methods are working for the forwarding streams to the CDN feature.
ZegoCDNConfig *config = [[ZegoCDNConfig alloc] init];
// Fill in the URL based on the actual situation.
config.URL = @"rtmp://xxxxxxxx";
[self.engine enablePublishDirectToCDN:YES config:config];
[self.engine startPublishing:@"STREAM_ID"];

Stop direct publishing the stream to the CDN

To stop direct publishing streams to CDN, call the stopPublishingStream method.

After stream publishing to CDN stops, if you want to disable the direct publishing to CDN feature for the next time, do the following:

  1. Call the enablePublishDirectToCDN method.
  2. Pass the false value into the method.
  • Calling this method during stream publishing does not affect the current stream publishing operation.
[[ZegoExpressEngine sharedEngine] stopPublishingStream];
ZegoCDNConfig *config = [[ZegoCDNConfig alloc] init];
[[ZegoExpressEngine sharedEngine] enablePublishDirectToCDN:NO config:config];
Page Directory
  • Free trial
  • 提交工单
    咨询集成、功能及报价等问题
    电话咨询
    400 1006 604
    Get Consulting
    Scan Wechat QR code