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 return NO_ERROR; 74 } 75 createAudioPatch(const struct audio_patch * patch,audio_patch_handle_t * handle,int)76 status_t createAudioPatch(const struct audio_patch *patch, 77 audio_patch_handle_t *handle, 78 int /*delayMs*/) override { 79 auto iter = mActivePatches.find(*handle); 80 if (iter != mActivePatches.end()) { 81 mActivePatches.erase(*handle); 82 } 83 *handle = mNextPatchHandle++; 84 mActivePatches.insert(std::make_pair(*handle, *patch)); 85 return NO_ERROR; 86 } 87 releaseAudioPatch(audio_patch_handle_t handle,int)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 onAudioPortListUpdate()102 void onAudioPortListUpdate() override { 103 ++mAudioPortListUpdateCount; 104 } 105 setDeviceConnectedState(const struct audio_port_v7 * port,media::DeviceConnectedState state)106 status_t setDeviceConnectedState(const struct audio_port_v7 *port, 107 media::DeviceConnectedState state) override { 108 if (state == media::DeviceConnectedState::CONNECTED) { 109 mConnectedDevicePorts.push_back(*port); 110 } else if (state == media::DeviceConnectedState::DISCONNECTED){ 111 mDisconnectedDevicePorts.push_back(*port); 112 } 113 return NO_ERROR; 114 } 115 116 // Helper methods for tests getActivePatchesCount()117 size_t getActivePatchesCount() const { return mActivePatches.size(); } 118 getLastAddedPatch()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 peekNextModuleHandle()127 audio_module_handle_t peekNextModuleHandle() const { return mNextModuleHandle; } 128 129 void swapAllowedModuleNames(std::set<std::string>&& names = {}) { 130 mAllowedModuleNames.swap(names); 131 } 132 getAudioPortListUpdateCount()133 size_t getAudioPortListUpdateCount() const { return mAudioPortListUpdateCount; } 134 onRoutingUpdated()135 void onRoutingUpdated() override { 136 mRoutingUpdatedUpdateCount++; 137 } 138 resetRoutingUpdatedCounter()139 void resetRoutingUpdatedCounter() { 140 mRoutingUpdatedUpdateCount = 0; 141 } 142 getRoutingUpdatedCounter()143 size_t getRoutingUpdatedCounter() const { 144 return mRoutingUpdatedUpdateCount; 145 } 146 onVolumeRangeInitRequest()147 void onVolumeRangeInitRequest() override { 148 149 } 150 updateSecondaryOutputs(const TrackSecondaryOutputsMap & trackSecondaryOutputs __unused)151 status_t updateSecondaryOutputs( 152 const TrackSecondaryOutputsMap& trackSecondaryOutputs __unused) override { 153 return NO_ERROR; 154 } 155 getConnectedDevicePortCount()156 size_t getConnectedDevicePortCount() const { 157 return mConnectedDevicePorts.size(); 158 } 159 getLastConnectedDevicePort()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 getDisconnectedDevicePortCount()168 size_t getDisconnectedDevicePortCount() const { 169 return mDisconnectedDevicePorts.size(); 170 } 171 getLastDisconnectedDevicePort()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 getParameters(audio_io_handle_t,const String8 &)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) { 193 if (!audio_channel_mask_is_valid(cm)) { 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 addSupportedFormat(audio_format_t format)204 void addSupportedFormat(audio_format_t format) { 205 mSupportedFormats.insert(format); 206 } 207 addSupportedChannelMask(audio_channel_mask_t channelMask)208 void addSupportedChannelMask(audio_channel_mask_t channelMask) { 209 mSupportedChannelMasks.insert(channelMask); 210 } 211 212 private: 213 audio_module_handle_t mNextModuleHandle = AUDIO_MODULE_HANDLE_NONE + 1; 214 audio_io_handle_t mNextIoHandle = AUDIO_IO_HANDLE_NONE + 1; 215 audio_patch_handle_t mNextPatchHandle = AUDIO_PATCH_HANDLE_NONE + 1; 216 std::map<audio_patch_handle_t, struct audio_patch> mActivePatches; 217 std::set<std::string> mAllowedModuleNames; 218 size_t mAudioPortListUpdateCount = 0; 219 size_t mRoutingUpdatedUpdateCount = 0; 220 std::vector<struct audio_port_v7> mConnectedDevicePorts; 221 std::vector<struct audio_port_v7> mDisconnectedDevicePorts; 222 std::set<audio_format_t> mSupportedFormats; 223 std::set<audio_channel_mask_t> mSupportedChannelMasks; 224 }; 225 226 } // namespace android 227