Skip to content

Commit 3ae4c1b

Browse files
ojw28icbaker
authored andcommitted
Merge pull request #6500 from DolbyLaboratories:dev-v2-isDirectPlaybackSupported
PiperOrigin-RevId: 378895355
1 parent 9a876f7 commit 3ae4c1b

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

RELEASENOTES.md

+5
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,11 @@
55
* Video:
66
* Fix `IncorrectContextUseViolation` strict mode warning on Android 11
77
([#8246](https://github.com/google/ExoPlayer/pull/8246)).
8+
* Audio:
9+
* Use `AudioTrack.isDirectPlaybackSupported` to check for encoded audio
10+
passthrough capability from API 29 onwards, instead of using the HDMI
11+
audio plug intent
12+
([#6500](https://github.com/google/ExoPlayer/pull/6500)).
813
* UI:
914
* Add `PendingIntent.FLAG_IMMUTABLE` flag when creating a broadcast intent
1015
in `PlayerNotificationManager`. This is required to avoid an error on

library/core/src/main/java/com/google/android/exoplayer2/audio/AudioCapabilities.java

+49
Original file line numberDiff line numberDiff line change
@@ -21,30 +21,50 @@
2121
import android.content.IntentFilter;
2222
import android.media.AudioFormat;
2323
import android.media.AudioManager;
24+
import android.media.AudioTrack;
2425
import android.net.Uri;
2526
import android.provider.Settings.Global;
27+
import androidx.annotation.DoNotInline;
2628
import androidx.annotation.Nullable;
29+
import androidx.annotation.RequiresApi;
2730
import com.google.android.exoplayer2.C;
2831
import com.google.android.exoplayer2.util.Util;
32+
import com.google.common.collect.ImmutableList;
33+
import com.google.common.primitives.Ints;
2934
import java.util.Arrays;
3035

3136
/** Represents the set of audio formats that a device is capable of playing. */
3237
public final class AudioCapabilities {
3338

3439
private static final int DEFAULT_MAX_CHANNEL_COUNT = 8;
40+
private static final int DEFAULT_SAMPLE_RATE_HZ = 48_000;
3541

3642
/** The minimum audio capabilities supported by all devices. */
3743
public static final AudioCapabilities DEFAULT_AUDIO_CAPABILITIES =
3844
new AudioCapabilities(new int[] {AudioFormat.ENCODING_PCM_16BIT}, DEFAULT_MAX_CHANNEL_COUNT);
3945

4046
/** Audio capabilities when the device specifies external surround sound. */
47+
@SuppressWarnings("InlinedApi")
4148
private static final AudioCapabilities EXTERNAL_SURROUND_SOUND_CAPABILITIES =
4249
new AudioCapabilities(
4350
new int[] {
4451
AudioFormat.ENCODING_PCM_16BIT, AudioFormat.ENCODING_AC3, AudioFormat.ENCODING_E_AC3
4552
},
4653
DEFAULT_MAX_CHANNEL_COUNT);
4754

55+
/** Array of all surround sound encodings that a device may be capable of playing. */
56+
@SuppressWarnings("InlinedApi")
57+
private static final int[] ALL_SURROUND_ENCODINGS =
58+
new int[] {
59+
AudioFormat.ENCODING_AC3,
60+
AudioFormat.ENCODING_E_AC3,
61+
AudioFormat.ENCODING_E_AC3_JOC,
62+
AudioFormat.ENCODING_AC4,
63+
AudioFormat.ENCODING_DOLBY_TRUEHD,
64+
AudioFormat.ENCODING_DTS,
65+
AudioFormat.ENCODING_DTS_HD,
66+
};
67+
4868
/** Global settings key for devices that can specify external surround sound. */
4969
private static final String EXTERNAL_SURROUND_SOUND_KEY = "external_surround_sound_enabled";
5070

@@ -68,6 +88,10 @@ public static AudioCapabilities getCapabilities(Context context) {
6888
&& Global.getInt(context.getContentResolver(), EXTERNAL_SURROUND_SOUND_KEY, 0) == 1) {
6989
return EXTERNAL_SURROUND_SOUND_CAPABILITIES;
7090
}
91+
if (Util.SDK_INT >= 29) {
92+
return new AudioCapabilities(
93+
AudioTrackWrapperV29.getDirectPlaybackSupportedEncodingsV29(), DEFAULT_MAX_CHANNEL_COUNT);
94+
}
7195
if (intent == null || intent.getIntExtra(AudioManager.EXTRA_AUDIO_PLUG_STATE, 0) == 0) {
7296
return DEFAULT_AUDIO_CAPABILITIES;
7397
}
@@ -158,4 +182,29 @@ private static boolean deviceMaySetExternalSurroundSoundGlobalSetting() {
158182
return Util.SDK_INT >= 17
159183
&& ("Amazon".equals(Util.MANUFACTURER) || "Xiaomi".equals(Util.MANUFACTURER));
160184
}
185+
186+
@RequiresApi(29)
187+
private static final class AudioTrackWrapperV29 {
188+
@DoNotInline
189+
public static int[] getDirectPlaybackSupportedEncodingsV29() {
190+
ImmutableList.Builder<Integer> supportedEncodingsListBuilder = ImmutableList.builder();
191+
for (int encoding : ALL_SURROUND_ENCODINGS) {
192+
if (AudioTrack.isDirectPlaybackSupported(
193+
new AudioFormat.Builder()
194+
.setChannelMask(AudioFormat.CHANNEL_OUT_STEREO)
195+
.setEncoding(encoding)
196+
.setSampleRate(DEFAULT_SAMPLE_RATE_HZ)
197+
.build(),
198+
new android.media.AudioAttributes.Builder()
199+
.setUsage(android.media.AudioAttributes.USAGE_MEDIA)
200+
.setContentType(android.media.AudioAttributes.CONTENT_TYPE_MOVIE)
201+
.setFlags(0)
202+
.build())) {
203+
supportedEncodingsListBuilder.add(encoding);
204+
}
205+
}
206+
supportedEncodingsListBuilder.add(AudioFormat.ENCODING_PCM_16BIT);
207+
return Ints.toArray(supportedEncodingsListBuilder.build());
208+
}
209+
}
161210
}

0 commit comments

Comments
 (0)