1 /*
2 * Copyright (C) 2014 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
18 #ifndef ANDROID_AUDIO_POLICY_H
19 #define ANDROID_AUDIO_POLICY_H
20
21 #include <binder/IBinder.h>
22 #include <binder/Parcel.h>
23 #include <media/AudioDeviceTypeAddr.h>
24 #include <system/audio.h>
25 #include <system/audio_policy.h>
26 #include <utils/String8.h>
27 #include <utils/Vector.h>
28 #include <cutils/multiuser.h>
29
30 namespace android {
31
32 // Keep in sync with AudioMix.java, AudioMixingRule.java, AudioPolicyConfig.java
33 #define RULE_EXCLUSION_MASK 0x8000
34 #define RULE_MATCH_ATTRIBUTE_USAGE 0x1
35 #define RULE_MATCH_ATTRIBUTE_CAPTURE_PRESET (0x1 << 1)
36 #define RULE_MATCH_UID (0x1 << 2)
37 #define RULE_MATCH_USERID (0x1 << 3)
38 #define RULE_MATCH_AUDIO_SESSION_ID (0x1 << 4)
39 #define RULE_EXCLUDE_ATTRIBUTE_USAGE (RULE_EXCLUSION_MASK|RULE_MATCH_ATTRIBUTE_USAGE)
40 #define RULE_EXCLUDE_ATTRIBUTE_CAPTURE_PRESET \
41 (RULE_EXCLUSION_MASK|RULE_MATCH_ATTRIBUTE_CAPTURE_PRESET)
42 #define RULE_EXCLUDE_UID (RULE_EXCLUSION_MASK|RULE_MATCH_UID)
43 #define RULE_EXCLUDE_USERID (RULE_EXCLUSION_MASK|RULE_MATCH_USERID)
44 #define RULE_EXCLUDE_AUDIO_SESSION_ID (RULE_EXCLUSION_MASK|RULE_MATCH_AUDIO_SESSION_ID)
45
46 #define MIX_TYPE_INVALID (-1)
47 #define MIX_TYPE_PLAYERS 0
48 #define MIX_TYPE_RECORDERS 1
49
50 // definition of the different events that can be reported on a dynamic policy from
51 // AudioSystem's implementation of the AudioPolicyClient interface
52 // keep in sync with AudioSystem.java
53 #define DYNAMIC_POLICY_EVENT_MIX_STATE_UPDATE 0
54
55 #define MIX_STATE_DISABLED (-1)
56 #define MIX_STATE_IDLE 0
57 #define MIX_STATE_MIXING 1
58
59 /** Control to which device some audio is rendered */
60 #define MIX_ROUTE_FLAG_RENDER 0x1
61 /** Loop back some audio instead of rendering it */
62 #define MIX_ROUTE_FLAG_LOOP_BACK (0x1 << 1)
63 /** Loop back some audio while it is rendered */
64 #define MIX_ROUTE_FLAG_LOOP_BACK_AND_RENDER (MIX_ROUTE_FLAG_RENDER | MIX_ROUTE_FLAG_LOOP_BACK)
65 /** Control if audio routing disallows preferred device routing **/
66 #define MIX_ROUTE_FLAG_DISALLOWS_PREFERRED_DEVICE (0x1 << 2)
67 #define MIX_ROUTE_FLAG_ALL (MIX_ROUTE_FLAG_RENDER | MIX_ROUTE_FLAG_LOOP_BACK | \
68 MIX_ROUTE_FLAG_DISALLOWS_PREFERRED_DEVICE)
69
70 #define MAX_MIXES_PER_POLICY 50
71 #define MAX_CRITERIA_PER_MIX 20
72
73 class AudioMixMatchCriterion {
74 public:
AudioMixMatchCriterion()75 AudioMixMatchCriterion() {}
76 AudioMixMatchCriterion(audio_usage_t usage, audio_source_t source, uint32_t rule);
77
78 status_t readFromParcel(Parcel *parcel);
79 status_t writeToParcel(Parcel *parcel) const;
80
81 bool isExcludeCriterion() const;
82 union {
83 audio_usage_t mUsage;
84 audio_source_t mSource;
85 uid_t mUid;
86 int mUserId;
87 audio_session_t mAudioSessionId;
88 } mValue;
89 uint32_t mRule;
90 };
91
92 class AudioMix {
93 public:
94 // flag on an AudioMix indicating the activity on this mix (IDLE, MIXING)
95 // must be reported through the AudioPolicyClient interface
96 static const uint32_t kCbFlagNotifyActivity = 0x1;
97
AudioMix()98 AudioMix() {}
AudioMix(const std::vector<AudioMixMatchCriterion> & criteria,uint32_t mixType,audio_config_t format,uint32_t routeFlags,const String8 & registrationId,uint32_t flags)99 AudioMix(const std::vector<AudioMixMatchCriterion> &criteria, uint32_t mixType,
100 audio_config_t format, uint32_t routeFlags,const String8 ®istrationId,
101 uint32_t flags) :
102 mCriteria(criteria), mMixType(mixType), mFormat(format),
103 mRouteFlags(routeFlags), mDeviceAddress(registrationId), mCbFlags(flags){}
104
105 status_t readFromParcel(Parcel *parcel);
106 status_t writeToParcel(Parcel *parcel) const;
107
108 void setExcludeUid(uid_t uid);
109 void setMatchUid(uid_t uid);
110 /** returns true if this mix has a rule to match or exclude the given uid */
111 bool hasUidRule(bool match, uid_t uid) const;
112 /** returns true if this mix has a rule for uid match (any uid) */
113 bool hasMatchUidRule() const;
114
115 void setExcludeUserId(int userId);
116 void setMatchUserId(int userId);
117 /** returns true if this mix has a rule to match or exclude the given userId */
118 bool hasUserIdRule(bool match, int userId) const;
119 /** returns true if this mix has a rule to match or exclude (any userId) */
120 bool hasUserIdRule(bool match) const;
121 /** returns true if this mix has a rule for userId exclude (any userId) */
122 bool isDeviceAffinityCompatible() const;
123
124 std::vector<AudioMixMatchCriterion> mCriteria;
125 uint32_t mMixType;
126 audio_config_t mFormat;
127 uint32_t mRouteFlags;
128 audio_devices_t mDeviceType;
129 String8 mDeviceAddress;
130 uint32_t mCbFlags; // flags indicating which callbacks to use, see kCbFlag*
131 sp<IBinder> mToken;
132 uint32_t mVirtualDeviceId;
133 /** Ignore the AUDIO_FLAG_NO_MEDIA_PROJECTION */
134 bool mAllowPrivilegedMediaPlaybackCapture = false;
135 /** Indicates if the caller can capture voice communication output */
136 bool mVoiceCommunicationCaptureAllowed = false;
137 };
138
139
140 // definitions for audio recording configuration updates;
141 // keep in sync with AudioManager.java for values used from native code
142 #define RECORD_CONFIG_EVENT_START 0
143 #define RECORD_CONFIG_EVENT_STOP 1
144 #define RECORD_CONFIG_EVENT_UPDATE 2
145
is_mix_loopback_render(uint32_t routeFlags)146 static inline bool is_mix_loopback_render(uint32_t routeFlags) {
147 return (routeFlags & MIX_ROUTE_FLAG_LOOP_BACK_AND_RENDER)
148 == MIX_ROUTE_FLAG_LOOP_BACK_AND_RENDER;
149 }
150
is_mix_loopback(uint32_t routeFlags)151 static inline bool is_mix_loopback(uint32_t routeFlags) {
152 return (routeFlags & MIX_ROUTE_FLAG_LOOP_BACK)
153 == MIX_ROUTE_FLAG_LOOP_BACK;
154 }
155
is_mix_disallows_preferred_device(uint32_t routeFlags)156 static inline bool is_mix_disallows_preferred_device(uint32_t routeFlags) {
157 return (routeFlags & MIX_ROUTE_FLAG_DISALLOWS_PREFERRED_DEVICE)
158 == MIX_ROUTE_FLAG_DISALLOWS_PREFERRED_DEVICE;
159 }
160
161 }; // namespace android
162
163 #endif // ANDROID_AUDIO_POLICY_H
164