From 0ab7ff476b947a3552a8901123d5137c17bf31ba Mon Sep 17 00:00:00 2001 From: Gabriel Rivero Date: Wed, 3 Nov 2021 19:14:19 -0400 Subject: [PATCH] VEX-6030-reduce-buffer-size-based-on-heap --- .../com/brentvatne/exoplayer/ReactExoplayerView.java | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/android-exoplayer/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java b/android-exoplayer/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java index 24dda940..6ce67812 100644 --- a/android-exoplayer/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java +++ b/android-exoplayer/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java @@ -2,6 +2,7 @@ package com.brentvatne.exoplayer; import android.annotation.SuppressLint; import android.app.Activity; +import android.app.ActivityManager; import android.content.Context; import android.media.AudioManager; import android.net.Uri; @@ -198,7 +199,7 @@ class ReactExoplayerView extends FrameLayout implements public double getPositionInFirstPeriodMsForCurrentWindow(long currentPosition) { Timeline.Window window = new Timeline.Window(); - if(!player.getCurrentTimeline().isEmpty()) { + if(!player.getCurrentTimeline().isEmpty()) { player.getCurrentTimeline().getWindow(player.getCurrentWindowIndex(), window); } return window.windowStartTimeMs + currentPosition; @@ -401,6 +402,7 @@ class ReactExoplayerView extends FrameLayout implements } 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) { super(allocator, minBufferMs, @@ -411,10 +413,18 @@ class ReactExoplayerView extends FrameLayout implements prioritizeTimeOverSizeThresholds, backBufferDurationMs, 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 public boolean shouldContinueLoading(long playbackPositionUs, long bufferedDurationUs, float playbackSpeed) { + int loadedBytes = getAllocator().getTotalBytesAllocated(); + if (availableHeapInBytes > 0 && loadedBytes >= availableHeapInBytes) { + return false; + } if (ReactExoplayerView.this.disableBuffering) { return false; }