blob: 433a6ff0083455695f6c0fabe45c518cad4fce2d [file] [log] [blame]
jiabin38b2c5d2019-09-26 17:56:44 -07001/*
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 Naganov21762fa2020-03-05 16:28:57 -080018#include <set>
jiabin38b2c5d2019-09-26 17:56:44 -070019
20#include <system/audio.h>
21#include <utils/Log.h>
22#include <utils/String8.h>
23
24#include "AudioPolicyTestClient.h"
25
26namespace android {
27
28class AudioPolicyManagerTestClient : public AudioPolicyTestClient {
29public:
30 // AudioPolicyClientInterface implementation
Mikhail Naganov21762fa2020-03-05 16:28:57 -080031 audio_module_handle_t loadHwModule(const char* name) override {
32 if (!mAllowedModuleNames.empty() && !mAllowedModuleNames.count(name)) {
33 return AUDIO_MODULE_HANDLE_NONE;
34 }
jiabin38b2c5d2019-09-26 17:56:44 -070035 return mNextModuleHandle++;
36 }
37
38 status_t openOutput(audio_module_handle_t module,
39 audio_io_handle_t *output,
40 audio_config_t * /*config*/,
jiabinc0106832019-10-24 14:58:31 -070041 const sp<DeviceDescriptorBase>& /*device*/,
jiabin38b2c5d2019-09-26 17:56:44 -070042 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 Wheatley514b4312020-06-17 21:45:00 +100078 auto iter = mActivePatches.find(*handle);
79 if (iter != mActivePatches.end()) {
80 mActivePatches.erase(*handle);
81 }
jiabin38b2c5d2019-09-26 17:56:44 -070082 *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 Naganova30ec142020-03-24 09:32:34 -0700101 void onAudioPortListUpdate() override {
102 ++mAudioPortListUpdateCount;
103 }
104
jiabin38b2c5d2019-09-26 17:56:44 -0700105 // 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 Naganov21762fa2020-03-05 16:28:57 -0800116 audio_module_handle_t peekNextModuleHandle() const { return mNextModuleHandle; }
117
118 void swapAllowedModuleNames(std::set<std::string>&& names = {}) {
119 mAllowedModuleNames.swap(names);
120 }
121
Mikhail Naganova30ec142020-03-24 09:32:34 -0700122 size_t getAudioPortListUpdateCount() const { return mAudioPortListUpdateCount; }
123
Kriti Dangef6be8f2020-11-05 11:58:19 +0100124 virtual void addSupportedFormat(audio_format_t /* format */) {}
125
jiabin38b2c5d2019-09-26 17:56:44 -0700126private:
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 Naganov21762fa2020-03-05 16:28:57 -0800131 std::set<std::string> mAllowedModuleNames;
Mikhail Naganova30ec142020-03-24 09:32:34 -0700132 size_t mAudioPortListUpdateCount = 0;
jiabin38b2c5d2019-09-26 17:56:44 -0700133};
134
135} // namespace android