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 "AudioRoute.h" 21 #include <utils/RefBase.h> 22 #include <utils/String8.h> 23 #include <utils/Errors.h> 24 #include <utils/Vector.h> 25 #include <system/audio.h> 26 #include <cutils/config_utils.h> 27 #include <string> 28 29 namespace android { 30 31 class IOProfile; 32 class InputProfile; 33 class OutputProfile; 34 35 typedef Vector<sp<IOProfile> > InputProfileCollection; 36 typedef Vector<sp<IOProfile> > OutputProfileCollection; 37 typedef Vector<sp<IOProfile> > IOProfileCollection; 38 39 class HwModule : public RefBase 40 { 41 public: 42 explicit HwModule(const char *name, uint32_t halVersionMajor, uint32_t halVersionMinor); 43 HwModule(const char *name, uint32_t halVersion = 0); 44 ~HwModule(); 45 getName()46 const char *getName() const { return mName.string(); } 47 getDeclaredDevices()48 const DeviceVector &getDeclaredDevices() const { return mDeclaredDevices; } 49 void setDeclaredDevices(const DeviceVector &devices); getAllDevices()50 DeviceVector getAllDevices() const 51 { 52 DeviceVector devices = mDeclaredDevices; 53 devices.merge(mDynamicDevices); 54 return devices; 55 } 56 std::string getTagForDevice(audio_devices_t device, 57 const String8 &address = String8(), 58 audio_format_t codec = AUDIO_FORMAT_DEFAULT); addDynamicDevice(const sp<DeviceDescriptor> & device)59 void addDynamicDevice(const sp<DeviceDescriptor> &device) 60 { 61 device->setDynamic(); 62 mDynamicDevices.add(device); 63 } 64 removeDynamicDevice(const sp<DeviceDescriptor> & device)65 bool removeDynamicDevice(const sp<DeviceDescriptor> &device) 66 { 67 return mDynamicDevices.remove(device) >= 0; 68 } getDynamicDevices()69 DeviceVector getDynamicDevices() const { return mDynamicDevices; } 70 getInputProfiles()71 const InputProfileCollection &getInputProfiles() const { return mInputProfiles; } getOutputProfiles()72 const OutputProfileCollection &getOutputProfiles() const { return mOutputProfiles; } 73 74 void setProfiles(const IOProfileCollection &profiles); 75 setHalVersion(uint32_t major,uint32_t minor)76 void setHalVersion(uint32_t major, uint32_t minor) { 77 mHalVersion = (major << 8) | (minor & 0xff); 78 } getHalVersionMajor()79 uint32_t getHalVersionMajor() const { return mHalVersion >> 8; } getHalVersionMinor()80 uint32_t getHalVersionMinor() const { return mHalVersion & 0xff; } 81 82 sp<DeviceDescriptor> getRouteSinkDevice(const sp<AudioRoute> &route) const; 83 DeviceVector getRouteSourceDevices(const sp<AudioRoute> &route) const; getRoutes()84 const AudioRouteVector& getRoutes() const { return mRoutes; } 85 void setRoutes(const AudioRouteVector &routes); 86 87 status_t addOutputProfile(const sp<IOProfile> &profile); 88 status_t addInputProfile(const sp<IOProfile> &profile); 89 status_t addProfile(const sp<IOProfile> &profile); 90 91 status_t addOutputProfile(const std::string& name, const audio_config_t *config, 92 audio_devices_t device, const String8& address); 93 status_t removeOutputProfile(const std::string& name); 94 status_t addInputProfile(const std::string& name, const audio_config_t *config, 95 audio_devices_t device, const String8& address); 96 status_t removeInputProfile(const std::string& name); 97 getHandle()98 audio_module_handle_t getHandle() const { return mHandle; } 99 void setHandle(audio_module_handle_t handle); 100 findPortByTagName(const std::string & tagName)101 sp<PolicyAudioPort> findPortByTagName(const std::string &tagName) const 102 { 103 return findByTagName(mPorts, tagName); 104 } 105 106 /** 107 * @brief supportsPatch checks if an audio patch between 2 ports beloging to this HwModule 108 * is supported by a HwModule. The ports and the route shall be declared in the 109 * audio_policy_configuration.xml file. 110 * @param srcPort (aka the source) to be considered 111 * @param dstPort (aka the sink) to be considered 112 * @return true if the HwModule supports the connection between the sink and the source, 113 * false otherwise 114 */ 115 bool supportsPatch(const sp<PolicyAudioPort> &srcPort, 116 const sp<PolicyAudioPort> &dstPort) const; 117 118 // TODO remove from here (split serialization) 119 void dump(String8 *dst, int spaces) const; 120 121 private: 122 void refreshSupportedDevices(); 123 124 const String8 mName; // base name of the audio HW module (primary, a2dp ...) 125 audio_module_handle_t mHandle; 126 OutputProfileCollection mOutputProfiles; // output profiles exposed by this module 127 InputProfileCollection mInputProfiles; // input profiles exposed by this module 128 uint32_t mHalVersion; // audio HAL API version 129 DeviceVector mDeclaredDevices; // devices declared in audio_policy configuration file. 130 DeviceVector mDynamicDevices; /**< devices that can be added/removed at runtime (e.g. rsbumix)*/ 131 AudioRouteVector mRoutes; 132 PolicyAudioPortVector mPorts; 133 }; 134 135 class HwModuleCollection : public Vector<sp<HwModule> > 136 { 137 public: 138 sp<HwModule> getModuleFromName(const char *name) const; 139 140 /** 141 * @brief getModuleForDeviceType try to get a device from type / format on all modules 142 * @param device type to consider 143 * @param encodedFormat to consider 144 * @param[out] tagName if not null, if a matching device is found, will return the tagName 145 * of original device from XML file so that audio routes matchin rules work. 146 * @return valid module if considered device found, nullptr otherwise. 147 */ 148 sp<HwModule> getModuleForDeviceType(audio_devices_t device, 149 audio_format_t encodedFormat, 150 std::string *tagName = nullptr) const; 151 152 sp<HwModule> getModuleForDevice(const sp<DeviceDescriptor> &device, 153 audio_format_t encodedFormat) const; 154 155 DeviceVector getAvailableDevicesFromModuleName(const char *name, 156 const DeviceVector &availableDevices) const; 157 158 /** 159 * @brief getDeviceDescriptor returns a device descriptor associated to the device type and 160 * device address (if matchAddress is true). 161 * It may loop twice on all modules to check if allowToCreate is true 162 * -first loop will check if the device is found on a module since declared in the list 163 * of device port in configuration file 164 * -(allowToCreate is true)second loop will check if the device is weakly supported by one 165 * or more profiles on a given module and will add as a supported device for this module. 166 * The device will also be added to the dynamic list of device of this module 167 * @param type of the device requested 168 * @param address of the device requested 169 * @param name of the device that requested 170 * @param encodedFormat if not AUDIO_FORMAT_DEFAULT, must match one supported format 171 * @param matchAddress true if a strong match is required 172 * @param allowToCreate true if allowed to create dynamic device (e.g. hdmi, usb...) 173 * @return device descriptor associated to the type (and address if matchAddress is true) 174 */ 175 sp<DeviceDescriptor> getDeviceDescriptor(const audio_devices_t type, 176 const char *address, 177 const char *name, 178 audio_format_t encodedFormat, 179 bool allowToCreate = false, 180 bool matchAddress = true) const; 181 182 /** 183 * @brief createDevice creates a new device from the type and address given. It checks that 184 * according to the device type, a module is supporting this device (weak check). 185 * This concerns only dynamic device, aka device with a specific address and not 186 * already supported by module/underlying profiles. 187 * @param type of the device to be created 188 * @param address of the device to be created 189 * @param name of the device to be created 190 * @return device descriptor if a module is supporting this type, nullptr otherwise. 191 */ 192 sp<DeviceDescriptor> createDevice(const audio_devices_t type, 193 const char *address, 194 const char *name, 195 const audio_format_t encodedFormat) const; 196 197 /** 198 * @brief cleanUpForDevice: loop on all profiles of all modules to remove device from 199 * the list of supported device. If this device is a dynamic device (aka a device not in the 200 * xml file with a runtime address), it is also removed from the module collection of dynamic 201 * devices. 202 * @param device that has been disconnected 203 */ 204 void cleanUpForDevice(const sp<DeviceDescriptor> &device); 205 206 void dump(String8 *dst) const; 207 }; 208 209 } // namespace android 210