Documentation
Low_Latency_Live Live Streaming
Documentation
Demo APP
SDK Center
API Center
FAQ
Code Market
Console
Sign Up
Log In
中文站 English
  • Documentation
  • Live Streaming
  • Guides
  • Advanced features
  • Publishing multiple streams

Publish multiple video streams

Last updated:2023-05-17 19:00

Introduction

ZEGOCLOUD’s SDKs provide the ability to publish multiple streams, two streams currently, simultaneously. Correspondingly, the "ZegoExpressEngine" has two publish channels: the MAIN channel and the AUX channel.

Here are some typical use cases for this feature:

  • Video Game Live Streaming: publish two video streams simultaneously, one captured from the camera and the other from the screen.
  • Outdoor Live Streaming: stream live video from the front and rear cameras simultaneously.

Prerequisites

Before implementing the functions, please make sure:

  • A project has been created in ZEGOCLOUD Console and applied for a valid AppID and AppSign. For details, please refer to Console - How to view project information .
  • ZEGO Express SDK has been integrated into the project to implement basic real-time audio and video functions. For details, please refer to Integrate the SDK and Implement a basic video call.
  • By default, when you use a publish channel other than the MAIN channel, the SDK can not capture video data directly from a camera, but only audio.

  • If you want to publish a video stream using a publish channel other than the MAIN channel, you need to enable custom video capture for the channel.

Create a ZegoExpressEngine Instance

ZegoEngineProfile *profile = [ZegoEngineProfile new];
// The AppID value you get from the ZEGO Admin console.
profile.appID = appID;  
// Use the general scenario.
profile.scenario = ZegoScenarioGeneral; 
// Create a ZegoExpressEngine instance and set eventHandler to [self]. If eventHandler is set to [nil], no callback will be received. You can set up the event handler later by calling the [-setEventHandler:] method.
self.engine = [ZegoExpressEngine createEngineWithProfile:profile eventHandler:self];

(Optional) Enable Custom Video Capture for the AUX Channel

Enable Custom Video Capture for the AUX Channel

If you want to publish a video stream using the AUX channel, you need to enable custom video capture for the AUX channel. First, create a ZegoCustomVideoCaptureConfig object and configure the bufferType attribute to specify the data type to be used to send the captured video frame data to the SDK. Then, call enableCustomVideoCapture to enable custom video capture for the AUX channel.

Currently the SDK supports two video buffer types for iOS: "ZegoVideoBufferTypeCVPixelBuffer" for the CVPixelBuffer data type, and "ZegoVideoBufferTypeGLTexture2D" for the GLTexture2D data type.

ZegoCustomVideoCaptureConfig *captureConfig = [[ZegoCustomVideoCaptureConfig alloc] init];
// Set the data type of the captured video frame to CVPixelBuffer.
captureConfig.bufferType = ZegoVideoBufferTypeCVPixelBuffer;

[[ZegoExpressEngine sharedEngine] enableCustomVideoCapture:YES config:captureConfig channel:ZegoPublishChannelAux];

(Optional) Set up the Custom Video Capture Callback Handler for the AUX Channel

Set up the Custom Video Capture Callback Handler for the AUX Channel

If custom video capture is enabled, you need to set up an event handler to listen for and handle the callbacks related to custom video capture.

Set the Callback Handler Object

Set "ViewController" as the callback object of the custom video capture callbacks, which conforms to the ZegoCustomVideoCaptureHandler protocol.

@interface ViewController () <ZegoEventHandler, ZegoCustomVideoCaptureHandler>

    ......

@end

Call setCustomVideoCaptureHandler to set up an event handler to listen for and handle the callbacks related to custom video capture.

// Set the engine itself as the callback handler object.
[[ZegoExpressEngine sharedEngine] setCustomVideoCaptureHandler:self];

Implement the Callback Handler Methods

Implement the callback handler methods for the callbacks onStart and onStop.

// Note: This callback is not called in the main thread. Please switch to the main thread if you need to operate the UI objects. 
- (void)onStart {

    // On receiving the onStart callback, you can execute the tasks to start up your customized video capture process and start sending video frame data to the SDK.

    // Here is an example of turning on the video capture device.
    [self.captureDevice startCapture];
}

// Note: This callback is not called in the main thread. Please switch to the main thread if you need to operate the UI objects. 
- (void)onStop {

    // On receiving the onStop callback, you can execute the tasks to stop your customized video capture process and stop sending video frame data to the SDK.

    // Here is an example of turning off the video capture device.
    [self.captureDevice stopCapture];
}

Start Previewing or Publishing the AUX Stream

Call startPreview to start the local preview or call startPublishingStream to start the stream publishing for the AUX channel.

[self.engine startPreview:mainPreviewCanvas channel:ZegoPublishChannelAux];
[self.engine startPublishingStream:self.auxStreamID channel:ZegoPublishChannelAux];

(Optional) Send the Captured Video Frame Data to the SDK

Send the Captured Video Frame Data to the SDK

If custom video capture is enabled, the callback onStart will be triggered when the stream preview or stream publishing is started. On receiving this callback, you can start the video capture process and then call sendCustomVideoCaptureTextureData or sendCustomVideoCapturePixelBuffer to send the captured video frame data to the SDK for the AUX channel.

[self.engine sendCustomVideoCaptureTextureData:textureID size:size timeStamp:timeStamp channel:ZegoPublishChannelAux];

or

[self.engine sendCustomVideoCapturePixelBuffer:data timeStamp:timeStamp channel:ZegoPublishChannelAux];

Set up the Callback Handler for the AUX Stream Publishing

Call setEventHandler to set up an event handler to listen for and handle the callbacks related to the stream publishing of the AUX channel, such as "onPublisherStateUpdate", "onPublisherQualityUpdate", and "onPublisherCapturedVideoFirstFrame".

[self.player setEventHandler:self];

- (void)onPublisherStateUpdate:(ZegoPublisherState)state errorCode:(int)errorCode extendedData:(nullable NSDictionary *)extendedData streamID:(NSString *)streamID{
    // Handle the status change of the stream publishing. 
        ...
};
- (void)onPublisherQualityUpdate:(ZegoPublishStreamQuality *)quality streamID:(NSString *)streamID{
    // Handle the stream publishing quality update. 
        ...
};
- (void)onPublisherCapturedVideoFirstFrame:(ZegoPublishChannel)channel{
    // Handle the event of the first video frame captured.
        ...
};
- (void)onPublisherVideoSizeChanged:(CGSize)size channel:(ZegoPublishChannel)channel{
    // Start displaying the video on the preview UI or adjust the view according to the updated resolution.
};
- (void)onPublisherRelayCDNStateUpdate:(NSArray<ZegoStreamRelayCDNInfo *> *)infoList streamID:(NSString *)streamID{
    // Handle the status update of stream relay to CDN.
};

// Overrides of other callback handler methods.
...

});

FAQ

  1. When will the callback onStart be triggered?

    If custom video capture is enabled, the onStart callback will be triggered when the local preview or stream publishing is started (by calling startPreview or startPublishingStream).

Page Directory
  • Free trial
  • 提交工单
    咨询集成、功能及报价等问题
    电话咨询
    400 1006 604
    Get Consulting
    Scan Wechat QR code