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> |
| 18 | |
| 19 | #include <system/audio.h> |
| 20 | #include <utils/Log.h> |
| 21 | #include <utils/String8.h> |
| 22 | |
| 23 | #include "AudioPolicyTestClient.h" |
| 24 | |
| 25 | namespace android { |
| 26 | |
| 27 | class AudioPolicyManagerTestClient : public AudioPolicyTestClient { |
| 28 | public: |
| 29 | // AudioPolicyClientInterface implementation |
| 30 | audio_module_handle_t loadHwModule(const char * /*name*/) override { |
| 31 | return mNextModuleHandle++; |
| 32 | } |
| 33 | |
| 34 | status_t openOutput(audio_module_handle_t module, |
| 35 | audio_io_handle_t *output, |
| 36 | audio_config_t * /*config*/, |
jiabin | c010683 | 2019-10-24 14:58:31 -0700 | [diff] [blame^] | 37 | const sp<DeviceDescriptorBase>& /*device*/, |
jiabin | 38b2c5d | 2019-09-26 17:56:44 -0700 | [diff] [blame] | 38 | uint32_t * /*latencyMs*/, |
| 39 | audio_output_flags_t /*flags*/) override { |
| 40 | if (module >= mNextModuleHandle) { |
| 41 | ALOGE("%s: Module handle %d has not been allocated yet (next is %d)", |
| 42 | __func__, module, mNextModuleHandle); |
| 43 | return BAD_VALUE; |
| 44 | } |
| 45 | *output = mNextIoHandle++; |
| 46 | return NO_ERROR; |
| 47 | } |
| 48 | |
| 49 | audio_io_handle_t openDuplicateOutput(audio_io_handle_t /*output1*/, |
| 50 | audio_io_handle_t /*output2*/) override { |
| 51 | audio_io_handle_t id = mNextIoHandle++; |
| 52 | return id; |
| 53 | } |
| 54 | |
| 55 | status_t openInput(audio_module_handle_t module, |
| 56 | audio_io_handle_t *input, |
| 57 | audio_config_t * /*config*/, |
| 58 | audio_devices_t * /*device*/, |
| 59 | const String8 & /*address*/, |
| 60 | audio_source_t /*source*/, |
| 61 | audio_input_flags_t /*flags*/) override { |
| 62 | if (module >= mNextModuleHandle) { |
| 63 | ALOGE("%s: Module handle %d has not been allocated yet (next is %d)", |
| 64 | __func__, module, mNextModuleHandle); |
| 65 | return BAD_VALUE; |
| 66 | } |
| 67 | *input = mNextIoHandle++; |
| 68 | return NO_ERROR; |
| 69 | } |
| 70 | |
| 71 | status_t createAudioPatch(const struct audio_patch *patch, |
| 72 | audio_patch_handle_t *handle, |
| 73 | int /*delayMs*/) override { |
| 74 | *handle = mNextPatchHandle++; |
| 75 | mActivePatches.insert(std::make_pair(*handle, *patch)); |
| 76 | return NO_ERROR; |
| 77 | } |
| 78 | |
| 79 | status_t releaseAudioPatch(audio_patch_handle_t handle, |
| 80 | int /*delayMs*/) override { |
| 81 | if (mActivePatches.erase(handle) != 1) { |
| 82 | if (handle >= mNextPatchHandle) { |
| 83 | ALOGE("%s: Patch handle %d has not been allocated yet (next is %d)", |
| 84 | __func__, handle, mNextPatchHandle); |
| 85 | } else { |
| 86 | ALOGE("%s: Attempt to release patch %d twice", __func__, handle); |
| 87 | } |
| 88 | return BAD_VALUE; |
| 89 | } |
| 90 | return NO_ERROR; |
| 91 | } |
| 92 | |
| 93 | // Helper methods for tests |
| 94 | size_t getActivePatchesCount() const { return mActivePatches.size(); } |
| 95 | |
| 96 | const struct audio_patch *getLastAddedPatch() const { |
| 97 | if (mActivePatches.empty()) { |
| 98 | return nullptr; |
| 99 | } |
| 100 | auto it = --mActivePatches.end(); |
| 101 | return &it->second; |
| 102 | }; |
| 103 | |
| 104 | private: |
| 105 | audio_module_handle_t mNextModuleHandle = AUDIO_MODULE_HANDLE_NONE + 1; |
| 106 | audio_io_handle_t mNextIoHandle = AUDIO_IO_HANDLE_NONE + 1; |
| 107 | audio_patch_handle_t mNextPatchHandle = AUDIO_PATCH_HANDLE_NONE + 1; |
| 108 | std::map<audio_patch_handle_t, struct audio_patch> mActivePatches; |
| 109 | }; |
| 110 | |
| 111 | } // namespace android |