Skip to content

Commit

Permalink
issue/130272: explicitly mark unstable api
Browse files Browse the repository at this point in the history
  • Loading branch information
emakar committed Apr 16, 2024
1 parent 3c0e29d commit 222c1a3
Showing 1 changed file with 64 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import android.net.Uri;
import android.view.Surface;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.annotation.OptIn;
import androidx.annotation.VisibleForTesting;
import androidx.media3.common.AudioAttributes;
Expand Down Expand Up @@ -42,9 +43,6 @@
import java.util.List;
import java.util.Map;

// API marked with UnstableApi is safe to use, it is only delicate when targeting stable ABI
// https://developer.android.com/media/media3/exoplayer/migration-guide#unstableapi
@OptIn(markerClass = UnstableApi.class)
final class VideoPlayer {
private static final String FORMAT_SS = "ss";
private static final String FORMAT_DASH = "dash";
Expand Down Expand Up @@ -90,7 +88,7 @@ final class VideoPlayer {

MediaSource mediaSource = buildMediaSource(uri, dataSourceFactory, formatHint);

exoPlayer.setMediaSource(mediaSource);
unstableSetMediaSource(exoPlayer, mediaSource);
exoPlayer.prepare();

setUpVideoPlayer(exoPlayer, new QueuingEventSink());
Expand Down Expand Up @@ -121,11 +119,8 @@ public void buildHttpDataSourceFactory(@NonNull Map<String, String> httpHeaders)
? httpHeaders.get(USER_AGENT)
: "ExoPlayer";

httpDataSourceFactory.setUserAgent(userAgent).setAllowCrossProtocolRedirects(true);

if (httpHeadersNotEmpty) {
httpDataSourceFactory.setDefaultRequestProperties(httpHeaders);
}
unstableUpdateDataSourceFactory(
httpDataSourceFactory, httpHeaders, userAgent, httpHeadersNotEmpty);
}

private MediaSource buildMediaSource(
Expand All @@ -152,26 +147,7 @@ private MediaSource buildMediaSource(
break;
}
}
switch (type) {
case C.CONTENT_TYPE_SS:
return new SsMediaSource.Factory(
new DefaultSsChunkSource.Factory(mediaDataSourceFactory), mediaDataSourceFactory)
.createMediaSource(MediaItem.fromUri(uri));
case C.CONTENT_TYPE_DASH:
return new DashMediaSource.Factory(
new DefaultDashChunkSource.Factory(mediaDataSourceFactory), mediaDataSourceFactory)
.createMediaSource(MediaItem.fromUri(uri));
case C.CONTENT_TYPE_HLS:
return new HlsMediaSource.Factory(mediaDataSourceFactory)
.createMediaSource(MediaItem.fromUri(uri));
case C.CONTENT_TYPE_OTHER:
return new ProgressiveMediaSource.Factory(mediaDataSourceFactory)
.createMediaSource(MediaItem.fromUri(uri));
default:
{
throw new IllegalStateException("Unsupported type: " + type);
}
}
return unstableBuildMediaSource(uri, mediaDataSourceFactory, type);
}

private void setUpVideoPlayer(ExoPlayer exoPlayer, QueuingEventSink eventSink) {
Expand Down Expand Up @@ -309,15 +285,15 @@ void sendInitialized() {
event.put("event", "initialized");
event.put("duration", exoPlayer.getDuration());

if (exoPlayer.getVideoFormat() != null) {
Format videoFormat = exoPlayer.getVideoFormat();
Format videoFormat = unstableGetVideoFormat(exoPlayer);
if (videoFormat != null) {
int width = videoFormat.width;
int height = videoFormat.height;
int rotationDegrees = videoFormat.rotationDegrees;
int rotationDegrees = unstableGetRotationDegrees(videoFormat);
// Switch the width/height if video was taken in portrait mode
if (rotationDegrees == 90 || rotationDegrees == 270) {
width = exoPlayer.getVideoFormat().height;
height = exoPlayer.getVideoFormat().width;
width = videoFormat.height;
height = videoFormat.width;
}
event.put("width", width);
event.put("height", height);
Expand Down Expand Up @@ -348,4 +324,58 @@ void dispose() {
exoPlayer.release();
}
}

@OptIn(markerClass = UnstableApi.class)
private static void unstableSetMediaSource(ExoPlayer exoPlayer, MediaSource mediaSource) {
exoPlayer.setMediaSource(mediaSource);
}

@OptIn(markerClass = UnstableApi.class)
private static void unstableUpdateDataSourceFactory(
DefaultHttpDataSource.Factory factory,
@NonNull Map<String, String> httpHeaders,
String userAgent,
boolean httpHeadersNotEmpty) {
factory.setUserAgent(userAgent).setAllowCrossProtocolRedirects(true);

if (httpHeadersNotEmpty) {
factory.setDefaultRequestProperties(httpHeaders);
}
}

@OptIn(markerClass = UnstableApi.class)
private static MediaSource unstableBuildMediaSource(
Uri uri, DataSource.Factory mediaDataSourceFactory, int type) {
switch (type) {
case C.CONTENT_TYPE_SS:
return new SsMediaSource.Factory(
new DefaultSsChunkSource.Factory(mediaDataSourceFactory), mediaDataSourceFactory)
.createMediaSource(MediaItem.fromUri(uri));
case C.CONTENT_TYPE_DASH:
return new DashMediaSource.Factory(
new DefaultDashChunkSource.Factory(mediaDataSourceFactory), mediaDataSourceFactory)
.createMediaSource(MediaItem.fromUri(uri));
case C.CONTENT_TYPE_HLS:
return new HlsMediaSource.Factory(mediaDataSourceFactory)
.createMediaSource(MediaItem.fromUri(uri));
case C.CONTENT_TYPE_OTHER:
return new ProgressiveMediaSource.Factory(mediaDataSourceFactory)
.createMediaSource(MediaItem.fromUri(uri));
default:
{
throw new IllegalStateException("Unsupported type: " + type);
}
}
}

@OptIn(markerClass = UnstableApi.class)
@Nullable
private static Format unstableGetVideoFormat(ExoPlayer exoPlayer) {
return exoPlayer.getVideoFormat();
}

@OptIn(markerClass = UnstableApi.class)
private static int unstableGetRotationDegrees(Format videoFormat) {
return videoFormat.rotationDegrees;
}
}

0 comments on commit 222c1a3

Please sign in to comment.