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:
Before implementing the functions, please make sure:
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.
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];
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];
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 "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 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];
}
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];
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];
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.
...
});
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).