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 | |
| 26 | |
| 27 | static const audio_attributes_t defaultAttr = AUDIO_ATTRIBUTES_INITIALIZER; |
| 28 | |
| 29 | } // namespace android |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 30 | |
François Gaffie | 5fcd6f9 | 2015-11-27 13:46:12 +0100 | [diff] [blame] | 31 | static const audio_format_t gDynamicFormat = AUDIO_FORMAT_DEFAULT; |
François Gaffie | 5fcd6f9 | 2015-11-27 13:46:12 +0100 | [diff] [blame] | 32 | |
François Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame^] | 33 | static const uint32_t SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY = 5000; |
| 34 | |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 35 | // For mixed output and inputs, the policy will use max mixer sampling rates. |
| 36 | // Do not limit sampling rate otherwise |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 37 | #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 Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 41 | |
| 42 | // For mixed output and inputs, the policy will use max mixer channel count. |
| 43 | // Do not limit channel count otherwise |
Glenn Kasten | 05ddca5 | 2016-02-11 08:17:12 -0800 | [diff] [blame] | 44 | #define MAX_MIXER_CHANNEL_COUNT FCC_8 |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 45 | |
| 46 | /** |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 47 | * 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 Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 50 | #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] | 51 | |
Jean-Michel Trivi | 7638ca2 | 2016-03-04 17:42:44 -0800 | [diff] [blame] | 52 | #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] | 53 | |
| 54 | /** |
Eric Laurent | 5a2b629 | 2016-04-14 18:05:57 -0700 | [diff] [blame] | 55 | * 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 Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 62 | * 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 | */ |
| 69 | static 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 Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 75 | * 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 Hsieh | 5603d28 | 2015-05-04 17:14:15 -0700 | [diff] [blame] | 82 | static inline bool device_distinguishes_on_address(audio_devices_t device) |
François Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 83 | { |
Eric Laurent | 7c1ec5f | 2015-07-09 14:52:47 -0700 | [diff] [blame] | 84 | 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 Gaffie | 53615e2 | 2015-03-19 09:24:12 +0100 | [diff] [blame] | 88 | } |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 89 | |
| 90 | /** |
Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 91 | * 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 | */ |
| 97 | static 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 Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 106 | * 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 Laurent | ae4b6ec | 2019-01-15 18:34:38 -0800 | [diff] [blame] | 113 | * - AUDIO_SOURCE_VOICE_PERFORMANCE |
| 114 | * - AUDIO_SOURCE_UNPROCESSED |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 115 | * - AUDIO_SOURCE_MIC |
Eric Laurent | ae4b6ec | 2019-01-15 18:34:38 -0800 | [diff] [blame] | 116 | * - AUDIO_SOURCE_ECHO_REFERENCE |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 117 | * - 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 | */ |
| 125 | static inline int32_t source_priority(audio_source_t inputSource) |
| 126 | { |
| 127 | switch (inputSource) { |
| 128 | case AUDIO_SOURCE_VOICE_COMMUNICATION: |
Eric Laurent | ae4b6ec | 2019-01-15 18:34:38 -0800 | [diff] [blame] | 129 | return 9; |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 130 | case AUDIO_SOURCE_CAMCORDER: |
Eric Laurent | ae4b6ec | 2019-01-15 18:34:38 -0800 | [diff] [blame] | 131 | return 8; |
| 132 | case AUDIO_SOURCE_VOICE_PERFORMANCE: |
| 133 | return 7; |
| 134 | case AUDIO_SOURCE_UNPROCESSED: |
| 135 | return 6; |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 136 | case AUDIO_SOURCE_MIC: |
Eric Laurent | ae4b6ec | 2019-01-15 18:34:38 -0800 | [diff] [blame] | 137 | return 5; |
| 138 | case AUDIO_SOURCE_ECHO_REFERENCE: |
Eric Laurent | fb66dd9 | 2016-01-28 18:32:03 -0800 | [diff] [blame] | 139 | 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 Laurent | e693002 | 2016-02-11 10:20:40 -0800 | [diff] [blame] | 151 | |
| 152 | /* Indicates if audio formats are equivalent when considering a match between |
| 153 | * audio HAL supported formats and client requested formats |
| 154 | */ |
| 155 | static 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 Gaffie | dc7553f | 2018-11-02 10:39:57 +0100 | [diff] [blame^] | 166 | |
| 167 | constexpr 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 | } |