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.UnsupportedAppUsage;
20 import android.media.AudioSystem;
21 
22 /**
23  * The AudioDevicePort is a specialized type of AudioPort
24  * describing an input (e.g microphone) or output device (e.g speaker)
25  * of the system.
26  * An AudioDevicePort is an AudioPort controlled by the audio HAL, almost always a physical
27  * device at the boundary of the audio system.
28  * In addition to base audio port attributes, the device descriptor contains:
29  * - the device type (e.g AudioManager.DEVICE_OUT_SPEAKER)
30  * - the device address (e.g MAC adddress for AD2P sink).
31  * @see AudioPort
32  * @hide
33  */
34 
35 public class AudioDevicePort extends AudioPort {
36 
37     private final int mType;
38     private final String mAddress;
39 
40     @UnsupportedAppUsage
AudioDevicePort(AudioHandle handle, String deviceName, int[] samplingRates, int[] channelMasks, int[] channelIndexMasks, int[] formats, AudioGain[] gains, int type, String address)41     AudioDevicePort(AudioHandle handle, String deviceName,
42             int[] samplingRates, int[] channelMasks, int[] channelIndexMasks,
43             int[] formats, AudioGain[] gains, int type, String address) {
44         super(handle,
45              (AudioManager.isInputDevice(type) == true)  ?
46                         AudioPort.ROLE_SOURCE : AudioPort.ROLE_SINK,
47              deviceName, samplingRates, channelMasks, channelIndexMasks, formats, gains);
48         mType = type;
49         mAddress = address;
50     }
51 
52     /**
53      * Get the device type (e.g AudioManager.DEVICE_OUT_SPEAKER)
54      */
55     @UnsupportedAppUsage
type()56     public int type() {
57         return mType;
58     }
59 
60     /**
61      * Get the device address. Address format varies with the device type.
62      * - USB devices ({@link AudioManager#DEVICE_OUT_USB_DEVICE},
63      * {@link AudioManager#DEVICE_IN_USB_DEVICE}) use an address composed of the ALSA card number
64      * and device number: "card=2;device=1"
65      * - Bluetooth devices ({@link AudioManager#DEVICE_OUT_BLUETOOTH_SCO},
66      * {@link AudioManager#DEVICE_OUT_BLUETOOTH_SCO}, {@link AudioManager#DEVICE_OUT_BLUETOOTH_A2DP})
67      * use the MAC address of the bluetooth device in the form "00:11:22:AA:BB:CC" as reported by
68      * {@link BluetoothDevice#getAddress()}.
69      * - Deivces that do not have an address will indicate an empty string "".
70      */
address()71     public String address() {
72         return mAddress;
73     }
74 
75     /**
76      * Build a specific configuration of this audio device port for use by methods
77      * like AudioManager.connectAudioPatch().
78      */
buildConfig(int samplingRate, int channelMask, int format, AudioGainConfig gain)79     public AudioDevicePortConfig buildConfig(int samplingRate, int channelMask, int format,
80                                           AudioGainConfig gain) {
81         return new AudioDevicePortConfig(this, samplingRate, channelMask, format, gain);
82     }
83 
84     @Override
equals(Object o)85     public boolean equals(Object o) {
86         if (o == null || !(o instanceof AudioDevicePort)) {
87             return false;
88         }
89         AudioDevicePort other = (AudioDevicePort)o;
90         if (mType != other.type()) {
91             return false;
92         }
93         if (mAddress == null && other.address() != null) {
94             return false;
95         }
96         if (!mAddress.equals(other.address())) {
97             return false;
98         }
99         return super.equals(o);
100     }
101 
102     @Override
toString()103     public String toString() {
104         String type = (mRole == ROLE_SOURCE ?
105                             AudioSystem.getInputDeviceName(mType) :
106                             AudioSystem.getOutputDeviceName(mType));
107         return "{" + super.toString()
108                 + ", mType: " + type
109                 + ", mAddress: " + mAddress
110                 + "}";
111     }
112 }
113