This guide describes how to implement basic audio and video functions with the ZEGO Express SDK.
Basic concepts:
ZEGO Express SDK: The real-time audio and video SDK developed by ZEGOCLOUD to help you quickly build high-quality, low-latency, and smooth real-time audio and video communications into your apps across different platforms, with support for massive concurrency.
Stream publishing: The process of the client app capturing and transmitting audio and video streams to the ZEGOCLOUD Real-Time Audio and Video Cloud.
Stream playing: The process of the client app receiving and playing audio and video streams from the ZEGOCLOUD Real-Time Audio and Video Cloud.
Room: The service for organizing groups of users, allowing users in the same room to send and receive real-time audio, video, and messages to each other.
For more basic concepts, refer to the Glossary.
Before you begin, make sure you complete the following steps:
If the version of the ZEGO Express SDK you are using is under 2.17.0, to get the AppSign, contact the ZEGOCLOUD Technical Support. To upgrade the authentication mode from using the AppSign to Token, see Guide for upgrading the authentication mode from using the AppSign to Token.
The following diagram shows the basic process of User A playing a stream published by User B:
The following sections explain each step of this process in more detail.
1. Create the UI
Before creating a ZegoExpressEngine
instance, we recommend you add the following UI elements to implement basic real-time audio and video features:
2. Import the header file
Import the header file ZegoExpressSDK.h
into the project.
// Import the header file ZegoExpressEngine.h
#include "ZegoExpressSDK.h"
3. Create a ZegoExpressEngine
instance
To create a singleton instance of the ZegoExpressEngine
class, call the createEngine
method with the AppID of your project.
To receive callbacks, implement an event handler object that conforms to the IZegoEventHandler
protocol, and then pass the implemented event handler object as the eventHandler
parameter.
Alternatively, you can pass nullptr
as the eventHandler
parameter for now, and then call the method setEventHandler
to set up the event handler after creating the engine.
ZegoEngineProfile profile;
profile.appID = appID;
profile.scenario = ZegoScenario::ZEGO_SCENARIO_GENERAL;
// Create a ZegoExpressEngine instance.
auto engine = ZegoExpressSDK::createEngine(profile, nullptr);
Before logging in to a room, you will need to generate a token first; Otherwise, the login will fail.
To generate a token, refer to the Use Tokens for authentication.
To log in to a room, call the loginRoom
method. If the roomID does not exist, a new room will be created and you will log in automatically when you call the loginRoom
method.
// Create a ZegoUser object.
ZegoUser user("user1", "user1");
ZegoRoomConfig roomConfig;
// Token is generated by the user's own server. For an easier and convenient debugging, you can get a temporary token from the ZEGOCLOUD Admin Console
roomConfig.token = "xxxx";
// To receive the onRoomUserUpdate callback, you must set the [isUserStatusNotify] property of the room configuration parameter [ZegoRoomConfig] to [true].
roomConfig.isUserStatusNotify = true;
// Log in to a room.
engine->loginRoom(roomID, user, roomConfig, [=](){
// (Optional callback) The result of logging in to the room. If you only pay attention to the login result, you can use this callback.
});
To monitor your room connection status in real time after making a method call to log in to a room, listen for onRoomStateUpdate
callback.
You can only start publishing streams (startPublishingStream
) or playing streams (startPlayingStream
) when the room state is connected.
void VideoTalk::onRoomStateUpdate(const std::string &roomID, ZegoRoomState state, int errorCode, const std::string &extendData) {
if(errorCode != 0)
{
// Room state error.
}
if(state == ZegoRoomState::ZEGO_ROOM_STATE_CONNECTED)
{
// You can only perform the stream publishing (startPublishingStream) and stream playing (startPlayingStream) operations when the room state is connected.
//Publish your audio and video streams to the ZEGO Real-Time Audio and Video Cloud.
}
else if (state == ZegoRoomState::ZEGO_ROOM_STATE_CONNECTING)
{
// Connecting to the room.
} else if (state == ZegoRoomState::ZEGO_ROOM_STATE_DISCONNECTED)
{
// Room get disconnected.
}
}
To start the local video preview, call the startPreview
method to set the canvas for the video preview, and enable the local video preview.
// Set the canvas for the local video preview, and enable the local video preview. The SDK uses the default view (Fill in the window handle of Windows).
ZegoCanvas canvas((void*)view);
engine->startPreview(&canvas);
To publish the audio and video streams to the ZEGO Real-Time Audio and Video Cloud after logging in to a room, call the startPublishingStream
method with the corresponding stream ID passed to the streamID
parameter.
You can generate the streamID
as needed, but make sure it meets the requirement:
It must be globally unique within the scope of the AppID. If different streams are published with the same streamID
, the ones that are published after the first one will fail.
You have not restricted to only publishing the streams immediately after logging in to a room, you can also publish the streams at other times as long as the room state is Connected.
void VideoTalk::onRoomStateUpdate(const std::string &roomID, ZegoRoomState state, int errorCode, const std::string &extendData) {
if (state == ZegoRoomStateConnected) {
// Room connected successfully.
// You can only perform stream publishing, stream playing, and other operations when the room state is connected.
// Publish the audio and video streams to the ZEGO Real-Time Audio and Video Cloud.
engine->startPublishingStream("stream1");
}
}
To play the audio and video streams of other users during a video call, refer to the following:
You receive an event notification through the callback onRoomStreamUpdate
when there are new streams are published to the ZEGO Real-Time Audio and Video Cloud.
After receiving the event notifications, you can call the startPlayingStream
method in the callback with the corresponding stream ID passed to the streamID
parameter to play the audio and video streams of other users.
For errors occurred during a video call, refer to the Error codes.
// You will receive a event notification on here when new streams are published to the room or existing streams in the room stop.
void VideoTalk::onRoomStreamUpdate(const std::string &roomID, ZegoUpdateType updateType, const std::vector<ZegoStream> &streamList, const std::string &extendData) {
//When [updateType] is [ZEGO_UPDATE_TYPE_ADD], indicating new stream is published to the room, at this time, you can call the [startPlayingStream] method to play the new audio or video stream.
if (updateType == ZEGO_UPDATE_TYPE_ADD) {
// To start playing a stream, set the canvas for the remote rendering view (The SDK uses the default view, that is, fill in the window handle of Windows).
// The [playView] is the window handle of the UI.
std::string streamID = streamList[0].streamID;
ZegoCanvas canvas((void*)playView);
engine->startPlayingStream(streamID , &canvas);
}
}
We recommend you run your project on a real device. If your app runs successfully, you should hear the sound and see the video captured locally from your device.
To test out the real-time audio and video features, visit the ZEGO Express Web Demo, and enter the same AppID
, Server
and RoomID
to join the same room. If it runs successfully, you should be able to view the video from both the local side and the remote side, and hear the sound from both sides as well.
In audio-only scenarios, no video will be captured and displayed.
onRoomStateUpdate
: To monitor your room connection status in real time, listen for this callback when calling the loginRoom
method to log in to a room.
You can implement the callback handling logic based on the actual room state.
void VideoTalk::onRoomStateUpdate(const std::string &roomID, ZegoRoomState state, int errorCode, const std::string &extendData) {
}
The following shows the implications of the ZegoRoomState
state:
State | Enumerated value | Implication |
---|---|---|
ZEGO_ROOM_STATE_DISCONNECTED |
0 |
Disconnected state, which appears before you log in to a room or after you log out from a room. This state also appears if a steady-state exception occurs during the login operation, for example, the AppID and token are incorrect, or the local end user is kicked out when the same username is used to log in to another rooms. |
ZEGO_ROOM_STATE_CONNECTING |
1 |
Connecting state, which appears when you perform the room login operation successfully. (Generally, the user can set a corresponding UI to indicate the current state.) This state also appears when the SDK automatically tries to reconnect when users disconnected from a room due to network errors. |
ZEGO_ROOM_STATE_CONNECTED |
2 |
Connected state, which appears when you log in to a room successfully. In this state, users can receive event notifications related to room users and streams. |
onRoomUserUpdate
: When other users join or leave the room, you will receive the event notification through this callback.
onRoomUserUpdate
callback, you must set the isUserStatusNotify
property of the room configuration parameter ZegoRoomConfig
to true
when calling the loginRoom
method to log in to a room.ZegoUpdateType
is ZEGO_UPDATE_TYPE_ADD
: indicates new users join the room. When the ZegoUpdateType
is ZEGO_UPDATE_TYPE_DELETE
: indicates other users leave the room. void VideoTalk::onRoomUserUpdate(const std::string &roomID, ZegoUpdateType updateType, const std::vector<ZegoUser> &userList) {
// You can implement the callback handling logic based on the actual situation (users join/leave the room).
if (updateType == ZEGO_UPDATE_TYPE_ADD) {
} else if (updateType == ZEGO_UPDATE_TYPE_DELETE) {
}
}
onPublisherStateUpdate
: After stream publishing starts, if the status changes, (for example, when the stream publishing is interrupted due to network issues and the SDK retries to start publishing the stream again), you will receive the event notification through this callback.
void VideoTalk::onPublisherStateUpdate(const std::string &streamID, ZegoPublisherState state, int errorCode, const std::string &extendData) {
if (errorCode != 0) {
// Error publishing streams.
} else {
switch (state) {
case ZEGO_PUBLISHER_STATE_PUBLISHING:
// Stream is being published.
break;
case ZEGO_PUBLISHER_STATE_PUBLISH_REQUESTING:
// Requesting the stream publishing operation.
break;
case ZEGO_PUBLISHER_STATE_NO_PUBLISH:
// No stream is being published.
break;
}
}
}
onPlayerStateUpdate
: After stream playing starts, if the status changes (for example, when the stream playing is interrupted due to network issues and the SDK retries to start playing the stream again), you will receive the event notification through this callback.
void VideoTalk::onPlayerStateUpdate(const std::string &streamID, ZegoPlayerState state, int errorCode, const std::string &extendData) {
if (errorCode != 0) {
//Error playing the stream.
} else {
switch (state) {
case ZEGO_PLAYER_STATE_NO_PLAY:
// Stream is being played.
break;
case ZEGO_PLAYER_STATE_PLAY_REQUESTING:
// Requesting the stream playing operation.
break;
case ZEGO_PLAYER_STATE_NO_PLAY:
// No stream is being played
break;
}
}
}
To stop publishing a local audio or video stream to remote users, call the stopPublishingStream
method.
// Stop publishing a stream.
engine->stopPublishingStream();
If local video preview is started, call the stopPreview
method to stop it as needed.
// Stop the local video preview.
engine->stopPreview();
To stop playing a remote audio or video stream, call the stopPlayingStream
method.
// Stop playing a stream.
engine->stopPlayingStream("streamID");
To log out of a room, call the logoutRoom
method with the corresponding room ID passed to the roomID
parameter.
// Log out from a room.
engine->logoutRoom("room1");
ZegoExpressEngine
instanceTo destroy the ZegoExpressEngine
instance and release the microphone, camera, memory, CPU, and other resources it occupies, call the destroyEngine
.
callback
when destroying the ZegoExpressEngine
instance.
This callback can only be used to send out a notification when the destruction of the engine is completed. You can't use this callback method to release engine-related resources.nullptr
to destroyEngine
instead.// Destroy a ZegoExpressEngine instance.
ZegoExpressSDK::destroyEngine(engine, nullptr);
logoutRoom
method to log out from a room?If you have done so, which may possibly cause the signaling of the logoutRoom
method call to fail to be sent. In this case, the ZEGO server does not determine that the user has logged out of the room until the heartbeat times out.
To make sure the signaling of the logoutRoom
method call be sent successfully, we recommend you call the destroyEngine
method and kill the process after you received the event notifications.
The following diagram shows the API call sequence of the stream publishing and playing process: