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