From 7dbc5eb078f530168394c68731c1185fb334982a Mon Sep 17 00:00:00 2001 From: Gabriel Rivero Date: Thu, 4 Nov 2021 13:54:43 -0400 Subject: [PATCH] add config parameter maxHeapAllocationPercent --- README.md | 1 + Video.js | 1 + .../exoplayer/ReactExoplayerView.java | 19 +++++++++++-------- .../exoplayer/ReactExoplayerViewManager.java | 6 +++++- 4 files changed, 18 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index ca6745d39c..564423f8ea 100644 --- a/README.md +++ b/README.md @@ -385,6 +385,7 @@ minBufferMs | number | The default minimum duration of media that the player wil maxBufferMs | number | The default maximum duration of media that the player will attempt to buffer, in milliseconds. bufferForPlaybackMs | number | The default duration of media that must be buffered for playback to start or resume following a user action such as a seek, in milliseconds. bufferForPlaybackAfterRebufferMs | number | The default duration of media that must be buffered for playback to resume after a rebuffer, in milliseconds. A rebuffer is defined to be caused by buffer depletion rather than a user action. +maxHeapAllocationPercent | number | The percentage of available heap that the video can use to buffer, between 0 and 1 This prop should only be set when you are setting the source, changing it after the media is loaded will cause it to be reloaded. diff --git a/Video.js b/Video.js index 62a179a445..fcad843702 100644 --- a/Video.js +++ b/Video.js @@ -468,6 +468,7 @@ Video.propTypes = { maxBufferMs: PropTypes.number, bufferForPlaybackMs: PropTypes.number, bufferForPlaybackAfterRebufferMs: PropTypes.number, + maxHeapAllocationPercent: PropTypes.number, }), stereoPan: PropTypes.number, rate: PropTypes.number, 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 981bca2231..37c9657e8c 100644 --- a/android-exoplayer/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java +++ b/android-exoplayer/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java @@ -98,6 +98,8 @@ class ReactExoplayerView extends FrameLayout implements MetadataOutput, DrmSessionEventListener { + public static final double DEFAULT_MAX_HEAP_ALLOCATION_PERCENT = 1; + private static final String TAG = "ReactExoplayerView"; private static final CookieManager DEFAULT_COOKIE_MANAGER; @@ -142,6 +144,7 @@ class ReactExoplayerView extends FrameLayout implements private int maxBufferMs = DefaultLoadControl.DEFAULT_MAX_BUFFER_MS; private int bufferForPlaybackMs = DefaultLoadControl.DEFAULT_BUFFER_FOR_PLAYBACK_MS; private int bufferForPlaybackAfterRebufferMs = DefaultLoadControl.DEFAULT_BUFFER_FOR_PLAYBACK_AFTER_REBUFFER_MS; + private double maxHeapAllocationPercent = ReactExoplayerView.DEFAULT_MAX_HEAP_ALLOCATION_PERCENT; private Handler mainHandler; private Timer bufferCheckTimer; @@ -413,19 +416,18 @@ public RNVLoadControl(DefaultAllocator allocator, int minBufferMs, int maxBuffer 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; - } + ActivityManager activityManager = (ActivityManager) themedReactContext.getSystemService(themedReactContext.ACTIVITY_SERVICE); + availableHeapInBytes = (int) Math.floor(activityManager.getMemoryClass() * maxHeapAllocationPercent * 1024 * 1024); } @Override public boolean shouldContinueLoading(long playbackPositionUs, long bufferedDurationUs, float playbackSpeed) { - int loadedBytes = getAllocator().getTotalBytesAllocated(); - if (availableHeapInBytes > 0 && loadedBytes >= availableHeapInBytes) { + if (ReactExoplayerView.this.disableBuffering) { return false; } - if (ReactExoplayerView.this.disableBuffering) { + int loadedBytes = getAllocator().getTotalBytesAllocated(); + boolean isHeapReached = availableHeapInBytes > 0 && loadedBytes >= availableHeapInBytes; + if (isHeapReached) { return false; } return super.shouldContinueLoading(playbackPositionUs, bufferedDurationUs, playbackSpeed); @@ -1528,11 +1530,12 @@ public void setHideShutterView(boolean hideShutterView) { exoPlayerView.setHideShutterView(hideShutterView); } - public void setBufferConfig(int newMinBufferMs, int newMaxBufferMs, int newBufferForPlaybackMs, int newBufferForPlaybackAfterRebufferMs) { + public void setBufferConfig(int newMinBufferMs, int newMaxBufferMs, int newBufferForPlaybackMs, int newBufferForPlaybackAfterRebufferMs, double newMaxHeapAllocationPercent) { minBufferMs = newMinBufferMs; maxBufferMs = newMaxBufferMs; bufferForPlaybackMs = newBufferForPlaybackMs; bufferForPlaybackAfterRebufferMs = newBufferForPlaybackAfterRebufferMs; + maxHeapAllocationPercent = newMaxHeapAllocationPercent; releasePlayer(); initializePlayer(); } diff --git a/android-exoplayer/src/main/java/com/brentvatne/exoplayer/ReactExoplayerViewManager.java b/android-exoplayer/src/main/java/com/brentvatne/exoplayer/ReactExoplayerViewManager.java index fc9a1354f2..3bffbc37ab 100644 --- a/android-exoplayer/src/main/java/com/brentvatne/exoplayer/ReactExoplayerViewManager.java +++ b/android-exoplayer/src/main/java/com/brentvatne/exoplayer/ReactExoplayerViewManager.java @@ -55,6 +55,7 @@ public class ReactExoplayerViewManager extends ViewGroupManager