blob: 0537365ae2a877c0622f9bd1da28a26454bec7e7 [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
jiabinc0106832019-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
François Gaffie53615e22015-03-19 09:24:12 +010036// For mixed output and inputs, the policy will use max mixer sampling rates.
37// Do not limit sampling rate otherwise
Glenn Kasten05ddca52016-02-11 08:17:12 -080038#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
François Gaffie53615e22015-03-19 09:24:12 +010042
43// For mixed output and inputs, the policy will use max mixer channel count.
44// Do not limit channel count otherwise
Glenn Kasten05ddca52016-02-11 08:17:12 -080045#define MAX_MIXER_CHANNEL_COUNT FCC_8
François Gaffie53615e22015-03-19 09:24:12 +010046
47/**
Eric Laurent5a2b6292016-04-14 18:05:57 -070048 * 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/**
François Gaffie53615e22015-03-19 09:24:12 +010055 * 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 */
62static inline bool is_state_in_call(int state)
63{
64 return (state == AUDIO_MODE_IN_CALL) || (state == AUDIO_MODE_IN_COMMUNICATION);
65}
66
67/**
jiabin9b307d72019-09-18 15:13:13 -070068 * 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 */
75static 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 */
89static 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/**
François Gaffie53615e22015-03-19 09:24:12 +010096 * 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 */
Chih-Hung Hsieh5603d282015-05-04 17:14:15 -0700103static inline bool device_distinguishes_on_address(audio_devices_t device)
François Gaffie53615e22015-03-19 09:24:12 +0100104{
jiabin9b307d72019-09-18 15:13:13 -0700105 return apm_audio_in_device_distinguishes_on_address(device) ||
106 apm_audio_out_device_distinguishes_on_address(device);
François Gaffie53615e22015-03-19 09:24:12 +0100107}
Eric Laurentfb66dd92016-01-28 18:32:03 -0800108
109/**
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800110 * 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 */
116static inline bool device_has_encoding_capability(audio_devices_t device)
117{
jiabin9b307d72019-09-18 15:13:13 -0700118 return audio_is_a2dp_out_device(device);
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800119}
120
121/**
Eric Laurentfb66dd92016-01-28 18:32:03 -0800122 * 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
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800129 * - AUDIO_SOURCE_VOICE_PERFORMANCE
130 * - AUDIO_SOURCE_UNPROCESSED
Eric Laurentfb66dd92016-01-28 18:32:03 -0800131 * - AUDIO_SOURCE_MIC
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800132 * - AUDIO_SOURCE_ECHO_REFERENCE
Eric Laurentfb66dd92016-01-28 18:32:03 -0800133 * - AUDIO_SOURCE_FM_TUNER
134 * - AUDIO_SOURCE_VOICE_RECOGNITION
135 * - AUDIO_SOURCE_HOTWORD
136 *
137 * @return the corresponding input source priority or 0 if priority is irrelevant for this source.
138 * This happens when the specified source cannot share a given input stream (e.g remote submix)
139 * The higher the value, the higher the priority.
140 */
141static inline int32_t source_priority(audio_source_t inputSource)
142{
143 switch (inputSource) {
144 case AUDIO_SOURCE_VOICE_COMMUNICATION:
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800145 return 9;
Eric Laurentfb66dd92016-01-28 18:32:03 -0800146 case AUDIO_SOURCE_CAMCORDER:
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800147 return 8;
148 case AUDIO_SOURCE_VOICE_PERFORMANCE:
149 return 7;
150 case AUDIO_SOURCE_UNPROCESSED:
151 return 6;
Eric Laurentfb66dd92016-01-28 18:32:03 -0800152 case AUDIO_SOURCE_MIC:
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800153 return 5;
154 case AUDIO_SOURCE_ECHO_REFERENCE:
Eric Laurentfb66dd92016-01-28 18:32:03 -0800155 return 4;
156 case AUDIO_SOURCE_FM_TUNER:
157 return 3;
158 case AUDIO_SOURCE_VOICE_RECOGNITION:
159 return 2;
160 case AUDIO_SOURCE_HOTWORD:
161 return 1;
162 default:
163 break;
164 }
165 return 0;
166}
Eric Laurente6930022016-02-11 10:20:40 -0800167
168/* Indicates if audio formats are equivalent when considering a match between
169 * audio HAL supported formats and client requested formats
170 */
171static inline bool audio_formats_match(audio_format_t format1,
172 audio_format_t format2)
173{
174 if (audio_is_linear_pcm(format1) &&
175 (audio_bytes_per_sample(format1) > 2) &&
176 audio_is_linear_pcm(format2) &&
177 (audio_bytes_per_sample(format2) > 2)) {
178 return true;
179 }
180 return format1 == format2;
181}
François Gaffiec005e562018-11-06 15:04:49 +0100182
183/**
184 * @brief hasStream checks if a given stream type is found in the list of streams
185 * @param streams collection of stream types to consider.
186 * @param streamType to consider
187 * @return true if voice stream is found in the given streams, false otherwise
188 */
189static inline bool hasStream(const android::StreamTypeVector &streams,
190 audio_stream_type_t streamType)
191{
192 return std::find(begin(streams), end(streams), streamType) != end(streams);
193}
194
195/**
196 * @brief hasVoiceStream checks if a voice stream is found in the list of streams
197 * @param streams collection to consider.
198 * @return true if voice stream is found in the given streams, false otherwise
199 */
200static inline bool hasVoiceStream(const android::StreamTypeVector &streams)
201{
202 return hasStream(streams, AUDIO_STREAM_VOICE_CALL);
203}
jiabinc0106832019-10-24 14:58:31 -0700204
205/**
206 * @brief extract one device relevant from multiple device selection
207 * @param deviceTypes collection of audio device type
208 * @return the device type that is selected
209 */
210static inline audio_devices_t apm_extract_one_audio_device(
211 const android::DeviceTypeSet& deviceTypes) {
212 if (deviceTypes.empty()) {
213 return AUDIO_DEVICE_NONE;
214 } else if (deviceTypes.size() == 1) {
215 return *(deviceTypes.begin());
216 } else {
217 // Multiple device selection is either:
218 // - 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.
223 if (deviceTypes.count(AUDIO_DEVICE_OUT_SPEAKER) != 0) {
224 return AUDIO_DEVICE_OUT_SPEAKER;
225 } else if (deviceTypes.count(AUDIO_DEVICE_OUT_SPEAKER_SAFE) != 0) {
226 return AUDIO_DEVICE_OUT_SPEAKER_SAFE;
227 } else if (deviceTypes.count(AUDIO_DEVICE_OUT_HDMI_ARC) != 0) {
228 return AUDIO_DEVICE_OUT_HDMI_ARC;
229 } else if (deviceTypes.count(AUDIO_DEVICE_OUT_AUX_LINE) != 0) {
230 return AUDIO_DEVICE_OUT_AUX_LINE;
231 } else if (deviceTypes.count(AUDIO_DEVICE_OUT_SPDIF) != 0) {
232 return AUDIO_DEVICE_OUT_SPDIF;
233 } else {
234 std::vector<audio_devices_t> a2dpDevices = android::Intersection(
235 deviceTypes, android::getAudioDeviceOutAllA2dpSet());
236 if (a2dpDevices.empty() || a2dpDevices.size() > 1) {
237 ALOGW("%s invalid device combination: %s",
238 __func__, android::dumpDeviceTypes(deviceTypes).c_str());
239 }
240 return a2dpDevices.empty() ? AUDIO_DEVICE_NONE : a2dpDevices[0];
241 }
242 }
243}