restartTrack method Null safety

Future<void> restartTrack(
  1. [LocalVideoTrackOptions? options]
)

Restarts the track with new options. This is useful when switching between front and back cameras.

Implementation

Future<void> restartTrack([
  LocalVideoTrackOptions? options,
]) async {
  if (sender == null) throw TrackCreateException('could not restart track');
  if (options != null && currentOptions.runtimeType != options.runtimeType) {
    throw Exception('options must be a ${currentOptions.runtimeType}');
  }

  currentOptions = options ?? currentOptions;

  // stop the current track & stream
  await mediaStreamTrack.stop();
  await mediaStream.dispose();

  // create new track with options
  final newStream = await _createStream(currentOptions);
  final newTrack = newStream.getVideoTracks().first;

  // replace track on sender
  await sender?.replaceTrack(newTrack);

  // set new stream & track to this object
  setMediaStream(newStream);
  mediaStreamTrack = newTrack;
}