François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 1 | /* |
| 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 Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 20 | #include <vector> |
| 21 | |
| 22 | namespace android { |
| 23 | |
| 24 | using StreamTypeVector = std::vector<audio_stream_type_t>; |
| 25 | |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 26 | static const audio_attributes_t defaultAttr = AUDIO_ATTRIBUTES_INITIALIZER; |
| 27 | |
| 28 | } // namespace android |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 29 | |
François Gaffie | 5fcd6f9 | 2015-11-27 13:46:12 +0100 | [diff] [blame] | 30 | static const audio_format_t gDynamicFormat = AUDIO_FORMAT_DEFAULT; |
François Gaffie | 5fcd6f9 | 2015-11-27 13:46:12 +0100 | [diff] [blame] | 31 | |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame] | 32 | static const uint32_t SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY = 5000; |
| 33 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 34 | // For mixed output and inputs, the policy will use max mixer sampling rates. |
| 35 | // Do not limit sampling rate otherwise |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 36 | #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 Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 40 | |
| 41 | // For mixed output and inputs, the policy will use max mixer channel count. |
| 42 | // Do not limit channel count otherwise |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 43 | #define MAX_MIXER_CHANNEL_COUNT FCC_8 |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 44 | |
| 45 | /** |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 46 | * 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 Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 53 | * 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 | */ |
| 60 | static inline bool is_state_in_call(int state) |
| 61 | { |
| 62 | return (state == AUDIO_MODE_IN_CALL) || (state == AUDIO_MODE_IN_COMMUNICATION); |
| 63 | } |
| 64 | |
| 65 | /** |
jiabin | b124ec5 | 2019-09-18 15:13:13 -0700 | [diff] [blame^] | 66 | * 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 | */ |
| 73 | static 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 | */ |
| 87 | static 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 Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 94 | * 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 Hsieh | 5603d28 | 2015-05-04 17:14:15 -0700 | [diff] [blame] | 101 | static inline bool device_distinguishes_on_address(audio_devices_t device) |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 102 | { |
jiabin | b124ec5 | 2019-09-18 15:13:13 -0700 | [diff] [blame^] | 103 | return apm_audio_in_device_distinguishes_on_address(device) || |
| 104 | apm_audio_out_device_distinguishes_on_address(device); |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 105 | } |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 106 | |
| 107 | /** |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 108 | * 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 | */ |
| 114 | static inline bool device_has_encoding_capability(audio_devices_t device) |
| 115 | { |
jiabin | b124ec5 | 2019-09-18 15:13:13 -0700 | [diff] [blame^] | 116 | return audio_is_a2dp_out_device(device); |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | /** |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 120 | * 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 Laurent | ae4b6ec | 2019-01-15 18:34:38 -0800 | [diff] [blame] | 127 | * - AUDIO_SOURCE_VOICE_PERFORMANCE |
| 128 | * - AUDIO_SOURCE_UNPROCESSED |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 129 | * - AUDIO_SOURCE_MIC |
Eric Laurent | ae4b6ec | 2019-01-15 18:34:38 -0800 | [diff] [blame] | 130 | * - AUDIO_SOURCE_ECHO_REFERENCE |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 131 | * - 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 | */ |
| 139 | static inline int32_t source_priority(audio_source_t inputSource) |
| 140 | { |
| 141 | switch (inputSource) { |
| 142 | case AUDIO_SOURCE_VOICE_COMMUNICATION: |
Eric Laurent | ae4b6ec | 2019-01-15 18:34:38 -0800 | [diff] [blame] | 143 | return 9; |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 144 | case AUDIO_SOURCE_CAMCORDER: |
Eric Laurent | ae4b6ec | 2019-01-15 18:34:38 -0800 | [diff] [blame] | 145 | return 8; |
| 146 | case AUDIO_SOURCE_VOICE_PERFORMANCE: |
| 147 | return 7; |
| 148 | case AUDIO_SOURCE_UNPROCESSED: |
| 149 | return 6; |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 150 | case AUDIO_SOURCE_MIC: |
Eric Laurent | ae4b6ec | 2019-01-15 18:34:38 -0800 | [diff] [blame] | 151 | return 5; |
| 152 | case AUDIO_SOURCE_ECHO_REFERENCE: |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 153 | 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 Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 165 | |
| 166 | /* Indicates if audio formats are equivalent when considering a match between |
| 167 | * audio HAL supported formats and client requested formats |
| 168 | */ |
| 169 | static 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 Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 180 | |
| 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 | */ |
| 187 | static 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 | */ |
| 198 | static inline bool hasVoiceStream(const android::StreamTypeVector &streams) |
| 199 | { |
| 200 | return hasStream(streams, AUDIO_STREAM_VOICE_CALL); |
| 201 | } |