replaceTrack method

Future<void> replaceTrack(
  1. MediaStreamTrack track
)

Replaces the current track with a new one.

Implementation

Future<void> replaceTrack(MediaStreamTrack track) async {
  _logger.debug('replcaeTrack() ${track.toString()}');

  if (closed) {
    // Thus must be done here. Otherwise there is no chance to stop the given track.
    if (stopTracks) {
      try {
        track.stop();
      } catch (error) {}
    }
    throw 'closed';
  }

  // flutter_webrtc, как получить состояние? : )
  // else if (track != null && track.readyState == 'ended'))

  // Do nothing if this is the same track as the current handled one.
  if (track == this.track) {
    _logger.debug('replaceTrack() | same track, ignored.');

    return;
  }

  if (zeroRtpOnPause || paused) {
    await safeEmitAsFuture('@replacetrack', {
      'track': track,
    });
  }

  // Destroy the previous track.
  _destroyTrack();

  // Set the new track.
  track = track;

  // If this Producer was paused/resumed and the state of the new
  // track does not match, fix it.
  if (disableTrackOnPause) {
    if (!paused) {
      track.enabled = true;
    } else if (paused) {
      track.enabled = false;
    }
  }

  // Handle the effective track.
  _handleTrack();
}