selectVariantTrack(track, clearBuffer = false, safeMargin = 0) {
    const selectMediaSourceMode = () => {
      const variant = this.manifest_.variants.find(
          (variant) => variant.id == track.id);
      if (!variant) {
        if (!this.isRemotePlayback()) {
          shaka.log.error('No variant with id', track.id);
        }
        return;
      }

      // Double check that the track is allowed to be played. The track list
      // should only contain playable variants, but if restrictions change and
      // |selectVariantTrack| is called before the track list is updated, we
      // could get a now-restricted variant.
      if (!shaka.util.StreamUtils.isPlayable(variant)) {
        shaka.log.error('Unable to switch to restricted track', track.id);
        return;
      }

      const active = this.streamingEngine_.getCurrentVariant();
      if (this.config_.abr.enabled && (active.video != variant.video ||
          (active.audio && variant.audio &&
          active.audio.language == variant.audio.language &&
          active.audio.channelsCount == variant.audio.channelsCount &&
          active.audio.label == variant.audio.label))) {
        shaka.log.alwaysWarn('Changing tracks while abr manager is enabled ' +
                             'will likely result in the selected track ' +
                             'being overridden. Consider disabling abr ' +
                             'before calling selectVariantTrack().');
      }

      if (this.isRemotePlayback()) {
        this.switchVariant_(
            variant, /* fromAdaptation= */ false,
            /* clearBuffer= */ false, /* safeMargin= */ 0);
      } else {
        this.switchVariant_(
            variant, /* fromAdaptation= */ false,
            clearBuffer || false, safeMargin || 0);
      }

      // Workaround for
      // https://github.com/shaka-project/shaka-player/issues/1299
      // When track is selected, back-propagate the language to
      // currentAudioLanguage_.
      this.currentAdaptationSetCriteria_.configure({
        language: variant.language,
        role: (variant.audio && variant.audio.roles &&
          variant.audio.roles[0]) || '',
        channelCount: variant.audio && variant.audio.channelsCount ?
          variant.audio.channelsCount : 0,
        hdrLevel: variant.video && variant.video.hdr ? variant.video.hdr : '',
        spatialAudio: variant.audio && variant.audio.spatialAudio ?
          variant.audio.spatialAudio : false,
        videoLayout: variant.video && variant.video.videoLayout ?
          variant.video.videoLayout : '',
        audioLabel: variant.audio && variant.audio.label ?
          variant.audio.label : '',
        videoLabel: '',
        codecSwitchingStrategy: this.config_.mediaSource.codecSwitchingStrategy,
        audioCodec: variant.audio && variant.audio.codecs ?
          variant.audio.codecs : '',
        activeAudioCodec: active.audio && active.audio.codecs ?
          active.audio.codecs : '',
        activeAudioChannelCount: active.audio && active.audio.channelsCount ?
          active.audio.channelsCount : 0,
        preferredAudioCodecs: this.config_.preferredAudioCodecs,
        preferredAudioChannelCount: this.config_.preferredAudioChannelCount,
      });

      // Update AbrManager variants to match these new settings.
      this.updateAbrManagerVariants_();
    };
    const selectSrcEqualsMode = () => {
      if (!track.originalAudioId) {
        return;
      }
      if (this.video_ && this.video_.audioTracks) {
        // Safari's native HLS won't let you choose an explicit variant, though
        // you can choose audio languages this way.
        const audioTracks = Array.from(this.video_.audioTracks);
        for (const audioTrack of audioTracks) {
          if (shaka.util.StreamUtils.html5TrackId(audioTrack) == track.id) {
            // This will reset the "enabled" of other tracks to false.
            this.switchHtml5Track_(audioTrack);
            return;
          }
        }
      }
    };
    if (this.manifest_ && this.playhead_) {
      selectMediaSourceMode();
      // When using MSE + remote we need to set tracks for both MSE and native
      // apis so that synchronization is maintained.
      if (!this.isRemotePlayback()) {
        return;
      }
    }
    selectSrcEqualsMode();
  }
Select a specific variant track to play.  <code>track</code> should come
from a call to <code>getVariantTracks</code>. If <code>track</code> cannot
be found, this will be a no-op. If the player has not loaded content, this
will be a no-op.

<p>
Changing variants will take effect once the currently buffered content has
been played. To force the change to happen sooner, use
<code>clearBuffer</code> with <code>safeMargin</code>. Setting
<code>clearBuffer</code> to <code>true</code> will clear all buffered
content after <code>safeMargin</code>, allowing the new variant to start
playing sooner.

<p>
Note that <code>AdaptationEvents</code> are not fired for manual track
selections.

@param {shaka.extern.Track} track
@param {boolean=} clearBuffer
@param {number=} safeMargin Optional amount of buffer (in seconds) to
  retain when clearing the buffer. Useful for switching variant quickly
  without causing a buffering event. Defaults to 0 if not provided. Ignored
  if clearBuffer is false. Can cause hiccups on some browsers if chosen too
  small, e.g. The amount of two segments is a fair minimum to consider as
  safeMargin value.
@export