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