AudioInputDescriptor.cpp revision 53615e29c99c5e9d2ca77aaefd7bf5c770513120
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::AudioInputDescriptor"
18//#define LOG_NDEBUG 0
19
20#include "AudioInputDescriptor.h"
21#include "IOProfile.h"
22#include "AudioGain.h"
23#include "HwModule.h"
24#include <media/AudioPolicy.h>
25#include <policy.h>
26
27namespace android {
28
29AudioInputDescriptor::AudioInputDescriptor(const sp<IOProfile>& profile)
30    : mId(0), mIoHandle(0),
31      mDevice(AUDIO_DEVICE_NONE), mPolicyMix(NULL), mPatchHandle(0), mRefCount(0),
32      mInputSource(AUDIO_SOURCE_DEFAULT), mProfile(profile), mIsSoundTrigger(false)
33{
34    if (profile != NULL) {
35        mSamplingRate = profile->pickSamplingRate();
36        mFormat = profile->pickFormat();
37        mChannelMask = profile->pickChannelMask();
38        if (profile->mGains.size() > 0) {
39            profile->mGains[0]->getDefaultConfig(&mGain);
40        }
41    }
42}
43
44void AudioInputDescriptor::setIoHandle(audio_io_handle_t ioHandle)
45{
46    mId = AudioPort::getNextUniqueId();
47    mIoHandle = ioHandle;
48}
49
50audio_module_handle_t AudioInputDescriptor::getModuleHandle() const
51{
52    return mProfile->getModuleHandle();
53}
54
55void AudioInputDescriptor::toAudioPortConfig(struct audio_port_config *dstConfig,
56                                             const struct audio_port_config *srcConfig) const
57{
58    ALOG_ASSERT(mProfile != 0,
59                "toAudioPortConfig() called on input with null profile %d", mIoHandle);
60    dstConfig->config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE|AUDIO_PORT_CONFIG_CHANNEL_MASK|
61                            AUDIO_PORT_CONFIG_FORMAT|AUDIO_PORT_CONFIG_GAIN;
62    if (srcConfig != NULL) {
63        dstConfig->config_mask |= srcConfig->config_mask;
64    }
65
66    AudioPortConfig::toAudioPortConfig(dstConfig, srcConfig);
67
68    dstConfig->id = mId;
69    dstConfig->role = AUDIO_PORT_ROLE_SINK;
70    dstConfig->type = AUDIO_PORT_TYPE_MIX;
71    dstConfig->ext.mix.hw_module = mProfile->mModule->mHandle;
72    dstConfig->ext.mix.handle = mIoHandle;
73    dstConfig->ext.mix.usecase.source = mInputSource;
74}
75
76void AudioInputDescriptor::toAudioPort(struct audio_port *port) const
77{
78    ALOG_ASSERT(mProfile != 0, "toAudioPort() called on input with null profile %d", mIoHandle);
79
80    mProfile->toAudioPort(port);
81    port->id = mId;
82    toAudioPortConfig(&port->active_config);
83    port->ext.mix.hw_module = mProfile->mModule->mHandle;
84    port->ext.mix.handle = mIoHandle;
85    port->ext.mix.latency_class = AUDIO_LATENCY_NORMAL;
86}
87
88status_t AudioInputDescriptor::dump(int fd)
89{
90    const size_t SIZE = 256;
91    char buffer[SIZE];
92    String8 result;
93
94    snprintf(buffer, SIZE, " ID: %d\n", mId);
95    result.append(buffer);
96    snprintf(buffer, SIZE, " Sampling rate: %d\n", mSamplingRate);
97    result.append(buffer);
98    snprintf(buffer, SIZE, " Format: %d\n", mFormat);
99    result.append(buffer);
100    snprintf(buffer, SIZE, " Channels: %08x\n", mChannelMask);
101    result.append(buffer);
102    snprintf(buffer, SIZE, " Devices %08x\n", mDevice);
103    result.append(buffer);
104    snprintf(buffer, SIZE, " Ref Count %d\n", mRefCount);
105    result.append(buffer);
106    snprintf(buffer, SIZE, " Open Ref Count %d\n", mOpenRefCount);
107    result.append(buffer);
108
109    write(fd, result.string(), result.size());
110
111    return NO_ERROR;
112}
113
114bool AudioInputCollection::isSourceActive(audio_source_t source) const
115{
116    for (size_t i = 0; i < size(); i++) {
117        const sp<AudioInputDescriptor>  inputDescriptor = valueAt(i);
118        if (inputDescriptor->mRefCount == 0) {
119            continue;
120        }
121        if (inputDescriptor->mInputSource == (int)source) {
122            return true;
123        }
124    }
125    return false;
126}
127
128sp<AudioInputDescriptor> AudioInputCollection::getInputFromId(audio_port_handle_t id) const
129{
130    sp<AudioInputDescriptor> inputDesc = NULL;
131    for (size_t i = 0; i < size(); i++) {
132        inputDesc = valueAt(i);
133        if (inputDesc->mId == id) {
134            break;
135        }
136    }
137    return inputDesc;
138}
139
140uint32_t AudioInputCollection::activeInputsCount() const
141{
142    uint32_t count = 0;
143    for (size_t i = 0; i < size(); i++) {
144        const sp<AudioInputDescriptor>  desc = valueAt(i);
145        if (desc->mRefCount > 0) {
146            count++;
147        }
148    }
149    return count;
150}
151
152audio_io_handle_t AudioInputCollection::getActiveInput(bool ignoreVirtualInputs)
153{
154    for (size_t i = 0; i < size(); i++) {
155        const sp<AudioInputDescriptor>  input_descriptor = valueAt(i);
156        if ((input_descriptor->mRefCount > 0)
157                && (!ignoreVirtualInputs || !is_virtual_input_device(input_descriptor->mDevice))) {
158            return keyAt(i);
159        }
160    }
161    return 0;
162}
163
164audio_devices_t AudioInputCollection::getSupportedDevices(audio_io_handle_t handle) const
165{
166    sp<AudioInputDescriptor> inputDesc = valueFor(handle);
167    audio_devices_t devices = inputDesc->mProfile->mSupportedDevices.types();
168    return devices;
169}
170
171status_t AudioInputCollection::dump(int fd) const
172{
173    const size_t SIZE = 256;
174    char buffer[SIZE];
175
176    snprintf(buffer, SIZE, "\nInputs dump:\n");
177    write(fd, buffer, strlen(buffer));
178    for (size_t i = 0; i < size(); i++) {
179        snprintf(buffer, SIZE, "- Input %d dump:\n", keyAt(i));
180        write(fd, buffer, strlen(buffer));
181        valueAt(i)->dump(fd);
182    }
183
184    return NO_ERROR;
185}
186
187}; //namespace android
188