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