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 #include <set>
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
loadHwModule(const char * name)31     audio_module_handle_t loadHwModule(const char* name) override {
32         if (!mAllowedModuleNames.empty() && !mAllowedModuleNames.count(name)) {
33             return AUDIO_MODULE_HANDLE_NONE;
34         }
35         return mNextModuleHandle++;
36     }
37 
openOutput(audio_module_handle_t module,audio_io_handle_t * output,audio_config_t *,audio_config_base_t *,const sp<DeviceDescriptorBase> &,uint32_t *,audio_output_flags_t)38     status_t openOutput(audio_module_handle_t module,
39                         audio_io_handle_t *output,
40                         audio_config_t * /*halConfig*/,
41                         audio_config_base_t * /*mixerConfig*/,
42                         const sp<DeviceDescriptorBase>& /*device*/,
43                         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 
openDuplicateOutput(audio_io_handle_t,audio_io_handle_t)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 
openInput(audio_module_handle_t module,audio_io_handle_t * input,audio_config_t *,audio_devices_t *,const String8 &,audio_source_t,audio_input_flags_t)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 
createAudioPatch(const struct audio_patch * patch,audio_patch_handle_t * handle,int)76     status_t createAudioPatch(const struct audio_patch *patch,
77                               audio_patch_handle_t *handle,
78                               int /*delayMs*/) override {
79         auto iter = mActivePatches.find(*handle);
80         if (iter != mActivePatches.end()) {
81             mActivePatches.erase(*handle);
82         }
83         *handle = mNextPatchHandle++;
84         mActivePatches.insert(std::make_pair(*handle, *patch));
85         return NO_ERROR;
86     }
87 
releaseAudioPatch(audio_patch_handle_t handle,int)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 
onAudioPortListUpdate()102     void onAudioPortListUpdate() override {
103         ++mAudioPortListUpdateCount;
104     }
105 
106     // Helper methods for tests
getActivePatchesCount()107     size_t getActivePatchesCount() const { return mActivePatches.size(); }
108 
getLastAddedPatch()109     const struct audio_patch *getLastAddedPatch() const {
110         if (mActivePatches.empty()) {
111             return nullptr;
112         }
113         auto it = --mActivePatches.end();
114         return &it->second;
115     };
116 
peekNextModuleHandle()117     audio_module_handle_t peekNextModuleHandle() const { return mNextModuleHandle; }
118 
119     void swapAllowedModuleNames(std::set<std::string>&& names = {}) {
120         mAllowedModuleNames.swap(names);
121     }
122 
getAudioPortListUpdateCount()123     size_t getAudioPortListUpdateCount() const { return mAudioPortListUpdateCount; }
124 
addSupportedFormat(audio_format_t)125     virtual void addSupportedFormat(audio_format_t /* format */) {}
126 
onRoutingUpdated()127     void onRoutingUpdated() override {
128         mRoutingUpdatedUpdateCount++;
129     }
130 
resetRoutingUpdatedCounter()131     void resetRoutingUpdatedCounter() {
132         mRoutingUpdatedUpdateCount = 0;
133     }
134 
getRoutingUpdatedCounter()135     size_t getRoutingUpdatedCounter() const {
136         return mRoutingUpdatedUpdateCount; }
137 
updateSecondaryOutputs(const TrackSecondaryOutputsMap & trackSecondaryOutputs __unused)138     status_t updateSecondaryOutputs(
139             const TrackSecondaryOutputsMap& trackSecondaryOutputs __unused) override {
140         return NO_ERROR;
141     }
142 
143 private:
144     audio_module_handle_t mNextModuleHandle = AUDIO_MODULE_HANDLE_NONE + 1;
145     audio_io_handle_t mNextIoHandle = AUDIO_IO_HANDLE_NONE + 1;
146     audio_patch_handle_t mNextPatchHandle = AUDIO_PATCH_HANDLE_NONE + 1;
147     std::map<audio_patch_handle_t, struct audio_patch> mActivePatches;
148     std::set<std::string> mAllowedModuleNames;
149     size_t mAudioPortListUpdateCount = 0;
150     size_t mRoutingUpdatedUpdateCount = 0;
151 };
152 
153 } // namespace android
154