Home | History | Annotate | Download | only in media
      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 
     17 package android.media;
     18 
     19 import android.annotation.NonNull;
     20 import android.util.SparseIntArray;
     21 
     22 import java.util.TreeSet;
     23 
     24 /**
     25  * Class to provide information about the audio devices.
     26  */
     27 public 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      * A type-agnostic device used for communication with external audio systems
    116      */
    117     public static final int TYPE_BUS              = 21;
    118 
    119     private final AudioDevicePort mPort;
    120 
    121     AudioDeviceInfo(AudioDevicePort port) {
    122        mPort = port;
    123     }
    124 
    125     /**
    126      * @return The internal device ID.
    127      */
    128     public int getId() {
    129         return mPort.handle().id();
    130     }
    131 
    132     /**
    133      * @return The human-readable name of the audio device.
    134      */
    135     public CharSequence getProductName() {
    136         String portName = mPort.name();
    137         return portName.length() != 0 ? portName : android.os.Build.MODEL;
    138     }
    139 
    140     /**
    141      * @hide
    142      * @return The "address" string of the device. This generally contains device-specific
    143      * parameters.
    144      */
    145     public String getAddress() {
    146         return mPort.address();
    147     }
    148 
    149    /**
    150      * @return true if the audio device is a source for audio data (e.e an input).
    151      */
    152     public boolean isSource() {
    153         return mPort.role() == AudioPort.ROLE_SOURCE;
    154     }
    155 
    156     /**
    157      * @return true if the audio device is a sink for audio data (i.e. an output).
    158      */
    159     public boolean isSink() {
    160         return mPort.role() == AudioPort.ROLE_SINK;
    161     }
    162 
    163     /**
    164      * @return An array of sample rates supported by the audio device.
    165      *
    166      * Note: an empty array indicates that the device supports arbitrary rates.
    167      */
    168     public @NonNull int[] getSampleRates() {
    169         return mPort.samplingRates();
    170     }
    171 
    172     /**
    173      * @return An array of channel position masks (e.g. {@link AudioFormat#CHANNEL_IN_STEREO},
    174      * {@link AudioFormat#CHANNEL_OUT_7POINT1}) for which this audio device can be configured.
    175      *
    176      * @see AudioFormat
    177      *
    178      * Note: an empty array indicates that the device supports arbitrary channel masks.
    179      */
    180     public @NonNull int[] getChannelMasks() {
    181         return mPort.channelMasks();
    182     }
    183 
    184     /**
    185      * @return An array of channel index masks for which this audio device can be configured.
    186      *
    187      * @see AudioFormat
    188      *
    189      * Note: an empty array indicates that the device supports arbitrary channel index masks.
    190      */
    191     public @NonNull int[] getChannelIndexMasks() {
    192         return mPort.channelIndexMasks();
    193     }
    194 
    195     /**
    196      * @return An array of channel counts (1, 2, 4, ...) for which this audio device
    197      * can be configured.
    198      *
    199      * Note: an empty array indicates that the device supports arbitrary channel counts.
    200      */
    201     public @NonNull int[] getChannelCounts() {
    202         TreeSet<Integer> countSet = new TreeSet<Integer>();
    203 
    204         // Channel Masks
    205         for (int mask : getChannelMasks()) {
    206             countSet.add(isSink() ?
    207                     AudioFormat.channelCountFromOutChannelMask(mask)
    208                     : AudioFormat.channelCountFromInChannelMask(mask));
    209         }
    210 
    211         // Index Masks
    212         for (int index_mask : getChannelIndexMasks()) {
    213             countSet.add(Integer.bitCount(index_mask));
    214         }
    215 
    216         int[] counts = new int[countSet.size()];
    217         int index = 0;
    218         for (int count : countSet) {
    219             counts[index++] = count;
    220         }
    221         return counts;
    222     }
    223 
    224     /**
    225      * @return An array of audio encodings (e.g. {@link AudioFormat#ENCODING_PCM_16BIT},
    226      * {@link AudioFormat#ENCODING_PCM_FLOAT}) supported by the audio device.
    227      * <code>ENCODING_PCM_FLOAT</code> indicates the device supports more
    228      * than 16 bits of integer precision.  As there is no AudioFormat constant
    229      * specifically defined for 24-bit PCM, the value <code>ENCODING_PCM_FLOAT</code>
    230      * indicates that {@link AudioTrack} or {@link AudioRecord} can preserve at least 24 bits of
    231      * integer precision to that device.
    232      *
    233      * @see AudioFormat
    234      *
    235      * Note: an empty array indicates that the device supports arbitrary encodings.
    236      */
    237     public @NonNull int[] getEncodings() {
    238         return AudioFormat.filterPublicFormats(mPort.formats());
    239     }
    240 
    241    /**
    242      * @return The device type identifier of the audio device (i.e. TYPE_BUILTIN_SPEAKER).
    243      */
    244     public int getType() {
    245         return INT_TO_EXT_DEVICE_MAPPING.get(mPort.type(), TYPE_UNKNOWN);
    246     }
    247 
    248     /** @hide */
    249     public static int convertDeviceTypeToInternalDevice(int deviceType) {
    250         return EXT_TO_INT_DEVICE_MAPPING.get(deviceType, AudioSystem.DEVICE_NONE);
    251     }
    252 
    253     /** @hide */
    254     public static int convertInternalDeviceToDeviceType(int intDevice) {
    255         return INT_TO_EXT_DEVICE_MAPPING.get(intDevice, TYPE_UNKNOWN);
    256     }
    257 
    258     private static final SparseIntArray INT_TO_EXT_DEVICE_MAPPING;
    259 
    260     private static final SparseIntArray EXT_TO_INT_DEVICE_MAPPING;
    261 
    262     static {
    263         INT_TO_EXT_DEVICE_MAPPING = new SparseIntArray();
    264         INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_OUT_EARPIECE, TYPE_BUILTIN_EARPIECE);
    265         INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_OUT_SPEAKER, TYPE_BUILTIN_SPEAKER);
    266         INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_OUT_WIRED_HEADSET, TYPE_WIRED_HEADSET);
    267         INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_OUT_WIRED_HEADPHONE, TYPE_WIRED_HEADPHONES);
    268         INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_OUT_BLUETOOTH_SCO, TYPE_BLUETOOTH_SCO);
    269         INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_OUT_BLUETOOTH_SCO_HEADSET, TYPE_BLUETOOTH_SCO);
    270         INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_OUT_BLUETOOTH_SCO_CARKIT, TYPE_BLUETOOTH_SCO);
    271         INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP, TYPE_BLUETOOTH_A2DP);
    272         INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES, TYPE_BLUETOOTH_A2DP);
    273         INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER, TYPE_BLUETOOTH_A2DP);
    274         INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_OUT_HDMI, TYPE_HDMI);
    275         INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_OUT_ANLG_DOCK_HEADSET, TYPE_DOCK);
    276         INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_OUT_DGTL_DOCK_HEADSET, TYPE_DOCK);
    277         INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_OUT_USB_ACCESSORY, TYPE_USB_ACCESSORY);
    278         INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_OUT_USB_DEVICE, TYPE_USB_DEVICE);
    279         INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_OUT_TELEPHONY_TX, TYPE_TELEPHONY);
    280         INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_OUT_LINE, TYPE_LINE_ANALOG);
    281         INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_OUT_HDMI_ARC, TYPE_HDMI_ARC);
    282         INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_OUT_SPDIF, TYPE_LINE_DIGITAL);
    283         INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_OUT_FM, TYPE_FM);
    284         INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_OUT_AUX_LINE, TYPE_AUX_LINE);
    285         INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_OUT_IP, TYPE_IP);
    286         INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_OUT_BUS, TYPE_BUS);
    287 
    288         INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_IN_BUILTIN_MIC, TYPE_BUILTIN_MIC);
    289         INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_IN_BLUETOOTH_SCO_HEADSET, TYPE_BLUETOOTH_SCO);
    290         INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_IN_WIRED_HEADSET, TYPE_WIRED_HEADSET);
    291         INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_IN_HDMI, TYPE_HDMI);
    292         INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_IN_TELEPHONY_RX, TYPE_TELEPHONY);
    293         INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_IN_BACK_MIC, TYPE_BUILTIN_MIC);
    294         INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_IN_ANLG_DOCK_HEADSET, TYPE_DOCK);
    295         INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_IN_DGTL_DOCK_HEADSET, TYPE_DOCK);
    296         INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_IN_USB_ACCESSORY, TYPE_USB_ACCESSORY);
    297         INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_IN_USB_DEVICE, TYPE_USB_DEVICE);
    298         INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_IN_FM_TUNER, TYPE_FM_TUNER);
    299         INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_IN_TV_TUNER, TYPE_TV_TUNER);
    300         INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_IN_LINE, TYPE_LINE_ANALOG);
    301         INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_IN_SPDIF, TYPE_LINE_DIGITAL);
    302         INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_IN_BLUETOOTH_A2DP, TYPE_BLUETOOTH_A2DP);
    303         INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_IN_IP, TYPE_IP);
    304         INT_TO_EXT_DEVICE_MAPPING.put(AudioSystem.DEVICE_IN_BUS, TYPE_BUS);
    305 
    306         // not covered here, legacy
    307         //AudioSystem.DEVICE_OUT_REMOTE_SUBMIX
    308         //AudioSystem.DEVICE_IN_REMOTE_SUBMIX
    309 
    310         // privileges mapping to output device
    311         EXT_TO_INT_DEVICE_MAPPING = new SparseIntArray();
    312         EXT_TO_INT_DEVICE_MAPPING.put(TYPE_BUILTIN_EARPIECE, AudioSystem.DEVICE_OUT_EARPIECE);
    313         EXT_TO_INT_DEVICE_MAPPING.put(TYPE_BUILTIN_SPEAKER, AudioSystem.DEVICE_OUT_SPEAKER);
    314         EXT_TO_INT_DEVICE_MAPPING.put(TYPE_WIRED_HEADSET, AudioSystem.DEVICE_OUT_WIRED_HEADSET);
    315         EXT_TO_INT_DEVICE_MAPPING.put(TYPE_WIRED_HEADPHONES, AudioSystem.DEVICE_OUT_WIRED_HEADPHONE);
    316         EXT_TO_INT_DEVICE_MAPPING.put(TYPE_LINE_ANALOG, AudioSystem.DEVICE_OUT_LINE);
    317         EXT_TO_INT_DEVICE_MAPPING.put(TYPE_LINE_DIGITAL, AudioSystem.DEVICE_OUT_SPDIF);
    318         EXT_TO_INT_DEVICE_MAPPING.put(TYPE_BLUETOOTH_SCO, AudioSystem.DEVICE_OUT_BLUETOOTH_SCO);
    319         EXT_TO_INT_DEVICE_MAPPING.put(TYPE_BLUETOOTH_A2DP, AudioSystem.DEVICE_OUT_BLUETOOTH_A2DP);
    320         EXT_TO_INT_DEVICE_MAPPING.put(TYPE_HDMI, AudioSystem.DEVICE_OUT_HDMI);
    321         EXT_TO_INT_DEVICE_MAPPING.put(TYPE_HDMI_ARC, AudioSystem.DEVICE_OUT_HDMI_ARC);
    322         EXT_TO_INT_DEVICE_MAPPING.put(TYPE_USB_DEVICE, AudioSystem.DEVICE_OUT_USB_DEVICE);
    323         EXT_TO_INT_DEVICE_MAPPING.put(TYPE_USB_ACCESSORY, AudioSystem.DEVICE_OUT_USB_ACCESSORY);
    324         EXT_TO_INT_DEVICE_MAPPING.put(TYPE_DOCK, AudioSystem.DEVICE_OUT_ANLG_DOCK_HEADSET);
    325         EXT_TO_INT_DEVICE_MAPPING.put(TYPE_FM, AudioSystem.DEVICE_OUT_FM);
    326         EXT_TO_INT_DEVICE_MAPPING.put(TYPE_BUILTIN_MIC, AudioSystem.DEVICE_IN_BUILTIN_MIC);
    327         EXT_TO_INT_DEVICE_MAPPING.put(TYPE_FM_TUNER, AudioSystem.DEVICE_IN_FM_TUNER);
    328         EXT_TO_INT_DEVICE_MAPPING.put(TYPE_TV_TUNER, AudioSystem.DEVICE_IN_TV_TUNER);
    329         EXT_TO_INT_DEVICE_MAPPING.put(TYPE_TELEPHONY, AudioSystem.DEVICE_OUT_TELEPHONY_TX);
    330         EXT_TO_INT_DEVICE_MAPPING.put(TYPE_AUX_LINE, AudioSystem.DEVICE_OUT_AUX_LINE);
    331         EXT_TO_INT_DEVICE_MAPPING.put(TYPE_IP, AudioSystem.DEVICE_OUT_IP);
    332         EXT_TO_INT_DEVICE_MAPPING.put(TYPE_BUS, AudioSystem.DEVICE_OUT_BUS);
    333     }
    334 }
    335 
    336