SegmentDownloader

@UnstableApi
public abstract class SegmentDownloader<M extends FilterableManifest<M>> implements Downloader

Known direct subclasses
DashDownloader

A downloader for DASH streams.

HlsDownloader

A downloader for HLS streams.

SsDownloader

A downloader for SmoothStreaming streams.


Base class for multi segment stream downloaders.

Parameters
<M extends FilterableManifest<M>>

The type of the manifest object.

Summary

Nested types

protected class SegmentDownloader.Segment implements Comparable

Smallest unit of content to be downloaded.

Constants

static final long

Public constructors

SegmentDownloader(
    MediaItem mediaItem,
    ParsingLoadable.Parser<M> manifestParser,
    CacheDataSource.Factory cacheDataSourceFactory,
    Executor executor
)

This method is deprecated.

Use SegmentDownloader instead.

SegmentDownloader(
    MediaItem mediaItem,
    ParsingLoadable.Parser<M> manifestParser,
    CacheDataSource.Factory cacheDataSourceFactory,
    Executor executor,
    long maxMergedSegmentStartTimeDiffMs
)

Public methods

void

Permanently cancels the downloading by this downloader.

final void

Downloads the content.

final void

Removes the content.

Protected methods

final T
<T> execute(RunnableFutureTask<T, Object> runnable, boolean removing)

Executes the provided RunnableFutureTask.

static DataSpec
final M
getManifest(DataSource dataSource, DataSpec dataSpec, boolean removing)

Loads and parses a manifest.

abstract List<SegmentDownloader.Segment>
getSegments(DataSource dataSource, M manifest, boolean removing)

Returns a list of all downloadable Segments for a given manifest.

Constants

DEFAULT_MAX_MERGED_SEGMENT_START_TIME_DIFF_MS

public static final long DEFAULT_MAX_MERGED_SEGMENT_START_TIME_DIFF_MS = 20000

Public constructors

SegmentDownloader

public SegmentDownloader(
    MediaItem mediaItem,
    ParsingLoadable.Parser<M> manifestParser,
    CacheDataSource.Factory cacheDataSourceFactory,
    Executor executor
)

SegmentDownloader

public SegmentDownloader(
    MediaItem mediaItem,
    ParsingLoadable.Parser<M> manifestParser,
    CacheDataSource.Factory cacheDataSourceFactory,
    Executor executor,
    long maxMergedSegmentStartTimeDiffMs
)
Parameters
MediaItem mediaItem

The MediaItem to be downloaded.

ParsingLoadable.Parser<M> manifestParser

A parser for manifests belonging to the media to be downloaded.

CacheDataSource.Factory cacheDataSourceFactory

A CacheDataSource.Factory for the cache into which the download will be written.

Executor executor

An Executor used to make requests for the media being downloaded. Providing an Executor that uses multiple threads will speed up the download by allowing parts of it to be executed in parallel.

long maxMergedSegmentStartTimeDiffMs

The maximum difference of the start time of two segments, up to which the segments (of the same URI) should be merged into a single download segment, in milliseconds.

Public methods

cancel

public void cancel()

Permanently cancels the downloading by this downloader. The caller should also interrupt the downloading thread immediately after calling this method.

Once canceled, download cannot be called again.

download

public final void download(@Nullable Downloader.ProgressListener progressListener)

Downloads 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.

Parameters
@Nullable Downloader.ProgressListener progressListener

A listener to receive progress updates, or null.

Throws
java.io.IOException

If the download failed to complete successfully.

java.lang.InterruptedException

If the download was interrupted.

java.util.concurrent.CancellationException

If the download was canceled.

remove

public final void remove()

Removes the content.

Protected methods

execute

protected final T <T> execute(RunnableFutureTask<T, Object> runnable, boolean removing)

Executes the provided RunnableFutureTask.

Parameters
RunnableFutureTask<T, Object> runnable

The RunnableFutureTask to execute.

boolean removing

Whether the execution is part of the download being removed.

Returns
T

The result.

Throws
java.lang.InterruptedException

If the thread on which the method is called is interrupted.

java.io.IOException

If an error occurs during execution.

getCompressibleDataSpec

protected static DataSpec getCompressibleDataSpec(Uri uri)

getManifest

protected final M getManifest(DataSource dataSource, DataSpec dataSpec, boolean removing)

Loads and parses a manifest.

Parameters
DataSource dataSource

The source to use when loading the manifest.

DataSpec dataSpec

The manifest DataSpec.

boolean removing

Whether the manifest is being loaded as part of the download being removed.

Returns
M

The loaded manifest.

Throws
java.lang.InterruptedException

If the thread on which the method is called is interrupted.

java.io.IOException

If an error occurs during execution.

getSegments

protected abstract List<SegmentDownloader.SegmentgetSegments(DataSource dataSource, M manifest, boolean removing)

Returns a list of all downloadable Segments for a given manifest. Any required data should be loaded using getManifest or execute.

Parameters
DataSource dataSource

The DataSource through which to load any required data.

M manifest

The manifest containing the segments.

boolean 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 an IOException will be thrown in this case.

Returns
List<SegmentDownloader.Segment>

The list of downloadable Segments.

Throws
java.io.IOException

Thrown if allowPartialIndex is false and an execution error occurs, or if the media is not in a form that allows for its segments to be listed.

java.lang.InterruptedException