1 /*
2  * Copyright (C) 2015 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 "PolicyAudioPort.h"
20 #include <media/AudioContainers.h>
21 #include <media/DeviceDescriptorBase.h>
22 #include <utils/Errors.h>
23 #include <utils/String8.h>
24 #include <utils/SortedVector.h>
25 #include <cutils/config_utils.h>
26 #include <system/audio.h>
27 #include <system/audio_policy.h>
28 
29 namespace android {
30 
31 class AudioPolicyClientInterface;
32 
33 class DeviceDescriptor : public DeviceDescriptorBase,
34                          public PolicyAudioPort, public PolicyAudioPortConfig
35 {
36 public:
37      // Note that empty name refers by convention to a generic device.
38     explicit DeviceDescriptor(audio_devices_t type);
39     DeviceDescriptor(audio_devices_t type, const std::string &tagName,
40             const FormatVector &encodedFormats = FormatVector{});
41     DeviceDescriptor(audio_devices_t type, const std::string &tagName,
42             const std::string &address, const FormatVector &encodedFormats = FormatVector{});
43     DeviceDescriptor(const AudioDeviceTypeAddr &deviceTypeAddr, const std::string &tagName = "",
44             const FormatVector &encodedFormats = FormatVector{});
45 
~DeviceDescriptor()46     virtual ~DeviceDescriptor() {}
47 
addAudioProfile(const sp<AudioProfile> & profile)48     virtual void addAudioProfile(const sp<AudioProfile> &profile) {
49         addAudioProfileAndSort(mProfiles, profile);
50     }
51 
getTagName()52     virtual const std::string getTagName() const { return mTagName; }
53 
encodedFormats()54     const FormatVector& encodedFormats() const { return mEncodedFormats; }
55 
getEncodedFormat()56     audio_format_t getEncodedFormat() { return mCurrentEncodedFormat; }
57 
setEncodedFormat(audio_format_t format)58     void setEncodedFormat(audio_format_t format) {
59         mCurrentEncodedFormat = format;
60     }
61 
62     bool equals(const sp<DeviceDescriptor>& other) const;
63 
64     bool hasCurrentEncodedFormat() const;
65 
66     bool supportsFormat(audio_format_t format);
67 
setDynamic()68     void setDynamic() { mIsDynamic = true; }
isDynamic()69     bool isDynamic() const { return mIsDynamic; }
70 
71     // PolicyAudioPortConfig
getPolicyAudioPort()72     virtual sp<PolicyAudioPort> getPolicyAudioPort() const {
73         return static_cast<PolicyAudioPort*>(const_cast<DeviceDescriptor*>(this));
74     }
75 
76     // AudioPortConfig
77     virtual status_t applyAudioPortConfig(const struct audio_port_config *config,
78                                           struct audio_port_config *backupConfig = NULL);
79     virtual void toAudioPortConfig(struct audio_port_config *dstConfig,
80             const struct audio_port_config *srcConfig = NULL) const;
81 
82     // PolicyAudioPort
asAudioPort()83     virtual sp<AudioPort> asAudioPort() const {
84         return static_cast<AudioPort*>(const_cast<DeviceDescriptor*>(this));
85     }
86     virtual void attach(const sp<HwModule>& module);
87     virtual void detach();
88 
89     // AudioPort
90     virtual void toAudioPort(struct audio_port *port) const;
91     virtual void toAudioPort(struct audio_port_v7 *port) const;
92 
93     void importAudioPortAndPickAudioProfile(const sp<PolicyAudioPort>& policyPort,
94                                             bool force = false);
95 
96     void setEncapsulationInfoFromHal(AudioPolicyClientInterface *clientInterface);
97 
98     void dump(String8 *dst, int spaces, int index, bool verbose = true) const;
99 
100 private:
101     template <typename T, std::enable_if_t<std::is_same<T, struct audio_port>::value
102                                         || std::is_same<T, struct audio_port_v7>::value, int> = 0>
103     void toAudioPortInternal(T* port) const {
104         DeviceDescriptorBase::toAudioPort(port);
105         port->ext.device.hw_module = getModuleHandle();
106     }
107 
108     std::string mTagName; // Unique human readable identifier for a device port found in conf file.
109     FormatVector        mEncodedFormats;
110     audio_format_t      mCurrentEncodedFormat;
111     bool                mIsDynamic = false;
112     const std::string   mDeclaredAddress; // Original device address
113 };
114 
115 class DeviceVector : public SortedVector<sp<DeviceDescriptor> >
116 {
117 public:
118     DeviceVector() : SortedVector() {}
119     explicit DeviceVector(const sp<DeviceDescriptor>& item) : DeviceVector()
120     {
121         add(item);
122     }
123 
124     ssize_t add(const sp<DeviceDescriptor>& item);
125     void add(const DeviceVector &devices);
126     ssize_t remove(const sp<DeviceDescriptor>& item);
127     void remove(const DeviceVector &devices);
128     ssize_t indexOf(const sp<DeviceDescriptor>& item) const;
129 
130     DeviceTypeSet types() const { return mDeviceTypes; }
131 
132     // If 'address' is empty and 'codec' is AUDIO_FORMAT_DEFAULT, a device with a non-empty
133     // address may be returned if there is no device with the specified 'type' and empty address.
134     sp<DeviceDescriptor> getDevice(audio_devices_t type, const String8 &address,
135                                    audio_format_t codec) const;
136     DeviceVector getDevicesFromTypes(const DeviceTypeSet& types) const;
137     DeviceVector getDevicesFromType(audio_devices_t type) const {
138         return getDevicesFromTypes({type});
139     }
140 
141     /**
142      * @brief getDeviceFromId
143      * @param id of the DeviceDescriptor to seach (aka Port handle).
144      * @return DeviceDescriptor associated to port id if found, nullptr otherwise. If the id is
145      * equal to AUDIO_PORT_HANDLE_NONE, it also returns a nullptr.
146      */
147     sp<DeviceDescriptor> getDeviceFromId(audio_port_handle_t id) const;
148     sp<DeviceDescriptor> getDeviceFromTagName(const std::string &tagName) const;
149     DeviceVector getDevicesFromHwModule(audio_module_handle_t moduleHandle) const;
150 
151     DeviceVector getFirstDevicesFromTypes(std::vector<audio_devices_t> orderedTypes) const;
152     sp<DeviceDescriptor> getFirstExistingDevice(std::vector<audio_devices_t> orderedTypes) const;
153 
154     // Return device descriptor that is used to open an input/output stream.
155     // Null pointer will be returned if
156     //     1) this collection is empty
157     //     2) the device descriptors are not the same category(input or output)
158     //     3) there are more than one device type for input case
159     //     4) the combination of all devices is invalid for selection
160     sp<DeviceDescriptor> getDeviceForOpening() const;
161 
162     // Return the device descriptor that matches the given AudioDeviceTypeAddr
163     sp<DeviceDescriptor> getDeviceFromDeviceTypeAddr(
164             const AudioDeviceTypeAddr& deviceTypeAddr) const;
165 
166     // Return the device vector that contains device descriptor whose AudioDeviceTypeAddr appears
167     // in the given AudioDeviceTypeAddrVector
168     DeviceVector getDevicesFromDeviceTypeAddrVec(
169             const AudioDeviceTypeAddrVector& deviceTypeAddrVector) const;
170 
171     // Return the device vector that contains device descriptor whose AudioDeviceTypeAddr appears
172     // in the given AudioDeviceTypeAddrVector
173     AudioDeviceTypeAddrVector toTypeAddrVector() const;
174 
175     // If there are devices with the given type and the devices to add is not empty,
176     // remove all the devices with the given type and add all the devices to add.
177     void replaceDevicesByType(audio_devices_t typeToRemove, const DeviceVector &devicesToAdd);
178 
179     bool containsDeviceAmongTypes(const DeviceTypeSet& deviceTypes) const {
180         return !Intersection(mDeviceTypes, deviceTypes).empty();
181     }
182 
183     bool containsDeviceWithType(audio_devices_t deviceType) const {
184         return containsDeviceAmongTypes({deviceType});
185     }
186 
187     bool onlyContainsDevicesWithType(audio_devices_t deviceType) const {
188         return isSingleDeviceType(mDeviceTypes, deviceType);
189     }
190 
191     bool contains(const sp<DeviceDescriptor>& item) const { return indexOf(item) >= 0; }
192 
193     /**
194      * @brief containsAtLeastOne
195      * @param devices vector of devices to check against.
196      * @return true if the DeviceVector contains at list one of the devices from the given vector.
197      */
198     bool containsAtLeastOne(const DeviceVector &devices) const;
199 
200     /**
201      * @brief containsAllDevices
202      * @param devices vector of devices to check against.
203      * @return true if the DeviceVector contains all the devices from the given vector
204      */
205     bool containsAllDevices(const DeviceVector &devices) const;
206 
207     /**
208      * @brief filter the devices supported by this collection against another collection
209      * @param devices to filter against
210      * @return a filtered DeviceVector
211      */
212     DeviceVector filter(const DeviceVector &devices) const;
213 
214     /**
215      * @brief filter the devices supported by this collection before sending
216      * then to the Engine via AudioPolicyManagerObserver interface
217      * @return a filtered DeviceVector
218      */
219     DeviceVector filterForEngine() const;
220 
221     /**
222      * @brief merge two vectors. As SortedVector Implementation is buggy (it does not check the size
223      * of the destination vector, only of the source, it provides a safe implementation
224      * @param devices source device vector to merge with
225      * @return size of the merged vector.
226      */
227     ssize_t merge(const DeviceVector &devices)
228     {
229         if (isEmpty()) {
230             add(devices);
231             return size();
232         }
233         ssize_t ret = SortedVector::merge(devices);
234         refreshTypes();
235         return ret;
236     }
237 
238     /**
239      * @brief operator == DeviceVector are equals if all the DeviceDescriptor can be found (aka
240      * DeviceDescriptor with same type and address) and the vector has same size.
241      * @param right DeviceVector to compare to.
242      * @return true if right contains the same device and has the same size.
243      */
244     bool operator==(const DeviceVector &right) const
245     {
246         if (size() != right.size()) {
247             return false;
248         }
249         for (const auto &device : *this) {
250             if (right.indexOf(device) < 0) {
251                 return false;
252             }
253         }
254         return true;
255     }
256 
257     bool operator!=(const DeviceVector &right) const
258     {
259         return !operator==(right);
260     }
261 
262     /**
263      * @brief getFirstValidAddress
264      * @return the first valid address of a list of device, "" if no device with valid address
265      * found.
266      * This helper function helps maintaining compatibility with legacy where we used to have a
267      * devices mask and an address.
268      */
269     String8 getFirstValidAddress() const
270     {
271         for (const auto &device : *this) {
272             if (device->address() != "") {
273                 return String8(device->address().c_str());
274             }
275         }
276         return String8("");
277     }
278 
279     const AudioProfileVector& getSupportedProfiles() { return mSupportedProfiles; }
280 
281     // Return a string to describe the DeviceVector. The sensitive information will only be
282     // added to the string if `includeSensitiveInfo` is true.
283     std::string toString(bool includeSensitiveInfo = false) const;
284 
285     void dump(String8 *dst, const String8 &tag, int spaces = 0, bool verbose = true) const;
286 
287 protected:
288     int     do_compare(const void* lhs, const void* rhs) const;
289 private:
290     void refreshTypes();
291     void refreshAudioProfiles();
292     DeviceTypeSet mDeviceTypes;
293     AudioProfileVector mSupportedProfiles;
294 };
295 
296 } // namespace android
297