blob: 630efc166dee8ff16abe938c7d5aa22f88a7ce6a [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
22namespace android {
23
24using StreamTypeVector = std::vector<audio_stream_type_t>;
25
François Gaffiedc7553f2018-11-02 10:39:57 +010026static const audio_attributes_t defaultAttr = AUDIO_ATTRIBUTES_INITIALIZER;
27
28} // namespace android
François Gaffie53615e22015-03-19 09:24:12 +010029
François Gaffie5fcd6f92015-11-27 13:46:12 +010030static const audio_format_t gDynamicFormat = AUDIO_FORMAT_DEFAULT;
François Gaffie5fcd6f92015-11-27 13:46:12 +010031
François Gaffiedc7553f2018-11-02 10:39:57 +010032static const uint32_t SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY = 5000;
33
François Gaffie53615e22015-03-19 09:24:12 +010034// For mixed output and inputs, the policy will use max mixer sampling rates.
35// Do not limit sampling rate otherwise
Glenn Kasten05ddca52016-02-11 08:17:12 -080036#define SAMPLE_RATE_HZ_MAX 192000
37
38// Used when a client opens a capture stream, without specifying a desired sample rate.
39#define SAMPLE_RATE_HZ_DEFAULT 48000
François Gaffie53615e22015-03-19 09:24:12 +010040
41// For mixed output and inputs, the policy will use max mixer channel count.
42// Do not limit channel count otherwise
Glenn Kasten05ddca52016-02-11 08:17:12 -080043#define MAX_MIXER_CHANNEL_COUNT FCC_8
François Gaffie53615e22015-03-19 09:24:12 +010044
45/**
Eric Laurent5a2b6292016-04-14 18:05:57 -070046 * Alias to AUDIO_DEVICE_OUT_DEFAULT defined for clarification when this value is used by volume
47 * control APIs (e.g setStreamVolumeIndex().
48 */
49#define AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME AUDIO_DEVICE_OUT_DEFAULT
50
51
52/**
François Gaffie53615e22015-03-19 09:24:12 +010053 * Check if the state given correspond to an in call state.
54 * @TODO find a better name for widely call state
55 *
56 * @param[in] state to consider
57 *
58 * @return true if given state represents a device in a telephony or VoIP call
59 */
60static inline bool is_state_in_call(int state)
61{
62 return (state == AUDIO_MODE_IN_CALL) || (state == AUDIO_MODE_IN_COMMUNICATION);
63}
64
65/**
jiabin9b307d72019-09-18 15:13:13 -070066 * Check whether the output device type is one
67 * where addresses are used to distinguish between one connected device and another
68 *
69 * @param[in] device to consider
70 *
71 * @return true if the device needs distinguish on address, false otherwise..
72 */
73static inline bool apm_audio_out_device_distinguishes_on_address(audio_devices_t device)
74{
75 return device == AUDIO_DEVICE_OUT_REMOTE_SUBMIX ||
76 device == AUDIO_DEVICE_OUT_BUS;
77}
78
79/**
80 * Check whether the input device type is one
81 * where addresses are used to distinguish between one connected device and another
82 *
83 * @param[in] device to consider
84 *
85 * @return true if the device needs distinguish on address, false otherwise..
86 */
87static inline bool apm_audio_in_device_distinguishes_on_address(audio_devices_t device)
88{
89 return device == AUDIO_DEVICE_IN_REMOTE_SUBMIX ||
90 device == AUDIO_DEVICE_IN_BUS;
91}
92
93/**
François Gaffie53615e22015-03-19 09:24:12 +010094 * Check whether the device type is one
95 * where addresses are used to distinguish between one connected device and another
96 *
97 * @param[in] device to consider
98 *
99 * @return true if the device needs distinguish on address, false otherwise..
100 */
Chih-Hung Hsieh5603d282015-05-04 17:14:15 -0700101static inline bool device_distinguishes_on_address(audio_devices_t device)
François Gaffie53615e22015-03-19 09:24:12 +0100102{
jiabin9b307d72019-09-18 15:13:13 -0700103 return apm_audio_in_device_distinguishes_on_address(device) ||
104 apm_audio_out_device_distinguishes_on_address(device);
François Gaffie53615e22015-03-19 09:24:12 +0100105}
Eric Laurentfb66dd92016-01-28 18:32:03 -0800106
107/**
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800108 * Check whether audio device has encoding capability.
109 *
110 * @param[in] device to consider
111 *
112 * @return true if device has encoding capability, false otherwise..
113 */
114static inline bool device_has_encoding_capability(audio_devices_t device)
115{
jiabin9b307d72019-09-18 15:13:13 -0700116 return audio_is_a2dp_out_device(device);
Aniket Kumar Lata4e464702019-01-10 23:38:46 -0800117}
118
119/**
Eric Laurentfb66dd92016-01-28 18:32:03 -0800120 * Returns the priority of a given audio source for capture. The priority is used when more than one
121 * capture session is active on a given input stream to determine which session drives routing and
122 * effect configuration.
123 *
124 * @param[in] inputSource to consider. Valid sources are:
125 * - AUDIO_SOURCE_VOICE_COMMUNICATION
126 * - AUDIO_SOURCE_CAMCORDER
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800127 * - AUDIO_SOURCE_VOICE_PERFORMANCE
128 * - AUDIO_SOURCE_UNPROCESSED
Eric Laurentfb66dd92016-01-28 18:32:03 -0800129 * - AUDIO_SOURCE_MIC
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800130 * - AUDIO_SOURCE_ECHO_REFERENCE
Eric Laurentfb66dd92016-01-28 18:32:03 -0800131 * - AUDIO_SOURCE_FM_TUNER
132 * - AUDIO_SOURCE_VOICE_RECOGNITION
133 * - AUDIO_SOURCE_HOTWORD
134 *
135 * @return the corresponding input source priority or 0 if priority is irrelevant for this source.
136 * This happens when the specified source cannot share a given input stream (e.g remote submix)
137 * The higher the value, the higher the priority.
138 */
139static inline int32_t source_priority(audio_source_t inputSource)
140{
141 switch (inputSource) {
142 case AUDIO_SOURCE_VOICE_COMMUNICATION:
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800143 return 9;
Eric Laurentfb66dd92016-01-28 18:32:03 -0800144 case AUDIO_SOURCE_CAMCORDER:
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800145 return 8;
146 case AUDIO_SOURCE_VOICE_PERFORMANCE:
147 return 7;
148 case AUDIO_SOURCE_UNPROCESSED:
149 return 6;
Eric Laurentfb66dd92016-01-28 18:32:03 -0800150 case AUDIO_SOURCE_MIC:
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800151 return 5;
152 case AUDIO_SOURCE_ECHO_REFERENCE:
Eric Laurentfb66dd92016-01-28 18:32:03 -0800153 return 4;
154 case AUDIO_SOURCE_FM_TUNER:
155 return 3;
156 case AUDIO_SOURCE_VOICE_RECOGNITION:
157 return 2;
158 case AUDIO_SOURCE_HOTWORD:
159 return 1;
160 default:
161 break;
162 }
163 return 0;
164}
Eric Laurente6930022016-02-11 10:20:40 -0800165
166/* Indicates if audio formats are equivalent when considering a match between
167 * audio HAL supported formats and client requested formats
168 */
169static inline bool audio_formats_match(audio_format_t format1,
170 audio_format_t format2)
171{
172 if (audio_is_linear_pcm(format1) &&
173 (audio_bytes_per_sample(format1) > 2) &&
174 audio_is_linear_pcm(format2) &&
175 (audio_bytes_per_sample(format2) > 2)) {
176 return true;
177 }
178 return format1 == format2;
179}
François Gaffiec005e562018-11-06 15:04:49 +0100180
181/**
182 * @brief hasStream checks if a given stream type is found in the list of streams
183 * @param streams collection of stream types to consider.
184 * @param streamType to consider
185 * @return true if voice stream is found in the given streams, false otherwise
186 */
187static inline bool hasStream(const android::StreamTypeVector &streams,
188 audio_stream_type_t streamType)
189{
190 return std::find(begin(streams), end(streams), streamType) != end(streams);
191}
192
193/**
194 * @brief hasVoiceStream checks if a voice stream is found in the list of streams
195 * @param streams collection to consider.
196 * @return true if voice stream is found in the given streams, false otherwise
197 */
198static inline bool hasVoiceStream(const android::StreamTypeVector &streams)
199{
200 return hasStream(streams, AUDIO_STREAM_VOICE_CALL);
201}