| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 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 | #define LOG_TAG "APM::Serializer" |
| 18 | //#define LOG_NDEBUG 0 |
| 19 | |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 20 | #include <memory> |
| 21 | #include <string> |
| Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 22 | #include <utility> |
| Mikhail Naganov | 6ef1b95 | 2021-02-25 18:22:02 -0800 | [diff] [blame] | 23 | #include <variant> |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 24 | |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 25 | #include <libxml/parser.h> |
| 26 | #include <libxml/xinclude.h> |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 27 | #include <media/convert.h> |
| 28 | #include <utils/Log.h> |
| 29 | #include <utils/StrongPointer.h> |
| 30 | #include <utils/Errors.h> |
| 31 | #include <utils/RefBase.h> |
| Mikhail Naganov | 68e3f64 | 2023-04-28 13:06:32 -0700 | [diff] [blame] | 32 | #include "IOProfile.h" |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 33 | #include "Serializer.h" |
| 34 | #include "TypeConverter.h" |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 35 | |
| 36 | namespace android { |
| 37 | |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 38 | namespace { |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 39 | |
| 40 | using utilities::convertTo; |
| 41 | |
| Mikhail Naganov | 6ef1b95 | 2021-02-25 18:22:02 -0800 | [diff] [blame] | 42 | static inline bool maybeVendorExtension(const std::string& s) { |
| 43 | // Only checks whether the string starts with the "vendor prefix". |
| 44 | static const std::string vendorPrefix = "VX_"; |
| 45 | return s.size() > vendorPrefix.size() && s.substr(0, vendorPrefix.size()) == vendorPrefix; |
| 46 | } |
| 47 | |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 48 | template<typename E, typename C> |
| Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 49 | struct AndroidCollectionTraits { |
| Mikhail Naganov | 43c386c | 2018-09-19 12:38:29 -0700 | [diff] [blame] | 50 | typedef sp<E> Element; |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 51 | typedef C Collection; |
| 52 | typedef void* PtrSerializingCtx; |
| Mikhail Naganov | 43c386c | 2018-09-19 12:38:29 -0700 | [diff] [blame] | 53 | |
| 54 | static status_t addElementToCollection(const Element &element, Collection *collection) { |
| 55 | return collection->add(element) >= 0 ? NO_ERROR : BAD_VALUE; |
| 56 | } |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 57 | }; |
| François Gaffie | c9d7c4e | 2016-01-22 13:54:30 +0100 | [diff] [blame] | 58 | |
| Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 59 | template<typename C> |
| 60 | struct StdCollectionTraits { |
| 61 | typedef C Collection; |
| 62 | typedef typename C::value_type Element; |
| 63 | typedef void* PtrSerializingCtx; |
| 64 | |
| 65 | static status_t addElementToCollection(const Element &element, Collection *collection) { |
| 66 | auto pair = collection->insert(element); |
| 67 | return pair.second ? NO_ERROR : BAD_VALUE; |
| 68 | } |
| 69 | }; |
| 70 | |
| François Gaffie | 43c7344 | 2018-11-08 08:21:55 +0100 | [diff] [blame] | 71 | struct AudioGainTraits : public AndroidCollectionTraits<AudioGain, AudioGains> |
| François Gaffie | c9d7c4e | 2016-01-22 13:54:30 +0100 | [diff] [blame] | 72 | { |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 73 | static constexpr const char *tag = "gain"; |
| 74 | static constexpr const char *collectionTag = "gains"; |
| 75 | |
| 76 | struct Attributes |
| 77 | { |
| 78 | /** gain modes supported, e.g. AUDIO_GAIN_MODE_CHANNELS. */ |
| 79 | static constexpr const char *mode = "mode"; |
| 80 | /** controlled channels, needed if mode AUDIO_GAIN_MODE_CHANNELS. */ |
| 81 | static constexpr const char *channelMask = "channel_mask"; |
| 82 | static constexpr const char *minValueMB = "minValueMB"; /**< min value in millibel. */ |
| 83 | static constexpr const char *maxValueMB = "maxValueMB"; /**< max value in millibel. */ |
| 84 | /** default value in millibel. */ |
| 85 | static constexpr const char *defaultValueMB = "defaultValueMB"; |
| 86 | static constexpr const char *stepValueMB = "stepValueMB"; /**< step value in millibel. */ |
| 87 | /** needed if mode AUDIO_GAIN_MODE_RAMP. */ |
| 88 | static constexpr const char *minRampMs = "minRampMs"; |
| 89 | /** needed if mode AUDIO_GAIN_MODE_RAMP. */ |
| 90 | static constexpr const char *maxRampMs = "maxRampMs"; |
| François Gaffie | 43c7344 | 2018-11-08 08:21:55 +0100 | [diff] [blame] | 91 | /** needed to allow use setPortGain instead of setStreamVolume. */ |
| 92 | static constexpr const char *useForVolume = "useForVolume"; |
| 93 | |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 94 | }; |
| 95 | |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 96 | // No children |
| 97 | }; |
| 98 | |
| 99 | // A profile section contains a name, one audio format and the list of supported sampling rates |
| 100 | // and channel masks for this format |
| Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 101 | struct AudioProfileTraits : public AndroidCollectionTraits<AudioProfile, AudioProfileVector> |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 102 | { |
| 103 | static constexpr const char *tag = "profile"; |
| 104 | static constexpr const char *collectionTag = "profiles"; |
| 105 | |
| 106 | struct Attributes |
| 107 | { |
| 108 | static constexpr const char *samplingRates = "samplingRates"; |
| 109 | static constexpr const char *format = "format"; |
| 110 | static constexpr const char *channelMasks = "channelMasks"; |
| 111 | }; |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 112 | }; |
| 113 | |
| Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 114 | struct MixPortTraits : public AndroidCollectionTraits<IOProfile, IOProfileCollection> |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 115 | { |
| 116 | static constexpr const char *tag = "mixPort"; |
| 117 | static constexpr const char *collectionTag = "mixPorts"; |
| 118 | |
| 119 | struct Attributes |
| 120 | { |
| 121 | static constexpr const char *name = "name"; |
| 122 | static constexpr const char *role = "role"; |
| 123 | static constexpr const char *roleSource = "source"; /**< <attribute role source value>. */ |
| 124 | static constexpr const char *flags = "flags"; |
| 125 | static constexpr const char *maxOpenCount = "maxOpenCount"; |
| 126 | static constexpr const char *maxActiveCount = "maxActiveCount"; |
| Jasmine Cha | f6074fe | 2021-08-17 13:44:31 +0800 | [diff] [blame] | 127 | static constexpr const char *recommendedMuteDurationMs = "recommendedMuteDurationMs"; |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 128 | }; |
| 129 | |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 130 | // Children: GainTraits |
| 131 | }; |
| 132 | |
| Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 133 | struct DevicePortTraits : public AndroidCollectionTraits<DeviceDescriptor, DeviceVector> |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 134 | { |
| 135 | static constexpr const char *tag = "devicePort"; |
| 136 | static constexpr const char *collectionTag = "devicePorts"; |
| 137 | |
| 138 | struct Attributes |
| 139 | { |
| 140 | /** <device tag name>: any string without space. */ |
| 141 | static constexpr const char *tagName = "tagName"; |
| 142 | static constexpr const char *type = "type"; /**< <device type>. */ |
| 143 | static constexpr const char *role = "role"; /**< <device role: sink or source>. */ |
| 144 | static constexpr const char *roleSource = "source"; /**< <attribute role source value>. */ |
| 145 | /** optional: device address, char string less than 64. */ |
| 146 | static constexpr const char *address = "address"; |
| Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 147 | /** optional: the list of encoded audio formats that are known to be supported. */ |
| 148 | static constexpr const char *encodedFormats = "encodedFormats"; |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 149 | }; |
| 150 | |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 151 | // Children: GainTraits (optional) |
| 152 | }; |
| 153 | |
| Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 154 | struct RouteTraits : public AndroidCollectionTraits<AudioRoute, AudioRouteVector> |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 155 | { |
| 156 | static constexpr const char *tag = "route"; |
| 157 | static constexpr const char *collectionTag = "routes"; |
| 158 | |
| 159 | struct Attributes |
| 160 | { |
| 161 | static constexpr const char *type = "type"; /**< <route type>: mix or mux. */ |
| 162 | static constexpr const char *typeMix = "mix"; /**< type attribute mix value. */ |
| 163 | static constexpr const char *sink = "sink"; /**< <sink: involved in this route>. */ |
| 164 | /** sources: all source that can be involved in this route. */ |
| 165 | static constexpr const char *sources = "sources"; |
| 166 | }; |
| 167 | |
| 168 | typedef HwModule *PtrSerializingCtx; |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 169 | }; |
| 170 | |
| Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 171 | struct ModuleTraits : public AndroidCollectionTraits<HwModule, HwModuleCollection> |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 172 | { |
| 173 | static constexpr const char *tag = "module"; |
| 174 | static constexpr const char *collectionTag = "modules"; |
| 175 | |
| 176 | static constexpr const char *childAttachedDevicesTag = "attachedDevices"; |
| 177 | static constexpr const char *childAttachedDeviceTag = "item"; |
| 178 | static constexpr const char *childDefaultOutputDeviceTag = "defaultOutputDevice"; |
| 179 | |
| 180 | struct Attributes |
| 181 | { |
| 182 | static constexpr const char *name = "name"; |
| 183 | static constexpr const char *version = "halVersion"; |
| 184 | }; |
| 185 | |
| 186 | typedef AudioPolicyConfig *PtrSerializingCtx; |
| 187 | |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 188 | // Children: mixPortTraits, devicePortTraits, and routeTraits |
| 189 | // Need to call deserialize on each child |
| 190 | }; |
| 191 | |
| 192 | struct GlobalConfigTraits |
| 193 | { |
| Mikhail Naganov | 6ef1b95 | 2021-02-25 18:22:02 -0800 | [diff] [blame] | 194 | typedef std::monostate Element; |
| Mikhail Naganov | a7c4e03 | 2021-02-04 19:20:12 +0000 | [diff] [blame] | 195 | |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 196 | static constexpr const char *tag = "globalConfiguration"; |
| 197 | |
| 198 | struct Attributes |
| 199 | { |
| Eric Laurent | 8340e67 | 2019-11-06 11:01:08 -0800 | [diff] [blame] | 200 | static constexpr const char *callScreenModeSupported= "call_screen_mode_supported"; |
| François Gaffie | 8475ae5 | 2019-11-19 09:50:48 +0100 | [diff] [blame] | 201 | static constexpr const char *engineLibrarySuffix = "engine_library"; |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 202 | }; |
| 203 | |
| Mikhail Naganov | a7c4e03 | 2021-02-04 19:20:12 +0000 | [diff] [blame] | 204 | typedef AudioPolicyConfig *PtrSerializingCtx; |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 205 | }; |
| 206 | |
| Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 207 | struct SurroundSoundTraits |
| 208 | { |
| Mikhail Naganov | 6ef1b95 | 2021-02-25 18:22:02 -0800 | [diff] [blame] | 209 | typedef std::monostate Element; |
| Mikhail Naganov | a7c4e03 | 2021-02-04 19:20:12 +0000 | [diff] [blame] | 210 | |
| Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 211 | static constexpr const char *tag = "surroundSound"; |
| 212 | |
| Mikhail Naganov | a7c4e03 | 2021-02-04 19:20:12 +0000 | [diff] [blame] | 213 | typedef AudioPolicyConfig *PtrSerializingCtx; |
| Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 214 | // Children: SurroundSoundFormatTraits |
| 215 | }; |
| 216 | |
| 217 | struct SurroundSoundFormatTraits : public StdCollectionTraits<AudioPolicyConfig::SurroundFormats> |
| 218 | { |
| 219 | static constexpr const char *tag = "format"; |
| 220 | static constexpr const char *collectionTag = "formats"; |
| 221 | |
| 222 | struct Attributes |
| 223 | { |
| 224 | static constexpr const char *name = "name"; |
| 225 | static constexpr const char *subformats = "subformats"; |
| 226 | }; |
| Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 227 | }; |
| 228 | |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 229 | class PolicySerializer |
| 230 | { |
| 231 | public: |
| Mikhail Naganov | 6ef1b95 | 2021-02-25 18:22:02 -0800 | [diff] [blame] | 232 | status_t deserialize(const char *configFile, AudioPolicyConfig *config, |
| 233 | bool ignoreVendorExtensions = false); |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 234 | |
| Mikhail Naganov | a7c4e03 | 2021-02-04 19:20:12 +0000 | [diff] [blame] | 235 | template <class Trait> |
| 236 | status_t deserializeCollection(const xmlNode *cur, |
| 237 | typename Trait::Collection *collection, |
| 238 | typename Trait::PtrSerializingCtx serializingContext); |
| 239 | template <class Trait> |
| Mikhail Naganov | 6ef1b95 | 2021-02-25 18:22:02 -0800 | [diff] [blame] | 240 | std::variant<status_t, typename Trait::Element> deserialize(const xmlNode *cur, |
| Mikhail Naganov | a7c4e03 | 2021-02-04 19:20:12 +0000 | [diff] [blame] | 241 | typename Trait::PtrSerializingCtx serializingContext); |
| 242 | |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 243 | private: |
| 244 | static constexpr const char *rootName = "audioPolicyConfiguration"; |
| 245 | static constexpr const char *versionAttribute = "version"; |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 246 | |
| 247 | typedef AudioPolicyConfig Element; |
| 248 | |
| Mikhail Naganov | 6ef1b95 | 2021-02-25 18:22:02 -0800 | [diff] [blame] | 249 | bool mIgnoreVendorExtensions = false; |
| Mikhail Naganov | a7c4e03 | 2021-02-04 19:20:12 +0000 | [diff] [blame] | 250 | std::string mChannelMasksSeparator = ","; |
| 251 | std::string mSamplingRatesSeparator = ","; |
| 252 | std::string mFlagsSeparator = "|"; |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 253 | |
| Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 254 | // Children: ModulesTraits, VolumeTraits, SurroundSoundTraits (optional) |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 255 | }; |
| 256 | |
| Mikhail Naganov | 91d50bb | 2021-04-16 17:38:23 -0700 | [diff] [blame] | 257 | // Deleter using free() for use with std::unique_ptr<>. See also UniqueCPtr<> below. |
| 258 | struct FreeDelete { |
| 259 | // NOTE: Deleting a const object is valid but free() takes a non-const pointer. |
| 260 | void operator()(const void* ptr) const { |
| 261 | free(const_cast<void*>(ptr)); |
| 262 | } |
| 263 | }; |
| 264 | |
| 265 | // Alias for std::unique_ptr<> that uses the C function free() to delete objects. |
| 266 | template <typename T> |
| 267 | using UniqueCPtr = std::unique_ptr<T, FreeDelete>; |
| 268 | |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 269 | template <class T> |
| 270 | constexpr void (*xmlDeleter)(T* t); |
| 271 | template <> |
| 272 | constexpr auto xmlDeleter<xmlDoc> = xmlFreeDoc; |
| 273 | template <> |
| Mikhail Naganov | 8916ae9 | 2020-10-21 13:04:58 -0700 | [diff] [blame] | 274 | constexpr auto xmlDeleter<xmlChar> = [](xmlChar *s) { xmlFree(s); }; |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 275 | |
| 276 | /** @return a unique_ptr with the correct deleter for the libxml2 object. */ |
| 277 | template <class T> |
| 278 | constexpr auto make_xmlUnique(T *t) { |
| 279 | // Wrap deleter in lambda to enable empty base optimization |
| 280 | auto deleter = [](T *t) { xmlDeleter<T>(t); }; |
| 281 | return std::unique_ptr<T, decltype(deleter)>{t, deleter}; |
| 282 | } |
| 283 | |
| 284 | std::string getXmlAttribute(const xmlNode *cur, const char *attribute) |
| 285 | { |
| 286 | auto xmlValue = make_xmlUnique(xmlGetProp(cur, reinterpret_cast<const xmlChar*>(attribute))); |
| 287 | if (xmlValue == nullptr) { |
| 288 | return ""; |
| 289 | } |
| 290 | std::string value(reinterpret_cast<const char*>(xmlValue.get())); |
| 291 | return value; |
| François Gaffie | c9d7c4e | 2016-01-22 13:54:30 +0100 | [diff] [blame] | 292 | } |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 293 | |
| 294 | template <class Trait> |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 295 | const xmlNode* getReference(const xmlNode *cur, const std::string &refName) |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 296 | { |
| Mikhail Naganov | 43c386c | 2018-09-19 12:38:29 -0700 | [diff] [blame] | 297 | for (; cur != NULL; cur = cur->next) { |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 298 | if (!xmlStrcmp(cur->name, reinterpret_cast<const xmlChar*>(Trait::collectionTag))) { |
| Mikhail Naganov | 43c386c | 2018-09-19 12:38:29 -0700 | [diff] [blame] | 299 | for (const xmlNode *child = cur->children; child != NULL; child = child->next) { |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 300 | if ((!xmlStrcmp(child->name, |
| 301 | reinterpret_cast<const xmlChar*>(Trait::referenceTag)))) { |
| 302 | std::string name = getXmlAttribute(child, Trait::Attributes::referenceName); |
| 303 | if (refName == name) { |
| 304 | return child; |
| 305 | } |
| 306 | } |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 307 | } |
| 308 | } |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 309 | } |
| 310 | return NULL; |
| 311 | } |
| 312 | |
| 313 | template <class Trait> |
| Mikhail Naganov | a7c4e03 | 2021-02-04 19:20:12 +0000 | [diff] [blame] | 314 | status_t PolicySerializer::deserializeCollection(const xmlNode *cur, |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 315 | typename Trait::Collection *collection, |
| 316 | typename Trait::PtrSerializingCtx serializingContext) |
| 317 | { |
| Mikhail Naganov | 43c386c | 2018-09-19 12:38:29 -0700 | [diff] [blame] | 318 | for (cur = cur->xmlChildrenNode; cur != NULL; cur = cur->next) { |
| 319 | const xmlNode *child = NULL; |
| 320 | if (!xmlStrcmp(cur->name, reinterpret_cast<const xmlChar*>(Trait::collectionTag))) { |
| 321 | child = cur->xmlChildrenNode; |
| 322 | } else if (!xmlStrcmp(cur->name, reinterpret_cast<const xmlChar*>(Trait::tag))) { |
| 323 | child = cur; |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 324 | } |
| Mikhail Naganov | 43c386c | 2018-09-19 12:38:29 -0700 | [diff] [blame] | 325 | for (; child != NULL; child = child->next) { |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 326 | if (!xmlStrcmp(child->name, reinterpret_cast<const xmlChar*>(Trait::tag))) { |
| Mikhail Naganov | 6ef1b95 | 2021-02-25 18:22:02 -0800 | [diff] [blame] | 327 | auto maybeElement = deserialize<Trait>(child, serializingContext); |
| 328 | if (maybeElement.index() == 1) { |
| 329 | status_t status = Trait::addElementToCollection( |
| 330 | std::get<1>(maybeElement), collection); |
| Mikhail Naganov | 43c386c | 2018-09-19 12:38:29 -0700 | [diff] [blame] | 331 | if (status != NO_ERROR) { |
| 332 | ALOGE("%s: could not add element to %s collection", __func__, |
| 333 | Trait::collectionTag); |
| 334 | return status; |
| 335 | } |
| Mikhail Naganov | 6ef1b95 | 2021-02-25 18:22:02 -0800 | [diff] [blame] | 336 | } else if (mIgnoreVendorExtensions && std::get<status_t>(maybeElement) == NO_INIT) { |
| 337 | // Skip a vendor extension element. |
| Mikhail Naganov | 43c386c | 2018-09-19 12:38:29 -0700 | [diff] [blame] | 338 | } else { |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 339 | return BAD_VALUE; |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 340 | } |
| 341 | } |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 342 | } |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 343 | if (!xmlStrcmp(cur->name, reinterpret_cast<const xmlChar*>(Trait::tag))) { |
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 344 | return NO_ERROR; |
| 345 | } |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 346 | } |
| François Gaffie | d1ab2bd | 2015-12-02 18:20:06 +0100 | [diff] [blame] | 347 | return NO_ERROR; |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 348 | } |
| 349 | |
| Mikhail Naganov | a7c4e03 | 2021-02-04 19:20:12 +0000 | [diff] [blame] | 350 | template<> |
| Mikhail Naganov | 6ef1b95 | 2021-02-25 18:22:02 -0800 | [diff] [blame] | 351 | std::variant<status_t, AudioGainTraits::Element> PolicySerializer::deserialize<AudioGainTraits>( |
| 352 | const xmlNode *cur, AudioGainTraits::PtrSerializingCtx /*serializingContext*/) |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 353 | { |
| Mikhail Naganov | a7c4e03 | 2021-02-04 19:20:12 +0000 | [diff] [blame] | 354 | using Attributes = AudioGainTraits::Attributes; |
| 355 | |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 356 | static uint32_t index = 0; |
| Mikhail Naganov | a7c4e03 | 2021-02-04 19:20:12 +0000 | [diff] [blame] | 357 | AudioGainTraits::Element gain = new AudioGain(index++, true); |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 358 | |
| Mikhail Naganov | 43c386c | 2018-09-19 12:38:29 -0700 | [diff] [blame] | 359 | std::string mode = getXmlAttribute(cur, Attributes::mode); |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 360 | if (!mode.empty()) { |
| Mikhail Naganov | dc6be0d | 2020-09-25 23:03:05 +0000 | [diff] [blame] | 361 | gain->setMode(GainModeConverter::maskFromString(mode, " ")); |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 362 | } |
| 363 | |
| Mikhail Naganov | 43c386c | 2018-09-19 12:38:29 -0700 | [diff] [blame] | 364 | std::string channelsLiteral = getXmlAttribute(cur, Attributes::channelMask); |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 365 | if (!channelsLiteral.empty()) { |
| 366 | gain->setChannelMask(channelMaskFromString(channelsLiteral)); |
| 367 | } |
| 368 | |
| Mikhail Naganov | 43c386c | 2018-09-19 12:38:29 -0700 | [diff] [blame] | 369 | std::string minValueMBLiteral = getXmlAttribute(cur, Attributes::minValueMB); |
| Kevin Rocard | 575bb3a | 2017-10-27 16:49:20 -0700 | [diff] [blame] | 370 | int32_t minValueMB; |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 371 | if (!minValueMBLiteral.empty() && convertTo(minValueMBLiteral, minValueMB)) { |
| 372 | gain->setMinValueInMb(minValueMB); |
| 373 | } |
| 374 | |
| Mikhail Naganov | 43c386c | 2018-09-19 12:38:29 -0700 | [diff] [blame] | 375 | std::string maxValueMBLiteral = getXmlAttribute(cur, Attributes::maxValueMB); |
| Kevin Rocard | 575bb3a | 2017-10-27 16:49:20 -0700 | [diff] [blame] | 376 | int32_t maxValueMB; |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 377 | if (!maxValueMBLiteral.empty() && convertTo(maxValueMBLiteral, maxValueMB)) { |
| 378 | gain->setMaxValueInMb(maxValueMB); |
| 379 | } |
| 380 | |
| Mikhail Naganov | 43c386c | 2018-09-19 12:38:29 -0700 | [diff] [blame] | 381 | std::string defaultValueMBLiteral = getXmlAttribute(cur, Attributes::defaultValueMB); |
| Kevin Rocard | 575bb3a | 2017-10-27 16:49:20 -0700 | [diff] [blame] | 382 | int32_t defaultValueMB; |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 383 | if (!defaultValueMBLiteral.empty() && convertTo(defaultValueMBLiteral, defaultValueMB)) { |
| 384 | gain->setDefaultValueInMb(defaultValueMB); |
| 385 | } |
| 386 | |
| Mikhail Naganov | 43c386c | 2018-09-19 12:38:29 -0700 | [diff] [blame] | 387 | std::string stepValueMBLiteral = getXmlAttribute(cur, Attributes::stepValueMB); |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 388 | uint32_t stepValueMB; |
| 389 | if (!stepValueMBLiteral.empty() && convertTo(stepValueMBLiteral, stepValueMB)) { |
| 390 | gain->setStepValueInMb(stepValueMB); |
| 391 | } |
| 392 | |
| Mikhail Naganov | 43c386c | 2018-09-19 12:38:29 -0700 | [diff] [blame] | 393 | std::string minRampMsLiteral = getXmlAttribute(cur, Attributes::minRampMs); |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 394 | uint32_t minRampMs; |
| 395 | if (!minRampMsLiteral.empty() && convertTo(minRampMsLiteral, minRampMs)) { |
| 396 | gain->setMinRampInMs(minRampMs); |
| 397 | } |
| 398 | |
| Mikhail Naganov | 43c386c | 2018-09-19 12:38:29 -0700 | [diff] [blame] | 399 | std::string maxRampMsLiteral = getXmlAttribute(cur, Attributes::maxRampMs); |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 400 | uint32_t maxRampMs; |
| 401 | if (!maxRampMsLiteral.empty() && convertTo(maxRampMsLiteral, maxRampMs)) { |
| 402 | gain->setMaxRampInMs(maxRampMs); |
| 403 | } |
| François Gaffie | 43c7344 | 2018-11-08 08:21:55 +0100 | [diff] [blame] | 404 | std::string useForVolumeLiteral = getXmlAttribute(cur, Attributes::useForVolume); |
| 405 | bool useForVolume = false; |
| 406 | if (!useForVolumeLiteral.empty() && convertTo(useForVolumeLiteral, useForVolume)) { |
| 407 | gain->setUseForVolume(useForVolume); |
| 408 | } |
| 409 | ALOGV("%s: adding new gain mode %08x channel mask %08x min mB %d max mB %d UseForVolume: %d", |
| 410 | __func__, gain->getMode(), gain->getChannelMask(), gain->getMinValueInMb(), |
| 411 | gain->getMaxValueInMb(), useForVolume); |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 412 | |
| Mikhail Naganov | 43c386c | 2018-09-19 12:38:29 -0700 | [diff] [blame] | 413 | if (gain->getMode() != 0) { |
| 414 | return gain; |
| 415 | } else { |
| Mikhail Naganov | 6ef1b95 | 2021-02-25 18:22:02 -0800 | [diff] [blame] | 416 | return BAD_VALUE; |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 417 | } |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 418 | } |
| 419 | |
| Mikhail Naganov | a7c4e03 | 2021-02-04 19:20:12 +0000 | [diff] [blame] | 420 | template<> |
| Mikhail Naganov | 6ef1b95 | 2021-02-25 18:22:02 -0800 | [diff] [blame] | 421 | std::variant<status_t, AudioProfileTraits::Element> |
| 422 | PolicySerializer::deserialize<AudioProfileTraits>( |
| Mikhail Naganov | a7c4e03 | 2021-02-04 19:20:12 +0000 | [diff] [blame] | 423 | const xmlNode *cur, AudioProfileTraits::PtrSerializingCtx /*serializingContext*/) |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 424 | { |
| Mikhail Naganov | a7c4e03 | 2021-02-04 19:20:12 +0000 | [diff] [blame] | 425 | using Attributes = AudioProfileTraits::Attributes; |
| 426 | |
| Mikhail Naganov | 43c386c | 2018-09-19 12:38:29 -0700 | [diff] [blame] | 427 | std::string samplingRates = getXmlAttribute(cur, Attributes::samplingRates); |
| 428 | std::string format = getXmlAttribute(cur, Attributes::format); |
| 429 | std::string channels = getXmlAttribute(cur, Attributes::channelMasks); |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 430 | |
| Mikhail Naganov | 6ef1b95 | 2021-02-25 18:22:02 -0800 | [diff] [blame] | 431 | if (mIgnoreVendorExtensions && maybeVendorExtension(format)) { |
| 432 | ALOGI("%s: vendor extension format \"%s\" skipped", __func__, format.c_str()); |
| 433 | return NO_INIT; |
| 434 | } |
| Mikhail Naganov | a7c4e03 | 2021-02-04 19:20:12 +0000 | [diff] [blame] | 435 | AudioProfileTraits::Element profile = new AudioProfile(formatFromString(format, gDynamicFormat), |
| 436 | channelMasksFromString(channels, mChannelMasksSeparator.c_str()), |
| 437 | samplingRatesFromString(samplingRates, mSamplingRatesSeparator.c_str())); |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 438 | |
| 439 | profile->setDynamicFormat(profile->getFormat() == gDynamicFormat); |
| jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 440 | profile->setDynamicChannels(profile->getChannels().empty()); |
| 441 | profile->setDynamicRate(profile->getSampleRates().empty()); |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 442 | |
| Mikhail Naganov | 43c386c | 2018-09-19 12:38:29 -0700 | [diff] [blame] | 443 | return profile; |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 444 | } |
| 445 | |
| Mikhail Naganov | a7c4e03 | 2021-02-04 19:20:12 +0000 | [diff] [blame] | 446 | template<> |
| Mikhail Naganov | 6ef1b95 | 2021-02-25 18:22:02 -0800 | [diff] [blame] | 447 | std::variant<status_t, MixPortTraits::Element> PolicySerializer::deserialize<MixPortTraits>( |
| 448 | const xmlNode *child, MixPortTraits::PtrSerializingCtx /*serializingContext*/) |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 449 | { |
| Mikhail Naganov | a7c4e03 | 2021-02-04 19:20:12 +0000 | [diff] [blame] | 450 | using Attributes = MixPortTraits::Attributes; |
| 451 | |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 452 | std::string name = getXmlAttribute(child, Attributes::name); |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 453 | if (name.empty()) { |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 454 | ALOGE("%s: No %s found", __func__, Attributes::name); |
| Mikhail Naganov | 6ef1b95 | 2021-02-25 18:22:02 -0800 | [diff] [blame] | 455 | return BAD_VALUE; |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 456 | } |
| Mikhail Naganov | a7c4e03 | 2021-02-04 19:20:12 +0000 | [diff] [blame] | 457 | ALOGV("%s: %s %s=%s", __func__, MixPortTraits::tag, Attributes::name, name.c_str()); |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 458 | std::string role = getXmlAttribute(child, Attributes::role); |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 459 | if (role.empty()) { |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 460 | ALOGE("%s: No %s found", __func__, Attributes::role); |
| Mikhail Naganov | 6ef1b95 | 2021-02-25 18:22:02 -0800 | [diff] [blame] | 461 | return BAD_VALUE; |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 462 | } |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 463 | ALOGV("%s: Role=%s", __func__, role.c_str()); |
| 464 | audio_port_role_t portRole = (role == Attributes::roleSource) ? |
| 465 | AUDIO_PORT_ROLE_SOURCE : AUDIO_PORT_ROLE_SINK; |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 466 | |
| Mikhail Naganov | a7c4e03 | 2021-02-04 19:20:12 +0000 | [diff] [blame] | 467 | MixPortTraits::Element mixPort = new IOProfile(name, portRole); |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 468 | |
| 469 | AudioProfileTraits::Collection profiles; |
| Mikhail Naganov | 43c386c | 2018-09-19 12:38:29 -0700 | [diff] [blame] | 470 | status_t status = deserializeCollection<AudioProfileTraits>(child, &profiles, NULL); |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 471 | if (status != NO_ERROR) { |
| Mikhail Naganov | 6ef1b95 | 2021-02-25 18:22:02 -0800 | [diff] [blame] | 472 | return status; |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 473 | } |
| jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 474 | if (profiles.empty()) { |
| jiabin | 9bb3a1e | 2019-08-19 10:10:17 -0700 | [diff] [blame] | 475 | profiles.add(AudioProfile::createFullDynamic(gDynamicFormat)); |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 476 | } |
| jiabin | 3e277cc | 2019-09-10 14:27:34 -0700 | [diff] [blame] | 477 | // The audio profiles are in order of listed in audio policy configuration file. |
| 478 | // Sort audio profiles accroding to the format. |
| 479 | sortAudioProfiles(profiles); |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 480 | mixPort->setAudioProfiles(profiles); |
| 481 | |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 482 | std::string flags = getXmlAttribute(child, Attributes::flags); |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 483 | if (!flags.empty()) { |
| 484 | // Source role |
| 485 | if (portRole == AUDIO_PORT_ROLE_SOURCE) { |
| Eric Laurent | e28c66d | 2022-01-21 13:40:41 +0100 | [diff] [blame] | 486 | //TODO: b/193496180 use spatializer flag at audio HAL when available until then, |
| 487 | // use DEEP_BUFFER+FAST flag combo to indicate the spatializer output profile |
| 488 | uint32_t intFlags = |
| 489 | OutputFlagConverter::maskFromString(flags, mFlagsSeparator.c_str()); |
| 490 | if (intFlags == (AUDIO_OUTPUT_FLAG_FAST | AUDIO_OUTPUT_FLAG_DEEP_BUFFER)) { |
| 491 | intFlags = AUDIO_OUTPUT_FLAG_SPATIALIZER; |
| 492 | } |
| 493 | mixPort->setFlags(intFlags); |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 494 | } else { |
| 495 | // Sink role |
| Mikhail Naganov | a7c4e03 | 2021-02-04 19:20:12 +0000 | [diff] [blame] | 496 | mixPort->setFlags(InputFlagConverter::maskFromString(flags, mFlagsSeparator.c_str())); |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 497 | } |
| 498 | } |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 499 | std::string maxOpenCount = getXmlAttribute(child, Attributes::maxOpenCount); |
| Eric Laurent | ae60317 | 2017-12-07 17:59:01 -0800 | [diff] [blame] | 500 | if (!maxOpenCount.empty()) { |
| 501 | convertTo(maxOpenCount, mixPort->maxOpenCount); |
| 502 | } |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 503 | std::string maxActiveCount = getXmlAttribute(child, Attributes::maxActiveCount); |
| Eric Laurent | ae60317 | 2017-12-07 17:59:01 -0800 | [diff] [blame] | 504 | if (!maxActiveCount.empty()) { |
| 505 | convertTo(maxActiveCount, mixPort->maxActiveCount); |
| 506 | } |
| Jasmine Cha | f6074fe | 2021-08-17 13:44:31 +0800 | [diff] [blame] | 507 | |
| 508 | std::string recommendedmuteDurationMsLiteral = |
| 509 | getXmlAttribute(child, Attributes::recommendedMuteDurationMs); |
| 510 | if (!recommendedmuteDurationMsLiteral.empty()) { |
| 511 | convertTo(recommendedmuteDurationMsLiteral, mixPort->recommendedMuteDurationMs); |
| 512 | } |
| 513 | |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 514 | // Deserialize children |
| 515 | AudioGainTraits::Collection gains; |
| Mikhail Naganov | 43c386c | 2018-09-19 12:38:29 -0700 | [diff] [blame] | 516 | status = deserializeCollection<AudioGainTraits>(child, &gains, NULL); |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 517 | if (status != NO_ERROR) { |
| Mikhail Naganov | 6ef1b95 | 2021-02-25 18:22:02 -0800 | [diff] [blame] | 518 | return status; |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 519 | } |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 520 | mixPort->setGains(gains); |
| 521 | |
| Mikhail Naganov | 43c386c | 2018-09-19 12:38:29 -0700 | [diff] [blame] | 522 | return mixPort; |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 523 | } |
| 524 | |
| Mikhail Naganov | a7c4e03 | 2021-02-04 19:20:12 +0000 | [diff] [blame] | 525 | template<> |
| Mikhail Naganov | 6ef1b95 | 2021-02-25 18:22:02 -0800 | [diff] [blame] | 526 | std::variant<status_t, DevicePortTraits::Element> PolicySerializer::deserialize<DevicePortTraits>( |
| Mikhail Naganov | a7c4e03 | 2021-02-04 19:20:12 +0000 | [diff] [blame] | 527 | const xmlNode *cur, DevicePortTraits::PtrSerializingCtx /*serializingContext*/) |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 528 | { |
| Mikhail Naganov | a7c4e03 | 2021-02-04 19:20:12 +0000 | [diff] [blame] | 529 | using Attributes = DevicePortTraits::Attributes; |
| 530 | auto& tag = DevicePortTraits::tag; |
| 531 | |
| Mikhail Naganov | 43c386c | 2018-09-19 12:38:29 -0700 | [diff] [blame] | 532 | std::string name = getXmlAttribute(cur, Attributes::tagName); |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 533 | if (name.empty()) { |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 534 | ALOGE("%s: No %s found", __func__, Attributes::tagName); |
| Mikhail Naganov | 6ef1b95 | 2021-02-25 18:22:02 -0800 | [diff] [blame] | 535 | return BAD_VALUE; |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 536 | } |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 537 | ALOGV("%s: %s %s=%s", __func__, tag, Attributes::tagName, name.c_str()); |
| Mikhail Naganov | 43c386c | 2018-09-19 12:38:29 -0700 | [diff] [blame] | 538 | std::string typeName = getXmlAttribute(cur, Attributes::type); |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 539 | if (typeName.empty()) { |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 540 | ALOGE("%s: no type for %s", __func__, name.c_str()); |
| Mikhail Naganov | 6ef1b95 | 2021-02-25 18:22:02 -0800 | [diff] [blame] | 541 | return BAD_VALUE; |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 542 | } |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 543 | ALOGV("%s: %s %s=%s", __func__, tag, Attributes::type, typeName.c_str()); |
| Mikhail Naganov | 43c386c | 2018-09-19 12:38:29 -0700 | [diff] [blame] | 544 | std::string role = getXmlAttribute(cur, Attributes::role); |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 545 | if (role.empty()) { |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 546 | ALOGE("%s: No %s found", __func__, Attributes::role); |
| Mikhail Naganov | 6ef1b95 | 2021-02-25 18:22:02 -0800 | [diff] [blame] | 547 | return BAD_VALUE; |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 548 | } |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 549 | ALOGV("%s: %s %s=%s", __func__, tag, Attributes::role, role.c_str()); |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 550 | audio_port_role_t portRole = (role == Attributes::roleSource) ? |
| 551 | AUDIO_PORT_ROLE_SOURCE : AUDIO_PORT_ROLE_SINK; |
| 552 | |
| Mikhail Naganov | 6ef1b95 | 2021-02-25 18:22:02 -0800 | [diff] [blame] | 553 | if (mIgnoreVendorExtensions && maybeVendorExtension(typeName)) { |
| 554 | ALOGI("%s: vendor extension device type \"%s\" skipped", __func__, typeName.c_str()); |
| 555 | return NO_INIT; |
| 556 | } |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 557 | audio_devices_t type = AUDIO_DEVICE_NONE; |
| Mikhail Naganov | dc6be0d | 2020-09-25 23:03:05 +0000 | [diff] [blame] | 558 | if (!DeviceConverter::fromString(typeName, type) || |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 559 | (!audio_is_input_device(type) && portRole == AUDIO_PORT_ROLE_SOURCE) || |
| 560 | (!audio_is_output_devices(type) && portRole == AUDIO_PORT_ROLE_SINK)) { |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 561 | ALOGW("%s: bad type %08x", __func__, type); |
| Mikhail Naganov | 6ef1b95 | 2021-02-25 18:22:02 -0800 | [diff] [blame] | 562 | return BAD_VALUE; |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 563 | } |
| Aniket Kumar Lata | 4e46470 | 2019-01-10 23:38:46 -0800 | [diff] [blame] | 564 | std::string encodedFormatsLiteral = getXmlAttribute(cur, Attributes::encodedFormats); |
| 565 | ALOGV("%s: %s %s=%s", __func__, tag, Attributes::encodedFormats, encodedFormatsLiteral.c_str()); |
| 566 | FormatVector encodedFormats; |
| 567 | if (!encodedFormatsLiteral.empty()) { |
| 568 | encodedFormats = formatsFromString(encodedFormatsLiteral, " "); |
| 569 | } |
| Mikhail Naganov | 43c386c | 2018-09-19 12:38:29 -0700 | [diff] [blame] | 570 | std::string address = getXmlAttribute(cur, Attributes::address); |
| Mikhail Naganov | a7c4e03 | 2021-02-04 19:20:12 +0000 | [diff] [blame] | 571 | DevicePortTraits::Element deviceDesc = |
| 572 | new DeviceDescriptor(type, name, address, encodedFormats); |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 573 | |
| 574 | AudioProfileTraits::Collection profiles; |
| Mikhail Naganov | 43c386c | 2018-09-19 12:38:29 -0700 | [diff] [blame] | 575 | status_t status = deserializeCollection<AudioProfileTraits>(cur, &profiles, NULL); |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 576 | if (status != NO_ERROR) { |
| Mikhail Naganov | 6ef1b95 | 2021-02-25 18:22:02 -0800 | [diff] [blame] | 577 | return status; |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 578 | } |
| jiabin | 06e4bab | 2019-07-29 10:13:34 -0700 | [diff] [blame] | 579 | if (profiles.empty()) { |
| jiabin | 9bb3a1e | 2019-08-19 10:10:17 -0700 | [diff] [blame] | 580 | profiles.add(AudioProfile::createFullDynamic(gDynamicFormat)); |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 581 | } |
| jiabin | 3e277cc | 2019-09-10 14:27:34 -0700 | [diff] [blame] | 582 | // The audio profiles are in order of listed in audio policy configuration file. |
| 583 | // Sort audio profiles accroding to the format. |
| 584 | sortAudioProfiles(profiles); |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 585 | deviceDesc->setAudioProfiles(profiles); |
| 586 | |
| 587 | // Deserialize AudioGain children |
| Mikhail Naganov | 43c386c | 2018-09-19 12:38:29 -0700 | [diff] [blame] | 588 | status = deserializeCollection<AudioGainTraits>(cur, &deviceDesc->mGains, NULL); |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 589 | if (status != NO_ERROR) { |
| Mikhail Naganov | 6ef1b95 | 2021-02-25 18:22:02 -0800 | [diff] [blame] | 590 | return status; |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 591 | } |
| 592 | ALOGV("%s: adding device tag %s type %08x address %s", __func__, |
| jiabin | ce9f20e | 2019-09-12 16:29:15 -0700 | [diff] [blame] | 593 | deviceDesc->getName().c_str(), type, deviceDesc->address().c_str()); |
| Mikhail Naganov | 43c386c | 2018-09-19 12:38:29 -0700 | [diff] [blame] | 594 | return deviceDesc; |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 595 | } |
| 596 | |
| Mikhail Naganov | a7c4e03 | 2021-02-04 19:20:12 +0000 | [diff] [blame] | 597 | template<> |
| Mikhail Naganov | 6ef1b95 | 2021-02-25 18:22:02 -0800 | [diff] [blame] | 598 | std::variant<status_t, RouteTraits::Element> PolicySerializer::deserialize<RouteTraits>( |
| Mikhail Naganov | a7c4e03 | 2021-02-04 19:20:12 +0000 | [diff] [blame] | 599 | const xmlNode *cur, RouteTraits::PtrSerializingCtx ctx) |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 600 | { |
| Mikhail Naganov | a7c4e03 | 2021-02-04 19:20:12 +0000 | [diff] [blame] | 601 | using Attributes = RouteTraits::Attributes; |
| 602 | |
| Mikhail Naganov | 43c386c | 2018-09-19 12:38:29 -0700 | [diff] [blame] | 603 | std::string type = getXmlAttribute(cur, Attributes::type); |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 604 | if (type.empty()) { |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 605 | ALOGE("%s: No %s found", __func__, Attributes::type); |
| Mikhail Naganov | 6ef1b95 | 2021-02-25 18:22:02 -0800 | [diff] [blame] | 606 | return BAD_VALUE; |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 607 | } |
| 608 | audio_route_type_t routeType = (type == Attributes::typeMix) ? |
| 609 | AUDIO_ROUTE_MIX : AUDIO_ROUTE_MUX; |
| 610 | |
| Mikhail Naganov | a7c4e03 | 2021-02-04 19:20:12 +0000 | [diff] [blame] | 611 | ALOGV("%s: %s %s=%s", __func__, RouteTraits::tag, Attributes::type, type.c_str()); |
| 612 | RouteTraits::Element route = new AudioRoute(routeType); |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 613 | |
| Mikhail Naganov | 43c386c | 2018-09-19 12:38:29 -0700 | [diff] [blame] | 614 | std::string sinkAttr = getXmlAttribute(cur, Attributes::sink); |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 615 | if (sinkAttr.empty()) { |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 616 | ALOGE("%s: No %s found", __func__, Attributes::sink); |
| Mikhail Naganov | 6ef1b95 | 2021-02-25 18:22:02 -0800 | [diff] [blame] | 617 | return BAD_VALUE; |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 618 | } |
| 619 | // Convert Sink name to port pointer |
| jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 620 | sp<PolicyAudioPort> sink = ctx->findPortByTagName(sinkAttr); |
| Mikhail Naganov | 6ef1b95 | 2021-02-25 18:22:02 -0800 | [diff] [blame] | 621 | if (sink == NULL && !mIgnoreVendorExtensions) { |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 622 | ALOGE("%s: no sink found with name=%s", __func__, sinkAttr.c_str()); |
| Mikhail Naganov | 6ef1b95 | 2021-02-25 18:22:02 -0800 | [diff] [blame] | 623 | return BAD_VALUE; |
| 624 | } else if (sink == NULL) { |
| 625 | ALOGW("Skipping route to sink \"%s\" as it likely has vendor extension type", |
| 626 | sinkAttr.c_str()); |
| 627 | return NO_INIT; |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 628 | } |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 629 | route->setSink(sink); |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 630 | |
| Mikhail Naganov | 43c386c | 2018-09-19 12:38:29 -0700 | [diff] [blame] | 631 | std::string sourcesAttr = getXmlAttribute(cur, Attributes::sources); |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 632 | if (sourcesAttr.empty()) { |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 633 | ALOGE("%s: No %s found", __func__, Attributes::sources); |
| Mikhail Naganov | 6ef1b95 | 2021-02-25 18:22:02 -0800 | [diff] [blame] | 634 | return BAD_VALUE; |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 635 | } |
| 636 | // Tokenize and Convert Sources name to port pointer |
| jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 637 | PolicyAudioPortVector sources; |
| Mikhail Naganov | 91d50bb | 2021-04-16 17:38:23 -0700 | [diff] [blame] | 638 | UniqueCPtr<char> sourcesLiteral{strndup( |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 639 | sourcesAttr.c_str(), strlen(sourcesAttr.c_str()))}; |
| 640 | char *devTag = strtok(sourcesLiteral.get(), ","); |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 641 | while (devTag != NULL) { |
| 642 | if (strlen(devTag) != 0) { |
| jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 643 | sp<PolicyAudioPort> source = ctx->findPortByTagName(devTag); |
| Mikhail Naganov | 6ef1b95 | 2021-02-25 18:22:02 -0800 | [diff] [blame] | 644 | if (source == NULL && !mIgnoreVendorExtensions) { |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 645 | ALOGE("%s: no source found with name=%s", __func__, devTag); |
| Mikhail Naganov | 6ef1b95 | 2021-02-25 18:22:02 -0800 | [diff] [blame] | 646 | return BAD_VALUE; |
| 647 | } else if (source == NULL) { |
| 648 | ALOGW("Skipping route source \"%s\" as it likely has vendor extension type", |
| 649 | devTag); |
| 650 | } else { |
| 651 | sources.add(source); |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 652 | } |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 653 | } |
| 654 | devTag = strtok(NULL, ","); |
| 655 | } |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 656 | |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 657 | sink->addRoute(route); |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 658 | for (size_t i = 0; i < sources.size(); i++) { |
| jiabin | 4ef9345 | 2019-09-10 14:29:54 -0700 | [diff] [blame] | 659 | sp<PolicyAudioPort> source = sources.itemAt(i); |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 660 | source->addRoute(route); |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 661 | } |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 662 | route->setSources(sources); |
| Mikhail Naganov | 43c386c | 2018-09-19 12:38:29 -0700 | [diff] [blame] | 663 | return route; |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 664 | } |
| 665 | |
| Mikhail Naganov | a7c4e03 | 2021-02-04 19:20:12 +0000 | [diff] [blame] | 666 | template<> |
| Mikhail Naganov | 6ef1b95 | 2021-02-25 18:22:02 -0800 | [diff] [blame] | 667 | std::variant<status_t, ModuleTraits::Element> PolicySerializer::deserialize<ModuleTraits>( |
| Mikhail Naganov | a7c4e03 | 2021-02-04 19:20:12 +0000 | [diff] [blame] | 668 | const xmlNode *cur, ModuleTraits::PtrSerializingCtx ctx) |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 669 | { |
| Mikhail Naganov | a7c4e03 | 2021-02-04 19:20:12 +0000 | [diff] [blame] | 670 | using Attributes = ModuleTraits::Attributes; |
| 671 | auto& tag = ModuleTraits::tag; |
| 672 | auto& childAttachedDevicesTag = ModuleTraits::childAttachedDevicesTag; |
| 673 | auto& childAttachedDeviceTag = ModuleTraits::childAttachedDeviceTag; |
| 674 | auto& childDefaultOutputDeviceTag = ModuleTraits::childDefaultOutputDeviceTag; |
| 675 | |
| Mikhail Naganov | 43c386c | 2018-09-19 12:38:29 -0700 | [diff] [blame] | 676 | std::string name = getXmlAttribute(cur, Attributes::name); |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 677 | if (name.empty()) { |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 678 | ALOGE("%s: No %s found", __func__, Attributes::name); |
| Mikhail Naganov | 6ef1b95 | 2021-02-25 18:22:02 -0800 | [diff] [blame] | 679 | return BAD_VALUE; |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 680 | } |
| Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 681 | uint32_t versionMajor = 0, versionMinor = 0; |
| Mikhail Naganov | 43c386c | 2018-09-19 12:38:29 -0700 | [diff] [blame] | 682 | std::string versionLiteral = getXmlAttribute(cur, Attributes::version); |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 683 | if (!versionLiteral.empty()) { |
| Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 684 | sscanf(versionLiteral.c_str(), "%u.%u", &versionMajor, &versionMinor); |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 685 | ALOGV("%s: mHalVersion = major %u minor %u", __func__, |
| Mikhail Naganov | 9ee0540 | 2016-10-13 15:58:17 -0700 | [diff] [blame] | 686 | versionMajor, versionMajor); |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 687 | } |
| 688 | |
| Mikhail Naganov | a7c4e03 | 2021-02-04 19:20:12 +0000 | [diff] [blame] | 689 | ALOGV("%s: %s %s=%s", __func__, ModuleTraits::tag, Attributes::name, name.c_str()); |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 690 | |
| Mikhail Naganov | a7c4e03 | 2021-02-04 19:20:12 +0000 | [diff] [blame] | 691 | ModuleTraits::Element module = new HwModule(name.c_str(), versionMajor, versionMinor); |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 692 | |
| Mikhail Naganov | a7c4e03 | 2021-02-04 19:20:12 +0000 | [diff] [blame] | 693 | // Deserialize children: Audio Mix Port, Audio Device Ports (Source/Sink), Audio Routes |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 694 | MixPortTraits::Collection mixPorts; |
| Mikhail Naganov | 43c386c | 2018-09-19 12:38:29 -0700 | [diff] [blame] | 695 | status_t status = deserializeCollection<MixPortTraits>(cur, &mixPorts, NULL); |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 696 | if (status != NO_ERROR) { |
| Mikhail Naganov | 6ef1b95 | 2021-02-25 18:22:02 -0800 | [diff] [blame] | 697 | return status; |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 698 | } |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 699 | module->setProfiles(mixPorts); |
| 700 | |
| 701 | DevicePortTraits::Collection devicePorts; |
| Mikhail Naganov | 43c386c | 2018-09-19 12:38:29 -0700 | [diff] [blame] | 702 | status = deserializeCollection<DevicePortTraits>(cur, &devicePorts, NULL); |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 703 | if (status != NO_ERROR) { |
| Mikhail Naganov | 6ef1b95 | 2021-02-25 18:22:02 -0800 | [diff] [blame] | 704 | return status; |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 705 | } |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 706 | module->setDeclaredDevices(devicePorts); |
| 707 | |
| 708 | RouteTraits::Collection routes; |
| Mikhail Naganov | 43c386c | 2018-09-19 12:38:29 -0700 | [diff] [blame] | 709 | status = deserializeCollection<RouteTraits>(cur, &routes, module.get()); |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 710 | if (status != NO_ERROR) { |
| Mikhail Naganov | 6ef1b95 | 2021-02-25 18:22:02 -0800 | [diff] [blame] | 711 | return status; |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 712 | } |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 713 | module->setRoutes(routes); |
| 714 | |
| Mikhail Naganov | 43c386c | 2018-09-19 12:38:29 -0700 | [diff] [blame] | 715 | for (const xmlNode *children = cur->xmlChildrenNode; children != NULL; |
| 716 | children = children->next) { |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 717 | if (!xmlStrcmp(children->name, reinterpret_cast<const xmlChar*>(childAttachedDevicesTag))) { |
| 718 | ALOGV("%s: %s %s found", __func__, tag, childAttachedDevicesTag); |
| Mikhail Naganov | 43c386c | 2018-09-19 12:38:29 -0700 | [diff] [blame] | 719 | for (const xmlNode *child = children->xmlChildrenNode; child != NULL; |
| 720 | child = child->next) { |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 721 | if (!xmlStrcmp(child->name, |
| 722 | reinterpret_cast<const xmlChar*>(childAttachedDeviceTag))) { |
| 723 | auto attachedDevice = make_xmlUnique(xmlNodeListGetString( |
| Mikhail Naganov | 43c386c | 2018-09-19 12:38:29 -0700 | [diff] [blame] | 724 | child->doc, child->xmlChildrenNode, 1)); |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 725 | if (attachedDevice != nullptr) { |
| 726 | ALOGV("%s: %s %s=%s", __func__, tag, childAttachedDeviceTag, |
| 727 | reinterpret_cast<const char*>(attachedDevice.get())); |
| 728 | sp<DeviceDescriptor> device = module->getDeclaredDevices(). |
| jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 729 | getDeviceFromTagName(std::string(reinterpret_cast<const char*>( |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 730 | attachedDevice.get()))); |
| Mikhail Naganov | 6ef1b95 | 2021-02-25 18:22:02 -0800 | [diff] [blame] | 731 | if (device == nullptr && mIgnoreVendorExtensions) { |
| 732 | ALOGW("Skipped attached device \"%s\" because it likely uses a vendor" |
| 733 | "extension type", |
| 734 | reinterpret_cast<const char*>(attachedDevice.get())); |
| 735 | continue; |
| 736 | } |
| Mikhail Naganov | c0d0498 | 2020-03-02 21:02:28 +0000 | [diff] [blame] | 737 | ctx->addDevice(device); |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 738 | } |
| 739 | } |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 740 | } |
| 741 | } |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 742 | if (!xmlStrcmp(children->name, |
| 743 | reinterpret_cast<const xmlChar*>(childDefaultOutputDeviceTag))) { |
| 744 | auto defaultOutputDevice = make_xmlUnique(xmlNodeListGetString( |
| Mikhail Naganov | 43c386c | 2018-09-19 12:38:29 -0700 | [diff] [blame] | 745 | children->doc, children->xmlChildrenNode, 1)); |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 746 | if (defaultOutputDevice != nullptr) { |
| 747 | ALOGV("%s: %s %s=%s", __func__, tag, childDefaultOutputDeviceTag, |
| 748 | reinterpret_cast<const char*>(defaultOutputDevice.get())); |
| 749 | sp<DeviceDescriptor> device = module->getDeclaredDevices().getDeviceFromTagName( |
| jiabin | 5740f08 | 2019-08-19 15:08:30 -0700 | [diff] [blame] | 750 | std::string(reinterpret_cast<const char*>(defaultOutputDevice.get()))); |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 751 | if (device != 0 && ctx->getDefaultOutputDevice() == 0) { |
| 752 | ctx->setDefaultOutputDevice(device); |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 753 | ALOGV("%s: default is %08x", |
| 754 | __func__, ctx->getDefaultOutputDevice()->type()); |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 755 | } |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 756 | } |
| 757 | } |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 758 | } |
| Mikhail Naganov | 43c386c | 2018-09-19 12:38:29 -0700 | [diff] [blame] | 759 | return module; |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 760 | } |
| 761 | |
| Mikhail Naganov | a7c4e03 | 2021-02-04 19:20:12 +0000 | [diff] [blame] | 762 | template<> |
| Mikhail Naganov | 6ef1b95 | 2021-02-25 18:22:02 -0800 | [diff] [blame] | 763 | std::variant<status_t, GlobalConfigTraits::Element> |
| 764 | PolicySerializer::deserialize<GlobalConfigTraits>( |
| Mikhail Naganov | a7c4e03 | 2021-02-04 19:20:12 +0000 | [diff] [blame] | 765 | const xmlNode *root, GlobalConfigTraits::PtrSerializingCtx config) |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 766 | { |
| Mikhail Naganov | a7c4e03 | 2021-02-04 19:20:12 +0000 | [diff] [blame] | 767 | using Attributes = GlobalConfigTraits::Attributes; |
| 768 | |
| Mikhail Naganov | 43c386c | 2018-09-19 12:38:29 -0700 | [diff] [blame] | 769 | for (const xmlNode *cur = root->xmlChildrenNode; cur != NULL; cur = cur->next) { |
| Mikhail Naganov | a7c4e03 | 2021-02-04 19:20:12 +0000 | [diff] [blame] | 770 | if (!xmlStrcmp(cur->name, reinterpret_cast<const xmlChar*>(GlobalConfigTraits::tag))) { |
| Eric Laurent | 8340e67 | 2019-11-06 11:01:08 -0800 | [diff] [blame] | 771 | bool value; |
| Mikhail Naganov | ffd9771 | 2023-05-03 17:45:36 -0700 | [diff] [blame^] | 772 | std::string attr = getXmlAttribute(cur, Attributes::callScreenModeSupported); |
| Eric Laurent | 8340e67 | 2019-11-06 11:01:08 -0800 | [diff] [blame] | 773 | if (!attr.empty() && |
| 774 | convertTo<std::string, bool>(attr, value)) { |
| 775 | config->setCallScreenModeSupported(value); |
| 776 | } |
| François Gaffie | 8475ae5 | 2019-11-19 09:50:48 +0100 | [diff] [blame] | 777 | std::string engineLibrarySuffix = getXmlAttribute(cur, Attributes::engineLibrarySuffix); |
| 778 | if (!engineLibrarySuffix.empty()) { |
| 779 | config->setEngineLibraryNameSuffix(engineLibrarySuffix); |
| 780 | } |
| Mikhail Naganov | 6ef1b95 | 2021-02-25 18:22:02 -0800 | [diff] [blame] | 781 | return NO_ERROR; |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 782 | } |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 783 | } |
| Mikhail Naganov | 6ef1b95 | 2021-02-25 18:22:02 -0800 | [diff] [blame] | 784 | return NO_ERROR; |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 785 | } |
| 786 | |
| Mikhail Naganov | a7c4e03 | 2021-02-04 19:20:12 +0000 | [diff] [blame] | 787 | template<> |
| Mikhail Naganov | 6ef1b95 | 2021-02-25 18:22:02 -0800 | [diff] [blame] | 788 | std::variant<status_t, SurroundSoundTraits::Element> |
| 789 | PolicySerializer::deserialize<SurroundSoundTraits>( |
| Mikhail Naganov | a7c4e03 | 2021-02-04 19:20:12 +0000 | [diff] [blame] | 790 | const xmlNode *root, SurroundSoundTraits::PtrSerializingCtx config) |
| Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 791 | { |
| 792 | config->setDefaultSurroundFormats(); |
| 793 | |
| 794 | for (const xmlNode *cur = root->xmlChildrenNode; cur != NULL; cur = cur->next) { |
| Mikhail Naganov | a7c4e03 | 2021-02-04 19:20:12 +0000 | [diff] [blame] | 795 | if (!xmlStrcmp(cur->name, reinterpret_cast<const xmlChar*>(SurroundSoundTraits::tag))) { |
| Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 796 | AudioPolicyConfig::SurroundFormats formats; |
| 797 | status_t status = deserializeCollection<SurroundSoundFormatTraits>( |
| 798 | cur, &formats, nullptr); |
| 799 | if (status == NO_ERROR) { |
| 800 | config->setSurroundFormats(formats); |
| 801 | } |
| Mikhail Naganov | 6ef1b95 | 2021-02-25 18:22:02 -0800 | [diff] [blame] | 802 | return NO_ERROR; |
| Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 803 | } |
| 804 | } |
| Mikhail Naganov | 6ef1b95 | 2021-02-25 18:22:02 -0800 | [diff] [blame] | 805 | return NO_ERROR; |
| Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 806 | } |
| 807 | |
| Mikhail Naganov | a7c4e03 | 2021-02-04 19:20:12 +0000 | [diff] [blame] | 808 | template<> |
| Mikhail Naganov | 6ef1b95 | 2021-02-25 18:22:02 -0800 | [diff] [blame] | 809 | std::variant<status_t, SurroundSoundFormatTraits::Element> |
| 810 | PolicySerializer::deserialize<SurroundSoundFormatTraits>( |
| Mikhail Naganov | a7c4e03 | 2021-02-04 19:20:12 +0000 | [diff] [blame] | 811 | const xmlNode *cur, SurroundSoundFormatTraits::PtrSerializingCtx /*serializingContext*/) |
| Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 812 | { |
| Mikhail Naganov | a7c4e03 | 2021-02-04 19:20:12 +0000 | [diff] [blame] | 813 | using Attributes = SurroundSoundFormatTraits::Attributes; |
| 814 | |
| Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 815 | std::string formatLiteral = getXmlAttribute(cur, Attributes::name); |
| 816 | if (formatLiteral.empty()) { |
| 817 | ALOGE("%s: No %s found for a surround format", __func__, Attributes::name); |
| Mikhail Naganov | 6ef1b95 | 2021-02-25 18:22:02 -0800 | [diff] [blame] | 818 | return BAD_VALUE; |
| 819 | } |
| 820 | if (mIgnoreVendorExtensions && maybeVendorExtension(formatLiteral)) { |
| 821 | ALOGI("%s: vendor extension format \"%s\" skipped", __func__, formatLiteral.c_str()); |
| 822 | return NO_INIT; |
| Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 823 | } |
| 824 | audio_format_t format = formatFromString(formatLiteral); |
| 825 | if (format == AUDIO_FORMAT_DEFAULT) { |
| 826 | ALOGE("%s: Unrecognized format %s", __func__, formatLiteral.c_str()); |
| Mikhail Naganov | 6ef1b95 | 2021-02-25 18:22:02 -0800 | [diff] [blame] | 827 | return BAD_VALUE; |
| Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 828 | } |
| Mikhail Naganov | a7c4e03 | 2021-02-04 19:20:12 +0000 | [diff] [blame] | 829 | SurroundSoundFormatTraits::Element pair = std::make_pair( |
| 830 | format, SurroundSoundFormatTraits::Collection::mapped_type{}); |
| Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 831 | |
| 832 | std::string subformatsLiteral = getXmlAttribute(cur, Attributes::subformats); |
| 833 | if (subformatsLiteral.empty()) return pair; |
| 834 | FormatVector subformats = formatsFromString(subformatsLiteral, " "); |
| 835 | for (const auto& subformat : subformats) { |
| 836 | auto result = pair.second.insert(subformat); |
| 837 | if (!result.second) { |
| 838 | ALOGE("%s: could not add subformat %x to collection", __func__, subformat); |
| Mikhail Naganov | 6ef1b95 | 2021-02-25 18:22:02 -0800 | [diff] [blame] | 839 | return BAD_VALUE; |
| Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 840 | } |
| 841 | } |
| 842 | return pair; |
| 843 | } |
| 844 | |
| Mikhail Naganov | 6ef1b95 | 2021-02-25 18:22:02 -0800 | [diff] [blame] | 845 | status_t PolicySerializer::deserialize(const char *configFile, AudioPolicyConfig *config, |
| 846 | bool ignoreVendorExtensions) |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 847 | { |
| Mikhail Naganov | 6ef1b95 | 2021-02-25 18:22:02 -0800 | [diff] [blame] | 848 | mIgnoreVendorExtensions = ignoreVendorExtensions; |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 849 | auto doc = make_xmlUnique(xmlParseFile(configFile)); |
| 850 | if (doc == nullptr) { |
| 851 | ALOGE("%s: Could not parse %s document.", __func__, configFile); |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 852 | return BAD_VALUE; |
| 853 | } |
| Mikhail Naganov | 43c386c | 2018-09-19 12:38:29 -0700 | [diff] [blame] | 854 | xmlNodePtr root = xmlDocGetRootElement(doc.get()); |
| 855 | if (root == NULL) { |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 856 | ALOGE("%s: Could not parse %s document: empty.", __func__, configFile); |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 857 | return BAD_VALUE; |
| 858 | } |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 859 | if (xmlXIncludeProcess(doc.get()) < 0) { |
| 860 | ALOGE("%s: libxml failed to resolve XIncludes on %s document.", __func__, configFile); |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 861 | } |
| 862 | |
| Mikhail Naganov | 43c386c | 2018-09-19 12:38:29 -0700 | [diff] [blame] | 863 | if (xmlStrcmp(root->name, reinterpret_cast<const xmlChar*>(rootName))) { |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 864 | ALOGE("%s: No %s root element found in xml data %s.", __func__, rootName, |
| Mikhail Naganov | 43c386c | 2018-09-19 12:38:29 -0700 | [diff] [blame] | 865 | reinterpret_cast<const char*>(root->name)); |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 866 | return BAD_VALUE; |
| 867 | } |
| 868 | |
| Mikhail Naganov | 43c386c | 2018-09-19 12:38:29 -0700 | [diff] [blame] | 869 | std::string version = getXmlAttribute(root, versionAttribute); |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 870 | if (version.empty()) { |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 871 | ALOGE("%s: No version found in root node %s", __func__, rootName); |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 872 | return BAD_VALUE; |
| 873 | } |
| Mikhail Naganov | 6718c39 | 2022-01-27 22:17:21 +0000 | [diff] [blame] | 874 | if (version == "7.0" || version == "7.1") { |
| Mikhail Naganov | a7c4e03 | 2021-02-04 19:20:12 +0000 | [diff] [blame] | 875 | mChannelMasksSeparator = mSamplingRatesSeparator = mFlagsSeparator = " "; |
| 876 | } else if (version != "1.0") { |
| Mikhail Naganov | 6718c39 | 2022-01-27 22:17:21 +0000 | [diff] [blame] | 877 | ALOGE("%s: Version does not match; expected \"1.0\", \"7.0\", or \"7.1\" got \"%s\"", |
| Mikhail Naganov | a7c4e03 | 2021-02-04 19:20:12 +0000 | [diff] [blame] | 878 | __func__, version.c_str()); |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 879 | return BAD_VALUE; |
| 880 | } |
| Mikhail Naganov | a7c4e03 | 2021-02-04 19:20:12 +0000 | [diff] [blame] | 881 | // Let's deserialize children |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 882 | // Modules |
| 883 | ModuleTraits::Collection modules; |
| Mikhail Naganov | 43c386c | 2018-09-19 12:38:29 -0700 | [diff] [blame] | 884 | status_t status = deserializeCollection<ModuleTraits>(root, &modules, config); |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 885 | if (status != NO_ERROR) { |
| 886 | return status; |
| 887 | } |
| 888 | config->setHwModules(modules); |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 889 | |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 890 | // Global Configuration |
| Mikhail Naganov | a7c4e03 | 2021-02-04 19:20:12 +0000 | [diff] [blame] | 891 | deserialize<GlobalConfigTraits>(root, config); |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 892 | |
| Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 893 | // Surround configuration |
| Mikhail Naganov | a7c4e03 | 2021-02-04 19:20:12 +0000 | [diff] [blame] | 894 | deserialize<SurroundSoundTraits>(root, config); |
| Mikhail Naganov | 778bc1f | 2018-09-14 16:28:52 -0700 | [diff] [blame] | 895 | |
| François Gaffie | f4ad6e5 | 2015-11-19 16:59:57 +0100 | [diff] [blame] | 896 | return android::OK; |
| 897 | } |
| 898 | |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 899 | } // namespace |
| 900 | |
| 901 | status_t deserializeAudioPolicyFile(const char *fileName, AudioPolicyConfig *config) |
| 902 | { |
| 903 | PolicySerializer serializer; |
| Mikhail Naganov | 8916ae9 | 2020-10-21 13:04:58 -0700 | [diff] [blame] | 904 | status_t status = serializer.deserialize(fileName, config); |
| Mikhail Naganov | 8916ae9 | 2020-10-21 13:04:58 -0700 | [diff] [blame] | 905 | return status; |
| Mikhail Naganov | a289aea | 2018-09-17 15:26:23 -0700 | [diff] [blame] | 906 | } |
| 907 | |
| Mikhail Naganov | 6ef1b95 | 2021-02-25 18:22:02 -0800 | [diff] [blame] | 908 | status_t deserializeAudioPolicyFileForVts(const char *fileName, AudioPolicyConfig *config) |
| 909 | { |
| 910 | PolicySerializer serializer; |
| 911 | status_t status = serializer.deserialize(fileName, config, true /*ignoreVendorExtensions*/); |
| Mikhail Naganov | 6ef1b95 | 2021-02-25 18:22:02 -0800 | [diff] [blame] | 912 | return status; |
| 913 | } |
| 914 | |
| Mikhail Naganov | 1b2a794 | 2017-12-08 10:18:09 -0800 | [diff] [blame] | 915 | } // namespace android |