美文网首页Android直播
在exoplayer添加调试的自定义码率自适应算法

在exoplayer添加调试的自定义码率自适应算法

作者: 1700 | 来源:发表于2019-06-09 21:32 被阅读0次
  1. exoplayer-release-v2/demos/main/src/main/java/com/google/android/exoplayer2/Demo/PlayerActivity.java

调用处修改成自己的trackSelection算法

import com.google.android.exoplayer2.trackselection.OptAdaptiveTrackSelection;

...

private void initializePlayer() {
...
      TrackSelection.Factory trackSelectionFactory;
      String abrAlgorithm = intent.getStringExtra(ABR_ALGORITHM_EXTRA);
      if (abrAlgorithm == null || ABR_ALGORITHM_DEFAULT.equals(abrAlgorithm)) {
        -trackSelectionFactory = new AdaptiveTrackSelection.Factory();
        +trackSelectionFactory = new OptAdaptiveTrackSelection.Factory();
      } else if (ABR_ALGORITHM_RANDOM.equals(abrAlgorithm)) {
        trackSelectionFactory = new RandomTrackSelection.Factory();
      } else {
        showToast(R.string.error_unrecognized_abr_algorithm);
        finish();
        return;
      }
...
}
  1. exoplayer-release-v2/library/core/src/main/java/com/google/android/exoplayer2/Trackselection/OptAdaptiveTrackSelection.java

参考同一目录的AdaptiveTrackSelection.java,copy成自己的OptAdaptiveTrackSelection.java

关键函数是updateSelectedTrack,AdaptiveTrackSelection的该函数内容如下

@Override
public void updateSelectedTrack(
long playbackPositionUs,
long bufferedDurationUs,
long availableDurationUs,
List<? extends MediaChunk> queue,
MediaChunkIterator[] mediaChunkIterators) {
  long nowMs = clock.elapsedRealtime();
  // Update the estimated track bitrates.
  trackBitrateEstimator.getBitrates(formats, queue, mediaChunkIterators, trackBitrates);
  // Make initial selection
  if (reason == C.SELECTION_REASON_UNKNOWN) {
    reason = C.SELECTION_REASON_INITIAL;
    selectedIndex = determineIdealSelectedIndex(nowMs, trackBitrates);
    return;
  }

  // Stash the current selection, then make a new one.
  int currentSelectedIndex = selectedIndex;
  selectedIndex = determineIdealSelectedIndex(nowMs, trackBitrates);
  if (selectedIndex == currentSelectedIndex) {
    return;
  }

  if (!isBlacklisted(currentSelectedIndex, nowMs)) {
    // Revert back to the current selection if conditions are not suitable for switching.
    Format currentFormat = getFormat(currentSelectedIndex);
    Format selectedFormat = getFormat(selectedIndex);
    if (selectedFormat.bitrate > currentFormat.bitrate
      && bufferedDurationUs < minDurationForQualityIncreaseUs(availableDurationUs)) {
      // The selected track is a higher quality, but we have insufficient buffer to safely switch
      // up. Defer switching up for now.
      selectedIndex = currentSelectedIndex;
    } else if (selectedFormat.bitrate < currentFormat.bitrate
      && bufferedDurationUs >= maxDurationForQualityDecreaseUs) {
      // The selected track is a lower quality, but we have sufficient buffer to defer switching
      // down for now.
      selectedIndex = currentSelectedIndex;
    }
  }

  // If we adapted, update the trigger.
  if (selectedIndex != currentSelectedIndex) {
  reason = C.SELECTION_REASON_ADAPTIVE;
  }
}

对于DASH,audio一般只有一个码率,走FixedSelection,video有多个码率,根据PlayerActivity中使用的自适应算法来选择,我们换成了OptAdaptiveSelection


image.png

近几个月看到有这么一条提交,我之前加时还没有,先把我之前的记录下来


image.png

相关文章

网友评论

    本文标题:在exoplayer添加调试的自定义码率自适应算法

    本文链接:https://www.haomeiwen.com/subject/cvhoxctx.html