blob: 605fc1cda162c039ca00cf4a94908c6f8f7fcae7 [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/**
François Gaffie53615e22015-03-19 09:24:12 +010046 * A device mask for all audio input and output devices where matching inputs/outputs on device
47 * type alone is not enough: the address must match too
48 */
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -080049#define APM_AUDIO_DEVICE_OUT_MATCH_ADDRESS_ALL (AUDIO_DEVICE_OUT_REMOTE_SUBMIX|AUDIO_DEVICE_OUT_BUS)
Eric Laurent7c1ec5f2015-07-09 14:52:47 -070050
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -080051#define APM_AUDIO_DEVICE_IN_MATCH_ADDRESS_ALL (AUDIO_DEVICE_IN_REMOTE_SUBMIX|AUDIO_DEVICE_IN_BUS)
François Gaffie53615e22015-03-19 09:24:12 +010052
53/**
Eric Laurent5a2b6292016-04-14 18:05:57 -070054 * Alias to AUDIO_DEVICE_OUT_DEFAULT defined for clarification when this value is used by volume
55 * control APIs (e.g setStreamVolumeIndex().
56 */
57#define AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME AUDIO_DEVICE_OUT_DEFAULT
58
59
60/**
François Gaffie53615e22015-03-19 09:24:12 +010061 * Check if the state given correspond to an in call state.
62 * @TODO find a better name for widely call state
63 *
64 * @param[in] state to consider
65 *
66 * @return true if given state represents a device in a telephony or VoIP call
67 */
68static inline bool is_state_in_call(int state)
69{
70 return (state == AUDIO_MODE_IN_CALL) || (state == AUDIO_MODE_IN_COMMUNICATION);
71}
72
73/**
François Gaffie53615e22015-03-19 09:24:12 +010074 * Check whether the device type is one
75 * where addresses are used to distinguish between one connected device and another
76 *
77 * @param[in] device to consider
78 *
79 * @return true if the device needs distinguish on address, false otherwise..
80 */
Chih-Hung Hsieh5603d282015-05-04 17:14:15 -070081static inline bool device_distinguishes_on_address(audio_devices_t device)
François Gaffie53615e22015-03-19 09:24:12 +010082{
Eric Laurent7c1ec5f2015-07-09 14:52:47 -070083 return (((device & AUDIO_DEVICE_BIT_IN) != 0) &&
84 ((~AUDIO_DEVICE_BIT_IN & device & APM_AUDIO_DEVICE_IN_MATCH_ADDRESS_ALL) != 0)) ||
85 (((device & AUDIO_DEVICE_BIT_IN) == 0) &&
86 ((device & APM_AUDIO_DEVICE_OUT_MATCH_ADDRESS_ALL) != 0));
François Gaffie53615e22015-03-19 09:24:12 +010087}
Eric Laurentfb66dd92016-01-28 18:32:03 -080088
89/**
Aniket Kumar Lata4e464702019-01-10 23:38:46 -080090 * Check whether audio device has encoding capability.
91 *
92 * @param[in] device to consider
93 *
94 * @return true if device has encoding capability, false otherwise..
95 */
96static inline bool device_has_encoding_capability(audio_devices_t device)
97{
98 if (device & AUDIO_DEVICE_OUT_ALL_A2DP) {
99 return true;
100 }
101 return false;
102}
103
104/**
Eric Laurentfb66dd92016-01-28 18:32:03 -0800105 * Returns the priority of a given audio source for capture. The priority is used when more than one
106 * capture session is active on a given input stream to determine which session drives routing and
107 * effect configuration.
108 *
109 * @param[in] inputSource to consider. Valid sources are:
110 * - AUDIO_SOURCE_VOICE_COMMUNICATION
111 * - AUDIO_SOURCE_CAMCORDER
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800112 * - AUDIO_SOURCE_VOICE_PERFORMANCE
113 * - AUDIO_SOURCE_UNPROCESSED
Eric Laurentfb66dd92016-01-28 18:32:03 -0800114 * - AUDIO_SOURCE_MIC
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800115 * - AUDIO_SOURCE_ECHO_REFERENCE
Eric Laurentfb66dd92016-01-28 18:32:03 -0800116 * - AUDIO_SOURCE_FM_TUNER
117 * - AUDIO_SOURCE_VOICE_RECOGNITION
118 * - AUDIO_SOURCE_HOTWORD
119 *
120 * @return the corresponding input source priority or 0 if priority is irrelevant for this source.
121 * This happens when the specified source cannot share a given input stream (e.g remote submix)
122 * The higher the value, the higher the priority.
123 */
124static inline int32_t source_priority(audio_source_t inputSource)
125{
126 switch (inputSource) {
127 case AUDIO_SOURCE_VOICE_COMMUNICATION:
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800128 return 9;
Eric Laurentfb66dd92016-01-28 18:32:03 -0800129 case AUDIO_SOURCE_CAMCORDER:
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800130 return 8;
131 case AUDIO_SOURCE_VOICE_PERFORMANCE:
132 return 7;
133 case AUDIO_SOURCE_UNPROCESSED:
134 return 6;
Eric Laurentfb66dd92016-01-28 18:32:03 -0800135 case AUDIO_SOURCE_MIC:
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800136 return 5;
137 case AUDIO_SOURCE_ECHO_REFERENCE:
Eric Laurentfb66dd92016-01-28 18:32:03 -0800138 return 4;
139 case AUDIO_SOURCE_FM_TUNER:
140 return 3;
141 case AUDIO_SOURCE_VOICE_RECOGNITION:
142 return 2;
143 case AUDIO_SOURCE_HOTWORD:
144 return 1;
145 default:
146 break;
147 }
148 return 0;
149}
Eric Laurente6930022016-02-11 10:20:40 -0800150
151/* Indicates if audio formats are equivalent when considering a match between
152 * audio HAL supported formats and client requested formats
153 */
154static inline bool audio_formats_match(audio_format_t format1,
155 audio_format_t format2)
156{
157 if (audio_is_linear_pcm(format1) &&
158 (audio_bytes_per_sample(format1) > 2) &&
159 audio_is_linear_pcm(format2) &&
160 (audio_bytes_per_sample(format2) > 2)) {
161 return true;
162 }
163 return format1 == format2;
164}
François Gaffiec005e562018-11-06 15:04:49 +0100165
166/**
167 * @brief hasStream checks if a given stream type is found in the list of streams
168 * @param streams collection of stream types to consider.
169 * @param streamType to consider
170 * @return true if voice stream is found in the given streams, false otherwise
171 */
172static inline bool hasStream(const android::StreamTypeVector &streams,
173 audio_stream_type_t streamType)
174{
175 return std::find(begin(streams), end(streams), streamType) != end(streams);
176}
177
178/**
179 * @brief hasVoiceStream checks if a voice stream is found in the list of streams
180 * @param streams collection to consider.
181 * @return true if voice stream is found in the given streams, false otherwise
182 */
183static inline bool hasVoiceStream(const android::StreamTypeVector &streams)
184{
185 return hasStream(streams, AUDIO_STREAM_VOICE_CALL);
186}