blob: e1721ea50e9d2ac642ebef5f4a6dda9668f2d563 [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 {
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
Mikhail Naganova30ec142020-03-24 09:32:34 -070097 void onAudioPortListUpdate() override {
98 ++mAudioPortListUpdateCount;
99 }
100
jiabin38b2c5d2019-09-26 17:56:44 -0700101 // Helper methods for tests
102 size_t getActivePatchesCount() const { return mActivePatches.size(); }
103
104 const struct audio_patch *getLastAddedPatch() const {
105 if (mActivePatches.empty()) {
106 return nullptr;
107 }
108 auto it = --mActivePatches.end();
109 return &it->second;
110 };
111
Mikhail Naganov21762fa2020-03-05 16:28:57 -0800112 audio_module_handle_t peekNextModuleHandle() const { return mNextModuleHandle; }
113
114 void swapAllowedModuleNames(std::set<std::string>&& names = {}) {
115 mAllowedModuleNames.swap(names);
116 }
117
Mikhail Naganova30ec142020-03-24 09:32:34 -0700118 size_t getAudioPortListUpdateCount() const { return mAudioPortListUpdateCount; }
119
jiabin38b2c5d2019-09-26 17:56:44 -0700120private:
121 audio_module_handle_t mNextModuleHandle = AUDIO_MODULE_HANDLE_NONE + 1;
122 audio_io_handle_t mNextIoHandle = AUDIO_IO_HANDLE_NONE + 1;
123 audio_patch_handle_t mNextPatchHandle = AUDIO_PATCH_HANDLE_NONE + 1;
124 std::map<audio_patch_handle_t, struct audio_patch> mActivePatches;
Mikhail Naganov21762fa2020-03-05 16:28:57 -0800125 std::set<std::string> mAllowedModuleNames;
Mikhail Naganova30ec142020-03-24 09:32:34 -0700126 size_t mAudioPortListUpdateCount = 0;
jiabin38b2c5d2019-09-26 17:56:44 -0700127};
128
129} // namespace android