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