Through the stream-mixing service, multiple published media streams can compose a single stream , which allows audiences to play just one stream to improve quality and reduce performance cost.
ZEGOCLOUD supports using stream mixing with three methods, manual stream mix, automatic stream mix, and full-automatic stream mix. The following table shows their differences:
Method | Manual |
Auto |
Full-Auto |
---|---|---|---|
Description | Customize the stream mixing tasks and content, including input stream, layout, etc. Supports mixing audio/video streams. |
In specified rooms, automatically mixing the audio streams. Only supports mixing the audio streams. |
In each room, automatically mix the audio streams. Only supports mixing the audio streams. |
Scenario | Co-hosting across rooms, video/audio streams mixing, and specified stream mixing, etc. |
Audio-only scenarios. |
Audio-only scenarios. |
Pros | Flexible and enable you to implement logic according to business needs. |
It reduces the complexity of integration, and does not required to manage the audio stream list of the specified rooms. |
With very low complexity of integration, and does not required to manage the in-room audio stream mixing task and audio stream list. |
Relation with the server | The client side initiates the stream mixing task and manages the stream list. |
The client side initiates the stream mixing task, and the ZEGO server automatically manages the in-room input stream list. |
The ZEGO server manages the stream mixing tasks and the in-room stream list. |
You can use stream mixing for various use cases. The following are some typical examples:
Before you begin, make sure you complete the following:
With the manual stream mixing, you can customize the stream mixing tasks. which is often used for multiple users to co-host an interactive live streaming event and co-hosting across rooms.
To implement the stream mixing with SDK or ZEGOCLOUD server API, refer to the Start stream mixing and Stop stream mixing.
The following describes how to implement the manual stream mixing in detail.
ZegoMixerTask
is a class defined in the SDK for configuring a stream mixing task. The configuration parameters include the input streams, the outputs, and others.
/// Stream mixing task object.
@interface ZegoMixerTask: NSObject
-(instancetype)init NS_UNAVAILABLE;
/// Construct a stream mixing task object using TaskID.
-(instancetype)initWithTaskID:(NSString *)taskID;
/// Task ID of the stream mixing task.
@property (nonatomic, copy, readonly) NSString *taskID;
/// Set up the audio configuration of the mixed stream.
-(void)setAudioConfig:(ZegoMixerAudioConfig *)audioConfig;
/// Set up the video configuration of the mixed stream.
-(void)setVideoConfig:(ZegoMixerVideoConfig *)videoConfig;
/// Set up the list of input streams.
-(void)setInputList:(NSArray<ZegoMixerInput *> *)inputList;
/// Set up the list of mixing outputs.
-(void)setOutputList:(NSArray<ZegoMixerOutput *> *)outputList;
/// Set up the watermark of the mixed stream.
-(void)setWatermark:(ZegoWatermark *)watermark;
/// Set up the background image of the mixed stream.
-(void)setBackgroundImageURL:(NSString *)backgroundImageURL;
@end
Create a stream mixing task object using the constructor initWithTaskID
, and then call the instance methods to set up the input and output parameters.
ZegoMixerTask *task = [[ZegoMixerTask alloc] initWithTaskID:@"task-1"];
// Save the stream mixing task object.
self.mixerTask = task;
The default audio bitrate is 48 Kbps.
[task setAudioConfig:[ZegoMixerAudioConfig defaultConfig]];
For each input stream, create a ZegoMixerInput
object.
ContentType
parameter of the input stream), there is no need to set the layout
parameter.A stream mixing task can have up to 3 mixing outputs.
In the following code example, the stream mixing task generates an output with the stream name output-stream
, which will be published to the ZEGO server.
NSArray<ZegoMixerOutput *> *outputArray = @[[[ZegoMixerOutput alloc] initWithTarget:@"output-stream"]];
[task setOutputList:outputArray];
If you need to receive the event notification related to the sound level of the mixed stream, set the enableSoundLevel
parameter to True
to listening for the event callback.
The SDK sends out the sound level of each stream through the onMixerSoundLevelUpdate
callback when you play the mixed stream.
After configuring the ZegoMixerTask
object, call the startMixerTask
method to start the stream mixing task. If the stream mixing task doesn't start successfully, handle the failure in the callback that returns the execution result.
/// Start the stream mixing task.
/// @param task: The stream mixing task object.
/// @param callback: The callback to return the result of starting up the task.
-(void)startMixerTask:(ZegoMixerTask *)task callback:(nullable ZegoMixerStartCallback)callback;
[self.engine startMixerTask:task callback:^(ZegoMixerStartResult * _Nonnull result) {
if (result.errorCode == 0) {
NSLog(@"Start mixing task success.");
} else {
NSLog(@"Start mixing task failed.");
}
}];
When you need to change the stream mixing task, for example, when the number of input streams increases or decrease, or the bitrate of the output stream needs to be adjusted, you can update the stream mixing task object with the new configurations and then call the startMixerTask
method again.
When updating the configuration of a stream mixing task, you must not change the taskID
.
To stop a stream mixing task, call the stopMixerTask
method, with the corresponding task ID passed to the taskID
parameter.
/// Stop the stream mixing task.
/// @param taskID: The taskID of the steam mixing task to be stopped.
-(void)stopMixerTask:(NSString *)taskID;
// Pass in the taskID of the steam mixing task to be stopped.
[self.engine stopMixerTask:self.mixerTask.taskID];
ZegoAutoMixerTask
is a class defined in the SDK for configuring a stream mixing task. The configuration parameters include the layout of input streams, the outputs, and others.
/// Stream mixing task object.
@interface ZegoAutoMixerTask : NSObject
/// Task ID of the stream mixing task.
@property (nonatomic, copy) NSString *taskID;
/// Room ID of the stream mixing task.
@property (nonatomic, copy) NSString *roomID;
/// Set up the audio configuration of the mixed stream.
@property (nonatomic, strong) ZegoMixerAudioConfig *audioConfig;
/// Set up the list of mixing outputs.
@property (nonatomic, strong) NSArray<ZegoMixerOutput *> *outputList;
/// Set up the event callback related sound level of the mixed stream.
@property (nonatomic, assign) BOOL enableSoundLevel;
@end
Create an auto stream mixing task object and then call the instance methods to set up the input and output parameters.
ZegoAutoMixerTask *task = [[ZegoAutoMixerTask alloc] init];
task.taskID = @"taskID1";
task.roomID = @"roomID1";
To set up the audio configuration of the auto mixed audio streams, call the ZegoMixerAudioConfig
method, including the audio bitrate, channels, codec ID, and audio mix mode.
ZegoMixerAudioConfig *audioConfig = [ZegoMixerAudioConfig init];
audioConfig.bitrate = 48;/** Audio bitrate, unit: kbps, use 48 kbps by default. This can't be changed after stream mixing starts. */
audioConfig.channel = ZegoAudioChannelMono;/** Audio channel, use ZegoAudioChannelMono by default. */
audioConfig.codecID = ZegoAudioCodecIDNormal;/** Codec ID, use ZEGO_AUDIO_CODEC_ID_DEFAULT by default.*/
audioConfig.mixMode = ZegoAudioMixModeRaw;/** Audio mix mode, use ZegoAudioMixModeRaw by default. */
[task setAudioConfig:audioConfig];
To modify the audio channel, set the channel
property accordingly. The following are the supported channels:
Enumerated value | Description | Scenario |
---|---|---|
ZegoAudioChannelUnknown | Unkown | - |
ZegoAudioChannelMono | Mono | Mono-only scenario. |
ZegoAudioChannelStereo | Stereo | Stereo scenario. |
To modify the codec ID, set the codecID
property accordingly. The following are the supported codec IDs:
Enumerated value | Description |
---|---|
ZegoAudioCodecIDDefault | default |
ZegoAudioCodecIDNormal | Normal |
ZegoAudioCodecIDNormal2 | Normal2 |
ZegoAudioCodecIDNormal3 | Normal3 |
ZegoAudioCodecIDLow | Low |
ZegoAudioCodecIDLow2 | Low2 |
ZegoAudioCodecIDLow3 | Low3 |
To modify the mix mode, set the mixMode
property accordingly. The following are the supported audio mix modes:
Enumerated value | Description | Scenario |
---|---|---|
ZegoAudioMixModeRaw | Default mode. | Scenarios with no special need for audio. |
ZegoAudioMixModeFocused | Focus mode. Highlights the sound of a stream in a multi-channel audio stream. | The scenario where the sound of a stream needs to be highlighted. |
To set up the auto mixing outputs, call the ZegoMixerOutput
method to set the output list.
And you can play the mixed stream according to the output-stream
of the outputs.
NSArray<ZegoMixerOutput *> *outputArray = @[[[ZegoMixerOutput alloc] initWithTarget:@"output-stream"]];/** Mixing output target, URL or streamID. */
[task setOutputList:outputArray];
```tOutputList:outputArray];
If you need to receive the event notification related to the sound level of the auto mixed stream, set the enableSoundLevel
parameter to True
to listening for the event callback.
The SDK sends out the sound level of each stream through the onAutoMixerSoundLevelUpdate
callback when you play the mixed stream.
After configuring the ZegoAutoMixerTask
object, call the startAutoMixerTask
method to start the stream mixing task.
And the SDK sends out the result notifications through the onAutoMixerSoundLevelUpdate
callback.
[[ZegoExpressEngine sharedEngine] startAutoMixerTask:task callback:^(int errorCode, NSDictionary * _Nullable extendedData) {
if (errorCode == 0) {
/// Auto stream mixing task started successfully.
}
}];
To stop a stream mixing task, call the stopAutoMixerTask
method.
stopAutoMixerTask
method to stop the auto stream mixing task first. (In order to avoid the situation that when a host has started the next auto stream mixing stream task and mixed his/her stream with other hosts, while the audience is still playing the output stream of the old auto stream mixing task.)// Pass in the task object of the steam mixing task to be stopped.
[[ZegoExpressEngine sharedEngine] stopAutoMixerTask:self.autoMixerTask callback:^(int errorCode) {
if(errorCode == 0) {
///Auto stream mixing task stopped successfully.
}
}];
To fully-automatical mix the audio streams in each room, contact the ZEGOCLOUD Technical Support.
Can I forward the mixed stream to a third-party CDN or even multiple CDNs?
Yes. To forward the mixed stream to a third-party CDN, specify the CDN URL in the target
property of the ZegoMixerOutput
object. The URL must be in RTMP format: rtmp://xxxxxxxx
.
To forward the mixed stream to multiple CDNs, create multiple ZegoMixerOutput
objects accordingly, and put them into the outputList
of the ZegoMixerTask
object.