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 "AudioPort.h"
20 #include "DeviceDescriptor.h"
21 #include <utils/String8.h>
22 #include <system/audio.h>
23 
24 namespace android {
25 
26 class HwModule;
27 
28 // the IOProfile class describes the capabilities of an output or input stream.
29 // It is currently assumed that all combination of listed parameters are supported.
30 // It is used by the policy manager to determine if an output or input is suitable for
31 // a given use case,  open/close it accordingly and connect/disconnect audio tracks
32 // to/from it.
33 class IOProfile : public AudioPort
34 {
35 public:
IOProfile(const String8 & name,audio_port_role_t role)36     IOProfile(const String8 &name, audio_port_role_t role)
37         : AudioPort(name, AUDIO_PORT_TYPE_MIX, role) {}
38 
39     // For a Profile aka MixPort, tag name and name are equivalent.
getTagName()40     virtual const String8 getTagName() const { return getName(); }
41 
42     // This method is used for input and direct output, and is not used for other output.
43     // If parameter updatedSamplingRate is non-NULL, it is assigned the actual sample rate.
44     // For input, flags is interpreted as audio_input_flags_t.
45     // TODO: merge audio_output_flags_t and audio_input_flags_t.
46     bool isCompatibleProfile(audio_devices_t device,
47                              String8 address,
48                              uint32_t samplingRate,
49                              uint32_t *updatedSamplingRate,
50                              audio_format_t format,
51                              audio_format_t *updatedFormat,
52                              audio_channel_mask_t channelMask,
53                              audio_channel_mask_t *updatedChannelMask,
54                              uint32_t flags) const;
55 
56     void dump(int fd);
57     void log();
58 
hasSupportedDevices()59     bool hasSupportedDevices() const { return !mSupportedDevices.isEmpty(); }
60 
supportDevice(audio_devices_t device)61     bool supportDevice(audio_devices_t device) const
62     {
63         if (audio_is_output_devices(device)) {
64             return mSupportedDevices.types() & device;
65         }
66         return mSupportedDevices.types() & (device & ~AUDIO_DEVICE_BIT_IN);
67     }
68 
supportDeviceAddress(const String8 & address)69     bool supportDeviceAddress(const String8 &address) const
70     {
71         return mSupportedDevices[0]->mAddress == address;
72     }
73 
74     // chose first device present in mSupportedDevices also part of deviceType
getSupportedDeviceForType(audio_devices_t deviceType)75     audio_devices_t getSupportedDeviceForType(audio_devices_t deviceType) const
76     {
77         for (size_t k = 0; k  < mSupportedDevices.size(); k++) {
78             audio_devices_t profileType = mSupportedDevices[k]->type();
79             if (profileType & deviceType) {
80                 return profileType;
81             }
82         }
83         return AUDIO_DEVICE_NONE;
84     }
85 
getSupportedDevicesType()86     audio_devices_t getSupportedDevicesType() const { return mSupportedDevices.types(); }
87 
clearSupportedDevices()88     void clearSupportedDevices() { mSupportedDevices.clear(); }
addSupportedDevice(const sp<DeviceDescriptor> & device)89     void addSupportedDevice(const sp<DeviceDescriptor> &device)
90     {
91         mSupportedDevices.add(device);
92     }
93 
setSupportedDevices(const DeviceVector & devices)94     void setSupportedDevices(const DeviceVector &devices)
95     {
96         mSupportedDevices = devices;
97     }
98 
getSupportedDeviceByAddress(audio_devices_t type,String8 address)99     sp<DeviceDescriptor> getSupportedDeviceByAddress(audio_devices_t type, String8 address) const
100     {
101         return mSupportedDevices.getDevice(type, address);
102     }
103 
getSupportedDevices()104     const DeviceVector &getSupportedDevices() const { return mSupportedDevices; }
105 
106 private:
107     DeviceVector mSupportedDevices; // supported devices: this input/output can be routed from/to
108 };
109 
110 class InputProfile : public IOProfile
111 {
112 public:
InputProfile(const String8 & name)113     InputProfile(const String8 &name) : IOProfile(name, AUDIO_PORT_ROLE_SINK) {}
114 };
115 
116 class OutputProfile : public IOProfile
117 {
118 public:
OutputProfile(const String8 & name)119     OutputProfile(const String8 &name) : IOProfile(name, AUDIO_PORT_ROLE_SOURCE) {}
120 };
121 
122 }; // namespace android
123