Playing Stream by URL
Function Overview
When the publishing end uses third-party publishing tools (such as OBS software, network camera IP Camera, etc.) to publish the stream to CDN, or uses ZEGO SDK to relay CDN functionality to push the audio and video screen to a third-party CDN, you can directly pass in the URL address to play stream.
This feature does not support running in WebGL environment.
Example Source Code Download
Please refer to Download Example Source Code to get the source code.
For related source code, please see files in the "Assets/ZegoExpressExample/Examples/AdvancedStreaming/StreamByCDN" directory.
Prerequisites
Before implementing the URL play stream function, please ensure:
-
You have created a project in ZEGOCLOUD Console and applied for valid AppID and AppSign. For details, please refer to Console - Project Information.
-
You have integrated ZEGO Express SDK in the project and implemented basic audio and video streaming functions. For details, please refer to Quick Start - Integration and Quick Start - Implementation.
-
You have contacted ZEGOCLOUD Technical Support to enable the URL play stream function.
-
You have pushed the audio and video stream to CDN and know the corresponding URL. For details, please refer to Use CDN for Live Streaming.
Usage Steps
1 Configure Play Stream Parameters
To play stream directly through the CDN URL address, you need to use the ZegoCDNConfig object to fill in the URL parameters. When there is play stream authentication when playing stream from CDN, you also need to fill in the authentication parameters through the authParam field.
- The authentication parameter refers to the string after the "?" in the URL (excluding "?"). For example, if the CDN playing stream URL is "rtmp://xxxx.yyy.zzz?a=qqq&b=www", the authentication parameter is "a=qqq&b=www".
- The authentication parameter in the playing stream URL is mainly used for anti-leeching. For specific rules, please consult your CDN provider or ZEGO Technical Support. If there is no authentication parameter, you can ignore the "authParam" field.
- Supported playing stream protocols include: RTMP, FLV, HLS (if you need to use HLS protocol for playing stream, please contact ZEGO Technical Support to enable it).
// Set CDN parameters
ZegoCDNConfig cdnConfig = new ZegoCDNConfig();
// URL is the CDN play stream address
cdnConfig.url = "rtmp://xxxxxxxx";
// If authentication is required, set authentication parameters. If authentication is not required, it can be left unset (authentication parameters cannot contain "?" characters)
cdnConfig.authParam = "xxx";
ZegoPlayerConfig config = new ZegoPlayerConfig();
config.cdnConfig = cdnConfig;2 Start Playing Stream
Start playing stream by calling the startPlayingStream interface.
When playing stream, if an error occurs, please refer to Common Error Codes - 1004xxx Play Stream Related Error Codes.
- Before playing stream by URL, ensure that you have logged in to the room.
- When playing stream by URL, you cannot play stream by directly filling in
StreamID. The actual play stream screen is based on the URL. - Although
StreamIDcannot be used to play stream at this time, the SDK still usesStreamIDas the unique identifier internally for subsequent play stream related callbacks. Therefore,StreamIDstill needs to be globally unique within the entire AppID.
// Start playing stream
engine.StartPlayingStream(streamID, config);3 Stop Playing Stream
To stop playing stream, call the stopPlayingStream interface.
// Stop playing stream
engine.StopPlayingStream(streamID);4 (Optional) Listen to Play Stream Related Event Notifications
You can listen to the result of playing stream from CDN through OnPlayerStateUpdate.
If you are not using ZEGO SDK to publish stream, but using third-party publishing tools to publish stream directly, and using ZEGO SDK to play stream, in this scenario, the publishing end has not joined the room using ZEGO SDK, and the playing end will not receive the callback of OnRoomStreamUpdate by default. You can use the Add Room Stream and Delete Room Stream functions to allow the playing end to receive the callback of OnRoomStreamUpdate .
void OnPlayerStateUpdate(const std::string& streamID, ZegoPlayerState state, int errorCode, const std::string& extendedData) {
// After calling the play stream interface successfully, when the player status changes, such as network interruption causing publishing stream abnormalities, etc., the SDK will notify through this callback while retrying to play stream
// No play state, in this state before playing stream. If a steady-state exception occurs during the play stream process, such as incorrect AppID and AppSign, it will enter the no play state
// ZEGO_PLAYER_STATE_NO_PLAY = 0,
// Requesting play state, after the play stream operation is executed successfully, it will enter the requesting play state, usually this state is used for application interface display. If an interruption occurs due to poor network quality, the SDK will perform internal retry and will also return to the requesting play state
// ZEGO_PLAYER_STATE_PLAY_REQUESTING = 1,
// Playing state, entering this state indicates that play stream has been successful and users can communicate normally
// ZEGO_PLAYER_STATE_PLAYING = 2
// When playing stream by URL, the stream_id in the callback parameters is the stream ID when calling the play stream API, used to uniquely identify the current play stream event.
}
engine.onPlayerStateUpdate = OnPlayerStateUpdate;