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 *,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 * /*config*/, 41 const sp<DeviceDescriptorBase>& /*device*/, 42 uint32_t * /*latencyMs*/, 43 audio_output_flags_t /*flags*/) override { 44 if (module >= mNextModuleHandle) { 45 ALOGE("%s: Module handle %d has not been allocated yet (next is %d)", 46 __func__, module, mNextModuleHandle); 47 return BAD_VALUE; 48 } 49 *output = mNextIoHandle++; 50 return NO_ERROR; 51 } 52 openDuplicateOutput(audio_io_handle_t,audio_io_handle_t)53 audio_io_handle_t openDuplicateOutput(audio_io_handle_t /*output1*/, 54 audio_io_handle_t /*output2*/) override { 55 audio_io_handle_t id = mNextIoHandle++; 56 return id; 57 } 58 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)59 status_t openInput(audio_module_handle_t module, 60 audio_io_handle_t *input, 61 audio_config_t * /*config*/, 62 audio_devices_t * /*device*/, 63 const String8 & /*address*/, 64 audio_source_t /*source*/, 65 audio_input_flags_t /*flags*/) override { 66 if (module >= mNextModuleHandle) { 67 ALOGE("%s: Module handle %d has not been allocated yet (next is %d)", 68 __func__, module, mNextModuleHandle); 69 return BAD_VALUE; 70 } 71 *input = mNextIoHandle++; 72 return NO_ERROR; 73 } 74 createAudioPatch(const struct audio_patch * patch,audio_patch_handle_t * handle,int)75 status_t createAudioPatch(const struct audio_patch *patch, 76 audio_patch_handle_t *handle, 77 int /*delayMs*/) override { 78 auto iter = mActivePatches.find(*handle); 79 if (iter != mActivePatches.end()) { 80 mActivePatches.erase(*handle); 81 } 82 *handle = mNextPatchHandle++; 83 mActivePatches.insert(std::make_pair(*handle, *patch)); 84 return NO_ERROR; 85 } 86 releaseAudioPatch(audio_patch_handle_t handle,int)87 status_t releaseAudioPatch(audio_patch_handle_t handle, 88 int /*delayMs*/) override { 89 if (mActivePatches.erase(handle) != 1) { 90 if (handle >= mNextPatchHandle) { 91 ALOGE("%s: Patch handle %d has not been allocated yet (next is %d)", 92 __func__, handle, mNextPatchHandle); 93 } else { 94 ALOGE("%s: Attempt to release patch %d twice", __func__, handle); 95 } 96 return BAD_VALUE; 97 } 98 return NO_ERROR; 99 } 100 onAudioPortListUpdate()101 void onAudioPortListUpdate() override { 102 ++mAudioPortListUpdateCount; 103 } 104 105 // Helper methods for tests getActivePatchesCount()106 size_t getActivePatchesCount() const { return mActivePatches.size(); } 107 getLastAddedPatch()108 const struct audio_patch *getLastAddedPatch() const { 109 if (mActivePatches.empty()) { 110 return nullptr; 111 } 112 auto it = --mActivePatches.end(); 113 return &it->second; 114 }; 115 peekNextModuleHandle()116 audio_module_handle_t peekNextModuleHandle() const { return mNextModuleHandle; } 117 118 void swapAllowedModuleNames(std::set<std::string>&& names = {}) { 119 mAllowedModuleNames.swap(names); 120 } 121 getAudioPortListUpdateCount()122 size_t getAudioPortListUpdateCount() const { return mAudioPortListUpdateCount; } 123 124 private: 125 audio_module_handle_t mNextModuleHandle = AUDIO_MODULE_HANDLE_NONE + 1; 126 audio_io_handle_t mNextIoHandle = AUDIO_IO_HANDLE_NONE + 1; 127 audio_patch_handle_t mNextPatchHandle = AUDIO_PATCH_HANDLE_NONE + 1; 128 std::map<audio_patch_handle_t, struct audio_patch> mActivePatches; 129 std::set<std::string> mAllowedModuleNames; 130 size_t mAudioPortListUpdateCount = 0; 131 }; 132 133 } // namespace android 134