logo
Video Call
On this page

Publish Multiple Streams Simultaneously

2024-01-02

Feature Overview

Express SDK provides the ability to publish multiple streams simultaneously. When the following situations occur in the developer's business, it is recommended to use the SDK's publish multiple streams feature:

  • Game streamers publish camera video on the main stream and screen capture video on the second stream.
  • Outdoor streamers publish front camera video on the main stream and rear camera video on the second stream.
Notes

Currently the SDK supports a maximum of 4 publish channels. Versions before 2.14.0 default to a maximum of 2 publish channels. If you need to support more publish channels, please contact ZEGO Technical Support for special packaging.

Example Source Code Download

Please refer to Download Example Source Code to get the source code.

For related source code, please check the files in the "/ZegoExpressExample/AdvancedStreaming/src/main/java/im/zego/publishingmultiplestreams" directory in the downloaded example source code.

Prerequisites

Before implementing the publish multiple streams feature, please ensure:

Usage Steps

Warning
  • Publishing streams other than the main stream cannot directly obtain camera data.
  • By default, non-main streams can only publish audio data. If you need to publish audio and video data, you need to enable the custom video capture feature.
  • This article will introduce the implementation method of publishing auxiliary stream. The implementation of publishing other non-main streams is similar.

1 Create ZegoExpressEngine Engine

Please refer to Quick Start - Implementing Video Call for "Create Engine".

ZegoEngineProfile profile = new ZegoEngineProfile();
profile.appID = appID;
profile.appSign = appSign;
profile.scenario = ZegoScenario.DEFAULT;
profile.application = getApplication();
engine = ZegoExpressEngine.createEngine(profile, null);

2 (Optional) Set Custom Video Capture Configuration

Call the ZegoCustomVideoCaptureConfig interface to create a custom video capture object, set the property bufferType, and provide the video frame data type to the SDK; call the interface enableCustomVideoCapture to enable the custom video capture feature. For details, please refer to Custom Video Capture.

Notes

Due to the diversity of Android capture, the SDK supports multiple video frame data types bufferType, and developers need to inform the SDK of the data type used.

ZegoCustomVideoCaptureConfig videoCaptureConfig = new ZegoCustomVideoCaptureConfig();
// Select RAW_DATA type video frame data
videoCaptureConfig.bufferType = ZegoVideoBufferType.RAW_DATA;
engine.enableCustomVideoCapture(true, videoCaptureConfig, ZegoPublishChannel.AUX);

3 (Optional) Set Auxiliary Publisher Custom Video Capture Callback

Call the setCustomVideoCaptureHandler to set the custom video capture notification callback, and implement the onStart and onStop custom capture callback methods.

// Set itself as the custom video capture callback object
sdk.setCustomVideoCaptureHandler(new IZegoCustomVideoCaptureHandler() {
    @Override
    public void onStart(ZegoPublishChannel channel) {
        // After receiving the callback, developers need to execute business logic related to starting video capture, such as turning on the camera, etc.
        ...
    }
    @Override
    public void onStop(ZegoPublishChannel channel) {
        // After receiving the callback, developers need to execute business logic related to stopping video capture, such as turning off the camera, etc.
        ...
    }
});

4 Call Auxiliary Publisher Methods to Implement Preview and Publish Stream

After logging in to the Room, call the start preview interface startPreview or start publishing stream interface startPublishingStream to trigger the onStart of 3 Set Auxiliary Publisher Custom Video Capture Callback. When the onStart callback is triggered, developers can start capturing video frame data.

When stopping publishing stream stopPublishingStream and stopping preview stopPreview, the onStop callback of 3 Set Auxiliary Publisher Custom Video Capture Callback will be triggered. At this time, developers should stop capturing video data.

/**
*  Start preview, set local preview view, view mode uses SDK default mode, proportional scaling to fill the entire View
*  The following preview_view is a SurfaceView/TextureView/SurfaceTexture object on the UI interface
*/
engine.startPreview(new ZegoCanvas(preview_view), ZegoPublishChannel.AUX);

/** Start publishing stream */
engine.startPublishingStream("stream1", ZegoPublishChannel.AUX);

5 (Optional) Send Captured Video Frame Data to SDK

After the onStart callback is triggered, developers can call sendCustomVideoCaptureRawData or sendCustomVideoCaptureTextureData to send the captured video data to the SDK.

  • Call sendCustomVideoCaptureRawData

    engine.sendCustomVideoCaptureRawData(buffer, data.length, param, System.currentTimeMillis(), ZegoPublishChannel.AUX);
  • Call sendCustomVideoCaptureTextureData

    engine.sendCustomVideoCaptureTextureData(textureID, width, height, System.currentTimeMillis(), egoPublishChannel.AUX);

6 Set Auxiliary Publisher Event Callbacks

Call the auxiliary publisher's setEventHandler interface to set event callbacks for the auxiliary publisher to receive notifications such as "publish stream status change callback", "publish stream quality callback", "publish stream first frame callback", etc.

engine.setEventHandler(new IZegoEventHandler(){

    // Other overridden callbacks
    ...

    @Override
    public void onPublisherStateUpdate(String streamID, ZegoPublisherState state, int errorCode, JSONObject extendedData){
        // Logic for corresponding publish action result
        ...
    }
    @Override
    public void onPublisherQualityUpdate(String streamID, ZegoPublishStreamQuality quality){
        // Business logic such as corresponding publish mid-stream quality related display
        ...
    }
    @Override
    public void onPublisherCapturedVideoFirstFrame(ZegoPublishChannel channel){
        // Notification that SDK received video data for the first time
        ...
    }
    @Override
    public void onPublisherVideoSizeChanged(int width, int height, ZegoPublishChannel channel){
        // Developers can remove the logic such as UI mask space for displaying video in this callback
    }
    @Override
    public void onPublisherRelayCDNStateUpdate(String streamID, ArrayList<ZegoStreamRelayCDNInfo> infoList){
        // Notification of stream relay to CDN
    }

    // Other overridden callbacks
    ...

});

FAQ

  1. When will onStart be called back?

    When custom video capture is enabled, the SDK triggers when starting to publish stream startPublishingStream or starting preview startPreview.

  2. Does it support publishing more than 4 streams simultaneously?

    To cooperate with the real-time signaling feature, the SDK currently defaults to a maximum of 4 publish channels, but versions before 2.14.0 default to a maximum of 2 publish channels. If you need to support more publish channels, please contact ZEGO Technical Support for special packaging.

Previous

Multi-Source Capture

Next

Supplemental Enhancement Information (SEI)