unpublishTrack method

  1. @override
Future<void> unpublishTrack(
  1. String trackSid,
  2. {bool notify = true}
)
override

Unpublish a LocalTrackPublication that's already published by this LocalParticipant.

Implementation

@override
Future<void> unpublishTrack(String trackSid, {bool notify = true}) async {
  logger.finer('Unpublish track sid: $trackSid, notify: $notify');
  final pub = trackPublications.remove(trackSid);
  if (pub == null) {
    logger.warning('Publication not found $trackSid');
    return;
  }
  await pub.dispose();

  final track = pub.track;
  if (track != null) {
    if (room.roomOptions.stopLocalTrackOnUnpublish) {
      await track.stop();
    }

    final sender = track.transceiver?.sender;
    if (sender != null) {
      try {
        await room.engine.publisher?.pc.removeTrack(sender);
        if (track is LocalVideoTrack) {
          track.simulcastCodecs.forEach((key, simulcastTrack) async {
            await room.engine.publisher?.pc
                .removeTrack(simulcastTrack.sender!);
          });
        }
      } catch (_) {
        logger.warning('[$objectId] rtc.removeTrack() did throw ${_}');
      }

      // doesn't make sense to negotiate if already disposed
      if (!isDisposed) {
        // manual negotiation since track changed
        await room.engine.negotiate();
      }
    }

    // did unpublish
    await track.onUnpublish();
    await room.applyAudioSpeakerSettings();
  }

  if (notify) {
    [events, room.events].emit(LocalTrackUnpublishedEvent(
      participant: this,
      publication: pub,
    ));
  }

  await pub.dispose();
}