Record Mixed Stream
Feature Overview
The Local Server Recording SDK supports mixing the audio/video streams of all users in a room into one view and then recording it. After integrating the SDK, you can start using its mixed-stream recording feature.
Prerequisites
- The ZEGOCLOUD Local Server Recording SDK has been integrated into your project. For details, see Quick Start - Integration.
- You have created a project in the ZEGOCLOUD Console and obtained a valid AppID. For details, see "Project Information" in Console - Project Management.
Steps
Set the event callback listeners
Before initializing the SDK, first call the setZegoRoomCallback, setZegoStreamRecordCallback, and setZegoExternalVideoDataCallback APIs to set the room callbacks, the recording callbacks, and the video data callbacks respectively, so as to receive the related event notifications during audio/video stream recording.
private static IZegoRoomCallback mRoomCallback = new IZegoRoomCallback() {
//Handle room callbacks
};
private static IZegoStreamRecordCallback mStreamRecordCallback = new IZegoStreamRecordCallback() {
//Handle recording event callbacks
};
private static IZegoExternalVideoDataCallback mVideoDataCallback = new IZegoExternalVideoDataCallback() {
//Handle video frame data callbacks. The callback returns the decoded frame data of each single stream. Currently, it is only supported for mixed-stream recording
};
public void setZegoRoomCallback(IZegoRoomCallback callback)
public void setZegoStreamRecordCallback(IZegoStreamRecordCallback callback)
public void setZegoExternalVideoDataCallback(IZegoExternalVideoDataCallback callback)Before initializing the SDK, you can also call the setLogDirAndSize API as needed to set the log file path and size, which facilitates problem locating and analysis.
Initialize the SDK
Call the initSDK API to initialize the SDK, and pass the obtained AppID into the parameter "appID".
public boolean initSDK(long appID, IZegoInitSDKCompletionCallback callback)Register the asynchronous callback onInitSDK to receive the related event notifications during SDK initialization.
public interface IZegoInitSDKCompletionCallback {
void onInitSDK(int errorCode);
}Set the Token
After initializing the SDK and before logging in to a room, call the setToken API to pass in the Token you obtained for authentication.
For how to obtain the Token, see Authenticate with Token.
public void setToken(String token, String roomID);Log in to the room
After the authentication is passed, call the loginRoom API to log in to the room.
public boolean loginRoom(String roomID, IZegoLoginCompletionCallback callback)Register the asynchronous callback onLoginCompletion to receive the related event notifications during room login.
public interface IZegoLoginCompletionCallback {
void onLoginCompletion(int errorCode, String roomID);
}Start recording
After logging in to the room successfully, call the startRecordMixStream API to start recording the audio/video stream.
- It is recommended that, after logging in to the room successfully and receiving the onStreamUpdate stream information update callback notification and confirming the information of the added stream, you call the startRecordMixStream API to start mixed-stream recording.
- Before starting recording, you can call the getMaxRecordCount API to get the maximum number of streams the SDK supports recording simultaneously, and handle the number of concurrently recorded streams based on its value.
public boolean startRecordMixStream(ZegoMixStreamRecordConfig config, String roomID)Where: config refers to the various configurations for mixed-stream recording. For details, see ZegoMixStreamRecordConfig.
Register the asynchronous callbacks onStreamRecordBegin and onMixStreamRecordUpdate to receive the related event notifications during mixed-stream recording.
public interface IZegoStreamRecordCallback {
void onStreamRecordBegin(String streamID, String pathAndName);
void onMixStreamRecordUpdate(String[] listStreamID);
}After recording starts successfully, the onStreamRecordBegin callback is received, and when a stream with audio/video data is successfully pulled, the onMixStreamRecordUpdate callback is received.
If you need to record single streams and the mixed stream at the same time, call the enableSingleAndMixRecordMode API before starting recording to enable the mode of recording single streams and the mixed stream simultaneously:
- In this mode, "mixed-stream recording" mixes in all streams being recorded by "single-stream recording". Even if you do not set a single stream in "single-stream recording" as an input stream of "mixed-stream recording", as long as this single stream has audio, the audio will also be recorded by the mixed-stream recording.
- This mode consumes considerable system resources; you can only start mixed-stream recording first and then start single-stream recording; stopping mixed-stream recording also stops all single-stream recordings at the same time.
Use this mode as appropriate.
Update the mixed-stream configuration
When you need to add input streams, update input streams, or update the mixed-stream output configuration, you need to reconstruct the mixed-stream configuration ZegoStreamConfig and call the updateInputStreamConfig API to update the mixed-stream configuration.
public boolean updateInputStreamConfig(ZegoStreamConfig[] listStreamConfig, String roomID)Register the asynchronous callback onMixStreamRecordUpdate to receive the related event notifications during the mixed-stream configuration update.
public interface IZegoStreamRecordCallback {
void onMixStreamRecordUpdate(String[] listStreamID);
}- After the mixing succeeds, the list of stream IDs of the mixed-stream input streams is returned (only including the stream IDs whose data was successfully pulled).
- Whenever the input streams of mixed-stream recording change (for example, recording starts, or a stream encounters an exception during recording), the onMixStreamRecordUpdate callback is received.
Stop recording
When recording is no longer needed or is completed, call the stopRecordMixStream API to stop recording.
public boolean stopRecordMixStream(String roomID)Register the asynchronous callback onStreamRecordEnd to receive the related event notifications when recording is stopped.
public interface IZegoStreamRecordCallback {
void onStreamRecordEnd(String streamID, String pathAndName, int reason);
}Then:
- Call the logoutRoom API to exit the room.
- Call the setZegoRoomCallback(null), setZegoStreamRecordCallback(null), and setZegoExternalVideoDataCallback(null) APIs to stop the callback listeners.
- Call the unInitSDK API to uninitialize the SDK.
