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