This repository has been archived by the owner on Nov 8, 2023. It is now read-only.
/
AudioSystem.java
2644 lines (2433 loc) · 109 KB
/
AudioSystem.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (C) 2006 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* 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.
*/
package android.media;
import android.annotation.IntDef;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.RequiresPermission;
import android.annotation.TestApi;
import android.bluetooth.BluetoothCodecConfig;
import android.bluetooth.BluetoothLeAudioCodecConfig;
import android.compat.annotation.UnsupportedAppUsage;
import android.content.Context;
import android.content.pm.PackageManager;
import android.media.audio.common.AidlConversion;
import android.media.audiofx.AudioEffect;
import android.media.audiopolicy.AudioMix;
import android.media.audiopolicy.AudioMixingRule;
import android.media.audiopolicy.AudioProductStrategy;
import android.os.Build;
import android.os.IBinder;
import android.os.Parcel;
import android.os.Vibrator;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.util.Pair;
import com.android.internal.annotations.GuardedBy;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.TreeSet;
/* IF YOU CHANGE ANY OF THE CONSTANTS IN THIS FILE, DO NOT FORGET
* TO UPDATE THE CORRESPONDING NATIVE GLUE AND AudioManager.java.
* THANK YOU FOR YOUR COOPERATION.
*/
/**
* @hide
*/
@TestApi
public class AudioSystem
{
private static final boolean DEBUG_VOLUME = false;
private static final String TAG = "AudioSystem";
// private constructor to prevent instantiating AudioSystem
private AudioSystem() {
throw new UnsupportedOperationException("Trying to instantiate AudioSystem");
}
/* These values must be kept in sync with system/media/audio/include/system/audio-hal-enums.h */
/*
* If these are modified, please also update Settings.System.VOLUME_SETTINGS
* and attrs.xml and AudioManager.java.
*/
/** @hide Used to identify the default audio stream volume */
@TestApi
public static final int STREAM_DEFAULT = -1;
/** @hide Used to identify the volume of audio streams for phone calls */
public static final int STREAM_VOICE_CALL = 0;
/** @hide Used to identify the volume of audio streams for system sounds */
public static final int STREAM_SYSTEM = 1;
/** @hide Used to identify the volume of audio streams for the phone ring and message alerts */
public static final int STREAM_RING = 2;
/** @hide Used to identify the volume of audio streams for music playback */
public static final int STREAM_MUSIC = 3;
/** @hide Used to identify the volume of audio streams for alarms */
public static final int STREAM_ALARM = 4;
/** @hide Used to identify the volume of audio streams for notifications */
public static final int STREAM_NOTIFICATION = 5;
/** @hide
* Used to identify the volume of audio streams for phone calls when connected on bluetooth */
public static final int STREAM_BLUETOOTH_SCO = 6;
/** @hide Used to identify the volume of audio streams for enforced system sounds in certain
* countries (e.g camera in Japan) */
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
public static final int STREAM_SYSTEM_ENFORCED = 7;
/** @hide Used to identify the volume of audio streams for DTMF tones */
public static final int STREAM_DTMF = 8;
/** @hide Used to identify the volume of audio streams exclusively transmitted through the
* speaker (TTS) of the device */
public static final int STREAM_TTS = 9;
/** @hide Used to identify the volume of audio streams for accessibility prompts */
public static final int STREAM_ACCESSIBILITY = 10;
/** @hide Used to identify the volume of audio streams for virtual assistant */
public static final int STREAM_ASSISTANT = 11;
/**
* @hide
* @deprecated Use {@link #numStreamTypes() instead}
*/
public static final int NUM_STREAMS = 5;
/*
* Framework static final constants that are primitives or Strings
* accessed by CTS tests or internal applications must be set from methods
* (or in a static block) to prevent Java compile-time replacement.
* We set them from methods so they are read from the device framework.
* Do not un-hide or change to a numeric literal.
*/
/** Maximum value for AudioTrack channel count
* @hide
*/
public static final int OUT_CHANNEL_COUNT_MAX = native_getMaxChannelCount();
private static native int native_getMaxChannelCount();
/** Maximum value for sample rate, used by AudioFormat.
* @hide
*/
public static final int SAMPLE_RATE_HZ_MAX = native_getMaxSampleRate();
private static native int native_getMaxSampleRate();
/** Minimum value for sample rate, used by AudioFormat.
* @hide
*/
public static final int SAMPLE_RATE_HZ_MIN = native_getMinSampleRate();
private static native int native_getMinSampleRate();
/** @hide */
public static final int FCC_24 = 24; // fixed channel count 24; do not change.
// Expose only the getter method publicly so we can change it in the future
private static final int NUM_STREAM_TYPES = 12;
/**
* @hide
* @return total number of stream types
*/
@UnsupportedAppUsage
@TestApi
public static final int getNumStreamTypes() { return NUM_STREAM_TYPES; }
/** @hide */
public static final String[] STREAM_NAMES = new String[] {
"STREAM_VOICE_CALL",
"STREAM_SYSTEM",
"STREAM_RING",
"STREAM_MUSIC",
"STREAM_ALARM",
"STREAM_NOTIFICATION",
"STREAM_BLUETOOTH_SCO",
"STREAM_SYSTEM_ENFORCED",
"STREAM_DTMF",
"STREAM_TTS",
"STREAM_ACCESSIBILITY",
"STREAM_ASSISTANT"
};
/**
* @hide
* Sets the microphone mute on or off.
*
* @param on set <var>true</var> to mute the microphone;
* <var>false</var> to turn mute off
* @return command completion status see AUDIO_STATUS_OK, see AUDIO_STATUS_ERROR
*/
@UnsupportedAppUsage
public static native int muteMicrophone(boolean on);
/**
* @hide
* Checks whether the microphone mute is on or off.
*
* @return true if microphone is muted, false if it's not
*/
@UnsupportedAppUsage
public static native boolean isMicrophoneMuted();
/* modes for setPhoneState, must match AudioSystem.h audio_mode */
/** @hide */
public static final int MODE_INVALID = -2;
/** @hide */
public static final int MODE_CURRENT = -1;
/** @hide */
public static final int MODE_NORMAL = 0;
/** @hide */
public static final int MODE_RINGTONE = 1;
/** @hide */
public static final int MODE_IN_CALL = 2;
/** @hide */
public static final int MODE_IN_COMMUNICATION = 3;
/** @hide */
public static final int MODE_CALL_SCREENING = 4;
/** @hide */
public static final int MODE_CALL_REDIRECT = 5;
/** @hide */
public static final int MODE_COMMUNICATION_REDIRECT = 6;
/** @hide */
public static final int NUM_MODES = 7;
/** @hide */
public static String modeToString(int mode) {
switch (mode) {
case MODE_CURRENT: return "MODE_CURRENT";
case MODE_IN_CALL: return "MODE_IN_CALL";
case MODE_IN_COMMUNICATION: return "MODE_IN_COMMUNICATION";
case MODE_INVALID: return "MODE_INVALID";
case MODE_NORMAL: return "MODE_NORMAL";
case MODE_RINGTONE: return "MODE_RINGTONE";
case MODE_CALL_SCREENING: return "MODE_CALL_SCREENING";
case MODE_CALL_REDIRECT: return "MODE_CALL_REDIRECT";
case MODE_COMMUNICATION_REDIRECT: return "MODE_COMMUNICATION_REDIRECT";
default: return "unknown mode (" + mode + ")";
}
}
/* Formats for A2DP codecs, must match system/audio-base.h audio_format_t */
/** @hide */
public static final int AUDIO_FORMAT_INVALID = 0xFFFFFFFF;
/** @hide */
public static final int AUDIO_FORMAT_DEFAULT = 0;
/** @hide */
public static final int AUDIO_FORMAT_AAC = 0x04000000;
/** @hide */
public static final int AUDIO_FORMAT_SBC = 0x1F000000;
/** @hide */
public static final int AUDIO_FORMAT_APTX = 0x20000000;
/** @hide */
public static final int AUDIO_FORMAT_APTX_HD = 0x21000000;
/** @hide */
public static final int AUDIO_FORMAT_LDAC = 0x23000000;
/** @hide */
public static final int AUDIO_FORMAT_LC3 = 0x2B000000;
/** @hide */
public static final int AUDIO_FORMAT_OPUS = 0x08000000;
/** @hide */
@IntDef(flag = false, prefix = "AUDIO_FORMAT_", value = {
AUDIO_FORMAT_INVALID,
AUDIO_FORMAT_DEFAULT,
AUDIO_FORMAT_AAC,
AUDIO_FORMAT_SBC,
AUDIO_FORMAT_APTX,
AUDIO_FORMAT_APTX_HD,
AUDIO_FORMAT_LDAC,
AUDIO_FORMAT_LC3,
AUDIO_FORMAT_OPUS
}
)
@Retention(RetentionPolicy.SOURCE)
public @interface AudioFormatNativeEnumForBtCodec {}
/** @hide */
@IntDef(flag = false, prefix = "AUDIO_FORMAT_", value = {
AUDIO_FORMAT_LC3}
)
@Retention(RetentionPolicy.SOURCE)
public @interface AudioFormatNativeEnumForBtLeAudioCodec {}
/** @hide */
@IntDef(flag = false, prefix = "DEVICE_", value = {
DEVICE_OUT_BLUETOOTH_A2DP,
DEVICE_OUT_BLE_HEADSET,
DEVICE_OUT_BLE_BROADCAST}
)
@Retention(RetentionPolicy.SOURCE)
public @interface BtOffloadDeviceType {}
/**
* @hide
* Convert audio format enum values to Bluetooth codec values
*/
public static int audioFormatToBluetoothSourceCodec(
@AudioFormatNativeEnumForBtCodec int audioFormat) {
switch (audioFormat) {
case AUDIO_FORMAT_AAC: return BluetoothCodecConfig.SOURCE_CODEC_TYPE_AAC;
case AUDIO_FORMAT_SBC: return BluetoothCodecConfig.SOURCE_CODEC_TYPE_SBC;
case AUDIO_FORMAT_APTX: return BluetoothCodecConfig.SOURCE_CODEC_TYPE_APTX;
case AUDIO_FORMAT_APTX_HD: return BluetoothCodecConfig.SOURCE_CODEC_TYPE_APTX_HD;
case AUDIO_FORMAT_LDAC: return BluetoothCodecConfig.SOURCE_CODEC_TYPE_LDAC;
case AUDIO_FORMAT_LC3: return BluetoothCodecConfig.SOURCE_CODEC_TYPE_LC3;
case AUDIO_FORMAT_OPUS: return BluetoothCodecConfig.SOURCE_CODEC_TYPE_OPUS;
default:
Log.e(TAG, "Unknown audio format 0x" + Integer.toHexString(audioFormat)
+ " for conversion to BT codec");
return BluetoothCodecConfig.SOURCE_CODEC_TYPE_INVALID;
}
}
/**
* @hide
* Convert audio format enum values to Bluetooth LE audio codec values
*/
public static int audioFormatToBluetoothLeAudioSourceCodec(
@AudioFormatNativeEnumForBtLeAudioCodec int audioFormat) {
switch (audioFormat) {
case AUDIO_FORMAT_LC3: return BluetoothLeAudioCodecConfig.SOURCE_CODEC_TYPE_LC3;
default:
Log.e(TAG, "Unknown audio format 0x" + Integer.toHexString(audioFormat)
+ " for conversion to BT LE audio codec");
return BluetoothLeAudioCodecConfig.SOURCE_CODEC_TYPE_INVALID;
}
}
/**
* @hide
* Convert an A2DP Bluetooth codec to an audio format enum
* @param btCodec the codec to convert.
* @return the audio format, or {@link #AUDIO_FORMAT_DEFAULT} if unknown
*/
public static @AudioFormatNativeEnumForBtCodec int bluetoothA2dpCodecToAudioFormat(
int btCodec) {
switch (btCodec) {
case BluetoothCodecConfig.SOURCE_CODEC_TYPE_SBC:
return AudioSystem.AUDIO_FORMAT_SBC;
case BluetoothCodecConfig.SOURCE_CODEC_TYPE_AAC:
return AudioSystem.AUDIO_FORMAT_AAC;
case BluetoothCodecConfig.SOURCE_CODEC_TYPE_APTX:
return AudioSystem.AUDIO_FORMAT_APTX;
case BluetoothCodecConfig.SOURCE_CODEC_TYPE_APTX_HD:
return AudioSystem.AUDIO_FORMAT_APTX_HD;
case BluetoothCodecConfig.SOURCE_CODEC_TYPE_LDAC:
return AudioSystem.AUDIO_FORMAT_LDAC;
case BluetoothCodecConfig.SOURCE_CODEC_TYPE_LC3:
return AudioSystem.AUDIO_FORMAT_LC3;
case BluetoothCodecConfig.SOURCE_CODEC_TYPE_OPUS:
return AudioSystem.AUDIO_FORMAT_OPUS;
default:
Log.e(TAG, "Unknown A2DP BT codec 0x" + Integer.toHexString(btCodec)
+ " for conversion to audio format");
// TODO returning DEFAULT is the current behavior, should this return INVALID?
return AudioSystem.AUDIO_FORMAT_DEFAULT;
}
}
/**
* @hide
* Convert a LE Audio Bluetooth codec to an audio format enum
* @param btCodec the codec to convert.
* @return the audio format, or {@link #AUDIO_FORMAT_DEFAULT} if unknown
*/
public static @AudioFormatNativeEnumForBtCodec int bluetoothLeCodecToAudioFormat(int btCodec) {
switch (btCodec) {
case BluetoothLeAudioCodecConfig.SOURCE_CODEC_TYPE_LC3:
return AudioSystem.AUDIO_FORMAT_LC3;
default:
Log.e(TAG, "Unknown LE Audio BT codec 0x" + Integer.toHexString(btCodec)
+ " for conversion to audio format");
// TODO returning DEFAULT is the current behavior, should this return INVALID?
return AudioSystem.AUDIO_FORMAT_DEFAULT;
}
}
/**
* @hide
* Convert a native audio format integer constant to a string.
*/
public static String audioFormatToString(int audioFormat) {
switch (audioFormat) {
case /* AUDIO_FORMAT_INVALID */ 0xFFFFFFFF:
return "AUDIO_FORMAT_INVALID";
case /* AUDIO_FORMAT_DEFAULT */ 0:
return "AUDIO_FORMAT_DEFAULT";
case /* AUDIO_FORMAT_MP3 */ 0x01000000:
return "AUDIO_FORMAT_MP3";
case /* AUDIO_FORMAT_AMR_NB */ 0x02000000:
return "AUDIO_FORMAT_AMR_NB";
case /* AUDIO_FORMAT_AMR_WB */ 0x03000000:
return "AUDIO_FORMAT_AMR_WB";
case /* AUDIO_FORMAT_AAC */ 0x04000000:
return "AUDIO_FORMAT_AAC";
case /* AUDIO_FORMAT_HE_AAC_V1 */ 0x05000000:
return "AUDIO_FORMAT_HE_AAC_V1";
case /* AUDIO_FORMAT_HE_AAC_V2 */ 0x06000000:
return "AUDIO_FORMAT_HE_AAC_V2";
case /* AUDIO_FORMAT_VORBIS */ 0x07000000:
return "AUDIO_FORMAT_VORBIS";
case /* AUDIO_FORMAT_OPUS */ 0x08000000:
return "AUDIO_FORMAT_OPUS";
case /* AUDIO_FORMAT_AC3 */ 0x09000000:
return "AUDIO_FORMAT_AC3";
case /* AUDIO_FORMAT_E_AC3 */ 0x0A000000:
return "AUDIO_FORMAT_E_AC3";
case /* AUDIO_FORMAT_DTS */ 0x0B000000:
return "AUDIO_FORMAT_DTS";
case /* AUDIO_FORMAT_DTS_HD */ 0x0C000000:
return "AUDIO_FORMAT_DTS_HD";
case /* AUDIO_FORMAT_IEC61937 */ 0x0D000000:
return "AUDIO_FORMAT_IEC61937";
case /* AUDIO_FORMAT_DOLBY_TRUEHD */ 0x0E000000:
return "AUDIO_FORMAT_DOLBY_TRUEHD";
case /* AUDIO_FORMAT_EVRC */ 0x10000000:
return "AUDIO_FORMAT_EVRC";
case /* AUDIO_FORMAT_EVRCB */ 0x11000000:
return "AUDIO_FORMAT_EVRCB";
case /* AUDIO_FORMAT_EVRCWB */ 0x12000000:
return "AUDIO_FORMAT_EVRCWB";
case /* AUDIO_FORMAT_EVRCNW */ 0x13000000:
return "AUDIO_FORMAT_EVRCNW";
case /* AUDIO_FORMAT_AAC_ADIF */ 0x14000000:
return "AUDIO_FORMAT_AAC_ADIF";
case /* AUDIO_FORMAT_WMA */ 0x15000000:
return "AUDIO_FORMAT_WMA";
case /* AUDIO_FORMAT_WMA_PRO */ 0x16000000:
return "AUDIO_FORMAT_WMA_PRO";
case /* AUDIO_FORMAT_AMR_WB_PLUS */ 0x17000000:
return "AUDIO_FORMAT_AMR_WB_PLUS";
case /* AUDIO_FORMAT_MP2 */ 0x18000000:
return "AUDIO_FORMAT_MP2";
case /* AUDIO_FORMAT_QCELP */ 0x19000000:
return "AUDIO_FORMAT_QCELP";
case /* AUDIO_FORMAT_DSD */ 0x1A000000:
return "AUDIO_FORMAT_DSD";
case /* AUDIO_FORMAT_FLAC */ 0x1B000000:
return "AUDIO_FORMAT_FLAC";
case /* AUDIO_FORMAT_ALAC */ 0x1C000000:
return "AUDIO_FORMAT_ALAC";
case /* AUDIO_FORMAT_APE */ 0x1D000000:
return "AUDIO_FORMAT_APE";
case /* AUDIO_FORMAT_AAC_ADTS */ 0x1E000000:
return "AUDIO_FORMAT_AAC_ADTS";
case /* AUDIO_FORMAT_SBC */ 0x1F000000:
return "AUDIO_FORMAT_SBC";
case /* AUDIO_FORMAT_APTX */ 0x20000000:
return "AUDIO_FORMAT_APTX";
case /* AUDIO_FORMAT_APTX_HD */ 0x21000000:
return "AUDIO_FORMAT_APTX_HD";
case /* AUDIO_FORMAT_AC4 */ 0x22000000:
return "AUDIO_FORMAT_AC4";
case /* AUDIO_FORMAT_LDAC */ 0x23000000:
return "AUDIO_FORMAT_LDAC";
case /* AUDIO_FORMAT_MAT */ 0x24000000:
return "AUDIO_FORMAT_MAT";
case /* AUDIO_FORMAT_AAC_LATM */ 0x25000000:
return "AUDIO_FORMAT_AAC_LATM";
case /* AUDIO_FORMAT_CELT */ 0x26000000:
return "AUDIO_FORMAT_CELT";
case /* AUDIO_FORMAT_APTX_ADAPTIVE */ 0x27000000:
return "AUDIO_FORMAT_APTX_ADAPTIVE";
case /* AUDIO_FORMAT_LHDC */ 0x28000000:
return "AUDIO_FORMAT_LHDC";
case /* AUDIO_FORMAT_LHDC_LL */ 0x29000000:
return "AUDIO_FORMAT_LHDC_LL";
case /* AUDIO_FORMAT_APTX_TWSP */ 0x2A000000:
return "AUDIO_FORMAT_APTX_TWSP";
case /* AUDIO_FORMAT_LC3 */ 0x2B000000:
return "AUDIO_FORMAT_LC3";
case /* AUDIO_FORMAT_MPEGH */ 0x2C000000:
return "AUDIO_FORMAT_MPEGH";
case /* AUDIO_FORMAT_IEC60958 */ 0x2D000000:
return "AUDIO_FORMAT_IEC60958";
case /* AUDIO_FORMAT_DTS_UHD */ 0x2E000000:
return "AUDIO_FORMAT_DTS_UHD";
case /* AUDIO_FORMAT_DRA */ 0x2F000000:
return "AUDIO_FORMAT_DRA";
case /* AUDIO_FORMAT_APTX_ADAPTIVE_QLEA */ 0x30000000:
return "AUDIO_FORMAT_APTX_ADAPTIVE_QLEA";
case /* AUDIO_FORMAT_APTX_ADAPTIVE_R4 */ 0x31000000:
return "AUDIO_FORMAT_APTX_ADAPTIVE_R4";
case /* AUDIO_FORMAT_DTS_HD_MA */ 0x32000000:
return "AUDIO_FORMAT_DTS_HD_MA";
case /* AUDIO_FORMAT_DTS_UHD_P2 */ 0x33000000:
return "AUDIO_FORMAT_DTS_UHD_P2";
/* Aliases */
case /* AUDIO_FORMAT_PCM_16_BIT */ 0x1:
return "AUDIO_FORMAT_PCM_16_BIT"; // (PCM | PCM_SUB_16_BIT)
case /* AUDIO_FORMAT_PCM_8_BIT */ 0x2:
return "AUDIO_FORMAT_PCM_8_BIT"; // (PCM | PCM_SUB_8_BIT)
case /* AUDIO_FORMAT_PCM_32_BIT */ 0x3:
return "AUDIO_FORMAT_PCM_32_BIT"; // (PCM | PCM_SUB_32_BIT)
case /* AUDIO_FORMAT_PCM_8_24_BIT */ 0x4:
return "AUDIO_FORMAT_PCM_8_24_BIT"; // (PCM | PCM_SUB_8_24_BIT)
case /* AUDIO_FORMAT_PCM_FLOAT */ 0x5:
return "AUDIO_FORMAT_PCM_FLOAT"; // (PCM | PCM_SUB_FLOAT)
case /* AUDIO_FORMAT_PCM_24_BIT_PACKED */ 0x6:
return "AUDIO_FORMAT_PCM_24_BIT_PACKED"; // (PCM | PCM_SUB_24_BIT_PACKED)
case /* AUDIO_FORMAT_AAC_MAIN */ 0x4000001:
return "AUDIO_FORMAT_AAC_MAIN"; // (AAC | AAC_SUB_MAIN)
case /* AUDIO_FORMAT_AAC_LC */ 0x4000002:
return "AUDIO_FORMAT_AAC_LC"; // (AAC | AAC_SUB_LC)
case /* AUDIO_FORMAT_AAC_SSR */ 0x4000004:
return "AUDIO_FORMAT_AAC_SSR"; // (AAC | AAC_SUB_SSR)
case /* AUDIO_FORMAT_AAC_LTP */ 0x4000008:
return "AUDIO_FORMAT_AAC_LTP"; // (AAC | AAC_SUB_LTP)
case /* AUDIO_FORMAT_AAC_HE_V1 */ 0x4000010:
return "AUDIO_FORMAT_AAC_HE_V1"; // (AAC | AAC_SUB_HE_V1)
case /* AUDIO_FORMAT_AAC_SCALABLE */ 0x4000020:
return "AUDIO_FORMAT_AAC_SCALABLE"; // (AAC | AAC_SUB_SCALABLE)
case /* AUDIO_FORMAT_AAC_ERLC */ 0x4000040:
return "AUDIO_FORMAT_AAC_ERLC"; // (AAC | AAC_SUB_ERLC)
case /* AUDIO_FORMAT_AAC_LD */ 0x4000080:
return "AUDIO_FORMAT_AAC_LD"; // (AAC | AAC_SUB_LD)
case /* AUDIO_FORMAT_AAC_HE_V2 */ 0x4000100:
return "AUDIO_FORMAT_AAC_HE_V2"; // (AAC | AAC_SUB_HE_V2)
case /* AUDIO_FORMAT_AAC_ELD */ 0x4000200:
return "AUDIO_FORMAT_AAC_ELD"; // (AAC | AAC_SUB_ELD)
case /* AUDIO_FORMAT_AAC_XHE */ 0x4000300:
return "AUDIO_FORMAT_AAC_XHE"; // (AAC | AAC_SUB_XHE)
case /* AUDIO_FORMAT_AAC_ADTS_MAIN */ 0x1e000001:
return "AUDIO_FORMAT_AAC_ADTS_MAIN"; // (AAC_ADTS | AAC_SUB_MAIN)
case /* AUDIO_FORMAT_AAC_ADTS_LC */ 0x1e000002:
return "AUDIO_FORMAT_AAC_ADTS_LC"; // (AAC_ADTS | AAC_SUB_LC)
case /* AUDIO_FORMAT_AAC_ADTS_SSR */ 0x1e000004:
return "AUDIO_FORMAT_AAC_ADTS_SSR"; // (AAC_ADTS | AAC_SUB_SSR)
case /* AUDIO_FORMAT_AAC_ADTS_LTP */ 0x1e000008:
return "AUDIO_FORMAT_AAC_ADTS_LTP"; // (AAC_ADTS | AAC_SUB_LTP)
case /* AUDIO_FORMAT_AAC_ADTS_HE_V1 */ 0x1e000010:
return "AUDIO_FORMAT_AAC_ADTS_HE_V1"; // (AAC_ADTS | AAC_SUB_HE_V1)
case /* AUDIO_FORMAT_AAC_ADTS_SCALABLE */ 0x1e000020:
return "AUDIO_FORMAT_AAC_ADTS_SCALABLE"; // (AAC_ADTS | AAC_SUB_SCALABLE)
case /* AUDIO_FORMAT_AAC_ADTS_ERLC */ 0x1e000040:
return "AUDIO_FORMAT_AAC_ADTS_ERLC"; // (AAC_ADTS | AAC_SUB_ERLC)
case /* AUDIO_FORMAT_AAC_ADTS_LD */ 0x1e000080:
return "AUDIO_FORMAT_AAC_ADTS_LD"; // (AAC_ADTS | AAC_SUB_LD)
case /* AUDIO_FORMAT_AAC_ADTS_HE_V2 */ 0x1e000100:
return "AUDIO_FORMAT_AAC_ADTS_HE_V2"; // (AAC_ADTS | AAC_SUB_HE_V2)
case /* AUDIO_FORMAT_AAC_ADTS_ELD */ 0x1e000200:
return "AUDIO_FORMAT_AAC_ADTS_ELD"; // (AAC_ADTS | AAC_SUB_ELD)
case /* AUDIO_FORMAT_AAC_ADTS_XHE */ 0x1e000300:
return "AUDIO_FORMAT_AAC_ADTS_XHE"; // (AAC_ADTS | AAC_SUB_XHE)
case /* AUDIO_FORMAT_AAC_LATM_LC */ 0x25000002:
return "AUDIO_FORMAT_AAC_LATM_LC"; // (AAC_LATM | AAC_SUB_LC)
case /* AUDIO_FORMAT_AAC_LATM_HE_V1 */ 0x25000010:
return "AUDIO_FORMAT_AAC_LATM_HE_V1"; // (AAC_LATM | AAC_SUB_HE_V1)
case /* AUDIO_FORMAT_AAC_LATM_HE_V2 */ 0x25000100:
return "AUDIO_FORMAT_AAC_LATM_HE_V2"; // (AAC_LATM | AAC_SUB_HE_V2)
case /* AUDIO_FORMAT_E_AC3_JOC */ 0xA000001:
return "AUDIO_FORMAT_E_AC3_JOC"; // (E_AC3 | E_AC3_SUB_JOC)
case /* AUDIO_FORMAT_MAT_1_0 */ 0x24000001:
return "AUDIO_FORMAT_MAT_1_0"; // (MAT | MAT_SUB_1_0)
case /* AUDIO_FORMAT_MAT_2_0 */ 0x24000002:
return "AUDIO_FORMAT_MAT_2_0"; // (MAT | MAT_SUB_2_0)
case /* AUDIO_FORMAT_MAT_2_1 */ 0x24000003:
return "AUDIO_FORMAT_MAT_2_1"; // (MAT | MAT_SUB_2_1)
case /* AUDIO_FORMAT_MPEGH_SUB_BL_L3 */ 0x2C000013:
return "AUDIO_FORMAT_MPEGH_SUB_BL_L3";
case /* AUDIO_FORMAT_MPEGH_SUB_BL_L4 */ 0x2C000014:
return "AUDIO_FORMAT_MPEGH_SUB_BL_L4";
case /* AUDIO_FORMAT_MPEGH_SUB_LC_L3 */ 0x2C000023:
return "AUDIO_FORMAT_MPEGH_SUB_LC_L3";
case /* AUDIO_FORMAT_MPEGH_SUB_LC_L4 */ 0x2C000024:
return "AUDIO_FORMAT_MPEGH_SUB_LC_L4";
default:
return "AUDIO_FORMAT_(" + audioFormat + ")";
}
}
/* Routing bits for the former setRouting/getRouting API */
/** @hide @deprecated */
@Deprecated public static final int ROUTE_EARPIECE = (1 << 0);
/** @hide @deprecated */
@Deprecated public static final int ROUTE_SPEAKER = (1 << 1);
/** @hide @deprecated use {@link #ROUTE_BLUETOOTH_SCO} */
@Deprecated public static final int ROUTE_BLUETOOTH = (1 << 2);
/** @hide @deprecated */
@Deprecated public static final int ROUTE_BLUETOOTH_SCO = (1 << 2);
/** @hide @deprecated */
@Deprecated public static final int ROUTE_HEADSET = (1 << 3);
/** @hide @deprecated */
@Deprecated public static final int ROUTE_BLUETOOTH_A2DP = (1 << 4);
/** @hide @deprecated */
@Deprecated public static final int ROUTE_ALL = 0xFFFFFFFF;
// Keep in sync with system/media/audio/include/system/audio.h
/** @hide */
public static final int AUDIO_SESSION_ALLOCATE = 0;
/**
* @hide
* Checks whether the specified stream type is active.
*
* return true if any track playing on this stream is active.
*/
@UnsupportedAppUsage
public static native boolean isStreamActive(int stream, int inPastMs);
/**
* @hide
* Checks whether the specified stream type is active on a remotely connected device. The notion
* of what constitutes a remote device is enforced by the audio policy manager of the platform.
*
* return true if any track playing on this stream is active on a remote device.
*/
public static native boolean isStreamActiveRemotely(int stream, int inPastMs);
/**
* @hide
* Checks whether the specified audio source is active.
*
* return true if any recorder using this source is currently recording
*/
@UnsupportedAppUsage
public static native boolean isSourceActive(int source);
/**
* @hide
* Returns a new unused audio session ID
*/
public static native int newAudioSessionId();
/**
* @hide
* Returns a new unused audio player ID
*/
public static native int newAudioPlayerId();
/**
* @hide
* Returns a new unused audio recorder ID
*/
public static native int newAudioRecorderId();
/**
* @hide
* Sets a group generic audio configuration parameters. The use of these parameters
* are platform dependent, see libaudio
*
* param keyValuePairs list of parameters key value pairs in the form:
* key1=value1;key2=value2;...
*/
@UnsupportedAppUsage
public static native int setParameters(String keyValuePairs);
/**
* @hide
* Gets a group generic audio configuration parameters. The use of these parameters
* are platform dependent, see libaudio
*
* param keys list of parameters
* return value: list of parameters key value pairs in the form:
* key1=value1;key2=value2;...
*/
@UnsupportedAppUsage
public static native String getParameters(String keys);
// These match the enum AudioError in frameworks/base/core/jni/android_media_AudioSystem.cpp
/** @hide Command successful or Media server restarted. see ErrorCallback */
public static final int AUDIO_STATUS_OK = 0;
/** @hide Command failed or unspecified audio error. see ErrorCallback */
public static final int AUDIO_STATUS_ERROR = 1;
/** @hide Media server died. see ErrorCallback */
public static final int AUDIO_STATUS_SERVER_DIED = 100;
// all accesses must be synchronized (AudioSystem.class)
private static ErrorCallback sErrorCallback;
/** @hide
* Handles the audio error callback.
*/
public interface ErrorCallback
{
/*
* Callback for audio server errors.
* param error error code:
* - AUDIO_STATUS_OK
* - AUDIO_STATUS_SERVER_DIED
* - AUDIO_STATUS_ERROR
*/
void onError(int error);
};
/**
* @hide
* Registers a callback to be invoked when an error occurs.
* @param cb the callback to run
*/
@UnsupportedAppUsage
public static void setErrorCallback(ErrorCallback cb)
{
synchronized (AudioSystem.class) {
sErrorCallback = cb;
if (cb != null) {
cb.onError(checkAudioFlinger());
}
}
}
@UnsupportedAppUsage
private static void errorCallbackFromNative(int error)
{
ErrorCallback errorCallback;
synchronized (AudioSystem.class) {
errorCallback = sErrorCallback;
}
if (errorCallback != null) {
errorCallback.onError(error);
}
}
/**
* @hide
* Handles events from the audio policy manager about dynamic audio policies
* @see android.media.audiopolicy.AudioPolicy
*/
public interface DynamicPolicyCallback
{
void onDynamicPolicyMixStateUpdate(String regId, int state);
}
//keep in sync with include/media/AudioPolicy.h
private final static int DYNAMIC_POLICY_EVENT_MIX_STATE_UPDATE = 0;
// all accesses must be synchronized (AudioSystem.class)
private static DynamicPolicyCallback sDynPolicyCallback;
/** @hide */
public static void setDynamicPolicyCallback(DynamicPolicyCallback cb)
{
synchronized (AudioSystem.class) {
sDynPolicyCallback = cb;
native_register_dynamic_policy_callback();
}
}
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
private static void dynamicPolicyCallbackFromNative(int event, String regId, int val)
{
DynamicPolicyCallback cb;
synchronized (AudioSystem.class) {
cb = sDynPolicyCallback;
}
if (cb != null) {
switch(event) {
case DYNAMIC_POLICY_EVENT_MIX_STATE_UPDATE:
cb.onDynamicPolicyMixStateUpdate(regId, val);
break;
default:
Log.e(TAG, "dynamicPolicyCallbackFromNative: unknown event " + event);
}
}
}
/**
* @hide
* Handles events from the audio policy manager about recording events
* @see android.media.AudioManager.AudioRecordingCallback
*/
public interface AudioRecordingCallback
{
/**
* Callback for recording activity notifications events
* @param event
* @param riid recording identifier
* @param uid uid of the client app performing the recording
* @param session
* @param source
* @param recordingFormat an array of ints containing respectively the client and device
* recording configurations (2*3 ints), followed by the patch handle:
* index 0: client format
* 1: client channel mask
* 2: client sample rate
* 3: device format
* 4: device channel mask
* 5: device sample rate
* 6: patch handle
* @param packName package name of the client app performing the recording. NOT SUPPORTED
*/
void onRecordingConfigurationChanged(int event, int riid, int uid, int session, int source,
int portId, boolean silenced, int[] recordingFormat,
AudioEffect.Descriptor[] clienteffects, AudioEffect.Descriptor[] effects,
int activeSource, String packName);
}
// all accesses must be synchronized (AudioSystem.class)
private static AudioRecordingCallback sRecordingCallback;
/** @hide */
public static void setRecordingCallback(AudioRecordingCallback cb) {
synchronized (AudioSystem.class) {
sRecordingCallback = cb;
native_register_recording_callback();
}
}
/**
* Callback from native for recording configuration updates.
* @param event
* @param riid
* @param uid
* @param session
* @param source
* @param portId
* @param silenced
* @param recordingFormat see
* {@link AudioRecordingCallback#onRecordingConfigurationChanged(int, int, int, int, int, \
int, boolean, int[], AudioEffect.Descriptor[], AudioEffect.Descriptor[], int, String)}
* for the description of the record format.
* @param cleintEffects
* @param effects
* @param activeSource
*/
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
private static void recordingCallbackFromNative(int event, int riid, int uid, int session,
int source, int portId, boolean silenced, int[] recordingFormat,
AudioEffect.Descriptor[] clientEffects, AudioEffect.Descriptor[] effects,
int activeSource) {
AudioRecordingCallback cb;
synchronized (AudioSystem.class) {
cb = sRecordingCallback;
}
String clientEffectName = clientEffects.length == 0 ? "None" : clientEffects[0].name;
String effectName = effects.length == 0 ? "None" : effects[0].name;
if (cb != null) {
ArrayList<AudioPatch> audioPatches = new ArrayList<>();
if (AudioManager.listAudioPatches(audioPatches) == AudioManager.SUCCESS) {
boolean patchFound = false;
int patchHandle = recordingFormat[6];
for (AudioPatch patch : audioPatches) {
if (patch.id() == patchHandle) {
patchFound = true;
break;
}
}
if (!patchFound) {
// The cached audio patches in AudioManager is not up-to-date.
// Reset audio port generation to ensure callback side can
// get up-to-date audio port information.
AudioManager.resetAudioPortGeneration();
}
}
// TODO receive package name from native
cb.onRecordingConfigurationChanged(event, riid, uid, session, source, portId, silenced,
recordingFormat, clientEffects, effects, activeSource, "");
}
}
/**
* @hide
* Handles events from the audio policy manager about routing events
*/
public interface RoutingUpdateCallback {
/**
* Callback to notify a routing update event occurred
*/
void onRoutingUpdated();
}
@GuardedBy("AudioSystem.class")
private static RoutingUpdateCallback sRoutingUpdateCallback;
/** @hide */
public static void setRoutingCallback(RoutingUpdateCallback cb) {
synchronized (AudioSystem.class) {
sRoutingUpdateCallback = cb;
native_register_routing_callback();
}
}
private static void routingCallbackFromNative() {
final RoutingUpdateCallback cb;
synchronized (AudioSystem.class) {
cb = sRoutingUpdateCallback;
}
if (cb == null) {
Log.e(TAG, "routing update from APM was not captured");
return;
}
cb.onRoutingUpdated();
}
/**
* @hide
* Handles requests from the audio policy manager to (re-)initialize the volume ranges
*/
public interface VolumeRangeInitRequestCallback {
/**
* Callback to notify volume ranges need to be initialized
*/
void onVolumeRangeInitializationRequested();
}
@GuardedBy("AudioSystem.class")
private static VolumeRangeInitRequestCallback sVolRangeInitReqCallback;
/** @hide */
public static void setVolumeRangeInitRequestCallback(VolumeRangeInitRequestCallback cb) {
synchronized (AudioSystem.class) {
sVolRangeInitReqCallback = cb;
native_register_vol_range_init_req_callback();
}
}
private static void volRangeInitReqCallbackFromNative() {
final VolumeRangeInitRequestCallback cb;
synchronized (AudioSystem.class) {
cb = sVolRangeInitReqCallback;
}
if (cb == null) {
Log.e(TAG, "APM requested volume range initialization, but no callback found");
return;
}
cb.onVolumeRangeInitializationRequested();
}
/*
* Error codes used by public APIs (AudioTrack, AudioRecord, AudioManager ...)
* Must be kept in sync with frameworks/base/core/jni/android_media_AudioErrors.h
*/
/** @hide */
public static final int SUCCESS = 0;
/** @hide */
public static final int ERROR = -1;
/** @hide */
public static final int BAD_VALUE = -2;
/** @hide */
public static final int INVALID_OPERATION = -3;
/** @hide */
public static final int PERMISSION_DENIED = -4;
/** @hide */
public static final int NO_INIT = -5;
/** @hide */
public static final int DEAD_OBJECT = -6;
/** @hide */
public static final int WOULD_BLOCK = -7;
/** @hide */
@IntDef({
SUCCESS,
ERROR,
BAD_VALUE,
INVALID_OPERATION,
PERMISSION_DENIED,
NO_INIT,
DEAD_OBJECT,
WOULD_BLOCK
})
@Retention(RetentionPolicy.SOURCE)
public @interface AudioSystemError {}
/**
* @hide
* Convert an int error value to its String value for readability.
* Accepted error values are the java AudioSystem errors, matching android_media_AudioErrors.h,
* which map onto the native status_t type.
* @param error one of the java AudioSystem errors
* @return a human-readable string
*/
public static String audioSystemErrorToString(@AudioSystemError int error) {
switch(error) {
case SUCCESS:
return "SUCCESS";
case ERROR:
return "ERROR";
case BAD_VALUE:
return "BAD_VALUE";
case INVALID_OPERATION:
return "INVALID_OPERATION";
case PERMISSION_DENIED:
return "PERMISSION_DENIED";
case NO_INIT:
return "NO_INIT";
case DEAD_OBJECT:
return "DEAD_OBJECT";
case WOULD_BLOCK:
return "WOULD_BLOCK";
default:
return ("unknown error:" + error);
}
}
/*
* AudioPolicyService methods
*/
//
// audio device definitions: must be kept in sync with values
// in system/media/audio/include/system/audio-hal-enums.h
//
/** @hide */
public static final int DEVICE_NONE = 0x0;
// reserved bits
/** @hide */
public static final int DEVICE_BIT_IN = 0x80000000;
/** @hide */
public static final int DEVICE_BIT_DEFAULT = 0x40000000;
// output devices, be sure to update AudioManager.java also
/** @hide */
@UnsupportedAppUsage
public static final int DEVICE_OUT_EARPIECE = 0x1;
/** @hide */
@UnsupportedAppUsage
public static final int DEVICE_OUT_SPEAKER = 0x2;