154c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie/*
254c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie * Copyright (C) 2015 The Android Open Source Project
354c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie *
454c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie * Licensed under the Apache License, Version 2.0 (the "License");
554c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie * you may not use this file except in compliance with the License.
654c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie * You may obtain a copy of the License at
754c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie *
854c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie *      http://www.apache.org/licenses/LICENSE-2.0
954c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie *
1054c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie * Unless required by applicable law or agreed to in writing, software
1154c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie * distributed under the License is distributed on an "AS IS" BASIS,
1254c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1354c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie * See the License for the specific language governing permissions and
1454c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie * limitations under the License.
1554c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie */
1654c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie
1754c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie#define LOG_TAG "APM::SessionRoute"
1854c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie//#define LOG_NDEBUG 0
1954c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie
2054c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie#include "SessionRoute.h"
2154c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie#include "HwModule.h"
2254c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie#include "AudioGain.h"
2354c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie#include "DeviceDescriptor.h"
2454c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie#include <utils/Log.h>
2554c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie
2654c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffienamespace android {
2754c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie
2854c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie// --- SessionRoute class implementation
2954c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffievoid SessionRoute::log(const char* prefix)
3054c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie{
3154c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    ALOGI("%s[SessionRoute strm:0x%X, src:%d, sess:0x%X, dev:0x%X refs:%d act:%d",
3254c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie          prefix, mStreamType, mSource, mSession,
3354c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie          mDeviceDescriptor != 0 ? mDeviceDescriptor->type() : AUDIO_DEVICE_NONE,
3454c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie          mRefCount, mActivityCount);
3554c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie}
3654c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie
3754c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie// --- SessionRouteMap class implementation
3854c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffiebool SessionRouteMap::hasRoute(audio_session_t session)
3954c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie{
4054c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    return indexOfKey(session) >= 0 && valueFor(session)->mDeviceDescriptor != 0;
4154c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie}
4254c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie
4354c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffiebool SessionRouteMap::hasRouteChanged(audio_session_t session)
4454c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie{
4554c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    if (indexOfKey(session) >= 0) {
4654c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie        if (valueFor(session)->mChanged) {
4754c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie            valueFor(session)->mChanged = false;
4854c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie            return true;
4954c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie        }
5054c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    }
5154c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    return false;
5254c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie}
5354c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie
5454c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffievoid SessionRouteMap::removeRoute(audio_session_t session)
5554c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie{
5654c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    sp<SessionRoute> route = indexOfKey(session) >= 0 ? valueFor(session) : 0;
5754c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    if (route != 0) {
5854c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie        ALOG_ASSERT(route->mRefCount > 0);
5954c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie        --route->mRefCount;
6054c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie        if (route->mRefCount <= 0) {
6154c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie            removeItem(session);
6254c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie        }
6354c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    }
6454c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie}
6554c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie
6654c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffieint SessionRouteMap::incRouteActivity(audio_session_t session)
6754c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie{
6854c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    sp<SessionRoute> route = indexOfKey(session) >= 0 ? valueFor(session) : 0;
6954c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    return route != 0 ? ++(route->mActivityCount) : -1;
7054c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie}
7154c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie
7254c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffieint SessionRouteMap::decRouteActivity(audio_session_t session)
7354c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie{
7454c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    sp<SessionRoute> route = indexOfKey(session) >= 0 ? valueFor(session) : 0;
7554c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    if (route != 0 && route->mActivityCount > 0) {
7654c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie        return --(route->mActivityCount);
7754c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    } else {
7854c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie        return -1;
7954c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    }
8054c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie}
8154c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie
8254c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffievoid SessionRouteMap::log(const char* caption)
8354c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie{
8454c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    ALOGI("%s ----", caption);
8554c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    for(size_t index = 0; index < size(); index++) {
8654c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie        valueAt(index)->log("  ");
8754c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    }
8854c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie}
8954c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie
9054c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffievoid SessionRouteMap::addRoute(audio_session_t session,
9154c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie                               audio_stream_type_t streamType,
9254c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie                               audio_source_t source,
9354c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie                               sp<DeviceDescriptor> descriptor,
9454c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie                               uid_t uid)
9554c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie{
9654c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    if (mMapType == MAPTYPE_INPUT && streamType != SessionRoute::STREAM_TYPE_NA) {
9754c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie        ALOGE("Adding Output Route to InputRouteMap");
9854c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie        return;
9954c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    } else if (mMapType == MAPTYPE_OUTPUT && source != SessionRoute::SOURCE_TYPE_NA) {
10054c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie        ALOGE("Adding Input Route to OutputRouteMap");
10154c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie        return;
10254c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    }
10354c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie
10454c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    sp<SessionRoute> route = indexOfKey(session) >= 0 ? valueFor(session) : 0;
10554c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie
10654c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    if (route != 0) {
10754c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie        if (((route->mDeviceDescriptor == 0) && (descriptor != 0)) ||
10854c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie                ((route->mDeviceDescriptor != 0) &&
10954c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie                 ((descriptor == 0) || (!route->mDeviceDescriptor->equals(descriptor))))) {
11054c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie            route->mChanged = true;
11154c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie        }
11254c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie        route->mRefCount++;
11354c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie        route->mDeviceDescriptor = descriptor;
11454c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    } else {
11554c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie        route = new SessionRoute(session, streamType, source, descriptor, uid);
11654c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie        route->mRefCount++;
11754c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie        add(session, route);
11854c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie        if (descriptor != 0) {
11954c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie            route->mChanged = true;
12054c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie        }
12154c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    }
12254c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie}
12354c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie
12454c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie} // namespace android
125