Skip to content

Commit

Permalink
fix(android): android cache header (#3832)
Browse files Browse the repository at this point in the history
* fix: android cache header
  • Loading branch information
lovegaoshi authored May 28, 2024
1 parent 5c29b48 commit c2a1424
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,22 @@ import java.io.File
object RNVSimpleCache {
// TODO: when to release? how to check if cache is released?
private var simpleCache: SimpleCache? = null
var cacheDataSourceFactory: DataSource.Factory? = null

fun setSimpleCache(context: Context, cacheSize: Int, factory: HttpDataSource.Factory) {
if (cacheDataSourceFactory != null || cacheSize <= 0) return
fun setSimpleCache(context: Context, cacheSize: Int) {
if (simpleCache != null || cacheSize <= 0) return
simpleCache = SimpleCache(
File(context.cacheDir, "RNVCache"),
LeastRecentlyUsedCacheEvictor(
cacheSize.toLong() * 1024 * 1024
),
StandaloneDatabaseProvider(context)
)
cacheDataSourceFactory =
CacheDataSource.Factory()
.setCache(simpleCache!!)
.setUpstreamDataSourceFactory(factory)
}

fun getCacheFactory(factory: HttpDataSource.Factory): DataSource.Factory {
if (simpleCache == null) return factory
return CacheDataSource.Factory()
.setCache(simpleCache!!)
.setUpstreamDataSourceFactory(factory)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ public class ReactExoplayerView extends FrameLayout implements
private boolean selectTrackWhenReady = false;
private Handler mainHandler;
private Runnable mainRunnable;
private DataSource.Factory cacheDataSourceFactory;
private boolean useCache = false;
private ControlsConfig controlsConfig = new ControlsConfig();

// Props from React
Expand Down Expand Up @@ -709,8 +709,8 @@ private void initializePlayerCore(ReactExoplayerView self) {
.setAdErrorListener(this)
.build();
DefaultMediaSourceFactory mediaSourceFactory = new DefaultMediaSourceFactory(mediaDataSourceFactory);
if (cacheDataSourceFactory != null) {
mediaSourceFactory.setDataSourceFactory(cacheDataSourceFactory);
if (useCache) {
mediaSourceFactory.setDataSourceFactory(RNVSimpleCache.INSTANCE.getCacheFactory(buildHttpDataSourceFactory(true)));
}

if (adsLoader != null) {
Expand Down Expand Up @@ -1016,13 +1016,13 @@ private MediaSource buildMediaSource(Uri uri, String overrideExtension, DrmSessi
throw new IllegalStateException("cannot open input file" + srcUri);
}
} else if ("file".equals(srcUri.getScheme()) ||
cacheDataSourceFactory == null) {
!useCache) {
mediaSourceFactory = new ProgressiveMediaSource.Factory(
mediaDataSourceFactory
);
} else {
mediaSourceFactory = new ProgressiveMediaSource.Factory(
cacheDataSourceFactory
RNVSimpleCache.INSTANCE.getCacheFactory(buildHttpDataSourceFactory(true))
);

}
Expand Down Expand Up @@ -2241,12 +2241,11 @@ public void setBufferConfig(BufferConfig config) {
if (bufferConfig.getCacheSize() > 0) {
RNVSimpleCache.INSTANCE.setSimpleCache(
this.getContext(),
bufferConfig.getCacheSize(),
buildHttpDataSourceFactory(false)
bufferConfig.getCacheSize()
);
cacheDataSourceFactory = RNVSimpleCache.INSTANCE.getCacheDataSourceFactory();
useCache = true;
} else {
cacheDataSourceFactory = null;
useCache = false;
}
releasePlayer();
initializePlayer();
Expand Down
1 change: 1 addition & 0 deletions examples/basic/src/VideoPlayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ class VideoPlayer extends Component {
{
description: 'another bunny (can be saved)',
uri: 'https://rawgit.com/mediaelement/mediaelement-files/master/big_buck_bunny.mp4',
headers: {referer: 'www.github.com', 'User-Agent': 'react.native.video'},
},
{
description: 'sintel with subtitles',
Expand Down

0 comments on commit c2a1424

Please sign in to comment.