package androidx.media3.exoplayer.audio; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.media.AudioAttributes; import android.media.AudioDeviceInfo; import android.media.AudioFormat; import android.media.AudioManager; import android.media.AudioTrack; import android.net.Uri; import android.provider.Settings; import android.util.Pair; import androidx.media3.common.Format; import androidx.media3.common.MimeTypes; import androidx.media3.common.util.Assertions; import androidx.media3.common.util.Util; import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableSet; import com.google.common.collect.UnmodifiableIterator; import com.google.common.primitives.Ints; import java.util.Arrays; /* loaded from: classes2.dex */ public final class AudioCapabilities { private static final int DEFAULT_MAX_CHANNEL_COUNT = 10; static final int DEFAULT_SAMPLE_RATE_HZ = 48000; private static final String EXTERNAL_SURROUND_SOUND_KEY = "external_surround_sound_enabled"; private final int maxChannelCount; private final int[] supportedEncodings; public static final AudioCapabilities DEFAULT_AUDIO_CAPABILITIES = new AudioCapabilities(new int[]{2}, 10); private static final ImmutableList EXTERNAL_SURROUND_SOUND_ENCODINGS = ImmutableList.of(2, 5, 6); private static final ImmutableMap ALL_SURROUND_ENCODINGS_AND_MAX_CHANNELS = new ImmutableMap.Builder().put(5, 6).put(17, 6).put(7, 6).put(30, 10).put(18, 6).put(6, 8).put(8, 8).put(14, 8).buildOrThrow(); public int getMaxChannelCount() { return this.maxChannelCount; } public static AudioCapabilities getCapabilities(Context context) { return getCapabilities(context, context.registerReceiver(null, new IntentFilter("android.media.action.HDMI_AUDIO_PLUG"))); } /* JADX INFO: Access modifiers changed from: package-private */ public static AudioCapabilities getCapabilities(Context context, Intent intent) { if (Util.SDK_INT >= 23 && Api23.isBluetoothConnected(context)) { return DEFAULT_AUDIO_CAPABILITIES; } ImmutableSet.Builder builder = new ImmutableSet.Builder(); if (deviceMaySetExternalSurroundSoundGlobalSetting() && Settings.Global.getInt(context.getContentResolver(), EXTERNAL_SURROUND_SOUND_KEY, 0) == 1) { builder.addAll((Iterable) EXTERNAL_SURROUND_SOUND_ENCODINGS); } if (Util.SDK_INT >= 29 && (Util.isTv(context) || Util.isAutomotive(context))) { builder.addAll((Iterable) Api29.getDirectPlaybackSupportedEncodings()); return new AudioCapabilities(Ints.toArray(builder.build()), 10); } if (intent != null && intent.getIntExtra("android.media.extra.AUDIO_PLUG_STATE", 0) == 1) { int[] intArrayExtra = intent.getIntArrayExtra("android.media.extra.ENCODINGS"); if (intArrayExtra != null) { builder.addAll((Iterable) Ints.asList(intArrayExtra)); } return new AudioCapabilities(Ints.toArray(builder.build()), intent.getIntExtra("android.media.extra.MAX_CHANNEL_COUNT", 10)); } ImmutableSet build = builder.build(); return !build.isEmpty() ? new AudioCapabilities(Ints.toArray(build), 10) : DEFAULT_AUDIO_CAPABILITIES; } /* JADX INFO: Access modifiers changed from: package-private */ public static Uri getExternalSurroundSoundGlobalSettingUri() { if (deviceMaySetExternalSurroundSoundGlobalSetting()) { return Settings.Global.getUriFor(EXTERNAL_SURROUND_SOUND_KEY); } return null; } public AudioCapabilities(int[] iArr, int i) { if (iArr != null) { int[] copyOf = Arrays.copyOf(iArr, iArr.length); this.supportedEncodings = copyOf; Arrays.sort(copyOf); } else { this.supportedEncodings = new int[0]; } this.maxChannelCount = i; } public boolean supportsEncoding(int i) { return Arrays.binarySearch(this.supportedEncodings, i) >= 0; } public boolean isPassthroughPlaybackSupported(Format format) { return getEncodingAndChannelConfigForPassthrough(format) != null; } public Pair getEncodingAndChannelConfigForPassthrough(Format format) { int maxSupportedChannelCountForPassthrough; int encoding = MimeTypes.getEncoding((String) Assertions.checkNotNull(format.sampleMimeType), format.codecs); if (!ALL_SURROUND_ENCODINGS_AND_MAX_CHANNELS.containsKey(Integer.valueOf(encoding))) { return null; } if (encoding == 18 && !supportsEncoding(18)) { encoding = 6; } else if ((encoding == 8 && !supportsEncoding(8)) || (encoding == 30 && !supportsEncoding(30))) { encoding = 7; } if (!supportsEncoding(encoding)) { return null; } if (format.channelCount == -1 || encoding == 18) { maxSupportedChannelCountForPassthrough = getMaxSupportedChannelCountForPassthrough(encoding, format.sampleRate != -1 ? format.sampleRate : 48000); } else { maxSupportedChannelCountForPassthrough = format.channelCount; if (format.sampleMimeType.equals("audio/vnd.dts.uhd;profile=p2")) { if (maxSupportedChannelCountForPassthrough > 10) { return null; } } else if (maxSupportedChannelCountForPassthrough > this.maxChannelCount) { return null; } } int channelConfigForPassthrough = getChannelConfigForPassthrough(maxSupportedChannelCountForPassthrough); if (channelConfigForPassthrough == 0) { return null; } return Pair.create(Integer.valueOf(encoding), Integer.valueOf(channelConfigForPassthrough)); } public boolean equals(Object obj) { if (this == obj) { return true; } if (!(obj instanceof AudioCapabilities)) { return false; } AudioCapabilities audioCapabilities = (AudioCapabilities) obj; return Arrays.equals(this.supportedEncodings, audioCapabilities.supportedEncodings) && this.maxChannelCount == audioCapabilities.maxChannelCount; } public int hashCode() { return this.maxChannelCount + (Arrays.hashCode(this.supportedEncodings) * 31); } public String toString() { return "AudioCapabilities[maxChannelCount=" + this.maxChannelCount + ", supportedEncodings=" + Arrays.toString(this.supportedEncodings) + "]"; } private static boolean deviceMaySetExternalSurroundSoundGlobalSetting() { return Util.SDK_INT >= 17 && ("Amazon".equals(Util.MANUFACTURER) || "Xiaomi".equals(Util.MANUFACTURER)); } private static int getMaxSupportedChannelCountForPassthrough(int i, int i2) { if (Util.SDK_INT >= 29) { return Api29.getMaxSupportedChannelCountForPassthrough(i, i2); } return ((Integer) Assertions.checkNotNull(ALL_SURROUND_ENCODINGS_AND_MAX_CHANNELS.getOrDefault(Integer.valueOf(i), 0))).intValue(); } private static int getChannelConfigForPassthrough(int i) { if (Util.SDK_INT <= 28) { if (i == 7) { i = 8; } else if (i == 3 || i == 4 || i == 5) { i = 6; } } if (Util.SDK_INT <= 26 && "fugu".equals(Util.DEVICE) && i == 1) { i = 2; } return Util.getAudioTrackChannelConfig(i); } /* JADX INFO: Access modifiers changed from: private */ /* loaded from: classes2.dex */ public static final class Api23 { private Api23() { } public static final boolean isBluetoothConnected(Context context) { AudioDeviceInfo[] devices = ((AudioManager) Assertions.checkNotNull((AudioManager) context.getSystemService("audio"))).getDevices(2); ImmutableSet allBluetoothDeviceTypes = getAllBluetoothDeviceTypes(); for (AudioDeviceInfo audioDeviceInfo : devices) { if (allBluetoothDeviceTypes.contains(Integer.valueOf(audioDeviceInfo.getType()))) { return true; } } return false; } private static final ImmutableSet getAllBluetoothDeviceTypes() { ImmutableSet.Builder add = new ImmutableSet.Builder().add((Object[]) new Integer[]{8, 7}); if (Util.SDK_INT >= 31) { add.add((Object[]) new Integer[]{26, 27}); } if (Util.SDK_INT >= 33) { add.add((ImmutableSet.Builder) 30); } return add.build(); } } /* JADX INFO: Access modifiers changed from: private */ /* loaded from: classes2.dex */ public static final class Api29 { private static final AudioAttributes DEFAULT_AUDIO_ATTRIBUTES = new AudioAttributes.Builder().setUsage(1).setContentType(3).setFlags(0).build(); private Api29() { } /* JADX WARN: Multi-variable type inference failed */ public static ImmutableList getDirectPlaybackSupportedEncodings() { ImmutableList.Builder builder = ImmutableList.builder(); UnmodifiableIterator it = AudioCapabilities.ALL_SURROUND_ENCODINGS_AND_MAX_CHANNELS.keySet().iterator(); while (it.hasNext()) { int intValue = ((Integer) it.next()).intValue(); if (Util.SDK_INT >= 34 || intValue != 30) { if (AudioTrack.isDirectPlaybackSupported(new AudioFormat.Builder().setChannelMask(12).setEncoding(intValue).setSampleRate(48000).build(), DEFAULT_AUDIO_ATTRIBUTES)) { builder.add((ImmutableList.Builder) Integer.valueOf(intValue)); } } } builder.add((ImmutableList.Builder) 2); return builder.build(); } public static int getMaxSupportedChannelCountForPassthrough(int i, int i2) { for (int i3 = 10; i3 > 0; i3--) { if (AudioTrack.isDirectPlaybackSupported(new AudioFormat.Builder().setEncoding(i).setSampleRate(i2).setChannelMask(Util.getAudioTrackChannelConfig(i3)).build(), DEFAULT_AUDIO_ATTRIBUTES)) { return i3; } } return 0; } } }