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 #define LOG_TAG "APM::IOProfile"
18 //#define LOG_NDEBUG 0
19 
20 #include "IOProfile.h"
21 #include "HwModule.h"
22 #include "AudioGain.h"
23 #include "TypeConverter.h"
24 
25 namespace android {
26 
27 // checks if the IO profile is compatible with specified parameters.
28 // Sampling rate, format and channel mask must be specified in order to
29 // get a valid a match
isCompatibleProfile(audio_devices_t device,String8 address,uint32_t samplingRate,uint32_t * updatedSamplingRate,audio_format_t format,audio_format_t * updatedFormat,audio_channel_mask_t channelMask,audio_channel_mask_t * updatedChannelMask,uint32_t flags) const30 bool IOProfile::isCompatibleProfile(audio_devices_t device,
31                                     String8 address,
32                                     uint32_t samplingRate,
33                                     uint32_t *updatedSamplingRate,
34                                     audio_format_t format,
35                                     audio_format_t *updatedFormat,
36                                     audio_channel_mask_t channelMask,
37                                     audio_channel_mask_t *updatedChannelMask,
38                                     uint32_t flags) const
39 {
40     const bool isPlaybackThread =
41             getType() == AUDIO_PORT_TYPE_MIX && getRole() == AUDIO_PORT_ROLE_SOURCE;
42     const bool isRecordThread =
43             getType() == AUDIO_PORT_TYPE_MIX && getRole() == AUDIO_PORT_ROLE_SINK;
44     ALOG_ASSERT(isPlaybackThread != isRecordThread);
45 
46 
47     if (device != AUDIO_DEVICE_NONE) {
48         // just check types if multiple devices are selected
49         if (popcount(device & ~AUDIO_DEVICE_BIT_IN) > 1) {
50             if ((mSupportedDevices.types() & device) != device) {
51                 return false;
52             }
53         } else if (mSupportedDevices.getDevice(device, address) == 0) {
54             return false;
55         }
56     }
57 
58     if (!audio_is_valid_format(format) ||
59             (isPlaybackThread && (samplingRate == 0 || !audio_is_output_channel(channelMask))) ||
60             (isRecordThread && (!audio_is_input_channel(channelMask)))) {
61          return false;
62     }
63 
64     audio_format_t myUpdatedFormat = format;
65     audio_channel_mask_t myUpdatedChannelMask = channelMask;
66     uint32_t myUpdatedSamplingRate = samplingRate;
67     if (isRecordThread)
68     {
69         if (checkCompatibleAudioProfile(
70                 myUpdatedSamplingRate, myUpdatedChannelMask, myUpdatedFormat) != NO_ERROR) {
71             return false;
72         }
73     } else {
74         if (checkExactAudioProfile(samplingRate, channelMask, format) != NO_ERROR) {
75             return false;
76         }
77     }
78 
79     if (isPlaybackThread && (getFlags() & flags) != flags) {
80         return false;
81     }
82     // The only input flag that is allowed to be different is the fast flag.
83     // An existing fast stream is compatible with a normal track request.
84     // An existing normal stream is compatible with a fast track request,
85     // but the fast request will be denied by AudioFlinger and converted to normal track.
86     if (isRecordThread && ((getFlags() ^ flags) &
87             ~AUDIO_INPUT_FLAG_FAST)) {
88         return false;
89     }
90 
91     if (updatedSamplingRate != NULL) {
92         *updatedSamplingRate = myUpdatedSamplingRate;
93     }
94     if (updatedFormat != NULL) {
95         *updatedFormat = myUpdatedFormat;
96     }
97     if (updatedChannelMask != NULL) {
98         *updatedChannelMask = myUpdatedChannelMask;
99     }
100     return true;
101 }
102 
dump(int fd)103 void IOProfile::dump(int fd)
104 {
105     const size_t SIZE = 256;
106     char buffer[SIZE];
107     String8 result;
108 
109     AudioPort::dump(fd, 4);
110 
111     snprintf(buffer, SIZE, "    - flags: 0x%04x\n", getFlags());
112     result.append(buffer);
113     write(fd, result.string(), result.size());
114     mSupportedDevices.dump(fd, String8("Supported"), 4, false);
115 }
116 
log()117 void IOProfile::log()
118 {
119     // @TODO: forward log to AudioPort
120 }
121 
122 }; // namespace android
123