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 ~DeviceDescriptor()46 virtual ~DeviceDescriptor() {} 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 encodedFormats()54 const FormatVector& encodedFormats() const { return mEncodedFormats; } 55 getEncodedFormat()56 audio_format_t getEncodedFormat() { return mCurrentEncodedFormat; } 57 setEncodedFormat(audio_format_t format)58 void setEncodedFormat(audio_format_t format) { 59 mCurrentEncodedFormat = format; 60 } 61 62 bool equals(const sp<DeviceDescriptor>& other) const; 63 64 bool hasCurrentEncodedFormat() const; 65 66 bool supportsFormat(audio_format_t format); 67 68 // PolicyAudioPortConfig getPolicyAudioPort()69 virtual sp<PolicyAudioPort> getPolicyAudioPort() const { 70 return static_cast<PolicyAudioPort*>(const_cast<DeviceDescriptor*>(this)); 71 } 72 73 // AudioPortConfig 74 virtual status_t applyAudioPortConfig(const struct audio_port_config *config, 75 struct audio_port_config *backupConfig = NULL); 76 virtual void toAudioPortConfig(struct audio_port_config *dstConfig, 77 const struct audio_port_config *srcConfig = NULL) const; 78 79 // PolicyAudioPort asAudioPort()80 virtual sp<AudioPort> asAudioPort() const { 81 return static_cast<AudioPort*>(const_cast<DeviceDescriptor*>(this)); 82 } 83 virtual void attach(const sp<HwModule>& module); 84 virtual void detach(); 85 86 // AudioPort 87 virtual void toAudioPort(struct audio_port *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, int index, bool verbose = true) const; 95 96 private: 97 std::string mTagName; // Unique human readable identifier for a device port found in conf file. 98 FormatVector mEncodedFormats; 99 audio_format_t mCurrentEncodedFormat; 100 }; 101 102 class DeviceVector : public SortedVector<sp<DeviceDescriptor> > 103 { 104 public: DeviceVector()105 DeviceVector() : SortedVector() {} DeviceVector(const sp<DeviceDescriptor> & item)106 explicit DeviceVector(const sp<DeviceDescriptor>& item) : DeviceVector() 107 { 108 add(item); 109 } 110 111 ssize_t add(const sp<DeviceDescriptor>& item); 112 void add(const DeviceVector &devices); 113 ssize_t remove(const sp<DeviceDescriptor>& item); 114 void remove(const DeviceVector &devices); 115 ssize_t indexOf(const sp<DeviceDescriptor>& item) const; 116 types()117 DeviceTypeSet types() const { return mDeviceTypes; } 118 119 // If 'address' is empty and 'codec' is AUDIO_FORMAT_DEFAULT, a device with a non-empty 120 // address may be returned if there is no device with the specified 'type' and empty address. 121 sp<DeviceDescriptor> getDevice(audio_devices_t type, const String8 &address, 122 audio_format_t codec) const; 123 DeviceVector getDevicesFromTypes(const DeviceTypeSet& types) const; getDevicesFromType(audio_devices_t type)124 DeviceVector getDevicesFromType(audio_devices_t type) const { 125 return getDevicesFromTypes({type}); 126 } 127 128 /** 129 * @brief getDeviceFromId 130 * @param id of the DeviceDescriptor to seach (aka Port handle). 131 * @return DeviceDescriptor associated to port id if found, nullptr otherwise. If the id is 132 * equal to AUDIO_PORT_HANDLE_NONE, it also returns a nullptr. 133 */ 134 sp<DeviceDescriptor> getDeviceFromId(audio_port_handle_t id) const; 135 sp<DeviceDescriptor> getDeviceFromTagName(const std::string &tagName) const; 136 DeviceVector getDevicesFromHwModule(audio_module_handle_t moduleHandle) const; 137 138 DeviceVector getFirstDevicesFromTypes(std::vector<audio_devices_t> orderedTypes) const; 139 sp<DeviceDescriptor> getFirstExistingDevice(std::vector<audio_devices_t> orderedTypes) const; 140 141 // Return device descriptor that is used to open an input/output stream. 142 // Null pointer will be returned if 143 // 1) this collection is empty 144 // 2) the device descriptors are not the same category(input or output) 145 // 3) there are more than one device type for input case 146 // 4) the combination of all devices is invalid for selection 147 sp<DeviceDescriptor> getDeviceForOpening() const; 148 149 // If there are devices with the given type and the devices to add is not empty, 150 // remove all the devices with the given type and add all the devices to add. 151 void replaceDevicesByType(audio_devices_t typeToRemove, const DeviceVector &devicesToAdd); 152 containsDeviceAmongTypes(const DeviceTypeSet & deviceTypes)153 bool containsDeviceAmongTypes(const DeviceTypeSet& deviceTypes) const { 154 return !Intersection(mDeviceTypes, deviceTypes).empty(); 155 } 156 containsDeviceWithType(audio_devices_t deviceType)157 bool containsDeviceWithType(audio_devices_t deviceType) const { 158 return containsDeviceAmongTypes({deviceType}); 159 } 160 onlyContainsDevicesWithType(audio_devices_t deviceType)161 bool onlyContainsDevicesWithType(audio_devices_t deviceType) const { 162 return isSingleDeviceType(mDeviceTypes, deviceType); 163 } 164 contains(const sp<DeviceDescriptor> & item)165 bool contains(const sp<DeviceDescriptor>& item) const { return indexOf(item) >= 0; } 166 167 /** 168 * @brief containsAtLeastOne 169 * @param devices vector of devices to check against. 170 * @return true if the DeviceVector contains at list one of the devices from the given vector. 171 */ 172 bool containsAtLeastOne(const DeviceVector &devices) const; 173 174 /** 175 * @brief containsAllDevices 176 * @param devices vector of devices to check against. 177 * @return true if the DeviceVector contains all the devices from the given vector 178 */ 179 bool containsAllDevices(const DeviceVector &devices) const; 180 181 /** 182 * @brief filter the devices supported by this collection against another collection 183 * @param devices to filter against 184 * @return a filtered DeviceVector 185 */ 186 DeviceVector filter(const DeviceVector &devices) const; 187 188 /** 189 * @brief filter the devices supported by this collection before sending 190 * then to the Engine via AudioPolicyManagerObserver interface 191 * @return a filtered DeviceVector 192 */ 193 DeviceVector filterForEngine() const; 194 195 /** 196 * @brief merge two vectors. As SortedVector Implementation is buggy (it does not check the size 197 * of the destination vector, only of the source, it provides a safe implementation 198 * @param devices source device vector to merge with 199 * @return size of the merged vector. 200 */ merge(const DeviceVector & devices)201 ssize_t merge(const DeviceVector &devices) 202 { 203 if (isEmpty()) { 204 add(devices); 205 return size(); 206 } 207 return SortedVector::merge(devices); 208 } 209 210 /** 211 * @brief operator == DeviceVector are equals if all the DeviceDescriptor can be found (aka 212 * DeviceDescriptor with same type and address) and the vector has same size. 213 * @param right DeviceVector to compare to. 214 * @return true if right contains the same device and has the same size. 215 */ 216 bool operator==(const DeviceVector &right) const 217 { 218 if (size() != right.size()) { 219 return false; 220 } 221 for (const auto &device : *this) { 222 if (right.indexOf(device) < 0) { 223 return false; 224 } 225 } 226 return true; 227 } 228 229 bool operator!=(const DeviceVector &right) const 230 { 231 return !operator==(right); 232 } 233 234 /** 235 * @brief getFirstValidAddress 236 * @return the first valid address of a list of device, "" if no device with valid address 237 * found. 238 * This helper function helps maintaining compatibility with legacy where we used to have a 239 * devices mask and an address. 240 */ getFirstValidAddress()241 String8 getFirstValidAddress() const 242 { 243 for (const auto &device : *this) { 244 if (device->address() != "") { 245 return String8(device->address().c_str()); 246 } 247 } 248 return String8(""); 249 } 250 251 std::string toString() const; 252 253 void dump(String8 *dst, const String8 &tag, int spaces = 0, bool verbose = true) const; 254 255 private: 256 void refreshTypes(); 257 DeviceTypeSet mDeviceTypes; 258 }; 259 260 } // namespace android 261