Skip to content

Commit

Permalink
Move SimpleCache creation in PlayerDataSource to avoid an IllegalStat…
Browse files Browse the repository at this point in the history
…eException

This IllegalStateException, almost not reproducible, indicates that another SimpleCache instance uses the cache folder, which was so trying to be created at least twice.
Moving the SimpleCache creation in PlayerDataSource should avoid this exception.
  • Loading branch information
AudricV committed May 2, 2022
1 parent ce2cdaf commit c834534
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,44 +1,37 @@
package org.schabi.newpipe.player.helper;

import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.google.android.exoplayer2.database.StandaloneDatabaseProvider;
import com.google.android.exoplayer2.upstream.DataSource;
import com.google.android.exoplayer2.upstream.DefaultDataSource;
import com.google.android.exoplayer2.upstream.DefaultHttpDataSource;
import com.google.android.exoplayer2.upstream.FileDataSource;
import com.google.android.exoplayer2.upstream.TransferListener;
import com.google.android.exoplayer2.upstream.cache.CacheDataSink;
import com.google.android.exoplayer2.upstream.cache.CacheDataSource;
import com.google.android.exoplayer2.upstream.cache.LeastRecentlyUsedCacheEvictor;
import com.google.android.exoplayer2.upstream.cache.SimpleCache;

import org.schabi.newpipe.player.datasource.YoutubeHttpDataSource;

import java.io.File;

/* package-private */ final class CacheFactory implements DataSource.Factory {
private static final String TAG = CacheFactory.class.getSimpleName();

private static final String CACHE_FOLDER_NAME = "exoplayer";
private static final int CACHE_FLAGS = CacheDataSource.FLAG_IGNORE_CACHE_ON_ERROR;
private static SimpleCache cache;

private final long maxFileSize;
private final Context context;
private final String userAgent;
private final TransferListener transferListener;
private final DataSource.Factory upstreamDataSourceFactory;
private final SimpleCache simpleCache;

public static class Builder {
private final Context context;
private final String userAgent;
private final TransferListener transferListener;
private DataSource.Factory upstreamDataSourceFactory;
private SimpleCache simpleCache;

Builder(@NonNull final Context context,
@NonNull final String userAgent,
Expand All @@ -53,34 +46,31 @@ public void setUpstreamDataSourceFactory(
this.upstreamDataSourceFactory = upstreamDataSourceFactory;
}

public void setSimpleCache(@NonNull final SimpleCache simpleCache) {
this.simpleCache = simpleCache;
}

public CacheFactory build() {
return new CacheFactory(context, userAgent, transferListener,
if (simpleCache == null) {
throw new IllegalStateException("No SimpleCache instance has been specified. "
+ "Please specify one with setSimpleCache");
}
return new CacheFactory(context, userAgent, transferListener, simpleCache,
upstreamDataSourceFactory);
}
}

private CacheFactory(@NonNull final Context context,
@NonNull final String userAgent,
@NonNull final TransferListener transferListener,
@NonNull final SimpleCache simpleCache,
@Nullable final DataSource.Factory upstreamDataSourceFactory) {
this.context = context;
this.userAgent = userAgent;
this.transferListener = transferListener;
this.simpleCache = simpleCache;
this.upstreamDataSourceFactory = upstreamDataSourceFactory;

final File cacheDir = new File(context.getExternalCacheDir(), CACHE_FOLDER_NAME);
if (!cacheDir.exists()) {
//noinspection ResultOfMethodCallIgnored
cacheDir.mkdir();
}

if (cache == null) {
final LeastRecentlyUsedCacheEvictor evictor
= new LeastRecentlyUsedCacheEvictor(PlayerHelper.getPreferredCacheSize());
cache = new SimpleCache(cacheDir, evictor, new StandaloneDatabaseProvider(context));
Log.d(TAG, "initExoPlayerCache: cacheDir = " + cacheDir.getAbsolutePath());
}

maxFileSize = PlayerHelper.getPreferredFileSize();
}

Expand Down Expand Up @@ -112,7 +102,8 @@ public DataSource createDataSource() {
.createDataSource();

final FileDataSource fileSource = new FileDataSource();
final CacheDataSink dataSink = new CacheDataSink(cache, maxFileSize);
return new CacheDataSource(cache, dataSource, fileSource, dataSink, CACHE_FLAGS, null);
final CacheDataSink dataSink = new CacheDataSink(simpleCache, maxFileSize);
return new CacheDataSource(simpleCache, dataSource, fileSource, dataSink, CACHE_FLAGS,
null);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package org.schabi.newpipe.player.helper;

import android.content.Context;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.google.android.exoplayer2.database.StandaloneDatabaseProvider;
import com.google.android.exoplayer2.source.ProgressiveMediaSource;
import com.google.android.exoplayer2.source.SingleSampleMediaSource;
import com.google.android.exoplayer2.source.dash.DashMediaSource;
Expand All @@ -18,10 +20,14 @@
import com.google.android.exoplayer2.upstream.DefaultDataSource;
import com.google.android.exoplayer2.upstream.DefaultHttpDataSource;
import com.google.android.exoplayer2.upstream.TransferListener;
import com.google.android.exoplayer2.upstream.cache.LeastRecentlyUsedCacheEvictor;
import com.google.android.exoplayer2.upstream.cache.SimpleCache;

import org.schabi.newpipe.extractor.services.youtube.YoutubeDashManifestCreator;
import org.schabi.newpipe.player.datasource.YoutubeHttpDataSource;

import java.io.File;

public class PlayerDataSource {

public static final int LIVE_STREAM_EDGE_GAP_MILLIS = 10000;
Expand All @@ -39,6 +45,18 @@ public class PlayerDataSource {
*/
private static final int MAXIMUM_SIZE_CACHED_GENERATED_MANIFESTS_PER_CACHE = 500;

/**
* The folder name in which the ExoPlayer cache will be written.
*/
private static final String CACHE_FOLDER_NAME = "exoplayer";

/**
* The {@link SimpleCache} instance which will be used to build
* {@link com.google.android.exoplayer2.upstream.cache.CacheDataSource}s instances (with
* {@link CacheFactory}).
*/
private static SimpleCache cache;

private final int continueLoadingCheckIntervalBytes;
private final CacheFactory.Builder cacheDataSourceFactoryBuilder;
private final DataSource.Factory cachelessDataSourceFactory;
Expand All @@ -47,8 +65,24 @@ public PlayerDataSource(@NonNull final Context context,
@NonNull final String userAgent,
@NonNull final TransferListener transferListener) {
continueLoadingCheckIntervalBytes = PlayerHelper.getProgressiveLoadIntervalBytes(context);
final File cacheDir = new File(context.getExternalCacheDir(), CACHE_FOLDER_NAME);
if (!cacheDir.exists()) {
//noinspection ResultOfMethodCallIgnored
cacheDir.mkdir();
}

if (cache == null) {
final LeastRecentlyUsedCacheEvictor evictor
= new LeastRecentlyUsedCacheEvictor(PlayerHelper.getPreferredCacheSize());
cache = new SimpleCache(cacheDir, evictor, new StandaloneDatabaseProvider(context));
Log.d(PlayerDataSource.class.getSimpleName(), "initExoPlayerCache: cacheDir = "
+ cacheDir.getAbsolutePath());
}

cacheDataSourceFactoryBuilder = new CacheFactory.Builder(context, userAgent,
transferListener);
cacheDataSourceFactoryBuilder.setSimpleCache(cache);

cachelessDataSourceFactory = new DefaultDataSource.Factory(context,
new DefaultHttpDataSource.Factory().setUserAgent(userAgent))
.setTransferListener(transferListener);
Expand Down

0 comments on commit c834534

Please sign in to comment.