Add a Factory for DownloadHelper and deprecate forMediaItem() methods

PiperOrigin-RevId: 739909021
This commit is contained in:
tianyifeng 2025-03-24 06:01:30 -07:00 committed by Copybara-Service
parent ae563dbab0
commit a1738f96f9
3 changed files with 209 additions and 237 deletions

View File

@ -45,6 +45,8 @@
`DownloadRequest` carries the resolved byte range, with which a `DownloadRequest` carries the resolved byte range, with which a
`ProgressiveDownloader` can be created and download the content `ProgressiveDownloader` can be created and download the content
correspondingly. correspondingly.
* Add `DownloadHelper.Factory` with which the static
`DownloadHelper.forMediaItem()` methods are replaced.
* OkHttp extension: * OkHttp extension:
* Cronet extension: * Cronet extension:
* RTMP extension: * RTMP extension:

View File

@ -78,6 +78,7 @@ import androidx.media3.exoplayer.upstream.DefaultAllocator;
import androidx.media3.extractor.ExtractorsFactory; import androidx.media3.extractor.ExtractorsFactory;
import androidx.media3.extractor.SeekMap; import androidx.media3.extractor.SeekMap;
import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableList;
import com.google.errorprone.annotations.CanIgnoreReturnValue;
import java.io.IOException; import java.io.IOException;
import java.lang.annotation.Documented; import java.lang.annotation.Documented;
import java.lang.annotation.Retention; import java.lang.annotation.Retention;
@ -111,6 +112,113 @@ import org.checkerframework.checker.nullness.qual.RequiresNonNull;
@UnstableApi @UnstableApi
public final class DownloadHelper { public final class DownloadHelper {
/** A factory of {@link DownloadHelper}. */
public static final class Factory {
@Nullable private DataSource.Factory dataSourceFactory;
@Nullable private RenderersFactory renderersFactory;
private TrackSelectionParameters trackSelectionParameters;
@Nullable private DrmSessionManager drmSessionManager;
private boolean debugLoggingEnabled;
/** Creates a {@link Factory}. */
public Factory() {
this.trackSelectionParameters = DEFAULT_TRACK_SELECTOR_PARAMETERS;
}
/**
* Sets a {@link DataSource.Factory} used to load the manifest for adaptive streams or the
* {@link SeekMap} for progressive streams. The default is {@code null}.
*
* <p>A {@link DataSource.Factory} is required for adaptive streams or when requesting partial
* downloads for progressive streams. In the latter case, this has to be a {@link
* CacheDataSource.Factory} for the {@link Cache} into which downloads will be written.
*
* @return This factory, for convenience.
*/
@CanIgnoreReturnValue
public Factory setDataSourceFactory(@Nullable DataSource.Factory dataSourceFactory) {
this.dataSourceFactory = dataSourceFactory;
return this;
}
/**
* Sets a {@link RenderersFactory} creating the renderers for which tracks are selected. The
* default is {@code null}.
*
* <p>This is only used for adaptive streams.
*
* @return This factory, for convenience.
*/
@CanIgnoreReturnValue
public Factory setRenderersFactory(@Nullable RenderersFactory renderersFactory) {
this.renderersFactory = renderersFactory;
return this;
}
/**
* Sets a {@link TrackSelectionParameters} for selecting tracks for downloading. The default is
* {@link #DEFAULT_TRACK_SELECTOR_PARAMETERS}.
*
* <p>This is only used for adaptive streams.
*
* @return This factory, for convenience.
*/
@CanIgnoreReturnValue
public Factory setTrackSelectionParameters(TrackSelectionParameters trackSelectionParameters) {
this.trackSelectionParameters = trackSelectionParameters;
return this;
}
/**
* Sets a {@link DrmSessionManager}. Used to help determine which tracks can be selected. The
* default is {@code null}.
*
* @return This factory, for convenience.
*/
@CanIgnoreReturnValue
public Factory setDrmSessionManager(@Nullable DrmSessionManager drmSessionManager) {
this.drmSessionManager = drmSessionManager;
return this;
}
/**
* Sets whether to log debug information. The default is {@code false}.
*
* @return This factory, for convenience.
*/
@CanIgnoreReturnValue
public Factory setDebugLoggingEnabled(boolean debugLoggingEnabled) {
this.debugLoggingEnabled = debugLoggingEnabled;
return this;
}
/**
* Creates a new {@link DownloadHelper}.
*
* @param mediaItem The {@link MediaItem} to download.
* @throws IllegalStateException If the corresponding module is missing for DASH, HLS or
* SmoothStreaming media items.
* @throws IllegalArgumentException If the {@code dataSourceFactory} is null for adaptive
* streams.
*/
public DownloadHelper create(MediaItem mediaItem) {
boolean isProgressive = isProgressive(checkNotNull(mediaItem.localConfiguration));
Assertions.checkArgument(isProgressive || dataSourceFactory != null);
return new DownloadHelper(
mediaItem,
isProgressive && dataSourceFactory == null
? null
: createMediaSourceInternal(
mediaItem, castNonNull(dataSourceFactory), drmSessionManager),
trackSelectionParameters,
renderersFactory != null
? new DefaultRendererCapabilitiesList.Factory(renderersFactory)
.createRendererCapabilitiesList()
: new UnreleaseableRendererCapabilitiesList(new RendererCapabilities[0]),
debugLoggingEnabled);
}
}
/** Default track selection parameters for downloading. */ /** Default track selection parameters for downloading. */
public static final DefaultTrackSelector.Parameters DEFAULT_TRACK_SELECTOR_PARAMETERS = public static final DefaultTrackSelector.Parameters DEFAULT_TRACK_SELECTOR_PARAMETERS =
DefaultTrackSelector.Parameters.DEFAULT DefaultTrackSelector.Parameters.DEFAULT
@ -172,241 +280,126 @@ public final class DownloadHelper {
public static class LiveContentUnsupportedException extends IOException {} public static class LiveContentUnsupportedException extends IOException {}
/** /**
* Creates a {@link DownloadHelper} for the given progressive media item. * @deprecated Use {@link Factory#create(MediaItem)} instead.
*
* @param context The context.
* @param mediaItem A {@link MediaItem}.
* @return A {@link DownloadHelper} for progressive streams.
* @throws IllegalStateException If the media item is of type DASH, HLS or SmoothStreaming.
*/ */
@Deprecated
public static DownloadHelper forMediaItem(Context context, MediaItem mediaItem) { public static DownloadHelper forMediaItem(Context context, MediaItem mediaItem) {
Assertions.checkArgument(isProgressive(checkNotNull(mediaItem.localConfiguration))); Assertions.checkArgument(isProgressive(checkNotNull(mediaItem.localConfiguration)));
return forMediaItem( return new DownloadHelper.Factory().create(mediaItem);
mediaItem,
DEFAULT_TRACK_SELECTOR_PARAMETERS,
/* renderersFactory= */ null,
/* dataSourceFactory= */ null,
/* drmSessionManager= */ null);
} }
/** /**
* Creates a {@link DownloadHelper} for the given media item. * @deprecated Use {@link Factory#create(MediaItem)} instead.
*
* @param context The context.
* @param mediaItem A {@link MediaItem}.
* @param dataSourceFactory A {@link DataSource.Factory} used to load the manifest for adaptive
* streams or the {@link SeekMap} for progressive streams. In the latter case, this has to be
* a {@link CacheDataSource.Factory} for the {@link Cache} into which downloads will be
* written.
* @throws IllegalStateException If the corresponding module is missing for DASH, HLS or
* SmoothStreaming media items.
*/ */
@Deprecated
public static DownloadHelper forMediaItem( public static DownloadHelper forMediaItem(
Context context, MediaItem mediaItem, DataSource.Factory dataSourceFactory) { Context context, MediaItem mediaItem, DataSource.Factory dataSourceFactory) {
return forMediaItem(context, mediaItem, dataSourceFactory, /* debugLoggingEnabled= */ false); return new DownloadHelper.Factory().setDataSourceFactory(dataSourceFactory).create(mediaItem);
} }
/** /**
* Creates a {@link DownloadHelper} for the given media item. * @deprecated Use {@link Factory#create(MediaItem)} instead.
*
* @param context The context.
* @param mediaItem A {@link MediaItem}.
* @param dataSourceFactory A {@link DataSource.Factory} used to load the manifest for adaptive
* streams or the {@link SeekMap} for progressive streams. In the latter case, this has to be
* a {@link CacheDataSource.Factory} for the {@link Cache} into which downloads will be
* written.
* @param debugLoggingEnabled Whether to log debug information.
* @throws IllegalStateException If the corresponding module is missing for DASH, HLS or
* SmoothStreaming media items.
*/ */
@Deprecated
public static DownloadHelper forMediaItem( public static DownloadHelper forMediaItem(
Context context, Context context,
MediaItem mediaItem, MediaItem mediaItem,
DataSource.Factory dataSourceFactory, DataSource.Factory dataSourceFactory,
boolean debugLoggingEnabled) { boolean debugLoggingEnabled) {
return forMediaItem( return new DownloadHelper.Factory()
mediaItem, .setDataSourceFactory(dataSourceFactory)
getDefaultTrackSelectorParameters(context), .setDebugLoggingEnabled(debugLoggingEnabled)
/* renderersFactory= */ null, .create(mediaItem);
dataSourceFactory,
/* drmSessionManager= */ null,
debugLoggingEnabled);
} }
/** /**
* Creates a {@link DownloadHelper} for the given media item. * @deprecated Use {@link Factory#create(MediaItem)} instead.
*
* @param context The context.
* @param mediaItem A {@link MediaItem}.
* @param renderersFactory A {@link RenderersFactory} creating the renderers for which tracks are
* selected.
* @param dataSourceFactory A {@link DataSource.Factory} used to load the manifest for adaptive
* streams or the {@link SeekMap} for progressive streams. This argument is required for
* adaptive streams or when requesting partial downloads for progressive streams. In the
* latter case, this has to be a {@link CacheDataSource.Factory} for the {@link Cache} into
* which downloads will be written.
* @throws IllegalStateException If the corresponding module is missing for DASH, HLS or
* SmoothStreaming media items.
* @throws IllegalArgumentException If the {@code dataSourceFactory} is null for adaptive streams.
*/ */
@Deprecated
public static DownloadHelper forMediaItem( public static DownloadHelper forMediaItem(
Context context, Context context,
MediaItem mediaItem, MediaItem mediaItem,
@Nullable RenderersFactory renderersFactory, @Nullable RenderersFactory renderersFactory,
@Nullable DataSource.Factory dataSourceFactory) { @Nullable DataSource.Factory dataSourceFactory) {
return forMediaItem( return new DownloadHelper.Factory()
context, mediaItem, renderersFactory, dataSourceFactory, /* debugLoggingEnabled= */ false); .setDataSourceFactory(dataSourceFactory)
.setRenderersFactory(renderersFactory)
.create(mediaItem);
} }
/** /**
* Creates a {@link DownloadHelper} for the given media item. * @deprecated Use {@link Factory#create(MediaItem)} instead.
*
* @param context The context.
* @param mediaItem A {@link MediaItem}.
* @param renderersFactory A {@link RenderersFactory} creating the renderers for which tracks are
* selected.
* @param dataSourceFactory A {@link DataSource.Factory} used to load the manifest for adaptive
* streams or the {@link SeekMap} for progressive streams. This argument is required for
* adaptive streams or when requesting partial downloads for progressive streams. In the
* latter case, this has to be a {@link CacheDataSource.Factory} for the {@link Cache} into
* which downloads will be written.
* @param debugLoggingEnabled Whether to log debug information.
* @throws IllegalStateException If the corresponding module is missing for DASH, HLS or
* SmoothStreaming media items.
* @throws IllegalArgumentException If the {@code dataSourceFactory} is null for adaptive streams.
*/ */
@Deprecated
public static DownloadHelper forMediaItem( public static DownloadHelper forMediaItem(
Context context, Context context,
MediaItem mediaItem, MediaItem mediaItem,
@Nullable RenderersFactory renderersFactory, @Nullable RenderersFactory renderersFactory,
@Nullable DataSource.Factory dataSourceFactory, @Nullable DataSource.Factory dataSourceFactory,
boolean debugLoggingEnabled) { boolean debugLoggingEnabled) {
return forMediaItem( return new DownloadHelper.Factory()
mediaItem, .setDataSourceFactory(dataSourceFactory)
DEFAULT_TRACK_SELECTOR_PARAMETERS, .setRenderersFactory(renderersFactory)
renderersFactory, .setDebugLoggingEnabled(debugLoggingEnabled)
dataSourceFactory, .create(mediaItem);
/* drmSessionManager= */ null,
debugLoggingEnabled);
} }
/** /**
* Creates a {@link DownloadHelper} for the given media item. * @deprecated Use {@link Factory#create(MediaItem)} instead.
*
* @param mediaItem A {@link MediaItem}.
* @param renderersFactory A {@link RenderersFactory} creating the renderers for which tracks are
* selected.
* @param trackSelectionParameters {@link TrackSelectionParameters} for selecting tracks for
* downloading.
* @param dataSourceFactory A {@link DataSource.Factory} used to load the manifest for adaptive
* streams or the {@link SeekMap} for progressive streams. This argument is required for
* adaptive streams or when requesting partial downloads for progressive streams. In the
* latter case, this has to be a {@link CacheDataSource.Factory} for the {@link Cache} into
* which downloads will be written.
* @throws IllegalStateException If the corresponding module is missing for DASH, HLS or
* SmoothStreaming media items.
* @throws IllegalArgumentException If the {@code dataSourceFactory} is null for adaptive streams.
*/ */
@Deprecated
public static DownloadHelper forMediaItem( public static DownloadHelper forMediaItem(
MediaItem mediaItem, MediaItem mediaItem,
TrackSelectionParameters trackSelectionParameters, TrackSelectionParameters trackSelectionParameters,
@Nullable RenderersFactory renderersFactory, @Nullable RenderersFactory renderersFactory,
@Nullable DataSource.Factory dataSourceFactory) { @Nullable DataSource.Factory dataSourceFactory) {
return forMediaItem( return new DownloadHelper.Factory()
mediaItem, .setDataSourceFactory(dataSourceFactory)
trackSelectionParameters, .setTrackSelectionParameters(trackSelectionParameters)
renderersFactory, .setRenderersFactory(renderersFactory)
dataSourceFactory, .create(mediaItem);
/* debugLoggingEnabled= */ false);
} }
/** /**
* Creates a {@link DownloadHelper} for the given media item. * @deprecated Use {@link Factory#create(MediaItem)} instead.
*
* @param mediaItem A {@link MediaItem}.
* @param renderersFactory A {@link RenderersFactory} creating the renderers for which tracks are
* selected.
* @param trackSelectionParameters {@link TrackSelectionParameters} for selecting tracks for
* downloading.
* @param dataSourceFactory A {@link DataSource.Factory} used to load the manifest for adaptive
* streams or the {@link SeekMap} for progressive streams. This argument is required for
* adaptive streams or when requesting partial downloads for progressive streams. In the
* latter case, this has to be a {@link CacheDataSource.Factory} for the {@link Cache} into
* which downloads will be written.
* @param debugLoggingEnabled Whether to log debug information.
* @throws IllegalStateException If the corresponding module is missing for DASH, HLS or
* SmoothStreaming media items.
* @throws IllegalArgumentException If the {@code dataSourceFactory} is null for adaptive streams.
*/ */
@Deprecated
public static DownloadHelper forMediaItem( public static DownloadHelper forMediaItem(
MediaItem mediaItem, MediaItem mediaItem,
TrackSelectionParameters trackSelectionParameters, TrackSelectionParameters trackSelectionParameters,
@Nullable RenderersFactory renderersFactory, @Nullable RenderersFactory renderersFactory,
@Nullable DataSource.Factory dataSourceFactory, @Nullable DataSource.Factory dataSourceFactory,
boolean debugLoggingEnabled) { boolean debugLoggingEnabled) {
return forMediaItem( return new DownloadHelper.Factory()
mediaItem, .setDataSourceFactory(dataSourceFactory)
trackSelectionParameters, .setTrackSelectionParameters(trackSelectionParameters)
renderersFactory, .setRenderersFactory(renderersFactory)
dataSourceFactory, .setDebugLoggingEnabled(debugLoggingEnabled)
/* drmSessionManager= */ null, .create(mediaItem);
debugLoggingEnabled);
} }
/** /**
* Creates a {@link DownloadHelper} for the given media item. * @deprecated Use {@link Factory#create(MediaItem)} instead.
*
* @param mediaItem A {@link MediaItem}.
* @param renderersFactory A {@link RenderersFactory} creating the renderers for which tracks are
* selected.
* @param trackSelectionParameters {@link TrackSelectionParameters} for selecting tracks for
* downloading.
* @param dataSourceFactory A {@link DataSource.Factory} used to load the manifest for adaptive
* streams or the {@link SeekMap} for progressive streams. This argument is required for
* adaptive streams or when requesting partial downloads for progressive streams. In the
* latter case, this has to be a {@link CacheDataSource.Factory} for the {@link Cache} into
* which downloads will be written.
* @param drmSessionManager An optional {@link DrmSessionManager}. Used to help determine which
* tracks can be selected.
* @throws IllegalStateException If the corresponding module is missing for DASH, HLS or
* SmoothStreaming media items.
* @throws IllegalArgumentException If the {@code dataSourceFactory} is null for adaptive streams.
*/ */
@Deprecated
public static DownloadHelper forMediaItem( public static DownloadHelper forMediaItem(
MediaItem mediaItem, MediaItem mediaItem,
TrackSelectionParameters trackSelectionParameters, TrackSelectionParameters trackSelectionParameters,
@Nullable RenderersFactory renderersFactory, @Nullable RenderersFactory renderersFactory,
@Nullable DataSource.Factory dataSourceFactory, @Nullable DataSource.Factory dataSourceFactory,
@Nullable DrmSessionManager drmSessionManager) { @Nullable DrmSessionManager drmSessionManager) {
return forMediaItem( return new DownloadHelper.Factory()
mediaItem, .setDataSourceFactory(dataSourceFactory)
trackSelectionParameters, .setTrackSelectionParameters(trackSelectionParameters)
renderersFactory, .setRenderersFactory(renderersFactory)
dataSourceFactory, .setDrmSessionManager(drmSessionManager)
drmSessionManager, .create(mediaItem);
/* debugLoggingEnabled= */ false);
} }
/** /**
* Creates a {@link DownloadHelper} for the given media item. * @deprecated Use {@link Factory#create(MediaItem)} instead.
*
* @param mediaItem A {@link MediaItem}.
* @param renderersFactory A {@link RenderersFactory} creating the renderers for which tracks are
* selected.
* @param trackSelectionParameters {@link TrackSelectionParameters} for selecting tracks for
* downloading.
* @param dataSourceFactory A {@link DataSource.Factory} used to load the manifest for adaptive
* streams or the {@link SeekMap} for progressive streams. This argument is required for
* adaptive streams or when requesting partial downloads for progressive streams. In the
* latter case, this has to be a {@link CacheDataSource.Factory} for the {@link Cache} into
* which downloads will be written.
* @param drmSessionManager An optional {@link DrmSessionManager}. Used to help determine which
* tracks can be selected.
* @param debugLoggingEnabled Whether to log debug information.
* @throws IllegalStateException If the corresponding module is missing for DASH, HLS or
* SmoothStreaming media items.
* @throws IllegalArgumentException If the {@code dataSourceFactory} is null for adaptive streams.
*/ */
@Deprecated
public static DownloadHelper forMediaItem( public static DownloadHelper forMediaItem(
MediaItem mediaItem, MediaItem mediaItem,
TrackSelectionParameters trackSelectionParameters, TrackSelectionParameters trackSelectionParameters,
@ -414,20 +407,13 @@ public final class DownloadHelper {
@Nullable DataSource.Factory dataSourceFactory, @Nullable DataSource.Factory dataSourceFactory,
@Nullable DrmSessionManager drmSessionManager, @Nullable DrmSessionManager drmSessionManager,
boolean debugLoggingEnabled) { boolean debugLoggingEnabled) {
boolean isProgressive = isProgressive(checkNotNull(mediaItem.localConfiguration)); return new DownloadHelper.Factory()
Assertions.checkArgument(isProgressive || dataSourceFactory != null); .setDataSourceFactory(dataSourceFactory)
return new DownloadHelper( .setTrackSelectionParameters(trackSelectionParameters)
mediaItem, .setRenderersFactory(renderersFactory)
isProgressive && dataSourceFactory == null .setDrmSessionManager(drmSessionManager)
? null .setDebugLoggingEnabled(debugLoggingEnabled)
: createMediaSourceInternal( .create(mediaItem);
mediaItem, castNonNull(dataSourceFactory), drmSessionManager),
trackSelectionParameters,
renderersFactory != null
? new DefaultRendererCapabilitiesList.Factory(renderersFactory)
.createRendererCapabilitiesList()
: new UnreleaseableRendererCapabilitiesList(new RendererCapabilities[0]),
debugLoggingEnabled);
} }
/** /**

View File

@ -141,8 +141,7 @@ public class DownloadHelperTest {
public void prepare_withoutMediaSource_tracksInfoNotAvailable() throws Exception { public void prepare_withoutMediaSource_tracksInfoNotAvailable() throws Exception {
// DownloadHelper will be constructed without MediaSource if no DataSource.Factory is provided. // DownloadHelper will be constructed without MediaSource if no DataSource.Factory is provided.
DownloadHelper downloadHelper = DownloadHelper downloadHelper =
DownloadHelper.forMediaItem( new DownloadHelper.Factory().create(MediaItem.fromUri("asset:///media/mp4/sample.mp4"));
getApplicationContext(), MediaItem.fromUri("asset:///media/mp4/sample.mp4"));
boolean tracksInfoAvailable = prepareDownloadHelper(downloadHelper); boolean tracksInfoAvailable = prepareDownloadHelper(downloadHelper);
@ -153,10 +152,9 @@ public class DownloadHelperTest {
public void prepare_prepareProgressiveSource_tracksInfoNotAvailable() throws Exception { public void prepare_prepareProgressiveSource_tracksInfoNotAvailable() throws Exception {
Context context = getApplicationContext(); Context context = getApplicationContext();
DownloadHelper downloadHelper = DownloadHelper downloadHelper =
DownloadHelper.forMediaItem( new DownloadHelper.Factory()
context, .setDataSourceFactory(new DefaultDataSource.Factory(context))
MediaItem.fromUri("asset:///media/mp4/sample.mp4"), .create(MediaItem.fromUri("asset:///media/mp4/sample.mp4"));
new DefaultDataSource.Factory(context));
boolean tracksInfoAvailable = prepareDownloadHelper(downloadHelper); boolean tracksInfoAvailable = prepareDownloadHelper(downloadHelper);
@ -489,12 +487,10 @@ public class DownloadHelperTest {
public void public void
getDownloadRequest_createsDownloadRequestWithConcreteTimeRange_requestContainsConcreteByteRange() getDownloadRequest_createsDownloadRequestWithConcreteTimeRange_requestContainsConcreteByteRange()
throws Exception { throws Exception {
Context context = getApplicationContext();
DownloadHelper downloadHelper = DownloadHelper downloadHelper =
DownloadHelper.forMediaItem( new DownloadHelper.Factory()
context, .setDataSourceFactory(new DefaultDataSource.Factory(getApplicationContext()))
MediaItem.fromUri("asset:///media/mp4/long_1080p_lowbitrate.mp4"), .create(MediaItem.fromUri("asset:///media/mp4/long_1080p_lowbitrate.mp4"));
new DefaultDataSource.Factory(context));
prepareDownloadHelper(downloadHelper); prepareDownloadHelper(downloadHelper);
DownloadRequest downloadRequest = DownloadRequest downloadRequest =
@ -510,12 +506,10 @@ public class DownloadHelperTest {
public void public void
getDownloadRequest_createsDownloadRequestWithUnsetStartPosition_requestContainsConcreteByteRange() getDownloadRequest_createsDownloadRequestWithUnsetStartPosition_requestContainsConcreteByteRange()
throws Exception { throws Exception {
Context context = getApplicationContext();
DownloadHelper downloadHelper = DownloadHelper downloadHelper =
DownloadHelper.forMediaItem( new DownloadHelper.Factory()
context, .setDataSourceFactory(new DefaultDataSource.Factory(getApplicationContext()))
MediaItem.fromUri("asset:///media/mp4/long_1080p_lowbitrate.mp4"), .create(MediaItem.fromUri("asset:///media/mp4/long_1080p_lowbitrate.mp4"));
new DefaultDataSource.Factory(context));
prepareDownloadHelper(downloadHelper); prepareDownloadHelper(downloadHelper);
DownloadRequest downloadRequest = DownloadRequest downloadRequest =
@ -530,12 +524,10 @@ public class DownloadHelperTest {
@Test @Test
public void getDownloadRequest_createsDownloadRequestWithUnsetLength_requestContainsUnsetLength() public void getDownloadRequest_createsDownloadRequestWithUnsetLength_requestContainsUnsetLength()
throws Exception { throws Exception {
Context context = getApplicationContext();
DownloadHelper downloadHelper = DownloadHelper downloadHelper =
DownloadHelper.forMediaItem( new DownloadHelper.Factory()
context, .setDataSourceFactory(new DefaultDataSource.Factory(getApplicationContext()))
MediaItem.fromUri("asset:///media/mp4/long_1080p_lowbitrate.mp4"), .create(MediaItem.fromUri("asset:///media/mp4/long_1080p_lowbitrate.mp4"));
new DefaultDataSource.Factory(context));
prepareDownloadHelper(downloadHelper); prepareDownloadHelper(downloadHelper);
DownloadRequest downloadRequest = DownloadRequest downloadRequest =
@ -551,12 +543,10 @@ public class DownloadHelperTest {
public void public void
getDownloadRequest_createsDownloadRequestForTooShortStreamWithTimeRange_requestContainsUnsetLength() getDownloadRequest_createsDownloadRequestForTooShortStreamWithTimeRange_requestContainsUnsetLength()
throws Exception { throws Exception {
Context context = getApplicationContext();
DownloadHelper downloadHelper = DownloadHelper downloadHelper =
DownloadHelper.forMediaItem( new DownloadHelper.Factory()
context, .setDataSourceFactory(new DefaultDataSource.Factory(getApplicationContext()))
MediaItem.fromUri("asset:///media/mp4/sample.mp4"), .create(MediaItem.fromUri("asset:///media/mp4/sample.mp4"));
new DefaultDataSource.Factory(context));
prepareDownloadHelper(downloadHelper); prepareDownloadHelper(downloadHelper);
DownloadRequest downloadRequest = DownloadRequest downloadRequest =
@ -572,12 +562,10 @@ public class DownloadHelperTest {
public void public void
getDownloadRequest_createsDownloadRequestWithoutTimeRange_requestContainsNullByteRange() getDownloadRequest_createsDownloadRequestWithoutTimeRange_requestContainsNullByteRange()
throws Exception { throws Exception {
Context context = getApplicationContext();
DownloadHelper downloadHelper = DownloadHelper downloadHelper =
DownloadHelper.forMediaItem( new DownloadHelper.Factory()
getApplicationContext(), .setDataSourceFactory(new DefaultDataSource.Factory(getApplicationContext()))
MediaItem.fromUri("asset:///media/mp4/sample.mp4"), .create(MediaItem.fromUri("asset:///media/mp4/sample.mp4"));
new DefaultDataSource.Factory(context));
prepareDownloadHelper(downloadHelper); prepareDownloadHelper(downloadHelper);
DownloadRequest downloadRequest = downloadHelper.getDownloadRequest(/* data= */ null); DownloadRequest downloadRequest = downloadHelper.getDownloadRequest(/* data= */ null);
@ -612,11 +600,10 @@ public class DownloadHelperTest {
(handler, videoListener, audioListener, metadata, text) -> (handler, videoListener, audioListener, metadata, text) ->
new Renderer[] {textRenderer, audioRenderer, videoRenderer}; new Renderer[] {textRenderer, audioRenderer, videoRenderer};
DownloadHelper downloadHelper = DownloadHelper downloadHelper =
DownloadHelper.forMediaItem( new DownloadHelper.Factory()
MediaItem.fromUri("asset:///media/mp4/sample.mp4"), .setDataSourceFactory(new DefaultDataSource.Factory(getApplicationContext()))
DownloadHelper.DEFAULT_TRACK_SELECTOR_PARAMETERS, .setRenderersFactory(renderersFactory)
renderersFactory, .create(MediaItem.fromUri("asset:///media/mp4/sample.mp4"));
new DefaultDataSource.Factory(getApplicationContext()));
prepareDownloadHelper(downloadHelper); prepareDownloadHelper(downloadHelper);
downloadHelper.release(); downloadHelper.release();
@ -634,8 +621,7 @@ public class DownloadHelperTest {
new Thread( new Thread(
() -> { () -> {
try { try {
downloadHelper.set( downloadHelper.set(new DownloadHelper.Factory().create(testMediaItem));
DownloadHelper.forMediaItem(getApplicationContext(), testMediaItem));
} catch (Throwable e) { } catch (Throwable e) {
exception.set(e); exception.set(e);
} }
@ -662,11 +648,10 @@ public class DownloadHelperTest {
(handler, videoListener, audioListener, metadata, text) -> (handler, videoListener, audioListener, metadata, text) ->
new Renderer[] {videoRenderer}; new Renderer[] {videoRenderer};
downloadHelper.set( downloadHelper.set(
DownloadHelper.forMediaItem( new DownloadHelper.Factory()
getApplicationContext(), .setDataSourceFactory(new FakeDataSource.Factory())
testMediaItem, .setRenderersFactory(renderersFactory)
renderersFactory, .create(testMediaItem));
new FakeDataSource.Factory()));
} catch (Throwable e) { } catch (Throwable e) {
exception.set(e); exception.set(e);
} }
@ -692,11 +677,10 @@ public class DownloadHelperTest {
(handler, videoListener, audioListener, metadata, text) -> (handler, videoListener, audioListener, metadata, text) ->
new Renderer[] {videoRenderer}; new Renderer[] {videoRenderer};
downloadHelper.set( downloadHelper.set(
DownloadHelper.forMediaItem( new DownloadHelper.Factory()
testMediaItem, .setDataSourceFactory(new FakeDataSource.Factory())
TrackSelectionParameters.DEFAULT, .setRenderersFactory(renderersFactory)
renderersFactory, .create(testMediaItem));
new FakeDataSource.Factory()));
} catch (Throwable e) { } catch (Throwable e) {
exception.set(e); exception.set(e);
} }
@ -723,16 +707,16 @@ public class DownloadHelperTest {
(handler, videoListener, audioListener, metadata, text) -> (handler, videoListener, audioListener, metadata, text) ->
new Renderer[] {videoRenderer}; new Renderer[] {videoRenderer};
downloadHelper.set( downloadHelper.set(
DownloadHelper.forMediaItem( new DownloadHelper.Factory()
testMediaItem, .setDataSourceFactory(new FakeDataSource.Factory())
TrackSelectionParameters.DEFAULT, .setRenderersFactory(renderersFactory)
renderersFactory, .setDrmSessionManager(
new FakeDataSource.Factory(), new DefaultDrmSessionManager.Builder()
new DefaultDrmSessionManager.Builder() .build(
.build( new HttpMediaDrmCallback(
new HttpMediaDrmCallback( /* defaultLicenseUrl= */ null,
/* defaultLicenseUrl= */ null, new DefaultDataSource.Factory(getApplicationContext()))))
new DefaultDataSource.Factory(getApplicationContext()))))); .create(testMediaItem));
} catch (Throwable e) { } catch (Throwable e) {
exception.set(e); exception.set(e);
} }