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/AudioPolicy.h> 22 #include <utils/Vector.h> 23 #include <system/audio.h> 24 #include <utils/String8.h> 25 26 #include <DeviceDescriptor.h> 27 #include <AudioOutputDescriptor.h> 28 29 namespace android { 30 31 /** 32 * custom mix entry in mPolicyMixes 33 */ 34 class AudioPolicyMix : public AudioMix, public RefBase { 35 public: AudioPolicyMix(const AudioMix & mix)36 AudioPolicyMix(const AudioMix &mix) : AudioMix(mix) {} 37 AudioPolicyMix(const AudioPolicyMix&) = delete; 38 AudioPolicyMix& operator=(const AudioPolicyMix&) = delete; 39 getOutput()40 const sp<SwAudioOutputDescriptor> &getOutput() const { return mOutput; } setOutput(const sp<SwAudioOutputDescriptor> & output)41 void setOutput(const sp<SwAudioOutputDescriptor> &output) { mOutput = output; } clearOutput()42 void clearOutput() { mOutput.clear(); } 43 44 void dump(String8 *dst, int spaces, int index) const; 45 46 private: 47 sp<SwAudioOutputDescriptor> mOutput; // Corresponding output stream 48 }; 49 50 51 class AudioPolicyMixCollection : public Vector<sp<AudioPolicyMix>> 52 { 53 public: 54 status_t getAudioPolicyMix(audio_devices_t deviceType, 55 const String8& address, sp<AudioPolicyMix> &policyMix) const; 56 57 status_t registerMix(AudioMix mix, sp<SwAudioOutputDescriptor> desc); 58 59 status_t unregisterMix(const AudioMix& mix); 60 61 void closeOutput(sp<SwAudioOutputDescriptor> &desc); 62 63 /** 64 * Try to find an output descriptor for the given attributes. 65 * 66 * @param[in] attributes to consider fowr the research of output descriptor. 67 * @param[out] desc to return if an primary output could be found. 68 * @param[out] secondaryDesc other desc that the audio should be routed to. 69 * @return OK if the request is valid 70 * otherwise if the request is not supported 71 */ 72 status_t getOutputForAttr(const audio_attributes_t& attributes, uid_t uid, 73 audio_output_flags_t flags, 74 sp<SwAudioOutputDescriptor> &primaryDesc, 75 std::vector<sp<SwAudioOutputDescriptor>> *secondaryDescs); 76 77 sp<DeviceDescriptor> getDeviceAndMixForInputSource(audio_source_t inputSource, 78 const DeviceVector &availableDeviceTypes, 79 sp<AudioPolicyMix> *policyMix) const; 80 81 /** 82 * @brief try to find a matching mix for a given output descriptor and returns the associated 83 * output device. 84 * @param output to be considered 85 * @param availableOutputDevices list of output devices currently reachable 86 * @return device selected from the mix attached to the output, null pointer otherwise 87 */ 88 sp<DeviceDescriptor> getDeviceAndMixForOutput(const sp<SwAudioOutputDescriptor> &output, 89 const DeviceVector &availableOutputDevices); 90 91 status_t getInputMixForAttr(audio_attributes_t attr, sp<AudioPolicyMix> *policyMix); 92 93 /** 94 * Updates the mix rules in order to make streams associated with the given uid 95 * be routed to the given audio devices. 96 * @param uid the uid for which the device affinity is set 97 * @param devices the vector of devices that this uid may be routed to. A typical 98 * use is to pass the devices associated with a given zone in a multi-zone setup. 99 * @return NO_ERROR if the update was successful, INVALID_OPERATION otherwise. 100 * An example of failure is when there are already rules in place to restrict 101 * a mix to the given uid (i.e. when a MATCH_UID rule was set for it). 102 */ 103 status_t setUidDeviceAffinities(uid_t uid, const Vector<AudioDeviceTypeAddr>& devices); 104 status_t removeUidDeviceAffinities(uid_t uid); 105 status_t getDevicesForUid(uid_t uid, Vector<AudioDeviceTypeAddr>& devices) const; 106 107 void dump(String8 *dst) const; 108 109 private: 110 enum class MixMatchStatus { MATCH, NO_MATCH, INVALID_MIX }; 111 MixMatchStatus mixMatch(const AudioMix* mix, size_t mixIndex, 112 const audio_attributes_t& attributes, uid_t uid); 113 }; 114 115 } // namespace android 116