forked from fat-tire/android_hardware_alsa_sound
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathAudioPolicyManagerALSA.cpp
More file actions
executable file
·1148 lines (1039 loc) · 46.8 KB
/
Copy pathAudioPolicyManagerALSA.cpp
File metadata and controls
executable file
·1148 lines (1039 loc) · 46.8 KB
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 (C) 2009 The Android Open Source Project
* Copyright (c) 2011-2012, Code Aurora Forum. All rights reserved.
*
* 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 "AudioPolicyManagerALSA"
//#define LOG_NDEBUG 0
#define LOG_NDDEBUG 0
#include <utils/Log.h>
#include "AudioPolicyManagerALSA.h"
#include <media/mediarecorder.h>
namespace android_audio_legacy {
// ----------------------------------------------------------------------------
uint32_t AudioPolicyManager::getDeviceForStrategy(routing_strategy strategy, bool fromCache)
{
uint32_t device = 0;
if (fromCache) {
LOGV("getDeviceForStrategy() from cache strategy %d, device %x", strategy, mDeviceForStrategy[strategy]);
return mDeviceForStrategy[strategy];
}
switch (strategy) {
case STRATEGY_DTMF:
if (!isInCall()) {
// when off call, DTMF strategy follows the same rules as MEDIA strategy
device = getDeviceForStrategy(STRATEGY_MEDIA, false);
break;
}
// when in call, DTMF and PHONE strategies follow the same rules
// FALL THROUGH
case STRATEGY_PHONE:
// for phone strategy, we first consider the forced use and then the available devices by order
// of priority
switch (mForceUse[AudioSystem::FOR_COMMUNICATION]) {
case AudioSystem::FORCE_BT_SCO:
if (!isInCall() || strategy != STRATEGY_DTMF) {
device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_CARKIT;
if (device) break;
}
device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_HEADSET;
if (device) break;
device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_SCO;
if (device) break;
// if SCO device is requested but no SCO device is available, fall back to default case
// FALL THROUGH
default: // FORCE_NONE
#ifdef WITH_A2DP
// when not in a phone call, phone strategy should route STREAM_VOICE_CALL to A2DP
if (!isInCall()) {
device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP;
if (device) break;
device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
if (device) break;
}
#endif
device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_AUX_DIGITAL;
if (device) break;
device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_WIRED_HEADPHONE;
if (device) break;
device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_WIRED_HEADSET;
if (device) break;
device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_ANC_HEADPHONE;
if (device) break;
device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_ANC_HEADSET;
if (device) break;
device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_EARPIECE;
if (device == 0) {
LOGE("getDeviceForStrategy() earpiece device not found");
}
break;
case AudioSystem::FORCE_SPEAKER:
if (!isInCall() || strategy != STRATEGY_DTMF) {
device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_CARKIT;
if (device) break;
}
#ifdef WITH_A2DP
// when not in a phone call, phone strategy should route STREAM_VOICE_CALL to
// A2DP speaker when forcing to speaker output
if (!isInCall()) {
device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
if (device) break;
}
#endif
device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_SPEAKER;
if (device == 0) {
LOGE("getDeviceForStrategy() speaker device not found");
}
break;
}
if (mAvailableOutputDevices & AudioSystem::DEVICE_OUT_FM) {
device |= AudioSystem::DEVICE_OUT_FM;
if (mForceUse[AudioSystem::FOR_MEDIA] == AudioSystem::FORCE_SPEAKER) {
device &= ~(AudioSystem::DEVICE_OUT_WIRED_HEADSET);
device &= ~(AudioSystem::DEVICE_OUT_WIRED_HEADPHONE);
device |= AudioSystem::DEVICE_OUT_SPEAKER;
}
}
break;
case STRATEGY_SONIFICATION:
// If incall, just select the STRATEGY_PHONE device: The rest of the behavior is handled by
// handleIncallSonification().
if (isInCall()) {
device = getDeviceForStrategy(STRATEGY_PHONE, false);
break;
}
// FALL THROUGH
case STRATEGY_ENFORCED_AUDIBLE:
// strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION
// except when in call where it doesn't default to STRATEGY_PHONE behavior
device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_SPEAKER;
if (device == 0) {
LOGE("getDeviceForStrategy() speaker device not found");
}
// The second device used for sonification is the same as the device used by media strategy
// FALL THROUGH
case STRATEGY_MEDIA: {
//To route FM stream to speaker when headset is connected, a new switch case is added.
//case AudioSystem::FORCE_SPEAKER for STRATEGY_MEDIA will come only when we need to route
//FM stream to speaker.
switch (mForceUse[AudioSystem::FOR_MEDIA]) {
default:{
uint32_t device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_PROXY;
if(device2 != 0) {
// No combo device allowed with proxy device
device = 0;
}
#ifdef WITH_A2DP
if (mA2dpOutput != 0) {
if (strategy == STRATEGY_SONIFICATION && !a2dpUsedForSonification()) {
break;
}
if (device2 == 0) {
device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP;
}
if (device2 == 0) {
device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
}
if (device2 == 0) {
device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
}
}
#endif
if (device2 == 0) {
device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_AUX_DIGITAL;
}
if (device2 == 0) {
device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_WIRED_HEADPHONE;
}
if (device2 == 0) {
device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_WIRED_HEADSET;
}
if (device2 == 0) {
device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_ANC_HEADPHONE;
}
if (device2 == 0) {
device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_ANC_HEADSET;
}
if (device2 == 0) {
device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_FM_TX;
}
if (device2 == 0) {
device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_SPEAKER;
}
if (device2 == 0) {
device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_EARPIECE;
}
// device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or
// STRATEGY_ENFORCED_AUDIBLE, 0 otherwise
device |= device2;
}
break;
case AudioSystem::FORCE_SPEAKER:
device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_SPEAKER;
break;
}
if (mAvailableOutputDevices & AudioSystem::DEVICE_OUT_FM) {
device |= AudioSystem::DEVICE_OUT_FM;
}
if (mAvailableOutputDevices & AudioSystem::DEVICE_OUT_ANC_HEADSET) {
device |= AudioSystem::DEVICE_OUT_ANC_HEADSET;
}
if (mAvailableOutputDevices & AudioSystem::DEVICE_OUT_ANC_HEADPHONE) {
device |= AudioSystem::DEVICE_OUT_ANC_HEADPHONE;
}
// Do not play media stream if in call and the requested device would change the hardware
// output routing
if (mPhoneState == AudioSystem::MODE_IN_CALL &&
!AudioSystem::isA2dpDevice((AudioSystem::audio_devices)device) &&
device != getDeviceForStrategy(STRATEGY_PHONE)) {
device = getDeviceForStrategy(STRATEGY_PHONE);
LOGV("getDeviceForStrategy() incompatible media and phone devices");
}
} break;
default:
LOGW("getDeviceForStrategy() unknown strategy: %d", strategy);
break;
}
LOGV("getDeviceForStrategy() strategy %d, device %x", strategy, device);
return device;
}
status_t AudioPolicyManager::setDeviceConnectionState(AudioSystem::audio_devices device,
AudioSystem::device_connection_state state,
const char *device_address)
{
LOGV("setDeviceConnectionState() device: %x, state %d, address %s", device, state, device_address);
// connect/disconnect only 1 device at a time
if (AudioSystem::popCount(device) != 1) return BAD_VALUE;
if (strlen(device_address) >= MAX_DEVICE_ADDRESS_LEN) {
LOGE("setDeviceConnectionState() invalid address: %s", device_address);
return BAD_VALUE;
}
// handle output devices
if (AudioSystem::isOutputDevice(device)) {
#ifndef WITH_A2DP
if (AudioSystem::isA2dpDevice(device)) {
LOGE("setDeviceConnectionState() invalid device: %x", device);
return BAD_VALUE;
}
#endif
switch (state)
{
// handle output device connection
case AudioSystem::DEVICE_STATE_AVAILABLE:
if (mAvailableOutputDevices & device) {
LOGW("setDeviceConnectionState() device already connected: %x", device);
return INVALID_OPERATION;
}
LOGV("setDeviceConnectionState() connecting device %x", device);
// register new device as available
mAvailableOutputDevices |= device;
#ifdef WITH_A2DP
// handle A2DP device connection
if (AudioSystem::isA2dpDevice(device)) {
status_t status = AudioPolicyManagerBase::handleA2dpConnection(device, device_address);
if (status != NO_ERROR) {
mAvailableOutputDevices &= ~device;
return status;
}
} else
#endif
{
if (AudioSystem::isBluetoothScoDevice(device)) {
LOGV("setDeviceConnectionState() BT SCO device, address %s", device_address);
// keep track of SCO device address
mScoDeviceAddress = String8(device_address, MAX_DEVICE_ADDRESS_LEN);
}
}
break;
// handle output device disconnection
case AudioSystem::DEVICE_STATE_UNAVAILABLE: {
if (!(mAvailableOutputDevices & device)) {
LOGW("setDeviceConnectionState() device not connected: %x", device);
return INVALID_OPERATION;
}
LOGV("setDeviceConnectionState() disconnecting device %x", device);
// remove device from available output devices
mAvailableOutputDevices &= ~device;
#ifdef WITH_A2DP
// handle A2DP device disconnection
if (AudioSystem::isA2dpDevice(device)) {
status_t status = AudioPolicyManagerBase::handleA2dpDisconnection(device, device_address);
if (status != NO_ERROR) {
mAvailableOutputDevices |= device;
return status;
}
} else
#endif
{
if (AudioSystem::isBluetoothScoDevice(device)) {
mScoDeviceAddress = "";
}
}
} break;
default:
LOGE("setDeviceConnectionState() invalid state: %x", state);
return BAD_VALUE;
}
// request routing change if necessary
uint32_t newDevice = AudioPolicyManagerBase::getNewDevice(mHardwareOutput, false);
#ifdef WITH_QCOM_LPA
if(newDevice == 0 && mLPADecodeOutput != -1) {
newDevice = AudioPolicyManagerBase::getNewDevice(mLPADecodeOutput, false);
}
#endif
#ifdef HAVE_FM_RADIO
if(device == AudioSystem::DEVICE_OUT_FM) {
if (state == AudioSystem::DEVICE_STATE_AVAILABLE) {
mOutputs.valueFor(mHardwareOutput)->changeRefCount(AudioSystem::FM, 1);
}
else {
mOutputs.valueFor(mHardwareOutput)->changeRefCount(AudioSystem::FM, -1);
}
if(newDevice == 0){
newDevice = getDeviceForStrategy(STRATEGY_MEDIA, false);
}
AudioParameter param = AudioParameter();
param.addInt(String8(AudioParameter::keyHandleFm), (int)newDevice);
mpClientInterface->setParameters(mHardwareOutput, param.toString());
}
#endif
if(device == AudioSystem::DEVICE_OUT_ANC_HEADPHONE ||
device == AudioSystem::DEVICE_OUT_ANC_HEADSET) {
if(newDevice == 0){
newDevice = getDeviceForStrategy(STRATEGY_MEDIA, false);
}
}
#ifdef WITH_A2DP
AudioPolicyManagerBase::checkA2dpSuspend();
AudioPolicyManagerBase::checkOutputForAllStrategies();
// A2DP outputs must be closed after checkOutputForAllStrategies() is executed
if (state == AudioSystem::DEVICE_STATE_UNAVAILABLE && AudioSystem::isA2dpDevice(device)) {
AudioPolicyManagerBase::closeA2dpOutputs();
}
#endif
AudioPolicyManagerBase::updateDeviceForStrategy();
int delayMs = 0;
if (AudioSystem::DEVICE_OUT_SPEAKER == newDevice
&& AudioSystem::DEVICE_OUT_WIRED_HEADSET == device) {
//Add some delay(192ms) while switching from wired headset to speaker before pause
//music keeps playing from speaker if there's not delay or delay is too small.
delayMs = mOutputs.valueFor(mHardwareOutput)->mLatency*8;
}
#ifdef WITH_QCOM_LPA
if(mLPADecodeOutput != -1)
setOutputDevice(mLPADecodeOutput, newDevice, false, delayMs);
#endif
setOutputDevice(mHardwareOutput, newDevice, false, delayMs);
if (device == AudioSystem::DEVICE_OUT_WIRED_HEADSET) {
device = AudioSystem::DEVICE_IN_WIRED_HEADSET;
} else if (device == AudioSystem::DEVICE_OUT_BLUETOOTH_SCO ||
device == AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_HEADSET ||
device == AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_CARKIT) {
device = AudioSystem::DEVICE_IN_BLUETOOTH_SCO_HEADSET;
} else if(device == AudioSystem::DEVICE_OUT_ANC_HEADSET){
device = AudioSystem::DEVICE_IN_ANC_HEADSET; //wait for actual ANC device
} else {
return NO_ERROR;
}
}
// handle input devices
if (AudioSystem::isInputDevice(device)) {
switch (state)
{
// handle input device connection
case AudioSystem::DEVICE_STATE_AVAILABLE: {
if (mAvailableInputDevices & device) {
LOGW("setDeviceConnectionState() device already connected: %d", device);
return INVALID_OPERATION;
}
mAvailableInputDevices |= device;
}
break;
// handle input device disconnection
case AudioSystem::DEVICE_STATE_UNAVAILABLE: {
if (!(mAvailableInputDevices & device)) {
LOGW("setDeviceConnectionState() device not connected: %d", device);
return INVALID_OPERATION;
}
mAvailableInputDevices &= ~device;
} break;
default:
LOGE("setDeviceConnectionState() invalid state: %x", state);
return BAD_VALUE;
}
audio_io_handle_t activeInput = AudioPolicyManagerBase::getActiveInput();
if (activeInput != 0) {
AudioInputDescriptor *inputDesc = mInputs.valueFor(activeInput);
uint32_t newDevice = AudioPolicyManagerBase::getDeviceForInputSource(inputDesc->mInputSource);
if (newDevice != inputDesc->mDevice) {
LOGV("setDeviceConnectionState() changing device from %x to %x for input %d",
inputDesc->mDevice, newDevice, activeInput);
inputDesc->mDevice = newDevice;
AudioParameter param = AudioParameter();
param.addInt(String8(AudioParameter::keyRouting), (int)newDevice);
mpClientInterface->setParameters(activeInput, param.toString());
}
}
return NO_ERROR;
}
LOGW("setDeviceConnectionState() invalid device: %x", device);
return BAD_VALUE;
}
void AudioPolicyManager::setPhoneState(int state)
{
LOGD("setPhoneState() state %d", state);
uint32_t newDevice = 0;
if (state < 0 || state >= AudioSystem::NUM_MODES) {
LOGW("setPhoneState() invalid state %d", state);
return;
}
if (state == mPhoneState ) {
LOGW("setPhoneState() setting same state %d", state);
return;
}
// if leaving call state, handle special case of active streams
// pertaining to sonification strategy see handleIncallSonification()
if (isInCall()) {
LOGV("setPhoneState() in call state management: new state is %d", state);
for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
AudioPolicyManagerBase::handleIncallSonification(stream, false, true);
}
}
// store previous phone state for management of sonification strategy below
int oldState = mPhoneState;
mPhoneState = state;
// force routing command to audio hardware when starting call
// even if no device change is needed
bool force = (mPhoneState == AudioSystem::MODE_IN_CALL);
// are we entering or starting a call
if (!isStateInCall(oldState) && isStateInCall(state)) {
LOGV(" Entering call in setPhoneState()");
// force routing command to audio hardware when starting a call
// even if no device change is needed
force = true;
} else if (isStateInCall(oldState) && (state == AudioSystem::MODE_NORMAL)) {
LOGV(" Exiting call in setPhoneState()");
// force routing command to audio hardware when exiting a call
// even if no device change is needed
force = true;
} else if (isStateInCall(state) && (state != oldState)) {
LOGV(" Switching between telephony and VoIP in setPhoneState()");
// force routing command to audio hardware when switching between telephony and VoIP
// even if no device change is needed
force = true;
}
// check for device and output changes triggered by new phone state
newDevice = AudioPolicyManagerBase::getNewDevice(mHardwareOutput, false);
#ifdef WITH_QCOM_LPA
if (newDevice == 0 && (mLPADecodeOutput != -1 &&
mOutputs.valueFor(mLPADecodeOutput)->isUsedByStrategy(STRATEGY_MEDIA))) {
newDevice = getDeviceForStrategy(STRATEGY_MEDIA, false);
}
#endif
#ifdef WITH_A2DP
AudioPolicyManagerBase::checkA2dpSuspend();
AudioPolicyManagerBase::checkOutputForAllStrategies();
#endif
AudioPolicyManagerBase::updateDeviceForStrategy();
AudioOutputDescriptor *hwOutputDesc = mOutputs.valueFor(mHardwareOutput);
// force routing command to audio hardware when ending call
// even if no device change is needed
if (isStateInCall(oldState) && newDevice == 0) {
newDevice = hwOutputDesc->device();
if(state == AudioSystem::MODE_NORMAL) {
force = true;
}
}
// when changing from ring tone to in call mode, mute the ringing tone
// immediately and delay the route change to avoid sending the ring tone
// tail into the earpiece or headset.
int delayMs = 0;
if (isStateInCall(state) && oldState == AudioSystem::MODE_RINGTONE) {
// delay the device change command by twice the output latency to have some margin
// and be sure that audio buffers not yet affected by the mute are out when
// we actually apply the route change
delayMs = hwOutputDesc->mLatency*2;
setStreamMute(AudioSystem::RING, true, mHardwareOutput);
}
// change routing is necessary
setOutputDevice(mHardwareOutput, newDevice, force, delayMs);
// if entering in call state, handle special case of active streams
// pertaining to sonification strategy see handleIncallSonification()
if (isStateInCall(state)) {
LOGV("setPhoneState() in call state management: new state is %d", state);
// unmute the ringing tone after a sufficient delay if it was muted before
// setting output device above
if (oldState == AudioSystem::MODE_RINGTONE) {
setStreamMute(AudioSystem::RING, false, mHardwareOutput, MUTE_TIME_MS);
}
for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
AudioPolicyManagerBase::handleIncallSonification(stream, true, true);
}
}
// Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
if (state == AudioSystem::MODE_RINGTONE &&
(hwOutputDesc->mRefCount[AudioSystem::MUSIC] ||
(systemTime() - hwOutputDesc->mStopTime[AudioSystem::MUSIC]) < seconds(SONIFICATION_HEADSET_MUSIC_DELAY))) {
// (systemTime() - mMusicStopTime) < seconds(SONIFICATION_HEADSET_MUSIC_DELAY))) {
mLimitRingtoneVolume = true;
} else {
mLimitRingtoneVolume = false;
}
}
#ifdef WITH_QCOM_LPA
audio_io_handle_t AudioPolicyManager::getSession(AudioSystem::stream_type stream,
uint32_t format,
AudioSystem::output_flags flags,
int32_t sessionId)
{
audio_io_handle_t output = 0;
uint32_t latency = 0;
routing_strategy strategy = getStrategy((AudioSystem::stream_type)stream);
uint32_t device = getDeviceForStrategy(strategy);
LOGV("getSession() stream %d, format %d, sessionId %x, flags %x device %d", stream, format, sessionId, flags, device);
AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor();
outputDesc->mDevice = device;
outputDesc->mSamplingRate = 0;
outputDesc->mFormat = format;
outputDesc->mChannels = 2;
outputDesc->mLatency = 0;
outputDesc->mFlags = (AudioSystem::output_flags)(flags | AudioSystem::OUTPUT_FLAG_DIRECT);
outputDesc->mRefCount[stream] = 0;
output = mpClientInterface->openSession(&outputDesc->mDevice,
&outputDesc->mFormat,
outputDesc->mFlags,
stream,
sessionId);
// only accept an output with the requeted parameters
if ((format != 0 && format != outputDesc->mFormat) || !output) {
LOGE("openSession() failed opening a session: format %d, sessionId %d output %d",
format, sessionId, output);
if(output) {
mpClientInterface->closeSession(output);
}
delete outputDesc;
return 0;
}
//reset it here, it will get updated in startoutput
outputDesc->mDevice = 0;
mOutputs.add(output, outputDesc);
mLPADecodeOutput = output;
mLPAStreamType = stream;
return output;
}
void AudioPolicyManager::pauseSession(audio_io_handle_t output, AudioSystem::stream_type stream)
{
LOGV("pauseSession() Output [%d] and stream is [%d]", output, stream);
if ( (output == mLPADecodeOutput) &&
(stream == mLPAStreamType) ) {
AudioPolicyManager::stopOutput(output, mLPAStreamType);
mLPAActiveOuput = mLPADecodeOutput;
mLPAActiveStreamType = mLPAStreamType;
mLPADecodeOutput = -1;
mLPAStreamType = AudioSystem::DEFAULT;
mLPAMuted = false;
}
}
void AudioPolicyManager::resumeSession(audio_io_handle_t output, AudioSystem::stream_type stream)
{
LOGV("resumeSession() Output [%d] and stream is [%d]", output, stream);
if (output == mLPAActiveOuput)
{
mLPADecodeOutput = mLPAActiveOuput;
mLPAStreamType = stream;
AudioPolicyManager::startOutput(mLPADecodeOutput, mLPAStreamType);
// Set Volume if the music stream volume is changed in the Pause state of LPA Jagan
mLPAActiveOuput = -1;
mLPAActiveStreamType = AudioSystem::DEFAULT;
}
}
void AudioPolicyManager::releaseSession(audio_io_handle_t output)
{
LOGV("releaseSession() %d", output);
// This means the Output is put into Pause state
if (output == mLPAActiveOuput && mLPADecodeOutput == -1) {
mLPADecodeOutput = mLPAActiveOuput;
mLPAStreamType = mLPAActiveStreamType;
}
ssize_t index = mOutputs.indexOfKey(output);
if (index < 0) {
LOGW("releaseSession() releasing unknown output %d", output);
return;
}
AudioPolicyManager::stopOutput(output, mLPAStreamType);
delete mOutputs.valueAt(index);
mOutputs.removeItem(output);
mLPADecodeOutput = -1;
mLPAActiveOuput = -1;
mLPAStreamType = AudioSystem::DEFAULT;
mLPAActiveStreamType = AudioSystem::DEFAULT;
mLPAMuted = false;
}
#endif
status_t AudioPolicyManager::startOutput(audio_io_handle_t output, AudioSystem::stream_type stream, int session)
{
LOGV("startOutput() output %d, stream %d", output, stream);
ssize_t index = mOutputs.indexOfKey(output);
if (index < 0) {
LOGW("startOutput() unknow output %d", output);
return BAD_VALUE;
}
AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index);
routing_strategy strategy = getStrategy((AudioSystem::stream_type)stream);
#ifdef WITH_A2DP
if (mA2dpOutput != 0 && !a2dpUsedForSonification() &&
(strategy == STRATEGY_SONIFICATION || strategy == STRATEGY_ENFORCED_AUDIBLE)) {
setStrategyMute(STRATEGY_MEDIA, true, mA2dpOutput);
}
#endif
// incremenent usage count for this stream on the requested output:
// NOTE that the usage count is the same for duplicated output and hardware output which is
// necassary for a correct control of hardware output routing by startOutput() and stopOutput()
outputDesc->changeRefCount(stream, 1);
#ifdef WITH_QCOM_LPA
if (output != mLPADecodeOutput)
setOutputDevice(output, AudioPolicyManagerBase::getNewDevice(output));
else
#endif
setOutputDevice(output, AudioPolicyManagerBase::getNewDevice(output), true);
// handle special case for sonification while in call
if (isInCall()) {
AudioPolicyManagerBase::handleIncallSonification(stream, true, false);
}
// apply volume rules for current stream and device if necessary
checkAndSetVolume(stream, mStreams[stream].mIndexCur, output, outputDesc->device());
return NO_ERROR;
}
status_t AudioPolicyManager::stopOutput(audio_io_handle_t output, AudioSystem::stream_type stream, int session)
{
LOGV("stopOutput() output %d, stream %d", output, stream);
ssize_t index = mOutputs.indexOfKey(output);
if (index < 0) {
LOGW("stopOutput() unknow output %d", output);
return BAD_VALUE;
}
AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index);
routing_strategy strategy = AudioPolicyManagerBase::getStrategy((AudioSystem::stream_type)stream);
// handle special case for sonification while in call
if (isInCall()) {
AudioPolicyManagerBase::handleIncallSonification(stream, false, false);
}
if (outputDesc->mRefCount[stream] > 0) {
// decrement usage count of this stream on the output
outputDesc->changeRefCount(stream, -1);
// store time at which the last music track was stopped - see computeVolume()
if (stream == AudioSystem::MUSIC) {
outputDesc->mStopTime[stream] = systemTime();
// mMusicStopTime = systemTime();
}
uint32_t newDevice = AudioPolicyManagerBase::getNewDevice(mHardwareOutput, false);
#ifdef WITH_QCOM_LPA
if(newDevice == 0 && mLPADecodeOutput != -1) {
newDevice = AudioPolicyManagerBase::getNewDevice(mLPADecodeOutput, false);
}
#endif
setOutputDevice(output, newDevice);
#ifdef WITH_A2DP
if (mA2dpOutput != 0 && !a2dpUsedForSonification() &&
(strategy == STRATEGY_SONIFICATION || strategy == STRATEGY_ENFORCED_AUDIBLE)) {
setStrategyMute(STRATEGY_MEDIA,
false,
mA2dpOutput,
mOutputs.valueFor(mHardwareOutput)->mLatency*2);
}
#endif
if (output != mHardwareOutput) {
setOutputDevice(mHardwareOutput, AudioPolicyManagerBase::getNewDevice(mHardwareOutput), true);
}
return NO_ERROR;
} else {
LOGW("stopOutput() refcount is already 0 for output %d", output);
return INVALID_OPERATION;
}
}
// ----------------------------------------------------------------------------
// AudioPolicyManagerALSA
// ----------------------------------------------------------------------------
// --- class factory
extern "C" AudioPolicyInterface* createAudioPolicyManager(AudioPolicyClientInterface *clientInterface)
{
return new AudioPolicyManager(clientInterface);
}
extern "C" void destroyAudioPolicyManager(AudioPolicyInterface *interface)
{
delete interface;
}
// ---
void AudioPolicyManager::setOutputDevice(audio_io_handle_t output, uint32_t device, bool force, int delayMs)
{
LOGV("setOutputDevice() output %d device %x delayMs %d", output, device, delayMs);
AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
if (outputDesc->isDuplicated()) {
setOutputDevice(outputDesc->mOutput1->mId, device, force, delayMs);
setOutputDevice(outputDesc->mOutput2->mId, device, force, delayMs);
return;
}
#ifdef WITH_A2DP
// filter devices according to output selected
if (output == mA2dpOutput) {
device &= AudioSystem::DEVICE_OUT_ALL_A2DP;
} else {
device &= ~AudioSystem::DEVICE_OUT_ALL_A2DP;
}
#endif
uint32_t prevDevice = (uint32_t)outputDesc->device();
// Do not change the routing if:
// - the requestede device is 0
// - the requested device is the same as current device and force is not specified.
// Doing this check here allows the caller to call setOutputDevice() without conditions
if ((device == 0 || device == prevDevice) && !force) {
LOGV("setOutputDevice() setting same device %x or null device for output %d", device, output);
return;
}
outputDesc->mDevice = device;
// mute media streams if both speaker and headset are selected
if (device == (AudioSystem::DEVICE_OUT_SPEAKER | AudioSystem::DEVICE_OUT_WIRED_HEADSET)
|| device == (AudioSystem::DEVICE_OUT_SPEAKER | AudioSystem::DEVICE_OUT_ANC_HEADSET)
|| device == (AudioSystem::DEVICE_OUT_SPEAKER | AudioSystem::DEVICE_OUT_ANC_HEADPHONE)
|| device == (AudioSystem::DEVICE_OUT_SPEAKER | AudioSystem::DEVICE_OUT_WIRED_HEADPHONE)
|| device == (AudioSystem::DEVICE_OUT_SPEAKER | AudioSystem::DEVICE_OUT_WIRED_HEADSET | AudioSystem::DEVICE_OUT_FM)
|| device == (AudioSystem::DEVICE_OUT_SPEAKER | AudioSystem::DEVICE_OUT_WIRED_HEADPHONE | AudioSystem::DEVICE_OUT_FM)
|| device == (AudioSystem::DEVICE_OUT_SPEAKER | AudioSystem::DEVICE_OUT_FM_TX)) {
setStrategyMute(STRATEGY_MEDIA, true, output);
#ifdef WITH_QCOM_LPA
// Mute LPA output also if it belongs to STRATEGY_MEDIA
if(((mLPADecodeOutput != -1) && (mLPADecodeOutput != output) &&
mOutputs.valueFor(mLPADecodeOutput)->isUsedByStrategy(STRATEGY_MEDIA))) {
LOGV("setOutputDevice: Muting mLPADecodeOutput:%d",mLPADecodeOutput);
setStrategyMute(STRATEGY_MEDIA, true, mLPADecodeOutput);
}
#endif
// Mute hardware output also if it belongs to STRATEGY_MEDIA
if(((mHardwareOutput != -1) && (mHardwareOutput != output) &&
mOutputs.valueFor(mHardwareOutput)->isUsedByStrategy(STRATEGY_MEDIA))) {
LOGV("setOutputDevice: Muting mHardwareOutput:%d",mHardwareOutput);
setStrategyMute(STRATEGY_MEDIA, true, mHardwareOutput);
}
// wait for the PCM output buffers to empty before proceeding with the rest of the command
usleep(outputDesc->mLatency*2*1000);
}
// wait for output buffers to be played on the HDMI device before routing to new device
if(prevDevice == AudioSystem::DEVICE_OUT_AUX_DIGITAL) {
#ifdef WITH_QCOM_LPA
if((mLPADecodeOutput != -1 && output == mLPADecodeOutput &&
mOutputs.valueFor(mLPADecodeOutput)->isUsedByStrategy(STRATEGY_MEDIA))) {
checkAndSetVolume(AudioSystem::MUSIC, mStreams[AudioSystem::MUSIC].mIndexCur, mLPADecodeOutput, device, delayMs, force);
usleep(150*1000);
} else {
#endif
checkAndSetVolume(AudioSystem::MUSIC, mStreams[AudioSystem::MUSIC].mIndexCur, output, device, delayMs, force);
usleep(outputDesc->mLatency*6*1000);
#ifdef WITH_QCOM_LPA
}
#endif
}
// do the routing
AudioParameter param = AudioParameter();
param.addInt(String8(AudioParameter::keyRouting), (int)device);
mpClientInterface->setParameters(mHardwareOutput, param.toString(), delayMs);
// update stream volumes according to new device
AudioPolicyManagerBase::applyStreamVolumes(output, device, delayMs);
#ifdef WITH_QCOM_LPA
if((mLPADecodeOutput != -1 &&
mOutputs.valueFor(mLPADecodeOutput)->isUsedByStrategy(STRATEGY_MEDIA))) {
AudioPolicyManagerBase::applyStreamVolumes(mLPADecodeOutput, device, delayMs);
}
#endif
// if changing from a combined headset + speaker route, unmute media streams
if (prevDevice == (AudioSystem::DEVICE_OUT_SPEAKER | AudioSystem::DEVICE_OUT_WIRED_HEADSET)
|| prevDevice == (AudioSystem::DEVICE_OUT_SPEAKER | AudioSystem::DEVICE_OUT_ANC_HEADSET)
|| prevDevice == (AudioSystem::DEVICE_OUT_SPEAKER | AudioSystem::DEVICE_OUT_ANC_HEADPHONE)
|| prevDevice == (AudioSystem::DEVICE_OUT_SPEAKER | AudioSystem::DEVICE_OUT_WIRED_HEADPHONE)
#ifdef HAVE_FM_RADIO
|| prevDevice == (AudioSystem::DEVICE_OUT_SPEAKER | AudioSystem::DEVICE_OUT_WIRED_HEADSET | AudioSystem::DEVICE_OUT_FM)
|| prevDevice == (AudioSystem::DEVICE_OUT_SPEAKER | AudioSystem::DEVICE_OUT_WIRED_HEADPHONE | AudioSystem::DEVICE_OUT_FM)
|| prevDevice == (AudioSystem::DEVICE_OUT_SPEAKER | AudioSystem::DEVICE_OUT_FM_TX)
#endif
) {
setStrategyMute(STRATEGY_MEDIA, false, output, delayMs);
#ifdef WITH_QCOM_LPA
// Unmute LPA output also if it belongs to STRATEGY_MEDIA
if(((mLPADecodeOutput != -1) && (mLPADecodeOutput != output) &&
mOutputs.valueFor(mLPADecodeOutput)->isUsedByStrategy(STRATEGY_MEDIA))) {
LOGV("setOutputDevice: Unmuting mLPADecodeOutput:%d delayMs:%d",mLPADecodeOutput,delayMs);
setStrategyMute(STRATEGY_MEDIA, false, mLPADecodeOutput, delayMs);
}
#endif
// Unmute hardware output also if it belongs to STRATEGY_MEDIA
if(((mHardwareOutput != -1) && (mHardwareOutput != output) &&
mOutputs.valueFor(mHardwareOutput)->isUsedByStrategy(STRATEGY_MEDIA))) {
LOGV("setOutputDevice: Unmuting mHardwareOutput:%d delayMs:%d",mHardwareOutput,delayMs);
setStrategyMute(STRATEGY_MEDIA, false, mHardwareOutput, delayMs);
}
}
}
status_t AudioPolicyManager::checkAndSetVolume(int stream, int index, audio_io_handle_t output, uint32_t device, int delayMs, bool force)
{
#ifdef WITH_QCOM_LPA
// do not change actual stream volume if the stream is muted
if ((mOutputs.valueFor(output)->mMuteCount[stream] != 0 && output != mLPADecodeOutput) ||
(output == mLPADecodeOutput && stream == mLPAStreamType && mLPAMuted == true)) {
#else
if (mOutputs.valueFor(output)->mMuteCount[stream] != 0) {
#endif
LOGV("checkAndSetVolume() stream %d muted count %d", stream, mOutputs.valueFor(output)->mMuteCount[stream]);
return NO_ERROR;
}
// do not change in call volume if bluetooth is connected and vice versa
if ((stream == AudioSystem::VOICE_CALL && mForceUse[AudioSystem::FOR_COMMUNICATION] == AudioSystem::FORCE_BT_SCO) ||
(stream == AudioSystem::BLUETOOTH_SCO && mForceUse[AudioSystem::FOR_COMMUNICATION] != AudioSystem::FORCE_BT_SCO)) {
LOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
stream, mForceUse[AudioSystem::FOR_COMMUNICATION]);
return INVALID_OPERATION;
}
float volume = computeVolume(stream, index, output, device);
// do not set volume if the float value did not change
if ((volume != mOutputs.valueFor(output)->mCurVolume[stream]) || (stream == AudioSystem::VOICE_CALL) ||
#ifdef HAVE_FM_RADIO
(stream == AudioSystem::FM) ||
#endif
force) {
mOutputs.valueFor(output)->mCurVolume[stream] = volume;
LOGD("setStreamVolume() for output %d stream %d, volume %f, delay %d", output, stream, volume, delayMs);
if (stream == AudioSystem::VOICE_CALL ||
stream == AudioSystem::DTMF ||
stream == AudioSystem::BLUETOOTH_SCO) {
float voiceVolume = -1.0;
// offset value to reflect actual hardware volume that never reaches 0
// 1% corresponds roughly to first step in VOICE_CALL stream volume setting (see AudioService.java)
volume = 0.01 + 0.99 * volume;
if (stream == AudioSystem::VOICE_CALL) {
voiceVolume = (float)index/(float)mStreams[stream].mIndexMax;
} else if (stream == AudioSystem::BLUETOOTH_SCO) {
String8 key ("bt_headset_vgs");
mpClientInterface->getParameters(output,key);
AudioParameter result(mpClientInterface->getParameters(0,key));
int value;
if(result.getInt(String8("isVGS"),value) == NO_ERROR){
LOGD("BT-SCO Voice Volume %f",(float)index/(float)mStreams[stream].mIndexMax);
voiceVolume = 1.0;
} else {
voiceVolume = (float)index/(float)mStreams[stream].mIndexMax;
}
}
if (voiceVolume >= 0 && output == mHardwareOutput) {
mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
}
#ifdef HAVE_FM_RADIO
} else if (stream == AudioSystem::FM) {
float fmVolume = -1.0;
fmVolume = computeVolume(stream, index, output, device);
if (fmVolume >= 0) {
if(output == mHardwareOutput)
mpClientInterface->setFmVolume(fmVolume, delayMs);
else if(output == mA2dpOutput)
mpClientInterface->setStreamVolume((AudioSystem::stream_type)stream, volume, output, delayMs);
}
return NO_ERROR;
#endif
}
mpClientInterface->setStreamVolume((AudioSystem::stream_type)stream, volume, output, delayMs);
}
return NO_ERROR;
}
void AudioPolicyManager::setStreamMute(int stream, bool on, audio_io_handle_t output, int delayMs)
{
StreamDescriptor &streamDesc = mStreams[stream];
AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
LOGV("setStreamMute() stream %d, mute %d, output %d, mMuteCount %d", stream, on, output, outputDesc->mMuteCount[stream]);
if (on) {
#ifdef WITH_QCOM_LPA
if ((outputDesc->mMuteCount[stream] == 0 && output != mLPADecodeOutput) ||
(output == mLPADecodeOutput && stream == mLPAStreamType && false == mLPAMuted)) {
#else
if (outputDesc->mMuteCount[stream] == 0) {
#endif
if (streamDesc.mCanBeMuted) {
checkAndSetVolume(stream, 0, output, outputDesc->device(), delayMs);
}
}
#ifdef WITH_QCOM_LPA
// increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
if(output == mLPADecodeOutput) {
if(stream == mLPAStreamType && false == mLPAMuted) {
mLPAMuted = true;
}
} else {
#endif
outputDesc->mMuteCount[stream]++;
#ifdef WITH_QCOM_LPA
}
#endif
} else {
#ifdef WITH_QCOM_LPA
if ((outputDesc->mMuteCount[stream] == 0 && output != mLPADecodeOutput) ||
(output == mLPADecodeOutput && stream == mLPAStreamType && false == mLPAMuted)) {
#else
if (outputDesc->mMuteCount[stream] == 0) {
#endif
LOGW("setStreamMute() unmuting non muted stream!");
return;
}
#ifdef WITH_QCOM_LPA
if(output == mLPADecodeOutput) {
if(stream == mLPAStreamType && true == mLPAMuted) {
mLPAMuted = false;
checkAndSetVolume(stream, streamDesc.mIndexCur, output, outputDesc->device(), delayMs);
}