IOProfile.cpp revision a8ecc2c72ca26389bd6b0162181d60aaeaca8149
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
24namespace android {
25
26IOProfile::IOProfile(const String8 &name, audio_port_role_t role)
27    : AudioPort(name, AUDIO_PORT_TYPE_MIX, role)
28{
29}
30
31IOProfile::~IOProfile()
32{
33}
34
35// checks if the IO profile is compatible with specified parameters.
36// Sampling rate, format and channel mask must be specified in order to
37// get a valid a match
38bool IOProfile::isCompatibleProfile(audio_devices_t device,
39                                    String8 address,
40                                    uint32_t samplingRate,
41                                    uint32_t *updatedSamplingRate,
42                                    audio_format_t format,
43                                    audio_format_t *updatedFormat,
44                                    audio_channel_mask_t channelMask,
45                                    audio_channel_mask_t *updatedChannelMask,
46                                    uint32_t flags) const
47{
48    const bool isPlaybackThread =
49            getType() == AUDIO_PORT_TYPE_MIX && getRole() == AUDIO_PORT_ROLE_SOURCE;
50    const bool isRecordThread =
51            getType() == AUDIO_PORT_TYPE_MIX && getRole() == AUDIO_PORT_ROLE_SINK;
52    ALOG_ASSERT(isPlaybackThread != isRecordThread);
53
54
55    if (device != AUDIO_DEVICE_NONE) {
56        // just check types if multiple devices are selected
57        if (popcount(device & ~AUDIO_DEVICE_BIT_IN) > 1) {
58            if ((mSupportedDevices.types() & device) != device) {
59                return false;
60            }
61        } else if (mSupportedDevices.getDevice(device, address) == 0) {
62            return false;
63        }
64    }
65
66    if (samplingRate == 0) {
67         return false;
68    }
69    uint32_t myUpdatedSamplingRate = samplingRate;
70    if (isPlaybackThread && checkExactSamplingRate(samplingRate) != NO_ERROR) {
71         return false;
72    }
73    if (isRecordThread && checkCompatibleSamplingRate(samplingRate, &myUpdatedSamplingRate) !=
74            NO_ERROR) {
75         return false;
76    }
77
78    if (!audio_is_valid_format(format)) {
79        return false;
80    }
81    if (isPlaybackThread && checkExactFormat(format) != NO_ERROR) {
82        return false;
83    }
84    audio_format_t myUpdatedFormat = format;
85    if (isRecordThread && checkCompatibleFormat(format, &myUpdatedFormat) != NO_ERROR) {
86        return false;
87    }
88
89    if (isPlaybackThread && (!audio_is_output_channel(channelMask) ||
90            checkExactChannelMask(channelMask) != NO_ERROR)) {
91        return false;
92    }
93    audio_channel_mask_t myUpdatedChannelMask = channelMask;
94    if (isRecordThread && (!audio_is_input_channel(channelMask) ||
95            checkCompatibleChannelMask(channelMask, &myUpdatedChannelMask) != NO_ERROR)) {
96        return false;
97    }
98
99    if (isPlaybackThread && (getFlags() & flags) != flags) {
100        return false;
101    }
102    // The only input flag that is allowed to be different is the fast flag.
103    // An existing fast stream is compatible with a normal track request.
104    // An existing normal stream is compatible with a fast track request,
105    // but the fast request will be denied by AudioFlinger and converted to normal track.
106    if (isRecordThread && ((getFlags() ^ flags) &
107            ~AUDIO_INPUT_FLAG_FAST)) {
108        return false;
109    }
110
111    if (updatedSamplingRate != NULL) {
112        *updatedSamplingRate = myUpdatedSamplingRate;
113    }
114    if (updatedFormat != NULL) {
115        *updatedFormat = myUpdatedFormat;
116    }
117    if (updatedChannelMask != NULL) {
118        *updatedChannelMask = myUpdatedChannelMask;
119    }
120    return true;
121}
122
123void IOProfile::dump(int fd)
124{
125    const size_t SIZE = 256;
126    char buffer[SIZE];
127    String8 result;
128
129    AudioPort::dump(fd, 4);
130
131    snprintf(buffer, SIZE, "    - flags: 0x%04x\n", getFlags());
132    result.append(buffer);
133    snprintf(buffer, SIZE, "    - devices:\n");
134    result.append(buffer);
135    write(fd, result.string(), result.size());
136    for (size_t i = 0; i < mSupportedDevices.size(); i++) {
137        mSupportedDevices[i]->dump(fd, 6, i);
138    }
139}
140
141void IOProfile::log()
142{
143    const size_t SIZE = 256;
144    char buffer[SIZE];
145    String8 result;
146
147    ALOGV("    - sampling rates: ");
148    for (size_t i = 0; i < mSamplingRates.size(); i++) {
149        ALOGV("  %d", mSamplingRates[i]);
150    }
151
152    ALOGV("    - channel masks: ");
153    for (size_t i = 0; i < mChannelMasks.size(); i++) {
154        ALOGV("  0x%04x", mChannelMasks[i]);
155    }
156
157    ALOGV("    - formats: ");
158    for (size_t i = 0; i < mFormats.size(); i++) {
159        ALOGV("  0x%08x", mFormats[i]);
160    }
161
162    ALOGV("    - devices: 0x%04x\n", mSupportedDevices.types());
163    ALOGV("    - flags: 0x%04x\n", getFlags());
164}
165
166}; // namespace android
167