• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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 #pragma once
18 
19 #include "DeviceDescriptor.h"
20 #include <utils/RefBase.h>
21 #include <media/AudioDeviceTypeAddr.h>
22 #include <media/AudioPolicy.h>
23 #include <utils/Vector.h>
24 #include <system/audio.h>
25 #include <utils/String8.h>
26 
27 #include <DeviceDescriptor.h>
28 #include <AudioOutputDescriptor.h>
29 
30 namespace android {
31 
32 /**
33  * custom mix entry in mPolicyMixes
34  */
35 class AudioPolicyMix : public AudioMix, public RefBase {
36 public:
AudioPolicyMix(const AudioMix & mix)37     AudioPolicyMix(const AudioMix &mix) : AudioMix(mix) {}
38     AudioPolicyMix(const AudioPolicyMix&) = delete;
39     AudioPolicyMix& operator=(const AudioPolicyMix&) = delete;
40 
getOutput()41     const sp<SwAudioOutputDescriptor> &getOutput() const { return mOutput; }
setOutput(const sp<SwAudioOutputDescriptor> & output)42     void setOutput(const sp<SwAudioOutputDescriptor> &output) { mOutput = output; }
clearOutput()43     void clearOutput() { mOutput.clear(); }
44 
45     void dump(String8 *dst, int spaces, int index) const;
46 
47 private:
48     sp<SwAudioOutputDescriptor> mOutput;  // Corresponding output stream
49 };
50 
51 
52 class AudioPolicyMixCollection : public Vector<sp<AudioPolicyMix>>
53 {
54 public:
55     status_t getAudioPolicyMix(audio_devices_t deviceType,
56             const String8& address, sp<AudioPolicyMix> &policyMix) const;
57 
58     status_t registerMix(const AudioMix& mix, const sp<SwAudioOutputDescriptor>& desc);
59 
60     status_t unregisterMix(const AudioMix& mix);
61 
62     void closeOutput(sp<SwAudioOutputDescriptor> &desc);
63 
64     /**
65      * Tries to find the best matching audio policy mix
66      *
67      * @param[in] attributes to consider for the research of the audio policy mix.
68      * @param[in] config to consider for the research of the audio policy mix.
69      * @param[in] uid to consider for the research of the audio policy mix.
70      * @param[in] session to consider for the research of the audio policy mix.
71      * @param[in] flags to consider for the research of the audio policy mix.
72      * @param[in] availableOutputDevices available output devices can be use during the research
73      *      of the audio policy mix
74      * @param[in] requestedDevice currently requested device that can be used determined if the
75      *      matching audio policy mix should be used instead of the currently set preferred device.
76      * @param[out] primaryMix to return in case a matching audio plicy mix could be found.
77      * @param[out] secondaryMixes that audio should be routed to in case a matching
78      *      secondary mixes could be found.
79      * @param[out] usePrimaryOutputFromPolicyMixes to return in case the audio policy mix
80      *      should be use, including the case where the requested device is explicitly disallowed
81      *      by the audio policy.
82      *
83      * @return OK if the request is valid
84      *         otherwise if the request is not supported
85      */
86     status_t getOutputForAttr(const audio_attributes_t& attributes,
87                               const audio_config_base_t& config,
88                               uid_t uid,
89                               audio_session_t session,
90                               audio_output_flags_t flags,
91                               const DeviceVector &availableOutputDevices,
92                               const sp<DeviceDescriptor>& requestedDevice,
93                               sp<AudioPolicyMix> &primaryMix,
94                               std::vector<sp<AudioPolicyMix>> *secondaryMixes,
95                               bool& usePrimaryOutputFromPolicyMixes);
96 
97     sp<DeviceDescriptor> getDeviceAndMixForInputSource(const audio_attributes_t& attributes,
98                                                        const DeviceVector &availableDeviceTypes,
99                                                        uid_t uid,
100                                                        audio_session_t session,
101                                                        sp<AudioPolicyMix> *policyMix) const;
102 
103     /**
104      * @brief try to find a matching mix for a given output descriptor and returns the associated
105      * output device.
106      * @param output to be considered
107      * @param availableOutputDevices list of output devices currently reachable
108      * @return device selected from the mix attached to the output, null pointer otherwise
109      */
110     sp<DeviceDescriptor> getDeviceAndMixForOutput(const sp<SwAudioOutputDescriptor> &output,
111                                                   const DeviceVector &availableOutputDevices);
112 
113     status_t getInputMixForAttr(audio_attributes_t attr, sp<AudioPolicyMix> *policyMix);
114 
115     /**
116      * Updates the mix rules in order to make streams associated with the given uid
117      * be routed to the given audio devices.
118      * @param uid the uid for which the device affinity is set
119      * @param devices the vector of devices that this uid may be routed to. A typical
120      *    use is to pass the devices associated with a given zone in a multi-zone setup.
121      * @return NO_ERROR if the update was successful, INVALID_OPERATION otherwise.
122      *    An example of failure is when there are already rules in place to restrict
123      *    a mix to the given uid (i.e. when a MATCH_UID rule was set for it).
124      */
125     status_t setUidDeviceAffinities(uid_t uid, const AudioDeviceTypeAddrVector& devices);
126     status_t removeUidDeviceAffinities(uid_t uid);
127     status_t getDevicesForUid(uid_t uid, Vector<AudioDeviceTypeAddr>& devices) const;
128 
129     /**
130      * Updates the mix rules in order to make streams associated with the given user
131      * be routed to the given audio devices.
132      * @param userId the userId for which the device affinity is set
133      * @param devices the vector of devices that this userId may be routed to. A typical
134      *    use is to pass the devices associated with a given zone in a multi-zone setup.
135      * @return NO_ERROR if the update was successful, INVALID_OPERATION otherwise.
136      *    An example of failure is when there are already rules in place to restrict
137      *    a mix to the given userId (i.e. when a MATCH_USERID rule was set for it).
138      */
139     status_t setUserIdDeviceAffinities(int userId, const AudioDeviceTypeAddrVector& devices);
140     status_t removeUserIdDeviceAffinities(int userId);
141     status_t getDevicesForUserId(int userId, AudioDeviceTypeAddrVector& devices) const;
142 
143     void dump(String8 *dst) const;
144 
145 private:
146     bool mixMatch(const AudioMix* mix, size_t mixIndex,
147                             const audio_attributes_t& attributes,
148                             const audio_config_base_t& config,
149                             uid_t uid,
150                             audio_session_t session);
151     bool mixDisallowsRequestedDevice(const AudioMix* mix,
152                             const sp<DeviceDescriptor>& requestedDevice,
153                             const sp<DeviceDescriptor>& mixDevice,
154                             const uid_t uid);
155 
156     sp<DeviceDescriptor> getOutputDeviceForMix(const AudioMix* mix,
157                             const DeviceVector& availableOutputDevices);
158 };
159 
160 std::optional<std::string> extractAddressFromAudioAttributes(const audio_attributes_t& attr);
161 
162 } // namespace android
163