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 <utils/RefBase.h>
23 #include <HandleGenerator.h>
24 #include <string>
25 #include <vector>
26 #include <map>
27 #include <utils/Errors.h>
28 #include <utils/String8.h>
29 #include <media/AudioAttributes.h>
30 #include <media/AudioContainers.h>
31 #include <media/AudioPolicy.h>
32 
33 namespace android {
34 
35 /**
36  * @brief The ProductStrategy class describes for each product_strategy_t identifier the
37  * associated audio attributes, the device types to use, the device address to use.
38  * The identifier is voluntarily not strongly typed in order to be extensible by OEM.
39  */
40 class ProductStrategy : public virtual RefBase, private HandleGenerator<uint32_t>
41 {
42 private:
43     struct AudioAttributes {
44         audio_stream_type_t mStream = AUDIO_STREAM_DEFAULT;
45         volume_group_t mVolumeGroup = VOLUME_GROUP_NONE;
46         audio_attributes_t mAttributes = AUDIO_ATTRIBUTES_INITIALIZER;
47     };
48 
49     using AudioAttributesVector = std::vector<AudioAttributes>;
50 
51 public:
52     ProductStrategy(const std::string &name);
53 
54     void addAttributes(const AudioAttributes &audioAttributes);
55 
56     std::vector<android::AudioAttributes> listAudioAttributes() const;
57 
getName()58     std::string getName() const { return mName; }
59     AttributesVector getAudioAttributes() const;
getId()60     product_strategy_t getId() const { return mId; }
61     StreamTypeVector getSupportedStreams() const;
62 
63     /**
64      * @brief matches checks if the given audio attributes shall follow the strategy.
65      *        Order of the attributes within a strategy matters.
66      *        If only the usage is available, the check is performed on the usages of the given
67      *        attributes, otherwise all fields must match.
68      * @param attributes to consider
69      * @return true if attributes matches with the strategy, false otherwise.
70      */
71     bool matches(const audio_attributes_t attributes) const;
72 
73     bool supportStreamType(const audio_stream_type_t &streamType) const;
74 
setDeviceAddress(const std::string & address)75     void setDeviceAddress(const std::string &address)
76     {
77         mDeviceAddress = address;
78     }
79 
getDeviceAddress()80     std::string getDeviceAddress() const { return mDeviceAddress; }
81 
setDeviceTypes(const DeviceTypeSet & devices)82     void setDeviceTypes(const DeviceTypeSet& devices)
83     {
84         mApplicableDevices = devices;
85     }
86 
getDeviceTypes()87     DeviceTypeSet getDeviceTypes() const { return mApplicableDevices; }
88 
89     audio_attributes_t getAttributesForStreamType(audio_stream_type_t stream) const;
90     audio_stream_type_t getStreamTypeForAttributes(const audio_attributes_t &attr) const;
91 
92     volume_group_t getVolumeGroupForAttributes(const audio_attributes_t &attr) const;
93 
94     volume_group_t getVolumeGroupForStreamType(audio_stream_type_t stream) const;
95 
96     volume_group_t getDefaultVolumeGroup() const;
97 
98     bool isDefault() const;
99 
100     void dump(String8 *dst, int spaces = 0) const;
101 
102 private:
103     std::string mName;
104 
105     AudioAttributesVector mAttributesVector;
106 
107     product_strategy_t mId;
108 
109     std::string mDeviceAddress; /**< Device address applicable for this strategy, maybe empty */
110 
111     /**
112      * Applicable device(s) type mask for this strategy.
113      */
114     DeviceTypeSet mApplicableDevices;
115 };
116 
117 class ProductStrategyMap : public std::map<product_strategy_t, sp<ProductStrategy> >
118 {
119 public:
120     /**
121      * @brief initialize: set default product strategy in cache.
122      */
123     void initialize();
124     /**
125      * @brief getProductStrategyForAttribute. The order of the vector is dimensionning.
126      * @param attr
127      * @return applicable product strategy for the given attribute, default if none applicable.
128      */
129     product_strategy_t getProductStrategyForAttributes(const audio_attributes_t &attr) 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(const audio_attributes_t &attr) const;
154 
155     volume_group_t getVolumeGroupForStreamType(audio_stream_type_t stream) const;
156 
157     volume_group_t getDefaultVolumeGroup() const;
158 
159     product_strategy_t getDefault() const;
160 
161     void dump(String8 *dst, int spaces = 0) const;
162 
163 private:
164     product_strategy_t mDefaultStrategy = PRODUCT_STRATEGY_NONE;
165 };
166 
167 class ProductStrategyPreferredRoutingMap : public std::map<product_strategy_t, AudioDeviceTypeAddr>
168 {
169 public:
170     void dump(String8 *dst, int spaces = 0) const;
171 };
172 
173 } // namespace android
174