AudioInputDescriptor.cpp revision 64265b2fb8f5be63b6c2ad4fcbec9acf74705bc4
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    : mIoHandle(0),
31      mDevice(AUDIO_DEVICE_NONE), mPolicyMix(NULL), mPatchHandle(0), mRefCount(0),
32      mInputSource(AUDIO_SOURCE_DEFAULT), mProfile(profile), mIsSoundTrigger(false), mId(0)
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    if (mProfile == 0) {
53        return 0;
54    }
55    return mProfile->getModuleHandle();
56}
57
58audio_port_handle_t AudioInputDescriptor::getId() const
59{
60    return mId;
61}
62
63void AudioInputDescriptor::toAudioPortConfig(struct audio_port_config *dstConfig,
64                                             const struct audio_port_config *srcConfig) const
65{
66    ALOG_ASSERT(mProfile != 0,
67                "toAudioPortConfig() called on input with null profile %d", mIoHandle);
68    dstConfig->config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE|AUDIO_PORT_CONFIG_CHANNEL_MASK|
69                            AUDIO_PORT_CONFIG_FORMAT|AUDIO_PORT_CONFIG_GAIN;
70    if (srcConfig != NULL) {
71        dstConfig->config_mask |= srcConfig->config_mask;
72    }
73
74    AudioPortConfig::toAudioPortConfig(dstConfig, srcConfig);
75
76    dstConfig->id = mId;
77    dstConfig->role = AUDIO_PORT_ROLE_SINK;
78    dstConfig->type = AUDIO_PORT_TYPE_MIX;
79    dstConfig->ext.mix.hw_module = getModuleHandle();
80    dstConfig->ext.mix.handle = mIoHandle;
81    dstConfig->ext.mix.usecase.source = mInputSource;
82}
83
84void AudioInputDescriptor::toAudioPort(struct audio_port *port) const
85{
86    ALOG_ASSERT(mProfile != 0, "toAudioPort() called on input with null profile %d", mIoHandle);
87
88    mProfile->toAudioPort(port);
89    port->id = mId;
90    toAudioPortConfig(&port->active_config);
91    port->ext.mix.hw_module = getModuleHandle();
92    port->ext.mix.handle = mIoHandle;
93    port->ext.mix.latency_class = AUDIO_LATENCY_NORMAL;
94}
95
96void AudioInputDescriptor::setPreemptedSessions(const SortedVector<audio_session_t>& sessions)
97{
98    mPreemptedSessions = sessions;
99}
100
101SortedVector<audio_session_t> AudioInputDescriptor::getPreemptedSessions() const
102{
103    return mPreemptedSessions;
104}
105
106bool AudioInputDescriptor::hasPreemptedSession(audio_session_t session) const
107{
108    return (mPreemptedSessions.indexOf(session) >= 0);
109}
110
111void AudioInputDescriptor::clearPreemptedSessions()
112{
113    mPreemptedSessions.clear();
114}
115
116status_t AudioInputDescriptor::dump(int fd)
117{
118    const size_t SIZE = 256;
119    char buffer[SIZE];
120    String8 result;
121
122    snprintf(buffer, SIZE, " ID: %d\n", getId());
123    result.append(buffer);
124    snprintf(buffer, SIZE, " Sampling rate: %d\n", mSamplingRate);
125    result.append(buffer);
126    snprintf(buffer, SIZE, " Format: %d\n", mFormat);
127    result.append(buffer);
128    snprintf(buffer, SIZE, " Channels: %08x\n", mChannelMask);
129    result.append(buffer);
130    snprintf(buffer, SIZE, " Devices %08x\n", mDevice);
131    result.append(buffer);
132    snprintf(buffer, SIZE, " Ref Count %d\n", mRefCount);
133    result.append(buffer);
134    snprintf(buffer, SIZE, " Open Ref Count %d\n", mOpenRefCount);
135    result.append(buffer);
136
137    write(fd, result.string(), result.size());
138
139    return NO_ERROR;
140}
141
142bool AudioInputCollection::isSourceActive(audio_source_t source) const
143{
144    for (size_t i = 0; i < size(); i++) {
145        const sp<AudioInputDescriptor>  inputDescriptor = valueAt(i);
146        if (inputDescriptor->mRefCount == 0) {
147            continue;
148        }
149        if (inputDescriptor->mInputSource == (int)source) {
150            return true;
151        }
152    }
153    return false;
154}
155
156sp<AudioInputDescriptor> AudioInputCollection::getInputFromId(audio_port_handle_t id) const
157{
158    sp<AudioInputDescriptor> inputDesc = NULL;
159    for (size_t i = 0; i < size(); i++) {
160        inputDesc = valueAt(i);
161        if (inputDesc->getId() == id) {
162            break;
163        }
164    }
165    return inputDesc;
166}
167
168uint32_t AudioInputCollection::activeInputsCount() const
169{
170    uint32_t count = 0;
171    for (size_t i = 0; i < size(); i++) {
172        const sp<AudioInputDescriptor>  desc = valueAt(i);
173        if (desc->mRefCount > 0) {
174            count++;
175        }
176    }
177    return count;
178}
179
180audio_io_handle_t AudioInputCollection::getActiveInput(bool ignoreVirtualInputs)
181{
182    for (size_t i = 0; i < size(); i++) {
183        const sp<AudioInputDescriptor>  input_descriptor = valueAt(i);
184        if ((input_descriptor->mRefCount > 0)
185                && (!ignoreVirtualInputs || !is_virtual_input_device(input_descriptor->mDevice))) {
186            return keyAt(i);
187        }
188    }
189    return 0;
190}
191
192audio_devices_t AudioInputCollection::getSupportedDevices(audio_io_handle_t handle) const
193{
194    sp<AudioInputDescriptor> inputDesc = valueFor(handle);
195    audio_devices_t devices = inputDesc->mProfile->mSupportedDevices.types();
196    return devices;
197}
198
199status_t AudioInputCollection::dump(int fd) const
200{
201    const size_t SIZE = 256;
202    char buffer[SIZE];
203
204    snprintf(buffer, SIZE, "\nInputs dump:\n");
205    write(fd, buffer, strlen(buffer));
206    for (size_t i = 0; i < size(); i++) {
207        snprintf(buffer, SIZE, "- Input %d dump:\n", keyAt(i));
208        write(fd, buffer, strlen(buffer));
209        valueAt(i)->dump(fd);
210    }
211
212    return NO_ERROR;
213}
214
215}; //namespace android
216