xref: /aosp_15_r20/frameworks/av/services/audiopolicy/tests/AudioPolicyManagerTestClient.h (revision ec779b8e0859a360c3d303172224686826e6e0e1)
1 /*
2  * Copyright (C) 2019 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 #include <map>
18 #include <set>
19 
20 #include <media/TypeConverter.h>
21 #include <system/audio.h>
22 #include <utils/Log.h>
23 #include <utils/String8.h>
24 
25 #include "AudioPolicyTestClient.h"
26 
27 namespace android {
28 
29 class AudioPolicyManagerTestClient : public AudioPolicyTestClient {
30 public:
31     // AudioPolicyClientInterface implementation
loadHwModule(const char * name)32     audio_module_handle_t loadHwModule(const char* name) override {
33         if (!mAllowedModuleNames.empty() && !mAllowedModuleNames.count(name)) {
34             return AUDIO_MODULE_HANDLE_NONE;
35         }
36         return mNextModuleHandle++;
37     }
38 
openOutput(audio_module_handle_t module,audio_io_handle_t * output,audio_config_t * halConfig,audio_config_base_t * mixerConfig,const sp<DeviceDescriptorBase> &,uint32_t *,audio_output_flags_t * flags,audio_attributes_t)39     status_t openOutput(audio_module_handle_t module,
40                         audio_io_handle_t *output,
41                         audio_config_t *halConfig,
42                         audio_config_base_t *mixerConfig,
43                         const sp<DeviceDescriptorBase>& /*device*/,
44                         uint32_t * /*latencyMs*/,
45                         audio_output_flags_t *flags,
46                         audio_attributes_t /*attributes*/) override {
47         if (module >= mNextModuleHandle) {
48             ALOGE("%s: Module handle %d has not been allocated yet (next is %d)",
49                   __func__, module, mNextModuleHandle);
50             return BAD_VALUE;
51         }
52         *output = mNextIoHandle++;
53         mOpenedOutputs[*output] = *flags;
54         ALOGD("%s: opened output %d: HAL(%s %s %d) Mixer(%s %s %d) %s", __func__, *output,
55               audio_channel_out_mask_to_string(halConfig->channel_mask),
56               audio_format_to_string(halConfig->format), halConfig->sample_rate,
57               audio_channel_out_mask_to_string(mixerConfig->channel_mask),
58               audio_format_to_string(mixerConfig->format), mixerConfig->sample_rate,
59               android::toString(*flags).c_str());
60         return NO_ERROR;
61     }
62 
openDuplicateOutput(audio_io_handle_t,audio_io_handle_t)63     audio_io_handle_t openDuplicateOutput(audio_io_handle_t /*output1*/,
64                                           audio_io_handle_t /*output2*/) override {
65         audio_io_handle_t id = mNextIoHandle++;
66         return id;
67     }
68 
closeOutput(audio_io_handle_t output)69     status_t closeOutput(audio_io_handle_t output) override {
70         if (auto iter = mOpenedOutputs.find(output); iter != mOpenedOutputs.end()) {
71             mOpenedOutputs.erase(iter);
72             return NO_ERROR;
73         } else {
74             ALOGE("%s: Unknown output %d", __func__, output);
75             return BAD_VALUE;
76         }
77     }
78 
openInput(audio_module_handle_t module,audio_io_handle_t * input,audio_config_t *,audio_devices_t *,const String8 &,audio_source_t,audio_input_flags_t)79     status_t openInput(audio_module_handle_t module,
80                        audio_io_handle_t *input,
81                        audio_config_t * /*config*/,
82                        audio_devices_t * /*device*/,
83                        const String8 & /*address*/,
84                        audio_source_t /*source*/,
85                        audio_input_flags_t /*flags*/) override {
86         if (module >= mNextModuleHandle) {
87             ALOGE("%s: Module handle %d has not been allocated yet (next is %d)",
88                   __func__, module, mNextModuleHandle);
89             return BAD_VALUE;
90         }
91         *input = mNextIoHandle++;
92         mOpenedInputs.insert(*input);
93         ALOGD("%s: opened input %d", __func__, *input);
94         mOpenInputCallsCount++;
95         return NO_ERROR;
96     }
97 
closeInput(audio_io_handle_t input)98     status_t closeInput(audio_io_handle_t input) override {
99         if (mOpenedInputs.erase(input) != 1) {
100             if (input >= mNextIoHandle) {
101                 ALOGE("%s: I/O handle %d has not been allocated yet (next is %d)",
102                       __func__, input, mNextIoHandle);
103             } else {
104                 ALOGE("%s: Attempt to close input %d twice", __func__, input);
105             }
106             return BAD_VALUE;
107         }
108         ALOGD("%s: closed input %d", __func__, input);
109         mCloseInputCallsCount++;
110         return NO_ERROR;
111     }
112 
createAudioPatch(const struct audio_patch * patch,audio_patch_handle_t * handle,int)113     status_t createAudioPatch(const struct audio_patch *patch,
114                               audio_patch_handle_t *handle,
115                               int /*delayMs*/) override {
116         auto iter = mActivePatches.find(*handle);
117         if (iter != mActivePatches.end()) {
118             mActivePatches.erase(*handle);
119         }
120         *handle = mNextPatchHandle++;
121         mActivePatches.insert(std::make_pair(*handle, *patch));
122         return NO_ERROR;
123     }
124 
releaseAudioPatch(audio_patch_handle_t handle,int)125     status_t releaseAudioPatch(audio_patch_handle_t handle,
126                                int /*delayMs*/) override {
127         if (mActivePatches.erase(handle) != 1) {
128             if (handle >= mNextPatchHandle) {
129                 ALOGE("%s: Patch handle %d has not been allocated yet (next is %d)",
130                       __func__, handle, mNextPatchHandle);
131             } else {
132                 ALOGE("%s: Attempt to release patch %d twice", __func__, handle);
133             }
134             return BAD_VALUE;
135         }
136         return NO_ERROR;
137     }
138 
onAudioPortListUpdate()139     void onAudioPortListUpdate() override {
140         ++mAudioPortListUpdateCount;
141     }
142 
setDeviceConnectedState(const struct audio_port_v7 * port,media::DeviceConnectedState state)143     status_t setDeviceConnectedState(const struct audio_port_v7 *port,
144                                      media::DeviceConnectedState state) override {
145         if (state == media::DeviceConnectedState::CONNECTED) {
146             mConnectedDevicePorts.push_back(*port);
147         } else if (state == media::DeviceConnectedState::DISCONNECTED){
148             mDisconnectedDevicePorts.push_back(*port);
149         }
150         return NO_ERROR;
151     }
152 
153     // Helper methods for tests
getActivePatchesCount()154     size_t getActivePatchesCount() const { return mActivePatches.size(); }
155 
getLastAddedPatch()156     const struct audio_patch *getLastAddedPatch() const {
157         if (mActivePatches.empty()) {
158             return nullptr;
159         }
160         auto it = --mActivePatches.end();
161         return &it->second;
162     };
163 
getOpenedInputsCount()164     size_t getOpenedInputsCount() const { return mOpenedInputs.size(); }
165 
peekNextModuleHandle()166     audio_module_handle_t peekNextModuleHandle() const { return mNextModuleHandle; }
167 
168     void swapAllowedModuleNames(std::set<std::string>&& names = {}) {
169         mAllowedModuleNames.swap(names);
170     }
171 
getAudioPortListUpdateCount()172     size_t getAudioPortListUpdateCount() const { return mAudioPortListUpdateCount; }
173 
onRoutingUpdated()174     void onRoutingUpdated() override {
175         mRoutingUpdatedUpdateCount++;
176     }
177 
resetRoutingUpdatedCounter()178     void resetRoutingUpdatedCounter() {
179         mRoutingUpdatedUpdateCount = 0;
180     }
181 
getRoutingUpdatedCounter()182     size_t getRoutingUpdatedCounter() const {
183         return mRoutingUpdatedUpdateCount;
184     }
185 
onVolumeRangeInitRequest()186     void onVolumeRangeInitRequest() override {
187 
188     }
189 
updateSecondaryOutputs(const TrackSecondaryOutputsMap & trackSecondaryOutputs __unused)190     status_t updateSecondaryOutputs(
191             const TrackSecondaryOutputsMap& trackSecondaryOutputs __unused) override {
192         return NO_ERROR;
193     }
194 
getConnectedDevicePortCount()195     size_t getConnectedDevicePortCount() const {
196         return mConnectedDevicePorts.size();
197     }
198 
getLastConnectedDevicePort()199     const struct audio_port_v7 *getLastConnectedDevicePort() const {
200         if (mConnectedDevicePorts.empty()) {
201             return nullptr;
202         }
203         auto it = --mConnectedDevicePorts.end();
204         return &(*it);
205     }
206 
getDisconnectedDevicePortCount()207     size_t getDisconnectedDevicePortCount() const {
208         return mDisconnectedDevicePorts.size();
209     }
210 
getLastDisconnectedDevicePort()211     const struct audio_port_v7 *getLastDisconnectedDevicePort() const {
212         if (mDisconnectedDevicePorts.empty()) {
213             return nullptr;
214         }
215         auto it = --mDisconnectedDevicePorts.end();
216         return &(*it);
217     }
218 
getParameters(audio_io_handle_t,const String8 &)219     String8 getParameters(audio_io_handle_t /* ioHandle */, const String8&  /* keys*/ ) override {
220         AudioParameter mAudioParameters;
221         std::string formats;
222         for (const auto& f : mSupportedFormats) {
223             if (!formats.empty()) formats += AUDIO_PARAMETER_VALUE_LIST_SEPARATOR;
224             formats += audio_format_to_string(f);
225         }
226         mAudioParameters.add(
227                 String8(AudioParameter::keyStreamSupportedFormats),
228                 String8(formats.c_str()));
229         mAudioParameters.addInt(String8(AudioParameter::keyStreamSupportedSamplingRates), 48000);
230         std::string channelMasks;
231         for (const auto& cm : mSupportedChannelMasks) {
232             if (!audio_channel_mask_is_valid(cm)) {
233                 continue;
234             }
235             if (!channelMasks.empty()) channelMasks += AUDIO_PARAMETER_VALUE_LIST_SEPARATOR;
236             channelMasks += audio_channel_mask_to_string(cm);
237         }
238         mAudioParameters.add(
239                 String8(AudioParameter::keyStreamSupportedChannels), String8(channelMasks.c_str()));
240         return mAudioParameters.toString();
241     }
242 
getAudioMixPort(const struct audio_port_v7 * devicePort __unused,struct audio_port_v7 * mixPort)243     status_t getAudioMixPort(const struct audio_port_v7 *devicePort __unused,
244                              struct audio_port_v7 *mixPort) override {
245         mixPort->num_audio_profiles = 0;
246         for (auto format : mSupportedFormats) {
247             const int i = mixPort->num_audio_profiles;
248             mixPort->audio_profiles[i].format = format;
249             mixPort->audio_profiles[i].num_sample_rates = 1;
250             mixPort->audio_profiles[i].sample_rates[0] = 48000;
251             mixPort->audio_profiles[i].num_channel_masks = 0;
252             for (const auto& cm : mSupportedChannelMasks) {
253                 if (audio_channel_mask_is_valid(cm)) {
254                     mixPort->audio_profiles[i].channel_masks[
255                             mixPort->audio_profiles[i].num_channel_masks++] = cm;
256                 }
257             }
258             mixPort->num_audio_profiles++;
259         }
260         return NO_ERROR;
261     }
262 
setTracksInternalMute(const std::vector<media::TrackInternalMuteInfo> & tracksInternalMute)263     status_t setTracksInternalMute(
264             const std::vector<media::TrackInternalMuteInfo>& tracksInternalMute) override {
265         for (const auto& trackInternalMute : tracksInternalMute) {
266             mTracksInternalMute[(audio_port_handle_t)trackInternalMute.portId] =
267                     trackInternalMute.muted;
268         }
269         return NO_ERROR;
270     }
271 
addSupportedFormat(audio_format_t format)272     void addSupportedFormat(audio_format_t format) {
273         mSupportedFormats.insert(format);
274     }
275 
addSupportedChannelMask(audio_channel_mask_t channelMask)276     void addSupportedChannelMask(audio_channel_mask_t channelMask) {
277         mSupportedChannelMasks.insert(channelMask);
278     }
279 
getTrackInternalMute(audio_port_handle_t portId)280     bool getTrackInternalMute(audio_port_handle_t portId) {
281         auto it = mTracksInternalMute.find(portId);
282         return it == mTracksInternalMute.end() ? false : it->second;
283     }
resetInputApiCallsCounters()284     void resetInputApiCallsCounters() {
285         mOpenInputCallsCount = 0;
286         mCloseInputCallsCount = 0;
287     }
288 
getCloseInputCallsCount()289     size_t getCloseInputCallsCount() const {
290         return mCloseInputCallsCount;
291     }
292 
getOpenInputCallsCount()293     size_t getOpenInputCallsCount() const {
294         return mOpenInputCallsCount;
295     }
296 
getOpenOutputFlags(audio_io_handle_t output)297     std::optional<audio_output_flags_t> getOpenOutputFlags(audio_io_handle_t output) const {
298         if (auto iter = mOpenedOutputs.find(output); iter != mOpenedOutputs.end()) {
299             return iter->second;
300         }
301         return std::nullopt;
302     }
303 
304 private:
305     audio_module_handle_t mNextModuleHandle = AUDIO_MODULE_HANDLE_NONE + 1;
306     audio_io_handle_t mNextIoHandle = AUDIO_IO_HANDLE_NONE + 1;
307     audio_patch_handle_t mNextPatchHandle = AUDIO_PATCH_HANDLE_NONE + 1;
308     std::map<audio_patch_handle_t, struct audio_patch> mActivePatches;
309     std::set<std::string> mAllowedModuleNames;
310     size_t mAudioPortListUpdateCount = 0;
311     size_t mRoutingUpdatedUpdateCount = 0;
312     std::vector<struct audio_port_v7> mConnectedDevicePorts;
313     std::vector<struct audio_port_v7> mDisconnectedDevicePorts;
314     std::set<audio_format_t> mSupportedFormats;
315     std::set<audio_channel_mask_t> mSupportedChannelMasks;
316     std::map<audio_port_handle_t, bool> mTracksInternalMute;
317     std::set<audio_io_handle_t> mOpenedInputs;
318     size_t mOpenInputCallsCount = 0;
319     size_t mCloseInputCallsCount = 0;
320     std::map<audio_io_handle_t, audio_output_flags_t> mOpenedOutputs;
321 };
322 
323 } // namespace android
324