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