Class SegmentDownloader<M extends FilterableManifest<M>>
- java.lang.Object
-
- com.google.android.exoplayer2.offline.SegmentDownloader<M>
-
- Type Parameters:
M- The type of the manifest object.
- All Implemented Interfaces:
Downloader
- Direct Known Subclasses:
DashDownloader,HlsDownloader,SsDownloader
public abstract class SegmentDownloader<M extends FilterableManifest<M>> extends Object implements Downloader
Base class for multi segment stream downloaders.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description protected static classSegmentDownloader.SegmentSmallest unit of content to be downloaded.-
Nested classes/interfaces inherited from interface com.google.android.exoplayer2.offline.Downloader
Downloader.ProgressListener
-
-
Constructor Summary
Constructors Constructor Description SegmentDownloader(MediaItem mediaItem, ParsingLoadable.Parser<M> manifestParser, CacheDataSource.Factory cacheDataSourceFactory, Executor executor)
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description voidcancel()Permanently cancels the downloading by this downloader.voiddownload(Downloader.ProgressListener progressListener)Downloads the content.protected <T> Texecute(RunnableFutureTask<T,?> runnable, boolean removing)Executes the providedRunnableFutureTask.protected static DataSpecgetCompressibleDataSpec(Uri uri)protected MgetManifest(DataSource dataSource, DataSpec dataSpec, boolean removing)Loads and parses a manifest.protected abstract List<SegmentDownloader.Segment>getSegments(DataSource dataSource, M manifest, boolean removing)Returns a list of all downloadableSegmentDownloader.Segments for a given manifest.voidremove()Removes the content.
-
-
-
Constructor Detail
-
SegmentDownloader
public SegmentDownloader(MediaItem mediaItem, ParsingLoadable.Parser<M> manifestParser, CacheDataSource.Factory cacheDataSourceFactory, Executor executor)
- Parameters:
mediaItem- TheMediaItemto be downloaded.manifestParser- A parser for manifests belonging to the media to be downloaded.cacheDataSourceFactory- ACacheDataSource.Factoryfor the cache into which the download will be written.executor- AnExecutorused to make requests for the media being downloaded. Providing anExecutorthat uses multiple threads will speed up the download by allowing parts of it to be executed in parallel.
-
-
Method Detail
-
download
public final void download(@Nullable Downloader.ProgressListener progressListener) throws IOException, InterruptedExceptionDescription copied from interface:DownloaderDownloads the content.If downloading fails, this method can be called again to resume the download. It cannot be called again after the download has been
canceled.If downloading is canceled whilst this method is executing, then it is expected that it will return reasonably quickly. However, there are no guarantees about how the method will return, meaning that it can return without throwing, or by throwing any of its documented exceptions. The caller must use its own knowledge about whether downloading has been canceled to determine whether this is why the method has returned, rather than relying on the method returning in a particular way.
- Specified by:
downloadin interfaceDownloader- Parameters:
progressListener- A listener to receive progress updates, ornull.- Throws:
IOException- If the download failed to complete successfully.InterruptedException- If the download was interrupted.
-
cancel
public void cancel()
Description copied from interface:DownloaderPermanently cancels the downloading by this downloader. The caller should also interrupt the downloading thread immediately after calling this method.Once canceled,
Downloader.download(com.google.android.exoplayer2.offline.Downloader.ProgressListener)cannot be called again.- Specified by:
cancelin interfaceDownloader
-
remove
public final void remove()
Description copied from interface:DownloaderRemoves the content.- Specified by:
removein interfaceDownloader
-
getManifest
protected final M getManifest(DataSource dataSource, DataSpec dataSpec, boolean removing) throws InterruptedException, IOException
Loads and parses a manifest.- Parameters:
dataSpec- The manifestDataSpec.removing- Whether the manifest is being loaded as part of the download being removed.- Returns:
- The loaded manifest.
- Throws:
InterruptedException- If the thread on which the method is called is interrupted.IOException- If an error occurs during execution.
-
execute
protected final <T> T execute(RunnableFutureTask<T,?> runnable, boolean removing) throws InterruptedException, IOException
Executes the providedRunnableFutureTask.- Parameters:
runnable- TheRunnableFutureTaskto execute.removing- Whether the execution is part of the download being removed.- Returns:
- The result.
- Throws:
InterruptedException- If the thread on which the method is called is interrupted.IOException- If an error occurs during execution.
-
getSegments
protected abstract List<SegmentDownloader.Segment> getSegments(DataSource dataSource, M manifest, boolean removing) throws IOException, InterruptedException
Returns a list of all downloadableSegmentDownloader.Segments for a given manifest. Any required data should be loaded usinggetManifest(com.google.android.exoplayer2.upstream.DataSource, com.google.android.exoplayer2.upstream.DataSpec, boolean)orexecute(com.google.android.exoplayer2.util.RunnableFutureTask<T, ?>, boolean).- Parameters:
dataSource- TheDataSourcethrough which to load any required data.manifest- The manifest containing the segments.removing- Whether the segments are being obtained as part of a removal. If true then a partial segment list is returned in the case that a load error prevents all segments from being listed. If false then anIOExceptionwill be thrown in this case.- Returns:
- The list of downloadable
SegmentDownloader.Segments. - Throws:
IOException- Thrown ifallowPartialIndexis false and an execution error occurs, or if the media is not in a form that allows for its segments to be listed.InterruptedException
-
-