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 
27 namespace android {
28 
AudioInputDescriptor(const sp<IOProfile> & profile)29 AudioInputDescriptor::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 
setIoHandle(audio_io_handle_t ioHandle)44 void AudioInputDescriptor::setIoHandle(audio_io_handle_t ioHandle)
45 {
46     mId = AudioPort::getNextUniqueId();
47     mIoHandle = ioHandle;
48 }
49 
getModuleHandle() const50 audio_module_handle_t AudioInputDescriptor::getModuleHandle() const
51 {
52     if (mProfile == 0) {
53         return 0;
54     }
55     return mProfile->getModuleHandle();
56 }
57 
getId() const58 audio_port_handle_t AudioInputDescriptor::getId() const
59 {
60     return mId;
61 }
62 
toAudioPortConfig(struct audio_port_config * dstConfig,const struct audio_port_config * srcConfig) const63 void 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 
toAudioPort(struct audio_port * port) const84 void 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 
dump(int fd)96 status_t AudioInputDescriptor::dump(int fd)
97 {
98     const size_t SIZE = 256;
99     char buffer[SIZE];
100     String8 result;
101 
102     snprintf(buffer, SIZE, " ID: %d\n", getId());
103     result.append(buffer);
104     snprintf(buffer, SIZE, " Sampling rate: %d\n", mSamplingRate);
105     result.append(buffer);
106     snprintf(buffer, SIZE, " Format: %d\n", mFormat);
107     result.append(buffer);
108     snprintf(buffer, SIZE, " Channels: %08x\n", mChannelMask);
109     result.append(buffer);
110     snprintf(buffer, SIZE, " Devices %08x\n", mDevice);
111     result.append(buffer);
112     snprintf(buffer, SIZE, " Ref Count %d\n", mRefCount);
113     result.append(buffer);
114     snprintf(buffer, SIZE, " Open Ref Count %d\n", mOpenRefCount);
115     result.append(buffer);
116 
117     write(fd, result.string(), result.size());
118 
119     return NO_ERROR;
120 }
121 
isSourceActive(audio_source_t source) const122 bool AudioInputCollection::isSourceActive(audio_source_t source) const
123 {
124     for (size_t i = 0; i < size(); i++) {
125         const sp<AudioInputDescriptor>  inputDescriptor = valueAt(i);
126         if (inputDescriptor->mRefCount == 0) {
127             continue;
128         }
129         if (inputDescriptor->mInputSource == (int)source) {
130             return true;
131         }
132     }
133     return false;
134 }
135 
getInputFromId(audio_port_handle_t id) const136 sp<AudioInputDescriptor> AudioInputCollection::getInputFromId(audio_port_handle_t id) const
137 {
138     sp<AudioInputDescriptor> inputDesc = NULL;
139     for (size_t i = 0; i < size(); i++) {
140         inputDesc = valueAt(i);
141         if (inputDesc->getId() == id) {
142             break;
143         }
144     }
145     return inputDesc;
146 }
147 
activeInputsCount() const148 uint32_t AudioInputCollection::activeInputsCount() const
149 {
150     uint32_t count = 0;
151     for (size_t i = 0; i < size(); i++) {
152         const sp<AudioInputDescriptor>  desc = valueAt(i);
153         if (desc->mRefCount > 0) {
154             count++;
155         }
156     }
157     return count;
158 }
159 
getActiveInput(bool ignoreVirtualInputs)160 audio_io_handle_t AudioInputCollection::getActiveInput(bool ignoreVirtualInputs)
161 {
162     for (size_t i = 0; i < size(); i++) {
163         const sp<AudioInputDescriptor>  input_descriptor = valueAt(i);
164         if ((input_descriptor->mRefCount > 0)
165                 && (!ignoreVirtualInputs || !is_virtual_input_device(input_descriptor->mDevice))) {
166             return keyAt(i);
167         }
168     }
169     return 0;
170 }
171 
getSupportedDevices(audio_io_handle_t handle) const172 audio_devices_t AudioInputCollection::getSupportedDevices(audio_io_handle_t handle) const
173 {
174     sp<AudioInputDescriptor> inputDesc = valueFor(handle);
175     audio_devices_t devices = inputDesc->mProfile->mSupportedDevices.types();
176     return devices;
177 }
178 
dump(int fd) const179 status_t AudioInputCollection::dump(int fd) const
180 {
181     const size_t SIZE = 256;
182     char buffer[SIZE];
183 
184     snprintf(buffer, SIZE, "\nInputs dump:\n");
185     write(fd, buffer, strlen(buffer));
186     for (size_t i = 0; i < size(); i++) {
187         snprintf(buffer, SIZE, "- Input %d dump:\n", keyAt(i));
188         write(fd, buffer, strlen(buffer));
189         valueAt(i)->dump(fd);
190     }
191 
192     return NO_ERROR;
193 }
194 
195 }; //namespace android
196