ExoPlayer 源碼分析 四 緩存策略

ExoPlayer 源碼分析 一 HLS 拉流及播放流程
ExoPlayer 源碼分析 二 類圖 & 名詞解釋
ExoPlayer 源碼分析 三 變速播放
ExoPlayer 源碼分析 四 緩存策略
ExoPlayer 源碼分析 五 碼率自適應(yīng)

本文基于ExoPlayer 2.13.2 。

ExoPlayer 通過 LoadControl 控制緩存:

LoadControl loadControl = new DefaultLoadControl(new DefaultAllocator(BUFFER_SEGMENT_SIZE));

ChunkSampleSource videoSampleSource = new ChunkSampleSource(videoChunkSource, loadControl,
    VIDEO_BUFFER_SEGMENTS * BUFFER_SEGMENT_SIZE, mainHandler, player,
    DemoPlayer.TYPE_VIDEO);

ChunkSampleSource audioSampleSource = new ChunkSampleSource(audioChunkSource, loadControl,
    AUDIO_BUFFER_SEGMENTS * BUFFER_SEGMENT_SIZE, mainHandler, player,
    DemoPlayer.TYPE_AUDIO);

SampleSource enable 時(shí)會(huì)向 loadControl 注冊,disable 時(shí)反注冊。

LoadControl 真正控制是否繼續(xù)緩存的方法是 update ,HlsSampleSource、ChunkSampleSource 都是通過這個(gè)方法確定是否繼續(xù)緩存。

DefaultLoadControl.update

@Override
public boolean update(Object loader, long playbackPositionUs, long nextLoadPositionUs,
    boolean loading) {
  // Update the loader state.
  int loaderBufferState = getLoaderBufferState(playbackPositionUs, nextLoadPositionUs);
  LoaderState loaderState = loaderStates.get(loader);
  boolean loaderStateChanged = loaderState.bufferState != loaderBufferState
      || loaderState.nextLoadPositionUs != nextLoadPositionUs || loaderState.loading != loading;
  if (loaderStateChanged) {
    loaderState.bufferState = loaderBufferState;
    loaderState.nextLoadPositionUs = nextLoadPositionUs;
    loaderState.loading = loading;
  }

  // Update the buffer state.
  int currentBufferSize = allocator.getTotalBytesAllocated();
  int bufferState = getBufferState(currentBufferSize);
  boolean bufferStateChanged = this.bufferState != bufferState;
  if (bufferStateChanged) {
    this.bufferState = bufferState;
  }

  // If either of the individual states have changed, update the shared control state.
  if (loaderStateChanged || bufferStateChanged) {
    updateControlState();
  }

  return nextLoadPositionUs != -1 && nextLoadPositionUs <= maxLoadStartPositionUs;
}

最終是通過計(jì)算 nextLoadPositionUs 是否小于 maxLoadStartPositionUs 且 nextLoadPositionUs 不等于 -1 來決定是否繼續(xù)緩存。

public static final int DEFAULT_LOW_WATERMARK_MS = 15000;
public static final int DEFAULT_HIGH_WATERMARK_MS = 30000;
public static final float DEFAULT_LOW_BUFFER_LOAD = 0.2f;
public static final float DEFAULT_HIGH_BUFFER_LOAD = 0.8f;

private static final int ABOVE_HIGH_WATERMARK = 0;
private static final int BETWEEN_WATERMARKS = 1;
private static final int BELOW_LOW_WATERMARK = 2;

LoadControl 在時(shí)間上和緩存占用比例上分別由兩個(gè)「水位」:低水位和高水位,達(dá)到高水位時(shí)暫停緩存,達(dá)到低水位時(shí)繼續(xù)緩存。
然后有兩個(gè)方法 getLoaderBufferState 和 getBufferState 分別從時(shí)間和緩存數(shù)據(jù)大小上來計(jì)算緩存狀態(tài)是否改變,如果改變則通過 updateControlState 更新 ControlState 。

private int getLoaderBufferState(long playbackPositionUs, long nextLoadPositionUs) {
  if (nextLoadPositionUs == -1) {
    return ABOVE_HIGH_WATERMARK;
  } else {
    long timeUntilNextLoadPosition = nextLoadPositionUs - playbackPositionUs;
    return timeUntilNextLoadPosition > highWatermarkUs ? ABOVE_HIGH_WATERMARK :
        timeUntilNextLoadPosition < lowWatermarkUs ? BELOW_LOW_WATERMARK :
        BETWEEN_WATERMARKS;
  }
}

private int getBufferState(int currentBufferSize) {
  float bufferLoad = (float) currentBufferSize / targetBufferSize;
  return bufferLoad > highBufferLoad ? ABOVE_HIGH_WATERMARK
      : bufferLoad < lowBufferLoad ? BELOW_LOW_WATERMARK
      : BETWEEN_WATERMARKS;
}

private void updateControlState() {
  boolean loading = false;
  boolean haveNextLoadPosition = false;
  int highestState = bufferState;
  for (int i = 0; i < loaders.size(); i++) {
    LoaderState loaderState = loaderStates.get(loaders.get(i));
    loading |= loaderState.loading;
    haveNextLoadPosition |= loaderState.nextLoadPositionUs != -1;
    highestState = Math.max(highestState, loaderState.bufferState);
  }

  fillingBuffers = !loaders.isEmpty() && (loading || haveNextLoadPosition)
      && (highestState == BELOW_LOW_WATERMARK
      || (highestState == BETWEEN_WATERMARKS && fillingBuffers));
  if (fillingBuffers && !streamingPrioritySet) {
    NetworkLock.instance.add(NetworkLock.STREAMING_PRIORITY);
    streamingPrioritySet = true;
    notifyLoadingChanged(true);
  } else if (!fillingBuffers && streamingPrioritySet && !loading) {
    NetworkLock.instance.remove(NetworkLock.STREAMING_PRIORITY);
    streamingPrioritySet = false;
    notifyLoadingChanged(false);
  }

  maxLoadStartPositionUs = -1;
  if (fillingBuffers) {
    for (int i = 0; i < loaders.size(); i++) {
      Object loader = loaders.get(i);
      LoaderState loaderState = loaderStates.get(loader);
      long loaderTime = loaderState.nextLoadPositionUs;
      if (loaderTime != -1
          && (maxLoadStartPositionUs == -1 || loaderTime < maxLoadStartPositionUs)) {
        maxLoadStartPositionUs = loaderTime;
      }
    }
  }
}

updateControlState 中真正起作用的是最后的 for 循環(huán),其中會(huì)更新 maxLoadStartPositionUs 。

總結(jié)

LoadControl 中時(shí)間和緩存數(shù)據(jù)大小上分別有兩個(gè)水位,低水位、高水位。
當(dāng)水位變動(dòng)的時(shí)候會(huì)更新 maxLoadStartPositionUs 來確定是否繼續(xù)緩存——達(dá)到高水位暫停緩存,達(dá)到低水位開始緩存。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容