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 "ConfigParsingUtils.h"
23 #include <cutils/log.h>
24 #include <utils/String8.h>
25 
26 namespace android {
27 
28 int32_t volatile AudioPatch::mNextUniqueId = 1;
29 
AudioPatch(const struct audio_patch * patch,uid_t uid)30 AudioPatch::AudioPatch(const struct audio_patch *patch, uid_t uid) :
31     mHandle(static_cast<audio_patch_handle_t>(android_atomic_inc(&mNextUniqueId))),
32     mPatch(*patch),
33     mUid(uid),
34     mAfPatchHandle(0)
35 {
36 }
37 
dump(int fd,int spaces,int index) const38 status_t AudioPatch::dump(int fd, int spaces, int index) const
39 {
40     const size_t SIZE = 256;
41     char buffer[SIZE];
42     String8 result;
43 
44     snprintf(buffer, SIZE, "%*sAudio patch %d:\n", spaces, "", index+1);
45     result.append(buffer);
46     snprintf(buffer, SIZE, "%*s- handle: %2d\n", spaces, "", mHandle);
47     result.append(buffer);
48     snprintf(buffer, SIZE, "%*s- audio flinger handle: %2d\n", spaces, "", mAfPatchHandle);
49     result.append(buffer);
50     snprintf(buffer, SIZE, "%*s- owner uid: %2d\n", spaces, "", mUid);
51     result.append(buffer);
52     snprintf(buffer, SIZE, "%*s- %d sources:\n", spaces, "", mPatch.num_sources);
53     result.append(buffer);
54     for (size_t i = 0; i < mPatch.num_sources; i++) {
55         if (mPatch.sources[i].type == AUDIO_PORT_TYPE_DEVICE) {
56             snprintf(buffer, SIZE, "%*s- Device ID %d %s\n", spaces + 2, "",
57                      mPatch.sources[i].id, ConfigParsingUtils::enumToString(sDeviceTypeToEnumTable,
58                                                         ARRAY_SIZE(sDeviceTypeToEnumTable),
59                                                         mPatch.sources[i].ext.device.type));
60         } else {
61             snprintf(buffer, SIZE, "%*s- Mix ID %d I/O handle %d\n", spaces + 2, "",
62                      mPatch.sources[i].id, mPatch.sources[i].ext.mix.handle);
63         }
64         result.append(buffer);
65     }
66     snprintf(buffer, SIZE, "%*s- %d sinks:\n", spaces, "", mPatch.num_sinks);
67     result.append(buffer);
68     for (size_t i = 0; i < mPatch.num_sinks; i++) {
69         if (mPatch.sinks[i].type == AUDIO_PORT_TYPE_DEVICE) {
70             snprintf(buffer, SIZE, "%*s- Device ID %d %s\n", spaces + 2, "",
71                      mPatch.sinks[i].id, ConfigParsingUtils::enumToString(sDeviceTypeToEnumTable,
72                                                         ARRAY_SIZE(sDeviceTypeToEnumTable),
73                                                         mPatch.sinks[i].ext.device.type));
74         } else {
75             snprintf(buffer, SIZE, "%*s- Mix ID %d I/O handle %d\n", spaces + 2, "",
76                      mPatch.sinks[i].id, mPatch.sinks[i].ext.mix.handle);
77         }
78         result.append(buffer);
79     }
80 
81     write(fd, result.string(), result.size());
82     return NO_ERROR;
83 }
84 
addAudioPatch(audio_patch_handle_t handle,const sp<AudioPatch> & patch)85 status_t AudioPatchCollection::addAudioPatch(audio_patch_handle_t handle,
86                                              const sp<AudioPatch>& patch)
87 {
88     ssize_t index = indexOfKey(handle);
89 
90     if (index >= 0) {
91         ALOGW("addAudioPatch() patch %d already in", handle);
92         return ALREADY_EXISTS;
93     }
94     add(handle, patch);
95     ALOGV("addAudioPatch() handle %d af handle %d num_sources %d num_sinks %d source handle %d"
96             "sink handle %d",
97           handle, patch->mAfPatchHandle, patch->mPatch.num_sources, patch->mPatch.num_sinks,
98           patch->mPatch.sources[0].id, patch->mPatch.sinks[0].id);
99     return NO_ERROR;
100 }
101 
removeAudioPatch(audio_patch_handle_t handle)102 status_t AudioPatchCollection::removeAudioPatch(audio_patch_handle_t handle)
103 {
104     ssize_t index = indexOfKey(handle);
105 
106     if (index < 0) {
107         ALOGW("removeAudioPatch() patch %d not in", handle);
108         return ALREADY_EXISTS;
109     }
110     ALOGV("removeAudioPatch() handle %d af handle %d", handle, valueAt(index)->mAfPatchHandle);
111     removeItemsAt(index);
112     return NO_ERROR;
113 }
114 
listAudioPatches(unsigned int * num_patches,struct audio_patch * patches) const115 status_t AudioPatchCollection::listAudioPatches(unsigned int *num_patches,
116                                                 struct audio_patch *patches) const
117 {
118     if (num_patches == NULL || (*num_patches != 0 && patches == NULL)) {
119         return BAD_VALUE;
120     }
121     ALOGV("listAudioPatches() num_patches %d patches %p available patches %zu",
122           *num_patches, patches, size());
123     if (patches == NULL) {
124         *num_patches = 0;
125     }
126 
127     size_t patchesWritten = 0;
128     size_t patchesMax = *num_patches;
129     for (size_t i = 0; i  < size() && patchesWritten < patchesMax; i++) {
130         const sp<AudioPatch>  patch = valueAt(i);
131         patches[patchesWritten] = patch->mPatch;
132         patches[patchesWritten++].id = patch->mHandle;
133         ALOGV("listAudioPatches() patch %zu num_sources %d num_sinks %d",
134               i, patch->mPatch.num_sources, patch->mPatch.num_sinks);
135     }
136     *num_patches = size();
137 
138     ALOGV("listAudioPatches() got %zu patches needed %d", patchesWritten, *num_patches);
139     return NO_ERROR;
140 }
141 
dump(int fd) const142 status_t AudioPatchCollection::dump(int fd) const
143 {
144     const size_t SIZE = 256;
145     char buffer[SIZE];
146     snprintf(buffer, SIZE, "\nAudio Patches:\n");
147     write(fd, buffer, strlen(buffer));
148     for (size_t i = 0; i < size(); i++) {
149         valueAt(i)->dump(fd, 2, i);
150     }
151     return NO_ERROR;
152 }
153 
154 }; // namespace android
155