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 *num_patches = 0; 132 for (size_t patchIndex = 0; patchIndex < size(); patchIndex++) { 133 // do not report patches with AUDIO_DEVICE_IN_STUB as source or 134 // AUDIO_DEVICE_OUT_STUB as sink as those devices are used by stub HALs by convention 135 const sp<AudioPatch> patch = valueAt(patchIndex); 136 bool skip = false; 137 for (size_t srcIndex = 0; srcIndex < patch->mPatch.num_sources && !skip; srcIndex++) { 138 if (patch->mPatch.sources[srcIndex].type == AUDIO_PORT_TYPE_DEVICE && 139 patch->mPatch.sources[srcIndex].ext.device.type == AUDIO_DEVICE_IN_STUB) { 140 skip = true; 141 } 142 } 143 for (size_t sinkIndex = 0; sinkIndex < patch->mPatch.num_sinks && !skip; sinkIndex++) { 144 if (patch->mPatch.sinks[sinkIndex].type == AUDIO_PORT_TYPE_DEVICE && 145 patch->mPatch.sinks[sinkIndex].ext.device.type == AUDIO_DEVICE_OUT_STUB) { 146 skip = true; 147 } 148 } 149 if (skip) { 150 continue; // to next audio patch 151 } 152 if (patchesWritten < patchesMax) { 153 patches[patchesWritten] = patch->mPatch; 154 patches[patchesWritten++].id = patch->mHandle; 155 } 156 (*num_patches)++; 157 ALOGV("listAudioPatches() patch %zu num_sources %d num_sinks %d", 158 patchIndex, patch->mPatch.num_sources, patch->mPatch.num_sinks); 159 } 160 161 ALOGV("listAudioPatches() got %zu patches needed %d", patchesWritten, *num_patches); 162 return NO_ERROR; 163} 164 165status_t AudioPatchCollection::dump(int fd) const 166{ 167 const size_t SIZE = 256; 168 char buffer[SIZE]; 169 snprintf(buffer, SIZE, "\nAudio Patches:\n"); 170 write(fd, buffer, strlen(buffer)); 171 for (size_t i = 0; i < size(); i++) { 172 valueAt(i)->dump(fd, 2, i); 173 } 174 return NO_ERROR; 175} 176 177}; // namespace android 178