Debian Codegraph @

Xpjproject/pjmedia/src/pjmedia-audiodev/android/PjAudioDevInfo.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2021 Teluu Inc. (http://www.teluu.com)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 package org.pjsip;
19
20 import android.content.Context;
21 import android.media.AudioDeviceInfo;
22 import android.media.AudioManager;
23 import android.os.Build;
24 import android.util.Log;
25 import android.util.SparseArray;
26 import java.util.Arrays;
27 import java.util.HashMap;
28
29 public class PjAudioDevInfo {
30
31 /* API */
32
33 public int id;
34 public String name;
35 public int direction;
36 public int[] supportedClockRates;
37 public int[] supportedChannelCounts;
38
39 public static int GetCount()
40 {
41 return devices.size();
42 }
43
44 public static PjAudioDevInfo GetInfo(int idx)
45 {
46 return devices.valueAt(idx);
47 }
48
49 public static void RefreshDevices(Context context)
50 {
51 devices = new SparseArray<>();
52
53 /* Default device */
54 PjAudioDevInfo pj_adi = new PjAudioDevInfo();
55 pj_adi.id = 0;
56 pj_adi.name = "Default";
57 pj_adi.direction = 3;
58 devices.put(0, pj_adi);
59
60 /* Enumerate devices (for API level 23 or later) */
61 if (android.os.Build.VERSION.SDK_INT < 23)
62 return;
63
64 AudioManager am = (AudioManager)context.getSystemService(Context.AUDIO_SERVICE);
65 AudioDeviceInfo[] devs = am.getDevices(AudioManager.GET_DEVICES_ALL);
66
67 HashMap<String, Integer> micNamesCounterMap = new HashMap<>();
68 for (AudioDeviceInfo adi : devs) {
69 micNamesCounterMap.merge(adi.getProductName().toString(), 1, Integer::sum);
70 }
71
72 Log.i("Oboe", "Enumerating AudioManager devices..");
73 for (AudioDeviceInfo adi: devs) {
74 LogDevInfo(adi);
75 String productName = adi.getProductName().toString();
76 StringBuilder nameStringBuilder = new StringBuilder().append(DevTypeStr(adi.getType())).append(" - ").append(productName);
77 Integer micNameCounter = micNamesCounterMap.get(productName);
78 if (micNameCounter != null && micNameCounter > 1) {
79 nameStringBuilder.append(" - ").append(adi.getId());
80 }
81 pj_adi = new PjAudioDevInfo();
82 pj_adi.id = adi.getId();
83 pj_adi.name = nameStringBuilder.toString();
84 pj_adi.direction = 0;
85 if (adi.isSource()) pj_adi.direction |= 1;
86 if (adi.isSink()) pj_adi.direction |= 2;
87 pj_adi.supportedChannelCounts = adi.getChannelCounts();
88 pj_adi.supportedClockRates = adi.getSampleRates();
89 devices.put(adi.getId(), pj_adi);
90 }
91 }
92
93 /* Private members */
94
95 private static SparseArray<PjAudioDevInfo> devices;
96
97 private static String DevTypeStr(int type) {
98 if (android.os.Build.VERSION.SDK_INT < 23)
99 return "Unknown";
100
101 /*
102 if (android.os.Build.VERSION.SDK_INT > 30) {
103 if (type == AudioDeviceInfo.TYPE_BLE_HEADSET)
104 return "BLE Headset";
105 if (type == AudioDeviceInfo.TYPE_BLE_SPEAKER)
106 return "BLE Speaker";
107 }
108 */
109
110 switch (type) {
111 case AudioDeviceInfo.TYPE_BUILTIN_EARPIECE:
112 return "Builtin Earpiece";
113 case AudioDeviceInfo.TYPE_BUILTIN_SPEAKER:
114 return "Builtin Speaker";
115 case AudioDeviceInfo.TYPE_WIRED_HEADSET:
116 return "Wired Headset";
117 case AudioDeviceInfo.TYPE_WIRED_HEADPHONES:
118 return "Wired Headphones";
119 case AudioDeviceInfo.TYPE_LINE_ANALOG:
120 return "Line Analog";
121 case AudioDeviceInfo.TYPE_LINE_DIGITAL:
122 return "Line Digital";
123 case AudioDeviceInfo.TYPE_BLUETOOTH_SCO:
124 return "Bluetooth SCO";
125 case AudioDeviceInfo.TYPE_BLUETOOTH_A2DP:
126 return "Bluetooth A2DP";
127 case AudioDeviceInfo.TYPE_HDMI:
128 return "HDMI";
129 case AudioDeviceInfo.TYPE_HDMI_ARC:
130 return "HDMI ARC";
131 case AudioDeviceInfo.TYPE_USB_DEVICE:
132 return "USB Device";
133 case AudioDeviceInfo.TYPE_USB_ACCESSORY:
134 return "USB Accessory";
135 case AudioDeviceInfo.TYPE_DOCK:
136 return "Dock";
137 case AudioDeviceInfo.TYPE_FM:
138 return "FM";
139 case AudioDeviceInfo.TYPE_BUILTIN_MIC:
140 return "Builtin Mic";
141 case AudioDeviceInfo.TYPE_FM_TUNER:
142 return "FM Tuner";
143 case AudioDeviceInfo.TYPE_TV_TUNER:
144 return "TV Tuner";
145 case AudioDeviceInfo.TYPE_TELEPHONY:
146 return "Telephony";
147 case AudioDeviceInfo.TYPE_AUX_LINE:
148 return "AUX Line";
149 case AudioDeviceInfo.TYPE_IP:
150 return "IP";
151 case AudioDeviceInfo.TYPE_BUS:
152 return "Bus";
153 case AudioDeviceInfo.TYPE_USB_HEADSET:
154 return "USB Headset";
155 default:
156 return "Unknown ("+ type +")";
157 }
158 }
159
160 private static void LogDevInfo(AudioDeviceInfo adi) {
161 if (Build.VERSION.SDK_INT < 23)
162 return;
163
164 StringBuilder info = new StringBuilder();
165 info.append("id=").append(adi.getId());
166 info.append(",");
167 if (adi.isSource()) info.append(" in");
168 if (adi.isSink()) info.append(" out");
169 info.append(", ").append(DevTypeStr(adi.getType()));
170 if (adi.getChannelCounts().length > 0) {
171 info.append(", channels=").append(Arrays.toString(adi.getChannelCounts()));
172 }
173 if (adi.getSampleRates().length > 0) {
174 info.append(", clock rates=").append(Arrays.toString(adi.getSampleRates()));
175 }
176 Log.i("Oboe", info.toString());
177 }
178 }
179