• 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 "DeviceDescriptor.h"
20 #include "PolicyAudioPort.h"
21 #include "policy.h"
22 #include <media/AudioContainers.h>
23 #include <utils/String8.h>
24 #include <system/audio.h>
25 
26 namespace android {
27 
28 class HwModule;
29 
30 // the IOProfile class describes the capabilities of an output or input stream.
31 // It is currently assumed that all combination of listed parameters are supported.
32 // It is used by the policy manager to determine if an output or input is suitable for
33 // a given use case,  open/close it accordingly and connect/disconnect audio tracks
34 // to/from it.
35 class IOProfile : public AudioPort, public PolicyAudioPort
36 {
37 public:
38     IOProfile(const std::string &name, audio_port_role_t role);
39 
40     virtual ~IOProfile() = default;
41 
42     // For a Profile aka MixPort, tag name and name are equivalent.
getTagName()43     virtual const std::string getTagName() const { return getName(); }
44 
addAudioProfile(const sp<AudioProfile> & profile)45     virtual void addAudioProfile(const sp<AudioProfile> &profile) {
46         addAudioProfileAndSort(mProfiles, profile);
47     }
48 
asAudioPort()49     virtual sp<AudioPort> asAudioPort() const {
50         return static_cast<AudioPort*>(const_cast<IOProfile*>(this));
51     }
52 
53     // FIXME: this is needed because shared MMAP stream clients use the same audio session.
54     // Once capture clients are tracked individually and not per session this can be removed
55     // MMAP no IRQ input streams do not have the default limitation of one active client
56     // max as they can be used in shared mode by the same application.
57     // NOTE: Please consider moving to AudioPort when addressing the FIXME
58     // NOTE: this works for explicit values set in audio_policy_configuration.xml because
59     // flags are parsed before maxActiveCount by the serializer.
setFlags(uint32_t flags)60     void setFlags(uint32_t flags) override
61     {
62         AudioPort::setFlags(flags);
63         if (getRole() == AUDIO_PORT_ROLE_SINK && (flags & AUDIO_INPUT_FLAG_MMAP_NOIRQ) != 0) {
64             maxActiveCount = 0;
65         }
66         refreshMixerBehaviors();
67     }
68 
getMixerBehaviors()69     const MixerBehaviorSet& getMixerBehaviors() const {
70         return mMixerBehaviors;
71     }
72 
73     enum CompatibilityScore{
74         NO_MATCH = 0,
75         PARTIAL_MATCH = 1,
76         EXACT_MATCH = 2
77     };
78 
79     /**
80      * @brief compatibilityScore: This method is used for input and direct output,
81      * and is not used for other output.
82      * Return the compatibility score to measure how much the IO profile is compatible
83      * with specified parameters.
84      * For input, flags is interpreted as audio_input_flags_t.
85      * TODO: merge audio_output_flags_t and audio_input_flags_t.
86      *
87      * @param devices vector of devices to be checked for compatibility
88      * @param samplingRate to be checked for compatibility. Must be specified
89      * @param updatedSamplingRate if non-NULL, it is assigned the actual sample rate.
90      * @param format to be checked for compatibility. Must be specified
91      * @param updatedFormat if non-NULL, it is assigned the actual format
92      * @param channelMask to be checked for compatibility. Must be specified
93      * @param updatedChannelMask if non-NULL, it is assigned the actual channel mask
94      * @param flags to be checked for compatibility
95      * @param exactMatchRequiredForInputFlags true if exact match is required on flags
96      * @return how the IO profile is compatible with the given parameters.
97      */
98     CompatibilityScore getCompatibilityScore(const DeviceVector &devices,
99                                              uint32_t samplingRate,
100                                              uint32_t *updatedSamplingRate,
101                                              audio_format_t format,
102                                              audio_format_t *updatedFormat,
103                                              audio_channel_mask_t channelMask,
104                                              audio_channel_mask_t *updatedChannelMask,
105                                              // FIXME parameter type
106                                              uint32_t flags,
107                                              bool exactMatchRequiredForInputFlags = false) const;
108 
109     /**
110      * @brief areAllDevicesSupported: Checks if the given devices are supported by the IO profile.
111      *
112      * @param devices vector of devices to be checked for compatibility
113      * @return true if all devices are supported, false otherwise.
114      */
115     bool areAllDevicesSupported(const DeviceVector &devices) const;
116 
117     /**
118      * @brief isCompatibleProfileForFlags: Checks if the IO profile is compatible with
119      * specified flags.
120      *
121      * @param flags to be checked for compatibility
122      * @param exactMatchRequiredForInputFlags true if exact match is required on flags
123      * @return true if the profile is compatible, false otherwise.
124      */
125     bool isCompatibleProfileForFlags(uint32_t flags,
126                                      bool exactMatchRequiredForInputFlags = false) const;
127 
128     void dump(String8 *dst, int spaces) const;
129     void log();
130 
hasSupportedDevices()131     bool hasSupportedDevices() const { return !mSupportedDevices.isEmpty(); }
132 
supportsDeviceTypes(const DeviceTypeSet & deviceTypes)133     bool supportsDeviceTypes(const DeviceTypeSet& deviceTypes) const
134     {
135         const bool areOutputDevices = Intersection(deviceTypes, getAudioDeviceInAllSet()).empty();
136         const bool devicesSupported = !mSupportedDevices.getDevicesFromTypes(deviceTypes).empty();
137         return devicesSupported &&
138                (!areOutputDevices || devicesSupportEncodedFormats(deviceTypes));
139     }
140 
141     /**
142      * @brief getTag
143      * @param deviceTypes to be considered
144      * @return tagName of first matching device for the considered types, empty string otherwise.
145      */
getTag(const DeviceTypeSet & deviceTypes)146     std::string getTag(const DeviceTypeSet& deviceTypes) const
147     {
148         if (supportsDeviceTypes(deviceTypes)) {
149             return mSupportedDevices.getDevicesFromTypes(deviceTypes).itemAt(0)->getTagName();
150         }
151         return {};
152     }
153 
154     /**
155      * @brief supportsDevice
156      * @param device to be checked against
157      *        forceCheckOnAddress if true, check on type and address whatever the type, otherwise
158      *        the address enforcement is limited to "offical devices" that distinguishe on address
159      * @return true if the device is supported by type (for non bus / remote submix devices),
160      *         true if the device is supported (both type and address) for bus / remote submix
161      *         false otherwise
162      */
163     bool supportsDevice(const sp<DeviceDescriptor> &device, bool forceCheckOnAddress = false) const
164     {
165         if (!device_distinguishes_on_address(device->type()) && !forceCheckOnAddress) {
166             return supportsDeviceTypes(DeviceTypeSet({device->type()}));
167         }
168         return mSupportedDevices.contains(device);
169     }
170 
devicesSupportEncodedFormats(DeviceTypeSet deviceTypes)171     bool devicesSupportEncodedFormats(DeviceTypeSet deviceTypes) const
172     {
173         if (deviceTypes.empty()) {
174             return true; // required for getOffloadSupport() check
175         }
176         DeviceVector deviceList =
177             mSupportedDevices.getDevicesFromTypes(deviceTypes);
178         for (const auto& device : deviceList) {
179             if (device->hasCurrentEncodedFormat()) {
180                 return true;
181             }
182         }
183         return false;
184     }
185 
186     bool containsSingleDeviceSupportingEncodedFormats(const sp<DeviceDescriptor>& device) const;
187 
clearSupportedDevices()188     void clearSupportedDevices() { mSupportedDevices.clear(); }
addSupportedDevice(const sp<DeviceDescriptor> & device)189     void addSupportedDevice(const sp<DeviceDescriptor> &device)
190     {
191         mSupportedDevices.add(device);
192     }
removeSupportedDevice(const sp<DeviceDescriptor> & device)193     void removeSupportedDevice(const sp<DeviceDescriptor> &device)
194     {
195         ssize_t ret = mSupportedDevices.indexOf(device);
196         if (ret >= 0 && !mSupportedDevices.itemAt(ret)->isDynamic()) {
197             // devices equality checks only type, address, name and format
198             // Prevents from removing non dynamically added devices
199             return;
200         }
201         mSupportedDevices.remove(device);
202     }
setSupportedDevices(const DeviceVector & devices)203     void setSupportedDevices(const DeviceVector &devices)
204     {
205         mSupportedDevices = devices;
206     }
207 
getSupportedDevices()208     const DeviceVector &getSupportedDevices() const { return mSupportedDevices; }
209 
canOpenNewIo()210     bool canOpenNewIo() {
211         if (maxOpenCount == 0 || curOpenCount < maxOpenCount) {
212             return true;
213         }
214         return false;
215     }
216 
canStartNewIo()217     bool canStartNewIo() {
218         if (maxActiveCount == 0 || curActiveCount < maxActiveCount) {
219             return true;
220         }
221         return false;
222     }
223 
224     void toSupportedMixerAttributes(std::vector<audio_mixer_attributes_t>* mixerAttributes) const;
225 
226     status_t readFromParcelable(const media::AudioPortFw& parcelable);
227 
228     void importAudioPort(const audio_port_v7& port) override;
229 
230     // Number of streams currently opened for this profile.
231     uint32_t     curOpenCount;
232     // Number of streams currently active for this profile. This is not the number of active clients
233     // (AudioTrack or AudioRecord) but the number of active HAL streams.
234     uint32_t     curActiveCount;
235 
236 private:
237     void refreshMixerBehaviors();
238 
239     DeviceVector mSupportedDevices; // supported devices: this input/output can be routed from/to
240 
241     MixerBehaviorSet mMixerBehaviors;
242 };
243 
244 class InputProfile : public IOProfile
245 {
246 public:
InputProfile(const std::string & name)247     explicit InputProfile(const std::string &name) : IOProfile(name, AUDIO_PORT_ROLE_SINK) {}
248 };
249 
250 class OutputProfile : public IOProfile
251 {
252 public:
OutputProfile(const std::string & name)253     explicit OutputProfile(const std::string &name) : IOProfile(name, AUDIO_PORT_ROLE_SOURCE) {}
254 };
255 
256 } // namespace android
257