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 #pragma once
18 
19 #include <system/audio.h>
20 #include <utils/KeyedVector.h>
21 #include <utils/RefBase.h>
22 #include <utils/Errors.h>
23 
24 namespace android {
25 
26 class DeviceDescriptor;
27 
28 class SessionRoute : public RefBase
29 {
30 public:
31     // For Input (Source) routes, use STREAM_TYPE_NA ("NA" = "not applicable)for the
32     // streamType argument
33     static const audio_stream_type_t STREAM_TYPE_NA = AUDIO_STREAM_DEFAULT;
34 
35     // For Output (Sink) routes, use SOURCE_TYPE_NA ("NA" = "not applicable") for the
36     // source argument
37 
38     static const audio_source_t SOURCE_TYPE_NA = AUDIO_SOURCE_DEFAULT;
39 
SessionRoute(audio_session_t session,audio_stream_type_t streamType,audio_source_t source,sp<DeviceDescriptor> deviceDescriptor,uid_t uid)40     SessionRoute(audio_session_t session,
41                  audio_stream_type_t streamType,
42                  audio_source_t source,
43                  sp<DeviceDescriptor> deviceDescriptor,
44                  uid_t uid)
45         : mUid(uid),
46           mSession(session),
47           mDeviceDescriptor(deviceDescriptor),
48           mRefCount(0),
49           mActivityCount(0),
50           mChanged(false),
51           mStreamType(streamType),
52           mSource(source)
53     {}
54 
55     void log(const char* prefix);
56 
isActive()57     bool isActive() {
58         return (mDeviceDescriptor != 0) && (mChanged || (mActivityCount > 0));
59     }
60 
61     uid_t                       mUid;
62     audio_session_t             mSession;
63     sp<DeviceDescriptor>        mDeviceDescriptor;
64 
65     // "reference" counting
66     int                         mRefCount;      // +/- on references
67     int                         mActivityCount; // +/- on start/stop
68     bool                        mChanged;
69     // for outputs
70     const audio_stream_type_t   mStreamType;
71     // for inputs
72     const audio_source_t        mSource;
73 };
74 
75 class SessionRouteMap: public KeyedVector<audio_session_t, sp<SessionRoute> >
76 {
77 public:
78     // These constants identify the SessionRoutMap as holding EITHER input routes,
79     // or output routes.  An error will occur if an attempt is made to add a SessionRoute
80     // object with mStreamType == STREAM_TYPE_NA (i.e. an input SessionRoute) to a
81     // SessionRoutMap that is marked for output (i.e. mMapType == SESSION_ROUTE_MAP_OUTPUT)
82     // and similarly  for output SessionRoutes and Input SessionRouteMaps.
83     typedef enum
84     {
85         MAPTYPE_INPUT = 0,
86         MAPTYPE_OUTPUT = 1
87     } session_route_map_type_t;
88 
SessionRouteMap(session_route_map_type_t mapType)89     SessionRouteMap(session_route_map_type_t mapType) :
90         mMapType(mapType)
91     {}
92 
93     bool hasRoute(audio_session_t session);
94 
95     void removeRoute(audio_session_t session);
96 
97     int incRouteActivity(audio_session_t session);
98     int decRouteActivity(audio_session_t session);
99     bool hasRouteChanged(audio_session_t session); // also clears the changed flag
100     void log(const char* caption);
101 
102     // Specify an Output(Sink) route by passing SessionRoute::SOURCE_TYPE_NA in the
103     // source argument.
104     // Specify an Input(Source) rout by passing SessionRoute::AUDIO_STREAM_DEFAULT
105     // in the streamType argument.
106     void addRoute(audio_session_t session,
107                   audio_stream_type_t streamType,
108                   audio_source_t source,
109                   sp<DeviceDescriptor> deviceDescriptor,
110                   uid_t uid);
111 
112 private:
113     // Used to mark a SessionRoute as for either inputs (mMapType == kSessionRouteMap_Input)
114     // or outputs (mMapType == kSessionRouteMap_Output)
115     const session_route_map_type_t mMapType;
116 };
117 
118 }; // namespace android
119