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