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