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 "PolicyAudioPort.h" 21 #include "policy.h" 22 #include <media/AudioContainers.h> 23 #include <utils/String8.h> 24 #include <system/audio.h> 25 26 namespace android { 27 28 class HwModule; 29 30 // the IOProfile class describes the capabilities of an output or input stream. 31 // It is currently assumed that all combination of listed parameters are supported. 32 // It is used by the policy manager to determine if an output or input is suitable for 33 // a given use case, open/close it accordingly and connect/disconnect audio tracks 34 // to/from it. 35 class IOProfile : public AudioPort, public PolicyAudioPort 36 { 37 public: 38 IOProfile(const std::string &name, audio_port_role_t role); 39 40 virtual ~IOProfile() = default; 41 42 // For a Profile aka MixPort, tag name and name are equivalent. getTagName()43 virtual const std::string getTagName() const { return getName(); } 44 addAudioProfile(const sp<AudioProfile> & profile)45 virtual void addAudioProfile(const sp<AudioProfile> &profile) { 46 addAudioProfileAndSort(mProfiles, profile); 47 } 48 asAudioPort()49 virtual sp<AudioPort> asAudioPort() const { 50 return static_cast<AudioPort*>(const_cast<IOProfile*>(this)); 51 } 52 53 // FIXME: this is needed because shared MMAP stream clients use the same audio session. 54 // Once capture clients are tracked individually and not per session this can be removed 55 // MMAP no IRQ input streams do not have the default limitation of one active client 56 // max as they can be used in shared mode by the same application. 57 // NOTE: Please consider moving to AudioPort when addressing the FIXME 58 // NOTE: this works for explicit values set in audio_policy_configuration.xml because 59 // flags are parsed before maxActiveCount by the serializer. setFlags(uint32_t flags)60 void setFlags(uint32_t flags) override 61 { 62 AudioPort::setFlags(flags); 63 if (getRole() == AUDIO_PORT_ROLE_SINK && (flags & AUDIO_INPUT_FLAG_MMAP_NOIRQ) != 0) { 64 maxActiveCount = 0; 65 } 66 refreshMixerBehaviors(); 67 } 68 getMixerBehaviors()69 const MixerBehaviorSet& getMixerBehaviors() const { 70 return mMixerBehaviors; 71 } 72 73 enum CompatibilityScore{ 74 NO_MATCH = 0, 75 PARTIAL_MATCH = 1, 76 PARTIAL_MATCH_WITH_FLAG = 2, 77 EXACT_MATCH = 3 78 }; 79 80 /** 81 * @brief compatibilityScore: This method is used for input and direct output, 82 * and is not used for other output. 83 * Return the compatibility score to measure how much the IO profile is compatible 84 * with specified parameters. 85 * For input, flags is interpreted as audio_input_flags_t. 86 * TODO: merge audio_output_flags_t and audio_input_flags_t. 87 * 88 * @param devices vector of devices to be checked for compatibility 89 * @param samplingRate to be checked for compatibility. Must be specified 90 * @param updatedSamplingRate if non-NULL, it is assigned the actual sample rate. 91 * @param format to be checked for compatibility. Must be specified 92 * @param updatedFormat if non-NULL, it is assigned the actual format 93 * @param channelMask to be checked for compatibility. Must be specified 94 * @param updatedChannelMask if non-NULL, it is assigned the actual channel mask 95 * @param flags to be checked for compatibility 96 * @return how the IO profile is compatible with the given parameters. 97 */ 98 CompatibilityScore getCompatibilityScore(const DeviceVector &devices, 99 uint32_t samplingRate, 100 uint32_t *updatedSamplingRate, 101 audio_format_t format, 102 audio_format_t *updatedFormat, 103 audio_channel_mask_t channelMask, 104 audio_channel_mask_t *updatedChannelMask, 105 // FIXME parameter type 106 uint32_t flags) const; 107 108 /** 109 * @brief areAllDevicesSupported: Checks if the given devices are supported by the IO profile. 110 * 111 * @param devices vector of devices to be checked for compatibility 112 * @return true if all devices are supported, false otherwise. 113 */ 114 bool areAllDevicesSupported(const DeviceVector &devices) const; 115 116 /** 117 * @brief isCompatibleProfileForFlags: Checks if the IO profile is compatible with 118 * specified flags. 119 * 120 * @param flags to be checked for compatibility 121 * @return true if the profile is compatible, false otherwise. 122 */ 123 bool isCompatibleProfileForFlags(uint32_t flags) const; 124 125 void dump(String8 *dst, int spaces) const; 126 void log(); 127 hasSupportedDevices()128 bool hasSupportedDevices() const { return !mSupportedDevices.isEmpty(); } 129 supportsDeviceTypes(const DeviceTypeSet & deviceTypes)130 bool supportsDeviceTypes(const DeviceTypeSet& deviceTypes) const 131 { 132 const bool areOutputDevices = Intersection(deviceTypes, getAudioDeviceInAllSet()).empty(); 133 const bool devicesSupported = !mSupportedDevices.getDevicesFromTypes(deviceTypes).empty(); 134 return devicesSupported && 135 (!areOutputDevices || devicesSupportEncodedFormats(deviceTypes)); 136 } 137 138 /** 139 * @brief getTag 140 * @param deviceTypes to be considered 141 * @return tagName of first matching device for the considered types, empty string otherwise. 142 */ getTag(const DeviceTypeSet & deviceTypes)143 std::string getTag(const DeviceTypeSet& deviceTypes) const 144 { 145 if (supportsDeviceTypes(deviceTypes)) { 146 return mSupportedDevices.getDevicesFromTypes(deviceTypes).itemAt(0)->getTagName(); 147 } 148 return {}; 149 } 150 151 /** 152 * @brief supportsDevice 153 * @param device to be checked against 154 * forceCheckOnAddress if true, check on type and address whatever the type, otherwise 155 * the address enforcement is limited to "offical devices" that distinguishe on address 156 * @return true if the device is supported by type (for non bus / remote submix devices), 157 * true if the device is supported (both type and address) for bus / remote submix 158 * false otherwise 159 */ 160 bool supportsDevice(const sp<DeviceDescriptor> &device, bool forceCheckOnAddress = false) const 161 { 162 if (!device_distinguishes_on_address(device->type()) && !forceCheckOnAddress) { 163 return supportsDeviceTypes(DeviceTypeSet({device->type()})); 164 } 165 return mSupportedDevices.contains(device); 166 } 167 devicesSupportEncodedFormats(DeviceTypeSet deviceTypes)168 bool devicesSupportEncodedFormats(DeviceTypeSet deviceTypes) const 169 { 170 if (deviceTypes.empty()) { 171 return true; // required for getOffloadSupport() check 172 } 173 DeviceVector deviceList = 174 mSupportedDevices.getDevicesFromTypes(deviceTypes); 175 for (const auto& device : deviceList) { 176 if (device->hasCurrentEncodedFormat()) { 177 return true; 178 } 179 } 180 return false; 181 } 182 183 bool containsSingleDeviceSupportingEncodedFormats(const sp<DeviceDescriptor>& device) const; 184 clearSupportedDevices()185 void clearSupportedDevices() { mSupportedDevices.clear(); } addSupportedDevice(const sp<DeviceDescriptor> & device)186 void addSupportedDevice(const sp<DeviceDescriptor> &device) 187 { 188 mSupportedDevices.add(device); 189 } removeSupportedDevice(const sp<DeviceDescriptor> & device)190 void removeSupportedDevice(const sp<DeviceDescriptor> &device) 191 { 192 ssize_t ret = mSupportedDevices.indexOf(device); 193 if (ret >= 0 && !mSupportedDevices.itemAt(ret)->isDynamic()) { 194 // devices equality checks only type, address, name and format 195 // Prevents from removing non dynamically added devices 196 return; 197 } 198 mSupportedDevices.remove(device); 199 } setSupportedDevices(const DeviceVector & devices)200 void setSupportedDevices(const DeviceVector &devices) 201 { 202 mSupportedDevices = devices; 203 } 204 getSupportedDevices()205 const DeviceVector &getSupportedDevices() const { return mSupportedDevices; } 206 canOpenNewIo()207 bool canOpenNewIo() { 208 if (maxOpenCount == 0 || curOpenCount < maxOpenCount) { 209 return true; 210 } 211 return false; 212 } 213 canStartNewIo()214 bool canStartNewIo() { 215 if (maxActiveCount == 0 || curActiveCount < maxActiveCount) { 216 return true; 217 } 218 return false; 219 } 220 221 void toSupportedMixerAttributes(std::vector<audio_mixer_attributes_t>* mixerAttributes) const; 222 223 status_t readFromParcelable(const media::AudioPortFw& parcelable); 224 225 void importAudioPort(const audio_port_v7& port) override; 226 227 // Number of streams currently opened for this profile. 228 uint32_t curOpenCount; 229 // Number of streams currently active for this profile. This is not the number of active clients 230 // (AudioTrack or AudioRecord) but the number of active HAL streams. 231 uint32_t curActiveCount; 232 233 private: 234 void refreshMixerBehaviors(); 235 CompatibilityScore getFlagsCompatibleScore(uint32_t flags) const; 236 237 DeviceVector mSupportedDevices; // supported devices: this input/output can be routed from/to 238 239 MixerBehaviorSet mMixerBehaviors; 240 }; 241 242 class InputProfile : public IOProfile 243 { 244 public: InputProfile(const std::string & name)245 explicit InputProfile(const std::string &name) : IOProfile(name, AUDIO_PORT_ROLE_SINK) {} 246 }; 247 248 class OutputProfile : public IOProfile 249 { 250 public: OutputProfile(const std::string & name)251 explicit OutputProfile(const std::string &name) : IOProfile(name, AUDIO_PORT_ROLE_SOURCE) {} 252 }; 253 254 } // namespace android 255