Skip to content

Commit

Permalink
increase size of ExoPlayer video buffer by 50% when RAM > 1.5GB
Browse files Browse the repository at this point in the history
  • Loading branch information
warren-bank committed Mar 8, 2020
1 parent 7201d10 commit cba0230
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,17 @@
package com.github.warren_bank.exoplayer_airplay_receiver.ui.exoplayer2;

import com.github.warren_bank.exoplayer_airplay_receiver.R;
import com.github.warren_bank.exoplayer_airplay_receiver.utils.SystemUtils;

import android.content.Context;
import android.net.Uri;
import android.os.Handler;
import android.util.Log;
import android.view.KeyEvent;
import androidx.annotation.Nullable;

import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.DefaultLoadControl;
import com.google.android.exoplayer2.DefaultRenderersFactory;
import com.google.android.exoplayer2.ExoPlaybackException;
import com.google.android.exoplayer2.ExoPlayerFactory;
Expand All @@ -33,6 +36,7 @@
import com.google.android.exoplayer2.trackselection.MappingTrackSelector;
import com.google.android.exoplayer2.ui.PlayerView;
import com.google.android.exoplayer2.util.MimeTypes;
import com.google.android.exoplayer2.upstream.DefaultAllocator;
import com.google.android.exoplayer2.upstream.DefaultDataSourceFactory;
import com.google.android.exoplayer2.upstream.DefaultHttpDataSourceFactory;
import com.google.android.exoplayer2.upstream.RawResourceDataSource;
Expand All @@ -42,6 +46,8 @@
/** Manages ExoPlayer and an internal media queue */
public final class PlayerManager implements EventListener {

private static final String TAG = "PlayerManager";

private final class MyArrayList<E> extends ArrayList<E> {
public void retainLast() {
int last = this.size() - 1;
Expand Down Expand Up @@ -85,7 +91,8 @@ private PlayerManager(
this.concatenatingMediaSource = new ConcatenatingMediaSource();
this.trackSelector = new DefaultTrackSelector(context);
RenderersFactory renderersFactory = new DefaultRenderersFactory(context);
this.exoPlayer = ExoPlayerFactory.newSimpleInstance(context, renderersFactory, trackSelector);
DefaultLoadControl loadControl = getLoadControl(context);
this.exoPlayer = ExoPlayerFactory.newSimpleInstance(context, renderersFactory, trackSelector, loadControl);
this.exoPlayer.addListener(this);
this.playerView.setKeepContentOnPlayerReset(false);
this.playerView.setPlayer(this.exoPlayer);
Expand Down Expand Up @@ -113,6 +120,26 @@ public void run() {
};
}

private DefaultLoadControl getLoadControl(Context context) {
long thresholdMemorySizeInBytes = 1610612736l; // 1.5 GiB
long memorySizeInBytes = SystemUtils.getMemorySizeInBytes(context);
float factor = (memorySizeInBytes <= thresholdMemorySizeInBytes) ? 1.0f : 1.5f;

Log.d(TAG, "memory=" + (float)(((int)((memorySizeInBytes*100)/(1024*1024*1024)))/100f) + "GB, buffer factor=" + (int)(factor*100) + "%");

DefaultLoadControl loadControl = new DefaultLoadControl(
/* DefaultAllocator allocator= */ (new DefaultAllocator(true, C.DEFAULT_BUFFER_SEGMENT_SIZE)),
/* int minBufferMs= minBufferAudioMs= minBufferVideoMs= */ (int) (factor * DefaultLoadControl.DEFAULT_MIN_BUFFER_MS),
/* int maxBufferMs= */ (int) (factor * DefaultLoadControl.DEFAULT_MAX_BUFFER_MS),
/* int bufferForPlaybackMs= */ (int) (factor * DefaultLoadControl.DEFAULT_BUFFER_FOR_PLAYBACK_MS),
/* int bufferForPlaybackAfterRebufferMs= */ (int) (factor * DefaultLoadControl.DEFAULT_BUFFER_FOR_PLAYBACK_AFTER_REBUFFER_MS),
/* int targetBufferBytes= */ (int) (factor * DefaultLoadControl.DEFAULT_TARGET_BUFFER_BYTES),
/* boolean prioritizeTimeOverSizeThresholds= */ DefaultLoadControl.DEFAULT_PRIORITIZE_TIME_OVER_SIZE_THRESHOLDS
);

return loadControl;
}

// Query state of ExoPlayer.

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

import android.content.Context;
import android.os.Build;

import android.os.PowerManager;
import android.hardware.display.DisplayManager;
import android.view.Display;

import android.app.ActivityManager;

public class SystemUtils {

/**
Expand All @@ -25,10 +28,28 @@ public static boolean isScreenOn(Context context) {
}
}
return screenOn;
} else {
}
else {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
return pm.isScreenOn();
}
}

/**
* Returns the total amount of RAM in the current Android device in Bytes (ex: 1610612736 == 1.5 GiB)
* @return {Long}
*/
public static long getMemorySizeInBytes(Context context) {
if (Build.VERSION.SDK_INT >= 16) {
ActivityManager activityManager = (ActivityManager) context.getSystemService(Context.ACTIVITY_SERVICE);
ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
activityManager.getMemoryInfo(memoryInfo);
long totalMemory = memoryInfo.totalMem;
return totalMemory;
}
else {
return -1l;
}
}

}
4 changes: 2 additions & 2 deletions android-studio-project/constants.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
project.ext {
releaseVersionCode = Integer.parseInt("001000816", 10)
releaseVersion = '001.00.08-16API'
releaseVersionCode = Integer.parseInt("001000916", 10)
releaseVersion = '001.00.09-16API'
minSdkVersion = 16
targetSdkVersion = 28
compileSdkVersion = 28
Expand Down

0 comments on commit cba0230

Please sign in to comment.