VEX-6030-reduce-buffer-size-based-on-heap

This commit is contained in:
Gabriel Rivero 2021-11-03 19:14:19 -04:00
parent e27baeb065
commit 0ab7ff476b

View File

@ -2,6 +2,7 @@ package com.brentvatne.exoplayer;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.app.Activity; import android.app.Activity;
import android.app.ActivityManager;
import android.content.Context; import android.content.Context;
import android.media.AudioManager; import android.media.AudioManager;
import android.net.Uri; import android.net.Uri;
@ -198,7 +199,7 @@ class ReactExoplayerView extends FrameLayout implements
public double getPositionInFirstPeriodMsForCurrentWindow(long currentPosition) { public double getPositionInFirstPeriodMsForCurrentWindow(long currentPosition) {
Timeline.Window window = new Timeline.Window(); Timeline.Window window = new Timeline.Window();
if(!player.getCurrentTimeline().isEmpty()) { if(!player.getCurrentTimeline().isEmpty()) {
player.getCurrentTimeline().getWindow(player.getCurrentWindowIndex(), window); player.getCurrentTimeline().getWindow(player.getCurrentWindowIndex(), window);
} }
return window.windowStartTimeMs + currentPosition; return window.windowStartTimeMs + currentPosition;
@ -401,6 +402,7 @@ class ReactExoplayerView extends FrameLayout implements
} }
private class RNVLoadControl extends DefaultLoadControl { private class RNVLoadControl extends DefaultLoadControl {
private int availableHeapInBytes = 0;
public RNVLoadControl(DefaultAllocator allocator, int minBufferMs, int maxBufferMs, int bufferForPlaybackMs, int bufferForPlaybackAfterRebufferMs, int targetBufferBytes, boolean prioritizeTimeOverSizeThresholds, int backBufferDurationMs, boolean retainBackBufferFromKeyframe) { public RNVLoadControl(DefaultAllocator allocator, int minBufferMs, int maxBufferMs, int bufferForPlaybackMs, int bufferForPlaybackAfterRebufferMs, int targetBufferBytes, boolean prioritizeTimeOverSizeThresholds, int backBufferDurationMs, boolean retainBackBufferFromKeyframe) {
super(allocator, super(allocator,
minBufferMs, minBufferMs,
@ -411,10 +413,18 @@ class ReactExoplayerView extends FrameLayout implements
prioritizeTimeOverSizeThresholds, prioritizeTimeOverSizeThresholds,
backBufferDurationMs, backBufferDurationMs,
retainBackBufferFromKeyframe); retainBackBufferFromKeyframe);
if (android.os.Build.VERSION.SDK_INT <= android.os.Build.VERSION_CODES.N) {
ActivityManager activityManager = (ActivityManager) themedReactContext.getSystemService(themedReactContext.ACTIVITY_SERVICE);
availableHeapInBytes = activityManager.getMemoryClass() / 2 * 1024 * 1024;
}
} }
@Override @Override
public boolean shouldContinueLoading(long playbackPositionUs, long bufferedDurationUs, float playbackSpeed) { public boolean shouldContinueLoading(long playbackPositionUs, long bufferedDurationUs, float playbackSpeed) {
int loadedBytes = getAllocator().getTotalBytesAllocated();
if (availableHeapInBytes > 0 && loadedBytes >= availableHeapInBytes) {
return false;
}
if (ReactExoplayerView.this.disableBuffering) { if (ReactExoplayerView.this.disableBuffering) {
return false; return false;
} }