AudioPort.java revision 4bcdba848449b33d7022de527c526943aff1f5fd
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
19/**
20 * An audio port is a node of the audio framework or hardware that can be connected to or
21 * disconnect from another audio node to create a specific audio routing configuration.
22 * Examples of audio ports are an output device (speaker) or an output mix (see AudioMixPort).
23 * All attributes that are relevant for applications to make routing selection are decribed
24 * in an AudioPort,  in particular:
25 * - possible channel mask configurations.
26 * - audio format (PCM 16bit, PCM 24bit...)
27 * - gain: a port can be associated with one or more gain controllers (see AudioGain).
28 *
29 * This object is always created by the framework and read only by applications.
30 * A list of all audio port descriptors currently available for applications to control
31 * is obtained by AudioManager.listAudioPorts().
32 * An application can obtain an AudioPortConfig for a valid configuration of this port
33 * by calling AudioPort.buildConfig() and use this configuration
34 * to create a connection between audio sinks and sources with AudioManager.connectAudioPatch()
35 *
36 * @hide
37 */
38public class AudioPort {
39    private static final String TAG = "AudioPort";
40
41    /**
42     * For use by the audio framework.
43     */
44    public static final int ROLE_NONE = 0;
45    /**
46     * The audio port is a source (produces audio)
47     */
48    public static final int ROLE_SOURCE = 1;
49    /**
50     * The audio port is a sink (consumes audio)
51     */
52    public static final int ROLE_SINK = 2;
53
54    /**
55     * audio port type for use by audio framework implementation
56     */
57    public static final int TYPE_NONE = 0;
58    /**
59     */
60    public static final int TYPE_DEVICE = 1;
61    /**
62     */
63    public static final int TYPE_SUBMIX = 2;
64    /**
65     */
66    public static final int TYPE_SESSION = 3;
67
68
69    AudioHandle mHandle;
70    protected final int mRole;
71    private final String mName;
72    private final int[] mSamplingRates;
73    private final int[] mChannelMasks;
74    private final int[] mFormats;
75    private final AudioGain[] mGains;
76    private AudioPortConfig mActiveConfig;
77
78    AudioPort(AudioHandle handle, int role, String name,
79            int[] samplingRates, int[] channelMasks,
80            int[] formats, AudioGain[] gains) {
81
82        mHandle = handle;
83        mRole = role;
84        mName = name;
85        mSamplingRates = samplingRates;
86        mChannelMasks = channelMasks;
87        mFormats = formats;
88        mGains = gains;
89    }
90
91    AudioHandle handle() {
92        return mHandle;
93    }
94
95    /**
96     * Get the system unique device ID.
97     */
98    public int id() {
99        return mHandle.id();
100    }
101
102
103    /**
104     * Get the audio port role
105     */
106    public int role() {
107        return mRole;
108    }
109
110    /**
111     * Get the human-readable name of this port. Perhaps an internal
112     * designation or an physical device.
113     */
114    public String name() {
115        return mName;
116    }
117
118    /**
119     * Get the list of supported sampling rates
120     * Empty array if sampling rate is not relevant for this audio port
121     */
122    public int[] samplingRates() {
123        return mSamplingRates;
124    }
125
126    /**
127     * Get the list of supported channel mask configurations
128     * (e.g AudioFormat.CHANNEL_OUT_STEREO)
129     * Empty array if channel mask is not relevant for this audio port
130     */
131    public int[] channelMasks() {
132        return mChannelMasks;
133    }
134
135    /**
136     * Get the list of supported audio format configurations
137     * (e.g AudioFormat.ENCODING_PCM_16BIT)
138     * Empty array if format is not relevant for this audio port
139     */
140    public int[] formats() {
141        return mFormats;
142    }
143
144    /**
145     * Get the list of gain descriptors
146     * Empty array if this port does not have gain control
147     */
148    public AudioGain[] gains() {
149        return mGains;
150    }
151
152    /**
153     * Get the gain descriptor at a given index
154     */
155    AudioGain gain(int index) {
156        if (index < 0 || index >= mGains.length) {
157            return null;
158        }
159        return mGains[index];
160    }
161
162    /**
163     * Build a specific configuration of this audio port for use by methods
164     * like AudioManager.connectAudioPatch().
165     * @param channelMask The desired channel mask. AudioFormat.CHANNEL_OUT_DEFAULT if no change
166     * from active configuration requested.
167     * @param format The desired audio format. AudioFormat.ENCODING_DEFAULT if no change
168     * from active configuration requested.
169     * @param gain The desired gain. null if no gain changed requested.
170     */
171    public AudioPortConfig buildConfig(int samplingRate, int channelMask, int format,
172                                        AudioGainConfig gain) {
173        return new AudioPortConfig(this, samplingRate, channelMask, format, gain);
174    }
175
176    /**
177     * Get currently active configuration of this audio port.
178     */
179    public AudioPortConfig activeConfig() {
180        return mActiveConfig;
181    }
182
183    @Override
184    public boolean equals(Object o) {
185        if (o == null || !(o instanceof AudioPort)) {
186            return false;
187        }
188        AudioPort ap = (AudioPort)o;
189        return mHandle.equals(ap.handle());
190    }
191
192    @Override
193    public int hashCode() {
194        return mHandle.hashCode();
195    }
196
197    @Override
198    public String toString() {
199        String role = Integer.toString(mRole);
200        switch (mRole) {
201            case ROLE_NONE:
202                role = "NONE";
203                break;
204            case ROLE_SOURCE:
205                role = "SOURCE";
206                break;
207            case ROLE_SINK:
208                role = "SINK";
209                break;
210        }
211        return "{mHandle: " + mHandle
212                + ", mRole: " + role
213                + "}";
214    }
215}
216