• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
setDeviceConnectedState(const struct audio_port_v7 * port __unused,bool connected __unused)106     status_t setDeviceConnectedState(
107             const struct audio_port_v7 *port __unused, bool connected __unused) override {
108         return NO_ERROR;
109     }
110 
111     // Helper methods for tests
getActivePatchesCount()112     size_t getActivePatchesCount() const { return mActivePatches.size(); }
113 
getLastAddedPatch()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 
peekNextModuleHandle()122     audio_module_handle_t peekNextModuleHandle() const { return mNextModuleHandle; }
123 
124     void swapAllowedModuleNames(std::set<std::string>&& names = {}) {
125         mAllowedModuleNames.swap(names);
126     }
127 
getAudioPortListUpdateCount()128     size_t getAudioPortListUpdateCount() const { return mAudioPortListUpdateCount; }
129 
addSupportedFormat(audio_format_t)130     virtual void addSupportedFormat(audio_format_t /* format */) {}
131 
onRoutingUpdated()132     void onRoutingUpdated() override {
133         mRoutingUpdatedUpdateCount++;
134     }
135 
resetRoutingUpdatedCounter()136     void resetRoutingUpdatedCounter() {
137         mRoutingUpdatedUpdateCount = 0;
138     }
139 
getRoutingUpdatedCounter()140     size_t getRoutingUpdatedCounter() const {
141         return mRoutingUpdatedUpdateCount;
142     }
143 
onVolumeRangeInitRequest()144     void onVolumeRangeInitRequest() override {
145 
146     }
147 
updateSecondaryOutputs(const TrackSecondaryOutputsMap & trackSecondaryOutputs __unused)148     status_t updateSecondaryOutputs(
149             const TrackSecondaryOutputsMap& trackSecondaryOutputs __unused) override {
150         return NO_ERROR;
151     }
152 
153 private:
154     audio_module_handle_t mNextModuleHandle = AUDIO_MODULE_HANDLE_NONE + 1;
155     audio_io_handle_t mNextIoHandle = AUDIO_IO_HANDLE_NONE + 1;
156     audio_patch_handle_t mNextPatchHandle = AUDIO_PATCH_HANDLE_NONE + 1;
157     std::map<audio_patch_handle_t, struct audio_patch> mActivePatches;
158     std::set<std::string> mAllowedModuleNames;
159     size_t mAudioPortListUpdateCount = 0;
160     size_t mRoutingUpdatedUpdateCount = 0;
161 };
162 
163 } // namespace android
164