AudioInputDescriptor.cpp revision 599c758b258cc5da0dba9b530425381facc37d77
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),
32      mProfile(profile), 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
58uint32_t AudioInputDescriptor::getOpenRefCount() const
59{
60    return mSessions.getOpenCount();
61}
62
63audio_port_handle_t AudioInputDescriptor::getId() const
64{
65    return mId;
66}
67
68audio_source_t AudioInputDescriptor::inputSource() const
69{
70    // TODO: return highest priority input source
71    return mSessions.size() > 0 ? mSessions.valueAt(0)->inputSource() :
72                       AUDIO_SOURCE_DEFAULT;
73}
74
75void AudioInputDescriptor::toAudioPortConfig(struct audio_port_config *dstConfig,
76                                             const struct audio_port_config *srcConfig) const
77{
78    ALOG_ASSERT(mProfile != 0,
79                "toAudioPortConfig() called on input with null profile %d", mIoHandle);
80    dstConfig->config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE|AUDIO_PORT_CONFIG_CHANNEL_MASK|
81                            AUDIO_PORT_CONFIG_FORMAT|AUDIO_PORT_CONFIG_GAIN;
82    if (srcConfig != NULL) {
83        dstConfig->config_mask |= srcConfig->config_mask;
84    }
85
86    AudioPortConfig::toAudioPortConfig(dstConfig, srcConfig);
87
88    dstConfig->id = mId;
89    dstConfig->role = AUDIO_PORT_ROLE_SINK;
90    dstConfig->type = AUDIO_PORT_TYPE_MIX;
91    dstConfig->ext.mix.hw_module = getModuleHandle();
92    dstConfig->ext.mix.handle = mIoHandle;
93    dstConfig->ext.mix.usecase.source = inputSource();
94}
95
96void AudioInputDescriptor::toAudioPort(struct audio_port *port) const
97{
98    ALOG_ASSERT(mProfile != 0, "toAudioPort() called on input with null profile %d", mIoHandle);
99
100    mProfile->toAudioPort(port);
101    port->id = mId;
102    toAudioPortConfig(&port->active_config);
103    port->ext.mix.hw_module = getModuleHandle();
104    port->ext.mix.handle = mIoHandle;
105    port->ext.mix.latency_class = AUDIO_LATENCY_NORMAL;
106}
107
108void AudioInputDescriptor::setPreemptedSessions(const SortedVector<audio_session_t>& sessions)
109{
110    mPreemptedSessions = sessions;
111}
112
113SortedVector<audio_session_t> AudioInputDescriptor::getPreemptedSessions() const
114{
115    return mPreemptedSessions;
116}
117
118bool AudioInputDescriptor::hasPreemptedSession(audio_session_t session) const
119{
120    return (mPreemptedSessions.indexOf(session) >= 0);
121}
122
123void AudioInputDescriptor::clearPreemptedSessions()
124{
125    mPreemptedSessions.clear();
126}
127
128bool AudioInputDescriptor::isActive() const {
129    return mSessions.hasActiveSession();
130}
131
132bool AudioInputDescriptor::isSourceActive(audio_source_t source) const
133{
134    return mSessions.isSourceActive(source);
135}
136
137bool AudioInputDescriptor::isSoundTrigger() const {
138    // sound trigger and non sound trigger sessions are not mixed
139    // on a given input
140    return mSessions.valueAt(0)->isSoundTrigger();
141}
142
143sp<AudioSession> AudioInputDescriptor::getAudioSession(
144                                              audio_session_t session) const {
145    return mSessions.valueFor(session);
146}
147
148AudioSessionCollection AudioInputDescriptor::getActiveAudioSessions() const
149{
150    return mSessions.getActiveSessions();
151}
152
153status_t AudioInputDescriptor::addAudioSession(audio_session_t session,
154                         const sp<AudioSession>& audioSession) {
155    return mSessions.addSession(session, audioSession);
156}
157
158status_t AudioInputDescriptor::removeAudioSession(audio_session_t session) {
159    return mSessions.removeSession(session);
160}
161
162status_t AudioInputDescriptor::dump(int fd)
163{
164    const size_t SIZE = 256;
165    char buffer[SIZE];
166    String8 result;
167
168    snprintf(buffer, SIZE, " ID: %d\n", getId());
169    result.append(buffer);
170    snprintf(buffer, SIZE, " Sampling rate: %d\n", mSamplingRate);
171    result.append(buffer);
172    snprintf(buffer, SIZE, " Format: %d\n", mFormat);
173    result.append(buffer);
174    snprintf(buffer, SIZE, " Channels: %08x\n", mChannelMask);
175    result.append(buffer);
176    snprintf(buffer, SIZE, " Devices %08x\n", mDevice);
177    result.append(buffer);
178
179    write(fd, result.string(), result.size());
180
181    mSessions.dump(fd, 1);
182
183    return NO_ERROR;
184}
185
186bool AudioInputCollection::isSourceActive(audio_source_t source) const
187{
188    for (size_t i = 0; i < size(); i++) {
189        const sp<AudioInputDescriptor>  inputDescriptor = valueAt(i);
190        if (inputDescriptor->isSourceActive(source)) {
191            return true;
192        }
193    }
194    return false;
195}
196
197sp<AudioInputDescriptor> AudioInputCollection::getInputFromId(audio_port_handle_t id) const
198{
199    sp<AudioInputDescriptor> inputDesc = NULL;
200    for (size_t i = 0; i < size(); i++) {
201        inputDesc = valueAt(i);
202        if (inputDesc->getId() == id) {
203            break;
204        }
205    }
206    return inputDesc;
207}
208
209uint32_t AudioInputCollection::activeInputsCount() const
210{
211    uint32_t count = 0;
212    for (size_t i = 0; i < size(); i++) {
213        const sp<AudioInputDescriptor>  inputDescriptor = valueAt(i);
214        if (inputDescriptor->isActive()) {
215            count++;
216        }
217    }
218    return count;
219}
220
221audio_io_handle_t AudioInputCollection::getActiveInput(bool ignoreVirtualInputs)
222{
223    for (size_t i = 0; i < size(); i++) {
224        const sp<AudioInputDescriptor>  inputDescriptor = valueAt(i);
225        if ((inputDescriptor->isActive())
226                && (!ignoreVirtualInputs ||
227                    !is_virtual_input_device(inputDescriptor->mDevice))) {
228            return keyAt(i);
229        }
230    }
231    return 0;
232}
233
234audio_devices_t AudioInputCollection::getSupportedDevices(audio_io_handle_t handle) const
235{
236    sp<AudioInputDescriptor> inputDesc = valueFor(handle);
237    audio_devices_t devices = inputDesc->mProfile->getSupportedDevicesType();
238    return devices;
239}
240
241status_t AudioInputCollection::dump(int fd) const
242{
243    const size_t SIZE = 256;
244    char buffer[SIZE];
245
246    snprintf(buffer, SIZE, "\nInputs dump:\n");
247    write(fd, buffer, strlen(buffer));
248    for (size_t i = 0; i < size(); i++) {
249        snprintf(buffer, SIZE, "- Input %d dump:\n", keyAt(i));
250        write(fd, buffer, strlen(buffer));
251        valueAt(i)->dump(fd);
252    }
253
254    return NO_ERROR;
255}
256
257}; //namespace android
258