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 <system/audio.h>
22 #include <system/audio_policy.h>
23 #include <binder/Parcel.h>
24 #include <utils/String8.h>
25 #include <utils/Vector.h>
26
27 namespace android {
28
29 // Keep in sync with AudioMix.java, AudioMixingRule.java, AudioPolicyConfig.java
30 #define RULE_EXCLUSION_MASK 0x8000
31 #define RULE_MATCH_ATTRIBUTE_USAGE 0x1
32 #define RULE_MATCH_ATTRIBUTE_CAPTURE_PRESET (0x1 << 1)
33 #define RULE_MATCH_UID (0x1 << 2)
34 #define RULE_EXCLUDE_ATTRIBUTE_USAGE (RULE_EXCLUSION_MASK|RULE_MATCH_ATTRIBUTE_USAGE)
35 #define RULE_EXCLUDE_ATTRIBUTE_CAPTURE_PRESET \
36 (RULE_EXCLUSION_MASK|RULE_MATCH_ATTRIBUTE_CAPTURE_PRESET)
37 #define RULE_EXCLUDE_UID (RULE_EXCLUSION_MASK|RULE_MATCH_UID)
38
39 #define MIX_TYPE_INVALID (-1)
40 #define MIX_TYPE_PLAYERS 0
41 #define MIX_TYPE_RECORDERS 1
42
43 // definition of the different events that can be reported on a dynamic policy from
44 // AudioSystem's implementation of the AudioPolicyClient interface
45 // keep in sync with AudioSystem.java
46 #define DYNAMIC_POLICY_EVENT_MIX_STATE_UPDATE 0
47
48 #define MIX_STATE_DISABLED (-1)
49 #define MIX_STATE_IDLE 0
50 #define MIX_STATE_MIXING 1
51
52 /** Control to which device some audio is rendered */
53 #define MIX_ROUTE_FLAG_RENDER 0x1
54 /** Loop back some audio instead of rendering it */
55 #define MIX_ROUTE_FLAG_LOOP_BACK (0x1 << 1)
56 /** Loop back some audio while it is rendered */
57 #define MIX_ROUTE_FLAG_LOOP_BACK_AND_RENDER (MIX_ROUTE_FLAG_RENDER | MIX_ROUTE_FLAG_LOOP_BACK)
58 #define MIX_ROUTE_FLAG_ALL (MIX_ROUTE_FLAG_RENDER | MIX_ROUTE_FLAG_LOOP_BACK)
59
60 #define MAX_MIXES_PER_POLICY 10
61 #define MAX_CRITERIA_PER_MIX 20
62
63 class AudioDeviceTypeAddr {
64 public:
AudioDeviceTypeAddr()65 AudioDeviceTypeAddr() {}
AudioDeviceTypeAddr(audio_devices_t type,String8 address)66 AudioDeviceTypeAddr(audio_devices_t type, String8 address) :
67 mType(type), mAddress(address) {}
68
69 status_t readFromParcel(Parcel *parcel);
70 status_t writeToParcel(Parcel *parcel) const;
71
72 audio_devices_t mType;
73 String8 mAddress;
74 };
75
76 class AudioMixMatchCriterion {
77 public:
AudioMixMatchCriterion()78 AudioMixMatchCriterion() {}
79 AudioMixMatchCriterion(audio_usage_t usage, audio_source_t source, uint32_t rule);
80
81 status_t readFromParcel(Parcel *parcel);
82 status_t writeToParcel(Parcel *parcel) const;
83
84 union {
85 audio_usage_t mUsage;
86 audio_source_t mSource;
87 uid_t mUid;
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(Vector<AudioMixMatchCriterion> criteria,uint32_t mixType,audio_config_t format,uint32_t routeFlags,String8 registrationId,uint32_t flags)99 AudioMix(Vector<AudioMixMatchCriterion> criteria, uint32_t mixType, audio_config_t format,
100 uint32_t routeFlags, String8 registrationId, 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) const;
108 void setMatchUid(uid_t uid) const;
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 /** returns true if this mix can be used for uid-device affinity routing */
114 bool isDeviceAffinityCompatible() const;
115
116 mutable Vector<AudioMixMatchCriterion> mCriteria;
117 uint32_t mMixType;
118 audio_config_t mFormat;
119 uint32_t mRouteFlags;
120 audio_devices_t mDeviceType;
121 String8 mDeviceAddress;
122 uint32_t mCbFlags; // flags indicating which callbacks to use, see kCbFlag*
123 /** Ignore the AUDIO_FLAG_NO_MEDIA_PROJECTION */
124 bool mAllowPrivilegedPlaybackCapture = false;
125 };
126
127
128 // definitions for audio recording configuration updates;
129 // keep in sync with AudioManager.java for values used from native code
130 #define RECORD_CONFIG_EVENT_START 0
131 #define RECORD_CONFIG_EVENT_STOP 1
132 #define RECORD_CONFIG_EVENT_UPDATE 2
133
is_mix_loopback_render(uint32_t routeFlags)134 static inline bool is_mix_loopback_render(uint32_t routeFlags) {
135 return (routeFlags & MIX_ROUTE_FLAG_LOOP_BACK_AND_RENDER)
136 == MIX_ROUTE_FLAG_LOOP_BACK_AND_RENDER;
137 }
138
139 }; // namespace android
140
141 #endif // ANDROID_AUDIO_POLICY_H
142