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/AudioAttributes.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 struct AudioAttributes { 47 audio_stream_type_t mStream = AUDIO_STREAM_DEFAULT; 48 volume_group_t mVolumeGroup = VOLUME_GROUP_NONE; 49 audio_attributes_t mAttributes = AUDIO_ATTRIBUTES_INITIALIZER; 50 }; 51 52 using AudioAttributesVector = std::vector<AudioAttributes>; 53 54 public: 55 ProductStrategy(const std::string &name); 56 57 void addAttributes(const AudioAttributes &audioAttributes); 58 59 std::vector<android::AudioAttributes> listAudioAttributes() const; 60 getName()61 std::string getName() const { return mName; } 62 AttributesVector getAudioAttributes() const; getId()63 product_strategy_t getId() const { return mId; } 64 StreamTypeVector getSupportedStreams() const; 65 66 /** 67 * @brief matches checks if the given audio attributes shall follow the strategy. 68 * Order of the attributes within a strategy matters. 69 * If only the usage is available, the check is performed on the usages of the given 70 * attributes, otherwise all fields must match. 71 * @param attributes to consider 72 * @return true if attributes matches with the strategy, false otherwise. 73 */ 74 bool matches(const audio_attributes_t attributes) const; 75 76 bool supportStreamType(const audio_stream_type_t &streamType) const; 77 setDeviceAddress(const std::string & address)78 void setDeviceAddress(const std::string &address) 79 { 80 mDeviceAddress = address; 81 } 82 getDeviceAddress()83 std::string getDeviceAddress() const { return mDeviceAddress; } 84 setDeviceTypes(const DeviceTypeSet & devices)85 void setDeviceTypes(const DeviceTypeSet& devices) 86 { 87 mApplicableDevices = devices; 88 } 89 getDeviceTypes()90 DeviceTypeSet getDeviceTypes() const { return mApplicableDevices; } 91 92 audio_attributes_t getAttributesForStreamType(audio_stream_type_t stream) const; 93 audio_stream_type_t getStreamTypeForAttributes(const audio_attributes_t &attr) const; 94 95 volume_group_t getVolumeGroupForAttributes(const audio_attributes_t &attr) const; 96 97 volume_group_t getVolumeGroupForStreamType(audio_stream_type_t stream) const; 98 99 volume_group_t getDefaultVolumeGroup() const; 100 101 bool isDefault() const; 102 103 void dump(String8 *dst, int spaces = 0) const; 104 105 private: 106 std::string mName; 107 108 AudioAttributesVector mAttributesVector; 109 110 product_strategy_t mId; 111 112 std::string mDeviceAddress; /**< Device address applicable for this strategy, maybe empty */ 113 114 /** 115 * Applicable device(s) type mask for this strategy. 116 */ 117 DeviceTypeSet mApplicableDevices; 118 }; 119 120 class ProductStrategyMap : public std::map<product_strategy_t, sp<ProductStrategy> > 121 { 122 public: 123 /** 124 * @brief initialize: set default product strategy in cache. 125 */ 126 void initialize(); 127 /** 128 * @brief getProductStrategyForAttribute. The order of the vector is dimensionning. 129 * @param attr 130 * @return applicable product strategy for the given attribute, default if none applicable. 131 */ 132 product_strategy_t getProductStrategyForAttributes( 133 const audio_attributes_t &attr, bool fallbackOnDefault = true) const; 134 135 product_strategy_t getProductStrategyForStream(audio_stream_type_t stream) const; 136 137 audio_attributes_t getAttributesForStreamType(audio_stream_type_t stream) const; 138 139 audio_stream_type_t getStreamTypeForAttributes(const audio_attributes_t &attr) const; 140 141 /** 142 * @brief getAttributesForProductStrategy can be called from 143 * AudioManager: in this case, the product strategy IS the former routing strategy 144 * CarAudioManager: in this case, the product strategy IS the car usage 145 * [getAudioAttributesForCarUsage] 146 * OemExtension: in this case, the product strategy IS the Oem usage 147 * 148 * @param strategy 149 * @return audio attributes (or at least one of the attributes) following the given strategy. 150 */ 151 audio_attributes_t getAttributesForProductStrategy(product_strategy_t strategy) const; 152 153 DeviceTypeSet getDeviceTypesForProductStrategy(product_strategy_t strategy) const; 154 155 std::string getDeviceAddressForProductStrategy(product_strategy_t strategy) const; 156 157 volume_group_t getVolumeGroupForAttributes( 158 const audio_attributes_t &attr, bool fallbackOnDefault = true) const; 159 160 volume_group_t getVolumeGroupForStreamType( 161 audio_stream_type_t stream, bool fallbackOnDefault = true) const; 162 163 volume_group_t getDefaultVolumeGroup() const; 164 165 product_strategy_t getDefault() const; 166 167 void dump(String8 *dst, int spaces = 0) const; 168 169 private: 170 product_strategy_t mDefaultStrategy = PRODUCT_STRATEGY_NONE; 171 }; 172 173 using ProductStrategyDevicesRoleMap = 174 std::map<std::pair<product_strategy_t, device_role_t>, AudioDeviceTypeAddrVector>; 175 176 void dumpProductStrategyDevicesRoleMap( 177 const ProductStrategyDevicesRoleMap& productStrategyDeviceRoleMap, 178 String8 *dst, 179 int spaces); 180 181 } // namespace android 182