• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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>
20 #include <set>
21 #include <vector>
22 
23 #include <media/AudioContainers.h>
24 
25 #include <string.h>
26 
27 namespace android {
28 
29 using StreamTypeVector = std::vector<audio_stream_type_t>;
30 
31 #define AUDIO_ENUM_QUOTE(x) #x
32 #define AUDIO_ENUM_STRINGIFY(x) AUDIO_ENUM_QUOTE(x)
33 #define AUDIO_DEFINE_ENUM_SYMBOL_V(symbol, value) symbol = value,
34 #define AUDIO_DEFINE_STRINGIFY_CASE_V(symbol, _) case symbol: return AUDIO_ENUM_STRINGIFY(symbol);
35 #define AUDIO_DEFINE_PARSE_CASE_V(symbol, _) \
36     if (strcmp(s, AUDIO_ENUM_STRINGIFY(symbol)) == 0) { *t = symbol; return true; } else
37 #define AUDIO_DEFINE_MAP_ENTRY_V(symbol, _) { AUDIO_ENUM_STRINGIFY(symbol), symbol },
38 
39 /**
40  * Legacy audio policy product strategies IDs. These strategies are supported by the default
41  * policy engine.
42  * IMPORTANT NOTE: the order of this enum is important as it determines the priority
43  * between active strategies for routing decisions: lower enum value => higher priority
44  */
45 #define AUDIO_LEGACY_STRATEGY_LIST_DEF(V)      \
46     V(STRATEGY_NONE, -1)                       \
47     V(STRATEGY_PHONE, 0)                       \
48     V(STRATEGY_SONIFICATION, 1)                \
49     V(STRATEGY_ENFORCED_AUDIBLE, 2)            \
50     V(STRATEGY_ACCESSIBILITY, 3)               \
51     V(STRATEGY_SONIFICATION_RESPECTFUL, 4)     \
52     V(STRATEGY_MEDIA, 5)                       \
53     V(STRATEGY_DTMF, 6)                        \
54     V(STRATEGY_CALL_ASSISTANT, 7)              \
55     V(STRATEGY_TRANSMITTED_THROUGH_SPEAKER, 8) \
56     V(STRATEGY_REROUTING, 9)                   \
57     V(STRATEGY_PATCH, 10)
58 
59 enum legacy_strategy {
60     AUDIO_LEGACY_STRATEGY_LIST_DEF(AUDIO_DEFINE_ENUM_SYMBOL_V)
61 };
62 
legacy_strategy_to_string(legacy_strategy t)63 inline const char* legacy_strategy_to_string(legacy_strategy t) {
64     switch (t) {
65     AUDIO_LEGACY_STRATEGY_LIST_DEF(AUDIO_DEFINE_STRINGIFY_CASE_V)
66     }
67     return "";
68 }
69 
legacy_strategy_from_string(const char * s,legacy_strategy * t)70 inline bool legacy_strategy_from_string(const char* s, legacy_strategy* t) {
71     AUDIO_LEGACY_STRATEGY_LIST_DEF(AUDIO_DEFINE_PARSE_CASE_V)
72     return false;
73 }
74 
75 namespace audio_policy {
76 
77 struct legacy_strategy_map { const char *name; legacy_strategy id; };
78 
getLegacyStrategyMap()79 inline std::vector<legacy_strategy_map> getLegacyStrategyMap() {
80     return std::vector<legacy_strategy_map> {
81     AUDIO_LEGACY_STRATEGY_LIST_DEF(AUDIO_DEFINE_MAP_ENTRY_V)
82     };
83 }
84 
85 }  // namespace audio_policy
86 
87 #undef AUDIO_LEGACY_STRATEGY_LIST_DEF
88 
89 #undef AUDIO_DEFINE_MAP_ENTRY_V
90 #undef AUDIO_DEFINE_PARSE_CASE_V
91 #undef AUDIO_DEFINE_STRINGIFY_CASE_V
92 #undef AUDIO_DEFINE_ENUM_SYMBOL_V
93 #undef AUDIO_ENUM_STRINGIFY
94 #undef AUDIO_ENUM_QUOTE
95 
96 static const audio_attributes_t defaultAttr = AUDIO_ATTRIBUTES_INITIALIZER;
97 
98 static const std::set<audio_usage_t > gHighPriorityUseCases = {
99         AUDIO_USAGE_ALARM, AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE
100 };
101 
102 } // namespace android
103 
104 static const audio_format_t gDynamicFormat = AUDIO_FORMAT_DEFAULT;
105 
106 static const uint32_t SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY = 5000;
107 
108 // Used when a client opens a capture stream, without specifying a desired sample rate.
109 #define SAMPLE_RATE_HZ_DEFAULT 48000
110 
111 // For mixed output and inputs, the policy will use max mixer channel count.
112 // Do not limit channel count otherwise
113 #define MAX_MIXER_CHANNEL_COUNT FCC_LIMIT
114 
115 /**
116  * Alias to AUDIO_DEVICE_OUT_DEFAULT defined for clarification when this value is used by volume
117  * control APIs (e.g setStreamVolumeIndex().
118  */
119 #define AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME AUDIO_DEVICE_OUT_DEFAULT
120 
121 
122 /**
123  * Check if the state given correspond to an in call state.
124  * @TODO find a better name for widely call state
125  *
126  * @param[in] state to consider
127  *
128  * @return true if given state represents a device in a telephony or VoIP call
129  */
is_state_in_call(int state)130 static inline bool is_state_in_call(int state)
131 {
132     return (state == AUDIO_MODE_IN_CALL) || (state == AUDIO_MODE_IN_COMMUNICATION);
133 }
134 
135 /**
136  * Check whether the output device type is one
137  * where addresses are used to distinguish between one connected device and another
138  *
139  * @param[in] device to consider
140  *
141  * @return true if the device needs distinguish on address, false otherwise..
142  */
apm_audio_out_device_distinguishes_on_address(audio_devices_t device)143 static inline bool apm_audio_out_device_distinguishes_on_address(audio_devices_t device)
144 {
145     return device == AUDIO_DEVICE_OUT_REMOTE_SUBMIX ||
146            device == AUDIO_DEVICE_OUT_BUS;
147 }
148 
149 /**
150  * Check whether the input device type is one
151  * where addresses are used to distinguish between one connected device and another
152  *
153  * @param[in] device to consider
154  *
155  * @return true if the device needs distinguish on address, false otherwise..
156  */
apm_audio_in_device_distinguishes_on_address(audio_devices_t device)157 static inline bool apm_audio_in_device_distinguishes_on_address(audio_devices_t device)
158 {
159     return device == AUDIO_DEVICE_IN_REMOTE_SUBMIX ||
160            device == AUDIO_DEVICE_IN_BUS ||
161            device == AUDIO_DEVICE_IN_ECHO_REFERENCE;
162 }
163 
164 /**
165  * Check whether the device type is one
166  * where addresses are used to distinguish between one connected device and another
167  *
168  * @param[in] device to consider
169  *
170  * @return true if the device needs distinguish on address, false otherwise..
171  */
device_distinguishes_on_address(audio_devices_t device)172 static inline bool device_distinguishes_on_address(audio_devices_t device)
173 {
174     return apm_audio_in_device_distinguishes_on_address(device) ||
175            apm_audio_out_device_distinguishes_on_address(device);
176 }
177 
178 /**
179  * Check whether audio device has encoding capability.
180  *
181  * @param[in] device to consider
182  *
183  * @return true if device has encoding capability, false otherwise..
184  */
device_has_encoding_capability(audio_devices_t device)185 static inline bool device_has_encoding_capability(audio_devices_t device)
186 {
187     return audio_is_a2dp_out_device(device) || audio_is_ble_out_device(device);
188 }
189 
190 /**
191  * Returns the priority of a given audio source for capture. The priority is used when more than one
192  * capture session is active on a given input stream to determine which session drives routing and
193  * effect configuration.
194  *
195  * @param[in] inputSource to consider. Valid sources are:
196  * - AUDIO_SOURCE_VOICE_COMMUNICATION
197  * - AUDIO_SOURCE_CAMCORDER
198  * - AUDIO_SOURCE_VOICE_PERFORMANCE
199  * - AUDIO_SOURCE_UNPROCESSED
200  * - AUDIO_SOURCE_MIC
201  * - AUDIO_SOURCE_ECHO_REFERENCE
202  * - AUDIO_SOURCE_FM_TUNER
203  * - AUDIO_SOURCE_VOICE_RECOGNITION
204  * - AUDIO_SOURCE_HOTWORD
205  * - AUDIO_SOURCE_ULTRASOUND
206  *
207  * @return the corresponding input source priority or 0 if priority is irrelevant for this source.
208  *      This happens when the specified source cannot share a given input stream (e.g remote submix)
209  *      The higher the value, the higher the priority.
210  */
source_priority(audio_source_t inputSource)211 static inline int32_t source_priority(audio_source_t inputSource)
212 {
213     switch (inputSource) {
214     case AUDIO_SOURCE_VOICE_COMMUNICATION:
215         return 10;
216     case AUDIO_SOURCE_CAMCORDER:
217         return 9;
218     case AUDIO_SOURCE_VOICE_PERFORMANCE:
219         return 8;
220     case AUDIO_SOURCE_UNPROCESSED:
221         return 7;
222     case AUDIO_SOURCE_MIC:
223         return 6;
224     case AUDIO_SOURCE_ECHO_REFERENCE:
225         return 5;
226     case AUDIO_SOURCE_FM_TUNER:
227         return 4;
228     case AUDIO_SOURCE_VOICE_RECOGNITION:
229         return 3;
230     case AUDIO_SOURCE_HOTWORD:
231         return 2;
232     case AUDIO_SOURCE_ULTRASOUND:
233         return 1;
234     default:
235         break;
236     }
237     return 0;
238 }
239 
240 /* Indicates if audio formats are equivalent when considering a match between
241  * audio HAL supported formats and client requested formats
242  */
audio_formats_match(audio_format_t format1,audio_format_t format2)243 static inline bool audio_formats_match(audio_format_t format1,
244                                        audio_format_t format2)
245 {
246     if (audio_is_linear_pcm(format1) &&
247             (audio_bytes_per_sample(format1) > 2) &&
248             audio_is_linear_pcm(format2) &&
249             (audio_bytes_per_sample(format2) > 2)) {
250         return true;
251     }
252     return format1 == format2;
253 }
254 
255 /**
256  * @brief hasStream checks if a given stream type is found in the list of streams
257  * @param streams collection of stream types to consider.
258  * @param streamType to consider
259  * @return true if voice stream is found in the given streams, false otherwise
260  */
hasStream(const android::StreamTypeVector & streams,audio_stream_type_t streamType)261 static inline bool hasStream(const android::StreamTypeVector &streams,
262                              audio_stream_type_t streamType)
263 {
264     return std::find(begin(streams), end(streams), streamType) != end(streams);
265 }
266 
267 /**
268  * @brief hasVoiceStream checks if a voice stream is found in the list of streams
269  * @param streams collection to consider.
270  * @return true if voice stream is found in the given streams, false otherwise
271  */
hasVoiceStream(const android::StreamTypeVector & streams)272 static inline bool hasVoiceStream(const android::StreamTypeVector &streams)
273 {
274     return hasStream(streams, AUDIO_STREAM_VOICE_CALL);
275 }
276 
277 /**
278  * @brief extract one device relevant from multiple device selection
279  * @param deviceTypes collection of audio device type
280  * @return the device type that is selected
281  */
apm_extract_one_audio_device(const android::DeviceTypeSet & deviceTypes)282 static inline audio_devices_t apm_extract_one_audio_device(
283         const android::DeviceTypeSet& deviceTypes) {
284     if (deviceTypes.empty()) {
285         return AUDIO_DEVICE_NONE;
286     } else if (deviceTypes.size() == 1) {
287         return *(deviceTypes.begin());
288     } else {
289         // Multiple device selection is either:
290         //  - dock + one other device: give priority to dock in this case.
291         //  - speaker + one other device: give priority to speaker in this case.
292         //  - one removable device + another device: happens with duplicated output. In this case
293         // retain the removable device as the other must not correspond to an active
294         // selection if not the speaker.
295         //  - HDMI-CEC system audio mode only output: give priority to available item in order.
296         if (deviceTypes.count(AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET) != 0) {
297             return AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
298         } else if (deviceTypes.count(AUDIO_DEVICE_OUT_SPEAKER) != 0) {
299             return AUDIO_DEVICE_OUT_SPEAKER;
300         } else if (deviceTypes.count(AUDIO_DEVICE_OUT_SPEAKER_SAFE) != 0) {
301             return AUDIO_DEVICE_OUT_SPEAKER_SAFE;
302         } else if (deviceTypes.count(AUDIO_DEVICE_OUT_HDMI_ARC) != 0) {
303             return AUDIO_DEVICE_OUT_HDMI_ARC;
304         } else if (deviceTypes.count(AUDIO_DEVICE_OUT_HDMI_EARC) != 0) {
305             return AUDIO_DEVICE_OUT_HDMI_EARC;
306         } else if (deviceTypes.count(AUDIO_DEVICE_OUT_AUX_LINE) != 0) {
307             return AUDIO_DEVICE_OUT_AUX_LINE;
308         } else if (deviceTypes.count(AUDIO_DEVICE_OUT_SPDIF) != 0) {
309             return AUDIO_DEVICE_OUT_SPDIF;
310         } else {
311             std::vector<audio_devices_t> volumeDevices = android::Intersection(
312                     deviceTypes, android::getAudioDeviceOutPickForVolumeSet());
313             if (volumeDevices.empty() || volumeDevices.size() > 1) {
314                 ALOGW("%s invalid device combination: %s",
315                       __func__, android::dumpDeviceTypes(deviceTypes).c_str());
316             }
317             return volumeDevices.empty() ? AUDIO_DEVICE_NONE : volumeDevices[0];
318         }
319     }
320 }
321 
322 /**
323  * Indicates if two given audio output flags are considered as matched, which means that
324  * 1) the `supersetFlags` and `subsetFlags` both contain or both don't contain must match flags and
325  * 2) `supersetFlags` contains all flags from `subsetFlags`.
326  */
audio_output_flags_is_subset(audio_output_flags_t supersetFlags,audio_output_flags_t subsetFlags,uint32_t mustMatchFlags)327 static inline bool audio_output_flags_is_subset(audio_output_flags_t supersetFlags,
328                                                 audio_output_flags_t subsetFlags,
329                                                 uint32_t mustMatchFlags)
330 {
331     return ((supersetFlags ^ subsetFlags) & mustMatchFlags) == AUDIO_OUTPUT_FLAG_NONE
332             && (supersetFlags & subsetFlags) == subsetFlags;
333 }
334