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 <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 loadHwModule(const char * name)31 audio_module_handle_t loadHwModule(const char* name) override { 32 if (!mAllowedModuleNames.empty() && !mAllowedModuleNames.count(name)) { 33 return AUDIO_MODULE_HANDLE_NONE; 34 } 35 return mNextModuleHandle++; 36 } 37 openOutput(audio_module_handle_t module,audio_io_handle_t * output,audio_config_t *,audio_config_base_t *,const sp<DeviceDescriptorBase> &,uint32_t *,audio_output_flags_t)38 status_t openOutput(audio_module_handle_t module, 39 audio_io_handle_t *output, 40 audio_config_t * /*halConfig*/, 41 audio_config_base_t * /*mixerConfig*/, 42 const sp<DeviceDescriptorBase>& /*device*/, 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 openDuplicateOutput(audio_io_handle_t,audio_io_handle_t)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 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)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 mOpenedInputs.insert(*input); 74 ALOGD("%s: opened input %d", __func__, *input); 75 return NO_ERROR; 76 } 77 closeInput(audio_io_handle_t input)78 status_t closeInput(audio_io_handle_t input) override { 79 if (mOpenedInputs.erase(input) != 1) { 80 if (input >= mNextIoHandle) { 81 ALOGE("%s: I/O handle %d has not been allocated yet (next is %d)", 82 __func__, input, mNextIoHandle); 83 } else { 84 ALOGE("%s: Attempt to close input %d twice", __func__, input); 85 } 86 return BAD_VALUE; 87 } 88 ALOGD("%s: closed input %d", __func__, input); 89 return NO_ERROR; 90 } 91 createAudioPatch(const struct audio_patch * patch,audio_patch_handle_t * handle,int)92 status_t createAudioPatch(const struct audio_patch *patch, 93 audio_patch_handle_t *handle, 94 int /*delayMs*/) override { 95 auto iter = mActivePatches.find(*handle); 96 if (iter != mActivePatches.end()) { 97 mActivePatches.erase(*handle); 98 } 99 *handle = mNextPatchHandle++; 100 mActivePatches.insert(std::make_pair(*handle, *patch)); 101 return NO_ERROR; 102 } 103 releaseAudioPatch(audio_patch_handle_t handle,int)104 status_t releaseAudioPatch(audio_patch_handle_t handle, 105 int /*delayMs*/) override { 106 if (mActivePatches.erase(handle) != 1) { 107 if (handle >= mNextPatchHandle) { 108 ALOGE("%s: Patch handle %d has not been allocated yet (next is %d)", 109 __func__, handle, mNextPatchHandle); 110 } else { 111 ALOGE("%s: Attempt to release patch %d twice", __func__, handle); 112 } 113 return BAD_VALUE; 114 } 115 return NO_ERROR; 116 } 117 onAudioPortListUpdate()118 void onAudioPortListUpdate() override { 119 ++mAudioPortListUpdateCount; 120 } 121 setDeviceConnectedState(const struct audio_port_v7 * port,media::DeviceConnectedState state)122 status_t setDeviceConnectedState(const struct audio_port_v7 *port, 123 media::DeviceConnectedState state) override { 124 if (state == media::DeviceConnectedState::CONNECTED) { 125 mConnectedDevicePorts.push_back(*port); 126 } else if (state == media::DeviceConnectedState::DISCONNECTED){ 127 mDisconnectedDevicePorts.push_back(*port); 128 } 129 return NO_ERROR; 130 } 131 132 // Helper methods for tests getActivePatchesCount()133 size_t getActivePatchesCount() const { return mActivePatches.size(); } 134 getLastAddedPatch()135 const struct audio_patch *getLastAddedPatch() const { 136 if (mActivePatches.empty()) { 137 return nullptr; 138 } 139 auto it = --mActivePatches.end(); 140 return &it->second; 141 }; 142 getOpenedInputsCount()143 size_t getOpenedInputsCount() const { return mOpenedInputs.size(); } 144 peekNextModuleHandle()145 audio_module_handle_t peekNextModuleHandle() const { return mNextModuleHandle; } 146 147 void swapAllowedModuleNames(std::set<std::string>&& names = {}) { 148 mAllowedModuleNames.swap(names); 149 } 150 getAudioPortListUpdateCount()151 size_t getAudioPortListUpdateCount() const { return mAudioPortListUpdateCount; } 152 onRoutingUpdated()153 void onRoutingUpdated() override { 154 mRoutingUpdatedUpdateCount++; 155 } 156 resetRoutingUpdatedCounter()157 void resetRoutingUpdatedCounter() { 158 mRoutingUpdatedUpdateCount = 0; 159 } 160 getRoutingUpdatedCounter()161 size_t getRoutingUpdatedCounter() const { 162 return mRoutingUpdatedUpdateCount; 163 } 164 onVolumeRangeInitRequest()165 void onVolumeRangeInitRequest() override { 166 167 } 168 updateSecondaryOutputs(const TrackSecondaryOutputsMap & trackSecondaryOutputs __unused)169 status_t updateSecondaryOutputs( 170 const TrackSecondaryOutputsMap& trackSecondaryOutputs __unused) override { 171 return NO_ERROR; 172 } 173 getConnectedDevicePortCount()174 size_t getConnectedDevicePortCount() const { 175 return mConnectedDevicePorts.size(); 176 } 177 getLastConnectedDevicePort()178 const struct audio_port_v7 *getLastConnectedDevicePort() const { 179 if (mConnectedDevicePorts.empty()) { 180 return nullptr; 181 } 182 auto it = --mConnectedDevicePorts.end(); 183 return &(*it); 184 } 185 getDisconnectedDevicePortCount()186 size_t getDisconnectedDevicePortCount() const { 187 return mDisconnectedDevicePorts.size(); 188 } 189 getLastDisconnectedDevicePort()190 const struct audio_port_v7 *getLastDisconnectedDevicePort() const { 191 if (mDisconnectedDevicePorts.empty()) { 192 return nullptr; 193 } 194 auto it = --mDisconnectedDevicePorts.end(); 195 return &(*it); 196 } 197 getParameters(audio_io_handle_t,const String8 &)198 String8 getParameters(audio_io_handle_t /* ioHandle */, const String8& /* keys*/ ) override { 199 AudioParameter mAudioParameters; 200 std::string formats; 201 for (const auto& f : mSupportedFormats) { 202 if (!formats.empty()) formats += AUDIO_PARAMETER_VALUE_LIST_SEPARATOR; 203 formats += audio_format_to_string(f); 204 } 205 mAudioParameters.add( 206 String8(AudioParameter::keyStreamSupportedFormats), 207 String8(formats.c_str())); 208 mAudioParameters.addInt(String8(AudioParameter::keyStreamSupportedSamplingRates), 48000); 209 std::string channelMasks; 210 for (const auto& cm : mSupportedChannelMasks) { 211 if (!audio_channel_mask_is_valid(cm)) { 212 continue; 213 } 214 if (!channelMasks.empty()) channelMasks += AUDIO_PARAMETER_VALUE_LIST_SEPARATOR; 215 channelMasks += audio_channel_mask_to_string(cm); 216 } 217 mAudioParameters.add( 218 String8(AudioParameter::keyStreamSupportedChannels), String8(channelMasks.c_str())); 219 return mAudioParameters.toString(); 220 } 221 getAudioMixPort(const struct audio_port_v7 * devicePort __unused,struct audio_port_v7 * mixPort)222 status_t getAudioMixPort(const struct audio_port_v7 *devicePort __unused, 223 struct audio_port_v7 *mixPort) override { 224 mixPort->num_audio_profiles = 0; 225 for (auto format : mSupportedFormats) { 226 const int i = mixPort->num_audio_profiles; 227 mixPort->audio_profiles[i].format = format; 228 mixPort->audio_profiles[i].num_sample_rates = 1; 229 mixPort->audio_profiles[i].sample_rates[0] = 48000; 230 mixPort->audio_profiles[i].num_channel_masks = 0; 231 for (const auto& cm : mSupportedChannelMasks) { 232 if (audio_channel_mask_is_valid(cm)) { 233 mixPort->audio_profiles[i].channel_masks[ 234 mixPort->audio_profiles[i].num_channel_masks++] = cm; 235 } 236 } 237 mixPort->num_audio_profiles++; 238 } 239 return NO_ERROR; 240 } 241 setTracksInternalMute(const std::vector<media::TrackInternalMuteInfo> & tracksInternalMute)242 status_t setTracksInternalMute( 243 const std::vector<media::TrackInternalMuteInfo>& tracksInternalMute) override { 244 for (const auto& trackInternalMute : tracksInternalMute) { 245 mTracksInternalMute[(audio_port_handle_t)trackInternalMute.portId] = 246 trackInternalMute.muted; 247 } 248 return NO_ERROR; 249 } 250 addSupportedFormat(audio_format_t format)251 void addSupportedFormat(audio_format_t format) { 252 mSupportedFormats.insert(format); 253 } 254 addSupportedChannelMask(audio_channel_mask_t channelMask)255 void addSupportedChannelMask(audio_channel_mask_t channelMask) { 256 mSupportedChannelMasks.insert(channelMask); 257 } 258 getTrackInternalMute(audio_port_handle_t portId)259 bool getTrackInternalMute(audio_port_handle_t portId) { 260 auto it = mTracksInternalMute.find(portId); 261 return it == mTracksInternalMute.end() ? false : it->second; 262 } 263 264 private: 265 audio_module_handle_t mNextModuleHandle = AUDIO_MODULE_HANDLE_NONE + 1; 266 audio_io_handle_t mNextIoHandle = AUDIO_IO_HANDLE_NONE + 1; 267 audio_patch_handle_t mNextPatchHandle = AUDIO_PATCH_HANDLE_NONE + 1; 268 std::map<audio_patch_handle_t, struct audio_patch> mActivePatches; 269 std::set<std::string> mAllowedModuleNames; 270 size_t mAudioPortListUpdateCount = 0; 271 size_t mRoutingUpdatedUpdateCount = 0; 272 std::vector<struct audio_port_v7> mConnectedDevicePorts; 273 std::vector<struct audio_port_v7> mDisconnectedDevicePorts; 274 std::set<audio_format_t> mSupportedFormats; 275 std::set<audio_channel_mask_t> mSupportedChannelMasks; 276 std::map<audio_port_handle_t, bool> mTracksInternalMute; 277 std::set<audio_io_handle_t> mOpenedInputs; 278 }; 279 280 } // namespace android 281