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