On this page

Record Single Stream

2026-09-17

Feature Overview

The Local Server Recording SDK supports recording a single audio/video stream in a room. After integrating the SDK, you can start using its single-stream recording feature.

Prerequisites

Note

ZEGOCLOUD Local Server Recording SDK versions earlier than ga01596a1c8 (released before 2022-03-23) require you to apply for a valid AppID in the ZEGOCLOUD Console and contact ZEGOCLOUD Technical Support to obtain the AppSign. If you need to upgrade the authentication method, see How to upgrade from AppSign authentication to Token authentication.

Steps

Set the callback listener

Before initialization, you need to inherit and implement ICallback, and set the callback listener.

  • API prototype:

    ZEGO_API bool SetCallback(ICallback* pCB)
  • Parameters:

    pCB: Pointer to the callback object.

  • Note:

    Before initializing the SDK, you can call the following API as needed:

    SetLogDirAndSize sets the log file path and size

Initialize the SDK

Call the InitSDK API to initialize the SDK, and pass the obtained AppID into the parameter "uiAppID".

  • API prototype:

    ZEGO_API bool InitSDK(unsigned int uiAppID)
  • Parameters:

    uiAppID: The numeric ID issued by ZEGO, the unique identifier of each developer.

  • Asynchronous callback interface:

    class ICallback
    {
    public:
        virtual void OnInitSDK(int errorCode) {}
    }

    errorCode == 0 indicates that the initialization succeeded.

Set the token

Before logging in to a room, call the SetToken API to pass the Token into the SDK.

For how to obtain the Token, see Authenticate with Token.

  • API prototype:

    ZEGO_API void SetToken(const char *pszToken, const char *pszRoomID)
  • Parameters:

    pszToken: The authentication Token. pszRoomID: Room ID, required.

Log in to the room

You need to log in to the room before recording.

  • API prototype:

    ZEGO_API bool LoginRoom(const char* pszRoomID)
  • Parameters:

    pszRoomID: Room ID, the ID of the room that is publishing streams.

  • Asynchronous callback interface:

    class ICallback
    {
    public:
        virtual void OnLoginRoom(int errorCode, const char *pszRoomID) = 0;
    }

Start recording

  1. This API must be called after the initialization succeeds.
  2. It is recommended to start recording based on the added stream information after logging in to the room and receiving the OnStreamUpdate stream information update callback.
  3. Before starting recording, you can call GetMaxRecordCount to get the maximum number of streams the SDK supports recording simultaneously, and handle the number of concurrently recorded streams based on its value.
  • API prototype:

    ZEGO_API bool StartRecordSingleStream(const char *pszStreamID, const char *pszPathAndName,
                                        MuxerStreamType muxerStreamType = MuxerStreamTypeBoth,
                                        int nFragmentSeconds = 2,
                                        ZegoRecordSingleStreamConfig *pConfig = nullptr,
                                        const char *pszRoomID = nullptr)
  • Parameters:

    pszStreamID: The ID of the stream to be recorded.

    pszPathAndName: The full path of the recording file (including the file name), which must end with .mp4, .m3u8, .flv, .mp3, or .aac (.mp3 and .aac only support recording pure audio). The SDK automatically creates the corresponding path, and if a file with the same name exists, it directly overwrites the original file.

    muxerStreamType: The type of the stream to record. See MuxerStreamType. By default, both audio and video are recorded.

    nFragmentSeconds: The fragment interval of the recording file (0~10s). 0 means no fragmentation, and a value greater than 0 means fragmentation; the default interval is 2s. Fragmentation ensures that the saved recording files can still play properly when recording is interrupted unexpectedly. Note that fragmentation here is a logical concept for the SDK to process recording files internally, rather than splitting the recording file into multiple small files by interval for storage.

    pConfig: Advanced configuration for single-stream recording files.

    pszRoomID: Room ID, required in multi-room mode; in single-room mode, nullptr can be passed, and the currently logged-in room is used by default.

  • Asynchronous callback interface:

    class ICallback
    {
    public:
        virtual void OnStreamRecordBegin(const char* pszStreamID, const char *pszPathAndName) = 0;
    }

When recording starts successfully, the recording start callback is received.

Stop recording

  • API prototype:

    ZEGO_API bool StopRecordSingleStream(const char* pszStreamID)
  • Parameters:

    pszStreamID: Stream ID.

  • Asynchronous callback interface:

    class ICallback
    {
    public:
        virtual void OnStreamRecordEnd(const char* pszStreamID, const char *pszPathAndName, RecordEndReason reason) = 0;
    }
  • Note:

    It is recommended to do the following when recording is no longer needed:

    1. Call LogoutRoom to exit the room.
    2. Call LIVEROOM::SetCallback(nullptr); to remove the callback listener.
    3. Call UnInitSDK to uninitialize the SDK.

API Reference

MethodDescription
SetCallbackSets the callback listener.
InitSDKInitializes the SDK.
LoginRoomLogs in to a room.
GetMaxRecordCountGets the maximum number of streams the SDK supports recording simultaneously.
StartRecordSingleStreamStarts single-stream recording.
StopRecordSingleStreamStops single-stream recording.
SetMuxerOutTypeSets how the recorded data is output.
SetLogDirAndSizeSets the SDK log path and the size of a single log file.
SetConfigSets the configuration information.
SetMuxerCacheSizeSets the cache size before files are written to disk (64 KB~1 MB).
GetSDKVersionGets the SDK version.
UnInitSDKUninitializes the SDK.
LogoutRoomExits a room.

Previous

Integrate the SDK

Next

Record Mixed Stream

On this page

Back to top