From b9157a9e232a9a8b1e1c1ce3e68f924d853a5c52 Mon Sep 17 00:00:00 2001 From: ibaker Date: Wed, 20 May 2020 10:53:24 +0100 Subject: [PATCH] Add Guava dependency to ExoPlayer Guava is heavily optimized for Android and the impact on binary size is minimal (and outweighed by the organic growth of the ExoPlayer library). This change also replaces Util.toArray() with Guava's Ints.toArray() in order to introduce a Guava usage into a range of modules. PiperOrigin-RevId: 312449093 --- RELEASENOTES.md | 1 + constants.gradle | 2 +- javadoc_combined.gradle | 4 +- javadoc_library.gradle | 4 +- library/common/build.gradle | 1 + .../google/android/exoplayer2/util/Util.java | 55 ++----------------- .../android/exoplayer2/util/UtilTest.java | 19 +------ library/core/build.gradle | 1 + .../source/DefaultMediaSourceFactory.java | 3 +- .../trackselection/DefaultTrackSelector.java | 3 +- library/dash/build.gradle | 1 + .../source/dash/DashMediaPeriod.java | 3 +- library/hls/build.gradle | 1 + .../exoplayer2/source/hls/HlsMediaPeriod.java | 3 +- .../playbacktests/gts/DashTestRunner.java | 3 +- 15 files changed, 27 insertions(+), 77 deletions(-) diff --git a/RELEASENOTES.md b/RELEASENOTES.md index f00b7b1793..6dafa1530a 100644 --- a/RELEASENOTES.md +++ b/RELEASENOTES.md @@ -227,6 +227,7 @@ * MediaSession extension: Set session playback state to BUFFERING only when actually playing ([#7367](https://github.com/google/ExoPlayer/pull/7367), [#7206](https://github.com/google/ExoPlayer/issues/7206)). +* Add Guava dependency. ### 2.11.4 (2020-04-08) diff --git a/constants.gradle b/constants.gradle index f3bebf6038..b0a0e4fce5 100644 --- a/constants.gradle +++ b/constants.gradle @@ -21,7 +21,7 @@ project.ext { compileSdkVersion = 29 dexmakerVersion = '2.21.0' junitVersion = '4.13-rc-2' - guavaVersion = '28.2-android' + guavaVersion = '27.1-android' mockitoVersion = '2.25.0' robolectricVersion = '4.4-SNAPSHOT' checkerframeworkVersion = '2.5.0' diff --git a/javadoc_combined.gradle b/javadoc_combined.gradle index 3b482910ae..1030d3e16a 100644 --- a/javadoc_combined.gradle +++ b/javadoc_combined.gradle @@ -11,6 +11,7 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. +apply from: "${buildscript.sourceFile.parentFile}/constants.gradle" apply from: "${buildscript.sourceFile.parentFile}/javadoc_util.gradle" class CombinedJavadocPlugin implements Plugin { @@ -29,7 +30,8 @@ class CombinedJavadocPlugin implements Plugin { classpath = project.files([]) destinationDir = project.file("$project.buildDir/docs/javadoc") options { - links "https://developer.android.com/reference" + links "https://developer.android.com/reference", + "https://guava.dev/releases/$project.ext.guavaVersion/api/docs" encoding = "UTF-8" } exclude "**/BuildConfig.java" diff --git a/javadoc_library.gradle b/javadoc_library.gradle index dd508a1781..f135e3a624 100644 --- a/javadoc_library.gradle +++ b/javadoc_library.gradle @@ -11,6 +11,7 @@ // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. +apply from: "${buildscript.sourceFile.parentFile}/constants.gradle" apply from: "${buildscript.sourceFile.parentFile}/javadoc_util.gradle" android.libraryVariants.all { variant -> @@ -26,7 +27,8 @@ android.libraryVariants.all { variant -> title = "ExoPlayer ${javadocTitle}" source = allSourceDirs options { - links "https://developer.android.com/reference" + links "https://developer.android.com/reference", + "https://guava.dev/releases/$project.ext.guavaVersion/api/docs" encoding = "UTF-8" } exclude "**/BuildConfig.java" diff --git a/library/common/build.gradle b/library/common/build.gradle index 9dc3aabac3..03c5b9c4b6 100644 --- a/library/common/build.gradle +++ b/library/common/build.gradle @@ -39,6 +39,7 @@ android { dependencies { implementation 'androidx.annotation:annotation:' + androidxAnnotationVersion compileOnly 'com.google.code.findbugs:jsr305:' + jsr305Version + compileOnly 'com.google.guava:guava:' + guavaVersion compileOnly 'org.checkerframework:checker-qual:' + checkerframeworkVersion compileOnly 'org.checkerframework:checker-compat-qual:' + checkerframeworkVersion compileOnly 'org.jetbrains.kotlin:kotlin-annotations-jvm:' + kotlinAnnotationsVersion diff --git a/library/common/src/main/java/com/google/android/exoplayer2/util/Util.java b/library/common/src/main/java/com/google/android/exoplayer2/util/Util.java index 75064623f6..888f0afa16 100644 --- a/library/common/src/main/java/com/google/android/exoplayer2/util/Util.java +++ b/library/common/src/main/java/com/google/android/exoplayer2/util/Util.java @@ -53,6 +53,7 @@ import com.google.android.exoplayer2.Format; import com.google.android.exoplayer2.MediaItem; import com.google.android.exoplayer2.ParserException; import com.google.android.exoplayer2.upstream.DataSource; +import com.google.common.base.Ascii; import java.io.ByteArrayOutputStream; import java.io.Closeable; import java.io.File; @@ -63,7 +64,6 @@ import java.math.BigDecimal; import java.nio.ByteBuffer; import java.nio.ByteOrder; import java.nio.charset.Charset; -import java.util.ArrayList; import java.util.Arrays; import java.util.Calendar; import java.util.Collections; @@ -1234,41 +1234,6 @@ public final class Util { return Math.round((double) mediaDuration / speed); } - /** - * Converts a list of integers to a primitive array. - * - * @param list A list of integers. - * @return The list in array form, or null if the input list was null. - */ - public static int @PolyNull [] toArray(@PolyNull List list) { - if (list == null) { - return null; - } - int length = list.size(); - int[] intArray = new int[length]; - for (int i = 0; i < length; i++) { - intArray[i] = list.get(i); - } - return intArray; - } - - /** - * Converts an array of primitive ints to a list of integers. - * - * @param ints The ints. - * @return The input array in list form. - */ - public static List toList(int... ints) { - if (ints == null) { - return new ArrayList<>(); - } - List integers = new ArrayList<>(); - for (int anInt : ints) { - integers.add(anInt); - } - return integers; - } - /** * Returns the integer equal to the big-endian concatenation of the characters in {@code string} * as bytes. The string must be no more than four characters long. @@ -1312,6 +1277,9 @@ public final class Util { /** * Truncates a sequence of ASCII characters to a maximum length. * + *

This preserves span styling in the {@link CharSequence}. If that's not important, use {@link + * Ascii#truncate(CharSequence, int, String)}. + * *

Note: This is not safe to use in general on Unicode text because it may separate * characters from combining characters or split up surrogate pairs. * @@ -1324,21 +1292,6 @@ public final class Util { return sequence.length() <= maxLength ? sequence : sequence.subSequence(0, maxLength); } - /** - * Truncates a string of ASCII characters to a maximum length. - * - *

Note: This is not safe to use in general on Unicode text because it may separate - * characters from combining characters or split up surrogate pairs. - * - * @param string The string to truncate. - * @param maxLength The max length to truncate to. - * @return {@code string} directly if {@code string.length() <= maxLength}, otherwise {@code - * string.substring(0, maxLength}. - */ - public static String truncateAscii(String string, int maxLength) { - return string.length() <= maxLength ? string : string.substring(0, maxLength); - } - /** * Returns a byte array containing values parsed from the hex string provided. * diff --git a/library/common/src/test/java/com/google/android/exoplayer2/util/UtilTest.java b/library/common/src/test/java/com/google/android/exoplayer2/util/UtilTest.java index 861267fc3a..de51274697 100644 --- a/library/common/src/test/java/com/google/android/exoplayer2/util/UtilTest.java +++ b/library/common/src/test/java/com/google/android/exoplayer2/util/UtilTest.java @@ -723,15 +723,13 @@ public class UtilTest { String input = "a short string"; assertThat(Util.truncateAscii(input, 100)).isSameInstanceAs(input); - assertThat(Util.truncateAscii((CharSequence) input, 100)).isSameInstanceAs(input); } @Test public void truncateAscii_longInput_truncated() { String input = "a much longer string"; - assertThat(Util.truncateAscii(input, 5)).isEqualTo("a muc"); - assertThat(Util.truncateAscii((CharSequence) input, 5).toString()).isEqualTo("a muc"); + assertThat(Util.truncateAscii(input, 5).toString()).isEqualTo("a muc"); } @Test @@ -999,21 +997,6 @@ public class UtilTest { assertThat(Util.normalizeLanguageCode("hsn")).isEqualTo("zh-hsn"); } - @Test - public void toList() { - assertThat(Util.toList(0, 3, 4)).containsExactly(0, 3, 4).inOrder(); - } - - @Test - public void toList_nullPassed_returnsEmptyList() { - assertThat(Util.toList(null)).isEmpty(); - } - - @Test - public void toList_emptyArrayPassed_returnsEmptyList() { - assertThat(Util.toList(new int[0])).isEmpty(); - } - private static void assertEscapeUnescapeFileName(String fileName, String escapedFileName) { assertThat(escapeFileName(fileName)).isEqualTo(escapedFileName); assertThat(unescapeFileName(escapedFileName)).isEqualTo(fileName); diff --git a/library/core/build.gradle b/library/core/build.gradle index 8b8c3fd520..a1d04dcf7b 100644 --- a/library/core/build.gradle +++ b/library/core/build.gradle @@ -54,6 +54,7 @@ dependencies { api project(modulePrefix + 'library-extractor') implementation 'androidx.annotation:annotation:' + androidxAnnotationVersion compileOnly 'com.google.code.findbugs:jsr305:' + jsr305Version + compileOnly 'com.google.guava:guava:' + guavaVersion compileOnly 'org.checkerframework:checker-qual:' + checkerframeworkVersion compileOnly 'org.checkerframework:checker-compat-qual:' + checkerframeworkVersion compileOnly 'org.jetbrains.kotlin:kotlin-annotations-jvm:' + kotlinAnnotationsVersion diff --git a/library/core/src/main/java/com/google/android/exoplayer2/source/DefaultMediaSourceFactory.java b/library/core/src/main/java/com/google/android/exoplayer2/source/DefaultMediaSourceFactory.java index a164a1348d..98bede87eb 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/source/DefaultMediaSourceFactory.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/source/DefaultMediaSourceFactory.java @@ -41,6 +41,7 @@ import com.google.android.exoplayer2.util.Assertions; import com.google.android.exoplayer2.util.Log; import com.google.android.exoplayer2.util.MimeTypes; import com.google.android.exoplayer2.util.Util; +import com.google.common.primitives.Ints; import java.util.Arrays; import java.util.List; import java.util.Map; @@ -323,7 +324,7 @@ public final class DefaultMediaSourceFactory implements MediaSourceFactory { .setPlayClearSamplesWithoutKeys( mediaItem.playbackProperties.drmConfiguration.playClearContentWithoutKey) .setUseDrmSessionsForClearContent( - Util.toArray(mediaItem.playbackProperties.drmConfiguration.sessionForClearTypes)) + Ints.toArray(mediaItem.playbackProperties.drmConfiguration.sessionForClearTypes)) .build(createHttpMediaDrmCallback(mediaItem.playbackProperties.drmConfiguration)); } diff --git a/library/core/src/main/java/com/google/android/exoplayer2/trackselection/DefaultTrackSelector.java b/library/core/src/main/java/com/google/android/exoplayer2/trackselection/DefaultTrackSelector.java index 668202993a..5bdc7847b6 100644 --- a/library/core/src/main/java/com/google/android/exoplayer2/trackselection/DefaultTrackSelector.java +++ b/library/core/src/main/java/com/google/android/exoplayer2/trackselection/DefaultTrackSelector.java @@ -39,6 +39,7 @@ import com.google.android.exoplayer2.source.TrackGroupArray; import com.google.android.exoplayer2.upstream.BandwidthMeter; import com.google.android.exoplayer2.util.Assertions; import com.google.android.exoplayer2.util.Util; +import com.google.common.primitives.Ints; import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; @@ -1822,7 +1823,7 @@ public class DefaultTrackSelector extends MappingTrackSelector { maxVideoBitrate, selectedTrackIndices); - return selectedTrackIndices.size() < 2 ? NO_TRACKS : Util.toArray(selectedTrackIndices); + return selectedTrackIndices.size() < 2 ? NO_TRACKS : Ints.toArray(selectedTrackIndices); } private static int getAdaptiveVideoTrackCountForMimeType( diff --git a/library/dash/build.gradle b/library/dash/build.gradle index 0ffbc718f0..51fb08837a 100644 --- a/library/dash/build.gradle +++ b/library/dash/build.gradle @@ -40,6 +40,7 @@ android { dependencies { implementation project(modulePrefix + 'library-core') + compileOnly 'com.google.guava:guava:' + guavaVersion compileOnly 'org.checkerframework:checker-qual:' + checkerframeworkVersion compileOnly 'org.checkerframework:checker-compat-qual:' + checkerframeworkVersion compileOnly 'org.jetbrains.kotlin:kotlin-annotations-jvm:' + kotlinAnnotationsVersion diff --git a/library/dash/src/main/java/com/google/android/exoplayer2/source/dash/DashMediaPeriod.java b/library/dash/src/main/java/com/google/android/exoplayer2/source/dash/DashMediaPeriod.java index f0ab422f5e..712e7137cd 100644 --- a/library/dash/src/main/java/com/google/android/exoplayer2/source/dash/DashMediaPeriod.java +++ b/library/dash/src/main/java/com/google/android/exoplayer2/source/dash/DashMediaPeriod.java @@ -51,6 +51,7 @@ import com.google.android.exoplayer2.upstream.LoaderErrorThrower; import com.google.android.exoplayer2.upstream.TransferListener; import com.google.android.exoplayer2.util.MimeTypes; import com.google.android.exoplayer2.util.Util; +import com.google.common.primitives.Ints; import java.io.IOException; import java.lang.annotation.Documented; import java.lang.annotation.Retention; @@ -604,7 +605,7 @@ import org.checkerframework.checker.nullness.compatqual.NullableType; int[][] groupedAdaptationSetIndices = new int[adaptationSetGroupedIndices.size()][]; for (int i = 0; i < groupedAdaptationSetIndices.length; i++) { - groupedAdaptationSetIndices[i] = Util.toArray(adaptationSetGroupedIndices.get(i)); + groupedAdaptationSetIndices[i] = Ints.toArray(adaptationSetGroupedIndices.get(i)); // Restore the original adaptation set order within each group. Arrays.sort(groupedAdaptationSetIndices[i]); } diff --git a/library/hls/build.gradle b/library/hls/build.gradle index 4764cf9882..e6153d904f 100644 --- a/library/hls/build.gradle +++ b/library/hls/build.gradle @@ -38,6 +38,7 @@ android { dependencies { implementation 'androidx.annotation:annotation:' + androidxAnnotationVersion + compileOnly 'com.google.guava:guava:' + guavaVersion compileOnly 'org.checkerframework:checker-qual:' + checkerframeworkVersion compileOnly 'org.checkerframework:checker-compat-qual:' + checkerframeworkVersion compileOnly 'org.jetbrains.kotlin:kotlin-annotations-jvm:' + kotlinAnnotationsVersion diff --git a/library/hls/src/main/java/com/google/android/exoplayer2/source/hls/HlsMediaPeriod.java b/library/hls/src/main/java/com/google/android/exoplayer2/source/hls/HlsMediaPeriod.java index b6985a836c..8b6f51571d 100644 --- a/library/hls/src/main/java/com/google/android/exoplayer2/source/hls/HlsMediaPeriod.java +++ b/library/hls/src/main/java/com/google/android/exoplayer2/source/hls/HlsMediaPeriod.java @@ -46,6 +46,7 @@ import com.google.android.exoplayer2.upstream.TransferListener; import com.google.android.exoplayer2.util.Assertions; import com.google.android.exoplayer2.util.MimeTypes; import com.google.android.exoplayer2.util.Util; +import com.google.common.primitives.Ints; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; @@ -719,7 +720,7 @@ public final class HlsMediaPeriod implements MediaPeriod, HlsSampleStreamWrapper /* muxedCaptionFormats= */ Collections.emptyList(), overridingDrmInitData, positionUs); - manifestUrlsIndicesPerWrapper.add(Util.toArray(scratchIndicesList)); + manifestUrlsIndicesPerWrapper.add(Ints.toArray(scratchIndicesList)); sampleStreamWrappers.add(sampleStreamWrapper); if (allowChunklessPreparation && renditionsHaveCodecs) { diff --git a/playbacktests/src/androidTest/java/com/google/android/exoplayer2/playbacktests/gts/DashTestRunner.java b/playbacktests/src/androidTest/java/com/google/android/exoplayer2/playbacktests/gts/DashTestRunner.java index 5f96b961f6..ea745ed257 100644 --- a/playbacktests/src/androidTest/java/com/google/android/exoplayer2/playbacktests/gts/DashTestRunner.java +++ b/playbacktests/src/androidTest/java/com/google/android/exoplayer2/playbacktests/gts/DashTestRunner.java @@ -56,6 +56,7 @@ import com.google.android.exoplayer2.upstream.DefaultLoadErrorHandlingPolicy; import com.google.android.exoplayer2.util.Assertions; import com.google.android.exoplayer2.util.Log; import com.google.android.exoplayer2.util.Util; +import com.google.common.primitives.Ints; import java.util.ArrayList; import java.util.Arrays; import java.util.List; @@ -444,7 +445,7 @@ import java.util.List; } } - int[] trackIndicesArray = Util.toArray(trackIndices); + int[] trackIndicesArray = Ints.toArray(trackIndices); Arrays.sort(trackIndicesArray); return trackIndicesArray; }