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