summaryrefslogtreecommitdiff
path: root/libaudio/AudioHardwareInput.cpp
blob: 6a7a9f4c5cba8110f0f77041d160de22275dd449 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/*
**
** Copyright 2012, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
**     http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/

#define LOG_TAG "AudioHAL:AudioHardwareInput"
#include <utils/Log.h>

#include <fcntl.h>
#include <sys/eventfd.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>

#include <utils/String8.h>

#include "AudioHardwareInput.h"
#include "AudioHotplugThread.h"
#include "AudioStreamIn.h"

namespace android {

// Global singleton.
AudioHardwareInput gAudioHardwareInput;

AudioHardwareInput::AudioHardwareInput()
    : mMicMute(false)
{
    mHotplugThread = new AudioHotplugThread(*this);
    if (mHotplugThread == NULL) {
        ALOGE("Unable to create ATV Remote audio hotplug thread. "
              "Pluggable audio input devices will not function.");
    } else if (!mHotplugThread->start()) {
        ALOGE("Unable to start ATV Remote audio hotplug thread. "
              "Pluggable audio input devices will not function.");
        mHotplugThread.clear();
    }

    for (int i=0; i<kMaxDevices; i++) {
        mDeviceInfos[i].valid = false;
    }
}

AudioHardwareInput::~AudioHardwareInput()
{
    if (mHotplugThread != NULL) {
        mHotplugThread->shutdown();
        mHotplugThread.clear();
    }

    closeAllInputStreams();
}

status_t AudioHardwareInput::setMicMute(bool mute)
{
    mMicMute = mute;
    return NO_ERROR;
}

status_t AudioHardwareInput::getMicMute(bool* mute)
{
    *mute = mMicMute;
    return NO_ERROR;
}

// milliseconds per ALSA period
const uint32_t AudioHardwareInput::kPeriodMsec = 10;

size_t AudioHardwareInput::calculateInputBufferSize(uint32_t outputSampleRate,
                                                    audio_format_t format,
                                                    uint32_t channelCount)
{
    size_t size;

    // AudioFlinger expects audio buffers to be a multiple of 16 frames
    size = (kPeriodMsec * outputSampleRate) / 1000;
    size = ((size + 15) / 16) * 16;

    return size * channelCount * audio_bytes_per_sample(format);
}

status_t AudioHardwareInput::getInputBufferSize(const audio_config* config)
{
    size_t size = calculateInputBufferSize(config->sample_rate,
                                           config->format,
                                           audio_channel_count_from_in_mask(config->channel_mask));
    return size;
}

AudioStreamIn* AudioHardwareInput::openInputStream(uint32_t devices,
        audio_format_t* format, uint32_t* channelMask, uint32_t* sampleRate,
        status_t* status)
{
    (void) devices;
    Mutex::Autolock _l(mLock);

    AudioStreamIn* in;

    in = new AudioStreamIn(*this);
    if (in == NULL) {
        *status = NO_MEMORY;
        return NULL;
    }

    *status = in->set(format, channelMask, sampleRate);

    if (*status != NO_ERROR) {
        delete in;
        return NULL;
    }

    mInputStreams.add(in);

    return in;
}

void AudioHardwareInput::closeInputStream(AudioStreamIn* in)
{
    Mutex::Autolock _l(mLock);

    for (size_t i = 0; i < mInputStreams.size(); i++) {
        if (in == mInputStreams[i]) {
            mInputStreams.removeAt(i);
            in->standby();
            delete in;
            break;
        }
    }
}

void AudioHardwareInput::closeAllInputStreams()
{
    while (mInputStreams.size() != 0) {
        AudioStreamIn* in = mInputStreams[0];
        mInputStreams.removeAt(0);
        in->standby();
        delete in;
    }
}

void AudioHardwareInput::standbyAllInputStreams(const AudioHotplugThread::DeviceInfo* deviceInfo)
{
    for (size_t i = 0; i < mInputStreams.size(); i++) {
        if (deviceInfo == NULL || deviceInfo == mInputStreams[i]->getDeviceInfo()) {
            mInputStreams[i]->standby();
        }
    }
}

#define DUMP(a...) \
    snprintf(buffer, SIZE, a); \
    buffer[SIZE - 1] = 0; \
    result.append(buffer);
#define B2STR(b) b ? "true" : "false"

status_t AudioHardwareInput::dump(int fd)
{
    const size_t SIZE = 256;
    char buffer[SIZE];
    String8 result;

    DUMP("\nAudioHardwareInput::dump\n");

    for (int i=0; i<kMaxDevices; i++) {
        if (mDeviceInfos[i].valid) {
            DUMP("device[%d] is valid\n", i);
            DUMP("\tcapture card: %d\n", mDeviceInfos[i].pcmCard);
            DUMP("\tcapture device: %d\n", mDeviceInfos[i].pcmDevice);
        }
    }

    ::write(fd, result.string(), result.size());

    {
        Mutex::Autolock _l(mLock);
        for (size_t i = 0; i < mInputStreams.size(); i++) {
            mInputStreams[i]->dump(fd);
        }
    }

    return NO_ERROR;
}

#undef DUMP
#undef B2STR

// called on the audio hotplug thread
void AudioHardwareInput::onDeviceFound(
        const AudioHotplugThread::DeviceInfo& devInfo)
{
    bool foundSlot = false;
    Mutex::Autolock _l(mLock);

    ALOGD("AudioHardwareInput::onDeviceFound pcmCard = %d", devInfo.pcmCard);

    for (int i=0; i<kMaxDevices; i++) {
        if (mDeviceInfos[i].valid) {
            if ((mDeviceInfos[i].pcmCard == devInfo.pcmCard)
                && (mDeviceInfos[i].pcmDevice == devInfo.pcmDevice)) {
                ALOGW("AudioHardwareInput::onDeviceFound already has  %d:%d",
                    devInfo.pcmCard, devInfo.pcmDevice);
                return; // Got it already so no action needed.
            }
        }
    }

    // New device so find an empty slot and save it.
    for (int i=0; i<kMaxDevices; i++) {
        if (!mDeviceInfos[i].valid) {
            ALOGD("AudioHardwareInput::onDeviceFound saving as device #%d", i);
            mDeviceInfos[i] = devInfo;
            mDeviceInfos[i].valid = true;
            foundSlot = true;
            /* Restart any currently running streams. */
            standbyAllInputStreams(NULL);
            break;
        }
    }

    if (!foundSlot) {
        ALOGW("AudioHardwareInput::onDeviceFound found more devices than expected! Dropped");
    }
}

// called on the audio hotplug thread
void AudioHardwareInput::onDeviceRemoved(unsigned int pcmCard, unsigned int pcmDevice)
{
    Mutex::Autolock _l(mLock);

    ALOGD("AudioHardwareInput::onDeviceRemoved pcmCard = %d", pcmCard);
    // Find matching DeviceInfo.
    for (int i=0; i<kMaxDevices; i++) {
        if (mDeviceInfos[i].valid) {
            if ((mDeviceInfos[i].pcmCard == pcmCard) && (mDeviceInfos[i].pcmDevice == pcmDevice)) {
                ALOGD("AudioHardwareInput::onDeviceRemoved matches #%d", i);
                mDeviceInfos[i].valid = false;
                /* If currently active stream is using this device then restart. */
                standbyAllInputStreams(&mDeviceInfos[i]);
                break;
            }
        }
    }
}

const AudioHotplugThread::DeviceInfo* AudioHardwareInput::getBestDevice(int inputSource)
{
    bool doVoiceRecognition = (inputSource == AUDIO_SOURCE_VOICE_RECOGNITION);
    int chosenDeviceIndex = -1;
    Mutex::Autolock _l(mLock);

    ALOGD("AudioHardwareInput::getBestDevice inputSource = %d, doVoiceRecognition = %d",
        inputSource, (doVoiceRecognition ? 1 : 0));
    // RemoteControl is the only input device usable for voice recognition
    // and no other devices are used for voice recognition.
    // Currently the RemoteControl is the only device marked with forVoiceRecognition=true.
    // A connected USB mic could be used for anything but voice recognition.
    for (int i=0; i<kMaxDevices; i++) {
        if (mDeviceInfos[i].valid) {
            if (mDeviceInfos[i].forVoiceRecognition == doVoiceRecognition) {
                chosenDeviceIndex = i;
                break;
            }
        }
    }

    if (chosenDeviceIndex < 0) {
        ALOGE("ERROR AudioHardwareInput::getBestDevice, none for source %d", inputSource);
    } else {
        ALOGD("AudioHardwareInput::getBestDevice chose #%d", chosenDeviceIndex);
    }

    return (chosenDeviceIndex >= 0) ? &mDeviceInfos[chosenDeviceIndex] : NULL;
}

}; // namespace android