1 /*
2 * Copyright (C) 2015 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 #define LOG_TAG "APM::AudioPatch"
18 //#define LOG_NDEBUG 0
19
20 #include "AudioPatch.h"
21 #include "AudioGain.h"
22 #include "TypeConverter.h"
23
24 #include <log/log.h>
25 #include <utils/String8.h>
26
27 namespace android {
28
AudioPatch(const struct audio_patch * patch,uid_t uid)29 AudioPatch::AudioPatch(const struct audio_patch *patch, uid_t uid) :
30 mHandle(HandleGenerator<audio_patch_handle_t>::getNextHandle()),
31 mPatch(*patch),
32 mUid(uid),
33 mAfPatchHandle(AUDIO_PATCH_HANDLE_NONE)
34 {
35 }
36
dumpPatchEndpoints(String8 * dst,int spaces,const char * prefix,int count,const audio_port_config * cfgs)37 static void dumpPatchEndpoints(
38 String8 *dst, int spaces, const char *prefix, int count, const audio_port_config *cfgs)
39 {
40 for (int i = 0; i < count; ++i) {
41 const audio_port_config &cfg = cfgs[i];
42 dst->appendFormat("%*s [%s %d] ", spaces, "", prefix, i + 1);
43 if (cfg.type == AUDIO_PORT_TYPE_DEVICE) {
44 dst->appendFormat("Device ID %d %s", cfg.id, toString(cfg.ext.device.type).c_str());
45 } else {
46 dst->appendFormat("Mix ID %d I/O handle %d", cfg.id, cfg.ext.mix.handle);
47 }
48 dst->append("\n");
49 }
50 }
51
dump(String8 * dst,int spaces,int index) const52 void AudioPatch::dump(String8 *dst, int spaces, int index) const
53 {
54 dst->appendFormat("%*sPatch %d: owner uid %4d, handle %2d, af handle %2d\n",
55 spaces, "", index + 1, mUid, mHandle, mAfPatchHandle);
56 dumpPatchEndpoints(dst, spaces, "src ", mPatch.num_sources, mPatch.sources);
57 dumpPatchEndpoints(dst, spaces, "sink", mPatch.num_sinks, mPatch.sinks);
58 }
59
addAudioPatch(audio_patch_handle_t handle,const sp<AudioPatch> & patch)60 status_t AudioPatchCollection::addAudioPatch(audio_patch_handle_t handle,
61 const sp<AudioPatch>& patch)
62 {
63 ssize_t index = indexOfKey(handle);
64
65 if (index >= 0) {
66 ALOGW("addAudioPatch() patch %d already in", handle);
67 return ALREADY_EXISTS;
68 }
69 add(handle, patch);
70 ALOGV("addAudioPatch() handle %d af handle %d num_sources %d num_sinks %d source handle %d"
71 "sink handle %d",
72 handle, patch->mAfPatchHandle, patch->mPatch.num_sources, patch->mPatch.num_sinks,
73 patch->mPatch.sources[0].id, patch->mPatch.sinks[0].id);
74 return NO_ERROR;
75 }
76
removeAudioPatch(audio_patch_handle_t handle)77 status_t AudioPatchCollection::removeAudioPatch(audio_patch_handle_t handle)
78 {
79 ssize_t index = indexOfKey(handle);
80
81 if (index < 0) {
82 ALOGW("removeAudioPatch() patch %d not in", handle);
83 return ALREADY_EXISTS;
84 }
85 ALOGV("removeAudioPatch() handle %d af handle %d", handle, valueAt(index)->mAfPatchHandle);
86 removeItemsAt(index);
87 return NO_ERROR;
88 }
89
listAudioPatches(unsigned int * num_patches,struct audio_patch * patches) const90 status_t AudioPatchCollection::listAudioPatches(unsigned int *num_patches,
91 struct audio_patch *patches) const
92 {
93 if (num_patches == NULL || (*num_patches != 0 && patches == NULL)) {
94 return BAD_VALUE;
95 }
96 ALOGV("listAudioPatches() num_patches %d patches %p available patches %zu",
97 *num_patches, patches, size());
98 if (patches == NULL) {
99 *num_patches = 0;
100 }
101
102 size_t patchesWritten = 0;
103 size_t patchesMax = *num_patches;
104 *num_patches = 0;
105 for (size_t patchIndex = 0; patchIndex < size(); patchIndex++) {
106 // do not report patches with AUDIO_DEVICE_IN_STUB as source or
107 // AUDIO_DEVICE_OUT_STUB as sink as those devices are used by stub HALs by convention
108 const sp<AudioPatch> patch = valueAt(patchIndex);
109 bool skip = false;
110 for (size_t srcIndex = 0; srcIndex < patch->mPatch.num_sources && !skip; srcIndex++) {
111 if (patch->mPatch.sources[srcIndex].type == AUDIO_PORT_TYPE_DEVICE &&
112 patch->mPatch.sources[srcIndex].ext.device.type == AUDIO_DEVICE_IN_STUB) {
113 skip = true;
114 }
115 }
116 for (size_t sinkIndex = 0; sinkIndex < patch->mPatch.num_sinks && !skip; sinkIndex++) {
117 if (patch->mPatch.sinks[sinkIndex].type == AUDIO_PORT_TYPE_DEVICE &&
118 patch->mPatch.sinks[sinkIndex].ext.device.type == AUDIO_DEVICE_OUT_STUB) {
119 skip = true;
120 }
121 }
122 if (skip) {
123 continue; // to next audio patch
124 }
125 if (patchesWritten < patchesMax) {
126 patches[patchesWritten] = patch->mPatch;
127 patches[patchesWritten++].id = patch->mHandle;
128 }
129 (*num_patches)++;
130 ALOGV("listAudioPatches() patch %zu num_sources %d num_sinks %d",
131 patchIndex, patch->mPatch.num_sources, patch->mPatch.num_sinks);
132 }
133
134 ALOGV("listAudioPatches() got %zu patches needed %d", patchesWritten, *num_patches);
135 return NO_ERROR;
136 }
137
dump(String8 * dst) const138 void AudioPatchCollection::dump(String8 *dst) const
139 {
140 dst->append("\nAudio Patches:\n");
141 for (size_t i = 0; i < size(); i++) {
142 valueAt(i)->dump(dst, 2, i);
143 }
144 }
145
146 } // namespace android
147