xref: /aosp_15_r20/frameworks/av/services/audiopolicy/engine/common/include/ProductStrategy.h (revision ec779b8e0859a360c3d303172224686826e6e0e1)
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, int id = PRODUCT_STRATEGY_NONE);
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 
isPatchStrategy()95     bool isPatchStrategy() const {
96         return getVolumeGroupForStreamType(AUDIO_STREAM_PATCH) != VOLUME_GROUP_NONE;
97     }
98 
99     void dump(String8 *dst, int spaces = 0) const;
100 
101 private:
102     std::string mName;
103 
104     VolumeGroupAttributesVector mAttributesVector;
105 
106     product_strategy_t mId;
107 
108     std::string mDeviceAddress; /**< Device address applicable for this strategy, maybe empty */
109 
110     /**
111      * Applicable device(s) type mask for this strategy.
112      */
113     DeviceTypeSet mApplicableDevices;
114 };
115 
116 class ProductStrategyMap : public std::map<product_strategy_t, sp<ProductStrategy> >
117 {
118 public:
119     /**
120      * @brief initialize: set default product strategy in cache.
121      */
122     void initialize();
123     /**
124      * @brief getProductStrategyForAttribute. The order of the vector is dimensionning.
125      * @param attr
126      * @return applicable product strategy for the given attribute, default if none applicable.
127      */
128     product_strategy_t getProductStrategyForAttributes(
129             const audio_attributes_t &attr, bool fallbackOnDefault = true) const;
130 
131     product_strategy_t getProductStrategyForStream(audio_stream_type_t stream) const;
132 
133     audio_attributes_t getAttributesForStreamType(audio_stream_type_t stream) const;
134 
135     audio_stream_type_t getStreamTypeForAttributes(const audio_attributes_t &attr) const;
136 
137     /**
138      * @brief getAttributesForProductStrategy can be called from
139      *        AudioManager: in this case, the product strategy IS the former routing strategy
140      *        CarAudioManager: in this case, the product strategy IS the car usage
141      *                      [getAudioAttributesForCarUsage]
142      *        OemExtension: in this case, the product strategy IS the Oem usage
143      *
144      * @param strategy
145      * @return audio attributes (or at least one of the attributes) following the given strategy.
146      */
147     audio_attributes_t getAttributesForProductStrategy(product_strategy_t strategy) const;
148 
149     DeviceTypeSet getDeviceTypesForProductStrategy(product_strategy_t strategy) const;
150 
151     std::string getDeviceAddressForProductStrategy(product_strategy_t strategy) const;
152 
153     volume_group_t getVolumeGroupForAttributes(
154             const audio_attributes_t &attr, bool fallbackOnDefault = true) const;
155 
156     volume_group_t getVolumeGroupForStreamType(
157             audio_stream_type_t stream, bool fallbackOnDefault = true) const;
158 
159     volume_group_t getDefaultVolumeGroup() const;
160 
161     product_strategy_t getDefault() const;
162 
163     void dump(String8 *dst, int spaces = 0) const;
164 
165 private:
166     VolumeGroupAttributes getVolumeGroupAttributesForAttributes(
167             const audio_attributes_t &attr, bool fallbackOnDefault = true) const;
168 
169     product_strategy_t mDefaultStrategy = PRODUCT_STRATEGY_NONE;
170 };
171 
172 using ProductStrategyDevicesRoleMap =
173         std::map<std::pair<product_strategy_t, device_role_t>, AudioDeviceTypeAddrVector>;
174 
175 void dumpProductStrategyDevicesRoleMap(
176         const ProductStrategyDevicesRoleMap& productStrategyDeviceRoleMap,
177         String8 *dst,
178         int spaces);
179 
180 } // namespace android
181