AudioInputDescriptor.cpp revision fc732a63bd8c8c62ce75f36f03088ca6727caece
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),
32      mProfile(profile), mPatchHandle(AUDIO_PATCH_HANDLE_NONE), mId(0)
33{
34    if (profile != NULL) {
35        profile->pickAudioProfile(mSamplingRate, mChannelMask, mFormat);
36        if (profile->mGains.size() > 0) {
37            profile->mGains[0]->getDefaultConfig(&mGain);
38        }
39    }
40}
41
42void AudioInputDescriptor::setIoHandle(audio_io_handle_t ioHandle)
43{
44    mId = AudioPort::getNextUniqueId();
45    mIoHandle = ioHandle;
46}
47
48audio_module_handle_t AudioInputDescriptor::getModuleHandle() const
49{
50    if (mProfile == 0) {
51        return AUDIO_MODULE_HANDLE_NONE;
52    }
53    return mProfile->getModuleHandle();
54}
55
56uint32_t AudioInputDescriptor::getOpenRefCount() const
57{
58    return mSessions.getOpenCount();
59}
60
61audio_port_handle_t AudioInputDescriptor::getId() const
62{
63    return mId;
64}
65
66audio_source_t AudioInputDescriptor::inputSource() const
67{
68    // TODO: return highest priority input source
69    return mSessions.size() > 0 ? mSessions.valueAt(0)->inputSource() :
70                       AUDIO_SOURCE_DEFAULT;
71}
72
73void AudioInputDescriptor::toAudioPortConfig(struct audio_port_config *dstConfig,
74                                             const struct audio_port_config *srcConfig) const
75{
76    ALOG_ASSERT(mProfile != 0,
77                "toAudioPortConfig() called on input with null profile %d", mIoHandle);
78    dstConfig->config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE|AUDIO_PORT_CONFIG_CHANNEL_MASK|
79                            AUDIO_PORT_CONFIG_FORMAT|AUDIO_PORT_CONFIG_GAIN;
80    if (srcConfig != NULL) {
81        dstConfig->config_mask |= srcConfig->config_mask;
82    }
83
84    AudioPortConfig::toAudioPortConfig(dstConfig, srcConfig);
85
86    dstConfig->id = mId;
87    dstConfig->role = AUDIO_PORT_ROLE_SINK;
88    dstConfig->type = AUDIO_PORT_TYPE_MIX;
89    dstConfig->ext.mix.hw_module = getModuleHandle();
90    dstConfig->ext.mix.handle = mIoHandle;
91    dstConfig->ext.mix.usecase.source = inputSource();
92}
93
94void AudioInputDescriptor::toAudioPort(struct audio_port *port) const
95{
96    ALOG_ASSERT(mProfile != 0, "toAudioPort() called on input with null profile %d", mIoHandle);
97
98    mProfile->toAudioPort(port);
99    port->id = mId;
100    toAudioPortConfig(&port->active_config);
101    port->ext.mix.hw_module = getModuleHandle();
102    port->ext.mix.handle = mIoHandle;
103    port->ext.mix.latency_class = AUDIO_LATENCY_NORMAL;
104}
105
106void AudioInputDescriptor::setPreemptedSessions(const SortedVector<audio_session_t>& sessions)
107{
108    mPreemptedSessions = sessions;
109}
110
111SortedVector<audio_session_t> AudioInputDescriptor::getPreemptedSessions() const
112{
113    return mPreemptedSessions;
114}
115
116bool AudioInputDescriptor::hasPreemptedSession(audio_session_t session) const
117{
118    return (mPreemptedSessions.indexOf(session) >= 0);
119}
120
121void AudioInputDescriptor::clearPreemptedSessions()
122{
123    mPreemptedSessions.clear();
124}
125
126bool AudioInputDescriptor::isActive() const {
127    return mSessions.hasActiveSession();
128}
129
130bool AudioInputDescriptor::isSourceActive(audio_source_t source) const
131{
132    return mSessions.isSourceActive(source);
133}
134
135audio_source_t AudioInputDescriptor::getHighestPrioritySource(bool activeOnly) const
136{
137
138    return mSessions.getHighestPrioritySource(activeOnly);
139}
140
141bool AudioInputDescriptor::isSoundTrigger() const {
142    // sound trigger and non sound trigger sessions are not mixed
143    // on a given input
144    return mSessions.valueAt(0)->isSoundTrigger();
145}
146
147sp<AudioSession> AudioInputDescriptor::getAudioSession(
148                                              audio_session_t session) const {
149    return mSessions.valueFor(session);
150}
151
152AudioSessionCollection AudioInputDescriptor::getAudioSessions(bool activeOnly) const
153{
154    if (activeOnly) {
155        return mSessions.getActiveSessions();
156    } else {
157        return mSessions;
158    }
159}
160
161size_t AudioInputDescriptor::getAudioSessionCount(bool activeOnly) const
162{
163    if (activeOnly) {
164        return mSessions.getActiveSessionCount();
165    } else {
166        return mSessions.size();
167    }
168}
169
170status_t AudioInputDescriptor::addAudioSession(audio_session_t session,
171                         const sp<AudioSession>& audioSession) {
172    return mSessions.addSession(session, audioSession, /*AudioSessionInfoProvider*/this);
173}
174
175status_t AudioInputDescriptor::removeAudioSession(audio_session_t session) {
176    return mSessions.removeSession(session);
177}
178
179audio_patch_handle_t AudioInputDescriptor::getPatchHandle() const
180{
181    return mPatchHandle;
182}
183
184void AudioInputDescriptor::setPatchHandle(audio_patch_handle_t handle)
185{
186    mPatchHandle = handle;
187    mSessions.onSessionInfoUpdate();
188}
189
190audio_config_base_t AudioInputDescriptor::getConfig() const
191{
192    const audio_config_base_t config = { .sample_rate = mSamplingRate, .channel_mask = mChannelMask,
193            .format = mFormat };
194    return config;
195}
196
197status_t AudioInputDescriptor::dump(int fd)
198{
199    const size_t SIZE = 256;
200    char buffer[SIZE];
201    String8 result;
202
203    snprintf(buffer, SIZE, " ID: %d\n", getId());
204    result.append(buffer);
205    snprintf(buffer, SIZE, " Sampling rate: %d\n", mSamplingRate);
206    result.append(buffer);
207    snprintf(buffer, SIZE, " Format: %d\n", mFormat);
208    result.append(buffer);
209    snprintf(buffer, SIZE, " Channels: %08x\n", mChannelMask);
210    result.append(buffer);
211    snprintf(buffer, SIZE, " Devices %08x\n", mDevice);
212    result.append(buffer);
213
214    write(fd, result.string(), result.size());
215
216    mSessions.dump(fd, 1);
217
218    return NO_ERROR;
219}
220
221bool AudioInputCollection::isSourceActive(audio_source_t source) const
222{
223    for (size_t i = 0; i < size(); i++) {
224        const sp<AudioInputDescriptor>  inputDescriptor = valueAt(i);
225        if (inputDescriptor->isSourceActive(source)) {
226            return true;
227        }
228    }
229    return false;
230}
231
232sp<AudioInputDescriptor> AudioInputCollection::getInputFromId(audio_port_handle_t id) const
233{
234    sp<AudioInputDescriptor> inputDesc = NULL;
235    for (size_t i = 0; i < size(); i++) {
236        inputDesc = valueAt(i);
237        if (inputDesc->getId() == id) {
238            break;
239        }
240    }
241    return inputDesc;
242}
243
244uint32_t AudioInputCollection::activeInputsCount() const
245{
246    uint32_t count = 0;
247    for (size_t i = 0; i < size(); i++) {
248        const sp<AudioInputDescriptor>  inputDescriptor = valueAt(i);
249        if (inputDescriptor->isActive()) {
250            count++;
251        }
252    }
253    return count;
254}
255
256Vector<sp <AudioInputDescriptor> > AudioInputCollection::getActiveInputs(bool ignoreVirtualInputs)
257{
258    Vector<sp <AudioInputDescriptor> > activeInputs;
259
260    for (size_t i = 0; i < size(); i++) {
261        const sp<AudioInputDescriptor>  inputDescriptor = valueAt(i);
262        if ((inputDescriptor->isActive())
263                && (!ignoreVirtualInputs ||
264                    !is_virtual_input_device(inputDescriptor->mDevice))) {
265            activeInputs.add(inputDescriptor);
266        }
267    }
268    return activeInputs;
269}
270
271audio_devices_t AudioInputCollection::getSupportedDevices(audio_io_handle_t handle) const
272{
273    sp<AudioInputDescriptor> inputDesc = valueFor(handle);
274    audio_devices_t devices = inputDesc->mProfile->getSupportedDevicesType();
275    return devices;
276}
277
278status_t AudioInputCollection::dump(int fd) const
279{
280    const size_t SIZE = 256;
281    char buffer[SIZE];
282
283    snprintf(buffer, SIZE, "\nInputs dump:\n");
284    write(fd, buffer, strlen(buffer));
285    for (size_t i = 0; i < size(); i++) {
286        snprintf(buffer, SIZE, "- Input %d dump:\n", keyAt(i));
287        write(fd, buffer, strlen(buffer));
288        valueAt(i)->dump(fd);
289    }
290
291    return NO_ERROR;
292}
293
294}; //namespace android
295