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