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