Playing Stream by URL
Feature Introduction
When the publishing end uses third-party publishing tools (such as OBS software, network camera IP Camera, etc.) to publish streams to CDN, or pushes audio and video content to third-party CDNs through ZEGO SDK's relaying to CDN function, you can directly pass in the URL address to play streams.
If you need to switch from playing stream from CDN scenario to co-hosting scenario, please refer to the Switching from CDN Stream Playing to RTC Co-hosting documentation.
Example Source Code Download
Please refer to Download Example Source Code to get the source code.
For relevant source code, please see files in the "/ZegoExpressExample/Examples/AdvancedStreaming/StreamByCDN" directory.
Prerequisites
Before implementing URL stream playing functionality, ensure that:
-
You have created a project in the ZEGOCLOUD Console and applied for a valid AppID and AppSign. For details, please refer to Console - Project Information.
-
You have integrated ZEGO Express SDK into your project and implemented basic audio and video stream publishing and playing functionality. For details, please refer to Quick Start - Integration and Quick Start - Implementation.
-
You have contacted ZEGO Technical Support to enable URL stream playing functionality.
-
You have published audio and video streams to CDN and know the corresponding URL. For details, please refer to Using CDN for Live Streaming.
Usage Steps
1 Configure Stream Playing Parameters
To play streams directly through CDN URL addresses, you need to use the ZegoCDNConfig object to fill in URL parameters. If the corresponding CDN is configured with stream playing authentication, you also need to fill in 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;
// URL is the CDN stream playing address
cdnConfig.url = "rtmp://xxxxxxxx";
// If authentication is required, set authentication parameters. If authentication is not required, this does not need to be set (authentication parameters cannot contain "?" character)
cdnConfig.authParam = "xxx";
ZegoPlayerConfig config;
config.cdnConfig = &cdnConfig;2 Start Playing Stream
Start playing stream by calling the startPlayingStream interface.
When playing streams, if errors occur, please refer to Common Error Codes - 1004xxx Stream Playing Related Error Codes.
- Before playing streams by URL, ensure that you have logged into a room.
- When playing streams by URL, you cannot directly play streams by filling in the stream ID. The actual playing picture is based on the URL.
- Although the stream ID cannot be used for stream playing at this time, the SDK still uses the stream ID as the unique identifier internally, used in subsequent stream playing related callbacks. Therefore, the StreamID still needs to be globally unique within the entire AppID.
// Start playing stream, set remote playing rendering view canvas, view mode uses SDK's default mode, proportional scaling to fill the entire View
ZegoCanvas canvas((void*)view);
// After filling in the url parameter, the SDK will play audio and video streams from the url, but still needs to pass a unique streamID to the SDK, which will internally identify this stream with that streamID
engine->startPlayingStream(streamID, &canvas, config);3 Stop Playing Stream
To stop playing stream, call the stopPlayingStream interface.
// Stop playing stream
// The streamID passed when stopping playing stream is the streamID passed when playing stream
engine->stopPlayingStream(streamID);4 (Optional) Listen to Stream Playing Related Event Notifications
You can listen to the results of playing streams from CDN through onPlayerStateUpdate.
If you are not using ZEGO SDK for publishing but using third-party publishing tools to publish directly, but using ZEGO SDK for playing, in this scenario the publishing end is not using ZEGO SDK to join the room, and the playing end will not receive onRoomStreamUpdate callbacks by default. You can use the Add Room Stream and Delete Room Stream functions to allow the playing end to receive onRoomStreamUpdate callbacks.
class MyEventHandler :public IZegoEventHandler
{
public:
void onPlayerStateUpdate(const std::string& /*streamID*/, ZegoPlayerState /*state*/, int /*errorCode*/, const std::string& /*extendedData*/) {
// After successfully calling the playing stream interface, when the player state changes, such as network interruption causing publishing stream exceptions, etc., the SDK will notify through this callback while retrying to play stream
// Not playing state, in this state before playing stream. If a steady-state exception occurs during the playing process, such as incorrect AppID and AppSign, it will enter the not playing state
//ZEGO_PLAYER_STATE_NO_PLAY = 0,
// Requesting to play stream state, after the playing operation is executed successfully, it will enter the requesting to play stream state, usually this state is used for application interface display. If interruption occurs due to poor network quality, the SDK will internally retry and will also return to the requesting to play stream state
//ZEGO_PLAYER_STATE_PLAY_REQUESTING = 1,
// Playing stream state, entering this state indicates that playing 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 playing stream API, used to uniquely identify the current playing event.
}
};
auto handler=std::make_shared<MyEventHandler>();
engine->setEventHandler(handler);