blob: d4992224637445b9e58592ed698c90d3967ad417 [file] [log] [blame]
François Gaffie53615e22015-03-19 09:24:12 +01001/*
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>
jiabin220eea12024-05-17 17:55:20 +000020#include <set>
François Gaffiedc7553f2018-11-02 10:39:57 +010021#include <vector>
22
jiabin43810402019-10-24 14:58:31 -070023#include <media/AudioContainers.h>
24
François Gaffiedc7553f2018-11-02 10:39:57 +010025namespace android {
26
27using StreamTypeVector = std::vector<audio_stream_type_t>;
28
François Gaffiead3dce92024-03-26 17:20:04 +010029/**
30 * Legacy audio policy product strategies IDs. These strategies are supported by the default
31 * policy engine.
Eric Laurent30477832024-10-09 17:31:03 +000032 * IMPORTANT NOTE: the order of this enum is important as it determines the priority
33 * between active strategies for routing decisions: lower enum value => higher prioriy
François Gaffiead3dce92024-03-26 17:20:04 +010034 */
35enum legacy_strategy {
36 STRATEGY_NONE = -1,
François Gaffiead3dce92024-03-26 17:20:04 +010037 STRATEGY_PHONE,
38 STRATEGY_SONIFICATION,
François Gaffiead3dce92024-03-26 17:20:04 +010039 STRATEGY_ENFORCED_AUDIBLE,
François Gaffiead3dce92024-03-26 17:20:04 +010040 STRATEGY_ACCESSIBILITY,
Eric Laurent30477832024-10-09 17:31:03 +000041 STRATEGY_SONIFICATION_RESPECTFUL,
42 STRATEGY_MEDIA,
43 STRATEGY_DTMF,
François Gaffiead3dce92024-03-26 17:20:04 +010044 STRATEGY_CALL_ASSISTANT,
Eric Laurent30477832024-10-09 17:31:03 +000045 STRATEGY_TRANSMITTED_THROUGH_SPEAKER,
46 STRATEGY_REROUTING,
François Gaffiead3dce92024-03-26 17:20:04 +010047 STRATEGY_PATCH,
48};
49
François Gaffiedc7553f2018-11-02 10:39:57 +010050static const audio_attributes_t defaultAttr = AUDIO_ATTRIBUTES_INITIALIZER;
51
jiabin220eea12024-05-17 17:55:20 +000052static const std::set<audio_usage_t > gHighPriorityUseCases = {
53 AUDIO_USAGE_ALARM, AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE
54};
55
François Gaffiedc7553f2018-11-02 10:39:57 +010056} // namespace android
François Gaffie53615e22015-03-19 09:24:12 +010057
François Gaffie5fcd6f92015-11-27 13:46:12 +010058static const audio_format_t gDynamicFormat = AUDIO_FORMAT_DEFAULT;
François Gaffie5fcd6f92015-11-27 13:46:12 +010059
François Gaffiedc7553f2018-11-02 10:39:57 +010060static const uint32_t SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY = 5000;
61
Glenn Kasten05ddca52016-02-11 08:17:12 -080062// Used when a client opens a capture stream, without specifying a desired sample rate.
63#define SAMPLE_RATE_HZ_DEFAULT 48000
François Gaffie53615e22015-03-19 09:24:12 +010064
65// For mixed output and inputs, the policy will use max mixer channel count.
66// Do not limit channel count otherwise
Andy Hung936845a2021-06-08 00:09:06 -070067#define MAX_MIXER_CHANNEL_COUNT FCC_LIMIT
François Gaffie53615e22015-03-19 09:24:12 +010068
69/**
Eric Laurent5a2b6292016-04-14 18:05:57 -070070 * Alias to AUDIO_DEVICE_OUT_DEFAULT defined for clarification when this value is used by volume
71 * control APIs (e.g setStreamVolumeIndex().
72 */
73#define AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME AUDIO_DEVICE_OUT_DEFAULT
74
75
76/**
François Gaffie53615e22015-03-19 09:24:12 +010077 * Check if the state given correspond to an in call state.
78 * @TODO find a better name for widely call state
79 *
80 * @param[in] state to consider
81 *
82 * @return true if given state represents a device in a telephony or VoIP call
83 */
84static inline bool is_state_in_call(int state)
85{
86 return (state == AUDIO_MODE_IN_CALL) || (state == AUDIO_MODE_IN_COMMUNICATION);
87}
88
89/**
jiabinb124ec52019-09-18 15:13:13 -070090 * Check whether the output device type is one
91 * where addresses are used to distinguish between one connected device and another
92 *
93 * @param[in] device to consider
94 *
95 * @return true if the device needs distinguish on address, false otherwise..
96 */
97static inline bool apm_audio_out_device_distinguishes_on_address(audio_devices_t device)
98{
99 return device == AUDIO_DEVICE_OUT_REMOTE_SUBMIX ||
100 device == AUDIO_DEVICE_OUT_BUS;
101}
102
103/**
104 * Check whether the input device type is one
105 * where addresses are used to distinguish between one connected device and another
106 *
107 * @param[in] device to consider
108 *
109 * @return true if the device needs distinguish on address, false otherwise..
110 */
111static inline bool apm_audio_in_device_distinguishes_on_address(audio_devices_t device)
112{
113 return device == AUDIO_DEVICE_IN_REMOTE_SUBMIX ||
114 device == AUDIO_DEVICE_IN_BUS;
115}
116
117/**
François Gaffie53615e22015-03-19 09:24:12 +0100118 * Check whether the device type is one
119 * where addresses are used to distinguish between one connected device and another
120 *
121 * @param[in] device to consider
122 *
123 * @return true if the device needs distinguish on address, false otherwise..
124 */
Chih-Hung Hsieh5603d282015-05-04 17:14:15 -0700125static inline bool device_distinguishes_on_address(audio_devices_t device)
François Gaffie53615e22015-03-19 09:24:12 +0100126{
jiabinb124ec52019-09-18 15:13:13 -0700127 return apm_audio_in_device_distinguishes_on_address(device) ||
128 apm_audio_out_device_distinguishes_on_address(device);
François Gaffie53615e22015-03-19 09:24:12 +0100129}
Eric Laurentfb66dd92016-01-28 18:32:03 -0800130
131/**
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800132 * Check whether audio device has encoding capability.
133 *
134 * @param[in] device to consider
135 *
136 * @return true if device has encoding capability, false otherwise..
137 */
138static inline bool device_has_encoding_capability(audio_devices_t device)
139{
Eric Laurent7e3c0832023-11-30 15:04:50 +0100140 return audio_is_a2dp_out_device(device) || audio_is_ble_out_device(device);
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800141}
142
143/**
Eric Laurentfb66dd92016-01-28 18:32:03 -0800144 * Returns the priority of a given audio source for capture. The priority is used when more than one
145 * capture session is active on a given input stream to determine which session drives routing and
146 * effect configuration.
147 *
148 * @param[in] inputSource to consider. Valid sources are:
149 * - AUDIO_SOURCE_VOICE_COMMUNICATION
150 * - AUDIO_SOURCE_CAMCORDER
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800151 * - AUDIO_SOURCE_VOICE_PERFORMANCE
152 * - AUDIO_SOURCE_UNPROCESSED
Eric Laurentfb66dd92016-01-28 18:32:03 -0800153 * - AUDIO_SOURCE_MIC
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800154 * - AUDIO_SOURCE_ECHO_REFERENCE
Eric Laurentfb66dd92016-01-28 18:32:03 -0800155 * - AUDIO_SOURCE_FM_TUNER
156 * - AUDIO_SOURCE_VOICE_RECOGNITION
157 * - AUDIO_SOURCE_HOTWORD
Carter Hsua3abb402021-10-26 11:11:20 +0800158 * - AUDIO_SOURCE_ULTRASOUND
Eric Laurentfb66dd92016-01-28 18:32:03 -0800159 *
160 * @return the corresponding input source priority or 0 if priority is irrelevant for this source.
161 * This happens when the specified source cannot share a given input stream (e.g remote submix)
162 * The higher the value, the higher the priority.
163 */
164static inline int32_t source_priority(audio_source_t inputSource)
165{
166 switch (inputSource) {
167 case AUDIO_SOURCE_VOICE_COMMUNICATION:
Carter Hsua3abb402021-10-26 11:11:20 +0800168 return 10;
Eric Laurentfb66dd92016-01-28 18:32:03 -0800169 case AUDIO_SOURCE_CAMCORDER:
Carter Hsua3abb402021-10-26 11:11:20 +0800170 return 9;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800171 case AUDIO_SOURCE_VOICE_PERFORMANCE:
Carter Hsua3abb402021-10-26 11:11:20 +0800172 return 8;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800173 case AUDIO_SOURCE_UNPROCESSED:
Carter Hsua3abb402021-10-26 11:11:20 +0800174 return 7;
Eric Laurentfb66dd92016-01-28 18:32:03 -0800175 case AUDIO_SOURCE_MIC:
Carter Hsua3abb402021-10-26 11:11:20 +0800176 return 6;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800177 case AUDIO_SOURCE_ECHO_REFERENCE:
Carter Hsua3abb402021-10-26 11:11:20 +0800178 return 5;
Eric Laurentfb66dd92016-01-28 18:32:03 -0800179 case AUDIO_SOURCE_FM_TUNER:
Carter Hsua3abb402021-10-26 11:11:20 +0800180 return 4;
Eric Laurentfb66dd92016-01-28 18:32:03 -0800181 case AUDIO_SOURCE_VOICE_RECOGNITION:
Carter Hsua3abb402021-10-26 11:11:20 +0800182 return 3;
Eric Laurentfb66dd92016-01-28 18:32:03 -0800183 case AUDIO_SOURCE_HOTWORD:
Carter Hsua3abb402021-10-26 11:11:20 +0800184 return 2;
185 case AUDIO_SOURCE_ULTRASOUND:
Eric Laurentfb66dd92016-01-28 18:32:03 -0800186 return 1;
187 default:
188 break;
189 }
190 return 0;
191}
Eric Laurente6930022016-02-11 10:20:40 -0800192
193/* Indicates if audio formats are equivalent when considering a match between
194 * audio HAL supported formats and client requested formats
195 */
196static inline bool audio_formats_match(audio_format_t format1,
197 audio_format_t format2)
198{
199 if (audio_is_linear_pcm(format1) &&
200 (audio_bytes_per_sample(format1) > 2) &&
201 audio_is_linear_pcm(format2) &&
202 (audio_bytes_per_sample(format2) > 2)) {
203 return true;
204 }
205 return format1 == format2;
206}
François Gaffiec005e562018-11-06 15:04:49 +0100207
208/**
209 * @brief hasStream checks if a given stream type is found in the list of streams
210 * @param streams collection of stream types to consider.
211 * @param streamType to consider
212 * @return true if voice stream is found in the given streams, false otherwise
213 */
214static inline bool hasStream(const android::StreamTypeVector &streams,
215 audio_stream_type_t streamType)
216{
217 return std::find(begin(streams), end(streams), streamType) != end(streams);
218}
219
220/**
221 * @brief hasVoiceStream checks if a voice stream is found in the list of streams
222 * @param streams collection to consider.
223 * @return true if voice stream is found in the given streams, false otherwise
224 */
225static inline bool hasVoiceStream(const android::StreamTypeVector &streams)
226{
227 return hasStream(streams, AUDIO_STREAM_VOICE_CALL);
228}
jiabin43810402019-10-24 14:58:31 -0700229
230/**
231 * @brief extract one device relevant from multiple device selection
232 * @param deviceTypes collection of audio device type
233 * @return the device type that is selected
234 */
235static inline audio_devices_t apm_extract_one_audio_device(
236 const android::DeviceTypeSet& deviceTypes) {
237 if (deviceTypes.empty()) {
238 return AUDIO_DEVICE_NONE;
239 } else if (deviceTypes.size() == 1) {
240 return *(deviceTypes.begin());
241 } else {
242 // Multiple device selection is either:
jiabina35a0402023-04-12 16:35:18 +0000243 // - dock + one other device: give priority to dock in this case.
jiabin43810402019-10-24 14:58:31 -0700244 // - speaker + one other device: give priority to speaker in this case.
245 // - one A2DP device + another device: happens with duplicated output. In this case
246 // retain the device on the A2DP output as the other must not correspond to an active
247 // selection if not the speaker.
248 // - HDMI-CEC system audio mode only output: give priority to available item in order.
jiabina35a0402023-04-12 16:35:18 +0000249 if (deviceTypes.count(AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET) != 0) {
250 return AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
251 } else if (deviceTypes.count(AUDIO_DEVICE_OUT_SPEAKER) != 0) {
jiabin43810402019-10-24 14:58:31 -0700252 return AUDIO_DEVICE_OUT_SPEAKER;
253 } else if (deviceTypes.count(AUDIO_DEVICE_OUT_SPEAKER_SAFE) != 0) {
254 return AUDIO_DEVICE_OUT_SPEAKER_SAFE;
255 } else if (deviceTypes.count(AUDIO_DEVICE_OUT_HDMI_ARC) != 0) {
256 return AUDIO_DEVICE_OUT_HDMI_ARC;
Kuowei Li01a686b2020-10-27 16:54:39 +0800257 } else if (deviceTypes.count(AUDIO_DEVICE_OUT_HDMI_EARC) != 0) {
258 return AUDIO_DEVICE_OUT_HDMI_EARC;
jiabin43810402019-10-24 14:58:31 -0700259 } else if (deviceTypes.count(AUDIO_DEVICE_OUT_AUX_LINE) != 0) {
260 return AUDIO_DEVICE_OUT_AUX_LINE;
261 } else if (deviceTypes.count(AUDIO_DEVICE_OUT_SPDIF) != 0) {
262 return AUDIO_DEVICE_OUT_SPDIF;
263 } else {
264 std::vector<audio_devices_t> a2dpDevices = android::Intersection(
265 deviceTypes, android::getAudioDeviceOutAllA2dpSet());
266 if (a2dpDevices.empty() || a2dpDevices.size() > 1) {
267 ALOGW("%s invalid device combination: %s",
268 __func__, android::dumpDeviceTypes(deviceTypes).c_str());
269 }
270 return a2dpDevices.empty() ? AUDIO_DEVICE_NONE : a2dpDevices[0];
271 }
272 }
Kuowei Li01a686b2020-10-27 16:54:39 +0800273}
jiabina84c3d32022-12-02 18:59:55 +0000274
275/**
276 * Indicates if two given audio output flags are considered as matched, which means that
277 * 1) the `supersetFlags` and `subsetFlags` both contain or both don't contain must match flags and
278 * 2) `supersetFlags` contains all flags from `subsetFlags`.
279 */
280static inline bool audio_output_flags_is_subset(audio_output_flags_t supersetFlags,
281 audio_output_flags_t subsetFlags,
282 uint32_t mustMatchFlags)
283{
284 return ((supersetFlags ^ subsetFlags) & mustMatchFlags) == AUDIO_OUTPUT_FLAG_NONE
285 && (supersetFlags & subsetFlags) == subsetFlags;
286}