jiabin | 38b2c5d | 2019-09-26 17:56:44 -0700 | [diff] [blame] | 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> |
Mikhail Naganov | 21762fa | 2020-03-05 16:28:57 -0800 | [diff] [blame] | 18 | #include <set> |
jiabin | 38b2c5d | 2019-09-26 17:56:44 -0700 | [diff] [blame] | 19 | |
| 20 | #include <system/audio.h> |
| 21 | #include <utils/Log.h> |
| 22 | #include <utils/String8.h> |
| 23 | |
| 24 | #include "AudioPolicyTestClient.h" |
| 25 | |
| 26 | namespace android { |
| 27 | |
| 28 | class AudioPolicyManagerTestClient : public AudioPolicyTestClient { |
| 29 | public: |
| 30 | // AudioPolicyClientInterface implementation |
Mikhail Naganov | 21762fa | 2020-03-05 16:28:57 -0800 | [diff] [blame] | 31 | audio_module_handle_t loadHwModule(const char* name) override { |
| 32 | if (!mAllowedModuleNames.empty() && !mAllowedModuleNames.count(name)) { |
| 33 | return AUDIO_MODULE_HANDLE_NONE; |
| 34 | } |
jiabin | 38b2c5d | 2019-09-26 17:56:44 -0700 | [diff] [blame] | 35 | return mNextModuleHandle++; |
| 36 | } |
| 37 | |
| 38 | status_t openOutput(audio_module_handle_t module, |
| 39 | audio_io_handle_t *output, |
Eric Laurent | f1f22e7 | 2021-07-13 14:04:14 +0200 | [diff] [blame] | 40 | audio_config_t * /*halConfig*/, |
| 41 | audio_config_base_t * /*mixerConfig*/, |
jiabin | c010683 | 2019-10-24 14:58:31 -0700 | [diff] [blame] | 42 | const sp<DeviceDescriptorBase>& /*device*/, |
jiabin | 38b2c5d | 2019-09-26 17:56:44 -0700 | [diff] [blame] | 43 | uint32_t * /*latencyMs*/, |
| 44 | audio_output_flags_t /*flags*/) override { |
| 45 | if (module >= mNextModuleHandle) { |
| 46 | ALOGE("%s: Module handle %d has not been allocated yet (next is %d)", |
| 47 | __func__, module, mNextModuleHandle); |
| 48 | return BAD_VALUE; |
| 49 | } |
| 50 | *output = mNextIoHandle++; |
| 51 | return NO_ERROR; |
| 52 | } |
| 53 | |
| 54 | audio_io_handle_t openDuplicateOutput(audio_io_handle_t /*output1*/, |
| 55 | audio_io_handle_t /*output2*/) override { |
| 56 | audio_io_handle_t id = mNextIoHandle++; |
| 57 | return id; |
| 58 | } |
| 59 | |
| 60 | status_t openInput(audio_module_handle_t module, |
| 61 | audio_io_handle_t *input, |
| 62 | audio_config_t * /*config*/, |
| 63 | audio_devices_t * /*device*/, |
| 64 | const String8 & /*address*/, |
| 65 | audio_source_t /*source*/, |
| 66 | audio_input_flags_t /*flags*/) override { |
| 67 | if (module >= mNextModuleHandle) { |
| 68 | ALOGE("%s: Module handle %d has not been allocated yet (next is %d)", |
| 69 | __func__, module, mNextModuleHandle); |
| 70 | return BAD_VALUE; |
| 71 | } |
| 72 | *input = mNextIoHandle++; |
| 73 | return NO_ERROR; |
| 74 | } |
| 75 | |
| 76 | status_t createAudioPatch(const struct audio_patch *patch, |
| 77 | audio_patch_handle_t *handle, |
| 78 | int /*delayMs*/) override { |
Dean Wheatley | 514b431 | 2020-06-17 21:45:00 +1000 | [diff] [blame] | 79 | auto iter = mActivePatches.find(*handle); |
| 80 | if (iter != mActivePatches.end()) { |
| 81 | mActivePatches.erase(*handle); |
| 82 | } |
jiabin | 38b2c5d | 2019-09-26 17:56:44 -0700 | [diff] [blame] | 83 | *handle = mNextPatchHandle++; |
| 84 | mActivePatches.insert(std::make_pair(*handle, *patch)); |
| 85 | return NO_ERROR; |
| 86 | } |
| 87 | |
| 88 | status_t releaseAudioPatch(audio_patch_handle_t handle, |
| 89 | int /*delayMs*/) override { |
| 90 | if (mActivePatches.erase(handle) != 1) { |
| 91 | if (handle >= mNextPatchHandle) { |
| 92 | ALOGE("%s: Patch handle %d has not been allocated yet (next is %d)", |
| 93 | __func__, handle, mNextPatchHandle); |
| 94 | } else { |
| 95 | ALOGE("%s: Attempt to release patch %d twice", __func__, handle); |
| 96 | } |
| 97 | return BAD_VALUE; |
| 98 | } |
| 99 | return NO_ERROR; |
| 100 | } |
| 101 | |
Mikhail Naganov | a30ec14 | 2020-03-24 09:32:34 -0700 | [diff] [blame] | 102 | void onAudioPortListUpdate() override { |
| 103 | ++mAudioPortListUpdateCount; |
| 104 | } |
| 105 | |
jiabin | c004863 | 2023-04-27 22:04:31 +0000 | [diff] [blame] | 106 | status_t setDeviceConnectedState(const struct audio_port_v7 *port, |
| 107 | media::DeviceConnectedState state) override { |
| 108 | if (state == media::DeviceConnectedState::CONNECTED) { |
Mikhail Naganov | ddc5f31 | 2022-06-11 00:47:52 +0000 | [diff] [blame] | 109 | mConnectedDevicePorts.push_back(*port); |
jiabin | c004863 | 2023-04-27 22:04:31 +0000 | [diff] [blame] | 110 | } else if (state == media::DeviceConnectedState::DISCONNECTED){ |
Mikhail Naganov | ddc5f31 | 2022-06-11 00:47:52 +0000 | [diff] [blame] | 111 | mDisconnectedDevicePorts.push_back(*port); |
| 112 | } |
Mikhail Naganov | 516d398 | 2022-02-01 23:53:59 +0000 | [diff] [blame] | 113 | return NO_ERROR; |
| 114 | } |
| 115 | |
jiabin | 38b2c5d | 2019-09-26 17:56:44 -0700 | [diff] [blame] | 116 | // Helper methods for tests |
| 117 | size_t getActivePatchesCount() const { return mActivePatches.size(); } |
| 118 | |
| 119 | const struct audio_patch *getLastAddedPatch() const { |
| 120 | if (mActivePatches.empty()) { |
| 121 | return nullptr; |
| 122 | } |
| 123 | auto it = --mActivePatches.end(); |
| 124 | return &it->second; |
| 125 | }; |
| 126 | |
Mikhail Naganov | 21762fa | 2020-03-05 16:28:57 -0800 | [diff] [blame] | 127 | audio_module_handle_t peekNextModuleHandle() const { return mNextModuleHandle; } |
| 128 | |
| 129 | void swapAllowedModuleNames(std::set<std::string>&& names = {}) { |
| 130 | mAllowedModuleNames.swap(names); |
| 131 | } |
| 132 | |
Mikhail Naganov | a30ec14 | 2020-03-24 09:32:34 -0700 | [diff] [blame] | 133 | size_t getAudioPortListUpdateCount() const { return mAudioPortListUpdateCount; } |
| 134 | |
Jean-Michel Trivi | 9a6b9ad | 2020-10-22 16:46:43 -0700 | [diff] [blame] | 135 | void onRoutingUpdated() override { |
| 136 | mRoutingUpdatedUpdateCount++; |
| 137 | } |
| 138 | |
| 139 | void resetRoutingUpdatedCounter() { |
| 140 | mRoutingUpdatedUpdateCount = 0; |
| 141 | } |
| 142 | |
| 143 | size_t getRoutingUpdatedCounter() const { |
Jean-Michel Trivi | 78f2b30 | 2022-04-15 18:18:41 +0000 | [diff] [blame] | 144 | return mRoutingUpdatedUpdateCount; |
| 145 | } |
| 146 | |
| 147 | void onVolumeRangeInitRequest() override { |
| 148 | |
| 149 | } |
Jean-Michel Trivi | 9a6b9ad | 2020-10-22 16:46:43 -0700 | [diff] [blame] | 150 | |
jiabin | f042b9b | 2021-05-07 23:46:28 +0000 | [diff] [blame] | 151 | status_t updateSecondaryOutputs( |
| 152 | const TrackSecondaryOutputsMap& trackSecondaryOutputs __unused) override { |
| 153 | return NO_ERROR; |
| 154 | } |
| 155 | |
Mikhail Naganov | ddc5f31 | 2022-06-11 00:47:52 +0000 | [diff] [blame] | 156 | size_t getConnectedDevicePortCount() const { |
| 157 | return mConnectedDevicePorts.size(); |
| 158 | } |
| 159 | |
| 160 | const struct audio_port_v7 *getLastConnectedDevicePort() const { |
| 161 | if (mConnectedDevicePorts.empty()) { |
| 162 | return nullptr; |
| 163 | } |
| 164 | auto it = --mConnectedDevicePorts.end(); |
| 165 | return &(*it); |
| 166 | } |
| 167 | |
| 168 | size_t getDisconnectedDevicePortCount() const { |
| 169 | return mDisconnectedDevicePorts.size(); |
| 170 | } |
| 171 | |
| 172 | const struct audio_port_v7 *getLastDisconnectedDevicePort() const { |
| 173 | if (mDisconnectedDevicePorts.empty()) { |
| 174 | return nullptr; |
| 175 | } |
| 176 | auto it = --mDisconnectedDevicePorts.end(); |
| 177 | return &(*it); |
| 178 | } |
| 179 | |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 180 | String8 getParameters(audio_io_handle_t /* ioHandle */, const String8& /* keys*/ ) override { |
| 181 | AudioParameter mAudioParameters; |
| 182 | std::string formats; |
| 183 | for (const auto& f : mSupportedFormats) { |
| 184 | if (!formats.empty()) formats += AUDIO_PARAMETER_VALUE_LIST_SEPARATOR; |
| 185 | formats += audio_format_to_string(f); |
| 186 | } |
| 187 | mAudioParameters.add( |
| 188 | String8(AudioParameter::keyStreamSupportedFormats), |
| 189 | String8(formats.c_str())); |
| 190 | mAudioParameters.addInt(String8(AudioParameter::keyStreamSupportedSamplingRates), 48000); |
| 191 | std::string channelMasks; |
| 192 | for (const auto& cm : mSupportedChannelMasks) { |
jiabin | 3ff8d7d | 2022-12-13 06:27:44 +0000 | [diff] [blame] | 193 | if (!audio_channel_mask_is_valid(cm)) { |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 194 | continue; |
| 195 | } |
| 196 | if (!channelMasks.empty()) channelMasks += AUDIO_PARAMETER_VALUE_LIST_SEPARATOR; |
| 197 | channelMasks += audio_channel_mask_to_string(cm); |
| 198 | } |
| 199 | mAudioParameters.add( |
| 200 | String8(AudioParameter::keyStreamSupportedChannels), String8(channelMasks.c_str())); |
| 201 | return mAudioParameters.toString(); |
| 202 | } |
| 203 | |
jiabin | 12537fc | 2023-10-12 17:56:08 +0000 | [diff] [blame] | 204 | status_t getAudioMixPort(const struct audio_port_v7 *devicePort __unused, |
| 205 | struct audio_port_v7 *mixPort) override { |
| 206 | mixPort->num_audio_profiles = 0; |
| 207 | for (auto format : mSupportedFormats) { |
| 208 | const int i = mixPort->num_audio_profiles; |
| 209 | mixPort->audio_profiles[i].format = format; |
| 210 | mixPort->audio_profiles[i].num_sample_rates = 1; |
| 211 | mixPort->audio_profiles[i].sample_rates[0] = 48000; |
| 212 | mixPort->audio_profiles[i].num_channel_masks = 0; |
| 213 | for (const auto& cm : mSupportedChannelMasks) { |
| 214 | if (audio_channel_mask_is_valid(cm)) { |
| 215 | mixPort->audio_profiles[i].channel_masks[ |
| 216 | mixPort->audio_profiles[i].num_channel_masks++] = cm; |
| 217 | } |
| 218 | } |
| 219 | mixPort->num_audio_profiles++; |
| 220 | } |
| 221 | return NO_ERROR; |
| 222 | } |
| 223 | |
jiabin | 220eea1 | 2024-05-17 17:55:20 +0000 | [diff] [blame^] | 224 | status_t setTracksInternalMute( |
| 225 | const std::vector<media::TrackInternalMuteInfo>& tracksInternalMute) override { |
| 226 | for (const auto& trackInternalMute : tracksInternalMute) { |
| 227 | mTracksInternalMute[(audio_port_handle_t)trackInternalMute.portId] = |
| 228 | trackInternalMute.muted; |
| 229 | } |
| 230 | return NO_ERROR; |
| 231 | } |
| 232 | |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 233 | void addSupportedFormat(audio_format_t format) { |
| 234 | mSupportedFormats.insert(format); |
| 235 | } |
| 236 | |
| 237 | void addSupportedChannelMask(audio_channel_mask_t channelMask) { |
| 238 | mSupportedChannelMasks.insert(channelMask); |
| 239 | } |
| 240 | |
jiabin | 220eea1 | 2024-05-17 17:55:20 +0000 | [diff] [blame^] | 241 | bool getTrackInternalMute(audio_port_handle_t portId) { |
| 242 | auto it = mTracksInternalMute.find(portId); |
| 243 | return it == mTracksInternalMute.end() ? false : it->second; |
| 244 | } |
| 245 | |
jiabin | 38b2c5d | 2019-09-26 17:56:44 -0700 | [diff] [blame] | 246 | private: |
| 247 | audio_module_handle_t mNextModuleHandle = AUDIO_MODULE_HANDLE_NONE + 1; |
| 248 | audio_io_handle_t mNextIoHandle = AUDIO_IO_HANDLE_NONE + 1; |
| 249 | audio_patch_handle_t mNextPatchHandle = AUDIO_PATCH_HANDLE_NONE + 1; |
| 250 | std::map<audio_patch_handle_t, struct audio_patch> mActivePatches; |
Mikhail Naganov | 21762fa | 2020-03-05 16:28:57 -0800 | [diff] [blame] | 251 | std::set<std::string> mAllowedModuleNames; |
Mikhail Naganov | a30ec14 | 2020-03-24 09:32:34 -0700 | [diff] [blame] | 252 | size_t mAudioPortListUpdateCount = 0; |
Jean-Michel Trivi | 9a6b9ad | 2020-10-22 16:46:43 -0700 | [diff] [blame] | 253 | size_t mRoutingUpdatedUpdateCount = 0; |
Mikhail Naganov | ddc5f31 | 2022-06-11 00:47:52 +0000 | [diff] [blame] | 254 | std::vector<struct audio_port_v7> mConnectedDevicePorts; |
| 255 | std::vector<struct audio_port_v7> mDisconnectedDevicePorts; |
jiabin | a84c3d3 | 2022-12-02 18:59:55 +0000 | [diff] [blame] | 256 | std::set<audio_format_t> mSupportedFormats; |
| 257 | std::set<audio_channel_mask_t> mSupportedChannelMasks; |
jiabin | 220eea1 | 2024-05-17 17:55:20 +0000 | [diff] [blame^] | 258 | std::map<audio_port_handle_t, bool> mTracksInternalMute; |
jiabin | 38b2c5d | 2019-09-26 17:56:44 -0700 | [diff] [blame] | 259 | }; |
| 260 | |
| 261 | } // namespace android |