• 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:
IOProfile(const std::string & name,audio_port_role_t role)38     IOProfile(const std::string &name, audio_port_role_t role)
39         : AudioPort(name, AUDIO_PORT_TYPE_MIX, role),
40           curOpenCount(0),
41           curActiveCount(0) {}
42 
43     virtual ~IOProfile() = default;
44 
45     // For a Profile aka MixPort, tag name and name are equivalent.
getTagName()46     virtual const std::string getTagName() const { return getName(); }
47 
addAudioProfile(const sp<AudioProfile> & profile)48     virtual void addAudioProfile(const sp<AudioProfile> &profile) {
49         addAudioProfileAndSort(mProfiles, profile);
50     }
51 
asAudioPort()52     virtual sp<AudioPort> asAudioPort() const {
53         return static_cast<AudioPort*>(const_cast<IOProfile*>(this));
54     }
55 
56     // FIXME: this is needed because shared MMAP stream clients use the same audio session.
57     // Once capture clients are tracked individually and not per session this can be removed
58     // MMAP no IRQ input streams do not have the default limitation of one active client
59     // max as they can be used in shared mode by the same application.
60     // NOTE: Please consider moving to AudioPort when addressing the FIXME
61     // NOTE: this works for explicit values set in audio_policy_configuration.xml because
62     // flags are parsed before maxActiveCount by the serializer.
setFlags(uint32_t flags)63     void setFlags(uint32_t flags) override
64     {
65         AudioPort::setFlags(flags);
66         if (getRole() == AUDIO_PORT_ROLE_SINK && (flags & AUDIO_INPUT_FLAG_MMAP_NOIRQ) != 0) {
67             maxActiveCount = 0;
68         }
69     }
70 
71     /**
72      * @brief isCompatibleProfile: This method is used for input and direct output,
73      * and is not used for other output.
74      * Checks if the IO profile is compatible with specified parameters.
75      * For input, flags is interpreted as audio_input_flags_t.
76      * TODO: merge audio_output_flags_t and audio_input_flags_t.
77      *
78      * @param devices vector of devices to be checked for compatibility
79      * @param samplingRate to be checked for compatibility. Must be specified
80      * @param updatedSamplingRate if non-NULL, it is assigned the actual sample rate.
81      * @param format to be checked for compatibility. Must be specified
82      * @param updatedFormat if non-NULL, it is assigned the actual format
83      * @param channelMask to be checked for compatibility. Must be specified
84      * @param updatedChannelMask if non-NULL, it is assigned the actual channel mask
85      * @param flags to be checked for compatibility
86      * @param exactMatchRequiredForInputFlags true if exact match is required on flags
87      * @return true if the profile is compatible, false otherwise.
88      */
89     bool isCompatibleProfile(const DeviceVector &devices,
90                              uint32_t samplingRate,
91                              uint32_t *updatedSamplingRate,
92                              audio_format_t format,
93                              audio_format_t *updatedFormat,
94                              audio_channel_mask_t channelMask,
95                              audio_channel_mask_t *updatedChannelMask,
96                              // FIXME parameter type
97                              uint32_t flags,
98                              bool exactMatchRequiredForInputFlags = false) const;
99 
100     void dump(String8 *dst, int spaces) const;
101     void log();
102 
hasSupportedDevices()103     bool hasSupportedDevices() const { return !mSupportedDevices.isEmpty(); }
104 
supportsDeviceTypes(const DeviceTypeSet & deviceTypes)105     bool supportsDeviceTypes(const DeviceTypeSet& deviceTypes) const
106     {
107         const bool areOutputDevices = Intersection(deviceTypes, getAudioDeviceInAllSet()).empty();
108         const bool devicesSupported = !mSupportedDevices.getDevicesFromTypes(deviceTypes).empty();
109         return devicesSupported &&
110                (!areOutputDevices || devicesSupportEncodedFormats(deviceTypes));
111     }
112 
113     /**
114      * @brief getTag
115      * @param deviceTypes to be considered
116      * @return tagName of first matching device for the considered types, empty string otherwise.
117      */
getTag(const DeviceTypeSet & deviceTypes)118     std::string getTag(const DeviceTypeSet& deviceTypes) const
119     {
120         if (supportsDeviceTypes(deviceTypes)) {
121             return mSupportedDevices.getDevicesFromTypes(deviceTypes).itemAt(0)->getTagName();
122         }
123         return {};
124     }
125 
126     /**
127      * @brief supportsDevice
128      * @param device to be checked against
129      *        forceCheckOnAddress if true, check on type and address whatever the type, otherwise
130      *        the address enforcement is limited to "offical devices" that distinguishe on address
131      * @return true if the device is supported by type (for non bus / remote submix devices),
132      *         true if the device is supported (both type and address) for bus / remote submix
133      *         false otherwise
134      */
135     bool supportsDevice(const sp<DeviceDescriptor> &device, bool forceCheckOnAddress = false) const
136     {
137         if (!device_distinguishes_on_address(device->type()) && !forceCheckOnAddress) {
138             return supportsDeviceTypes(DeviceTypeSet({device->type()}));
139         }
140         return mSupportedDevices.contains(device);
141     }
142 
devicesSupportEncodedFormats(DeviceTypeSet deviceTypes)143     bool devicesSupportEncodedFormats(DeviceTypeSet deviceTypes) const
144     {
145         if (deviceTypes.empty()) {
146             return true; // required for getOffloadSupport() check
147         }
148         DeviceVector deviceList =
149             mSupportedDevices.getDevicesFromTypes(deviceTypes);
150         for (const auto& device : deviceList) {
151             if (device->hasCurrentEncodedFormat()) {
152                 return true;
153             }
154         }
155         return false;
156     }
157 
158     bool containsSingleDeviceSupportingEncodedFormats(const sp<DeviceDescriptor>& device) const;
159 
clearSupportedDevices()160     void clearSupportedDevices() { mSupportedDevices.clear(); }
addSupportedDevice(const sp<DeviceDescriptor> & device)161     void addSupportedDevice(const sp<DeviceDescriptor> &device)
162     {
163         mSupportedDevices.add(device);
164     }
removeSupportedDevice(const sp<DeviceDescriptor> & device)165     void removeSupportedDevice(const sp<DeviceDescriptor> &device)
166     {
167         ssize_t ret = mSupportedDevices.indexOf(device);
168         if (ret >= 0 && !mSupportedDevices.itemAt(ret)->isDynamic()) {
169             // devices equality checks only type, address, name and format
170             // Prevents from removing non dynamically added devices
171             return;
172         }
173         mSupportedDevices.remove(device);
174     }
setSupportedDevices(const DeviceVector & devices)175     void setSupportedDevices(const DeviceVector &devices)
176     {
177         mSupportedDevices = devices;
178     }
179 
getSupportedDevices()180     const DeviceVector &getSupportedDevices() const { return mSupportedDevices; }
181 
canOpenNewIo()182     bool canOpenNewIo() {
183         if (maxOpenCount == 0 || curOpenCount < maxOpenCount) {
184             return true;
185         }
186         return false;
187     }
188 
canStartNewIo()189     bool canStartNewIo() {
190         if (maxActiveCount == 0 || curActiveCount < maxActiveCount) {
191             return true;
192         }
193         return false;
194     }
195 
196     // Number of streams currently opened for this profile.
197     uint32_t     curOpenCount;
198     // Number of streams currently active for this profile. This is not the number of active clients
199     // (AudioTrack or AudioRecord) but the number of active HAL streams.
200     uint32_t     curActiveCount;
201 
202 private:
203     DeviceVector mSupportedDevices; // supported devices: this input/output can be routed from/to
204 };
205 
206 class InputProfile : public IOProfile
207 {
208 public:
InputProfile(const std::string & name)209     explicit InputProfile(const std::string &name) : IOProfile(name, AUDIO_PORT_ROLE_SINK) {}
210 };
211 
212 class OutputProfile : public IOProfile
213 {
214 public:
OutputProfile(const std::string & name)215     explicit OutputProfile(const std::string &name) : IOProfile(name, AUDIO_PORT_ROLE_SOURCE) {}
216 };
217 
218 } // namespace android
219