restartTrack method

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

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

Implementation

Future<void> restartTrack([
  LocalTrackOptions? 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 if not already stopped...
  await stop();

  // create new track with options
  final newStream = await LocalTrack.createStream(currentOptions);
  final newTrack = newStream.getTracks().first;

  // replace track on sender
  try {
    await sender?.replaceTrack(newTrack);
    if (this is LocalVideoTrack) {
      var videoTrack = this as LocalVideoTrack;
      await videoTrack.replaceTrackForMultiCodecSimulcast(newTrack);
    }
  } catch (error) {
    logger.severe('RTCRtpSender.replaceTrack() did throw $error');
  }

  // set new stream & track to this object
  updateMediaStreamAndTrack(newStream, newTrack);

  // mark as started
  await start();

  // notify so VideoView can re-compute mirror mode if necessary
  events.emit(LocalTrackOptionsUpdatedEvent(
    track: this,
    options: currentOptions,
  ));
}