blob: cf594c6a2e70a7dc395c0591cc6698d95c9431d2 [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>
François Gaffiedc7553f2018-11-02 10:39:57 +010020#include <vector>
21
jiabin43810402019-10-24 14:58:31 -070022#include <media/AudioContainers.h>
23
François Gaffiedc7553f2018-11-02 10:39:57 +010024namespace android {
25
26using StreamTypeVector = std::vector<audio_stream_type_t>;
27
François Gaffiedc7553f2018-11-02 10:39:57 +010028static const audio_attributes_t defaultAttr = AUDIO_ATTRIBUTES_INITIALIZER;
29
30} // namespace android
François Gaffie53615e22015-03-19 09:24:12 +010031
François Gaffie5fcd6f92015-11-27 13:46:12 +010032static const audio_format_t gDynamicFormat = AUDIO_FORMAT_DEFAULT;
François Gaffie5fcd6f92015-11-27 13:46:12 +010033
François Gaffiedc7553f2018-11-02 10:39:57 +010034static const uint32_t SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY = 5000;
35
Glenn Kasten05ddca52016-02-11 08:17:12 -080036// Used when a client opens a capture stream, without specifying a desired sample rate.
37#define SAMPLE_RATE_HZ_DEFAULT 48000
François Gaffie53615e22015-03-19 09:24:12 +010038
39// For mixed output and inputs, the policy will use max mixer channel count.
40// Do not limit channel count otherwise
Andy Hung936845a2021-06-08 00:09:06 -070041#define MAX_MIXER_CHANNEL_COUNT FCC_LIMIT
François Gaffie53615e22015-03-19 09:24:12 +010042
43/**
Eric Laurent5a2b6292016-04-14 18:05:57 -070044 * Alias to AUDIO_DEVICE_OUT_DEFAULT defined for clarification when this value is used by volume
45 * control APIs (e.g setStreamVolumeIndex().
46 */
47#define AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME AUDIO_DEVICE_OUT_DEFAULT
48
49
50/**
François Gaffie53615e22015-03-19 09:24:12 +010051 * Check if the state given correspond to an in call state.
52 * @TODO find a better name for widely call state
53 *
54 * @param[in] state to consider
55 *
56 * @return true if given state represents a device in a telephony or VoIP call
57 */
58static inline bool is_state_in_call(int state)
59{
60 return (state == AUDIO_MODE_IN_CALL) || (state == AUDIO_MODE_IN_COMMUNICATION);
61}
62
63/**
jiabinb124ec52019-09-18 15:13:13 -070064 * Check whether the output device type is one
65 * where addresses are used to distinguish between one connected device and another
66 *
67 * @param[in] device to consider
68 *
69 * @return true if the device needs distinguish on address, false otherwise..
70 */
71static inline bool apm_audio_out_device_distinguishes_on_address(audio_devices_t device)
72{
73 return device == AUDIO_DEVICE_OUT_REMOTE_SUBMIX ||
74 device == AUDIO_DEVICE_OUT_BUS;
75}
76
77/**
78 * Check whether the input device type is one
79 * where addresses are used to distinguish between one connected device and another
80 *
81 * @param[in] device to consider
82 *
83 * @return true if the device needs distinguish on address, false otherwise..
84 */
85static inline bool apm_audio_in_device_distinguishes_on_address(audio_devices_t device)
86{
87 return device == AUDIO_DEVICE_IN_REMOTE_SUBMIX ||
88 device == AUDIO_DEVICE_IN_BUS;
89}
90
91/**
François Gaffie53615e22015-03-19 09:24:12 +010092 * Check whether the device type is one
93 * where addresses are used to distinguish between one connected device and another
94 *
95 * @param[in] device to consider
96 *
97 * @return true if the device needs distinguish on address, false otherwise..
98 */
Chih-Hung Hsieh5603d282015-05-04 17:14:15 -070099static inline bool device_distinguishes_on_address(audio_devices_t device)
François Gaffie53615e22015-03-19 09:24:12 +0100100{
jiabinb124ec52019-09-18 15:13:13 -0700101 return apm_audio_in_device_distinguishes_on_address(device) ||
102 apm_audio_out_device_distinguishes_on_address(device);
François Gaffie53615e22015-03-19 09:24:12 +0100103}
Eric Laurentfb66dd92016-01-28 18:32:03 -0800104
105/**
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800106 * Check whether audio device has encoding capability.
107 *
108 * @param[in] device to consider
109 *
110 * @return true if device has encoding capability, false otherwise..
111 */
112static inline bool device_has_encoding_capability(audio_devices_t device)
113{
Eric Laurent7e3c0832023-11-30 15:04:50 +0100114 return audio_is_a2dp_out_device(device) || audio_is_ble_out_device(device);
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800115}
116
117/**
Eric Laurentfb66dd92016-01-28 18:32:03 -0800118 * Returns the priority of a given audio source for capture. The priority is used when more than one
119 * capture session is active on a given input stream to determine which session drives routing and
120 * effect configuration.
121 *
122 * @param[in] inputSource to consider. Valid sources are:
123 * - AUDIO_SOURCE_VOICE_COMMUNICATION
124 * - AUDIO_SOURCE_CAMCORDER
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800125 * - AUDIO_SOURCE_VOICE_PERFORMANCE
126 * - AUDIO_SOURCE_UNPROCESSED
Eric Laurentfb66dd92016-01-28 18:32:03 -0800127 * - AUDIO_SOURCE_MIC
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800128 * - AUDIO_SOURCE_ECHO_REFERENCE
Eric Laurentfb66dd92016-01-28 18:32:03 -0800129 * - AUDIO_SOURCE_FM_TUNER
130 * - AUDIO_SOURCE_VOICE_RECOGNITION
131 * - AUDIO_SOURCE_HOTWORD
Carter Hsua3abb402021-10-26 11:11:20 +0800132 * - AUDIO_SOURCE_ULTRASOUND
Eric Laurentfb66dd92016-01-28 18:32:03 -0800133 *
134 * @return the corresponding input source priority or 0 if priority is irrelevant for this source.
135 * This happens when the specified source cannot share a given input stream (e.g remote submix)
136 * The higher the value, the higher the priority.
137 */
138static inline int32_t source_priority(audio_source_t inputSource)
139{
140 switch (inputSource) {
141 case AUDIO_SOURCE_VOICE_COMMUNICATION:
Carter Hsua3abb402021-10-26 11:11:20 +0800142 return 10;
Eric Laurentfb66dd92016-01-28 18:32:03 -0800143 case AUDIO_SOURCE_CAMCORDER:
Carter Hsua3abb402021-10-26 11:11:20 +0800144 return 9;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800145 case AUDIO_SOURCE_VOICE_PERFORMANCE:
Carter Hsua3abb402021-10-26 11:11:20 +0800146 return 8;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800147 case AUDIO_SOURCE_UNPROCESSED:
Carter Hsua3abb402021-10-26 11:11:20 +0800148 return 7;
Eric Laurentfb66dd92016-01-28 18:32:03 -0800149 case AUDIO_SOURCE_MIC:
Carter Hsua3abb402021-10-26 11:11:20 +0800150 return 6;
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800151 case AUDIO_SOURCE_ECHO_REFERENCE:
Carter Hsua3abb402021-10-26 11:11:20 +0800152 return 5;
Eric Laurentfb66dd92016-01-28 18:32:03 -0800153 case AUDIO_SOURCE_FM_TUNER:
Carter Hsua3abb402021-10-26 11:11:20 +0800154 return 4;
Eric Laurentfb66dd92016-01-28 18:32:03 -0800155 case AUDIO_SOURCE_VOICE_RECOGNITION:
Carter Hsua3abb402021-10-26 11:11:20 +0800156 return 3;
Eric Laurentfb66dd92016-01-28 18:32:03 -0800157 case AUDIO_SOURCE_HOTWORD:
Carter Hsua3abb402021-10-26 11:11:20 +0800158 return 2;
159 case AUDIO_SOURCE_ULTRASOUND:
Eric Laurentfb66dd92016-01-28 18:32:03 -0800160 return 1;
161 default:
162 break;
163 }
164 return 0;
165}
Eric Laurente6930022016-02-11 10:20:40 -0800166
167/* Indicates if audio formats are equivalent when considering a match between
168 * audio HAL supported formats and client requested formats
169 */
170static inline bool audio_formats_match(audio_format_t format1,
171 audio_format_t format2)
172{
173 if (audio_is_linear_pcm(format1) &&
174 (audio_bytes_per_sample(format1) > 2) &&
175 audio_is_linear_pcm(format2) &&
176 (audio_bytes_per_sample(format2) > 2)) {
177 return true;
178 }
179 return format1 == format2;
180}
François Gaffiec005e562018-11-06 15:04:49 +0100181
182/**
183 * @brief hasStream checks if a given stream type is found in the list of streams
184 * @param streams collection of stream types to consider.
185 * @param streamType to consider
186 * @return true if voice stream is found in the given streams, false otherwise
187 */
188static inline bool hasStream(const android::StreamTypeVector &streams,
189 audio_stream_type_t streamType)
190{
191 return std::find(begin(streams), end(streams), streamType) != end(streams);
192}
193
194/**
195 * @brief hasVoiceStream checks if a voice stream is found in the list of streams
196 * @param streams collection to consider.
197 * @return true if voice stream is found in the given streams, false otherwise
198 */
199static inline bool hasVoiceStream(const android::StreamTypeVector &streams)
200{
201 return hasStream(streams, AUDIO_STREAM_VOICE_CALL);
202}
jiabin43810402019-10-24 14:58:31 -0700203
204/**
205 * @brief extract one device relevant from multiple device selection
206 * @param deviceTypes collection of audio device type
207 * @return the device type that is selected
208 */
209static inline audio_devices_t apm_extract_one_audio_device(
210 const android::DeviceTypeSet& deviceTypes) {
211 if (deviceTypes.empty()) {
212 return AUDIO_DEVICE_NONE;
213 } else if (deviceTypes.size() == 1) {
214 return *(deviceTypes.begin());
215 } else {
216 // Multiple device selection is either:
jiabina35a0402023-04-12 16:35:18 +0000217 // - dock + one other device: give priority to dock in this case.
jiabin43810402019-10-24 14:58:31 -0700218 // - speaker + one other device: give priority to speaker in this case.
219 // - one A2DP device + another device: happens with duplicated output. In this case
220 // retain the device on the A2DP output as the other must not correspond to an active
221 // selection if not the speaker.
222 // - HDMI-CEC system audio mode only output: give priority to available item in order.
jiabina35a0402023-04-12 16:35:18 +0000223 if (deviceTypes.count(AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET) != 0) {
224 return AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
225 } else if (deviceTypes.count(AUDIO_DEVICE_OUT_SPEAKER) != 0) {
jiabin43810402019-10-24 14:58:31 -0700226 return AUDIO_DEVICE_OUT_SPEAKER;
227 } else if (deviceTypes.count(AUDIO_DEVICE_OUT_SPEAKER_SAFE) != 0) {
228 return AUDIO_DEVICE_OUT_SPEAKER_SAFE;
229 } else if (deviceTypes.count(AUDIO_DEVICE_OUT_HDMI_ARC) != 0) {
230 return AUDIO_DEVICE_OUT_HDMI_ARC;
Kuowei Li01a686b2020-10-27 16:54:39 +0800231 } else if (deviceTypes.count(AUDIO_DEVICE_OUT_HDMI_EARC) != 0) {
232 return AUDIO_DEVICE_OUT_HDMI_EARC;
jiabin43810402019-10-24 14:58:31 -0700233 } else if (deviceTypes.count(AUDIO_DEVICE_OUT_AUX_LINE) != 0) {
234 return AUDIO_DEVICE_OUT_AUX_LINE;
235 } else if (deviceTypes.count(AUDIO_DEVICE_OUT_SPDIF) != 0) {
236 return AUDIO_DEVICE_OUT_SPDIF;
237 } else {
238 std::vector<audio_devices_t> a2dpDevices = android::Intersection(
239 deviceTypes, android::getAudioDeviceOutAllA2dpSet());
240 if (a2dpDevices.empty() || a2dpDevices.size() > 1) {
241 ALOGW("%s invalid device combination: %s",
242 __func__, android::dumpDeviceTypes(deviceTypes).c_str());
243 }
244 return a2dpDevices.empty() ? AUDIO_DEVICE_NONE : a2dpDevices[0];
245 }
246 }
Kuowei Li01a686b2020-10-27 16:54:39 +0800247}
jiabina84c3d32022-12-02 18:59:55 +0000248
249/**
250 * Indicates if two given audio output flags are considered as matched, which means that
251 * 1) the `supersetFlags` and `subsetFlags` both contain or both don't contain must match flags and
252 * 2) `supersetFlags` contains all flags from `subsetFlags`.
253 */
254static inline bool audio_output_flags_is_subset(audio_output_flags_t supersetFlags,
255 audio_output_flags_t subsetFlags,
256 uint32_t mustMatchFlags)
257{
258 return ((supersetFlags ^ subsetFlags) & mustMatchFlags) == AUDIO_OUTPUT_FLAG_NONE
259 && (supersetFlags & subsetFlags) == subsetFlags;
260}