AudioHardware.cpp revision 63ca66ea9da5ea61806ee547f3aa1c498ac235fe
1/*
2** Copyright 2008, Google Inc.
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#include <math.h>
18
19//#define LOG_NDEBUG 0
20#define LOG_TAG "AudioHardwareMSM72XX"
21#include <utils/Log.h>
22#include <utils/String8.h>
23
24#include <stdio.h>
25#include <unistd.h>
26#include <sys/ioctl.h>
27#include <sys/types.h>
28#include <sys/stat.h>
29#include <dlfcn.h>
30#include <fcntl.h>
31
32// hardware specific functions
33
34#include "AudioHardware.h"
35#include <media/AudioRecord.h>
36
37#define LOG_SND_RPC 0  // Set to 1 to log sound RPC's
38
39namespace android {
40static int audpre_index, tx_iir_index;
41static void * acoustic;
42const uint32_t AudioHardware::inputSamplingRates[] = {
43        8000, 11025, 12000, 16000, 22050, 24000, 32000, 44100, 48000
44};
45// ----------------------------------------------------------------------------
46
47AudioHardware::AudioHardware() :
48    mInit(false), mMicMute(true), mBluetoothNrec(true), mBluetoothId(0),
49    mOutput(0), mSndEndpoints(NULL), mCurSndDevice(-1),
50    SND_DEVICE_CURRENT(-1),
51    SND_DEVICE_HANDSET(-1),
52    SND_DEVICE_SPEAKER(-1),
53    SND_DEVICE_HEADSET(-1),
54    SND_DEVICE_BT(-1),
55    SND_DEVICE_CARKIT(-1),
56    SND_DEVICE_TTY_FULL(-1),
57    SND_DEVICE_TTY_VCO(-1),
58    SND_DEVICE_TTY_HCO(-1),
59    SND_DEVICE_NO_MIC_HEADSET(-1),
60    SND_DEVICE_FM_HEADSET(-1),
61    SND_DEVICE_HEADSET_AND_SPEAKER(-1),
62    SND_DEVICE_FM_SPEAKER(-1),
63    SND_DEVICE_BT_EC_OFF(-1)
64{
65
66    int (*snd_get_num)();
67    int (*snd_get_endpoint)(int, msm_snd_endpoint *);
68    int (*set_acoustic_parameters)();
69
70    struct msm_snd_endpoint *ept;
71
72    acoustic = ::dlopen("/system/lib/libhtc_acoustic.so", RTLD_NOW);
73    if (acoustic == NULL ) {
74        LOGE("Could not open libhtc_acoustic.so");
75        /* this is not really an error on non-htc devices... */
76        mNumSndEndpoints = 0;
77        mInit = true;
78        return;
79    }
80
81    set_acoustic_parameters = (int (*)(void))::dlsym(acoustic, "set_acoustic_parameters");
82    if ((*set_acoustic_parameters) == 0 ) {
83        LOGE("Could not open set_acoustic_parameters()");
84        return;
85    }
86
87    int rc = set_acoustic_parameters();
88    if (rc < 0) {
89        LOGE("Could not set acoustic parameters to share memory: %d", rc);
90//        return;
91    }
92
93    snd_get_num = (int (*)(void))::dlsym(acoustic, "snd_get_num_endpoints");
94    if ((*snd_get_num) == 0 ) {
95        LOGE("Could not open snd_get_num()");
96//        return;
97    }
98
99    mNumSndEndpoints = snd_get_num();
100    LOGD("mNumSndEndpoints = %d", mNumSndEndpoints);
101    mSndEndpoints = new msm_snd_endpoint[mNumSndEndpoints];
102    mInit = true;
103    LOGV("constructed %d SND endpoints)", mNumSndEndpoints);
104    ept = mSndEndpoints;
105    snd_get_endpoint = (int (*)(int, msm_snd_endpoint *))::dlsym(acoustic, "snd_get_endpoint");
106    if ((*snd_get_endpoint) == 0 ) {
107        LOGE("Could not open snd_get_endpoint()");
108        return;
109    }
110
111    for (int cnt = 0; cnt < mNumSndEndpoints; cnt++, ept++) {
112        ept->id = cnt;
113        snd_get_endpoint(cnt, ept);
114#define CHECK_FOR(desc) \
115        if (!strcmp(ept->name, #desc)) { \
116            SND_DEVICE_##desc = ept->id; \
117            LOGD("BT MATCH " #desc); \
118        } else
119        CHECK_FOR(CURRENT)
120        CHECK_FOR(HANDSET)
121        CHECK_FOR(SPEAKER)
122        CHECK_FOR(BT)
123        CHECK_FOR(BT_EC_OFF)
124        CHECK_FOR(HEADSET)
125        CHECK_FOR(CARKIT)
126        CHECK_FOR(TTY_FULL)
127        CHECK_FOR(TTY_VCO)
128        CHECK_FOR(TTY_HCO)
129        CHECK_FOR(NO_MIC_HEADSET)
130        CHECK_FOR(FM_HEADSET)
131        CHECK_FOR(FM_SPEAKER)
132        CHECK_FOR(HEADSET_AND_SPEAKER) {}
133#undef CHECK_FOR
134    }
135}
136
137AudioHardware::~AudioHardware()
138{
139    for (size_t index = 0; index < mInputs.size(); index++) {
140        closeInputStream((AudioStreamIn*)mInputs[index]);
141    }
142    mInputs.clear();
143    closeOutputStream((AudioStreamOut*)mOutput);
144    delete [] mSndEndpoints;
145    if (acoustic) {
146        ::dlclose(acoustic);
147        acoustic = 0;
148    }
149    mInit = false;
150}
151
152status_t AudioHardware::initCheck()
153{
154    return mInit ? NO_ERROR : NO_INIT;
155}
156
157AudioStreamOut* AudioHardware::openOutputStream(
158        uint32_t devices, int *format, uint32_t *channels, uint32_t *sampleRate, status_t *status)
159{
160    { // scope for the lock
161        Mutex::Autolock lock(mLock);
162
163        // only one output stream allowed
164        if (mOutput) {
165            if (status) {
166                *status = INVALID_OPERATION;
167            }
168            return 0;
169        }
170
171        // create new output stream
172        AudioStreamOutMSM72xx* out = new AudioStreamOutMSM72xx();
173        status_t lStatus = out->set(this, devices, format, channels, sampleRate);
174        if (status) {
175            *status = lStatus;
176        }
177        if (lStatus == NO_ERROR) {
178            mOutput = out;
179        } else {
180            delete out;
181        }
182    }
183    return mOutput;
184}
185
186void AudioHardware::closeOutputStream(AudioStreamOut* out) {
187    Mutex::Autolock lock(mLock);
188    if (mOutput == 0 || mOutput != out) {
189        LOGW("Attempt to close invalid output stream");
190    }
191    else {
192        delete mOutput;
193        mOutput = 0;
194    }
195}
196
197AudioStreamIn* AudioHardware::openInputStream(
198        uint32_t devices, int *format, uint32_t *channels, uint32_t *sampleRate, status_t *status,
199        AudioSystem::audio_in_acoustics acoustic_flags)
200{
201    // check for valid input source
202    if (!AudioSystem::isInputDevice((AudioSystem::audio_devices)devices)) {
203        return 0;
204    }
205
206    mLock.lock();
207
208    AudioStreamInMSM72xx* in = new AudioStreamInMSM72xx();
209    status_t lStatus = in->set(this, devices, format, channels, sampleRate, acoustic_flags);
210    if (status) {
211        *status = lStatus;
212    }
213    if (lStatus != NO_ERROR) {
214        mLock.unlock();
215        delete in;
216        return 0;
217    }
218
219    mInputs.add(in);
220    mLock.unlock();
221
222    return in;
223}
224
225void AudioHardware::closeInputStream(AudioStreamIn* in) {
226    Mutex::Autolock lock(mLock);
227
228    ssize_t index = mInputs.indexOf((AudioStreamInMSM72xx *)in);
229    if (index < 0) {
230        LOGW("Attempt to close invalid input stream");
231    } else {
232        mLock.unlock();
233        delete mInputs[index];
234        mLock.lock();
235        mInputs.removeAt(index);
236    }
237}
238
239status_t AudioHardware::setMode(int mode)
240{
241    status_t status = AudioHardwareBase::setMode(mode);
242    if (status == NO_ERROR) {
243        // make sure that doAudioRouteOrMute() is called by doRouting()
244        // even if the new device selected is the same as current one.
245        mCurSndDevice = -1;
246    }
247    return status;
248}
249
250bool AudioHardware::checkOutputStandby()
251{
252    if (mOutput)
253        if (!mOutput->checkStandby())
254            return false;
255
256    return true;
257}
258
259status_t AudioHardware::setMicMute(bool state)
260{
261    Mutex::Autolock lock(mLock);
262    return setMicMute_nosync(state);
263}
264
265// always call with mutex held
266status_t AudioHardware::setMicMute_nosync(bool state)
267{
268    if (mMicMute != state) {
269        mMicMute = state;
270        return doAudioRouteOrMute(SND_DEVICE_CURRENT);
271    }
272    return NO_ERROR;
273}
274
275status_t AudioHardware::getMicMute(bool* state)
276{
277    *state = mMicMute;
278    return NO_ERROR;
279}
280
281status_t AudioHardware::setParameters(const String8& keyValuePairs)
282{
283    AudioParameter param = AudioParameter(keyValuePairs);
284    String8 value;
285    String8 key;
286    const char BT_NREC_KEY[] = "bt_headset_nrec";
287    const char BT_NAME_KEY[] = "bt_headset_name";
288    const char BT_NREC_VALUE_ON[] = "on";
289
290
291    LOGV("setParameters() %s", keyValuePairs.string());
292
293    if (keyValuePairs.length() == 0) return BAD_VALUE;
294
295    key = String8(BT_NREC_KEY);
296    if (param.get(key, value) == NO_ERROR) {
297        if (value == BT_NREC_VALUE_ON) {
298            mBluetoothNrec = true;
299        } else {
300            mBluetoothNrec = false;
301            LOGI("Turning noise reduction and echo cancellation off for BT "
302                 "headset");
303        }
304    }
305    key = String8(BT_NAME_KEY);
306    if (param.get(key, value) == NO_ERROR) {
307        mBluetoothId = 0;
308        for (int i = 0; i < mNumSndEndpoints; i++) {
309            if (!strcasecmp(value.string(), mSndEndpoints[i].name)) {
310                mBluetoothId = mSndEndpoints[i].id;
311                LOGI("Using custom acoustic parameters for %s", value.string());
312                break;
313            }
314        }
315        if (mBluetoothId == 0) {
316            LOGI("Using default acoustic parameters "
317                 "(%s not in acoustic database)", value.string());
318            doRouting(NULL);
319        }
320    }
321    return NO_ERROR;
322}
323
324String8 AudioHardware::getParameters(const String8& keys)
325{
326    AudioParameter param = AudioParameter(keys);
327    return param.toString();
328}
329
330
331static unsigned calculate_audpre_table_index(unsigned index)
332{
333    switch (index) {
334        case 48000:    return SAMP_RATE_INDX_48000;
335        case 44100:    return SAMP_RATE_INDX_44100;
336        case 32000:    return SAMP_RATE_INDX_32000;
337        case 24000:    return SAMP_RATE_INDX_24000;
338        case 22050:    return SAMP_RATE_INDX_22050;
339        case 16000:    return SAMP_RATE_INDX_16000;
340        case 12000:    return SAMP_RATE_INDX_12000;
341        case 11025:    return SAMP_RATE_INDX_11025;
342        case 8000:    return SAMP_RATE_INDX_8000;
343        default:     return -1;
344    }
345}
346size_t AudioHardware::getInputBufferSize(uint32_t sampleRate, int format, int channelCount)
347{
348    if (format != AudioSystem::PCM_16_BIT) {
349        LOGW("getInputBufferSize bad format: %d", format);
350        return 0;
351    }
352    if (channelCount < 1 || channelCount > 2) {
353        LOGW("getInputBufferSize bad channel count: %d", channelCount);
354        return 0;
355    }
356
357    return 2048*channelCount;
358}
359
360static status_t set_volume_rpc(uint32_t device,
361                               uint32_t method,
362                               uint32_t volume)
363{
364    int fd;
365#if LOG_SND_RPC
366    LOGD("rpc_snd_set_volume(%d, %d, %d)\n", device, method, volume);
367#endif
368
369    if (device == -1UL) return NO_ERROR;
370
371    fd = open("/dev/msm_snd", O_RDWR);
372    if (fd < 0) {
373        LOGE("Can not open snd device");
374        return -EPERM;
375    }
376    /* rpc_snd_set_volume(
377     *     device,            # Any hardware device enum, including
378     *                        # SND_DEVICE_CURRENT
379     *     method,            # must be SND_METHOD_VOICE to do anything useful
380     *     volume,            # integer volume level, in range [0,5].
381     *                        # note that 0 is audible (not quite muted)
382     *  )
383     * rpc_snd_set_volume only works for in-call sound volume.
384     */
385     struct msm_snd_volume_config args;
386     args.device = device;
387     args.method = method;
388     args.volume = volume;
389
390     if (ioctl(fd, SND_SET_VOLUME, &args) < 0) {
391         LOGE("snd_set_volume error.");
392         close(fd);
393         return -EIO;
394     }
395     close(fd);
396     return NO_ERROR;
397}
398
399status_t AudioHardware::setVoiceVolume(float v)
400{
401    if (v < 0.0) {
402        LOGW("setVoiceVolume(%f) under 0.0, assuming 0.0\n", v);
403        v = 0.0;
404    } else if (v > 1.0) {
405        LOGW("setVoiceVolume(%f) over 1.0, assuming 1.0\n", v);
406        v = 1.0;
407    }
408
409    int vol = lrint(v * 5.0);
410    LOGD("setVoiceVolume(%f)\n", v);
411    LOGI("Setting in-call volume to %d (available range is 0 to 5)\n", vol);
412
413    Mutex::Autolock lock(mLock);
414    set_volume_rpc(SND_DEVICE_CURRENT, SND_METHOD_VOICE, vol);
415    return NO_ERROR;
416}
417
418status_t AudioHardware::setMasterVolume(float v)
419{
420    Mutex::Autolock lock(mLock);
421    int vol = ceil(v * 5.0);
422    LOGI("Set master volume to %d.\n", vol);
423    /*
424    set_volume_rpc(SND_DEVICE_HANDSET, SND_METHOD_VOICE, vol);
425    set_volume_rpc(SND_DEVICE_SPEAKER, SND_METHOD_VOICE, vol);
426    set_volume_rpc(SND_DEVICE_BT,      SND_METHOD_VOICE, vol);
427    set_volume_rpc(SND_DEVICE_HEADSET, SND_METHOD_VOICE, vol);
428    */
429    // We return an error code here to let the audioflinger do in-software
430    // volume on top of the maximum volume that we set through the SND API.
431    // return error - software mixer will handle it
432    return -1;
433}
434
435static status_t do_route_audio_rpc(uint32_t device,
436                                   bool ear_mute, bool mic_mute)
437{
438    if (device == -1UL)
439        return NO_ERROR;
440
441    int fd;
442#if LOG_SND_RPC
443    LOGD("rpc_snd_set_device(%d, %d, %d)\n", device, ear_mute, mic_mute);
444#endif
445
446    fd = open("/dev/msm_snd", O_RDWR);
447    if (fd < 0) {
448        LOGE("Can not open snd device");
449        return -EPERM;
450    }
451    // RPC call to switch audio path
452    /* rpc_snd_set_device(
453     *     device,            # Hardware device enum to use
454     *     ear_mute,          # Set mute for outgoing voice audio
455     *                        # this should only be unmuted when in-call
456     *     mic_mute,          # Set mute for incoming voice audio
457     *                        # this should only be unmuted when in-call or
458     *                        # recording.
459     *  )
460     */
461    struct msm_snd_device_config args;
462    args.device = device;
463    args.ear_mute = ear_mute ? SND_MUTE_MUTED : SND_MUTE_UNMUTED;
464    args.mic_mute = mic_mute ? SND_MUTE_MUTED : SND_MUTE_UNMUTED;
465
466    if (ioctl(fd, SND_SET_DEVICE, &args) < 0) {
467        LOGE("snd_set_device error.");
468        close(fd);
469        return -EIO;
470    }
471
472    close(fd);
473    return NO_ERROR;
474}
475
476// always call with mutex held
477status_t AudioHardware::doAudioRouteOrMute(uint32_t device)
478{
479    if (device == (uint32_t)SND_DEVICE_BT || device == (uint32_t)SND_DEVICE_CARKIT) {
480        if (mBluetoothId) {
481            device = mBluetoothId;
482        } else if (!mBluetoothNrec) {
483            device = SND_DEVICE_BT_EC_OFF;
484        }
485    }
486    LOGV("doAudioRouteOrMute() device %x, mMode %d, mMicMute %d", device, mMode, mMicMute);
487    return do_route_audio_rpc(device,
488                              mMode != AudioSystem::MODE_IN_CALL, mMicMute);
489}
490
491status_t AudioHardware::doRouting(AudioStreamInMSM72xx *input)
492{
493    /* currently this code doesn't work without the htc libacoustic */
494    if (!acoustic)
495        return 0;
496
497    Mutex::Autolock lock(mLock);
498    uint32_t outputDevices = mOutput->devices();
499    status_t ret = NO_ERROR;
500    int (*msm72xx_enable_audpp)(int);
501    msm72xx_enable_audpp = (int (*)(int))::dlsym(acoustic, "msm72xx_enable_audpp");
502    int audProcess = (ADRC_DISABLE | EQ_DISABLE | RX_IIR_DISABLE);
503    int sndDevice = -1;
504
505    if (input != NULL) {
506        uint32_t inputDevice = input->devices();
507        LOGI("do input routing device %x\n", inputDevice);
508        if (inputDevice != 0) {
509            if (inputDevice & AudioSystem::DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
510                LOGI("Routing audio to Bluetooth PCM\n");
511                sndDevice = SND_DEVICE_BT;
512            } else if (inputDevice & AudioSystem::DEVICE_IN_WIRED_HEADSET) {
513                if ((outputDevices & AudioSystem::DEVICE_OUT_WIRED_HEADSET) &&
514                    (outputDevices & AudioSystem::DEVICE_OUT_SPEAKER)) {
515                    LOGI("Routing audio to Wired Headset and Speaker\n");
516                    sndDevice = SND_DEVICE_HEADSET_AND_SPEAKER;
517                    audProcess = (ADRC_ENABLE | EQ_ENABLE | RX_IIR_ENABLE);
518                } else {
519                    LOGI("Routing audio to Wired Headset\n");
520                    sndDevice = SND_DEVICE_HEADSET;
521                }
522            } else {
523                if (outputDevices & AudioSystem::DEVICE_OUT_SPEAKER) {
524                    LOGI("Routing audio to Speakerphone\n");
525                    sndDevice = SND_DEVICE_SPEAKER;
526                    audProcess = (ADRC_ENABLE | EQ_ENABLE | RX_IIR_ENABLE);
527                } else {
528                    LOGI("Routing audio to Handset\n");
529                    sndDevice = SND_DEVICE_HANDSET;
530                }
531            }
532        }
533        // if inputDevice == 0, restore output routing
534    }
535
536    if (sndDevice == -1) {
537        if (outputDevices & (outputDevices - 1)) {
538            if ((outputDevices & AudioSystem::DEVICE_OUT_SPEAKER) == 0) {
539                LOGW("Hardware does not support requested route combination (%#X),"
540                     " picking closest possible route...", outputDevices);
541            }
542        }
543
544        if (outputDevices & AudioSystem::DEVICE_OUT_TTY) {
545                LOGI("Routing audio to TTY\n");
546                sndDevice = SND_DEVICE_TTY_FULL;
547        } else if (outputDevices &
548                   (AudioSystem::DEVICE_OUT_BLUETOOTH_SCO | AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_HEADSET)) {
549            LOGI("Routing audio to Bluetooth PCM\n");
550            sndDevice = SND_DEVICE_BT;
551        } else if (outputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_CARKIT) {
552            LOGI("Routing audio to Bluetooth PCM\n");
553            sndDevice = SND_DEVICE_CARKIT;
554        } else if ((outputDevices & AudioSystem::DEVICE_OUT_WIRED_HEADSET) &&
555                   (outputDevices & AudioSystem::DEVICE_OUT_SPEAKER)) {
556            LOGI("Routing audio to Wired Headset and Speaker\n");
557            sndDevice = SND_DEVICE_HEADSET_AND_SPEAKER;
558            audProcess = (ADRC_ENABLE | EQ_ENABLE | RX_IIR_ENABLE);
559        } else if (outputDevices & AudioSystem::DEVICE_OUT_FM_SPEAKER) {
560            LOGI("Routing audio to FM Speakerphone (%d,%x)\n", mMode, outputDevices);
561            sndDevice = SND_DEVICE_FM_SPEAKER;
562            audProcess = (ADRC_ENABLE | EQ_ENABLE | RX_IIR_DISABLE);
563        } else if (outputDevices & AudioSystem::DEVICE_OUT_FM_HEADPHONE) {
564            if (outputDevices & AudioSystem::DEVICE_OUT_SPEAKER) {
565                LOGI("Routing audio to FM Headset and Speaker (%d,%x)\n", mMode, outputDevices);
566                sndDevice = SND_DEVICE_HEADSET_AND_SPEAKER;
567                audProcess = (ADRC_ENABLE | EQ_ENABLE | RX_IIR_ENABLE);
568            } else {
569                LOGI("Routing audio to FM Headset (%d,%x)\n", mMode, outputDevices);
570                sndDevice = SND_DEVICE_FM_HEADSET;
571            }
572        } else if (outputDevices & AudioSystem::DEVICE_OUT_WIRED_HEADPHONE) {
573            if (outputDevices & AudioSystem::DEVICE_OUT_SPEAKER) {
574                LOGI("Routing audio to No microphone Wired Headset and Speaker (%d,%x)\n", mMode, outputDevices);
575                sndDevice = SND_DEVICE_HEADSET_AND_SPEAKER;
576                audProcess = (ADRC_ENABLE | EQ_ENABLE | RX_IIR_ENABLE);
577            } else {
578                LOGI("Routing audio to No microphone Wired Headset (%d,%x)\n", mMode, outputDevices);
579                sndDevice = SND_DEVICE_NO_MIC_HEADSET;
580            }
581        } else if (outputDevices & AudioSystem::DEVICE_OUT_WIRED_HEADSET) {
582            LOGI("Routing audio to Wired Headset\n");
583            sndDevice = SND_DEVICE_HEADSET;
584        } else if (outputDevices & AudioSystem::DEVICE_OUT_SPEAKER) {
585            LOGI("Routing audio to Speakerphone\n");
586            sndDevice = SND_DEVICE_SPEAKER;
587            audProcess = (ADRC_ENABLE | EQ_ENABLE | RX_IIR_ENABLE);
588        } else {
589            LOGI("Routing audio to Handset\n");
590            sndDevice = SND_DEVICE_HANDSET;
591        }
592    }
593
594    if (sndDevice != -1 && sndDevice != mCurSndDevice) {
595        ret = doAudioRouteOrMute(sndDevice);
596        if ((*msm72xx_enable_audpp) == 0 ) {
597            LOGE("Could not open msm72xx_enable_audpp()");
598        } else {
599            msm72xx_enable_audpp(audProcess);
600        }
601        mCurSndDevice = sndDevice;
602    }
603
604    return ret;
605}
606
607status_t AudioHardware::checkMicMute()
608{
609    Mutex::Autolock lock(mLock);
610    if (mMode != AudioSystem::MODE_IN_CALL) {
611        setMicMute_nosync(true);
612    }
613
614    return NO_ERROR;
615}
616
617status_t AudioHardware::dumpInternals(int fd, const Vector<String16>& args)
618{
619    const size_t SIZE = 256;
620    char buffer[SIZE];
621    String8 result;
622    result.append("AudioHardware::dumpInternals\n");
623    snprintf(buffer, SIZE, "\tmInit: %s\n", mInit? "true": "false");
624    result.append(buffer);
625    snprintf(buffer, SIZE, "\tmMicMute: %s\n", mMicMute? "true": "false");
626    result.append(buffer);
627    snprintf(buffer, SIZE, "\tmBluetoothNrec: %s\n", mBluetoothNrec? "true": "false");
628    result.append(buffer);
629    snprintf(buffer, SIZE, "\tmBluetoothId: %d\n", mBluetoothId);
630    result.append(buffer);
631    ::write(fd, result.string(), result.size());
632    return NO_ERROR;
633}
634
635status_t AudioHardware::dump(int fd, const Vector<String16>& args)
636{
637    dumpInternals(fd, args);
638    for (size_t index = 0; index < mInputs.size(); index++) {
639        mInputs[index]->dump(fd, args);
640    }
641
642    if (mOutput) {
643        mOutput->dump(fd, args);
644    }
645    return NO_ERROR;
646}
647
648uint32_t AudioHardware::getInputSampleRate(uint32_t sampleRate)
649{
650    uint32_t i;
651    uint32_t prevDelta;
652    uint32_t delta;
653
654    for (i = 0, prevDelta = 0xFFFFFFFF; i < sizeof(inputSamplingRates)/sizeof(uint32_t); i++, prevDelta = delta) {
655        delta = abs(sampleRate - inputSamplingRates[i]);
656        if (delta > prevDelta) break;
657    }
658    // i is always > 0 here
659    return inputSamplingRates[i-1];
660}
661
662// ----------------------------------------------------------------------------
663
664AudioHardware::AudioStreamOutMSM72xx::AudioStreamOutMSM72xx() :
665    mHardware(0), mFd(-1), mStartCount(0), mRetryCount(0), mStandby(true), mDevices(0)
666{
667}
668
669status_t AudioHardware::AudioStreamOutMSM72xx::set(
670        AudioHardware* hw, uint32_t devices, int *pFormat, uint32_t *pChannels, uint32_t *pRate)
671{
672    int lFormat = pFormat ? *pFormat : 0;
673    uint32_t lChannels = pChannels ? *pChannels : 0;
674    uint32_t lRate = pRate ? *pRate : 0;
675
676    mHardware = hw;
677
678    // fix up defaults
679    if (lFormat == 0) lFormat = format();
680    if (lChannels == 0) lChannels = channels();
681    if (lRate == 0) lRate = sampleRate();
682
683    // check values
684    if ((lFormat != format()) ||
685        (lChannels != channels()) ||
686        (lRate != sampleRate())) {
687        if (pFormat) *pFormat = format();
688        if (pChannels) *pChannels = channels();
689        if (pRate) *pRate = sampleRate();
690        return BAD_VALUE;
691    }
692
693    if (pFormat) *pFormat = lFormat;
694    if (pChannels) *pChannels = lChannels;
695    if (pRate) *pRate = lRate;
696
697    mDevices = devices;
698
699    return NO_ERROR;
700}
701
702AudioHardware::AudioStreamOutMSM72xx::~AudioStreamOutMSM72xx()
703{
704    if (mFd > 0) close(mFd);
705}
706
707ssize_t AudioHardware::AudioStreamOutMSM72xx::write(const void* buffer, size_t bytes)
708{
709    // LOGD("AudioStreamOutMSM72xx::write(%p, %u)", buffer, bytes);
710    status_t status = NO_INIT;
711    size_t count = bytes;
712    const uint8_t* p = static_cast<const uint8_t*>(buffer);
713
714    if (mStandby) {
715
716        // open driver
717        LOGV("open driver");
718        status = ::open("/dev/msm_pcm_out", O_RDWR);
719        if (status < 0) {
720            LOGE("Cannot open /dev/msm_pcm_out errno: %d", errno);
721            goto Error;
722        }
723        mFd = status;
724
725        // configuration
726        LOGV("get config");
727        struct msm_audio_config config;
728        status = ioctl(mFd, AUDIO_GET_CONFIG, &config);
729        if (status < 0) {
730            LOGE("Cannot read config");
731            goto Error;
732        }
733
734        LOGV("set config");
735        config.channel_count = AudioSystem::popCount(channels());
736        config.sample_rate = sampleRate();
737        config.buffer_size = bufferSize();
738        config.buffer_count = AUDIO_HW_NUM_OUT_BUF;
739        config.codec_type = CODEC_TYPE_PCM;
740        status = ioctl(mFd, AUDIO_SET_CONFIG, &config);
741        if (status < 0) {
742            LOGE("Cannot set config");
743            goto Error;
744        }
745
746        LOGV("buffer_size: %u", config.buffer_size);
747        LOGV("buffer_count: %u", config.buffer_count);
748        LOGV("channel_count: %u", config.channel_count);
749        LOGV("sample_rate: %u", config.sample_rate);
750
751        // fill 2 buffers before AUDIO_START
752        mStartCount = AUDIO_HW_NUM_OUT_BUF;
753        mStandby = false;
754    }
755
756    while (count) {
757        ssize_t written = ::write(mFd, p, count);
758        if (written >= 0) {
759            count -= written;
760            p += written;
761        } else {
762            if (errno != EAGAIN) return written;
763            mRetryCount++;
764            LOGW("EAGAIN - retry");
765        }
766    }
767
768    // start audio after we fill 2 buffers
769    if (mStartCount) {
770        if (--mStartCount == 0) {
771            ioctl(mFd, AUDIO_START, 0);
772        }
773    }
774    return bytes;
775
776Error:
777    if (mFd > 0) {
778        ::close(mFd);
779        mFd = -1;
780    }
781    // Simulate audio output timing in case of error
782    usleep(bytes * 1000000 / frameSize() / sampleRate());
783
784    return status;
785}
786
787status_t AudioHardware::AudioStreamOutMSM72xx::standby()
788{
789    status_t status = NO_ERROR;
790    if (!mStandby && mFd > 0) {
791        ::close(mFd);
792        mFd = -1;
793    }
794    mStandby = true;
795    return status;
796}
797
798status_t AudioHardware::AudioStreamOutMSM72xx::dump(int fd, const Vector<String16>& args)
799{
800    const size_t SIZE = 256;
801    char buffer[SIZE];
802    String8 result;
803    result.append("AudioStreamOutMSM72xx::dump\n");
804    snprintf(buffer, SIZE, "\tsample rate: %d\n", sampleRate());
805    result.append(buffer);
806    snprintf(buffer, SIZE, "\tbuffer size: %d\n", bufferSize());
807    result.append(buffer);
808    snprintf(buffer, SIZE, "\tchannels: %d\n", channels());
809    result.append(buffer);
810    snprintf(buffer, SIZE, "\tformat: %d\n", format());
811    result.append(buffer);
812    snprintf(buffer, SIZE, "\tmHardware: %p\n", mHardware);
813    result.append(buffer);
814    snprintf(buffer, SIZE, "\tmFd: %d\n", mFd);
815    result.append(buffer);
816    snprintf(buffer, SIZE, "\tmStartCount: %d\n", mStartCount);
817    result.append(buffer);
818    snprintf(buffer, SIZE, "\tmRetryCount: %d\n", mRetryCount);
819    result.append(buffer);
820    snprintf(buffer, SIZE, "\tmStandby: %s\n", mStandby? "true": "false");
821    result.append(buffer);
822    ::write(fd, result.string(), result.size());
823    return NO_ERROR;
824}
825
826bool AudioHardware::AudioStreamOutMSM72xx::checkStandby()
827{
828    return mStandby;
829}
830
831
832status_t AudioHardware::AudioStreamOutMSM72xx::setParameters(const String8& keyValuePairs)
833{
834    AudioParameter param = AudioParameter(keyValuePairs);
835    String8 key = String8(AudioParameter::keyRouting);
836    status_t status = NO_ERROR;
837    int device;
838    LOGV("AudioStreamOutMSM72xx::setParameters() %s", keyValuePairs.string());
839
840    if (param.getInt(key, device) == NO_ERROR) {
841        mDevices = device;
842        LOGV("set output routing %x", mDevices);
843        status = mHardware->doRouting(NULL);
844        param.remove(key);
845    }
846
847    if (param.size()) {
848        status = BAD_VALUE;
849    }
850    return status;
851}
852
853String8 AudioHardware::AudioStreamOutMSM72xx::getParameters(const String8& keys)
854{
855    AudioParameter param = AudioParameter(keys);
856    String8 value;
857    String8 key = String8(AudioParameter::keyRouting);
858
859    if (param.get(key, value) == NO_ERROR) {
860        LOGV("get routing %x", mDevices);
861        param.addInt(key, (int)mDevices);
862    }
863
864    LOGV("AudioStreamOutMSM72xx::getParameters() %s", param.toString().string());
865    return param.toString();
866}
867
868
869// ----------------------------------------------------------------------------
870
871AudioHardware::AudioStreamInMSM72xx::AudioStreamInMSM72xx() :
872    mHardware(0), mFd(-1), mState(AUDIO_INPUT_CLOSED), mRetryCount(0),
873    mFormat(AUDIO_HW_IN_FORMAT), mChannels(AUDIO_HW_IN_CHANNELS),
874    mSampleRate(AUDIO_HW_IN_SAMPLERATE), mBufferSize(AUDIO_HW_IN_BUFFERSIZE),
875    mAcoustics((AudioSystem::audio_in_acoustics)0), mDevices(0)
876{
877}
878
879status_t AudioHardware::AudioStreamInMSM72xx::set(
880        AudioHardware* hw, uint32_t devices, int *pFormat, uint32_t *pChannels, uint32_t *pRate,
881        AudioSystem::audio_in_acoustics acoustic_flags)
882{
883    if (pFormat == 0 || *pFormat != AUDIO_HW_IN_FORMAT) {
884        *pFormat = AUDIO_HW_IN_FORMAT;
885        return BAD_VALUE;
886    }
887    if (pRate == 0) {
888        return BAD_VALUE;
889    }
890    uint32_t rate = hw->getInputSampleRate(*pRate);
891    if (rate != *pRate) {
892        *pRate = rate;
893        return BAD_VALUE;
894    }
895
896    if (pChannels == 0 || (*pChannels != AudioSystem::CHANNEL_IN_MONO &&
897        *pChannels != AudioSystem::CHANNEL_IN_STEREO)) {
898        *pChannels = AUDIO_HW_IN_CHANNELS;
899        return BAD_VALUE;
900    }
901
902    mHardware = hw;
903
904    LOGV("AudioStreamInMSM72xx::set(%d, %d, %u)", *pFormat, *pChannels, *pRate);
905    if (mFd >= 0) {
906        LOGE("Audio record already open");
907        return -EPERM;
908    }
909
910    // open audio input device
911    status_t status = ::open("/dev/msm_pcm_in", O_RDWR);
912    if (status < 0) {
913        LOGE("Cannot open /dev/msm_pcm_in errno: %d", errno);
914        goto Error;
915    }
916    mFd = status;
917
918    // configuration
919    LOGV("get config");
920    struct msm_audio_config config;
921    status = ioctl(mFd, AUDIO_GET_CONFIG, &config);
922    if (status < 0) {
923        LOGE("Cannot read config");
924        goto Error;
925    }
926
927    LOGV("set config");
928    config.channel_count = AudioSystem::popCount(*pChannels);
929    config.sample_rate = *pRate;
930    config.buffer_size = bufferSize();
931    config.buffer_count = 2;
932    config.codec_type = CODEC_TYPE_PCM;
933    status = ioctl(mFd, AUDIO_SET_CONFIG, &config);
934    if (status < 0) {
935        LOGE("Cannot set config");
936        if (ioctl(mFd, AUDIO_GET_CONFIG, &config) == 0) {
937            if (config.channel_count == 1) {
938                *pChannels = AudioSystem::CHANNEL_IN_MONO;
939            } else {
940                *pChannels = AudioSystem::CHANNEL_IN_STEREO;
941            }
942            *pRate = config.sample_rate;
943        }
944        goto Error;
945    }
946
947    LOGV("confirm config");
948    status = ioctl(mFd, AUDIO_GET_CONFIG, &config);
949    if (status < 0) {
950        LOGE("Cannot read config");
951        goto Error;
952    }
953    LOGV("buffer_size: %u", config.buffer_size);
954    LOGV("buffer_count: %u", config.buffer_count);
955    LOGV("channel_count: %u", config.channel_count);
956    LOGV("sample_rate: %u", config.sample_rate);
957
958    mDevices = devices;
959    mFormat = AUDIO_HW_IN_FORMAT;
960    mChannels = *pChannels;
961    mSampleRate = config.sample_rate;
962    mBufferSize = config.buffer_size;
963
964    //mHardware->setMicMute_nosync(false);
965    mState = AUDIO_INPUT_OPENED;
966
967    if (!acoustic)
968        return NO_ERROR;
969
970    audpre_index = calculate_audpre_table_index(mSampleRate);
971    tx_iir_index = (audpre_index * 2) + (hw->checkOutputStandby() ? 0 : 1);
972    LOGD("audpre_index = %d, tx_iir_index = %d\n", audpre_index, tx_iir_index);
973
974    /**
975     * If audio-preprocessing failed, we should not block record.
976     */
977    int (*msm72xx_set_audpre_params)(int, int);
978    msm72xx_set_audpre_params = (int (*)(int, int))::dlsym(acoustic, "msm72xx_set_audpre_params");
979    status = msm72xx_set_audpre_params(audpre_index, tx_iir_index);
980    if (status < 0)
981        LOGE("Cannot set audpre parameters");
982
983    int (*msm72xx_enable_audpre)(int, int, int);
984    msm72xx_enable_audpre = (int (*)(int, int, int))::dlsym(acoustic, "msm72xx_enable_audpre");
985    mAcoustics = acoustic_flags;
986    status = msm72xx_enable_audpre((int)acoustic_flags, audpre_index, tx_iir_index);
987    if (status < 0)
988        LOGE("Cannot enable audpre");
989
990    return NO_ERROR;
991
992Error:
993    if (mFd > 0) {
994        ::close(mFd);
995        mFd = -1;
996    }
997    return status;
998}
999
1000AudioHardware::AudioStreamInMSM72xx::~AudioStreamInMSM72xx()
1001{
1002    LOGV("AudioStreamInMSM72xx destructor");
1003    standby();
1004}
1005
1006ssize_t AudioHardware::AudioStreamInMSM72xx::read( void* buffer, ssize_t bytes)
1007{
1008    LOGV("AudioStreamInMSM72xx::read(%p, %ld)", buffer, bytes);
1009    if (!mHardware) return -1;
1010
1011    size_t count = bytes;
1012    uint8_t* p = static_cast<uint8_t*>(buffer);
1013
1014    if (mState < AUDIO_INPUT_OPENED) {
1015        Mutex::Autolock lock(mHardware->mLock);
1016        if (set(mHardware, mDevices, &mFormat, &mChannels, &mSampleRate, mAcoustics) != NO_ERROR) {
1017            return -1;
1018        }
1019    }
1020
1021    if (mState < AUDIO_INPUT_STARTED) {
1022        if (ioctl(mFd, AUDIO_START, 0)) {
1023            LOGE("Error starting record");
1024            return -1;
1025        }
1026        mState = AUDIO_INPUT_STARTED;
1027    }
1028
1029    while (count) {
1030        ssize_t bytesRead = ::read(mFd, buffer, count);
1031        if (bytesRead >= 0) {
1032            count -= bytesRead;
1033            p += bytesRead;
1034        } else {
1035            if (errno != EAGAIN) return bytesRead;
1036            mRetryCount++;
1037            LOGW("EAGAIN - retrying");
1038        }
1039    }
1040    return bytes;
1041}
1042
1043status_t AudioHardware::AudioStreamInMSM72xx::standby()
1044{
1045    if (!mHardware) return -1;
1046    if (mState > AUDIO_INPUT_CLOSED) {
1047        if (mFd > 0) {
1048            ::close(mFd);
1049            mFd = -1;
1050        }
1051        //mHardware->checkMicMute();
1052        mState = AUDIO_INPUT_CLOSED;
1053    }
1054    return NO_ERROR;
1055}
1056
1057status_t AudioHardware::AudioStreamInMSM72xx::dump(int fd, const Vector<String16>& args)
1058{
1059    const size_t SIZE = 256;
1060    char buffer[SIZE];
1061    String8 result;
1062    result.append("AudioStreamInMSM72xx::dump\n");
1063    snprintf(buffer, SIZE, "\tsample rate: %d\n", sampleRate());
1064    result.append(buffer);
1065    snprintf(buffer, SIZE, "\tbuffer size: %d\n", bufferSize());
1066    result.append(buffer);
1067    snprintf(buffer, SIZE, "\tchannels: %d\n", channels());
1068    result.append(buffer);
1069    snprintf(buffer, SIZE, "\tformat: %d\n", format());
1070    result.append(buffer);
1071    snprintf(buffer, SIZE, "\tmHardware: %p\n", mHardware);
1072    result.append(buffer);
1073    snprintf(buffer, SIZE, "\tmFd count: %d\n", mFd);
1074    result.append(buffer);
1075    snprintf(buffer, SIZE, "\tmState: %d\n", mState);
1076    result.append(buffer);
1077    snprintf(buffer, SIZE, "\tmRetryCount: %d\n", mRetryCount);
1078    result.append(buffer);
1079    ::write(fd, result.string(), result.size());
1080    return NO_ERROR;
1081}
1082
1083status_t AudioHardware::AudioStreamInMSM72xx::setParameters(const String8& keyValuePairs)
1084{
1085    AudioParameter param = AudioParameter(keyValuePairs);
1086    String8 key = String8(AudioParameter::keyRouting);
1087    status_t status = NO_ERROR;
1088    int device;
1089    LOGV("AudioStreamInMSM72xx::setParameters() %s", keyValuePairs.string());
1090
1091    if (param.getInt(key, device) == NO_ERROR) {
1092        LOGV("set input routing %x", device);
1093        if (device & (device - 1)) {
1094            status = BAD_VALUE;
1095        } else {
1096            mDevices = device;
1097            status = mHardware->doRouting(this);
1098        }
1099        param.remove(key);
1100    }
1101
1102    if (param.size()) {
1103        status = BAD_VALUE;
1104    }
1105    return status;
1106}
1107
1108String8 AudioHardware::AudioStreamInMSM72xx::getParameters(const String8& keys)
1109{
1110    AudioParameter param = AudioParameter(keys);
1111    String8 value;
1112    String8 key = String8(AudioParameter::keyRouting);
1113
1114    if (param.get(key, value) == NO_ERROR) {
1115        LOGV("get routing %x", mDevices);
1116        param.addInt(key, (int)mDevices);
1117    }
1118
1119    LOGV("AudioStreamInMSM72xx::getParameters() %s", param.toString().string());
1120    return param.toString();
1121}
1122
1123// ----------------------------------------------------------------------------
1124
1125extern "C" AudioHardwareInterface* createAudioHardware(void) {
1126    return new AudioHardware();
1127}
1128
1129}; // namespace android
1130