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(AudioMix mix, sp<SwAudioOutputDescriptor> desc); 59 60 status_t unregisterMix(const AudioMix& mix); 61 62 void closeOutput(sp<SwAudioOutputDescriptor> &desc); 63 64 /** 65 * Try to find an output descriptor for the given attributes. 66 * 67 * @param[in] attributes to consider fowr the research of output descriptor. 68 * @param[out] desc to return if an primary output could be found. 69 * @param[out] secondaryDesc other desc that the audio should be routed to. 70 * @return OK if the request is valid 71 * otherwise if the request is not supported 72 */ 73 status_t getOutputForAttr(const audio_attributes_t& attributes, 74 const audio_config_base_t& config, 75 uid_t uid, audio_output_flags_t flags, 76 sp<AudioPolicyMix> &primaryMix, 77 std::vector<sp<AudioPolicyMix>> *secondaryMixes); 78 79 sp<DeviceDescriptor> getDeviceAndMixForInputSource(audio_source_t inputSource, 80 const DeviceVector &availableDeviceTypes, 81 uid_t uid, 82 sp<AudioPolicyMix> *policyMix) const; 83 84 /** 85 * @brief try to find a matching mix for a given output descriptor and returns the associated 86 * output device. 87 * @param output to be considered 88 * @param availableOutputDevices list of output devices currently reachable 89 * @return device selected from the mix attached to the output, null pointer otherwise 90 */ 91 sp<DeviceDescriptor> getDeviceAndMixForOutput(const sp<SwAudioOutputDescriptor> &output, 92 const DeviceVector &availableOutputDevices); 93 94 status_t getInputMixForAttr(audio_attributes_t attr, sp<AudioPolicyMix> *policyMix); 95 96 /** 97 * Updates the mix rules in order to make streams associated with the given uid 98 * be routed to the given audio devices. 99 * @param uid the uid for which the device affinity is set 100 * @param devices the vector of devices that this uid may be routed to. A typical 101 * use is to pass the devices associated with a given zone in a multi-zone setup. 102 * @return NO_ERROR if the update was successful, INVALID_OPERATION otherwise. 103 * An example of failure is when there are already rules in place to restrict 104 * a mix to the given uid (i.e. when a MATCH_UID rule was set for it). 105 */ 106 status_t setUidDeviceAffinities(uid_t uid, const AudioDeviceTypeAddrVector& devices); 107 status_t removeUidDeviceAffinities(uid_t uid); 108 status_t getDevicesForUid(uid_t uid, Vector<AudioDeviceTypeAddr>& devices) const; 109 110 /** 111 * Updates the mix rules in order to make streams associated with the given user 112 * be routed to the given audio devices. 113 * @param userId the userId for which the device affinity is set 114 * @param devices the vector of devices that this userId may be routed to. A typical 115 * use is to pass the devices associated with a given zone in a multi-zone setup. 116 * @return NO_ERROR if the update was successful, INVALID_OPERATION otherwise. 117 * An example of failure is when there are already rules in place to restrict 118 * a mix to the given userId (i.e. when a MATCH_USERID rule was set for it). 119 */ 120 status_t setUserIdDeviceAffinities(int userId, const AudioDeviceTypeAddrVector& devices); 121 status_t removeUserIdDeviceAffinities(int userId); 122 status_t getDevicesForUserId(int userId, Vector<AudioDeviceTypeAddr>& devices) const; 123 124 void dump(String8 *dst) const; 125 126 private: 127 enum class MixMatchStatus { MATCH, NO_MATCH, INVALID_MIX }; 128 MixMatchStatus mixMatch(const AudioMix* mix, size_t mixIndex, 129 const audio_attributes_t& attributes, 130 const audio_config_base_t& config, 131 uid_t uid); 132 }; 133 134 } // namespace android 135