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
  • Upgrade the livestream
  • Advanced features
  • Playing streams via URL

Playing streams via URL

Last updated:2023-08-31 15:50

Introduction

When a stream is published to a CDN using third-party streaming tools (OBS software, IP Camera, etc.), or a stream is relayed from ZEGO's real-time streaming server to a CDN, you can use the ZEGO Express SDK to play the stream directly from the CDN using the stream playback URL.

Prerequisites

Before implementing the "Playing Streams via URL" functions, please make sure:

  • 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.
  • Contact ZEGO technical support to activate the function of Playing streams via URL.

Configure the Stream Playback Parameters

To play the stream directly from a CDN URL, you need to create a ZegoCDNConfig object and config the "URL" parameter and the authentication parameter "authParam" (if required) accordingly.

  • Authentication parameter, that is, the string after the "?" of the URL (not including the "?"). For example, when the URL for streaming from CDN is "rtmp://xxxx.yyy.zzz?a=qqq&b=www", the authentication parameter is "a=qqq&b=www".
  • The authentication parameters of Pull Stream URL are mainly used to prevent theft chain. For specific authentication rules, please contact the specific CDN manufacturer or ZEGO technical support for consultation. If there is no authentication parameter, please ignore the "authParam" field.
  • API Function Prototype:

    /// CDN configuration object 
    ///
    /// It includes the URL of the CDN and the authentication parameter
    @interface ZegoCDNConfig : NSObject
    
    /// The CDN URL
    @property (nonatomic, copy) NSString *url;
    
    /// The URL authentication parameter
    @property (nonatomic, copy) NSString *authParam;
    
    @end
  • API Call Example:

    /** Set CDN parameters */
    ZegoCDNConfig *cdnConfig = [[ZegoCDNConfig alloc] init];
    // URL needs to be the CDN stream-playing address
    cdnConfig.url = @"rtmp://xxxx.yyy.zzz";
    // If you need authentication, you need to set the authentication parameters, if you don’t need authentication, you don’t need to set it (authentication parameters cannot contain "?" characters)
    cdnConfig.authParam = @"a=qqq&b=www";
    ZegoPlayerConfig *config = [[ZegoPlayerConfig alloc] init];
    config.cdnConfig = cdnConfig;

Start Playing the Streams

  • When pulling a stream through a URL, you cannot pull the stream directly by filling in the stream ID. The actual drawing screen is subject to the URL.
  • Although the stream ID cannot be used to pull the stream at this time, the SDK still uses the stream ID as a unique identifier for subsequent pull-related callbacks. Therefore, the stream ID still needs to be globally unique within the entire AppID.
  • API Function Prototype:

    /// Start playing the stream
    ///
    /// This API allows users to play a remote stream from the ZEGO real-time streaming server, or from the CDN URL if configured.
    /// Before starting to play the stream, you need to join a room first. For any new stream published to the room, you can obtain the stream ID by listening for the event callback [onRoomStreamUpdate].
    /// When the network quality is poor, the stream playback may get interrupted. The SDK will try to reconnect automatically. You can obtain the current state of the stream playback or any error that may occur by listening for the event callback [onPlayerStateUpdate].
    /// If the stream with the specified stream ID does not exist, the SDK will keep trying to pull the stream. Once the stream is published to the room, the SDK will start to play it. 
    /// The Canvas for displaying the received video can be updated by calling this API again (with the same stream ID).
    ///
    /// @param streamID: The stream ID of the stream to be played, a string with maximum length of 256 characters. A stream ID cannot contain any URL keyword, otherwise the stream playback will fail. It can only contain numbers, English letters, and special characters listed here: '~', '!', '@', '#', '$', '%', '^', '&', '*', '(', ')', '_', '+', '=', '-', '`', ';', '’', ',', '.', '<', '>', '/', '\'.
    /// @param canvas: The view for displaying the received video. If the view is set to [nil], it will not be displayed.
    /// @param config: The advanced stream playback configurations.
    -(void)startPlayingStream:(NSString *)streamID canvas:(nullable ZegoCanvas *)canvas config:(ZegoPlayerConfig *)config;
  • API Call Example:

    /** Start playing the stream */
    ZegoCDNConfig *cdnConfig = [[ZegoEngineConfig alloc] init];
    // URL needs to be the CDN stream-playing address 
    cdnConfig.url = @"rtmp://xxxxxxxx";
    // If you need authentication, you need to set the authentication parameters, if you don’t need authentication, you don’t need to set it (authentication parameters cannot carry "?" characters).
    cdnConfig.authParam = @"xxx";
    ZegoPlayerConfig *config = [[ZegoPlayerConfig alloc] init];
    config.cdnConfig = cdnConfig;
    // Start playing the stream
    [self.engine startPlayingStream:streamID canvas:playCanvas config:config];
    
    /** Stop playing the stream */
    [self.engine stopPlayingStream:self.streamID];

If there is an error while pulling the stream, please refer to Error Codes - 1004xxx Errors Related to Stream Playing.

(Optional) Monitor the Stream Playback Status

Monitor the Stream Playback Status

You can monitor the status of the stream playback from CDN by listening for the callback onPlayerStateUpdate.

If you are using a third-party tool (e.g., OBS) for stream publishing, but using the ZEGO SDK for stream playback, the stream receiver by default cannot receive the callback onRoomStreamUpdate because the stream publisher does not use the ZEGO SDK to log in to a ZEGO room. In such cases, you can use ZEGO's server-side APIs Add Stream and Delete Stream to inform ZEGO's backend service about any new stream added to or existing stream deleted from a room, so that other users in the same room can receive the callback onRoomStreamUpdate.

  • API Function Prototype:

    /// Callback for stream playback status update
    ///
    /// After the stream playback is started successfully, you can receive the status update through this callback.
    /// By checking whether the stream playback is in the state of ZegoPlayerStatePlayRequesting, you can roughly determine the user's network condition.
    ///
    /// @param state: The current state of stream playback.
    /// @param errorCode: The error code corresponding to the change of stream playback status.
    /// @param extendedData: The extended information.
    /// @param streamID: The stream ID of the stream being played.
    - (void)onPlayerStateUpdate:(ZegoPlayerState)state errorCode:(int)errorCode extendedData:(nullable NSDictionary *)extendedData streamID:(NSString *)streamID;
  • API Call Example:

    @implementation xx
    - (void)onPlayerStateUpdate:(ZegoPlayerState)state errorCode:(int)errorCode extendedData:(NSDictionary *)extendedData streamID:(NSString *)streamID {
         /** After the stream playback is started successfully, if there is any status change, such as an exception caused by network interruption, the SDK will send out notification of such status change through this callback while retrying to resume the stream playback. */
        // 1. If the state is ZegoPlayerStatePlaying, it means that the stream is received successfully and being played out.
        // 2. If the state is ZegoPlayerStatePlayRequesting, it means that the SDK is still trying to pull the stream, or retrying to pull it after it is interrupted by network problems.
        // 3. If the state is ZegoPlayerStateNoPlay, it means that the stream playback has stopped.
        // 4. When playing a stream via a URL, the 'stream_id' in the callback parameters is the stream ID used for calling the API [startPlayingStream], which can be used as a unique identifier of the current stream playback.
    }
    
    // Other callback method overrides
    ...
    @end
    [self.engine setEventHandler:self];
Page Directory
  • Free trial
  • 提交工单
    咨询集成、功能及报价等问题
    电话咨询
    400 1006 604
    Get Consulting
    Scan Wechat QR code