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