| 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 { | 
| Dean Wheatley | 514b431 | 2020-06-17 21:45:00 +1000 | [diff] [blame] | 78 |         auto iter = mActivePatches.find(*handle); | 
 | 79 |         if (iter != mActivePatches.end()) { | 
 | 80 |             mActivePatches.erase(*handle); | 
 | 81 |         } | 
| jiabin | 38b2c5d | 2019-09-26 17:56:44 -0700 | [diff] [blame] | 82 |         *handle = mNextPatchHandle++; | 
 | 83 |         mActivePatches.insert(std::make_pair(*handle, *patch)); | 
 | 84 |         return NO_ERROR; | 
 | 85 |     } | 
 | 86 |  | 
 | 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 |  | 
| Mikhail Naganov | a30ec14 | 2020-03-24 09:32:34 -0700 | [diff] [blame] | 101 |     void onAudioPortListUpdate() override { | 
 | 102 |         ++mAudioPortListUpdateCount; | 
 | 103 |     } | 
 | 104 |  | 
| jiabin | 38b2c5d | 2019-09-26 17:56:44 -0700 | [diff] [blame] | 105 |     // Helper methods for tests | 
 | 106 |     size_t getActivePatchesCount() const { return mActivePatches.size(); } | 
 | 107 |  | 
 | 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 |  | 
| Mikhail Naganov | 21762fa | 2020-03-05 16:28:57 -0800 | [diff] [blame] | 116 |     audio_module_handle_t peekNextModuleHandle() const { return mNextModuleHandle; } | 
 | 117 |  | 
 | 118 |     void swapAllowedModuleNames(std::set<std::string>&& names = {}) { | 
 | 119 |         mAllowedModuleNames.swap(names); | 
 | 120 |     } | 
 | 121 |  | 
| Mikhail Naganov | a30ec14 | 2020-03-24 09:32:34 -0700 | [diff] [blame] | 122 |     size_t getAudioPortListUpdateCount() const { return mAudioPortListUpdateCount; } | 
 | 123 |  | 
| Kriti Dang | ef6be8f | 2020-11-05 11:58:19 +0100 | [diff] [blame] | 124 |     virtual void addSupportedFormat(audio_format_t /* format */) {} | 
 | 125 |  | 
| jiabin | 38b2c5d | 2019-09-26 17:56:44 -0700 | [diff] [blame] | 126 | private: | 
 | 127 |     audio_module_handle_t mNextModuleHandle = AUDIO_MODULE_HANDLE_NONE + 1; | 
 | 128 |     audio_io_handle_t mNextIoHandle = AUDIO_IO_HANDLE_NONE + 1; | 
 | 129 |     audio_patch_handle_t mNextPatchHandle = AUDIO_PATCH_HANDLE_NONE + 1; | 
 | 130 |     std::map<audio_patch_handle_t, struct audio_patch> mActivePatches; | 
| Mikhail Naganov | 21762fa | 2020-03-05 16:28:57 -0800 | [diff] [blame] | 131 |     std::set<std::string> mAllowedModuleNames; | 
| Mikhail Naganov | a30ec14 | 2020-03-24 09:32:34 -0700 | [diff] [blame] | 132 |     size_t mAudioPortListUpdateCount = 0; | 
| jiabin | 38b2c5d | 2019-09-26 17:56:44 -0700 | [diff] [blame] | 133 | }; | 
 | 134 |  | 
 | 135 | } // namespace android |