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#pragma once
1854c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie
1954c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie#include <system/audio.h>
2054c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie#include <utils/KeyedVector.h>
2154c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie#include <utils/RefBase.h>
2254c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie#include <utils/Errors.h>
2354c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie
2454c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffienamespace android {
2554c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie
2654c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffieclass DeviceDescriptor;
2754c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie
2854c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffieclass SessionRoute : public RefBase
2954c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie{
3054c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffiepublic:
3154c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    // For Input (Source) routes, use STREAM_TYPE_NA ("NA" = "not applicable)for the
3254c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    // streamType argument
3354c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    static const audio_stream_type_t STREAM_TYPE_NA = AUDIO_STREAM_DEFAULT;
3454c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie
3554c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    // For Output (Sink) routes, use SOURCE_TYPE_NA ("NA" = "not applicable") for the
3654c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    // source argument
3754c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie
3854c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    static const audio_source_t SOURCE_TYPE_NA = AUDIO_SOURCE_DEFAULT;
3954c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie
4054c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    SessionRoute(audio_session_t session,
4154c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie                 audio_stream_type_t streamType,
4254c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie                 audio_source_t source,
4354c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie                 sp<DeviceDescriptor> deviceDescriptor,
4454c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie                 uid_t uid)
4554c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie        : mUid(uid),
4654c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie          mSession(session),
4754c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie          mDeviceDescriptor(deviceDescriptor),
4854c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie          mRefCount(0),
4954c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie          mActivityCount(0),
5054c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie          mChanged(false),
5154c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie          mStreamType(streamType),
5254c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie          mSource(source)
5354c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    {}
5454c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie
5554c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    void log(const char* prefix);
5654c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie
5754c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    bool isActive() {
5854c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie        return (mDeviceDescriptor != 0) && (mChanged || (mActivityCount > 0));
5954c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    }
6054c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie
6154c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    uid_t                       mUid;
6254c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    audio_session_t             mSession;
6354c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    sp<DeviceDescriptor>        mDeviceDescriptor;
6454c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie
6554c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    // "reference" counting
6654c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    int                         mRefCount;      // +/- on references
6754c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    int                         mActivityCount; // +/- on start/stop
6854c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    bool                        mChanged;
6954c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    // for outputs
7054c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    const audio_stream_type_t   mStreamType;
7154c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    // for inputs
7254c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    const audio_source_t        mSource;
7354c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie};
7454c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie
7554c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffieclass SessionRouteMap: public KeyedVector<audio_session_t, sp<SessionRoute> >
7654c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie{
7754c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffiepublic:
7854c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    // These constants identify the SessionRoutMap as holding EITHER input routes,
7954c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    // or output routes.  An error will occur if an attempt is made to add a SessionRoute
8054c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    // object with mStreamType == STREAM_TYPE_NA (i.e. an input SessionRoute) to a
8154c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    // SessionRoutMap that is marked for output (i.e. mMapType == SESSION_ROUTE_MAP_OUTPUT)
8254c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    // and similarly  for output SessionRoutes and Input SessionRouteMaps.
8354c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    typedef enum
8454c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    {
8554c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie        MAPTYPE_INPUT = 0,
8654c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie        MAPTYPE_OUTPUT = 1
8754c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    } session_route_map_type_t;
8854c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie
8954c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    SessionRouteMap(session_route_map_type_t mapType) :
9054c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie        mMapType(mapType)
9154c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    {}
9254c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie
9354c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    bool hasRoute(audio_session_t session);
9454c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie
9554c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    void removeRoute(audio_session_t session);
9654c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie
9754c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    int incRouteActivity(audio_session_t session);
9854c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    int decRouteActivity(audio_session_t session);
9954c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    bool hasRouteChanged(audio_session_t session); // also clears the changed flag
10054c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    void log(const char* caption);
10154c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie
10254c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    // Specify an Output(Sink) route by passing SessionRoute::SOURCE_TYPE_NA in the
10354c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    // source argument.
10454c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    // Specify an Input(Source) rout by passing SessionRoute::AUDIO_STREAM_DEFAULT
10554c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    // in the streamType argument.
10654c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    void addRoute(audio_session_t session,
10754c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie                  audio_stream_type_t streamType,
10854c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie                  audio_source_t source,
10954c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie                  sp<DeviceDescriptor> deviceDescriptor,
11054c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie                  uid_t uid);
11154c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie
11254c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffieprivate:
11354c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    // Used to mark a SessionRoute as for either inputs (mMapType == kSessionRouteMap_Input)
11454c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    // or outputs (mMapType == kSessionRouteMap_Output)
11554c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie    const session_route_map_type_t mMapType;
11654c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie};
11754c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie
11854c0659b9efa72d11997c590c4d377c44789c7fdFrançois Gaffie}; // namespace android
119