/
AudioHardware.cpp
2520 lines (2167 loc) · 77.1 KB
/
AudioHardware.cpp
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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
** Copyright 2010, 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.
*/
#include <math.h>
#define LOG_NDEBUG 0
#define LOG_TAG "AudioHardware"
#include <utils/Log.h>
#include <utils/String8.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/resource.h>
#include <dlfcn.h>
#include <fcntl.h>
#include "AudioHardware.h"
#include <audio_effects/effect_aec.h>
#include <hardware_legacy/power.h>
extern "C" {
#include <tinyalsa/asoundlib.h>
}
#ifdef HAVE_FM_RADIO
#define Si4709_IOC_MAGIC 0xFA
#define Si4709_IOC_VOLUME_SET _IOW(Si4709_IOC_MAGIC, 15, __u8)
#endif
#ifdef USES_FROYO_RILCLIENT
#define OEM_FUNC_SOUND 0x08
#define OEM_SND_SET_VOLUME_CTRL 0x03
#define OEM_SND_SET_AUDIO_PATH 0x05
#define OEM_SND_SET_CLOCK_CTRL 0x0A
#define OEM_SND_TYPE_VOICE 0x01
#define OEM_SND_TYPE_SPEAKER 0x11
#define OEM_SND_TYPE_HEADSET 0x31
#define OEM_SND_TYPE_BTVOICE 0x41
#define OEM_SND_AUDIO_PATH_HANDSET 0x01
#define OEM_SND_AUDIO_PATH_HEADSET 0x02
#define OEM_SND_AUDIO_PATH_BLUETOOTH 0x04
#define OEM_SND_AUDIO_PATH_STEREO_BLUETOOTH 0x05
#define OEM_SND_AUDIO_PATH_SPEAKER 0x06
#define OEM_SND_AUDIO_PATH_HEADPHONE 0x07
#define OEM_SND_AUDIO_PATH_BT_NSEC_OFF 0x08
#endif
namespace android_audio_legacy {
const uint32_t AudioHardware::inputConfigTable[][AudioHardware::INPUT_CONFIG_CNT] = {
{8000, 4},
{11025, 4},
{16000, 2},
{22050, 2},
{32000, 1},
{44100, 1}
};
// trace driver operations for dump
//
#define DRIVER_TRACE
enum {
DRV_NONE,
DRV_PCM_OPEN,
DRV_PCM_CLOSE,
DRV_PCM_WRITE,
DRV_PCM_READ,
DRV_MIXER_OPEN,
DRV_MIXER_CLOSE,
DRV_MIXER_GET,
DRV_MIXER_SEL
};
#ifdef DRIVER_TRACE
#define TRACE_DRIVER_IN(op) mDriverOp = op;
#define TRACE_DRIVER_OUT mDriverOp = DRV_NONE;
#else
#define TRACE_DRIVER_IN(op)
#define TRACE_DRIVER_OUT
#endif
// ----------------------------------------------------------------------------
const char *AudioHardware::inputPathNameDefault = "Default";
const char *AudioHardware::inputPathNameCamcorder = "Camcorder";
const char *AudioHardware::inputPathNameVoiceRecognition = "Voice Recognition";
const char *AudioHardware::inputPathNameVoiceCommunication = "Voice Communication";
AudioHardware::AudioHardware() :
mInit(false),
mMicMute(false),
mPcm(NULL),
mMixer(NULL),
mPcmOpenCnt(0),
mMixerOpenCnt(0),
mInCallAudioMode(false),
mVoiceVol(1.0f),
mInputSource(AUDIO_SOURCE_DEFAULT),
mBluetoothNrec(true),
mTTYMode(TTY_MODE_OFF),
mSecRilLibHandle(NULL),
mRilClient(0),
mActivatedCP(false),
mEchoReference(NULL),
#ifdef HAVE_FM_RADIO
mFmFd(-1),
mFmVolume(1),
mFmResumeAfterCall(false),
#endif
mDriverOp(DRV_NONE)
{
loadRILD();
mInit = true;
}
AudioHardware::~AudioHardware()
{
for (size_t index = 0; index < mInputs.size(); index++) {
closeInputStream(mInputs[index].get());
}
mInputs.clear();
closeOutputStream((AudioStreamOut*)mOutput.get());
if (mMixer) {
TRACE_DRIVER_IN(DRV_MIXER_CLOSE)
mixer_close(mMixer);
TRACE_DRIVER_OUT
}
if (mPcm) {
TRACE_DRIVER_IN(DRV_PCM_CLOSE)
pcm_close(mPcm);
TRACE_DRIVER_OUT
}
if (mSecRilLibHandle) {
if (disconnectRILD(mRilClient) != RIL_CLIENT_ERR_SUCCESS)
ALOGE("Disconnect_RILD() error");
if (closeClientRILD(mRilClient) != RIL_CLIENT_ERR_SUCCESS)
ALOGE("CloseClient_RILD() error");
mRilClient = 0;
dlclose(mSecRilLibHandle);
mSecRilLibHandle = NULL;
}
mInit = false;
}
status_t AudioHardware::initCheck()
{
return mInit ? NO_ERROR : NO_INIT;
}
void AudioHardware::loadRILD(void)
{
mSecRilLibHandle = dlopen("libsecril-client.so", RTLD_NOW);
if (mSecRilLibHandle) {
ALOGV("libsecril-client.so is loaded");
openClientRILD = (HRilClient (*)(void))
dlsym(mSecRilLibHandle, "OpenClient_RILD");
disconnectRILD = (int (*)(HRilClient))
dlsym(mSecRilLibHandle, "Disconnect_RILD");
closeClientRILD = (int (*)(HRilClient))
dlsym(mSecRilLibHandle, "CloseClient_RILD");
isConnectedRILD = (int (*)(HRilClient))
dlsym(mSecRilLibHandle, "isConnected_RILD");
connectRILD = (int (*)(HRilClient))
dlsym(mSecRilLibHandle, "Connect_RILD");
#ifdef USES_FROYO_RILCLIENT
invokeOemRequestHookRaw = (int (*)(HRilClient client, char *data, size_t len))
dlsym(mSecRilLibHandle, "InvokeOemRequestHookRaw");
bool rilFunctionsLoaded = invokeOemRequestHookRaw;
#else
setCallVolume = (int (*)(HRilClient, SoundType, int))
dlsym(mSecRilLibHandle, "SetCallVolume");
setCallAudioPath = (int (*)(HRilClient, AudioPath))
dlsym(mSecRilLibHandle, "SetCallAudioPath");
setCallClockSync = (int (*)(HRilClient, SoundClockCondition))
dlsym(mSecRilLibHandle, "SetCallClockSync");
bool rilFunctionsLoaded = setCallVolume && setCallAudioPath && setCallClockSync;
#endif
if (!openClientRILD || !disconnectRILD || !closeClientRILD ||
!isConnectedRILD || !connectRILD ||
!rilFunctionsLoaded) {
ALOGE("Can't load all functions from libsecril-client.so");
dlclose(mSecRilLibHandle);
mSecRilLibHandle = NULL;
} else {
mRilClient = openClientRILD();
if (!mRilClient) {
ALOGE("OpenClient_RILD() error");
dlclose(mSecRilLibHandle);
mSecRilLibHandle = NULL;
}
}
} else {
ALOGE("Can't load libsecril-client.so");
}
}
status_t AudioHardware::connectRILDIfRequired(void)
{
if (!mSecRilLibHandle) {
ALOGE("connectIfRequired() lib is not loaded");
return INVALID_OPERATION;
}
if (isConnectedRILD(mRilClient)) {
return OK;
}
if (connectRILD(mRilClient) != RIL_CLIENT_ERR_SUCCESS) {
ALOGE("Connect_RILD() error");
return INVALID_OPERATION;
}
return OK;
}
#ifdef USES_FROYO_RILCLIENT
int AudioHardware::convertSoundType(SoundType type) {
switch (type) {
case SOUND_TYPE_VOICE: return OEM_SND_TYPE_VOICE;
case SOUND_TYPE_SPEAKER: return OEM_SND_TYPE_SPEAKER;
case SOUND_TYPE_HEADSET: return OEM_SND_TYPE_HEADSET;
case SOUND_TYPE_BTVOICE: return OEM_SND_TYPE_BTVOICE;
default:
ALOGE("convertSoundType: unknown type! %d", type);
return OEM_SND_TYPE_VOICE;
}
}
int AudioHardware::convertAudioPath(AudioPath path) {
switch (path) {
case SOUND_AUDIO_PATH_HANDSET: return OEM_SND_AUDIO_PATH_HANDSET;
case SOUND_AUDIO_PATH_SPEAKER: return OEM_SND_AUDIO_PATH_SPEAKER;
case SOUND_AUDIO_PATH_BLUETOOTH: return OEM_SND_AUDIO_PATH_BLUETOOTH;
case SOUND_AUDIO_PATH_BLUETOOTH_NO_NR: return OEM_SND_AUDIO_PATH_BT_NSEC_OFF;
case SOUND_AUDIO_PATH_HEADPHONE: return OEM_SND_AUDIO_PATH_HEADPHONE;
case SOUND_AUDIO_PATH_HEADSET: return OEM_SND_AUDIO_PATH_HEADSET;
default:
ALOGE("convertAudioPath: unknown path! %d", path);
return OEM_SND_AUDIO_PATH_HANDSET;
}
}
int AudioHardware::setCallVolume(HRilClient client, SoundType type, int vol) {
char data[6] = {0,};
data[0] = OEM_FUNC_SOUND;
data[1] = OEM_SND_SET_VOLUME_CTRL;
data[2] = 0x00;
data[3] = 0x06;
data[4] = convertSoundType(type);
data[5] = vol;
return invokeOemRequestHookRaw(client, data, sizeof(data));
}
int AudioHardware::setCallAudioPath(HRilClient client, AudioPath path) {
char data[5] = {0,};
data[0] = OEM_FUNC_SOUND;
data[1] = OEM_SND_SET_AUDIO_PATH;
data[2] = 0x00;
data[3] = 0x05;
data[4] = convertAudioPath(path);
return invokeOemRequestHookRaw(client, data, sizeof(data));
}
int AudioHardware::setCallClockSync(HRilClient client, SoundClockCondition condition) {
char data[5] = {0,};
data[0] = OEM_FUNC_SOUND;
data[1] = OEM_SND_SET_CLOCK_CTRL;
data[2] = 0x00;
data[3] = 0x05;
data[4] = condition;
return invokeOemRequestHookRaw(client, data, sizeof(data));
}
#endif
AudioStreamOut* AudioHardware::openOutputStream(
uint32_t devices, int *format, uint32_t *channels,
uint32_t *sampleRate, status_t *status)
{
sp <AudioStreamOutALSA> out;
status_t rc;
{ // scope for the lock
Mutex::Autolock lock(mLock);
// only one output stream allowed
if (mOutput != 0) {
if (status) {
*status = INVALID_OPERATION;
}
return NULL;
}
out = new AudioStreamOutALSA();
rc = out->set(this, devices, format, channels, sampleRate);
if (rc == NO_ERROR) {
mOutput = out;
}
}
if (rc != NO_ERROR) {
if (out != 0) {
out.clear();
}
}
if (status) {
*status = rc;
}
return out.get();
}
void AudioHardware::closeOutputStream(AudioStreamOut* out) {
sp <AudioStreamOutALSA> spOut;
sp<AudioStreamInALSA> spIn;
{
Mutex::Autolock lock(mLock);
if (mOutput == 0 || mOutput.get() != out) {
ALOGW("Attempt to close invalid output stream");
return;
}
spOut = mOutput;
mOutput.clear();
if (mEchoReference != NULL) {
spIn = getActiveInput_l();
}
}
if (spIn != 0) {
// this will safely release the echo reference by calling releaseEchoReference()
// after placing the active input in standby
spIn->standby();
}
spOut.clear();
}
AudioStreamIn* AudioHardware::openInputStream(
uint32_t devices, int *format, uint32_t *channels,
uint32_t *sampleRate, status_t *status,
AudioSystem::audio_in_acoustics acoustic_flags)
{
// check for valid input source
if (!AudioSystem::isInputDevice((AudioSystem::audio_devices)devices)) {
if (status) {
*status = BAD_VALUE;
}
return NULL;
}
status_t rc = NO_ERROR;
sp <AudioStreamInALSA> in;
{ // scope for the lock
Mutex::Autolock lock(mLock);
in = new AudioStreamInALSA();
rc = in->set(this, devices, format, channels, sampleRate, acoustic_flags);
if (rc == NO_ERROR) {
mInputs.add(in);
}
}
if (rc != NO_ERROR) {
if (in != 0) {
in.clear();
}
}
if (status) {
*status = rc;
}
ALOGV("AudioHardware::openInputStream()%p", in.get());
return in.get();
}
void AudioHardware::closeInputStream(AudioStreamIn* in) {
sp<AudioStreamInALSA> spIn;
{
Mutex::Autolock lock(mLock);
ssize_t index = mInputs.indexOf((AudioStreamInALSA *)in);
if (index < 0) {
ALOGW("Attempt to close invalid input stream");
return;
}
spIn = mInputs[index];
mInputs.removeAt(index);
}
ALOGV("AudioHardware::closeInputStream()%p", in);
spIn.clear();
}
status_t AudioHardware::setMode(int mode)
{
sp<AudioStreamOutALSA> spOut;
sp<AudioStreamInALSA> spIn;
status_t status;
// Mutex acquisition order is always out -> in -> hw
AutoMutex lock(mLock);
spOut = mOutput;
while (spOut != 0) {
if (!spOut->checkStandby()) {
int cnt = spOut->prepareLock();
mLock.unlock();
spOut->lock();
mLock.lock();
// make sure that another thread did not change output state while the
// mutex is released
if ((spOut == mOutput) && (cnt == spOut->standbyCnt())) {
break;
}
spOut->unlock();
spOut = mOutput;
} else {
spOut.clear();
}
}
// spOut is not 0 here only if the output is active
spIn = getActiveInput_l();
while (spIn != 0) {
int cnt = spIn->prepareLock();
mLock.unlock();
spIn->lock();
mLock.lock();
// make sure that another thread did not change input state while the
// mutex is released
if ((spIn == getActiveInput_l()) && (cnt == spIn->standbyCnt())) {
break;
}
spIn->unlock();
spIn = getActiveInput_l();
}
// spIn is not 0 here only if the input is active
int prevMode = mMode;
status = AudioHardwareBase::setMode(mode);
ALOGV("setMode() : new %d, old %d", mMode, prevMode);
if (status == NO_ERROR) {
// activate call clock in radio when entering in call mode
if (mMode == AudioSystem::MODE_IN_CALL)
{
if ((!mActivatedCP) && (mSecRilLibHandle) && (connectRILDIfRequired() == OK)) {
setCallClockSync(mRilClient, SOUND_CLOCK_START);
mActivatedCP = true;
}
}
if (mMode == AudioSystem::MODE_IN_CALL && !mInCallAudioMode) {
if (spOut != 0) {
ALOGV("setMode() in call force output standby");
spOut->doStandby_l();
}
if (spIn != 0) {
ALOGV("setMode() in call force input standby");
spIn->doStandby_l();
}
ALOGV("setMode() openPcmOut_l()");
openPcmOut_l();
openMixer_l();
setInputSource_l(AUDIO_SOURCE_DEFAULT);
setVoiceVolume_l(mVoiceVol);
mInCallAudioMode = true;
}
if (mMode == AudioSystem::MODE_NORMAL && mInCallAudioMode) {
setInputSource_l(mInputSource);
if (mMixer != NULL) {
TRACE_DRIVER_IN(DRV_MIXER_GET)
struct mixer_ctl *ctl= mixer_get_ctl_by_name(mMixer, "Playback Path");
TRACE_DRIVER_OUT
if (ctl != NULL) {
ALOGV("setMode() reset Playback Path to RCV");
TRACE_DRIVER_IN(DRV_MIXER_SEL)
mixer_ctl_set_enum_by_string(ctl, "RCV");
TRACE_DRIVER_OUT
}
}
ALOGV("setMode() closePcmOut_l()");
closeMixer_l();
closePcmOut_l();
if (spOut != 0) {
ALOGV("setMode() off call force output standby");
spOut->doStandby_l();
}
if (spIn != 0) {
ALOGV("setMode() off call force input standby");
spIn->doStandby_l();
}
mInCallAudioMode = false;
}
if (mMode == AudioSystem::MODE_NORMAL) {
if(mActivatedCP)
mActivatedCP = false;
}
}
if (spIn != 0) {
spIn->unlock();
}
if (spOut != 0) {
spOut->unlock();
}
#ifdef HAVE_FM_RADIO
if (mFmResumeAfterCall) {
mFmResumeAfterCall = false;
enableFMRadio();
}
#endif
return status;
}
status_t AudioHardware::setMicMute(bool state)
{
ALOGV("setMicMute(%d) mMicMute %d", state, mMicMute);
sp<AudioStreamInALSA> spIn;
{
AutoMutex lock(mLock);
if (mMicMute != state) {
mMicMute = state;
// in call mute is handled by RIL
if (mMode != AudioSystem::MODE_IN_CALL) {
spIn = getActiveInput_l();
}
}
}
if (spIn != 0) {
spIn->standby();
}
return NO_ERROR;
}
status_t AudioHardware::getMicMute(bool* state)
{
*state = mMicMute;
return NO_ERROR;
}
status_t AudioHardware::setParameters(const String8& keyValuePairs)
{
AudioParameter param = AudioParameter(keyValuePairs);
String8 value;
String8 key;
const char BT_NREC_KEY[] = "bt_headset_nrec";
const char BT_NREC_VALUE_ON[] = "on";
const char TTY_MODE_KEY[] = "tty_mode";
const char TTY_MODE_VALUE_OFF[] = "tty_off";
const char TTY_MODE_VALUE_VCO[] = "tty_vco";
const char TTY_MODE_VALUE_HCO[] = "tty_hco";
const char TTY_MODE_VALUE_FULL[] = "tty_full";
#ifdef HAVE_FM_RADIO
const char FM_RADIO_KEY_ON[] = "fm_on";
const char FM_RADIO_KEY_OFF[] = "fm_off";
const char FM_RADIO_VALUE_ON[] = "2055";
const char FM_RADIO_VALUE_OFF[] = "7";
#endif
key = String8(BT_NREC_KEY);
if (param.get(key, value) == NO_ERROR) {
if (value == BT_NREC_VALUE_ON) {
mBluetoothNrec = true;
} else {
mBluetoothNrec = false;
ALOGD("Turning noise reduction and echo cancellation off for BT "
"headset");
}
param.remove(String8(BT_NREC_KEY));
}
key = String8(TTY_MODE_KEY);
if (param.get(key, value) == NO_ERROR) {
int ttyMode;
if (value == TTY_MODE_VALUE_OFF) {
ttyMode = TTY_MODE_OFF;
} else if (value == TTY_MODE_VALUE_VCO) {
ttyMode = TTY_MODE_VCO;
} else if (value == TTY_MODE_VALUE_HCO) {
ttyMode = TTY_MODE_HCO;
} else if (value == TTY_MODE_VALUE_FULL) {
ttyMode = TTY_MODE_FULL;
} else {
return BAD_VALUE;
}
if (ttyMode != mTTYMode) {
ALOGV("new tty mode %d", ttyMode);
mTTYMode = ttyMode;
if (mOutput != 0 && mMode == AudioSystem::MODE_IN_CALL) {
setIncallPath_l(mOutput->device());
}
}
param.remove(String8(TTY_MODE_KEY));
}
#ifdef HAVE_FM_RADIO
// fm radio on
key = String8(AudioParameter::keyFmOn);
if (param.get(key, value) == NO_ERROR) {
enableFMRadio();
}
param.remove(key);
// fm radio off
key = String8(AudioParameter::keyFmOff);
if (param.get(key, value) == NO_ERROR) {
disableFMRadio();
}
param.remove(key);
#endif
return NO_ERROR;
}
String8 AudioHardware::getParameters(const String8& keys)
{
AudioParameter request = AudioParameter(keys);
AudioParameter reply = AudioParameter();
ALOGV("getParameters() %s", keys.string());
return reply.toString();
}
size_t AudioHardware::getInputBufferSize(uint32_t sampleRate, int format, int channelCount)
{
if (format != AudioSystem::PCM_16_BIT) {
ALOGW("getInputBufferSize bad format: %d", format);
return 0;
}
if (channelCount < 1 || channelCount > 2) {
ALOGW("getInputBufferSize bad channel count: %d", channelCount);
return 0;
}
if (sampleRate != getInputSampleRate(sampleRate)) {
ALOGW("getInputBufferSize bad sample rate: %d", sampleRate);
return 0;
}
return AudioStreamInALSA::getBufferSize(sampleRate, channelCount);
}
status_t AudioHardware::setVoiceVolume(float volume)
{
AutoMutex lock(mLock);
setVoiceVolume_l(volume);
return NO_ERROR;
}
void AudioHardware::setVoiceVolume_l(float volume)
{
ALOGD("### setVoiceVolume_l");
mVoiceVol = volume;
if ( (AudioSystem::MODE_IN_CALL == mMode) && (mSecRilLibHandle) &&
(connectRILDIfRequired() == OK) ) {
uint32_t device = AudioSystem::DEVICE_OUT_EARPIECE;
if (mOutput != 0) {
device = mOutput->device();
}
int int_volume = (int)(volume * 5);
SoundType type;
ALOGD("### route(%d) call volume(%f)", device, volume);
switch (device) {
case AudioSystem::DEVICE_OUT_EARPIECE:
ALOGD("### earpiece call volume");
type = SOUND_TYPE_VOICE;
break;
case AudioSystem::DEVICE_OUT_SPEAKER:
case AudioSystem::DEVICE_OUT_ANLG_DOCK_HEADSET:
ALOGD("### speaker call volume");
type = SOUND_TYPE_SPEAKER;
break;
case AudioSystem::DEVICE_OUT_BLUETOOTH_SCO:
case AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_HEADSET:
case AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_CARKIT:
ALOGD("### bluetooth call volume");
type = SOUND_TYPE_BTVOICE;
break;
case AudioSystem::DEVICE_OUT_WIRED_HEADSET:
case AudioSystem::DEVICE_OUT_WIRED_HEADPHONE: // Use receive path with 3 pole headset.
ALOGD("### headset call volume");
type = SOUND_TYPE_HEADSET;
break;
default:
ALOGW("### Call volume setting error!!!0x%08x \n", device);
type = SOUND_TYPE_VOICE;
break;
}
setCallVolume(mRilClient, type, int_volume);
}
}
status_t AudioHardware::setMasterVolume(float volume)
{
ALOGV("Set master volume to %f.\n", volume);
// We return an error code here to let the audioflinger do in-software
// volume on top of the maximum volume that we set through the SND API.
// return error - software mixer will handle it
return -1;
}
#ifdef HAVE_FM_RADIO
status_t AudioHardware::setFmVolume(float v)
{
mFmVolume = v;
if (mFmFd > 0) {
__u8 fmVolume = (AudioSystem::logToLinear(v) + 5) / 7;
ALOGD("%s %f %d", __func__, v, (int) fmVolume);
if (ioctl(mFmFd, Si4709_IOC_VOLUME_SET, &fmVolume) < 0) {
ALOGE("set_volume_fm error.");
return -EIO;
}
}
return NO_ERROR;
}
#endif
static const int kDumpLockRetries = 50;
static const int kDumpLockSleep = 20000;
static bool tryLock(Mutex& mutex)
{
bool locked = false;
for (int i = 0; i < kDumpLockRetries; ++i) {
if (mutex.tryLock() == NO_ERROR) {
locked = true;
break;
}
usleep(kDumpLockSleep);
}
return locked;
}
status_t AudioHardware::dump(int fd, const Vector<String16>& args)
{
const size_t SIZE = 256;
char buffer[SIZE];
String8 result;
bool locked = tryLock(mLock);
if (!locked) {
snprintf(buffer, SIZE, "\n\tAudioHardware maybe deadlocked\n");
} else {
mLock.unlock();
}
snprintf(buffer, SIZE, "\tInit %s\n", (mInit) ? "OK" : "Failed");
result.append(buffer);
snprintf(buffer, SIZE, "\tMic Mute %s\n", (mMicMute) ? "ON" : "OFF");
result.append(buffer);
snprintf(buffer, SIZE, "\tmPcm: %p\n", mPcm);
result.append(buffer);
snprintf(buffer, SIZE, "\tmPcmOpenCnt: %d\n", mPcmOpenCnt);
result.append(buffer);
snprintf(buffer, SIZE, "\tmMixer: %p\n", mMixer);
result.append(buffer);
snprintf(buffer, SIZE, "\tmMixerOpenCnt: %d\n", mMixerOpenCnt);
result.append(buffer);
snprintf(buffer, SIZE, "\tIn Call Audio Mode %s\n",
(mInCallAudioMode) ? "ON" : "OFF");
result.append(buffer);
snprintf(buffer, SIZE, "\tInput source %d\n", mInputSource);
result.append(buffer);
snprintf(buffer, SIZE, "\tmSecRilLibHandle: %p\n", mSecRilLibHandle);
result.append(buffer);
snprintf(buffer, SIZE, "\tmRilClient: %p\n", mRilClient);
result.append(buffer);
snprintf(buffer, SIZE, "\tCP %s\n",
(mActivatedCP) ? "Activated" : "Deactivated");
result.append(buffer);
snprintf(buffer, SIZE, "\tmDriverOp: %d\n", mDriverOp);
result.append(buffer);
snprintf(buffer, SIZE, "\n\tmOutput %p dump:\n", mOutput.get());
result.append(buffer);
write(fd, result.string(), result.size());
if (mOutput != 0) {
mOutput->dump(fd, args);
}
snprintf(buffer, SIZE, "\n\t%d inputs opened:\n", mInputs.size());
write(fd, buffer, strlen(buffer));
for (size_t i = 0; i < mInputs.size(); i++) {
snprintf(buffer, SIZE, "\t- input %d dump:\n", i);
write(fd, buffer, strlen(buffer));
mInputs[i]->dump(fd, args);
}
return NO_ERROR;
}
status_t AudioHardware::setIncallPath_l(uint32_t device)
{
ALOGV("setIncallPath_l: device %x", device);
// Setup sound path for CP clocking
if ((mSecRilLibHandle) &&
(connectRILDIfRequired() == OK)) {
if (mMode == AudioSystem::MODE_IN_CALL) {
ALOGD("### incall mode route (%d)", device);
AudioPath path;
switch(device){
case AudioSystem::DEVICE_OUT_EARPIECE:
ALOGD("### incall mode earpiece route");
path = SOUND_AUDIO_PATH_HANDSET;
break;
case AudioSystem::DEVICE_OUT_SPEAKER:
case AudioSystem::DEVICE_OUT_ANLG_DOCK_HEADSET:
ALOGD("### incall mode speaker route");
path = SOUND_AUDIO_PATH_SPEAKER;
break;
case AudioSystem::DEVICE_OUT_BLUETOOTH_SCO:
case AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_HEADSET:
case AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_CARKIT:
ALOGD("### incall mode bluetooth route %s NR", mBluetoothNrec ? "" : "NO");
if (mBluetoothNrec) {
path = SOUND_AUDIO_PATH_BLUETOOTH;
} else {
path = SOUND_AUDIO_PATH_BLUETOOTH_NO_NR;
}
break;
case AudioSystem::DEVICE_OUT_WIRED_HEADPHONE :
ALOGD("### incall mode headphone route");
path = SOUND_AUDIO_PATH_HEADPHONE;
break;
case AudioSystem::DEVICE_OUT_WIRED_HEADSET :
ALOGD("### incall mode headset route");
path = SOUND_AUDIO_PATH_HEADSET;
break;
default:
ALOGW("### incall mode Error!! route = [%d]", device);
path = SOUND_AUDIO_PATH_HANDSET;
break;
}
setCallAudioPath(mRilClient, path);
if (mMixer != NULL) {
TRACE_DRIVER_IN(DRV_MIXER_GET)
struct mixer_ctl *ctl= mixer_get_ctl_by_name(mMixer, "Voice Call Path");
TRACE_DRIVER_OUT
ALOGE_IF(ctl == NULL, "setIncallPath_l() could not get mixer ctl");
if (ctl != NULL) {
ALOGV("setIncallPath_l() Voice Call Path, (%x)", device);
TRACE_DRIVER_IN(DRV_MIXER_SEL)
mixer_ctl_set_enum_by_string(ctl, getVoiceRouteFromDevice(device));
TRACE_DRIVER_OUT
}
}
}
}
return NO_ERROR;
}
#ifdef HAVE_FM_RADIO
void AudioHardware::enableFMRadio() {
ALOGV("AudioHardware::enableFMRadio() Turning FM Radio ON");
if (mMode == AudioSystem::MODE_IN_CALL) {
ALOGV("AudioHardware::enableFMRadio() Call is active. Delaying FM enable.");
mFmResumeAfterCall = true;
}
else {
openPcmOut_l();
openMixer_l();
setInputSource_l(AUDIO_SOURCE_DEFAULT);
if (mMixer != NULL) {
ALOGV("AudioHardware::enableFMRadio() FM Radio is ON, calling setFMRadioPath_l()");
setFMRadioPath_l(mOutput->device());
}
if (mFmFd < 0) {
mFmFd = open("/dev/radio0", O_RDWR);
// In case setFmVolume was called before FM was enabled, we save the volume and call it here.
setFmVolume(mFmVolume);
}
}
}
void AudioHardware::disableFMRadio() {
ALOGV("AudioHardware::disableFMRadio() Turning FM Radio OFF");
if (mMixer != NULL) {
// Disable FM radio flag to allow the codec to be turned off
// (the flag is automatically set by the kernel driver when FM is enabled)
// No need to turn off the FM Radio path as the kernel driver will handle that
TRACE_DRIVER_IN(DRV_MIXER_GET)
struct mixer_ctl *ctl = mixer_get_control(mMixer, "Codec Status", 0);
TRACE_DRIVER_OUT
if (ctl != NULL) {
TRACE_DRIVER_IN(DRV_MIXER_SEL)
mixer_ctl_select(ctl, "FMR_FLAG_CLEAR");
TRACE_DRIVER_OUT
}
closeMixer_l();
closePcmOut_l();
}
if (mFmFd > 0) {
close(mFmFd);
mFmFd = -1;
}
}
status_t AudioHardware::setFMRadioPath_l(uint32_t device)
{
ALOGV("setFMRadioPath_l() device %x", device);
AudioPath path;
const char *fmpath;
if (device != AudioSystem::DEVICE_OUT_SPEAKER && (device & AudioSystem::DEVICE_OUT_SPEAKER) != 0) {
/* Fix the case where we're on headset and the system has just played a
* notification sound to both the speaker and the headset. The device
* now is an ORed value and we need to get back its original value.
*/
device -= AudioSystem::DEVICE_OUT_SPEAKER;
ALOGD("setFMRadioPath_l() device removed speaker %x", device);
}
switch(device){
case AudioSystem::DEVICE_OUT_SPEAKER:
ALOGD("setFMRadioPath_l() fmradio speaker route");
fmpath = "FMR_SPK";
break;
case AudioSystem::DEVICE_OUT_BLUETOOTH_SCO:
case AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_HEADSET:
case AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_CARKIT:
ALOGD("setFMRadioPath_l() fmradio bluetooth route");
break;
case AudioSystem::DEVICE_OUT_WIRED_HEADSET:
case AudioSystem::DEVICE_OUT_WIRED_HEADPHONE:
ALOGD("setFMRadioPath_l() fmradio headphone route");
fmpath = "FMR_HP";