1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.media;
18
19import android.annotation.NonNull;
20import android.util.SparseIntArray;
21
22import java.util.TreeSet;
23
24/**
25 * Class to provide information about the audio devices.
26 */
27public final class AudioDeviceInfo {
28
29    /**
30     * A device type associated with an unknown or uninitialized device.
31     */
32    public static final int TYPE_UNKNOWN          = 0;
33    /**
34     * A device type describing the attached earphone speaker.
35     */
36    public static final int TYPE_BUILTIN_EARPIECE = 1;
37    /**
38     * A device type describing the speaker system (i.e. a mono speaker or stereo speakers) built
39     * in a device.
40     */
41    public static final int TYPE_BUILTIN_SPEAKER  = 2;
42    /**
43     * A device type describing a headset, which is the combination of a headphones and microphone.
44     */
45    public static final int TYPE_WIRED_HEADSET    = 3;
46    /**
47     * A device type describing a pair of wired headphones.
48     */
49    public static final int TYPE_WIRED_HEADPHONES = 4;
50    /**
51     * A device type describing an analog line-level connection.
52     */
53    public static final int TYPE_LINE_ANALOG      = 5;
54    /**
55     * A device type describing a digital line connection (e.g. SPDIF).
56     */
57    public static final int TYPE_LINE_DIGITAL     = 6;
58    /**
59     * A device type describing a Bluetooth device typically used for telephony.
60     */
61    public static final int TYPE_BLUETOOTH_SCO    = 7;
62    /**
63     * A device type describing a Bluetooth device supporting the A2DP profile.
64     */
65    public static final int TYPE_BLUETOOTH_A2DP   = 8;
66    /**
67     * A device type describing an HDMI connection .
68     */
69    public static final int TYPE_HDMI             = 9;
70    /**
71     * A device type describing the Audio Return Channel of an HDMI connection.
72     */
73    public static final int TYPE_HDMI_ARC         = 10;
74    /**
75     * A device type describing a USB audio device.
76     */
77    public static final int TYPE_USB_DEVICE       = 11;
78    /**
79     * A device type describing a USB audio device in accessory mode.
80     */
81    public static final int TYPE_USB_ACCESSORY    = 12;
82    /**
83     * A device type describing the audio device associated with a dock.
84     */
85    public static final int TYPE_DOCK             = 13;
86    /**
87     * A device type associated with the transmission of audio signals over FM.
88     */
89    public static final int TYPE_FM               = 14;
90    /**
91     * A device type describing the microphone(s) built in a device.
92     */
93    public static final int TYPE_BUILTIN_MIC      = 15;
94    /**
95     * A device type for accessing the audio content transmitted over FM.
96     */
97    public static final int TYPE_FM_TUNER         = 16;
98    /**
99     * A device type for accessing the audio content transmitted over the TV tuner system.
100     */
101    public static final int TYPE_TV_TUNER         = 17;
102    /**
103     * A device type describing the transmission of audio signals over the telephony network.
104     */
105    public static final int TYPE_TELEPHONY        = 18;
106    /**
107     * A device type describing the auxiliary line-level connectors.
108     */
109    public static final int TYPE_AUX_LINE         = 19;
110    /**
111     * A device type connected over IP.
112     */
113    public static final int TYPE_IP               = 20;
114
115    private final AudioDevicePort mPort;
116
117    AudioDeviceInfo(AudioDevicePort port) {
118       mPort = port;
119    }
120
121    /**
122     * @return The internal device ID.
123     */
124    public int getId() {
125        return mPort.handle().id();
126    }
127
128    /**
129     * @return The human-readable name of the audio device.
130     */
131    public CharSequence getProductName() {
132        String portName = mPort.name();
133        return portName.length() != 0 ? portName : android.os.Build.MODEL;
134    }
135
136    /**
137     * @hide
138     * @return The "address" string of the device. This generally contains device-specific
139     * parameters.
140     */
141    public String getAddress() {
142        return mPort.address();
143    }
144
145   /**
146     * @return true if the audio device is a source for audio data (e.e an input).
147     */
148    public boolean isSource() {
149        return mPort.role() == AudioPort.ROLE_SOURCE;
150    }
151
152    /**
153     * @return true if the audio device is a sink for audio data (i.e. an output).
154     */
155    public boolean isSink() {
156        return mPort.role() == AudioPort.ROLE_SINK;
157    }
158
159    /**
160     * @return An array of sample rates supported by the audio device.
161     *
162     * Note: an empty array indicates that the device supports arbitrary rates.
163     */
164    public @NonNull int[] getSampleRates() {
165        return mPort.samplingRates();
166    }
167
168    /**
169     * @return An array of channel position masks (e.g. {@link AudioFormat#CHANNEL_IN_STEREO},
170     * {@link AudioFormat#CHANNEL_OUT_7POINT1}) for which this audio device can be configured.
171     *
172     * @see AudioFormat
173     *
174     * Note: an empty array indicates that the device supports arbitrary channel masks.
175     */
176    public @NonNull int[] getChannelMasks() {
177        return mPort.channelMasks();
178    }
179
180    /**
181     * @return An array of channel index masks for which this audio device can be configured.
182     *
183     * @see AudioFormat
184     *
185     * Note: an empty array indicates that the device supports arbitrary channel index masks.
186     */
187    public @NonNull int[] getChannelIndexMasks() {
188        return mPort.channelIndexMasks();
189    }
190
191    /**
192     * @return An array of channel counts (1, 2, 4, ...) for which this audio device
193     * can be configured.
194     *
195     * Note: an empty array indicates that the device supports arbitrary channel counts.
196     */
197    public @NonNull int[] getChannelCounts() {
198        TreeSet<Integer> countSet = new TreeSet<Integer>();
199
200        // Channel Masks
201        for (int mask : getChannelMasks()) {
202            countSet.add(isSink() ?
203                    AudioFormat.channelCountFromOutChannelMask(mask)
204                    : AudioFormat.channelCountFromInChannelMask(mask));
205        }
206
207        // Index Masks
208        for (int index_mask : getChannelIndexMasks()) {
209            countSet.add(Integer.bitCount(index_mask));
210        }
211
212        int[] counts = new int[countSet.size()];
213        int index = 0;
214        for (int count : countSet) {
215            counts[index++] = count;
216        }
217        return counts;
218    }
219
220    /**
221     * @return An array of audio encodings (e.g. {@link AudioFormat#ENCODING_PCM_16BIT},
222     * {@link AudioFormat#ENCODING_PCM_FLOAT}) supported by the audio device.
223     * <code>ENCODING_PCM_FLOAT</code> indicates the device supports more
224     * than 16 bits of integer precision.  Specifying <code>ENCODING_PCM_FLOAT</code>
225     * with {@link AudioTrack} or {@link AudioRecord} can preserve at least 24 bits of
226     * integer precision to that device.
227     *
228     * @see AudioFormat
229     *
230     * Note: an empty array indicates that the device supports arbitrary encodings.
231     */
232    public @NonNull int[] getEncodings() {
233        return AudioFormat.filterPublicFormats(mPort.formats());
234    }
235
236   /**
237     * @return The device type identifier of the audio device (i.e. TYPE_BUILTIN_SPEAKER).
238     */
239    public int getType() {
240        return INT_TO_EXT_DEVICE_MAPPING.get(mPort.type(), TYPE_UNKNOWN);
241    }
242
243    /** @hide */
244    public static int convertDeviceTypeToInternalDevice(int deviceType) {
245        return EXT_TO_INT_DEVICE_MAPPING.get(deviceType, AudioSystem.DEVICE_NONE);
246    }
247
248    /** @hide */
249    public static int convertInternalDeviceToDeviceType(int intDevice) {
250        return INT_TO_EXT_DEVICE_MAPPING.get(intDevice, TYPE_UNKNOWN);
251    }
252
253    private static final SparseIntArray INT_TO_EXT_DEVICE_MAPPING;
254
255    private static final SparseIntArray EXT_TO_INT_DEVICE_MAPPING;
256
257    static {
258        INT_TO_EXT_DEVICE_MAPPING = new SparseIntArray();
259        INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_OUT_EARPIECE, TYPE_BUILTIN_EARPIECE);
260        INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_OUT_SPEAKER, TYPE_BUILTIN_SPEAKER);
261        INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_OUT_WIRED_HEADSET, TYPE_WIRED_HEADSET);
262        INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_OUT_WIRED_HEADPHONE, TYPE_WIRED_HEADPHONES);
263        INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_OUT_BLUETOOTH_SCO, TYPE_BLUETOOTH_SCO);
264        INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_OUT_BLUETOOTH_SCO_HEADSET, TYPE_BLUETOOTH_SCO);
265        INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_OUT_BLUETOOTH_SCO_CARKIT, TYPE_BLUETOOTH_SCO);
266        INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, TYPE_BLUETOOTH_A2DP);
267        INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES, TYPE_BLUETOOTH_A2DP);
268        INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER, TYPE_BLUETOOTH_A2DP);
269        INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_OUT_HDMI, TYPE_HDMI);
270        INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_OUT_ANLG_DOCK_HEADSET, TYPE_DOCK);
271        INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_OUT_DGTL_DOCK_HEADSET, TYPE_DOCK);
272        INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_OUT_USB_ACCESSORY, TYPE_USB_ACCESSORY);
273        INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_OUT_USB_DEVICE, TYPE_USB_DEVICE);
274        INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_OUT_TELEPHONY_TX, TYPE_TELEPHONY);
275        INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_OUT_LINE, TYPE_LINE_ANALOG);
276        INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_OUT_HDMI_ARC, TYPE_HDMI_ARC);
277        INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_OUT_SPDIF, TYPE_LINE_DIGITAL);
278        INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_OUT_FM, TYPE_FM);
279        INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_OUT_AUX_LINE, TYPE_AUX_LINE);
280        INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_OUT_IP, TYPE_IP);
281
282        INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_IN_BUILTIN_MIC, TYPE_BUILTIN_MIC);
283        INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_IN_BLUETOOTH_SCO_HEADSET, TYPE_BLUETOOTH_SCO);
284        INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_IN_WIRED_HEADSET, TYPE_WIRED_HEADSET);
285        INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_IN_HDMI, TYPE_HDMI);
286        INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_IN_TELEPHONY_RX, TYPE_TELEPHONY);
287        INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_IN_BACK_MIC, TYPE_BUILTIN_MIC);
288        INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_IN_ANLG_DOCK_HEADSET, TYPE_DOCK);
289        INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_IN_DGTL_DOCK_HEADSET, TYPE_DOCK);
290        INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_IN_USB_ACCESSORY, TYPE_USB_ACCESSORY);
291        INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_IN_USB_DEVICE, TYPE_USB_DEVICE);
292        INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_IN_FM_TUNER, TYPE_FM_TUNER);
293        INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_IN_TV_TUNER, TYPE_TV_TUNER);
294        INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_IN_LINE, TYPE_LINE_ANALOG);
295        INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_IN_SPDIF, TYPE_LINE_DIGITAL);
296        INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP, TYPE_BLUETOOTH_A2DP);
297        INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_IN_IP, TYPE_IP);
298
299        // not covered here, legacy
300        //AudioSystem.DEVICE_OUT_REMOTE_SUBMIX
301        //AudioSystem.DEVICE_IN_REMOTE_SUBMIX
302
303        // privileges mapping to output device
304        EXT_TO_INT_DEVICE_MAPPING = new SparseIntArray();
305        EXT_TO_INT_DEVICE_MAPPING.put(TYPE_BUILTIN_EARPIECE, AudioSystem.DEVICE_OUT_EARPIECE);
306        EXT_TO_INT_DEVICE_MAPPING.put(TYPE_BUILTIN_SPEAKER, AudioSystem.DEVICE_OUT_SPEAKER);
307        EXT_TO_INT_DEVICE_MAPPING.put(TYPE_WIRED_HEADSET, AudioSystem.DEVICE_OUT_WIRED_HEADSET);
308        EXT_TO_INT_DEVICE_MAPPING.put(TYPE_WIRED_HEADPHONES, AudioSystem.DEVICE_OUT_WIRED_HEADPHONE);
309        EXT_TO_INT_DEVICE_MAPPING.put(TYPE_LINE_ANALOG, AudioSystem.DEVICE_OUT_LINE);
310        EXT_TO_INT_DEVICE_MAPPING.put(TYPE_LINE_DIGITAL, AudioSystem.DEVICE_OUT_SPDIF);
311        EXT_TO_INT_DEVICE_MAPPING.put(TYPE_BLUETOOTH_SCO, AudioSystem.DEVICE_OUT_BLUETOOTH_SCO);
312        EXT_TO_INT_DEVICE_MAPPING.put(TYPE_BLUETOOTH_A2DP, AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP);
313        EXT_TO_INT_DEVICE_MAPPING.put(TYPE_HDMI, AudioSystem.DEVICE_OUT_HDMI);
314        EXT_TO_INT_DEVICE_MAPPING.put(TYPE_HDMI_ARC, AudioSystem.DEVICE_OUT_HDMI_ARC);
315        EXT_TO_INT_DEVICE_MAPPING.put(TYPE_USB_DEVICE, AudioSystem.DEVICE_OUT_USB_DEVICE);
316        EXT_TO_INT_DEVICE_MAPPING.put(TYPE_USB_ACCESSORY, AudioSystem.DEVICE_OUT_USB_ACCESSORY);
317        EXT_TO_INT_DEVICE_MAPPING.put(TYPE_DOCK, AudioSystem.DEVICE_OUT_ANLG_DOCK_HEADSET);
318        EXT_TO_INT_DEVICE_MAPPING.put(TYPE_FM, AudioSystem.DEVICE_OUT_FM);
319        EXT_TO_INT_DEVICE_MAPPING.put(TYPE_BUILTIN_MIC, AudioSystem.DEVICE_IN_BUILTIN_MIC);
320        EXT_TO_INT_DEVICE_MAPPING.put(TYPE_FM_TUNER, AudioSystem.DEVICE_IN_FM_TUNER);
321        EXT_TO_INT_DEVICE_MAPPING.put(TYPE_TV_TUNER, AudioSystem.DEVICE_IN_TV_TUNER);
322        EXT_TO_INT_DEVICE_MAPPING.put(TYPE_TELEPHONY, AudioSystem.DEVICE_OUT_TELEPHONY_TX);
323        EXT_TO_INT_DEVICE_MAPPING.put(TYPE_AUX_LINE, AudioSystem.DEVICE_OUT_AUX_LINE);
324        EXT_TO_INT_DEVICE_MAPPING.put(TYPE_IP, AudioSystem.DEVICE_OUT_IP);
325    }
326}
327
328