logo
On this page

Multi-Source Capture

2024-09-20

Feature overview

Multi-source capture is used to manage audio and video source configurations for each channel. Through this capability, you can flexibly and quickly implement real-time interaction of different audio and video content, such as screen sharing, audio mixing, and other functions.

The main capabilities and limitations of multi-source capture are as follows:

  • Publishing stream channels support setting multiple audio and video sources, but the same audio and video source can only be occupied by one channel.
  • The main channel's audio and video sources support dynamic switching and also support audio mixing capabilities. The auxiliary channel only supports specifying capture sources before publishing stream and does not support mixing.
  • When capturing screen, only iOS and Android platforms support capturing video and audio at the same time; other platforms only support capturing video. If you need to capture audio, please implement relevant logic yourself.

Example source code

For relevant source code, please refer to the official website example source code. For details, please refer to Run Example Code to obtain the source code.

Prerequisites

Before using the multi-source capture feature, please ensure:

Implementation process

Warning

The Voice Call product only supports setting audio capture sources.

1 Create ZegoExpressEngine engine

For the process of creating ZegoExpressEngine engine, please refer to Create Engine for implementing video calls.

// Import ZegoExpressEngine
const zgEngine = window.require('zego-express-engine-electron/ZegoExpressEngine');
const zgDefines = window.require('zego-express-engine-electron/ZegoExpressDefines');

// Specify using live streaming scenario (please fill in the scenario suitable for your business according to the actual situation)
const profile = {
appID : xxx,
appSign : "xxx",
scenario : zgDefines.ZegoScenario.Default
};

zgEngine.createEngine(profile)
.then(() => {
    console.log("init succeed")
}).catch((e) => {
    console.log("init failed", e)
});

2 Set audio and video capture sources

  1. Call the setVideoSource interface to set the video capture source.
/** Use camera as video capture source */
zgEngine.setVideoSource(zgDefines.ZegoVideoSourceType.Camera, 0, zgDefines.ZegoPublishChannel.Main)
Note
  • If you need to use custom video capture as the video capture source, you need to set the video capture source type to Custom, call the enableCustomVideoCapture interface to set the video frame data type to be sent, and then register a custom video capture plugin registerCustomVideoCapturePlugin to the SDK.
  • If you need to use a media player as the video capture source, you need to call the createMediaPlayer interface to create a media player first, then call the setVideoSource interface to set the video capture source type to Player and set the media player instance index used by the video capture source, and then use the newly created media player to load resources and play.
  • If you need to use screen sharing as the video capture source, you need to set the video capture source type to ScreenCapture and enable screen sharing to push screen sharing video data to the SDK. For details, please refer to Screen Sharing .
  1. Call the setAudioSource interface to set the audio capture source.

    • Set audio capture source only

      /** Use microphone as audio capture source */
      zgEngine.setAudioSource(zgDefines.ZegoAudioSourceType.Microphone, zgDefines.ZegoPublishChannel.Main)
    • Set audio capture source and set mixing configuration at the same time

      let audioSourceMixConfig;
      /** Mix sound played by system sound card*/
      audioSourceMixConfig.enableMixSystemPlayout = true;
      /** Mix sound played by SDK*/
      audioSourceMixConfig.enableMixEnginePlayout = true;
      
      /** Mix sound played by media player*/
      let mediaplayerList = []
      mp = zgEngine.createMediaPlayer();
      mediaplayerList.push(mp.getIndex())
      audioSourceMixConfig.mediaPlayerIndexList = mediaplayerList;
      
      /** Mix sound played by audio effect player*/
      let audioplayerList = []
      audioEffectPlayer = zgEngine.createAudioEffectPlayer();
      audioplayerList.push(audioEffectPlayer.getIndex())
      audioSourceMixConfig.audioEffectPlayerIndexList = audioplayerList ;
      
      /** Use microphone as audio capture source and set mixing configuration */
      zgEngine.setAudioSourceWithConfig(zgDefines.ZegoAudioSourceType.Microphone, audioSourceMixConfig);
Note
  • If you need to use custom audio capture as the audio capture source, you need to set the audio capture source type to Custom and register an audio capture plugin registerCustomAudioCapturePlugin to the SDK.
  • If you need to use a media player as the audio capture source, you need to set the audio capture source type to MediaPlayer, call the createMediaPlayer interface to create a media player, and then use the newly created media player to load resources and play.

3 Login to room and publish stream

For the process of logging in to the room and publishing stream, please refer to Login to Room and Publish Stream for implementing video calls.

4 (Optional) Switch audio and video capture sources

  1. Call the setVideoSource interface to switch the video capture source.
/** Use screen sharing as video capture source */
zgEngine.setVideoSource(zgDefines.ZegoVideoSourceType.ScreenCapture, 0, zgDefines.ZegoPublishChannel.Main)
  1. Call the setAudioSource interface to switch the audio capture source.
/** Use media player as audio capture source */
zgEngine.setAudioSource(zgDefines.ZegoAudioSourceType.MediaPlayer, zgDefines.ZegoPublishChannel.Main)

5 End publishing stream

For the process of ending publishing stream, please refer to Stop Publishing and Playing Stream for implementing video calls.

/** Stop publishing stream*/
zgEngine.stopPublishingStream(zgDefines.ZegoPublishChannel.Main);

FAQ

  1. How to capture screen video and system sound?

For capturing screen video and system sound, please refer to Screen Sharing.

  1. Does it support setting multiple media players?

Yes, but note that the same player instance can only be occupied by one channel.

  1. What are the limitations when combining video sources and audio sources?

    The limitations for combining video sources and audio sources are as follows:

    1. The main channel cannot use media player's video or audio source.
    2. When the auxiliary channel uses a media player as the audio and video source, the main channel needs to use and start the microphone device (enableMicrophone). If you do not need microphone sound on the main channel, you can disable audio publishing through mutePublishStreamAudio to keep the microphone started.
    3. Only when the main channel uses camera video source or microphone audio source, the auxiliary channel copying the main channel video source or audio source can take effect.
    4. The main channel's audio source cannot be set to None, otherwise audio cannot be rendered.

Previous

Using Token Authentication

Next

Traffic Control