21
21
import android .content .IntentFilter ;
22
22
import android .media .AudioFormat ;
23
23
import android .media .AudioManager ;
24
+ import android .media .AudioTrack ;
24
25
import android .net .Uri ;
25
26
import android .provider .Settings .Global ;
27
+ import androidx .annotation .DoNotInline ;
26
28
import androidx .annotation .Nullable ;
29
+ import androidx .annotation .RequiresApi ;
27
30
import com .google .android .exoplayer2 .C ;
28
31
import com .google .android .exoplayer2 .util .Util ;
32
+ import com .google .common .collect .ImmutableList ;
33
+ import com .google .common .primitives .Ints ;
29
34
import java .util .Arrays ;
30
35
31
36
/** Represents the set of audio formats that a device is capable of playing. */
32
37
public final class AudioCapabilities {
33
38
34
39
private static final int DEFAULT_MAX_CHANNEL_COUNT = 8 ;
40
+ private static final int DEFAULT_SAMPLE_RATE_HZ = 48_000 ;
35
41
36
42
/** The minimum audio capabilities supported by all devices. */
37
43
public static final AudioCapabilities DEFAULT_AUDIO_CAPABILITIES =
38
44
new AudioCapabilities (new int [] {AudioFormat .ENCODING_PCM_16BIT }, DEFAULT_MAX_CHANNEL_COUNT );
39
45
40
46
/** Audio capabilities when the device specifies external surround sound. */
47
+ @ SuppressWarnings ("InlinedApi" )
41
48
private static final AudioCapabilities EXTERNAL_SURROUND_SOUND_CAPABILITIES =
42
49
new AudioCapabilities (
43
50
new int [] {
44
51
AudioFormat .ENCODING_PCM_16BIT , AudioFormat .ENCODING_AC3 , AudioFormat .ENCODING_E_AC3
45
52
},
46
53
DEFAULT_MAX_CHANNEL_COUNT );
47
54
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
+
48
68
/** Global settings key for devices that can specify external surround sound. */
49
69
private static final String EXTERNAL_SURROUND_SOUND_KEY = "external_surround_sound_enabled" ;
50
70
@@ -68,6 +88,10 @@ public static AudioCapabilities getCapabilities(Context context) {
68
88
&& Global .getInt (context .getContentResolver (), EXTERNAL_SURROUND_SOUND_KEY , 0 ) == 1 ) {
69
89
return EXTERNAL_SURROUND_SOUND_CAPABILITIES ;
70
90
}
91
+ if (Util .SDK_INT >= 29 ) {
92
+ return new AudioCapabilities (
93
+ AudioTrackWrapperV29 .getDirectPlaybackSupportedEncodingsV29 (), DEFAULT_MAX_CHANNEL_COUNT );
94
+ }
71
95
if (intent == null || intent .getIntExtra (AudioManager .EXTRA_AUDIO_PLUG_STATE , 0 ) == 0 ) {
72
96
return DEFAULT_AUDIO_CAPABILITIES ;
73
97
}
@@ -158,4 +182,29 @@ private static boolean deviceMaySetExternalSurroundSoundGlobalSetting() {
158
182
return Util .SDK_INT >= 17
159
183
&& ("Amazon" .equals (Util .MANUFACTURER ) || "Xiaomi" .equals (Util .MANUFACTURER ));
160
184
}
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
+ }
161
210
}
0 commit comments