setVideoEnabled method

Future<void> setVideoEnabled(
  1. bool enabled
)

Enables/disables local video stream based on enabled.

Implementation

Future<void> setVideoEnabled(bool enabled) async {
  Log.debug('setVideoEnabled($enabled)', '$runtimeType');

  switch (videoState.value) {
    case LocalTrackState.disabled:
    case LocalTrackState.disabling:
      if (enabled) {
        videoState.value = LocalTrackState.enabling;
        try {
          await _room?.enableVideo(MediaSourceKind.device);
          videoState.value = LocalTrackState.enabled;

          final List<LocalMediaTrack> tracks = await MediaUtils.getTracks(
            video: VideoPreferences(
              device: videoDevice.value?.deviceId(),
              facingMode: videoDevice.value == null ? FacingMode.user : null,
            ),
          );
          tracks.forEach(_addLocalTrack);
        } on MediaStateTransitionException catch (_) {
          // No-op.
        } on LocalMediaInitException catch (e) {
          videoState.value = LocalTrackState.disabled;
          if (e.message().contains('Permission denied')) {
            _notifications.add(CameraPermissionDeniedNotification());
          } else {
            addError('enableVideo() call failed with $e');
            rethrow;
          }
        } catch (e) {
          addError('enableVideo() call failed with $e');
          videoState.value = LocalTrackState.disabled;
          rethrow;
        }
      }
      break;

    case LocalTrackState.enabled:
    case LocalTrackState.enabling:
      if (!enabled) {
        videoState.value = LocalTrackState.disabling;
        try {
          await _room?.disableVideo(MediaSourceKind.device);
          _removeLocalTracks(MediaKind.video, MediaSourceKind.device);
          videoState.value = LocalTrackState.disabled;
        } on MediaStateTransitionException catch (_) {
          // No-op.
        } catch (e) {
          addError('disableVideo() call failed with $e');
          videoState.value = LocalTrackState.enabled;
          rethrow;
        }
        break;
      }
  }
}