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 <cutils/atomic.h>
26 #include <utils/String8.h>
27 
28 namespace android {
29 
30 int32_t volatile AudioPatch::mNextUniqueId = 1;
31 
AudioPatch(const struct audio_patch * patch,uid_t uid)32 AudioPatch::AudioPatch(const struct audio_patch *patch, uid_t uid) :
33     mHandle(static_cast<audio_patch_handle_t>(android_atomic_inc(&mNextUniqueId))),
34     mPatch(*patch),
35     mUid(uid),
36     mAfPatchHandle(AUDIO_PATCH_HANDLE_NONE)
37 {
38 }
39 
dump(int fd,int spaces,int index) const40 status_t AudioPatch::dump(int fd, int spaces, int index) const
41 {
42     const size_t SIZE = 256;
43     char buffer[SIZE];
44     String8 result;
45 
46     snprintf(buffer, SIZE, "%*sAudio patch %d:\n", spaces, "", index+1);
47     result.append(buffer);
48     snprintf(buffer, SIZE, "%*s- handle: %2d\n", spaces, "", mHandle);
49     result.append(buffer);
50     snprintf(buffer, SIZE, "%*s- audio flinger handle: %2d\n", spaces, "", mAfPatchHandle);
51     result.append(buffer);
52     snprintf(buffer, SIZE, "%*s- owner uid: %2d\n", spaces, "", mUid);
53     result.append(buffer);
54     snprintf(buffer, SIZE, "%*s- %d sources:\n", spaces, "", mPatch.num_sources);
55     result.append(buffer);
56     for (size_t i = 0; i < mPatch.num_sources; i++) {
57         if (mPatch.sources[i].type == AUDIO_PORT_TYPE_DEVICE) {
58             std::string device;
59             deviceToString(mPatch.sources[i].ext.device.type, device);
60             snprintf(buffer, SIZE, "%*s- Device ID %d %s\n", spaces + 2, "",
61                      mPatch.sources[i].id,
62                      device.c_str());
63         } else {
64             snprintf(buffer, SIZE, "%*s- Mix ID %d I/O handle %d\n", spaces + 2, "",
65                      mPatch.sources[i].id, mPatch.sources[i].ext.mix.handle);
66         }
67         result.append(buffer);
68     }
69     snprintf(buffer, SIZE, "%*s- %d sinks:\n", spaces, "", mPatch.num_sinks);
70     result.append(buffer);
71     for (size_t i = 0; i < mPatch.num_sinks; i++) {
72         if (mPatch.sinks[i].type == AUDIO_PORT_TYPE_DEVICE) {
73             std::string device;
74             deviceToString(mPatch.sinks[i].ext.device.type, device);
75             snprintf(buffer, SIZE, "%*s- Device ID %d %s\n", spaces + 2, "",
76                      mPatch.sinks[i].id,
77                      device.c_str());
78         } else {
79             snprintf(buffer, SIZE, "%*s- Mix ID %d I/O handle %d\n", spaces + 2, "",
80                      mPatch.sinks[i].id, mPatch.sinks[i].ext.mix.handle);
81         }
82         result.append(buffer);
83     }
84 
85     write(fd, result.string(), result.size());
86     return NO_ERROR;
87 }
88 
addAudioPatch(audio_patch_handle_t handle,const sp<AudioPatch> & patch)89 status_t AudioPatchCollection::addAudioPatch(audio_patch_handle_t handle,
90                                              const sp<AudioPatch>& patch)
91 {
92     ssize_t index = indexOfKey(handle);
93 
94     if (index >= 0) {
95         ALOGW("addAudioPatch() patch %d already in", handle);
96         return ALREADY_EXISTS;
97     }
98     add(handle, patch);
99     ALOGV("addAudioPatch() handle %d af handle %d num_sources %d num_sinks %d source handle %d"
100             "sink handle %d",
101           handle, patch->mAfPatchHandle, patch->mPatch.num_sources, patch->mPatch.num_sinks,
102           patch->mPatch.sources[0].id, patch->mPatch.sinks[0].id);
103     return NO_ERROR;
104 }
105 
removeAudioPatch(audio_patch_handle_t handle)106 status_t AudioPatchCollection::removeAudioPatch(audio_patch_handle_t handle)
107 {
108     ssize_t index = indexOfKey(handle);
109 
110     if (index < 0) {
111         ALOGW("removeAudioPatch() patch %d not in", handle);
112         return ALREADY_EXISTS;
113     }
114     ALOGV("removeAudioPatch() handle %d af handle %d", handle, valueAt(index)->mAfPatchHandle);
115     removeItemsAt(index);
116     return NO_ERROR;
117 }
118 
listAudioPatches(unsigned int * num_patches,struct audio_patch * patches) const119 status_t AudioPatchCollection::listAudioPatches(unsigned int *num_patches,
120                                                 struct audio_patch *patches) const
121 {
122     if (num_patches == NULL || (*num_patches != 0 && patches == NULL)) {
123         return BAD_VALUE;
124     }
125     ALOGV("listAudioPatches() num_patches %d patches %p available patches %zu",
126           *num_patches, patches, size());
127     if (patches == NULL) {
128         *num_patches = 0;
129     }
130 
131     size_t patchesWritten = 0;
132     size_t patchesMax = *num_patches;
133     *num_patches = 0;
134     for (size_t patchIndex = 0; patchIndex < size(); patchIndex++) {
135         // do not report patches with AUDIO_DEVICE_IN_STUB as source or
136         // AUDIO_DEVICE_OUT_STUB as sink as those devices are used by stub HALs by convention
137         const sp<AudioPatch> patch = valueAt(patchIndex);
138         bool skip = false;
139         for (size_t srcIndex = 0; srcIndex < patch->mPatch.num_sources && !skip; srcIndex++) {
140             if (patch->mPatch.sources[srcIndex].type == AUDIO_PORT_TYPE_DEVICE &&
141                     patch->mPatch.sources[srcIndex].ext.device.type == AUDIO_DEVICE_IN_STUB) {
142                 skip = true;
143             }
144         }
145         for (size_t sinkIndex = 0; sinkIndex < patch->mPatch.num_sinks && !skip; sinkIndex++) {
146             if (patch->mPatch.sinks[sinkIndex].type == AUDIO_PORT_TYPE_DEVICE &&
147                     patch->mPatch.sinks[sinkIndex].ext.device.type == AUDIO_DEVICE_OUT_STUB) {
148                 skip = true;
149             }
150         }
151         if (skip) {
152             continue; // to next audio patch
153         }
154         if (patchesWritten < patchesMax) {
155             patches[patchesWritten] = patch->mPatch;
156             patches[patchesWritten++].id = patch->mHandle;
157         }
158         (*num_patches)++;
159         ALOGV("listAudioPatches() patch %zu num_sources %d num_sinks %d",
160               patchIndex, patch->mPatch.num_sources, patch->mPatch.num_sinks);
161     }
162 
163     ALOGV("listAudioPatches() got %zu patches needed %d", patchesWritten, *num_patches);
164     return NO_ERROR;
165 }
166 
dump(int fd) const167 status_t AudioPatchCollection::dump(int fd) const
168 {
169     const size_t SIZE = 256;
170     char buffer[SIZE];
171     snprintf(buffer, SIZE, "\nAudio Patches:\n");
172     write(fd, buffer, strlen(buffer));
173     for (size_t i = 0; i < size(); i++) {
174         valueAt(i)->dump(fd, 2, i);
175     }
176     return NO_ERROR;
177 }
178 
179 }; // namespace android
180