198cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie/*
298cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie * Copyright (C) 2015 The Android Open Source Project
398cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie *
498cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie * Licensed under the Apache License, Version 2.0 (the "License");
598cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie * you may not use this file except in compliance with the License.
698cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie * You may obtain a copy of the License at
798cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie *
898cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie *      http://www.apache.org/licenses/LICENSE-2.0
998cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie *
1098cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie * Unless required by applicable law or agreed to in writing, software
1198cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie * distributed under the License is distributed on an "AS IS" BASIS,
1298cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1398cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie * See the License for the specific language governing permissions and
1498cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie * limitations under the License.
1598cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie */
1698cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie
1798cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie#define LOG_TAG "APM::AudioPatch"
1898cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie//#define LOG_NDEBUG 0
1998cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie
2098cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie#include "AudioPatch.h"
2153615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffie#include "AudioGain.h"
22a8ecc2c72ca26389bd6b0162181d60aaeaca8149François Gaffie#include "TypeConverter.h"
2398cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie#include <cutils/log.h>
2498cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie#include <utils/String8.h>
2598cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie
2698cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffienamespace android {
2798cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie
2898cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffieint32_t volatile AudioPatch::mNextUniqueId = 1;
2998cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie
3098cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois GaffieAudioPatch::AudioPatch(const struct audio_patch *patch, uid_t uid) :
3198cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie    mHandle(static_cast<audio_patch_handle_t>(android_atomic_inc(&mNextUniqueId))),
3298cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie    mPatch(*patch),
3398cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie    mUid(uid),
34a13cde98a880341f0a56d91da6364b093fb5d24eGlenn Kasten    mAfPatchHandle(AUDIO_PATCH_HANDLE_NONE)
3598cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie{
3698cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie}
3798cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie
3898cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffiestatus_t AudioPatch::dump(int fd, int spaces, int index) const
3998cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie{
4098cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie    const size_t SIZE = 256;
4198cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie    char buffer[SIZE];
4298cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie    String8 result;
4398cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie
4498cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie    snprintf(buffer, SIZE, "%*sAudio patch %d:\n", spaces, "", index+1);
4598cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie    result.append(buffer);
4698cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie    snprintf(buffer, SIZE, "%*s- handle: %2d\n", spaces, "", mHandle);
4798cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie    result.append(buffer);
4898cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie    snprintf(buffer, SIZE, "%*s- audio flinger handle: %2d\n", spaces, "", mAfPatchHandle);
4998cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie    result.append(buffer);
5098cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie    snprintf(buffer, SIZE, "%*s- owner uid: %2d\n", spaces, "", mUid);
5198cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie    result.append(buffer);
5298cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie    snprintf(buffer, SIZE, "%*s- %d sources:\n", spaces, "", mPatch.num_sources);
5398cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie    result.append(buffer);
5498cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie    for (size_t i = 0; i < mPatch.num_sources; i++) {
5598cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie        if (mPatch.sources[i].type == AUDIO_PORT_TYPE_DEVICE) {
56a8ecc2c72ca26389bd6b0162181d60aaeaca8149François Gaffie            std::string device;
57a8ecc2c72ca26389bd6b0162181d60aaeaca8149François Gaffie            DeviceConverter::toString(mPatch.sources[i].ext.device.type, device);
5898cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie            snprintf(buffer, SIZE, "%*s- Device ID %d %s\n", spaces + 2, "",
59a8ecc2c72ca26389bd6b0162181d60aaeaca8149François Gaffie                     mPatch.sources[i].id,
60a8ecc2c72ca26389bd6b0162181d60aaeaca8149François Gaffie                     device.c_str());
6198cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie        } else {
6298cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie            snprintf(buffer, SIZE, "%*s- Mix ID %d I/O handle %d\n", spaces + 2, "",
6398cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie                     mPatch.sources[i].id, mPatch.sources[i].ext.mix.handle);
6498cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie        }
6598cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie        result.append(buffer);
6698cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie    }
6798cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie    snprintf(buffer, SIZE, "%*s- %d sinks:\n", spaces, "", mPatch.num_sinks);
6898cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie    result.append(buffer);
6998cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie    for (size_t i = 0; i < mPatch.num_sinks; i++) {
7098cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie        if (mPatch.sinks[i].type == AUDIO_PORT_TYPE_DEVICE) {
71a8ecc2c72ca26389bd6b0162181d60aaeaca8149François Gaffie            std::string device;
72a8ecc2c72ca26389bd6b0162181d60aaeaca8149François Gaffie            DeviceConverter::toString(mPatch.sinks[i].ext.device.type, device);
7398cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie            snprintf(buffer, SIZE, "%*s- Device ID %d %s\n", spaces + 2, "",
74a8ecc2c72ca26389bd6b0162181d60aaeaca8149François Gaffie                     mPatch.sinks[i].id,
75a8ecc2c72ca26389bd6b0162181d60aaeaca8149François Gaffie                     device.c_str());
7698cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie        } else {
7798cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie            snprintf(buffer, SIZE, "%*s- Mix ID %d I/O handle %d\n", spaces + 2, "",
7898cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie                     mPatch.sinks[i].id, mPatch.sinks[i].ext.mix.handle);
7998cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie        }
8098cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie        result.append(buffer);
8198cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie    }
8298cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie
8398cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie    write(fd, result.string(), result.size());
8498cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie    return NO_ERROR;
8598cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie}
8698cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie
8753615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffiestatus_t AudioPatchCollection::addAudioPatch(audio_patch_handle_t handle,
8853615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffie                                             const sp<AudioPatch>& patch)
8953615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffie{
9053615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffie    ssize_t index = indexOfKey(handle);
9153615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffie
9253615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffie    if (index >= 0) {
9353615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffie        ALOGW("addAudioPatch() patch %d already in", handle);
9453615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffie        return ALREADY_EXISTS;
9553615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffie    }
9653615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffie    add(handle, patch);
9753615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffie    ALOGV("addAudioPatch() handle %d af handle %d num_sources %d num_sinks %d source handle %d"
9853615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffie            "sink handle %d",
9953615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffie          handle, patch->mAfPatchHandle, patch->mPatch.num_sources, patch->mPatch.num_sinks,
10053615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffie          patch->mPatch.sources[0].id, patch->mPatch.sinks[0].id);
10153615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffie    return NO_ERROR;
10253615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffie}
10353615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffie
10453615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffiestatus_t AudioPatchCollection::removeAudioPatch(audio_patch_handle_t handle)
10553615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffie{
10653615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffie    ssize_t index = indexOfKey(handle);
10753615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffie
10853615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffie    if (index < 0) {
10953615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffie        ALOGW("removeAudioPatch() patch %d not in", handle);
11053615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffie        return ALREADY_EXISTS;
11153615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffie    }
11253615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffie    ALOGV("removeAudioPatch() handle %d af handle %d", handle, valueAt(index)->mAfPatchHandle);
11353615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffie    removeItemsAt(index);
11453615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffie    return NO_ERROR;
11553615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffie}
11653615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffie
11753615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffiestatus_t AudioPatchCollection::listAudioPatches(unsigned int *num_patches,
11853615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffie                                                struct audio_patch *patches) const
11953615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffie{
12053615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffie    if (num_patches == NULL || (*num_patches != 0 && patches == NULL)) {
12153615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffie        return BAD_VALUE;
12253615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffie    }
12353615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffie    ALOGV("listAudioPatches() num_patches %d patches %p available patches %zu",
12453615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffie          *num_patches, patches, size());
12553615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffie    if (patches == NULL) {
12653615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffie        *num_patches = 0;
12753615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffie    }
12853615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffie
12953615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffie    size_t patchesWritten = 0;
13053615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffie    size_t patchesMax = *num_patches;
1315a2b62984c7cecd1761fe272c078dd814c167942Eric Laurent    *num_patches = 0;
1325a2b62984c7cecd1761fe272c078dd814c167942Eric Laurent    for (size_t patchIndex = 0; patchIndex < size(); patchIndex++) {
1335a2b62984c7cecd1761fe272c078dd814c167942Eric Laurent        // do not report patches with AUDIO_DEVICE_IN_STUB as source or
1345a2b62984c7cecd1761fe272c078dd814c167942Eric Laurent        // AUDIO_DEVICE_OUT_STUB as sink as those devices are used by stub HALs by convention
1355a2b62984c7cecd1761fe272c078dd814c167942Eric Laurent        const sp<AudioPatch> patch = valueAt(patchIndex);
1365a2b62984c7cecd1761fe272c078dd814c167942Eric Laurent        bool skip = false;
1375a2b62984c7cecd1761fe272c078dd814c167942Eric Laurent        for (size_t srcIndex = 0; srcIndex < patch->mPatch.num_sources && !skip; srcIndex++) {
1385a2b62984c7cecd1761fe272c078dd814c167942Eric Laurent            if (patch->mPatch.sources[srcIndex].type == AUDIO_PORT_TYPE_DEVICE &&
1395a2b62984c7cecd1761fe272c078dd814c167942Eric Laurent                    patch->mPatch.sources[srcIndex].ext.device.type == AUDIO_DEVICE_IN_STUB) {
1405a2b62984c7cecd1761fe272c078dd814c167942Eric Laurent                skip = true;
1415a2b62984c7cecd1761fe272c078dd814c167942Eric Laurent            }
1425a2b62984c7cecd1761fe272c078dd814c167942Eric Laurent        }
1435a2b62984c7cecd1761fe272c078dd814c167942Eric Laurent        for (size_t sinkIndex = 0; sinkIndex < patch->mPatch.num_sinks && !skip; sinkIndex++) {
1445a2b62984c7cecd1761fe272c078dd814c167942Eric Laurent            if (patch->mPatch.sinks[sinkIndex].type == AUDIO_PORT_TYPE_DEVICE &&
1455a2b62984c7cecd1761fe272c078dd814c167942Eric Laurent                    patch->mPatch.sinks[sinkIndex].ext.device.type == AUDIO_DEVICE_OUT_STUB) {
1465a2b62984c7cecd1761fe272c078dd814c167942Eric Laurent                skip = true;
1475a2b62984c7cecd1761fe272c078dd814c167942Eric Laurent            }
1485a2b62984c7cecd1761fe272c078dd814c167942Eric Laurent        }
1495a2b62984c7cecd1761fe272c078dd814c167942Eric Laurent        if (skip) {
1505a2b62984c7cecd1761fe272c078dd814c167942Eric Laurent            continue; // to next audio patch
1515a2b62984c7cecd1761fe272c078dd814c167942Eric Laurent        }
1525a2b62984c7cecd1761fe272c078dd814c167942Eric Laurent        if (patchesWritten < patchesMax) {
1535a2b62984c7cecd1761fe272c078dd814c167942Eric Laurent            patches[patchesWritten] = patch->mPatch;
1545a2b62984c7cecd1761fe272c078dd814c167942Eric Laurent            patches[patchesWritten++].id = patch->mHandle;
1555a2b62984c7cecd1761fe272c078dd814c167942Eric Laurent        }
1565a2b62984c7cecd1761fe272c078dd814c167942Eric Laurent        (*num_patches)++;
15753615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffie        ALOGV("listAudioPatches() patch %zu num_sources %d num_sinks %d",
1585a2b62984c7cecd1761fe272c078dd814c167942Eric Laurent              patchIndex, patch->mPatch.num_sources, patch->mPatch.num_sinks);
15953615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffie    }
16053615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffie
16153615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffie    ALOGV("listAudioPatches() got %zu patches needed %d", patchesWritten, *num_patches);
16253615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffie    return NO_ERROR;
16353615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffie}
16453615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffie
16553615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffiestatus_t AudioPatchCollection::dump(int fd) const
16653615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffie{
16753615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffie    const size_t SIZE = 256;
16853615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffie    char buffer[SIZE];
16953615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffie    snprintf(buffer, SIZE, "\nAudio Patches:\n");
17053615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffie    write(fd, buffer, strlen(buffer));
17153615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffie    for (size_t i = 0; i < size(); i++) {
17253615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffie        valueAt(i)->dump(fd, 2, i);
17353615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffie    }
17453615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffie    return NO_ERROR;
17553615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffie}
17653615e29c99c5e9d2ca77aaefd7bf5c770513120François Gaffie
17798cc191247388132b6fd8a4ecd07abd6e4c5a0edFrançois Gaffie}; // namespace android
178