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 status_t updateMix(const AudioMix& mix, const std::vector<AudioMixMatchCriterion>& newCriteria); 63 64 void closeOutput(sp<SwAudioOutputDescriptor> &desc, const SwAudioOutputCollection& allOutputs); 65 66 /** 67 * Tries to find the best matching audio policy mix 68 * 69 * @param[in] attributes to consider for the research of the audio policy mix. 70 * @param[in] config to consider for the research of the audio policy mix. 71 * @param[in] uid to consider for the research of the audio policy mix. 72 * @param[in] session to consider for the research of the audio policy mix. 73 * @param[in] flags to consider for the research of the audio policy mix. 74 * @param[in] availableOutputDevices available output devices can be use during the research 75 * of the audio policy mix 76 * @param[in] requestedDevice currently requested device that can be used determined if the 77 * matching audio policy mix should be used instead of the currently set preferred device. 78 * @param[out] primaryMix to return in case a matching audio plicy mix could be found. 79 * @param[out] secondaryMixes that audio should be routed to in case a matching 80 * secondary mixes could be found. 81 * @param[out] usePrimaryOutputFromPolicyMixes to return in case the audio policy mix 82 * should be use, including the case where the requested device is explicitly disallowed 83 * by the audio policy. 84 * 85 * @return OK if the request is valid 86 * otherwise if the request is not supported 87 */ 88 status_t getOutputForAttr(const audio_attributes_t& attributes, 89 const audio_config_base_t& config, 90 uid_t uid, 91 audio_session_t session, 92 audio_output_flags_t flags, 93 const DeviceVector &availableOutputDevices, 94 const sp<DeviceDescriptor>& requestedDevice, 95 sp<AudioPolicyMix> &primaryMix, 96 std::vector<sp<AudioPolicyMix>> *secondaryMixes, 97 bool& usePrimaryOutputFromPolicyMixes); 98 99 sp<DeviceDescriptor> getDeviceAndMixForInputSource(const audio_attributes_t& attributes, 100 const DeviceVector &availableDeviceTypes, 101 uid_t uid, 102 audio_session_t session, 103 sp<AudioPolicyMix> *policyMix) const; 104 105 /** 106 * @brief try to find a matching mix for a given output descriptor and returns the associated 107 * output device. 108 * @param output to be considered 109 * @param availableOutputDevices list of output devices currently reachable 110 * @return device selected from the mix attached to the output, null pointer otherwise 111 */ 112 sp<DeviceDescriptor> getDeviceAndMixForOutput(const sp<SwAudioOutputDescriptor> &output, 113 const DeviceVector &availableOutputDevices); 114 115 status_t getInputMixForAttr(audio_attributes_t attr, sp<AudioPolicyMix> *policyMix); 116 117 /** 118 * Updates the mix rules in order to make streams associated with the given uid 119 * be routed to the given audio devices. 120 * @param uid the uid for which the device affinity is set 121 * @param devices the vector of devices that this uid may be routed to. A typical 122 * use is to pass the devices associated with a given zone in a multi-zone setup. 123 * @return NO_ERROR if the update was successful, INVALID_OPERATION otherwise. 124 * An example of failure is when there are already rules in place to restrict 125 * a mix to the given uid (i.e. when a MATCH_UID rule was set for it). 126 */ 127 status_t setUidDeviceAffinities(uid_t uid, const AudioDeviceTypeAddrVector& devices); 128 status_t removeUidDeviceAffinities(uid_t uid); 129 status_t getDevicesForUid(uid_t uid, Vector<AudioDeviceTypeAddr>& devices) const; 130 131 /** 132 * Updates the mix rules in order to make streams associated with the given user 133 * be routed to the given audio devices. 134 * @param userId the userId for which the device affinity is set 135 * @param devices the vector of devices that this userId may be routed to. A typical 136 * use is to pass the devices associated with a given zone in a multi-zone setup. 137 * @return NO_ERROR if the update was successful, INVALID_OPERATION otherwise. 138 * An example of failure is when there are already rules in place to restrict 139 * a mix to the given userId (i.e. when a MATCH_USERID rule was set for it). 140 */ 141 status_t setUserIdDeviceAffinities(int userId, const AudioDeviceTypeAddrVector& devices); 142 status_t removeUserIdDeviceAffinities(int userId); 143 status_t getDevicesForUserId(int userId, AudioDeviceTypeAddrVector& devices) const; 144 145 void dump(String8 *dst) const; 146 147 private: 148 bool mixMatch(const AudioMix* mix, size_t mixIndex, 149 const audio_attributes_t& attributes, 150 const audio_config_base_t& config, 151 uid_t uid, 152 audio_session_t session); 153 bool mixDisallowsRequestedDevice(const AudioMix* mix, 154 const sp<DeviceDescriptor>& requestedDevice, 155 const sp<DeviceDescriptor>& mixDevice, 156 const uid_t uid); 157 158 sp<DeviceDescriptor> getOutputDeviceForMix(const AudioMix* mix, 159 const DeviceVector& availableOutputDevices); 160 }; 161 162 std::optional<std::string> extractAddressFromAudioAttributes(const audio_attributes_t& attr); 163 164 } // namespace android 165