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 onlyContainsDevice(const sp<DeviceDescriptor> & item)186 bool onlyContainsDevice(const sp<DeviceDescriptor>& item) const { 187 return this->size() == 1 && contains(item); 188 } 189 contains(const sp<DeviceDescriptor> & item)190 bool contains(const sp<DeviceDescriptor>& item) const { return indexOf(item) >= 0; } 191 192 /** 193 * @brief containsAtLeastOne 194 * @param devices vector of devices to check against. 195 * @return true if the DeviceVector contains at list one of the devices from the given vector. 196 */ 197 bool containsAtLeastOne(const DeviceVector &devices) const; 198 199 /** 200 * @brief containsAllDevices 201 * @param devices vector of devices to check against. 202 * @return true if the DeviceVector contains all the devices from the given vector 203 */ 204 bool containsAllDevices(const DeviceVector &devices) const; 205 206 /** 207 * @brief filter the devices supported by this collection against another collection 208 * @param devices to filter against 209 * @return a filtered DeviceVector 210 */ 211 DeviceVector filter(const DeviceVector &devices) const; 212 213 /** 214 * @brief filter the devices supported by this collection before sending 215 * then to the Engine via AudioPolicyManagerObserver interface 216 * @return a filtered DeviceVector 217 */ 218 DeviceVector filterForEngine() const; 219 220 /** 221 * @brief merge two vectors. As SortedVector Implementation is buggy (it does not check the size 222 * of the destination vector, only of the source, it provides a safe implementation 223 * @param devices source device vector to merge with 224 * @return size of the merged vector. 225 */ merge(const DeviceVector & devices)226 ssize_t merge(const DeviceVector &devices) 227 { 228 if (isEmpty()) { 229 add(devices); 230 return size(); 231 } 232 ssize_t ret = SortedVector::merge(devices); 233 refreshTypes(); 234 return ret; 235 } 236 237 /** 238 * @brief operator == DeviceVector are equals if all the DeviceDescriptor can be found (aka 239 * DeviceDescriptor with same type and address) and the vector has same size. 240 * @param right DeviceVector to compare to. 241 * @return true if right contains the same device and has the same size. 242 */ 243 bool operator==(const DeviceVector &right) const 244 { 245 if (size() != right.size()) { 246 return false; 247 } 248 for (const auto &device : *this) { 249 if (right.indexOf(device) < 0) { 250 return false; 251 } 252 } 253 return true; 254 } 255 256 bool operator!=(const DeviceVector &right) const 257 { 258 return !operator==(right); 259 } 260 261 /** 262 * @brief getFirstValidAddress 263 * @return the first valid address of a list of device, "" if no device with valid address 264 * found. 265 * This helper function helps maintaining compatibility with legacy where we used to have a 266 * devices mask and an address. 267 */ getFirstValidAddress()268 String8 getFirstValidAddress() const 269 { 270 for (const auto &device : *this) { 271 if (device->address() != "") { 272 return String8(device->address().c_str()); 273 } 274 } 275 return String8(""); 276 } 277 getSupportedProfiles()278 const AudioProfileVector& getSupportedProfiles() { return mSupportedProfiles; } 279 280 // Return a string to describe the DeviceVector. The sensitive information will only be 281 // added to the string if `includeSensitiveInfo` is true. 282 std::string toString(bool includeSensitiveInfo = false) const; 283 284 void dump(String8 *dst, const String8 &tag, int spaces = 0, bool verbose = true) const; 285 286 protected: 287 int do_compare(const void* lhs, const void* rhs) const; 288 private: 289 void refreshTypes(); 290 void refreshAudioProfiles(); 291 DeviceTypeSet mDeviceTypes; 292 AudioProfileVector mSupportedProfiles; 293 }; 294 295 } // namespace android 296