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 /** 74 * @brief isCompatibleProfile: This method is used for input and direct output, 75 * and is not used for other output. 76 * Checks if the IO profile is compatible with specified parameters. 77 * For input, flags is interpreted as audio_input_flags_t. 78 * TODO: merge audio_output_flags_t and audio_input_flags_t. 79 * 80 * @param devices vector of devices to be checked for compatibility 81 * @param samplingRate to be checked for compatibility. Must be specified 82 * @param updatedSamplingRate if non-NULL, it is assigned the actual sample rate. 83 * @param format to be checked for compatibility. Must be specified 84 * @param updatedFormat if non-NULL, it is assigned the actual format 85 * @param channelMask to be checked for compatibility. Must be specified 86 * @param updatedChannelMask if non-NULL, it is assigned the actual channel mask 87 * @param flags to be checked for compatibility 88 * @param exactMatchRequiredForInputFlags true if exact match is required on flags 89 * @return true if the profile is compatible, false otherwise. 90 */ 91 bool isCompatibleProfile(const DeviceVector &devices, 92 uint32_t samplingRate, 93 uint32_t *updatedSamplingRate, 94 audio_format_t format, 95 audio_format_t *updatedFormat, 96 audio_channel_mask_t channelMask, 97 audio_channel_mask_t *updatedChannelMask, 98 // FIXME parameter type 99 uint32_t flags, 100 bool exactMatchRequiredForInputFlags = false) const; 101 102 /** 103 * @brief areAllDevicesSupported: Checks if the given devices are supported by the IO profile. 104 * 105 * @param devices vector of devices to be checked for compatibility 106 * @return true if all devices are supported, false otherwise. 107 */ 108 bool areAllDevicesSupported(const DeviceVector &devices) const; 109 110 /** 111 * @brief isCompatibleProfileForFlags: Checks if the IO profile is compatible with 112 * specified flags. 113 * 114 * @param flags to be checked for compatibility 115 * @param exactMatchRequiredForInputFlags true if exact match is required on flags 116 * @return true if the profile is compatible, false otherwise. 117 */ 118 bool isCompatibleProfileForFlags(uint32_t flags, 119 bool exactMatchRequiredForInputFlags = false) const; 120 121 void dump(String8 *dst, int spaces) const; 122 void log(); 123 hasSupportedDevices()124 bool hasSupportedDevices() const { return !mSupportedDevices.isEmpty(); } 125 supportsDeviceTypes(const DeviceTypeSet & deviceTypes)126 bool supportsDeviceTypes(const DeviceTypeSet& deviceTypes) const 127 { 128 const bool areOutputDevices = Intersection(deviceTypes, getAudioDeviceInAllSet()).empty(); 129 const bool devicesSupported = !mSupportedDevices.getDevicesFromTypes(deviceTypes).empty(); 130 return devicesSupported && 131 (!areOutputDevices || devicesSupportEncodedFormats(deviceTypes)); 132 } 133 134 /** 135 * @brief getTag 136 * @param deviceTypes to be considered 137 * @return tagName of first matching device for the considered types, empty string otherwise. 138 */ getTag(const DeviceTypeSet & deviceTypes)139 std::string getTag(const DeviceTypeSet& deviceTypes) const 140 { 141 if (supportsDeviceTypes(deviceTypes)) { 142 return mSupportedDevices.getDevicesFromTypes(deviceTypes).itemAt(0)->getTagName(); 143 } 144 return {}; 145 } 146 147 /** 148 * @brief supportsDevice 149 * @param device to be checked against 150 * forceCheckOnAddress if true, check on type and address whatever the type, otherwise 151 * the address enforcement is limited to "offical devices" that distinguishe on address 152 * @return true if the device is supported by type (for non bus / remote submix devices), 153 * true if the device is supported (both type and address) for bus / remote submix 154 * false otherwise 155 */ 156 bool supportsDevice(const sp<DeviceDescriptor> &device, bool forceCheckOnAddress = false) const 157 { 158 if (!device_distinguishes_on_address(device->type()) && !forceCheckOnAddress) { 159 return supportsDeviceTypes(DeviceTypeSet({device->type()})); 160 } 161 return mSupportedDevices.contains(device); 162 } 163 devicesSupportEncodedFormats(DeviceTypeSet deviceTypes)164 bool devicesSupportEncodedFormats(DeviceTypeSet deviceTypes) const 165 { 166 if (deviceTypes.empty()) { 167 return true; // required for getOffloadSupport() check 168 } 169 DeviceVector deviceList = 170 mSupportedDevices.getDevicesFromTypes(deviceTypes); 171 for (const auto& device : deviceList) { 172 if (device->hasCurrentEncodedFormat()) { 173 return true; 174 } 175 } 176 return false; 177 } 178 179 bool containsSingleDeviceSupportingEncodedFormats(const sp<DeviceDescriptor>& device) const; 180 clearSupportedDevices()181 void clearSupportedDevices() { mSupportedDevices.clear(); } addSupportedDevice(const sp<DeviceDescriptor> & device)182 void addSupportedDevice(const sp<DeviceDescriptor> &device) 183 { 184 mSupportedDevices.add(device); 185 } removeSupportedDevice(const sp<DeviceDescriptor> & device)186 void removeSupportedDevice(const sp<DeviceDescriptor> &device) 187 { 188 ssize_t ret = mSupportedDevices.indexOf(device); 189 if (ret >= 0 && !mSupportedDevices.itemAt(ret)->isDynamic()) { 190 // devices equality checks only type, address, name and format 191 // Prevents from removing non dynamically added devices 192 return; 193 } 194 mSupportedDevices.remove(device); 195 } setSupportedDevices(const DeviceVector & devices)196 void setSupportedDevices(const DeviceVector &devices) 197 { 198 mSupportedDevices = devices; 199 } 200 getSupportedDevices()201 const DeviceVector &getSupportedDevices() const { return mSupportedDevices; } 202 canOpenNewIo()203 bool canOpenNewIo() { 204 if (maxOpenCount == 0 || curOpenCount < maxOpenCount) { 205 return true; 206 } 207 return false; 208 } 209 canStartNewIo()210 bool canStartNewIo() { 211 if (maxActiveCount == 0 || curActiveCount < maxActiveCount) { 212 return true; 213 } 214 return false; 215 } 216 217 void toSupportedMixerAttributes(std::vector<audio_mixer_attributes_t>* mixerAttributes) const; 218 219 status_t readFromParcelable(const media::AudioPortFw& parcelable); 220 221 // Number of streams currently opened for this profile. 222 uint32_t curOpenCount; 223 // Number of streams currently active for this profile. This is not the number of active clients 224 // (AudioTrack or AudioRecord) but the number of active HAL streams. 225 uint32_t curActiveCount; 226 227 private: 228 void refreshMixerBehaviors(); 229 230 DeviceVector mSupportedDevices; // supported devices: this input/output can be routed from/to 231 232 MixerBehaviorSet mMixerBehaviors; 233 }; 234 235 class InputProfile : public IOProfile 236 { 237 public: InputProfile(const std::string & name)238 explicit InputProfile(const std::string &name) : IOProfile(name, AUDIO_PORT_ROLE_SINK) {} 239 }; 240 241 class OutputProfile : public IOProfile 242 { 243 public: OutputProfile(const std::string & name)244 explicit OutputProfile(const std::string &name) : IOProfile(name, AUDIO_PORT_ROLE_SOURCE) {} 245 }; 246 247 } // namespace android 248