1 /* 2 * Copyright (C) 2018 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 "VolumeGroup.h" 20 21 #include <map> 22 #include <string> 23 #include <utility> 24 #include <vector> 25 26 #include <HandleGenerator.h> 27 #include <media/VolumeGroupAttributes.h> 28 #include <media/AudioContainers.h> 29 #include <media/AudioDeviceTypeAddr.h> 30 #include <media/AudioPolicy.h> 31 #include <system/audio.h> 32 #include <utils/Errors.h> 33 #include <utils/RefBase.h> 34 #include <utils/String8.h> 35 36 namespace android { 37 38 /** 39 * @brief The ProductStrategy class describes for each product_strategy_t identifier the 40 * associated audio attributes, the device types to use, the device address to use. 41 * The identifier is voluntarily not strongly typed in order to be extensible by OEM. 42 */ 43 class ProductStrategy : public virtual RefBase, private HandleGenerator<uint32_t> 44 { 45 private: 46 using VolumeGroupAttributesVector = std::vector<VolumeGroupAttributes>; 47 48 public: 49 ProductStrategy(const std::string &name); 50 51 void addAttributes(const VolumeGroupAttributes &volumeGroupAttributes); 52 53 std::vector<android::VolumeGroupAttributes> listVolumeGroupAttributes() const; 54 getName()55 std::string getName() const { return mName; } 56 AttributesVector getAudioAttributes() const; getId()57 product_strategy_t getId() const { return mId; } 58 StreamTypeVector getSupportedStreams() const; getVolumeGroupAttributes()59 VolumeGroupAttributesVector getVolumeGroupAttributes() const { return mAttributesVector; } 60 61 /** 62 * @brief matches checks if the given audio attributes shall follow the strategy. 63 * Order of the attributes within a strategy matters. 64 * If only the usage is available, the check is performed on the usages of the given 65 * attributes, otherwise all fields must match. 66 * @param attributes to consider 67 * @return matching score, negative value if no match. 68 */ 69 int matchesScore(const audio_attributes_t attributes) const; 70 71 bool supportStreamType(const audio_stream_type_t &streamType) const; 72 setDeviceAddress(const std::string & address)73 void setDeviceAddress(const std::string &address) 74 { 75 mDeviceAddress = address; 76 } 77 getDeviceAddress()78 std::string getDeviceAddress() const { return mDeviceAddress; } 79 setDeviceTypes(const DeviceTypeSet & devices)80 void setDeviceTypes(const DeviceTypeSet& devices) 81 { 82 mApplicableDevices = devices; 83 } 84 getDeviceTypes()85 DeviceTypeSet getDeviceTypes() const { return mApplicableDevices; } 86 87 audio_attributes_t getAttributesForStreamType(audio_stream_type_t stream) const; 88 89 volume_group_t getVolumeGroupForStreamType(audio_stream_type_t stream) const; 90 91 volume_group_t getDefaultVolumeGroup() const; 92 93 bool isDefault() const; 94 95 void dump(String8 *dst, int spaces = 0) const; 96 97 private: 98 std::string mName; 99 100 VolumeGroupAttributesVector mAttributesVector; 101 102 product_strategy_t mId; 103 104 std::string mDeviceAddress; /**< Device address applicable for this strategy, maybe empty */ 105 106 /** 107 * Applicable device(s) type mask for this strategy. 108 */ 109 DeviceTypeSet mApplicableDevices; 110 }; 111 112 class ProductStrategyMap : public std::map<product_strategy_t, sp<ProductStrategy> > 113 { 114 public: 115 /** 116 * @brief initialize: set default product strategy in cache. 117 */ 118 void initialize(); 119 /** 120 * @brief getProductStrategyForAttribute. The order of the vector is dimensionning. 121 * @param attr 122 * @return applicable product strategy for the given attribute, default if none applicable. 123 */ 124 product_strategy_t getProductStrategyForAttributes( 125 const audio_attributes_t &attr, bool fallbackOnDefault = true) const; 126 127 product_strategy_t getProductStrategyForStream(audio_stream_type_t stream) const; 128 129 audio_attributes_t getAttributesForStreamType(audio_stream_type_t stream) const; 130 131 audio_stream_type_t getStreamTypeForAttributes(const audio_attributes_t &attr) const; 132 133 /** 134 * @brief getAttributesForProductStrategy can be called from 135 * AudioManager: in this case, the product strategy IS the former routing strategy 136 * CarAudioManager: in this case, the product strategy IS the car usage 137 * [getAudioAttributesForCarUsage] 138 * OemExtension: in this case, the product strategy IS the Oem usage 139 * 140 * @param strategy 141 * @return audio attributes (or at least one of the attributes) following the given strategy. 142 */ 143 audio_attributes_t getAttributesForProductStrategy(product_strategy_t strategy) const; 144 145 DeviceTypeSet getDeviceTypesForProductStrategy(product_strategy_t strategy) const; 146 147 std::string getDeviceAddressForProductStrategy(product_strategy_t strategy) const; 148 149 volume_group_t getVolumeGroupForAttributes( 150 const audio_attributes_t &attr, bool fallbackOnDefault = true) const; 151 152 volume_group_t getVolumeGroupForStreamType( 153 audio_stream_type_t stream, bool fallbackOnDefault = true) const; 154 155 volume_group_t getDefaultVolumeGroup() const; 156 157 product_strategy_t getDefault() const; 158 159 void dump(String8 *dst, int spaces = 0) const; 160 161 private: 162 VolumeGroupAttributes getVolumeGroupAttributesForAttributes( 163 const audio_attributes_t &attr, bool fallbackOnDefault = true) const; 164 165 product_strategy_t mDefaultStrategy = PRODUCT_STRATEGY_NONE; 166 }; 167 168 using ProductStrategyDevicesRoleMap = 169 std::map<std::pair<product_strategy_t, device_role_t>, AudioDeviceTypeAddrVector>; 170 171 void dumpProductStrategyDevicesRoleMap( 172 const ProductStrategyDevicesRoleMap& productStrategyDeviceRoleMap, 173 String8 *dst, 174 int spaces); 175 176 } // namespace android 177