blob: adef8f1558f15adf1ba00780c5a8ad107eed3f75 [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,
Eric Laurentf1f22e72021-07-13 14:04:14 +020040 audio_config_t * /*halConfig*/,
41 audio_config_base_t * /*mixerConfig*/,
jiabinc0106832019-10-24 14:58:31 -070042 const sp<DeviceDescriptorBase>& /*device*/,
jiabin38b2c5d2019-09-26 17:56:44 -070043 uint32_t * /*latencyMs*/,
44 audio_output_flags_t /*flags*/) override {
45 if (module >= mNextModuleHandle) {
46 ALOGE("%s: Module handle %d has not been allocated yet (next is %d)",
47 __func__, module, mNextModuleHandle);
48 return BAD_VALUE;
49 }
50 *output = mNextIoHandle++;
51 return NO_ERROR;
52 }
53
54 audio_io_handle_t openDuplicateOutput(audio_io_handle_t /*output1*/,
55 audio_io_handle_t /*output2*/) override {
56 audio_io_handle_t id = mNextIoHandle++;
57 return id;
58 }
59
60 status_t openInput(audio_module_handle_t module,
61 audio_io_handle_t *input,
62 audio_config_t * /*config*/,
63 audio_devices_t * /*device*/,
64 const String8 & /*address*/,
65 audio_source_t /*source*/,
66 audio_input_flags_t /*flags*/) override {
67 if (module >= mNextModuleHandle) {
68 ALOGE("%s: Module handle %d has not been allocated yet (next is %d)",
69 __func__, module, mNextModuleHandle);
70 return BAD_VALUE;
71 }
72 *input = mNextIoHandle++;
73 return NO_ERROR;
74 }
75
76 status_t createAudioPatch(const struct audio_patch *patch,
77 audio_patch_handle_t *handle,
78 int /*delayMs*/) override {
Dean Wheatley514b4312020-06-17 21:45:00 +100079 auto iter = mActivePatches.find(*handle);
80 if (iter != mActivePatches.end()) {
81 mActivePatches.erase(*handle);
82 }
jiabin38b2c5d2019-09-26 17:56:44 -070083 *handle = mNextPatchHandle++;
84 mActivePatches.insert(std::make_pair(*handle, *patch));
85 return NO_ERROR;
86 }
87
88 status_t releaseAudioPatch(audio_patch_handle_t handle,
89 int /*delayMs*/) override {
90 if (mActivePatches.erase(handle) != 1) {
91 if (handle >= mNextPatchHandle) {
92 ALOGE("%s: Patch handle %d has not been allocated yet (next is %d)",
93 __func__, handle, mNextPatchHandle);
94 } else {
95 ALOGE("%s: Attempt to release patch %d twice", __func__, handle);
96 }
97 return BAD_VALUE;
98 }
99 return NO_ERROR;
100 }
101
Mikhail Naganova30ec142020-03-24 09:32:34 -0700102 void onAudioPortListUpdate() override {
103 ++mAudioPortListUpdateCount;
104 }
105
Mikhail Naganov516d3982022-02-01 23:53:59 +0000106 status_t setDeviceConnectedState(
107 const struct audio_port_v7 *port __unused, bool connected __unused) override {
108 return NO_ERROR;
109 }
110
jiabin38b2c5d2019-09-26 17:56:44 -0700111 // Helper methods for tests
112 size_t getActivePatchesCount() const { return mActivePatches.size(); }
113
114 const struct audio_patch *getLastAddedPatch() const {
115 if (mActivePatches.empty()) {
116 return nullptr;
117 }
118 auto it = --mActivePatches.end();
119 return &it->second;
120 };
121
Mikhail Naganov21762fa2020-03-05 16:28:57 -0800122 audio_module_handle_t peekNextModuleHandle() const { return mNextModuleHandle; }
123
124 void swapAllowedModuleNames(std::set<std::string>&& names = {}) {
125 mAllowedModuleNames.swap(names);
126 }
127
Mikhail Naganova30ec142020-03-24 09:32:34 -0700128 size_t getAudioPortListUpdateCount() const { return mAudioPortListUpdateCount; }
129
Kriti Dangef6be8f2020-11-05 11:58:19 +0100130 virtual void addSupportedFormat(audio_format_t /* format */) {}
131
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -0700132 void onRoutingUpdated() override {
133 mRoutingUpdatedUpdateCount++;
134 }
135
136 void resetRoutingUpdatedCounter() {
137 mRoutingUpdatedUpdateCount = 0;
138 }
139
140 size_t getRoutingUpdatedCounter() const {
141 return mRoutingUpdatedUpdateCount; }
142
jiabinf042b9b2021-05-07 23:46:28 +0000143 status_t updateSecondaryOutputs(
144 const TrackSecondaryOutputsMap& trackSecondaryOutputs __unused) override {
145 return NO_ERROR;
146 }
147
jiabin38b2c5d2019-09-26 17:56:44 -0700148private:
149 audio_module_handle_t mNextModuleHandle = AUDIO_MODULE_HANDLE_NONE + 1;
150 audio_io_handle_t mNextIoHandle = AUDIO_IO_HANDLE_NONE + 1;
151 audio_patch_handle_t mNextPatchHandle = AUDIO_PATCH_HANDLE_NONE + 1;
152 std::map<audio_patch_handle_t, struct audio_patch> mActivePatches;
Mikhail Naganov21762fa2020-03-05 16:28:57 -0800153 std::set<std::string> mAllowedModuleNames;
Mikhail Naganova30ec142020-03-24 09:32:34 -0700154 size_t mAudioPortListUpdateCount = 0;
Jean-Michel Trivi9a6b9ad2020-10-22 16:46:43 -0700155 size_t mRoutingUpdatedUpdateCount = 0;
jiabin38b2c5d2019-09-26 17:56:44 -0700156};
157
158} // namespace android