blob: afe6f2071bd31f7399da95b6da6cd6c962f64b7e [file] [log] [blame]
jiabinb3f98042019-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>
18
19#include <system/audio.h>
20#include <utils/Log.h>
21#include <utils/String8.h>
22
23#include "AudioPolicyTestClient.h"
24
25namespace android {
26
27class AudioPolicyManagerTestClient : public AudioPolicyTestClient {
28public:
29 // AudioPolicyClientInterface implementation
30 audio_module_handle_t loadHwModule(const char * /*name*/) override {
31 return mNextModuleHandle++;
32 }
33
34 status_t openOutput(audio_module_handle_t module,
35 audio_io_handle_t *output,
36 audio_config_t * /*config*/,
37 audio_devices_t * /*devices*/,
38 const String8 & /*address*/,
39 uint32_t * /*latencyMs*/,
40 audio_output_flags_t /*flags*/) override {
41 if (module >= mNextModuleHandle) {
42 ALOGE("%s: Module handle %d has not been allocated yet (next is %d)",
43 __func__, module, mNextModuleHandle);
44 return BAD_VALUE;
45 }
46 *output = mNextIoHandle++;
47 return NO_ERROR;
48 }
49
50 audio_io_handle_t openDuplicateOutput(audio_io_handle_t /*output1*/,
51 audio_io_handle_t /*output2*/) override {
52 audio_io_handle_t id = mNextIoHandle++;
53 return id;
54 }
55
56 status_t openInput(audio_module_handle_t module,
57 audio_io_handle_t *input,
58 audio_config_t * /*config*/,
59 audio_devices_t * /*device*/,
60 const String8 & /*address*/,
61 audio_source_t /*source*/,
62 audio_input_flags_t /*flags*/) override {
63 if (module >= mNextModuleHandle) {
64 ALOGE("%s: Module handle %d has not been allocated yet (next is %d)",
65 __func__, module, mNextModuleHandle);
66 return BAD_VALUE;
67 }
68 *input = mNextIoHandle++;
69 return NO_ERROR;
70 }
71
72 status_t createAudioPatch(const struct audio_patch *patch,
73 audio_patch_handle_t *handle,
74 int /*delayMs*/) override {
75 *handle = mNextPatchHandle++;
76 mActivePatches.insert(std::make_pair(*handle, *patch));
77 return NO_ERROR;
78 }
79
80 status_t releaseAudioPatch(audio_patch_handle_t handle,
81 int /*delayMs*/) override {
82 if (mActivePatches.erase(handle) != 1) {
83 if (handle >= mNextPatchHandle) {
84 ALOGE("%s: Patch handle %d has not been allocated yet (next is %d)",
85 __func__, handle, mNextPatchHandle);
86 } else {
87 ALOGE("%s: Attempt to release patch %d twice", __func__, handle);
88 }
89 return BAD_VALUE;
90 }
91 return NO_ERROR;
92 }
93
94 // Helper methods for tests
95 size_t getActivePatchesCount() const { return mActivePatches.size(); }
96
97 const struct audio_patch *getLastAddedPatch() const {
98 if (mActivePatches.empty()) {
99 return nullptr;
100 }
101 auto it = --mActivePatches.end();
102 return &it->second;
103 };
104
105private:
106 audio_module_handle_t mNextModuleHandle = AUDIO_MODULE_HANDLE_NONE + 1;
107 audio_io_handle_t mNextIoHandle = AUDIO_IO_HANDLE_NONE + 1;
108 audio_patch_handle_t mNextPatchHandle = AUDIO_PATCH_HANDLE_NONE + 1;
109 std::map<audio_patch_handle_t, struct audio_patch> mActivePatches;
110};
111
112} // namespace android