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 | /** |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 46 | * 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 Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 49 | #define APM_AUDIO_DEVICE_OUT_MATCH_ADDRESS_ALL (AUDIO_DEVICE_OUT_REMOTE_SUBMIX|AUDIO_DEVICE_OUT_BUS) |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 50 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 51 | #define APM_AUDIO_DEVICE_IN_MATCH_ADDRESS_ALL (AUDIO_DEVICE_IN_REMOTE_SUBMIX|AUDIO_DEVICE_IN_BUS) |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 52 | |
| 53 | /** |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 54 | * 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 Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 61 | * 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 | */ |
| 68 | static 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 Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 74 | * 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 Hsieh | 5603d28 | 2015-05-04 17:14:15 -0700 | [diff] [blame] | 81 | static inline bool device_distinguishes_on_address(audio_devices_t device) |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 82 | { |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 83 | 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 Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 87 | } |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 88 | |
| 89 | /** |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 90 | * 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 | */ |
| 96 | static 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 Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 105 | * 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 Laurent | ae4b6ec | 2019-01-15 18:34:38 -0800 | [diff] [blame] | 112 | * - AUDIO_SOURCE_VOICE_PERFORMANCE |
| 113 | * - AUDIO_SOURCE_UNPROCESSED |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 114 | * - AUDIO_SOURCE_MIC |
Eric Laurent | ae4b6ec | 2019-01-15 18:34:38 -0800 | [diff] [blame] | 115 | * - AUDIO_SOURCE_ECHO_REFERENCE |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 116 | * - 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 | */ |
| 124 | static inline int32_t source_priority(audio_source_t inputSource) |
| 125 | { |
| 126 | switch (inputSource) { |
| 127 | case AUDIO_SOURCE_VOICE_COMMUNICATION: |
Eric Laurent | ae4b6ec | 2019-01-15 18:34:38 -0800 | [diff] [blame] | 128 | return 9; |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 129 | case AUDIO_SOURCE_CAMCORDER: |
Eric Laurent | ae4b6ec | 2019-01-15 18:34:38 -0800 | [diff] [blame] | 130 | return 8; |
| 131 | case AUDIO_SOURCE_VOICE_PERFORMANCE: |
| 132 | return 7; |
| 133 | case AUDIO_SOURCE_UNPROCESSED: |
| 134 | return 6; |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 135 | case AUDIO_SOURCE_MIC: |
Eric Laurent | ae4b6ec | 2019-01-15 18:34:38 -0800 | [diff] [blame] | 136 | return 5; |
| 137 | case AUDIO_SOURCE_ECHO_REFERENCE: |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 138 | 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 Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 150 | |
| 151 | /* Indicates if audio formats are equivalent when considering a match between |
| 152 | * audio HAL supported formats and client requested formats |
| 153 | */ |
| 154 | static 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 Gaffie | c005e56 | 2018-11-06 15:04:49 +0100 | [diff] [blame] | 165 | |
| 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 | */ |
| 172 | static 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 | */ |
| 183 | static inline bool hasVoiceStream(const android::StreamTypeVector &streams) |
| 184 | { |
| 185 | return hasStream(streams, AUDIO_STREAM_VOICE_CALL); |
| 186 | } |