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 status_t readFromParcelable(const media::AudioPortFw& parcelable) override; 93 94 void setEncapsulationInfoFromHal(AudioPolicyClientInterface *clientInterface); 95 96 void setPreferredConfig(const audio_config_base_t * preferredConfig); 97 98 void dump(String8 *dst, int spaces, bool verbose = true) const; 99 100 private: 101 template <typename T, std::enable_if_t<std::is_same<T, struct audio_port>::value 102 || std::is_same<T, struct audio_port_v7>::value, int> = 0> toAudioPortInternal(T * port)103 void toAudioPortInternal(T* port) const { 104 DeviceDescriptorBase::toAudioPort(port); 105 port->ext.device.hw_module = getModuleHandle(); 106 } 107 108 std::string mTagName; // Unique human readable identifier for a device port found in conf file. 109 audio_format_t mCurrentEncodedFormat; 110 bool mIsDynamic = false; 111 std::string mDeclaredAddress; // Original device address 112 std::optional<audio_config_base_t> mPreferredConfig; 113 }; 114 115 class DeviceVector : public SortedVector<sp<DeviceDescriptor> > 116 { 117 public: DeviceVector()118 DeviceVector() : SortedVector() {} DeviceVector(const sp<DeviceDescriptor> & item)119 explicit DeviceVector(const sp<DeviceDescriptor>& item) : DeviceVector() 120 { 121 add(item); 122 } 123 124 ssize_t add(const sp<DeviceDescriptor>& item); 125 void add(const DeviceVector &devices); 126 ssize_t remove(const sp<DeviceDescriptor>& item); 127 void remove(const DeviceVector &devices); 128 ssize_t indexOf(const sp<DeviceDescriptor>& item) const; 129 types()130 DeviceTypeSet types() const { return mDeviceTypes; } 131 132 // If 'address' is empty and 'codec' is AUDIO_FORMAT_DEFAULT, a device with a non-empty 133 // address may be returned if there is no device with the specified 'type' and empty address. 134 sp<DeviceDescriptor> getDevice(audio_devices_t type, const String8 &address, 135 audio_format_t codec) const; 136 DeviceVector getDevicesFromTypes(const DeviceTypeSet& types) const; getDevicesFromType(audio_devices_t type)137 DeviceVector getDevicesFromType(audio_devices_t type) const { 138 return getDevicesFromTypes({type}); 139 } 140 141 /** 142 * @brief getDeviceFromId 143 * @param id of the DeviceDescriptor to seach (aka Port handle). 144 * @return DeviceDescriptor associated to port id if found, nullptr otherwise. If the id is 145 * equal to AUDIO_PORT_HANDLE_NONE, it also returns a nullptr. 146 */ 147 sp<DeviceDescriptor> getDeviceFromId(audio_port_handle_t id) const; 148 sp<DeviceDescriptor> getDeviceFromTagName(const std::string &tagName) const; 149 DeviceVector getDevicesFromHwModule(audio_module_handle_t moduleHandle) const; 150 151 DeviceVector getFirstDevicesFromTypes(std::vector<audio_devices_t> orderedTypes) const; 152 sp<DeviceDescriptor> getFirstExistingDevice(std::vector<audio_devices_t> orderedTypes) const; 153 154 // Return device descriptor that is used to open an input/output stream. 155 // Null pointer will be returned if 156 // 1) this collection is empty 157 // 2) the device descriptors are not the same category(input or output) 158 // 3) there are more than one device type for input case 159 // 4) the combination of all devices is invalid for selection 160 sp<DeviceDescriptor> getDeviceForOpening() const; 161 162 // Return the device descriptor that matches the given AudioDeviceTypeAddr 163 sp<DeviceDescriptor> getDeviceFromDeviceTypeAddr( 164 const AudioDeviceTypeAddr& deviceTypeAddr) const; 165 166 // Return the device vector that contains device descriptor whose AudioDeviceTypeAddr appears 167 // in the given AudioDeviceTypeAddrVector 168 DeviceVector getDevicesFromDeviceTypeAddrVec( 169 const AudioDeviceTypeAddrVector& deviceTypeAddrVector) const; 170 171 // Return the device vector that contains device descriptor whose AudioDeviceTypeAddr appears 172 // in the given AudioDeviceTypeAddrVector 173 AudioDeviceTypeAddrVector toTypeAddrVector() const; 174 175 // If there are devices with the given type and the devices to add is not empty, 176 // remove all the devices with the given type and add all the devices to add. 177 void replaceDevicesByType(audio_devices_t typeToRemove, const DeviceVector &devicesToAdd); 178 containsDeviceAmongTypes(const DeviceTypeSet & deviceTypes)179 bool containsDeviceAmongTypes(const DeviceTypeSet& deviceTypes) const { 180 return !Intersection(mDeviceTypes, deviceTypes).empty(); 181 } 182 containsDeviceWithType(audio_devices_t deviceType)183 bool containsDeviceWithType(audio_devices_t deviceType) const { 184 return containsDeviceAmongTypes({deviceType}); 185 } 186 onlyContainsDevicesWithType(audio_devices_t deviceType)187 bool onlyContainsDevicesWithType(audio_devices_t deviceType) const { 188 return isSingleDeviceType(mDeviceTypes, deviceType); 189 } 190 onlyContainsDevice(const sp<DeviceDescriptor> & item)191 bool onlyContainsDevice(const sp<DeviceDescriptor>& item) const { 192 return this->size() == 1 && contains(item); 193 } 194 contains(const sp<DeviceDescriptor> & item)195 bool contains(const sp<DeviceDescriptor>& item) const { return indexOf(item) >= 0; } 196 197 /** 198 * @brief containsAtLeastOne 199 * @param devices vector of devices to check against. 200 * @return true if the DeviceVector contains at list one of the devices from the given vector. 201 */ 202 bool containsAtLeastOne(const DeviceVector &devices) const; 203 204 /** 205 * @brief containsAllDevices 206 * @param devices vector of devices to check against. 207 * @return true if the DeviceVector contains all the devices from the given vector 208 */ 209 bool containsAllDevices(const DeviceVector &devices) const; 210 211 /** 212 * @brief filter the devices supported by this collection against another collection 213 * @param devices to filter against 214 * @return a filtered DeviceVector 215 */ 216 DeviceVector filter(const DeviceVector &devices) const; 217 218 /** 219 * @brief filter the devices supported by this collection before sending 220 * then to the Engine via AudioPolicyManagerObserver interface 221 * @return a filtered DeviceVector 222 */ 223 DeviceVector filterForEngine() const; 224 225 /** 226 * @brief merge two vectors. As SortedVector Implementation is buggy (it does not check the size 227 * of the destination vector, only of the source, it provides a safe implementation 228 * @param devices source device vector to merge with 229 * @return size of the merged vector. 230 */ merge(const DeviceVector & devices)231 ssize_t merge(const DeviceVector &devices) 232 { 233 if (isEmpty()) { 234 add(devices); 235 return size(); 236 } 237 ssize_t ret = SortedVector::merge(devices); 238 refreshTypes(); 239 return ret; 240 } 241 242 /** 243 * @brief operator == DeviceVector are equals if all the DeviceDescriptor can be found (aka 244 * DeviceDescriptor with same type and address) and the vector has same size. 245 * @param right DeviceVector to compare to. 246 * @return true if right contains the same device and has the same size. 247 */ 248 bool operator==(const DeviceVector &right) const 249 { 250 if (size() != right.size()) { 251 return false; 252 } 253 for (const auto &device : *this) { 254 if (right.indexOf(device) < 0) { 255 return false; 256 } 257 } 258 return true; 259 } 260 261 bool operator!=(const DeviceVector &right) const 262 { 263 return !operator==(right); 264 } 265 266 /** 267 * @brief getFirstValidAddress 268 * @return the first valid address of a list of device, "" if no device with valid address 269 * found. 270 * This helper function helps maintaining compatibility with legacy where we used to have a 271 * devices mask and an address. 272 */ getFirstValidAddress()273 String8 getFirstValidAddress() const 274 { 275 for (const auto &device : *this) { 276 if (device->address() != "") { 277 return String8(device->address().c_str()); 278 } 279 } 280 return String8(""); 281 } 282 getSupportedProfiles()283 const AudioProfileVector& getSupportedProfiles() { return mSupportedProfiles; } 284 285 // Return a string to describe the DeviceVector. The sensitive information will only be 286 // added to the string if `includeSensitiveInfo` is true. 287 std::string toString(bool includeSensitiveInfo = false) const; 288 289 void dump(String8 *dst, const String8 &tag, int spaces = 0, bool verbose = true) const; 290 291 protected: 292 int do_compare(const void* lhs, const void* rhs) const; 293 private: 294 void refreshTypes(); 295 void refreshAudioProfiles(); 296 DeviceTypeSet mDeviceTypes; 297 AudioProfileVector mSupportedProfiles; 298 }; 299 300 } // namespace android 301