use of com.google.android.exoplayer2.upstream.cache.Cache in project react-native-track-player by react-native-kit.
the class LocalPlayback method initialize.
@Override
public void initialize() {
if (cacheMaxSize > 0) {
File cacheDir = new File(context.getCacheDir(), "TrackPlayer");
DatabaseProvider db = new ExoDatabaseProvider(context);
cache = new SimpleCache(cacheDir, new LeastRecentlyUsedCacheEvictor(cacheMaxSize), db);
} else {
cache = null;
}
super.initialize();
resetQueue();
}
use of com.google.android.exoplayer2.upstream.cache.Cache in project ExoPlayer by google.
the class CacheDataSource method openNextSource.
/**
* Opens the next source. If the cache contains data spanning the current read position then
* {@link #cacheReadDataSource} is opened to read from it. Else {@link #upstreamDataSource} is
* opened to read from the upstream source and write into the cache.
* @param initial Whether it is the initial open call.
*/
private boolean openNextSource(boolean initial) throws IOException {
DataSpec dataSpec;
CacheSpan span;
if (currentRequestIgnoresCache) {
span = null;
} else if (blockOnCache) {
try {
span = cache.startReadWrite(key, readPosition);
} catch (InterruptedException e) {
throw new InterruptedIOException();
}
} else {
span = cache.startReadWriteNonBlocking(key, readPosition);
}
if (span == null) {
// The data is locked in the cache, or we're ignoring the cache. Bypass the cache and read
// from upstream.
currentDataSource = upstreamDataSource;
dataSpec = new DataSpec(uri, readPosition, bytesRemaining, key, flags);
} else if (span.isCached) {
// Data is cached, read from cache.
Uri fileUri = Uri.fromFile(span.file);
long filePosition = readPosition - span.position;
long length = span.length - filePosition;
if (bytesRemaining != C.LENGTH_UNSET) {
length = Math.min(length, bytesRemaining);
}
dataSpec = new DataSpec(fileUri, readPosition, filePosition, length, key, flags);
currentDataSource = cacheReadDataSource;
} else {
// Data is not cached, and data is not locked, read from upstream with cache backing.
long length;
if (span.isOpenEnded()) {
length = bytesRemaining;
} else {
length = span.length;
if (bytesRemaining != C.LENGTH_UNSET) {
length = Math.min(length, bytesRemaining);
}
}
dataSpec = new DataSpec(uri, readPosition, length, key, flags);
if (cacheWriteDataSource != null) {
currentDataSource = cacheWriteDataSource;
lockedSpan = span;
} else {
currentDataSource = upstreamDataSource;
cache.releaseHoleSpan(span);
}
}
currentRequestUnbounded = dataSpec.length == C.LENGTH_UNSET;
boolean successful = false;
long currentBytesRemaining = 0;
try {
currentBytesRemaining = currentDataSource.open(dataSpec);
successful = true;
} catch (IOException e) {
// end of the stream.
if (!initial && currentRequestUnbounded) {
Throwable cause = e;
while (cause != null) {
if (cause instanceof DataSourceException) {
int reason = ((DataSourceException) cause).reason;
if (reason == DataSourceException.POSITION_OUT_OF_RANGE) {
e = null;
break;
}
}
cause = cause.getCause();
}
}
if (e != null) {
throw e;
}
}
// bytesRemaining == C.LENGTH_UNSET) and got a resolved length from open() request
if (currentRequestUnbounded && currentBytesRemaining != C.LENGTH_UNSET) {
bytesRemaining = currentBytesRemaining;
setContentLength(dataSpec.position + bytesRemaining);
}
return successful;
}
use of com.google.android.exoplayer2.upstream.cache.Cache in project react-native-track-player by react-native-kit.
the class ExoPlayback method load.
@Override
public void load(Track track, Promise callback) {
loadCallback = callback;
Uri url = track.url;
String userAgent = Util.getUserAgent(context, "react-native-track-player");
DefaultHttpDataSourceFactory httpDataSourceFactory = new DefaultHttpDataSourceFactory(userAgent, null, DefaultHttpDataSource.DEFAULT_CONNECT_TIMEOUT_MILLIS, DefaultHttpDataSource.DEFAULT_READ_TIMEOUT_MILLIS, true);
DataSource.Factory factory = new DefaultDataSourceFactory(context, null, httpDataSourceFactory);
MediaSource source;
if (cacheMaxSize > 0 && !track.urlLocal) {
File cacheDir = new File(context.getCacheDir(), "TrackPlayer");
Cache cache = new SimpleCache(cacheDir, new LeastRecentlyUsedCacheEvictor(cacheMaxSize));
factory = new CacheDataSourceFactory(cache, factory, 0, cacheMaxSize);
}
if (track.type == TrackType.DASH) {
source = new DashMediaSource(url, factory, new DefaultDashChunkSource.Factory(factory), null, null);
} else if (track.type == TrackType.HLS) {
source = new HlsMediaSource(url, factory, null, null);
} else if (track.type == TrackType.SMOOTH_STREAMING) {
source = new SsMediaSource(url, factory, new DefaultSsChunkSource.Factory(factory), null, null);
} else {
source = new ExtractorMediaSource(url, factory, new DefaultExtractorsFactory(), null, null);
}
player.prepare(source);
}
use of com.google.android.exoplayer2.upstream.cache.Cache in project ExoPlayer by google.
the class HlsChunkSource method maybeCreateEncryptionChunkFor.
@Nullable
private Chunk maybeCreateEncryptionChunkFor(@Nullable Uri keyUri, int selectedTrackIndex) {
if (keyUri == null) {
return null;
}
@Nullable byte[] encryptionKey = keyCache.remove(keyUri);
if (encryptionKey != null) {
// The key was present in the key cache. We re-insert it to prevent it from being evicted by
// the following key addition. Note that removal of the key is necessary to affect the
// eviction order.
keyCache.put(keyUri, encryptionKey);
return null;
}
DataSpec dataSpec = new DataSpec.Builder().setUri(keyUri).setFlags(DataSpec.FLAG_ALLOW_GZIP).build();
return new EncryptionKeyChunk(encryptionDataSource, dataSpec, playlistFormats[selectedTrackIndex], trackSelection.getSelectionReason(), trackSelection.getSelectionData(), scratchSpace);
}
use of com.google.android.exoplayer2.upstream.cache.Cache in project ExoPlayer by google.
the class DownloadManagerDashTest method setUp.
@Before
public void setUp() throws Exception {
ShadowLog.stream = System.out;
testThread = new DummyMainThread();
Context context = ApplicationProvider.getApplicationContext();
tempFolder = Util.createTempDirectory(context, "ExoPlayerTest");
File cacheFolder = new File(tempFolder, "cache");
cacheFolder.mkdir();
cache = new SimpleCache(cacheFolder, new NoOpCacheEvictor(), TestUtil.getInMemoryDatabaseProvider());
MockitoAnnotations.initMocks(this);
fakeDataSet = new FakeDataSet().setData(TEST_MPD_URI, TEST_MPD).setRandomData("audio_init_data", 10).setRandomData("audio_segment_1", 4).setRandomData("audio_segment_2", 5).setRandomData("audio_segment_3", 6).setRandomData("text_segment_1", 1).setRandomData("text_segment_2", 2).setRandomData("text_segment_3", 3);
fakeStreamKey1 = new StreamKey(0, 0, 0);
fakeStreamKey2 = new StreamKey(0, 1, 0);
downloadIndex = new DefaultDownloadIndex(TestUtil.getInMemoryDatabaseProvider());
createDownloadManager();
}
Aggregations