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 
21 static const audio_format_t gDynamicFormat = AUDIO_FORMAT_DEFAULT;
22 
23 // For mixed output and inputs, the policy will use max mixer sampling rates.
24 // Do not limit sampling rate otherwise
25 #define SAMPLE_RATE_HZ_MAX 192000
26 
27 // Used when a client opens a capture stream, without specifying a desired sample rate.
28 #define SAMPLE_RATE_HZ_DEFAULT 48000
29 
30 // For mixed output and inputs, the policy will use max mixer channel count.
31 // Do not limit channel count otherwise
32 #define MAX_MIXER_CHANNEL_COUNT FCC_8
33 
34 /**
35  * A device mask for all audio input devices that are considered "virtual" when evaluating
36  * active inputs in getActiveInputs()
37  */
38 #define APM_AUDIO_IN_DEVICE_VIRTUAL_ALL (AUDIO_DEVICE_IN_REMOTE_SUBMIX|\
39         AUDIO_DEVICE_IN_BUS|AUDIO_DEVICE_IN_FM_TUNER)
40 
41 
42 /**
43  * A device mask for all audio input and output devices where matching inputs/outputs on device
44  * type alone is not enough: the address must match too
45  */
46 #define APM_AUDIO_DEVICE_OUT_MATCH_ADDRESS_ALL (AUDIO_DEVICE_OUT_REMOTE_SUBMIX|AUDIO_DEVICE_OUT_BUS)
47 
48 #define APM_AUDIO_DEVICE_IN_MATCH_ADDRESS_ALL (AUDIO_DEVICE_IN_REMOTE_SUBMIX|AUDIO_DEVICE_IN_BUS)
49 
50 /**
51  * Alias to AUDIO_DEVICE_OUT_DEFAULT defined for clarification when this value is used by volume
52  * control APIs (e.g setStreamVolumeIndex().
53  */
54 #define AUDIO_DEVICE_OUT_DEFAULT_FOR_VOLUME AUDIO_DEVICE_OUT_DEFAULT
55 
56 
57 /**
58  * Check if the state given correspond to an in call state.
59  * @TODO find a better name for widely call state
60  *
61  * @param[in] state to consider
62  *
63  * @return true if given state represents a device in a telephony or VoIP call
64  */
is_state_in_call(int state)65 static inline bool is_state_in_call(int state)
66 {
67     return (state == AUDIO_MODE_IN_CALL) || (state == AUDIO_MODE_IN_COMMUNICATION);
68 }
69 
70 /**
71  * Check if the input device given is considered as a virtual device.
72  *
73  * @param[in] device to consider
74  *
75  * @return true if the device is a virtual one, false otherwise.
76  */
is_virtual_input_device(audio_devices_t device)77 static inline bool is_virtual_input_device(audio_devices_t device)
78 {
79     if ((device & AUDIO_DEVICE_BIT_IN) != 0) {
80         device &= ~AUDIO_DEVICE_BIT_IN;
81         if ((popcount(device) == 1) && ((device & ~APM_AUDIO_IN_DEVICE_VIRTUAL_ALL) == 0))
82             return true;
83     }
84     return false;
85 }
86 
87 /**
88  * Check whether the device type is one
89  * where addresses are used to distinguish between one connected device and another
90  *
91  * @param[in] device to consider
92  *
93  * @return true if the device needs distinguish on address, false otherwise..
94  */
device_distinguishes_on_address(audio_devices_t device)95 static inline bool device_distinguishes_on_address(audio_devices_t device)
96 {
97     return (((device & AUDIO_DEVICE_BIT_IN) != 0) &&
98             ((~AUDIO_DEVICE_BIT_IN & device & APM_AUDIO_DEVICE_IN_MATCH_ADDRESS_ALL) != 0)) ||
99            (((device & AUDIO_DEVICE_BIT_IN) == 0) &&
100             ((device & APM_AUDIO_DEVICE_OUT_MATCH_ADDRESS_ALL) != 0));
101 }
102 
103 /**
104  * Returns the priority of a given audio source for capture. The priority is used when more than one
105  * capture session is active on a given input stream to determine which session drives routing and
106  * effect configuration.
107  *
108  * @param[in] inputSource to consider. Valid sources are:
109  * - AUDIO_SOURCE_VOICE_COMMUNICATION
110  * - AUDIO_SOURCE_CAMCORDER
111  * - AUDIO_SOURCE_MIC
112  * - AUDIO_SOURCE_FM_TUNER
113  * - AUDIO_SOURCE_VOICE_RECOGNITION
114  * - AUDIO_SOURCE_HOTWORD
115  *
116  * @return the corresponding input source priority or 0 if priority is irrelevant for this source.
117  *      This happens when the specified source cannot share a given input stream (e.g remote submix)
118  *      The higher the value, the higher the priority.
119  */
source_priority(audio_source_t inputSource)120 static inline int32_t source_priority(audio_source_t inputSource)
121 {
122     switch (inputSource) {
123     case AUDIO_SOURCE_VOICE_COMMUNICATION:
124         return 6;
125     case AUDIO_SOURCE_CAMCORDER:
126         return 5;
127     case AUDIO_SOURCE_MIC:
128         return 4;
129     case AUDIO_SOURCE_FM_TUNER:
130         return 3;
131     case AUDIO_SOURCE_VOICE_RECOGNITION:
132         return 2;
133     case AUDIO_SOURCE_HOTWORD:
134         return 1;
135     default:
136         break;
137     }
138     return 0;
139 }
140 
141 /* Indicates if audio formats are equivalent when considering a match between
142  * audio HAL supported formats and client requested formats
143  */
audio_formats_match(audio_format_t format1,audio_format_t format2)144 static inline bool audio_formats_match(audio_format_t format1,
145                                        audio_format_t format2)
146 {
147     if (audio_is_linear_pcm(format1) &&
148             (audio_bytes_per_sample(format1) > 2) &&
149             audio_is_linear_pcm(format2) &&
150             (audio_bytes_per_sample(format2) > 2)) {
151         return true;
152     }
153     return format1 == format2;
154 }
155