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;
@ -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;
} }