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 <system/audio-base.h>
21 #include "IOProfile.h"
22 #include "HwModule.h"
23 #include "AudioGain.h"
24 #include "TypeConverter.h"
25 
26 namespace android {
27 
isCompatibleProfile(const DeviceVector & devices,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,bool exactMatchRequiredForInputFlags) const28 bool IOProfile::isCompatibleProfile(const DeviceVector &devices,
29                                     uint32_t samplingRate,
30                                     uint32_t *updatedSamplingRate,
31                                     audio_format_t format,
32                                     audio_format_t *updatedFormat,
33                                     audio_channel_mask_t channelMask,
34                                     audio_channel_mask_t *updatedChannelMask,
35                                     // FIXME type punning here
36                                     uint32_t flags,
37                                     bool exactMatchRequiredForInputFlags) const
38 {
39     const bool isPlaybackThread =
40             getType() == AUDIO_PORT_TYPE_MIX && getRole() == AUDIO_PORT_ROLE_SOURCE;
41     const bool isRecordThread =
42             getType() == AUDIO_PORT_TYPE_MIX && getRole() == AUDIO_PORT_ROLE_SINK;
43     ALOG_ASSERT(isPlaybackThread != isRecordThread);
44 
45     if (!devices.isEmpty()) {
46         if (!mSupportedDevices.containsAllDevices(devices)) {
47             return false;
48         }
49     }
50 
51     if (!audio_is_valid_format(format) ||
52             (isPlaybackThread && (samplingRate == 0 || !audio_is_output_channel(channelMask))) ||
53             (isRecordThread && (!audio_is_input_channel(channelMask)))) {
54          return false;
55     }
56 
57     audio_format_t myUpdatedFormat = format;
58     audio_channel_mask_t myUpdatedChannelMask = channelMask;
59     uint32_t myUpdatedSamplingRate = samplingRate;
60     const struct audio_port_config config = {
61         .config_mask = AUDIO_PORT_CONFIG_ALL & ~AUDIO_PORT_CONFIG_GAIN,
62         .sample_rate = samplingRate,
63         .channel_mask = channelMask,
64         .format = format,
65     };
66     if (isRecordThread)
67     {
68         if ((flags & AUDIO_INPUT_FLAG_MMAP_NOIRQ) != 0) {
69             if (checkExactAudioProfile(&config) != NO_ERROR) {
70                 return false;
71             }
72         } else if (checkCompatibleAudioProfile(
73                 myUpdatedSamplingRate, myUpdatedChannelMask, myUpdatedFormat) != NO_ERROR) {
74             return false;
75         }
76     } else {
77         if (checkExactAudioProfile(&config) != NO_ERROR) {
78             return false;
79         }
80     }
81 
82     if (isPlaybackThread && (getFlags() & flags) != flags) {
83         return false;
84     }
85     // The only input flag that is allowed to be different is the fast flag.
86     // An existing fast stream is compatible with a normal track request.
87     // An existing normal stream is compatible with a fast track request,
88     // but the fast request will be denied by AudioFlinger and converted to normal track.
89     if (isRecordThread && ((getFlags() ^ flags) &
90             ~(exactMatchRequiredForInputFlags ? AUDIO_INPUT_FLAG_NONE : AUDIO_INPUT_FLAG_FAST))) {
91         return false;
92     }
93 
94     if (updatedSamplingRate != NULL) {
95         *updatedSamplingRate = myUpdatedSamplingRate;
96     }
97     if (updatedFormat != NULL) {
98         *updatedFormat = myUpdatedFormat;
99     }
100     if (updatedChannelMask != NULL) {
101         *updatedChannelMask = myUpdatedChannelMask;
102     }
103     return true;
104 }
105 
dump(String8 * dst) const106 void IOProfile::dump(String8 *dst) const
107 {
108     AudioPort::dump(dst, 4);
109 
110     dst->appendFormat("    - flags: 0x%04x", getFlags());
111     std::string flagsLiteral;
112     if (getRole() == AUDIO_PORT_ROLE_SINK) {
113         InputFlagConverter::maskToString(getFlags(), flagsLiteral);
114     } else if (getRole() == AUDIO_PORT_ROLE_SOURCE) {
115         OutputFlagConverter::maskToString(getFlags(), flagsLiteral);
116     }
117     if (!flagsLiteral.empty()) {
118         dst->appendFormat(" (%s)", flagsLiteral.c_str());
119     }
120     dst->append("\n");
121     mSupportedDevices.dump(dst, String8("Supported"), 4, false);
122     dst->appendFormat("\n    - maxOpenCount: %u - curOpenCount: %u\n",
123              maxOpenCount, curOpenCount);
124     dst->appendFormat("    - maxActiveCount: %u - curActiveCount: %u\n",
125              maxActiveCount, curActiveCount);
126 }
127 
log()128 void IOProfile::log()
129 {
130     // @TODO: forward log to AudioPort
131 }
132 
133 } // namespace android
134