• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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>
toAudioPortInternal(T * port)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:
DeviceVector()118     DeviceVector() : SortedVector() {}
DeviceVector(const sp<DeviceDescriptor> & item)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 
types()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;
getDevicesFromType(audio_devices_t type)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     // If there are devices with the given type and the devices to add is not empty,
172     // remove all the devices with the given type and add all the devices to add.
173     void replaceDevicesByType(audio_devices_t typeToRemove, const DeviceVector &devicesToAdd);
174 
containsDeviceAmongTypes(const DeviceTypeSet & deviceTypes)175     bool containsDeviceAmongTypes(const DeviceTypeSet& deviceTypes) const {
176         return !Intersection(mDeviceTypes, deviceTypes).empty();
177     }
178 
containsDeviceWithType(audio_devices_t deviceType)179     bool containsDeviceWithType(audio_devices_t deviceType) const {
180         return containsDeviceAmongTypes({deviceType});
181     }
182 
onlyContainsDevicesWithType(audio_devices_t deviceType)183     bool onlyContainsDevicesWithType(audio_devices_t deviceType) const {
184         return isSingleDeviceType(mDeviceTypes, deviceType);
185     }
186 
contains(const sp<DeviceDescriptor> & item)187     bool contains(const sp<DeviceDescriptor>& item) const { return indexOf(item) >= 0; }
188 
189     /**
190      * @brief containsAtLeastOne
191      * @param devices vector of devices to check against.
192      * @return true if the DeviceVector contains at list one of the devices from the given vector.
193      */
194     bool containsAtLeastOne(const DeviceVector &devices) const;
195 
196     /**
197      * @brief containsAllDevices
198      * @param devices vector of devices to check against.
199      * @return true if the DeviceVector contains all the devices from the given vector
200      */
201     bool containsAllDevices(const DeviceVector &devices) const;
202 
203     /**
204      * @brief filter the devices supported by this collection against another collection
205      * @param devices to filter against
206      * @return a filtered DeviceVector
207      */
208     DeviceVector filter(const DeviceVector &devices) const;
209 
210     /**
211      * @brief filter the devices supported by this collection before sending
212      * then to the Engine via AudioPolicyManagerObserver interface
213      * @return a filtered DeviceVector
214      */
215     DeviceVector filterForEngine() const;
216 
217     /**
218      * @brief merge two vectors. As SortedVector Implementation is buggy (it does not check the size
219      * of the destination vector, only of the source, it provides a safe implementation
220      * @param devices source device vector to merge with
221      * @return size of the merged vector.
222      */
merge(const DeviceVector & devices)223     ssize_t merge(const DeviceVector &devices)
224     {
225         if (isEmpty()) {
226             add(devices);
227             return size();
228         }
229         ssize_t ret = SortedVector::merge(devices);
230         refreshTypes();
231         return ret;
232     }
233 
234     /**
235      * @brief operator == DeviceVector are equals if all the DeviceDescriptor can be found (aka
236      * DeviceDescriptor with same type and address) and the vector has same size.
237      * @param right DeviceVector to compare to.
238      * @return true if right contains the same device and has the same size.
239      */
240     bool operator==(const DeviceVector &right) const
241     {
242         if (size() != right.size()) {
243             return false;
244         }
245         for (const auto &device : *this) {
246             if (right.indexOf(device) < 0) {
247                 return false;
248             }
249         }
250         return true;
251     }
252 
253     bool operator!=(const DeviceVector &right) const
254     {
255         return !operator==(right);
256     }
257 
258     /**
259      * @brief getFirstValidAddress
260      * @return the first valid address of a list of device, "" if no device with valid address
261      * found.
262      * This helper function helps maintaining compatibility with legacy where we used to have a
263      * devices mask and an address.
264      */
getFirstValidAddress()265     String8 getFirstValidAddress() const
266     {
267         for (const auto &device : *this) {
268             if (device->address() != "") {
269                 return String8(device->address().c_str());
270             }
271         }
272         return String8("");
273     }
274 
getSupportedProfiles()275     const AudioProfileVector& getSupportedProfiles() { return mSupportedProfiles; }
276 
277     // Return a string to describe the DeviceVector. The sensitive information will only be
278     // added to the string if `includeSensitiveInfo` is true.
279     std::string toString(bool includeSensitiveInfo = false) const;
280 
281     void dump(String8 *dst, const String8 &tag, int spaces = 0, bool verbose = true) const;
282 
283 protected:
284     int     do_compare(const void* lhs, const void* rhs) const;
285 private:
286     void refreshTypes();
287     void refreshAudioProfiles();
288     DeviceTypeSet mDeviceTypes;
289     AudioProfileVector mSupportedProfiles;
290 };
291 
292 } // namespace android
293