From f329adbc23ed01466a98481ec04f6702784f5c45 Mon Sep 17 00:00:00 2001 From: olly Date: Mon, 2 Aug 2021 19:06:55 +0100 Subject: [PATCH] Opt ExoPlayer out of transcoding when reading content URIs PiperOrigin-RevId: 388260014 --- RELEASENOTES.md | 1 + constants.gradle | 4 +-- .../upstream/ContentDataSource.java | 36 +++++++++++++++++-- 3 files changed, 37 insertions(+), 4 deletions(-) diff --git a/RELEASENOTES.md b/RELEASENOTES.md index e59c861854..424a083c67 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -50,6 +50,7 @@ * Change interface of `LoadErrorHandlingPolicy` to support configuring the behavior of track and location fallback. Location fallback is currently only supported for DASH manifests with multiple base URLs. + * Disable platform transcoding when playing content URIs on Android 12. * Remove deprecated symbols: * Remove `Player.getPlaybackError`. Use `Player.getPlayerError` instead. * Remove `Player.getCurrentTag`. Use `Player.getCurrentMediaItem` and diff --git a/constants.gradle b/constants.gradle index 94dd89fc6a..bb93d790dc 100644 --- a/constants.gradle +++ b/constants.gradle @@ -17,8 +17,8 @@ project.ext { releaseVersionCode = 2014002 minSdkVersion = 16 appTargetSdkVersion = 29 - targetSdkVersion = 30 - compileSdkVersion = 30 + targetSdkVersion = 31 + compileSdkVersion = 31 dexmakerVersion = '2.28.1' junitVersion = '4.13.2' // Use the same Guava version as the Android repo: diff --git a/library/core/src/main/java/com/google/android/exoplayer2/upstream/ContentDataSource.java b/library/core/src/main/java/com/google/android/exoplayer2/upstream/ContentDataSource.java index 3e53896ac0..b18ec6647f 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/upstream/ContentDataSource.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/upstream/ContentDataSource.java @@ -21,10 +21,18 @@ import static java.lang.Math.min; import android.content.ContentResolver; import android.content.Context; import android.content.res.AssetFileDescriptor; +import android.media.ApplicationMediaCapabilities; +import android.media.MediaFeature; +import android.media.MediaFormat; import android.net.Uri; +import android.os.Bundle; +import android.provider.MediaStore; +import androidx.annotation.DoNotInline; import androidx.annotation.Nullable; +import androidx.annotation.RequiresApi; import com.google.android.exoplayer2.C; import com.google.android.exoplayer2.PlaybackException; +import com.google.android.exoplayer2.util.Util; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; @@ -70,10 +78,17 @@ public final class ContentDataSource extends BaseDataSource { this.uri = uri; transferInitializing(dataSpec); - AssetFileDescriptor assetFileDescriptor = resolver.openAssetFileDescriptor(uri, "r"); + + Bundle providerOptions = new Bundle(); + if (Util.SDK_INT >= 31) { + Api31.disableTranscoding(providerOptions); + } + + AssetFileDescriptor assetFileDescriptor = + resolver.openTypedAssetFileDescriptor(uri, /* mimeType= */ "*/*", providerOptions); this.assetFileDescriptor = assetFileDescriptor; if (assetFileDescriptor == null) { - // openAssetFileDescriptor returns null if the provider recently crashed. + // openTypedAssetFileDescriptor returns null if the provider recently crashed. throw new ContentDataSourceException( new IOException("Could not open file descriptor for: " + uri), PlaybackException.ERROR_CODE_IO_UNSPECIFIED); @@ -205,4 +220,21 @@ public final class ContentDataSource extends BaseDataSource { } } } + + @RequiresApi(31) + private static final class Api31 { + + @DoNotInline + public static void disableTranscoding(Bundle providerOptions) { + ApplicationMediaCapabilities mediaCapabilities = + new ApplicationMediaCapabilities.Builder() + .addSupportedVideoMimeType(MediaFormat.MIMETYPE_VIDEO_HEVC) + .addSupportedHdrType(MediaFeature.HdrType.DOLBY_VISION) + .addSupportedHdrType(MediaFeature.HdrType.HDR10) + .addSupportedHdrType(MediaFeature.HdrType.HDR10_PLUS) + .addSupportedHdrType(MediaFeature.HdrType.HLG) + .build(); + providerOptions.putParcelable(MediaStore.EXTRA_MEDIA_CAPABILITIES, mediaCapabilities); + } + } }