Skip to content

Commit

Permalink
Make exo player cache size and directory configurable (TheWidlarzGrou…
Browse files Browse the repository at this point in the history
…p#31)

* Store exo player cache in subdirectory

* Let exo player fully manage cache size

* Restrict cache size to 380MB

* Make cache evictor configurable

* Make sub cache directory configurable

* Use correct type
  • Loading branch information
jaspermeijaard authored and RalfNieuwenhuizen committed Apr 20, 2022
1 parent ce495d6 commit 977261c
Showing 1 changed file with 22 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@
import com.facebook.react.bridge.Arguments;
import com.facebook.react.bridge.WritableMap;
import com.google.android.exoplayer2.C;
import com.google.android.exoplayer2.upstream.cache.NoOpCacheEvictor;
import com.google.android.exoplayer2.upstream.cache.LeastRecentlyUsedCacheEvictor;
import com.google.android.exoplayer2.upstream.cache.Cache;
import com.google.android.exoplayer2.upstream.cache.NoOpCacheEvictor;
import com.google.android.exoplayer2.upstream.cache.SimpleCache;
import com.google.android.exoplayer2.upstream.cache.CacheUtil;
import com.google.android.exoplayer2.upstream.cache.CacheDataSourceFactory;
Expand All @@ -17,10 +18,8 @@
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DataSourceInputStream;

import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;

Expand All @@ -33,6 +32,8 @@ public class ExoPlayerCache extends ReactContextBaseJavaModule {

private static SimpleCache instance = null;
private static final String CACHE_KEY_PREFIX = "exoPlayerCacheKeyPrefix";
private static int maxCacheSizeBytes = -1; // Default no maximum size
private static String cacheSubDirectory = "";

public ExoPlayerCache(ReactApplicationContext reactContext) {
super(reactContext);
Expand All @@ -43,6 +44,18 @@ public String getName() {
return "ExoPlayerCache";
}

@ReactMethod
public void setMaxCacheSize(final int bytes, final Promise promise) {
maxCacheSizeBytes = bytes;
promise.resolve(maxCacheSizeBytes);
}

@ReactMethod
public void setCacheSubDirectory(final String directory, final Promise promise) {
cacheSubDirectory = directory;
promise.resolve(cacheSubDirectory);
}

@ReactMethod
public void exportVideo(final String url, final Promise promise) {
Log.d(getName(), "exportVideo");
Expand Down Expand Up @@ -115,7 +128,12 @@ public void run() {

public static SimpleCache getInstance(Context context) {
if(instance == null) {
instance = new SimpleCache(new File(ExoPlayerCache.getCacheDir(context)), new NoOpCacheEvictor());
instance = new SimpleCache(
new File(ExoPlayerCache.getCacheDir(context) + cacheSubDirectory),
maxCacheSizeBytes == -1
? new NoOpCacheEvictor()
: new LeastRecentlyUsedCacheEvictor(maxCacheSizeBytes)
);
}
return instance;
}
Expand Down

0 comments on commit 977261c

Please sign in to comment.