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 <vector>
21
22 namespace android {
23
24 using StreamTypeVector = std::vector<audio_stream_type_t>;
25
26 static const audio_attributes_t defaultAttr = AUDIO_ATTRIBUTES_INITIALIZER;
27
28 } // namespace android
29
30 static const audio_format_t gDynamicFormat = AUDIO_FORMAT_DEFAULT;
31
32 static const uint32_t SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY = 5000;
33
34 // For mixed output and inputs, the policy will use max mixer sampling rates.
35 // Do not limit sampling rate otherwise
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
40
41 // For mixed output and inputs, the policy will use max mixer channel count.
42 // Do not limit channel count otherwise
43 #define MAX_MIXER_CHANNEL_COUNT FCC_8
44
45 /**
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 */
49 #define APM_AUDIO_DEVICE_OUT_MATCH_ADDRESS_ALL (AUDIO_DEVICE_OUT_REMOTE_SUBMIX|AUDIO_DEVICE_OUT_BUS)
50
51 #define APM_AUDIO_DEVICE_IN_MATCH_ADDRESS_ALL (AUDIO_DEVICE_IN_REMOTE_SUBMIX|AUDIO_DEVICE_IN_BUS)
52
53 /**
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 /**
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 */
is_state_in_call(int state)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 /**
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 */
device_distinguishes_on_address(audio_devices_t device)81 static inline bool device_distinguishes_on_address(audio_devices_t device)
82 {
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));
87 }
88
89 /**
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 */
device_has_encoding_capability(audio_devices_t device)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 /**
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
112 * - AUDIO_SOURCE_VOICE_PERFORMANCE
113 * - AUDIO_SOURCE_UNPROCESSED
114 * - AUDIO_SOURCE_MIC
115 * - AUDIO_SOURCE_ECHO_REFERENCE
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 */
source_priority(audio_source_t inputSource)124 static inline int32_t source_priority(audio_source_t inputSource)
125 {
126 switch (inputSource) {
127 case AUDIO_SOURCE_VOICE_COMMUNICATION:
128 return 9;
129 case AUDIO_SOURCE_CAMCORDER:
130 return 8;
131 case AUDIO_SOURCE_VOICE_PERFORMANCE:
132 return 7;
133 case AUDIO_SOURCE_UNPROCESSED:
134 return 6;
135 case AUDIO_SOURCE_MIC:
136 return 5;
137 case AUDIO_SOURCE_ECHO_REFERENCE:
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 }
150
151 /* Indicates if audio formats are equivalent when considering a match between
152 * audio HAL supported formats and client requested formats
153 */
audio_formats_match(audio_format_t format1,audio_format_t format2)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 }
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 */
hasStream(const android::StreamTypeVector & streams,audio_stream_type_t streamType)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 */
hasVoiceStream(const android::StreamTypeVector & streams)183 static inline bool hasVoiceStream(const android::StreamTypeVector &streams)
184 {
185 return hasStream(streams, AUDIO_STREAM_VOICE_CALL);
186 }
187