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, |
| 40 | audio_config_t * /*config*/, |
jiabin | c010683 | 2019-10-24 14:58:31 -0700 | [diff] [blame] | 41 | const sp<DeviceDescriptorBase>& /*device*/, |
jiabin | 38b2c5d | 2019-09-26 17:56:44 -0700 | [diff] [blame] | 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 | |
| 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 | |
| 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 | |
| 75 | status_t createAudioPatch(const struct audio_patch *patch, |
| 76 | audio_patch_handle_t *handle, |
| 77 | int /*delayMs*/) override { |
| 78 | *handle = mNextPatchHandle++; |
| 79 | mActivePatches.insert(std::make_pair(*handle, *patch)); |
| 80 | return NO_ERROR; |
| 81 | } |
| 82 | |
| 83 | status_t releaseAudioPatch(audio_patch_handle_t handle, |
| 84 | int /*delayMs*/) override { |
| 85 | if (mActivePatches.erase(handle) != 1) { |
| 86 | if (handle >= mNextPatchHandle) { |
| 87 | ALOGE("%s: Patch handle %d has not been allocated yet (next is %d)", |
| 88 | __func__, handle, mNextPatchHandle); |
| 89 | } else { |
| 90 | ALOGE("%s: Attempt to release patch %d twice", __func__, handle); |
| 91 | } |
| 92 | return BAD_VALUE; |
| 93 | } |
| 94 | return NO_ERROR; |
| 95 | } |
| 96 | |
| 97 | // Helper methods for tests |
| 98 | size_t getActivePatchesCount() const { return mActivePatches.size(); } |
| 99 | |
| 100 | const struct audio_patch *getLastAddedPatch() const { |
| 101 | if (mActivePatches.empty()) { |
| 102 | return nullptr; |
| 103 | } |
| 104 | auto it = --mActivePatches.end(); |
| 105 | return &it->second; |
| 106 | }; |
| 107 | |
Mikhail Naganov | 21762fa | 2020-03-05 16:28:57 -0800 | [diff] [blame^] | 108 | audio_module_handle_t peekNextModuleHandle() const { return mNextModuleHandle; } |
| 109 | |
| 110 | void swapAllowedModuleNames(std::set<std::string>&& names = {}) { |
| 111 | mAllowedModuleNames.swap(names); |
| 112 | } |
| 113 | |
jiabin | 38b2c5d | 2019-09-26 17:56:44 -0700 | [diff] [blame] | 114 | private: |
| 115 | audio_module_handle_t mNextModuleHandle = AUDIO_MODULE_HANDLE_NONE + 1; |
| 116 | audio_io_handle_t mNextIoHandle = AUDIO_IO_HANDLE_NONE + 1; |
| 117 | audio_patch_handle_t mNextPatchHandle = AUDIO_PATCH_HANDLE_NONE + 1; |
| 118 | std::map<audio_patch_handle_t, struct audio_patch> mActivePatches; |
Mikhail Naganov | 21762fa | 2020-03-05 16:28:57 -0800 | [diff] [blame^] | 119 | std::set<std::string> mAllowedModuleNames; |
jiabin | 38b2c5d | 2019-09-26 17:56:44 -0700 | [diff] [blame] | 120 | }; |
| 121 | |
| 122 | } // namespace android |