Skip to content

Commit

Permalink
issue/130272: make httpDataSourceFactory usage clear
Browse files Browse the repository at this point in the history
  • Loading branch information
emakar committed Jun 7, 2024
1 parent 6befcaa commit a134c80
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ final class VideoPlayer {

private final VideoPlayerOptions options;

private DefaultHttpDataSource.Factory httpDataSourceFactory = new DefaultHttpDataSource.Factory();
private final DefaultHttpDataSource.Factory httpDataSourceFactory;

VideoPlayer(
Context context,
Expand All @@ -78,9 +78,10 @@ final class VideoPlayer {
.setMimeType(mimeFromFormatHint(formatHint))
.build();

buildHttpDataSourceFactory(httpHeaders);
httpDataSourceFactory = new DefaultHttpDataSource.Factory();
configureHttpDataSourceFactory(httpHeaders);

ExoPlayer exoPlayer = buildExoPlayer(context);
ExoPlayer exoPlayer = buildExoPlayer(context, httpDataSourceFactory);

exoPlayer.setMediaItem(mediaItem);
exoPlayer.prepare();
Expand All @@ -106,7 +107,7 @@ final class VideoPlayer {
}

@VisibleForTesting
public void buildHttpDataSourceFactory(@NonNull Map<String, String> httpHeaders) {
public void configureHttpDataSourceFactory(@NonNull Map<String, String> httpHeaders) {
final boolean httpHeadersNotEmpty = !httpHeaders.isEmpty();
final String userAgent =
httpHeadersNotEmpty && httpHeaders.containsKey(USER_AGENT)
Expand All @@ -117,15 +118,6 @@ public void buildHttpDataSourceFactory(@NonNull Map<String, String> httpHeaders)
httpDataSourceFactory, httpHeaders, userAgent, httpHeadersNotEmpty);
}

@NonNull
private ExoPlayer buildExoPlayer(Context context) {
DataSource.Factory dataSourceFactory =
new DefaultDataSource.Factory(context, httpDataSourceFactory);
DefaultMediaSourceFactory mediaSourceFactory =
new DefaultMediaSourceFactory(context).setDataSourceFactory(dataSourceFactory);
return new ExoPlayer.Builder(context).setMediaSourceFactory(mediaSourceFactory).build();
}

private void setUpVideoPlayer(ExoPlayer exoPlayer, QueuingEventSink eventSink) {
this.exoPlayer = exoPlayer;
this.eventSink = eventSink;
Expand Down Expand Up @@ -301,6 +293,16 @@ void dispose() {
}
}

@NonNull
private static ExoPlayer buildExoPlayer(
Context context, DataSource.Factory baseDataSourceFactory) {
DataSource.Factory dataSourceFactory =
new DefaultDataSource.Factory(context, baseDataSourceFactory);
DefaultMediaSourceFactory mediaSourceFactory =
new DefaultMediaSourceFactory(context).setDataSourceFactory(dataSourceFactory);
return new ExoPlayer.Builder(context).setMediaSourceFactory(mediaSourceFactory).build();
}

@Nullable
private static String mimeFromFormatHint(@Nullable String formatHint) {
if (formatHint == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public void videoPlayer_buildsHttpDataSourceFactoryProperlyWhenHttpHeadersNull()
fakeEventSink,
httpDataSourceFactorySpy);

videoPlayer.buildHttpDataSourceFactory(new HashMap<>());
videoPlayer.configureHttpDataSourceFactory(new HashMap<>());

verify(httpDataSourceFactorySpy).setUserAgent("ExoPlayer");
verify(httpDataSourceFactorySpy).setAllowCrossProtocolRedirects(true);
Expand All @@ -97,7 +97,7 @@ public void videoPlayer_buildsHttpDataSourceFactoryProperlyWhenHttpHeadersNull()
}
};

videoPlayer.buildHttpDataSourceFactory(httpHeaders);
videoPlayer.configureHttpDataSourceFactory(httpHeaders);

verify(httpDataSourceFactorySpy).setUserAgent("userAgent");
verify(httpDataSourceFactorySpy).setAllowCrossProtocolRedirects(true);
Expand All @@ -122,7 +122,7 @@ public void videoPlayer_buildsHttpDataSourceFactoryProperlyWhenHttpHeadersNull()
}
};

videoPlayer.buildHttpDataSourceFactory(httpHeaders);
videoPlayer.configureHttpDataSourceFactory(httpHeaders);

verify(httpDataSourceFactorySpy).setUserAgent("ExoPlayer");
verify(httpDataSourceFactorySpy).setAllowCrossProtocolRedirects(true);
Expand Down

0 comments on commit a134c80

Please sign in to comment.