• 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 #define LOG_TAG "APM::IOProfile"
18 //#define LOG_NDEBUG 0
19 
20 #include <system/audio.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 
containsSingleDeviceSupportingEncodedFormats(const sp<DeviceDescriptor> & device) const108 bool IOProfile::containsSingleDeviceSupportingEncodedFormats(
109         const sp<DeviceDescriptor>& device) const {
110     if (device == nullptr) {
111         return false;
112     }
113     DeviceVector deviceList = mSupportedDevices.getDevicesFromType(device->type());
114     return std::count_if(deviceList.begin(), deviceList.end(),
115             [&device](sp<DeviceDescriptor> deviceDesc) {
116                 return device == deviceDesc && deviceDesc->hasCurrentEncodedFormat(); }) == 1;
117 }
118 
dump(String8 * dst,int spaces) const119 void IOProfile::dump(String8 *dst, int spaces) const
120 {
121     String8 extraInfo;
122     extraInfo.appendFormat("0x%04x", getFlags());
123     std::string flagsLiteral =
124             getRole() == AUDIO_PORT_ROLE_SINK ?
125             toString(static_cast<audio_input_flags_t>(getFlags())) :
126             getRole() == AUDIO_PORT_ROLE_SOURCE ?
127             toString(static_cast<audio_output_flags_t>(getFlags())) : "";
128     if (!flagsLiteral.empty()) {
129         extraInfo.appendFormat(" (%s)", flagsLiteral.c_str());
130     }
131 
132     std::string portStr;
133     AudioPort::dump(&portStr, spaces, extraInfo.c_str());
134     dst->append(portStr.c_str());
135 
136     mSupportedDevices.dump(dst, String8("- Supported"), spaces - 2, false);
137     dst->appendFormat("%*s- maxOpenCount: %u; curOpenCount: %u\n",
138             spaces - 2, "", maxOpenCount, curOpenCount);
139     dst->appendFormat("%*s- maxActiveCount: %u; curActiveCount: %u\n",
140             spaces - 2, "", maxActiveCount, curActiveCount);
141     dst->appendFormat("%*s- recommendedMuteDurationMs: %u ms\n",
142             spaces - 2, "", recommendedMuteDurationMs);
143 }
144 
log()145 void IOProfile::log()
146 {
147     // @TODO: forward log to AudioPort
148 }
149 
150 } // namespace android
151