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 <system/audio.h> 22 #include <AudioPolicyManagerInterface.h> 23 #include <utils/RefBase.h> 24 #include <HandleGenerator.h> 25 #include <string> 26 #include <vector> 27 #include <map> 28 #include <utils/Errors.h> 29 #include <utils/String8.h> 30 31 namespace android { 32 33 /** 34 * @brief The ProductStrategy class describes for each product_strategy_t identifier the 35 * associated audio attributes, the device types to use, the device address to use. 36 * The identifier is voluntarily not strongly typed in order to be extensible by OEM. 37 */ 38 class ProductStrategy : public virtual RefBase, private HandleGenerator<uint32_t> 39 { 40 private: 41 struct AudioAttributes { 42 audio_stream_type_t mStream = AUDIO_STREAM_DEFAULT; 43 volume_group_t mVolumeGroup = VOLUME_GROUP_NONE; 44 audio_attributes_t mAttributes = AUDIO_ATTRIBUTES_INITIALIZER; 45 }; 46 47 using AudioAttributesVector = std::vector<AudioAttributes>; 48 49 public: 50 ProductStrategy(const std::string &name); 51 52 void addAttributes(const AudioAttributes &audioAttributes); 53 54 std::vector<android::AudioAttributes> listAudioAttributes() const; 55 56 std::string getName() const { return mName; } 57 AttributesVector getAudioAttributes() const; 58 product_strategy_t getId() const { return mId; } 59 StreamTypeVector getSupportedStreams() const; 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 true if attributes matches with the strategy, false otherwise. 68 */ 69 bool matches(const audio_attributes_t attributes) const; 70 71 bool supportStreamType(const audio_stream_type_t &streamType) const; 72 73 void setDeviceAddress(const std::string &address) 74 { 75 mDeviceAddress = address; 76 } 77 78 std::string getDeviceAddress() const { return mDeviceAddress; } 79 80 void setDeviceTypes(audio_devices_t devices) 81 { 82 mApplicableDevices = devices; 83 } 84 85 audio_devices_t getDeviceTypes() const { return mApplicableDevices; } 86 87 audio_attributes_t getAttributesForStreamType(audio_stream_type_t stream) const; 88 audio_stream_type_t getStreamTypeForAttributes(const audio_attributes_t &attr) const; 89 90 volume_group_t getVolumeGroupForAttributes(const audio_attributes_t &attr) const; 91 92 volume_group_t getVolumeGroupForStreamType(audio_stream_type_t stream) const; 93 94 volume_group_t getDefaultVolumeGroup() const; 95 96 bool isDefault() const; 97 98 void dump(String8 *dst, int spaces = 0) const; 99 100 private: 101 std::string mName; 102 103 AudioAttributesVector mAttributesVector; 104 105 product_strategy_t mId; 106 107 std::string mDeviceAddress; /**< Device address applicable for this strategy, maybe empty */ 108 109 /** 110 * Applicable device(s) type mask for this strategy. 111 */ 112 audio_devices_t mApplicableDevices = AUDIO_DEVICE_NONE; 113 }; 114 115 class ProductStrategyMap : public std::map<product_strategy_t, sp<ProductStrategy> > 116 { 117 public: 118 /** 119 * @brief initialize: set default product strategy in cache. 120 */ 121 void initialize(); 122 /** 123 * @brief getProductStrategyForAttribute. The order of the vector is dimensionning. 124 * @param attr 125 * @return applicable product strategy for the given attribute, default if none applicable. 126 */ 127 product_strategy_t getProductStrategyForAttributes(const audio_attributes_t &attr) const; 128 129 product_strategy_t getProductStrategyForStream(audio_stream_type_t stream) const; 130 131 audio_attributes_t getAttributesForStreamType(audio_stream_type_t stream) const; 132 133 audio_stream_type_t getStreamTypeForAttributes(const audio_attributes_t &attr) const; 134 135 /** 136 * @brief getAttributesForProductStrategy can be called from 137 * AudioManager: in this case, the product strategy IS the former routing strategy 138 * CarAudioManager: in this case, the product strategy IS the car usage 139 * [getAudioAttributesForCarUsage] 140 * OemExtension: in this case, the product strategy IS the Oem usage 141 * 142 * @param strategy 143 * @return audio attributes (or at least one of the attributes) following the given strategy. 144 */ 145 audio_attributes_t getAttributesForProductStrategy(product_strategy_t strategy) const; 146 147 audio_devices_t getDeviceTypesForProductStrategy(product_strategy_t strategy) const; 148 149 std::string getDeviceAddressForProductStrategy(product_strategy_t strategy) const; 150 151 volume_group_t getVolumeGroupForAttributes(const audio_attributes_t &attr) const; 152 153 volume_group_t getVolumeGroupForStreamType(audio_stream_type_t stream) 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 product_strategy_t mDefaultStrategy = PRODUCT_STRATEGY_NONE; 163 }; 164 165 } // namespace android 166