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 "PolicyAudioPort.h" 20 #include <media/AudioContainers.h> 21 #include <media/DeviceDescriptorBase.h> 22 #include <utils/Errors.h> 23 #include <utils/String8.h> 24 #include <utils/SortedVector.h> 25 #include <cutils/config_utils.h> 26 #include <system/audio.h> 27 #include <system/audio_policy.h> 28 29 namespace android { 30 31 class AudioPolicyClientInterface; 32 33 class DeviceDescriptor : public DeviceDescriptorBase, 34 public PolicyAudioPort, public PolicyAudioPortConfig 35 { 36 public: 37 // Note that empty name refers by convention to a generic device. 38 explicit DeviceDescriptor(audio_devices_t type); 39 DeviceDescriptor(audio_devices_t type, const std::string &tagName, 40 const FormatVector &encodedFormats = FormatVector{}); 41 DeviceDescriptor(audio_devices_t type, const std::string &tagName, 42 const std::string &address, const FormatVector &encodedFormats = FormatVector{}); 43 DeviceDescriptor(const AudioDeviceTypeAddr &deviceTypeAddr, const std::string &tagName = "", 44 const FormatVector &encodedFormats = FormatVector{}); 45 46 virtual ~DeviceDescriptor() = default; 47 addAudioProfile(const sp<AudioProfile> & profile)48 virtual void addAudioProfile(const sp<AudioProfile> &profile) { 49 addAudioProfileAndSort(mProfiles, profile); 50 } 51 getTagName()52 virtual const std::string getTagName() const { return mTagName; } 53 getEncodedFormat()54 audio_format_t getEncodedFormat() { return mCurrentEncodedFormat; } 55 setEncodedFormat(audio_format_t format)56 void setEncodedFormat(audio_format_t format) { 57 mCurrentEncodedFormat = format; 58 } 59 60 bool equals(const sp<DeviceDescriptor>& other) const; 61 62 bool hasCurrentEncodedFormat() const; 63 setDynamic()64 void setDynamic() { mIsDynamic = true; } isDynamic()65 bool isDynamic() const { return mIsDynamic; } 66 67 // PolicyAudioPortConfig getPolicyAudioPort()68 virtual sp<PolicyAudioPort> getPolicyAudioPort() const { 69 return static_cast<PolicyAudioPort*>(const_cast<DeviceDescriptor*>(this)); 70 } 71 72 // AudioPortConfig 73 virtual status_t applyAudioPortConfig(const struct audio_port_config *config, 74 struct audio_port_config *backupConfig = NULL); 75 virtual void toAudioPortConfig(struct audio_port_config *dstConfig, 76 const struct audio_port_config *srcConfig = NULL) const; 77 78 // PolicyAudioPort asAudioPort()79 virtual sp<AudioPort> asAudioPort() const { 80 return static_cast<AudioPort*>(const_cast<DeviceDescriptor*>(this)); 81 } 82 virtual void attach(const sp<HwModule>& module); 83 virtual void detach(); 84 85 // AudioPort 86 virtual void toAudioPort(struct audio_port *port) const; 87 virtual void toAudioPort(struct audio_port_v7 *port) const; 88 89 void importAudioPortAndPickAudioProfile(const sp<PolicyAudioPort>& policyPort, 90 bool force = false); 91 92 void setEncapsulationInfoFromHal(AudioPolicyClientInterface *clientInterface); 93 94 void dump(String8 *dst, int spaces, bool verbose = true) const; 95 96 private: 97 template <typename T, std::enable_if_t<std::is_same<T, struct audio_port>::value 98 || std::is_same<T, struct audio_port_v7>::value, int> = 0> toAudioPortInternal(T * port)99 void toAudioPortInternal(T* port) const { 100 DeviceDescriptorBase::toAudioPort(port); 101 port->ext.device.hw_module = getModuleHandle(); 102 } 103 104 std::string mTagName; // Unique human readable identifier for a device port found in conf file. 105 audio_format_t mCurrentEncodedFormat; 106 bool mIsDynamic = false; 107 const std::string mDeclaredAddress; // Original device address 108 }; 109 110 class DeviceVector : public SortedVector<sp<DeviceDescriptor> > 111 { 112 public: DeviceVector()113 DeviceVector() : SortedVector() {} DeviceVector(const sp<DeviceDescriptor> & item)114 explicit DeviceVector(const sp<DeviceDescriptor>& item) : DeviceVector() 115 { 116 add(item); 117 } 118 119 ssize_t add(const sp<DeviceDescriptor>& item); 120 void add(const DeviceVector &devices); 121 ssize_t remove(const sp<DeviceDescriptor>& item); 122 void remove(const DeviceVector &devices); 123 ssize_t indexOf(const sp<DeviceDescriptor>& item) const; 124 types()125 DeviceTypeSet types() const { return mDeviceTypes; } 126 127 // If 'address' is empty and 'codec' is AUDIO_FORMAT_DEFAULT, a device with a non-empty 128 // address may be returned if there is no device with the specified 'type' and empty address. 129 sp<DeviceDescriptor> getDevice(audio_devices_t type, const String8 &address, 130 audio_format_t codec) const; 131 DeviceVector getDevicesFromTypes(const DeviceTypeSet& types) const; getDevicesFromType(audio_devices_t type)132 DeviceVector getDevicesFromType(audio_devices_t type) const { 133 return getDevicesFromTypes({type}); 134 } 135 136 /** 137 * @brief getDeviceFromId 138 * @param id of the DeviceDescriptor to seach (aka Port handle). 139 * @return DeviceDescriptor associated to port id if found, nullptr otherwise. If the id is 140 * equal to AUDIO_PORT_HANDLE_NONE, it also returns a nullptr. 141 */ 142 sp<DeviceDescriptor> getDeviceFromId(audio_port_handle_t id) const; 143 sp<DeviceDescriptor> getDeviceFromTagName(const std::string &tagName) const; 144 DeviceVector getDevicesFromHwModule(audio_module_handle_t moduleHandle) const; 145 146 DeviceVector getFirstDevicesFromTypes(std::vector<audio_devices_t> orderedTypes) const; 147 sp<DeviceDescriptor> getFirstExistingDevice(std::vector<audio_devices_t> orderedTypes) const; 148 149 // Return device descriptor that is used to open an input/output stream. 150 // Null pointer will be returned if 151 // 1) this collection is empty 152 // 2) the device descriptors are not the same category(input or output) 153 // 3) there are more than one device type for input case 154 // 4) the combination of all devices is invalid for selection 155 sp<DeviceDescriptor> getDeviceForOpening() const; 156 157 // Return the device descriptor that matches the given AudioDeviceTypeAddr 158 sp<DeviceDescriptor> getDeviceFromDeviceTypeAddr( 159 const AudioDeviceTypeAddr& deviceTypeAddr) const; 160 161 // Return the device vector that contains device descriptor whose AudioDeviceTypeAddr appears 162 // in the given AudioDeviceTypeAddrVector 163 DeviceVector getDevicesFromDeviceTypeAddrVec( 164 const AudioDeviceTypeAddrVector& deviceTypeAddrVector) const; 165 166 // Return the device vector that contains device descriptor whose AudioDeviceTypeAddr appears 167 // in the given AudioDeviceTypeAddrVector 168 AudioDeviceTypeAddrVector toTypeAddrVector() const; 169 170 // If there are devices with the given type and the devices to add is not empty, 171 // remove all the devices with the given type and add all the devices to add. 172 void replaceDevicesByType(audio_devices_t typeToRemove, const DeviceVector &devicesToAdd); 173 containsDeviceAmongTypes(const DeviceTypeSet & deviceTypes)174 bool containsDeviceAmongTypes(const DeviceTypeSet& deviceTypes) const { 175 return !Intersection(mDeviceTypes, deviceTypes).empty(); 176 } 177 containsDeviceWithType(audio_devices_t deviceType)178 bool containsDeviceWithType(audio_devices_t deviceType) const { 179 return containsDeviceAmongTypes({deviceType}); 180 } 181 onlyContainsDevicesWithType(audio_devices_t deviceType)182 bool onlyContainsDevicesWithType(audio_devices_t deviceType) const { 183 return isSingleDeviceType(mDeviceTypes, deviceType); 184 } 185 contains(const sp<DeviceDescriptor> & item)186 bool contains(const sp<DeviceDescriptor>& item) const { return indexOf(item) >= 0; } 187 188 /** 189 * @brief containsAtLeastOne 190 * @param devices vector of devices to check against. 191 * @return true if the DeviceVector contains at list one of the devices from the given vector. 192 */ 193 bool containsAtLeastOne(const DeviceVector &devices) const; 194 195 /** 196 * @brief containsAllDevices 197 * @param devices vector of devices to check against. 198 * @return true if the DeviceVector contains all the devices from the given vector 199 */ 200 bool containsAllDevices(const DeviceVector &devices) const; 201 202 /** 203 * @brief filter the devices supported by this collection against another collection 204 * @param devices to filter against 205 * @return a filtered DeviceVector 206 */ 207 DeviceVector filter(const DeviceVector &devices) const; 208 209 /** 210 * @brief filter the devices supported by this collection before sending 211 * then to the Engine via AudioPolicyManagerObserver interface 212 * @return a filtered DeviceVector 213 */ 214 DeviceVector filterForEngine() const; 215 216 /** 217 * @brief merge two vectors. As SortedVector Implementation is buggy (it does not check the size 218 * of the destination vector, only of the source, it provides a safe implementation 219 * @param devices source device vector to merge with 220 * @return size of the merged vector. 221 */ merge(const DeviceVector & devices)222 ssize_t merge(const DeviceVector &devices) 223 { 224 if (isEmpty()) { 225 add(devices); 226 return size(); 227 } 228 ssize_t ret = SortedVector::merge(devices); 229 refreshTypes(); 230 return ret; 231 } 232 233 /** 234 * @brief operator == DeviceVector are equals if all the DeviceDescriptor can be found (aka 235 * DeviceDescriptor with same type and address) and the vector has same size. 236 * @param right DeviceVector to compare to. 237 * @return true if right contains the same device and has the same size. 238 */ 239 bool operator==(const DeviceVector &right) const 240 { 241 if (size() != right.size()) { 242 return false; 243 } 244 for (const auto &device : *this) { 245 if (right.indexOf(device) < 0) { 246 return false; 247 } 248 } 249 return true; 250 } 251 252 bool operator!=(const DeviceVector &right) const 253 { 254 return !operator==(right); 255 } 256 257 /** 258 * @brief getFirstValidAddress 259 * @return the first valid address of a list of device, "" if no device with valid address 260 * found. 261 * This helper function helps maintaining compatibility with legacy where we used to have a 262 * devices mask and an address. 263 */ getFirstValidAddress()264 String8 getFirstValidAddress() const 265 { 266 for (const auto &device : *this) { 267 if (device->address() != "") { 268 return String8(device->address().c_str()); 269 } 270 } 271 return String8(""); 272 } 273 getSupportedProfiles()274 const AudioProfileVector& getSupportedProfiles() { return mSupportedProfiles; } 275 276 // Return a string to describe the DeviceVector. The sensitive information will only be 277 // added to the string if `includeSensitiveInfo` is true. 278 std::string toString(bool includeSensitiveInfo = false) const; 279 280 void dump(String8 *dst, const String8 &tag, int spaces = 0, bool verbose = true) const; 281 282 protected: 283 int do_compare(const void* lhs, const void* rhs) const; 284 private: 285 void refreshTypes(); 286 void refreshAudioProfiles(); 287 DeviceTypeSet mDeviceTypes; 288 AudioProfileVector mSupportedProfiles; 289 }; 290 291 } // namespace android 292