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