blob: 04f1eb2b469201ddecb42f32ab3e3ab0f9e76622 [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
26
27static const audio_attributes_t defaultAttr = AUDIO_ATTRIBUTES_INITIALIZER;
28
29} // namespace android
François Gaffie53615e22015-03-19 09:24:12 +010030
François Gaffie5fcd6f92015-11-27 13:46:12 +010031static const audio_format_t gDynamicFormat = AUDIO_FORMAT_DEFAULT;
François Gaffie5fcd6f92015-11-27 13:46:12 +010032
François Gaffiedc7553f2018-11-02 10:39:57 +010033static const uint32_t SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY = 5000;
34
François Gaffie53615e22015-03-19 09:24:12 +010035// For mixed output and inputs, the policy will use max mixer sampling rates.
36// Do not limit sampling rate otherwise
Glenn Kasten05ddca52016-02-11 08:17:12 -080037#define SAMPLE_RATE_HZ_MAX 192000
38
39// Used when a client opens a capture stream, without specifying a desired sample rate.
40#define SAMPLE_RATE_HZ_DEFAULT 48000
François Gaffie53615e22015-03-19 09:24:12 +010041
42// For mixed output and inputs, the policy will use max mixer channel count.
43// Do not limit channel count otherwise
Glenn Kasten05ddca52016-02-11 08:17:12 -080044#define MAX_MIXER_CHANNEL_COUNT FCC_8
François Gaffie53615e22015-03-19 09:24:12 +010045
46/**
François Gaffie53615e22015-03-19 09:24:12 +010047 * A device mask for all audio input and output devices where matching inputs/outputs on device
48 * type alone is not enough: the address must match too
49 */
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -080050#define APM_AUDIO_DEVICE_OUT_MATCH_ADDRESS_ALL (AUDIO_DEVICE_OUT_REMOTE_SUBMIX|AUDIO_DEVICE_OUT_BUS)
Eric Laurent7c1ec5f2015-07-09 14:52:47 -070051
Jean-Michel Trivi7638ca22016-03-04 17:42:44 -080052#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 +010053
54/**
Eric Laurent5a2b6292016-04-14 18:05:57 -070055 * Alias to AUDIO_DEVICE_OUT_DEFAULT defined for clarification when this value is used by volume
56 * control APIs (e.g setStreamVolumeIndex().
57 */
58#define AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME AUDIO_DEVICE_OUT_DEFAULT
59
60
61/**
François Gaffie53615e22015-03-19 09:24:12 +010062 * Check if the state given correspond to an in call state.
63 * @TODO find a better name for widely call state
64 *
65 * @param[in] state to consider
66 *
67 * @return true if given state represents a device in a telephony or VoIP call
68 */
69static inline bool is_state_in_call(int state)
70{
71 return (state == AUDIO_MODE_IN_CALL) || (state == AUDIO_MODE_IN_COMMUNICATION);
72}
73
74/**
François Gaffie53615e22015-03-19 09:24:12 +010075 * Check whether the device type is one
76 * where addresses are used to distinguish between one connected device and another
77 *
78 * @param[in] device to consider
79 *
80 * @return true if the device needs distinguish on address, false otherwise..
81 */
Chih-Hung Hsieh5603d282015-05-04 17:14:15 -070082static inline bool device_distinguishes_on_address(audio_devices_t device)
François Gaffie53615e22015-03-19 09:24:12 +010083{
Eric Laurent7c1ec5f2015-07-09 14:52:47 -070084 return (((device & AUDIO_DEVICE_BIT_IN) != 0) &&
85 ((~AUDIO_DEVICE_BIT_IN & device & APM_AUDIO_DEVICE_IN_MATCH_ADDRESS_ALL) != 0)) ||
86 (((device & AUDIO_DEVICE_BIT_IN) == 0) &&
87 ((device & APM_AUDIO_DEVICE_OUT_MATCH_ADDRESS_ALL) != 0));
François Gaffie53615e22015-03-19 09:24:12 +010088}
Eric Laurentfb66dd92016-01-28 18:32:03 -080089
90/**
Aniket Kumar Lata4e464702019-01-10 23:38:46 -080091 * Check whether audio device has encoding capability.
92 *
93 * @param[in] device to consider
94 *
95 * @return true if device has encoding capability, false otherwise..
96 */
97static inline bool device_has_encoding_capability(audio_devices_t device)
98{
99 if (device & AUDIO_DEVICE_OUT_ALL_A2DP) {
100 return true;
101 }
102 return false;
103}
104
105/**
Eric Laurentfb66dd92016-01-28 18:32:03 -0800106 * Returns the priority of a given audio source for capture. The priority is used when more than one
107 * capture session is active on a given input stream to determine which session drives routing and
108 * effect configuration.
109 *
110 * @param[in] inputSource to consider. Valid sources are:
111 * - AUDIO_SOURCE_VOICE_COMMUNICATION
112 * - AUDIO_SOURCE_CAMCORDER
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800113 * - AUDIO_SOURCE_VOICE_PERFORMANCE
114 * - AUDIO_SOURCE_UNPROCESSED
Eric Laurentfb66dd92016-01-28 18:32:03 -0800115 * - AUDIO_SOURCE_MIC
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800116 * - AUDIO_SOURCE_ECHO_REFERENCE
Eric Laurentfb66dd92016-01-28 18:32:03 -0800117 * - AUDIO_SOURCE_FM_TUNER
118 * - AUDIO_SOURCE_VOICE_RECOGNITION
119 * - AUDIO_SOURCE_HOTWORD
120 *
121 * @return the corresponding input source priority or 0 if priority is irrelevant for this source.
122 * This happens when the specified source cannot share a given input stream (e.g remote submix)
123 * The higher the value, the higher the priority.
124 */
125static inline int32_t source_priority(audio_source_t inputSource)
126{
127 switch (inputSource) {
128 case AUDIO_SOURCE_VOICE_COMMUNICATION:
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800129 return 9;
Eric Laurentfb66dd92016-01-28 18:32:03 -0800130 case AUDIO_SOURCE_CAMCORDER:
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800131 return 8;
132 case AUDIO_SOURCE_VOICE_PERFORMANCE:
133 return 7;
134 case AUDIO_SOURCE_UNPROCESSED:
135 return 6;
Eric Laurentfb66dd92016-01-28 18:32:03 -0800136 case AUDIO_SOURCE_MIC:
Eric Laurentae4b6ec2019-01-15 18:34:38 -0800137 return 5;
138 case AUDIO_SOURCE_ECHO_REFERENCE:
Eric Laurentfb66dd92016-01-28 18:32:03 -0800139 return 4;
140 case AUDIO_SOURCE_FM_TUNER:
141 return 3;
142 case AUDIO_SOURCE_VOICE_RECOGNITION:
143 return 2;
144 case AUDIO_SOURCE_HOTWORD:
145 return 1;
146 default:
147 break;
148 }
149 return 0;
150}
Eric Laurente6930022016-02-11 10:20:40 -0800151
152/* Indicates if audio formats are equivalent when considering a match between
153 * audio HAL supported formats and client requested formats
154 */
155static inline bool audio_formats_match(audio_format_t format1,
156 audio_format_t format2)
157{
158 if (audio_is_linear_pcm(format1) &&
159 (audio_bytes_per_sample(format1) > 2) &&
160 audio_is_linear_pcm(format2) &&
161 (audio_bytes_per_sample(format2) > 2)) {
162 return true;
163 }
164 return format1 == format2;
165}
François Gaffiedc7553f2018-11-02 10:39:57 +0100166
167constexpr bool operator==(const audio_attributes_t &lhs, const audio_attributes_t &rhs)
168{
169 return lhs.usage == rhs.usage && lhs.content_type == rhs.content_type &&
170 lhs.flags == rhs.flags && (std::strcmp(lhs.tags, rhs.tags) == 0);
171}