Skip to content

Commit 7dbc5eb

Browse files
add config parameter maxHeapAllocationPercent
1 parent 40e450b commit 7dbc5eb

4 files changed

Lines changed: 18 additions & 9 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@ minBufferMs | number | The default minimum duration of media that the player wil
385385
maxBufferMs | number | The default maximum duration of media that the player will attempt to buffer, in milliseconds.
386386
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.
387387
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.
388+
maxHeapAllocationPercent | number | The percentage of available heap that the video can use to buffer, between 0 and 1
388389

389390
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.
390391

Video.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -468,6 +468,7 @@ Video.propTypes = {
468468
maxBufferMs: PropTypes.number,
469469
bufferForPlaybackMs: PropTypes.number,
470470
bufferForPlaybackAfterRebufferMs: PropTypes.number,
471+
maxHeapAllocationPercent: PropTypes.number,
471472
}),
472473
stereoPan: PropTypes.number,
473474
rate: PropTypes.number,

android-exoplayer/src/main/java/com/brentvatne/exoplayer/ReactExoplayerView.java

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ class ReactExoplayerView extends FrameLayout implements
9898
MetadataOutput,
9999
DrmSessionEventListener {
100100

101+
public static final double DEFAULT_MAX_HEAP_ALLOCATION_PERCENT = 1;
102+
101103
private static final String TAG = "ReactExoplayerView";
102104

103105
private static final CookieManager DEFAULT_COOKIE_MANAGER;
@@ -142,6 +144,7 @@ class ReactExoplayerView extends FrameLayout implements
142144
private int maxBufferMs = DefaultLoadControl.DEFAULT_MAX_BUFFER_MS;
143145
private int bufferForPlaybackMs = DefaultLoadControl.DEFAULT_BUFFER_FOR_PLAYBACK_MS;
144146
private int bufferForPlaybackAfterRebufferMs = DefaultLoadControl.DEFAULT_BUFFER_FOR_PLAYBACK_AFTER_REBUFFER_MS;
147+
private double maxHeapAllocationPercent = ReactExoplayerView.DEFAULT_MAX_HEAP_ALLOCATION_PERCENT;
145148

146149
private Handler mainHandler;
147150
private Timer bufferCheckTimer;
@@ -413,19 +416,18 @@ public RNVLoadControl(DefaultAllocator allocator, int minBufferMs, int maxBuffer
413416
prioritizeTimeOverSizeThresholds,
414417
backBufferDurationMs,
415418
retainBackBufferFromKeyframe);
416-
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.N) {
417-
ActivityManager activityManager = (ActivityManager) themedReactContext.getSystemService(themedReactContext.ACTIVITY_SERVICE);
418-
availableHeapInBytes = activityManager.getMemoryClass() / 2 * 1024 * 1024;
419-
}
419+
ActivityManager activityManager = (ActivityManager) themedReactContext.getSystemService(themedReactContext.ACTIVITY_SERVICE);
420+
availableHeapInBytes = (int) Math.floor(activityManager.getMemoryClass() * maxHeapAllocationPercent * 1024 * 1024);
420421
}
421422

422423
@Override
423424
public boolean shouldContinueLoading(long playbackPositionUs, long bufferedDurationUs, float playbackSpeed) {
424-
int loadedBytes = getAllocator().getTotalBytesAllocated();
425-
if (availableHeapInBytes > 0 && loadedBytes >= availableHeapInBytes) {
425+
if (ReactExoplayerView.this.disableBuffering) {
426426
return false;
427427
}
428-
if (ReactExoplayerView.this.disableBuffering) {
428+
int loadedBytes = getAllocator().getTotalBytesAllocated();
429+
boolean isHeapReached = availableHeapInBytes > 0 && loadedBytes >= availableHeapInBytes;
430+
if (isHeapReached) {
429431
return false;
430432
}
431433
return super.shouldContinueLoading(playbackPositionUs, bufferedDurationUs, playbackSpeed);
@@ -1528,11 +1530,12 @@ public void setHideShutterView(boolean hideShutterView) {
15281530
exoPlayerView.setHideShutterView(hideShutterView);
15291531
}
15301532

1531-
public void setBufferConfig(int newMinBufferMs, int newMaxBufferMs, int newBufferForPlaybackMs, int newBufferForPlaybackAfterRebufferMs) {
1533+
public void setBufferConfig(int newMinBufferMs, int newMaxBufferMs, int newBufferForPlaybackMs, int newBufferForPlaybackAfterRebufferMs, double newMaxHeapAllocationPercent) {
15321534
minBufferMs = newMinBufferMs;
15331535
maxBufferMs = newMaxBufferMs;
15341536
bufferForPlaybackMs = newBufferForPlaybackMs;
15351537
bufferForPlaybackAfterRebufferMs = newBufferForPlaybackAfterRebufferMs;
1538+
maxHeapAllocationPercent = newMaxHeapAllocationPercent;
15361539
releasePlayer();
15371540
initializePlayer();
15381541
}

android-exoplayer/src/main/java/com/brentvatne/exoplayer/ReactExoplayerViewManager.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ public class ReactExoplayerViewManager extends ViewGroupManager<ReactExoplayerVi
5555
private static final String PROP_BUFFER_CONFIG_MAX_BUFFER_MS = "maxBufferMs";
5656
private static final String PROP_BUFFER_CONFIG_BUFFER_FOR_PLAYBACK_MS = "bufferForPlaybackMs";
5757
private static final String PROP_BUFFER_CONFIG_BUFFER_FOR_PLAYBACK_AFTER_REBUFFER_MS = "bufferForPlaybackAfterRebufferMs";
58+
private static final String PROP_BUFFER_CONFIG_MAX_HEAP_ALLOCATION_PERCENT = "maxHeapAllocationPercent";
5859
private static final String PROP_PREVENTS_DISPLAY_SLEEP_DURING_VIDEO_PLAYBACK = "preventsDisplaySleepDuringVideoPlayback";
5960
private static final String PROP_PROGRESS_UPDATE_INTERVAL = "progressUpdateInterval";
6061
private static final String PROP_REPORT_BANDWIDTH = "reportBandwidth";
@@ -338,6 +339,7 @@ public void setBufferConfig(final ReactExoplayerView videoView, @Nullable Readab
338339
int maxBufferMs = DefaultLoadControl.DEFAULT_MAX_BUFFER_MS;
339340
int bufferForPlaybackMs = DefaultLoadControl.DEFAULT_BUFFER_FOR_PLAYBACK_MS;
340341
int bufferForPlaybackAfterRebufferMs = DefaultLoadControl.DEFAULT_BUFFER_FOR_PLAYBACK_AFTER_REBUFFER_MS;
342+
double maxHeapAllocationPercent = ReactExoplayerView.DEFAULT_MAX_HEAP_ALLOCATION_PERCENT;
341343
if (bufferConfig != null) {
342344
minBufferMs = bufferConfig.hasKey(PROP_BUFFER_CONFIG_MIN_BUFFER_MS)
343345
? bufferConfig.getInt(PROP_BUFFER_CONFIG_MIN_BUFFER_MS) : minBufferMs;
@@ -347,7 +349,9 @@ public void setBufferConfig(final ReactExoplayerView videoView, @Nullable Readab
347349
? bufferConfig.getInt(PROP_BUFFER_CONFIG_BUFFER_FOR_PLAYBACK_MS) : bufferForPlaybackMs;
348350
bufferForPlaybackAfterRebufferMs = bufferConfig.hasKey(PROP_BUFFER_CONFIG_BUFFER_FOR_PLAYBACK_AFTER_REBUFFER_MS)
349351
? bufferConfig.getInt(PROP_BUFFER_CONFIG_BUFFER_FOR_PLAYBACK_AFTER_REBUFFER_MS) : bufferForPlaybackAfterRebufferMs;
350-
videoView.setBufferConfig(minBufferMs, maxBufferMs, bufferForPlaybackMs, bufferForPlaybackAfterRebufferMs);
352+
maxHeapAllocationPercent = bufferConfig.hasKey(PROP_BUFFER_CONFIG_MAX_HEAP_ALLOCATION_PERCENT)
353+
? bufferConfig.getDouble(PROP_BUFFER_CONFIG_MAX_HEAP_ALLOCATION_PERCENT) : maxHeapAllocationPercent;
354+
videoView.setBufferConfig(minBufferMs, maxBufferMs, bufferForPlaybackMs, bufferForPlaybackAfterRebufferMs, maxHeapAllocationPercent);
351355
}
352356
}
353357

0 commit comments

Comments
 (0)