1 /*
2 * Copyright (C) 2015 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 #pragma once
18
19 #include <system/audio.h>
20 #include <vector>
21
22 #include <media/AudioContainers.h>
23
24 namespace android {
25
26 using StreamTypeVector = std::vector<audio_stream_type_t>;
27
28 static const audio_attributes_t defaultAttr = AUDIO_ATTRIBUTES_INITIALIZER;
29
30 } // namespace android
31
32 static const audio_format_t gDynamicFormat = AUDIO_FORMAT_DEFAULT;
33
34 static const uint32_t SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY = 5000;
35
36 // For mixed output and inputs, the policy will use max mixer sampling rates.
37 // Do not limit sampling rate otherwise
38 #define SAMPLE_RATE_HZ_MAX 192000
39
40 // Used when a client opens a capture stream, without specifying a desired sample rate.
41 #define SAMPLE_RATE_HZ_DEFAULT 48000
42
43 // For mixed output and inputs, the policy will use max mixer channel count.
44 // Do not limit channel count otherwise
45 #define MAX_MIXER_CHANNEL_COUNT FCC_LIMIT
46
47 /**
48 * Alias to AUDIO_DEVICE_OUT_DEFAULT defined for clarification when this value is used by volume
49 * control APIs (e.g setStreamVolumeIndex().
50 */
51 #define AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME AUDIO_DEVICE_OUT_DEFAULT
52
53
54 /**
55 * Check if the state given correspond to an in call state.
56 * @TODO find a better name for widely call state
57 *
58 * @param[in] state to consider
59 *
60 * @return true if given state represents a device in a telephony or VoIP call
61 */
is_state_in_call(int state)62 static inline bool is_state_in_call(int state)
63 {
64 return (state == AUDIO_MODE_IN_CALL) || (state == AUDIO_MODE_IN_COMMUNICATION);
65 }
66
67 /**
68 * Check whether the output device type is one
69 * where addresses are used to distinguish between one connected device and another
70 *
71 * @param[in] device to consider
72 *
73 * @return true if the device needs distinguish on address, false otherwise..
74 */
apm_audio_out_device_distinguishes_on_address(audio_devices_t device)75 static inline bool apm_audio_out_device_distinguishes_on_address(audio_devices_t device)
76 {
77 return device == AUDIO_DEVICE_OUT_REMOTE_SUBMIX ||
78 device == AUDIO_DEVICE_OUT_BUS;
79 }
80
81 /**
82 * Check whether the input device type is one
83 * where addresses are used to distinguish between one connected device and another
84 *
85 * @param[in] device to consider
86 *
87 * @return true if the device needs distinguish on address, false otherwise..
88 */
apm_audio_in_device_distinguishes_on_address(audio_devices_t device)89 static inline bool apm_audio_in_device_distinguishes_on_address(audio_devices_t device)
90 {
91 return device == AUDIO_DEVICE_IN_REMOTE_SUBMIX ||
92 device == AUDIO_DEVICE_IN_BUS;
93 }
94
95 /**
96 * Check whether the device type is one
97 * where addresses are used to distinguish between one connected device and another
98 *
99 * @param[in] device to consider
100 *
101 * @return true if the device needs distinguish on address, false otherwise..
102 */
device_distinguishes_on_address(audio_devices_t device)103 static inline bool device_distinguishes_on_address(audio_devices_t device)
104 {
105 return apm_audio_in_device_distinguishes_on_address(device) ||
106 apm_audio_out_device_distinguishes_on_address(device);
107 }
108
109 /**
110 * Check whether audio device has encoding capability.
111 *
112 * @param[in] device to consider
113 *
114 * @return true if device has encoding capability, false otherwise..
115 */
device_has_encoding_capability(audio_devices_t device)116 static inline bool device_has_encoding_capability(audio_devices_t device)
117 {
118 return audio_is_a2dp_out_device(device);
119 }
120
121 /**
122 * Returns the priority of a given audio source for capture. The priority is used when more than one
123 * capture session is active on a given input stream to determine which session drives routing and
124 * effect configuration.
125 *
126 * @param[in] inputSource to consider. Valid sources are:
127 * - AUDIO_SOURCE_VOICE_COMMUNICATION
128 * - AUDIO_SOURCE_CAMCORDER
129 * - AUDIO_SOURCE_VOICE_PERFORMANCE
130 * - AUDIO_SOURCE_UNPROCESSED
131 * - AUDIO_SOURCE_MIC
132 * - AUDIO_SOURCE_ECHO_REFERENCE
133 * - AUDIO_SOURCE_FM_TUNER
134 * - AUDIO_SOURCE_VOICE_RECOGNITION
135 * - AUDIO_SOURCE_HOTWORD
136 * - AUDIO_SOURCE_ULTRASOUND
137 *
138 * @return the corresponding input source priority or 0 if priority is irrelevant for this source.
139 * This happens when the specified source cannot share a given input stream (e.g remote submix)
140 * The higher the value, the higher the priority.
141 */
source_priority(audio_source_t inputSource)142 static inline int32_t source_priority(audio_source_t inputSource)
143 {
144 switch (inputSource) {
145 case AUDIO_SOURCE_VOICE_COMMUNICATION:
146 return 10;
147 case AUDIO_SOURCE_CAMCORDER:
148 return 9;
149 case AUDIO_SOURCE_VOICE_PERFORMANCE:
150 return 8;
151 case AUDIO_SOURCE_UNPROCESSED:
152 return 7;
153 case AUDIO_SOURCE_MIC:
154 return 6;
155 case AUDIO_SOURCE_ECHO_REFERENCE:
156 return 5;
157 case AUDIO_SOURCE_FM_TUNER:
158 return 4;
159 case AUDIO_SOURCE_VOICE_RECOGNITION:
160 return 3;
161 case AUDIO_SOURCE_HOTWORD:
162 return 2;
163 case AUDIO_SOURCE_ULTRASOUND:
164 return 1;
165 default:
166 break;
167 }
168 return 0;
169 }
170
171 /* Indicates if audio formats are equivalent when considering a match between
172 * audio HAL supported formats and client requested formats
173 */
audio_formats_match(audio_format_t format1,audio_format_t format2)174 static inline bool audio_formats_match(audio_format_t format1,
175 audio_format_t format2)
176 {
177 if (audio_is_linear_pcm(format1) &&
178 (audio_bytes_per_sample(format1) > 2) &&
179 audio_is_linear_pcm(format2) &&
180 (audio_bytes_per_sample(format2) > 2)) {
181 return true;
182 }
183 return format1 == format2;
184 }
185
186 /**
187 * @brief hasStream checks if a given stream type is found in the list of streams
188 * @param streams collection of stream types to consider.
189 * @param streamType to consider
190 * @return true if voice stream is found in the given streams, false otherwise
191 */
hasStream(const android::StreamTypeVector & streams,audio_stream_type_t streamType)192 static inline bool hasStream(const android::StreamTypeVector &streams,
193 audio_stream_type_t streamType)
194 {
195 return std::find(begin(streams), end(streams), streamType) != end(streams);
196 }
197
198 /**
199 * @brief hasVoiceStream checks if a voice stream is found in the list of streams
200 * @param streams collection to consider.
201 * @return true if voice stream is found in the given streams, false otherwise
202 */
hasVoiceStream(const android::StreamTypeVector & streams)203 static inline bool hasVoiceStream(const android::StreamTypeVector &streams)
204 {
205 return hasStream(streams, AUDIO_STREAM_VOICE_CALL);
206 }
207
208 /**
209 * @brief extract one device relevant from multiple device selection
210 * @param deviceTypes collection of audio device type
211 * @return the device type that is selected
212 */
apm_extract_one_audio_device(const android::DeviceTypeSet & deviceTypes)213 static inline audio_devices_t apm_extract_one_audio_device(
214 const android::DeviceTypeSet& deviceTypes) {
215 if (deviceTypes.empty()) {
216 return AUDIO_DEVICE_NONE;
217 } else if (deviceTypes.size() == 1) {
218 return *(deviceTypes.begin());
219 } else {
220 // Multiple device selection is either:
221 // - speaker + one other device: give priority to speaker in this case.
222 // - one A2DP device + another device: happens with duplicated output. In this case
223 // retain the device on the A2DP output as the other must not correspond to an active
224 // selection if not the speaker.
225 // - HDMI-CEC system audio mode only output: give priority to available item in order.
226 if (deviceTypes.count(AUDIO_DEVICE_OUT_SPEAKER) != 0) {
227 return AUDIO_DEVICE_OUT_SPEAKER;
228 } else if (deviceTypes.count(AUDIO_DEVICE_OUT_SPEAKER_SAFE) != 0) {
229 return AUDIO_DEVICE_OUT_SPEAKER_SAFE;
230 } else if (deviceTypes.count(AUDIO_DEVICE_OUT_HDMI_ARC) != 0) {
231 return AUDIO_DEVICE_OUT_HDMI_ARC;
232 } else if (deviceTypes.count(AUDIO_DEVICE_OUT_HDMI_EARC) != 0) {
233 return AUDIO_DEVICE_OUT_HDMI_EARC;
234 } else if (deviceTypes.count(AUDIO_DEVICE_OUT_AUX_LINE) != 0) {
235 return AUDIO_DEVICE_OUT_AUX_LINE;
236 } else if (deviceTypes.count(AUDIO_DEVICE_OUT_SPDIF) != 0) {
237 return AUDIO_DEVICE_OUT_SPDIF;
238 } else {
239 std::vector<audio_devices_t> a2dpDevices = android::Intersection(
240 deviceTypes, android::getAudioDeviceOutAllA2dpSet());
241 if (a2dpDevices.empty() || a2dpDevices.size() > 1) {
242 ALOGW("%s invalid device combination: %s",
243 __func__, android::dumpDeviceTypes(deviceTypes).c_str());
244 }
245 return a2dpDevices.empty() ? AUDIO_DEVICE_NONE : a2dpDevices[0];
246 }
247 }
248 }
249