setSourceEnabled method

Future<LocalTrackPublication<LocalTrack>?> setSourceEnabled(
  1. TrackSource source,
  2. bool enabled,
  3. {bool? captureScreenAudio,
  4. AudioCaptureOptions? audioCaptureOptions,
  5. CameraCaptureOptions? cameraCaptureOptions,
  6. ScreenShareCaptureOptions? screenShareCaptureOptions}
)

A convenience method to publish a track for a specific TrackSource. This is the recommended method to publish tracks.

Implementation

Future<LocalTrackPublication?> setSourceEnabled(
    TrackSource source, bool enabled,
    {bool? captureScreenAudio,
    AudioCaptureOptions? audioCaptureOptions,
    CameraCaptureOptions? cameraCaptureOptions,
    ScreenShareCaptureOptions? screenShareCaptureOptions}) async {
  logger.fine('setSourceEnabled(source: $source, enabled: $enabled)');
  final publication = getTrackPublicationBySource(source);
  if (publication != null) {
    if (enabled) {
      await publication.unmute();
    } else {
      if (source == TrackSource.screenShareVideo) {
        await unpublishTrack(publication.sid);
      } else {
        await publication.mute();
      }
    }
    await room.applyAudioSpeakerSettings();
    return publication;
  } else if (enabled) {
    if (source == TrackSource.camera) {
      CameraCaptureOptions captureOptions = cameraCaptureOptions ??
          room.roomOptions.defaultCameraCaptureOptions;
      final track = await LocalVideoTrack.createCameraTrack(captureOptions);
      return await publishVideoTrack(track);
    } else if (source == TrackSource.microphone) {
      AudioCaptureOptions captureOptions =
          audioCaptureOptions ?? room.roomOptions.defaultAudioCaptureOptions;
      final track = await LocalAudioTrack.create(captureOptions);
      return await publishAudioTrack(track);
    } else if (source == TrackSource.screenShareVideo) {
      ScreenShareCaptureOptions captureOptions = screenShareCaptureOptions ??
          room.roomOptions.defaultScreenShareCaptureOptions;

      /// When capturing chrome table audio, we can't capture audio/video
      /// track separately, it has to be returned once in getDisplayMedia,
      /// so we publish it twice here, but only return videoTrack to user.
      if (captureScreenAudio != null) {
        captureOptions = captureOptions.copyWith(captureScreenAudio: true);
        final tracks = await LocalVideoTrack.createScreenShareTracksWithAudio(
            captureOptions);
        LocalTrackPublication<LocalVideoTrack>? publication;
        for (final track in tracks) {
          if (track is LocalVideoTrack) {
            publication = await publishVideoTrack(track);
          } else if (track is LocalAudioTrack) {
            await publishAudioTrack(track);
          }
        }

        /// just return the video track publication
        return publication;
      }
      final track =
          await LocalVideoTrack.createScreenShareTrack(captureOptions);
      return await publishVideoTrack(track);
    }
  }
  return null;
}