• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #define LOG_TAG "APM::AudioPolicyEngine"
18 //#define LOG_NDEBUG 0
19 
20 //#define VERY_VERBOSE_LOGGING
21 #ifdef VERY_VERBOSE_LOGGING
22 #define ALOGVV ALOGV
23 #else
24 #define ALOGVV(a...) do { } while(0)
25 #endif
26 
27 #include "Engine.h"
28 #include <AudioPolicyManagerObserver.h>
29 #include <AudioPort.h>
30 #include <IOProfile.h>
31 #include <policy.h>
32 #include <utils/String8.h>
33 #include <utils/Log.h>
34 
35 namespace android
36 {
37 namespace audio_policy
38 {
39 
Engine()40 Engine::Engine()
41     : mManagerInterface(this),
42       mPhoneState(AUDIO_MODE_NORMAL),
43       mApmObserver(NULL)
44 {
45     for (int i = 0; i < AUDIO_POLICY_FORCE_USE_CNT; i++) {
46         mForceUse[i] = AUDIO_POLICY_FORCE_NONE;
47     }
48 }
49 
~Engine()50 Engine::~Engine()
51 {
52 }
53 
setObserver(AudioPolicyManagerObserver * observer)54 void Engine::setObserver(AudioPolicyManagerObserver *observer)
55 {
56     ALOG_ASSERT(observer != NULL, "Invalid Audio Policy Manager observer");
57     mApmObserver = observer;
58 }
59 
initCheck()60 status_t Engine::initCheck()
61 {
62     return (mApmObserver != NULL) ?  NO_ERROR : NO_INIT;
63 }
64 
setPhoneState(audio_mode_t state)65 status_t Engine::setPhoneState(audio_mode_t state)
66 {
67     ALOGV("setPhoneState() state %d", state);
68 
69     if (state < 0 || state >= AUDIO_MODE_CNT) {
70         ALOGW("setPhoneState() invalid state %d", state);
71         return BAD_VALUE;
72     }
73 
74     if (state == mPhoneState ) {
75         ALOGW("setPhoneState() setting same state %d", state);
76         return BAD_VALUE;
77     }
78 
79     // store previous phone state for management of sonification strategy below
80     int oldState = mPhoneState;
81     mPhoneState = state;
82 
83     if (!is_state_in_call(oldState) && is_state_in_call(state)) {
84         ALOGV("  Entering call in setPhoneState()");
85         mApmObserver->getVolumeCurves().switchVolumeCurve(AUDIO_STREAM_VOICE_CALL,
86                                                           AUDIO_STREAM_DTMF);
87     } else if (is_state_in_call(oldState) && !is_state_in_call(state)) {
88         ALOGV("  Exiting call in setPhoneState()");
89         mApmObserver->getVolumeCurves().restoreOriginVolumeCurve(AUDIO_STREAM_DTMF);
90     }
91     return NO_ERROR;
92 }
93 
setForceUse(audio_policy_force_use_t usage,audio_policy_forced_cfg_t config)94 status_t Engine::setForceUse(audio_policy_force_use_t usage, audio_policy_forced_cfg_t config)
95 {
96     switch(usage) {
97     case AUDIO_POLICY_FORCE_FOR_COMMUNICATION:
98         if (config != AUDIO_POLICY_FORCE_SPEAKER && config != AUDIO_POLICY_FORCE_BT_SCO &&
99             config != AUDIO_POLICY_FORCE_NONE) {
100             ALOGW("setForceUse() invalid config %d for FOR_COMMUNICATION", config);
101             return BAD_VALUE;
102         }
103         mForceUse[usage] = config;
104         break;
105     case AUDIO_POLICY_FORCE_FOR_MEDIA:
106         if (config != AUDIO_POLICY_FORCE_HEADPHONES && config != AUDIO_POLICY_FORCE_BT_A2DP &&
107             config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
108             config != AUDIO_POLICY_FORCE_ANALOG_DOCK &&
109             config != AUDIO_POLICY_FORCE_DIGITAL_DOCK && config != AUDIO_POLICY_FORCE_NONE &&
110             config != AUDIO_POLICY_FORCE_NO_BT_A2DP && config != AUDIO_POLICY_FORCE_SPEAKER ) {
111             ALOGW("setForceUse() invalid config %d for FOR_MEDIA", config);
112             return BAD_VALUE;
113         }
114         mForceUse[usage] = config;
115         break;
116     case AUDIO_POLICY_FORCE_FOR_RECORD:
117         if (config != AUDIO_POLICY_FORCE_BT_SCO && config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
118             config != AUDIO_POLICY_FORCE_NONE) {
119             ALOGW("setForceUse() invalid config %d for FOR_RECORD", config);
120             return BAD_VALUE;
121         }
122         mForceUse[usage] = config;
123         break;
124     case AUDIO_POLICY_FORCE_FOR_DOCK:
125         if (config != AUDIO_POLICY_FORCE_NONE && config != AUDIO_POLICY_FORCE_BT_CAR_DOCK &&
126             config != AUDIO_POLICY_FORCE_BT_DESK_DOCK &&
127             config != AUDIO_POLICY_FORCE_WIRED_ACCESSORY &&
128             config != AUDIO_POLICY_FORCE_ANALOG_DOCK &&
129             config != AUDIO_POLICY_FORCE_DIGITAL_DOCK) {
130             ALOGW("setForceUse() invalid config %d for FOR_DOCK", config);
131         }
132         mForceUse[usage] = config;
133         break;
134     case AUDIO_POLICY_FORCE_FOR_SYSTEM:
135         if (config != AUDIO_POLICY_FORCE_NONE &&
136             config != AUDIO_POLICY_FORCE_SYSTEM_ENFORCED) {
137             ALOGW("setForceUse() invalid config %d for FOR_SYSTEM", config);
138         }
139         mForceUse[usage] = config;
140         break;
141     case AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO:
142         if (config != AUDIO_POLICY_FORCE_NONE &&
143             config != AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED) {
144             ALOGW("setForceUse() invalid config %d for HDMI_SYSTEM_AUDIO", config);
145         }
146         mForceUse[usage] = config;
147         break;
148     case AUDIO_POLICY_FORCE_FOR_ENCODED_SURROUND:
149         if (config != AUDIO_POLICY_FORCE_NONE &&
150                 config != AUDIO_POLICY_FORCE_ENCODED_SURROUND_NEVER &&
151                 config != AUDIO_POLICY_FORCE_ENCODED_SURROUND_ALWAYS) {
152             ALOGW("setForceUse() invalid config %d for ENCODED_SURROUND", config);
153             return BAD_VALUE;
154         }
155         mForceUse[usage] = config;
156         break;
157     default:
158         ALOGW("setForceUse() invalid usage %d", usage);
159         break; // TODO return BAD_VALUE?
160     }
161     return NO_ERROR;
162 }
163 
getStrategyForStream(audio_stream_type_t stream)164 routing_strategy Engine::getStrategyForStream(audio_stream_type_t stream)
165 {
166     // stream to strategy mapping
167     switch (stream) {
168     case AUDIO_STREAM_VOICE_CALL:
169     case AUDIO_STREAM_BLUETOOTH_SCO:
170         return STRATEGY_PHONE;
171     case AUDIO_STREAM_RING:
172     case AUDIO_STREAM_ALARM:
173         return STRATEGY_SONIFICATION;
174     case AUDIO_STREAM_NOTIFICATION:
175         return STRATEGY_SONIFICATION_RESPECTFUL;
176     case AUDIO_STREAM_DTMF:
177         return STRATEGY_DTMF;
178     default:
179         ALOGE("unknown stream type %d", stream);
180     case AUDIO_STREAM_SYSTEM:
181         // NOTE: SYSTEM stream uses MEDIA strategy because muting music and switching outputs
182         // while key clicks are played produces a poor result
183     case AUDIO_STREAM_MUSIC:
184         return STRATEGY_MEDIA;
185     case AUDIO_STREAM_ENFORCED_AUDIBLE:
186         return STRATEGY_ENFORCED_AUDIBLE;
187     case AUDIO_STREAM_TTS:
188         return STRATEGY_TRANSMITTED_THROUGH_SPEAKER;
189     case AUDIO_STREAM_ACCESSIBILITY:
190         return STRATEGY_ACCESSIBILITY;
191     case AUDIO_STREAM_REROUTING:
192         return STRATEGY_REROUTING;
193     }
194 }
195 
getStrategyForUsage(audio_usage_t usage)196 routing_strategy Engine::getStrategyForUsage(audio_usage_t usage)
197 {
198     // usage to strategy mapping
199     switch (usage) {
200     case AUDIO_USAGE_ASSISTANCE_ACCESSIBILITY:
201         return STRATEGY_ACCESSIBILITY;
202 
203     case AUDIO_USAGE_MEDIA:
204     case AUDIO_USAGE_GAME:
205     case AUDIO_USAGE_ASSISTANT:
206     case AUDIO_USAGE_ASSISTANCE_NAVIGATION_GUIDANCE:
207     case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
208         return STRATEGY_MEDIA;
209 
210     case AUDIO_USAGE_VOICE_COMMUNICATION:
211         return STRATEGY_PHONE;
212 
213     case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
214         return STRATEGY_DTMF;
215 
216     case AUDIO_USAGE_ALARM:
217     case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
218         return STRATEGY_SONIFICATION;
219 
220     case AUDIO_USAGE_NOTIFICATION:
221     case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
222     case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
223     case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
224     case AUDIO_USAGE_NOTIFICATION_EVENT:
225         return STRATEGY_SONIFICATION_RESPECTFUL;
226 
227     case AUDIO_USAGE_UNKNOWN:
228     default:
229         return STRATEGY_MEDIA;
230     }
231 }
232 
getDeviceForStrategy(routing_strategy strategy) const233 audio_devices_t Engine::getDeviceForStrategy(routing_strategy strategy) const
234 {
235     DeviceVector availableOutputDevices = mApmObserver->getAvailableOutputDevices();
236     DeviceVector availableInputDevices = mApmObserver->getAvailableInputDevices();
237 
238     const SwAudioOutputCollection &outputs = mApmObserver->getOutputs();
239 
240     return getDeviceForStrategyInt(strategy, availableOutputDevices,
241                                    availableInputDevices, outputs);
242 }
243 
244 
245 
getDeviceForStrategyInt(routing_strategy strategy,DeviceVector availableOutputDevices,DeviceVector availableInputDevices,const SwAudioOutputCollection & outputs) const246 audio_devices_t Engine::getDeviceForStrategyInt(routing_strategy strategy,
247                                                 DeviceVector availableOutputDevices,
248                                                 DeviceVector availableInputDevices,
249                                                 const SwAudioOutputCollection &outputs) const
250 {
251     uint32_t device = AUDIO_DEVICE_NONE;
252     uint32_t availableOutputDevicesType = availableOutputDevices.types();
253 
254     switch (strategy) {
255 
256     case STRATEGY_TRANSMITTED_THROUGH_SPEAKER:
257         device = availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER;
258         break;
259 
260     case STRATEGY_SONIFICATION_RESPECTFUL:
261         if (isInCall()) {
262             device = getDeviceForStrategyInt(
263                     STRATEGY_SONIFICATION, availableOutputDevices, availableInputDevices, outputs);
264         } else if (outputs.isStreamActiveRemotely(AUDIO_STREAM_MUSIC,
265                 SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
266             // while media is playing on a remote device, use the the sonification behavior.
267             // Note that we test this usecase before testing if media is playing because
268             //   the isStreamActive() method only informs about the activity of a stream, not
269             //   if it's for local playback. Note also that we use the same delay between both tests
270             device = getDeviceForStrategyInt(
271                     STRATEGY_SONIFICATION, availableOutputDevices, availableInputDevices, outputs);
272             //user "safe" speaker if available instead of normal speaker to avoid triggering
273             //other acoustic safety mechanisms for notification
274             if ((device & AUDIO_DEVICE_OUT_SPEAKER) &&
275                     (availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER_SAFE)) {
276                 device |= AUDIO_DEVICE_OUT_SPEAKER_SAFE;
277                 device &= ~AUDIO_DEVICE_OUT_SPEAKER;
278             }
279         } else if (outputs.isStreamActive(
280                                 AUDIO_STREAM_MUSIC, SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
281             // while media is playing (or has recently played), use the same device
282             device = getDeviceForStrategyInt(
283                     STRATEGY_MEDIA, availableOutputDevices, availableInputDevices, outputs);
284         } else {
285             // when media is not playing anymore, fall back on the sonification behavior
286             device = getDeviceForStrategyInt(
287                     STRATEGY_SONIFICATION, availableOutputDevices, availableInputDevices, outputs);
288             //user "safe" speaker if available instead of normal speaker to avoid triggering
289             //other acoustic safety mechanisms for notification
290             if ((device & AUDIO_DEVICE_OUT_SPEAKER) &&
291                     (availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER_SAFE)) {
292                 device |= AUDIO_DEVICE_OUT_SPEAKER_SAFE;
293                 device &= ~AUDIO_DEVICE_OUT_SPEAKER;
294             }
295         }
296         break;
297 
298     case STRATEGY_DTMF:
299         if (!isInCall()) {
300             // when off call, DTMF strategy follows the same rules as MEDIA strategy
301             device = getDeviceForStrategyInt(
302                     STRATEGY_MEDIA, availableOutputDevices, availableInputDevices, outputs);
303             break;
304         }
305         // when in call, DTMF and PHONE strategies follow the same rules
306         // FALL THROUGH
307 
308     case STRATEGY_PHONE:
309         // Force use of only devices on primary output if:
310         // - in call AND
311         //   - cannot route from voice call RX OR
312         //   - audio HAL version is < 3.0 and TX device is on the primary HW module
313         if (getPhoneState() == AUDIO_MODE_IN_CALL) {
314             audio_devices_t txDevice = getDeviceForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION);
315             sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput();
316             audio_devices_t availPrimaryInputDevices =
317                  availableInputDevices.getDevicesFromHwModule(primaryOutput->getModuleHandle());
318             audio_devices_t availPrimaryOutputDevices =
319                     primaryOutput->supportedDevices() & availableOutputDevices.types();
320 
321             if (((availableInputDevices.types() &
322                     AUDIO_DEVICE_IN_TELEPHONY_RX & ~AUDIO_DEVICE_BIT_IN) == 0) ||
323                     (((txDevice & availPrimaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) &&
324                          (primaryOutput->getAudioPort()->getModuleVersionMajor() < 3))) {
325                 availableOutputDevicesType = availPrimaryOutputDevices;
326             }
327         }
328         // for phone strategy, we first consider the forced use and then the available devices by
329         // order of priority
330         switch (mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]) {
331         case AUDIO_POLICY_FORCE_BT_SCO:
332             if (!isInCall() || strategy != STRATEGY_DTMF) {
333                 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT;
334                 if (device) break;
335             }
336             device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET;
337             if (device) break;
338             device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_SCO;
339             if (device) break;
340             // if SCO device is requested but no SCO device is available, fall back to default case
341             // FALL THROUGH
342 
343         default:    // FORCE_NONE
344             // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to A2DP
345             if (!isInCall() &&
346                     (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) &&
347                     (outputs.getA2dpOutput() != 0)) {
348                 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
349                 if (device) break;
350                 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
351                 if (device) break;
352             }
353             device = availableOutputDevicesType & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
354             if (device) break;
355             device = availableOutputDevicesType & AUDIO_DEVICE_OUT_WIRED_HEADSET;
356             if (device) break;
357             device = availableOutputDevicesType & AUDIO_DEVICE_OUT_LINE;
358             if (device) break;
359             device = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_HEADSET;
360             if (device) break;
361             device = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_DEVICE;
362             if (device) break;
363             if (!isInCall()) {
364                 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_ACCESSORY;
365                 if (device) break;
366                 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
367                 if (device) break;
368                 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_AUX_DIGITAL;
369                 if (device) break;
370                 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
371                 if (device) break;
372             }
373             device = availableOutputDevicesType & AUDIO_DEVICE_OUT_EARPIECE;
374             break;
375 
376         case AUDIO_POLICY_FORCE_SPEAKER:
377             // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to
378             // A2DP speaker when forcing to speaker output
379             if (!isInCall() &&
380                     (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) &&
381                     (outputs.getA2dpOutput() != 0)) {
382                 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
383                 if (device) break;
384             }
385             if (!isInCall()) {
386                 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_ACCESSORY;
387                 if (device) break;
388                 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_DEVICE;
389                 if (device) break;
390                 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
391                 if (device) break;
392                 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_AUX_DIGITAL;
393                 if (device) break;
394                 device = availableOutputDevicesType & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
395                 if (device) break;
396             }
397             device = availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER;
398             break;
399         }
400     break;
401 
402     case STRATEGY_SONIFICATION:
403 
404         // If incall, just select the STRATEGY_PHONE device: The rest of the behavior is handled by
405         // handleIncallSonification().
406         if (isInCall()) {
407             device = getDeviceForStrategyInt(
408                     STRATEGY_PHONE, availableOutputDevices, availableInputDevices, outputs);
409             break;
410         }
411         // FALL THROUGH
412 
413     case STRATEGY_ENFORCED_AUDIBLE:
414         // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION
415         // except:
416         //   - when in call where it doesn't default to STRATEGY_PHONE behavior
417         //   - in countries where not enforced in which case it follows STRATEGY_MEDIA
418 
419         if ((strategy == STRATEGY_SONIFICATION) ||
420                 (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)) {
421             device = availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER;
422         }
423 
424         // if SCO headset is connected and we are told to use it, play ringtone over
425         // speaker and BT SCO
426         if (((availableOutputDevicesType & AUDIO_DEVICE_OUT_ALL_SCO) != 0) &&
427                 (mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION] == AUDIO_POLICY_FORCE_BT_SCO)) {
428             uint32_t device2 = AUDIO_DEVICE_NONE;
429             device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT;
430             if (device2 == AUDIO_DEVICE_NONE) {
431                 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET;
432             }
433             if (device2 == AUDIO_DEVICE_NONE) {
434                 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_SCO;
435             }
436 
437             if (device2 != AUDIO_DEVICE_NONE) {
438                 device |= device2;
439                 break;
440             }
441         }
442         // The second device used for sonification is the same as the device used by media strategy
443         // FALL THROUGH
444 
445     case STRATEGY_ACCESSIBILITY:
446         if (strategy == STRATEGY_ACCESSIBILITY) {
447             // do not route accessibility prompts to a digital output currently configured with a
448             // compressed format as they would likely not be mixed and dropped.
449             for (size_t i = 0; i < outputs.size(); i++) {
450                 sp<AudioOutputDescriptor> desc = outputs.valueAt(i);
451                 audio_devices_t devices = desc->device() &
452                     (AUDIO_DEVICE_OUT_HDMI | AUDIO_DEVICE_OUT_SPDIF | AUDIO_DEVICE_OUT_HDMI_ARC);
453                 if (desc->isActive() && !audio_is_linear_pcm(desc->mFormat) &&
454                         devices != AUDIO_DEVICE_NONE) {
455                     availableOutputDevicesType = availableOutputDevices.types() & ~devices;
456                 }
457             }
458             availableOutputDevices =
459                     availableOutputDevices.getDevicesFromType(availableOutputDevicesType);
460             if (outputs.isStreamActive(AUDIO_STREAM_RING) ||
461                     outputs.isStreamActive(AUDIO_STREAM_ALARM)) {
462                 return getDeviceForStrategyInt(
463                     STRATEGY_SONIFICATION, availableOutputDevices, availableInputDevices, outputs);
464             }
465             if (isInCall()) {
466                 return getDeviceForStrategyInt(
467                         STRATEGY_PHONE, availableOutputDevices, availableInputDevices, outputs);
468             }
469         }
470         // For other cases, STRATEGY_ACCESSIBILITY behaves like STRATEGY_MEDIA
471         // FALL THROUGH
472 
473     // FIXME: STRATEGY_REROUTING follow STRATEGY_MEDIA for now
474     case STRATEGY_REROUTING:
475     case STRATEGY_MEDIA: {
476         uint32_t device2 = AUDIO_DEVICE_NONE;
477         if (strategy != STRATEGY_SONIFICATION) {
478             // no sonification on remote submix (e.g. WFD)
479             if (availableOutputDevices.getDevice(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
480                                                  String8("0")) != 0) {
481                 device2 = availableOutputDevices.types() & AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
482             }
483         }
484         if (isInCall() && (strategy == STRATEGY_MEDIA)) {
485             device = getDeviceForStrategyInt(
486                     STRATEGY_PHONE, availableOutputDevices, availableInputDevices, outputs);
487             break;
488         }
489         if ((device2 == AUDIO_DEVICE_NONE) &&
490                 (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) &&
491                 (outputs.getA2dpOutput() != 0)) {
492             device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
493             if (device2 == AUDIO_DEVICE_NONE) {
494                 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
495             }
496             if (device2 == AUDIO_DEVICE_NONE) {
497                 device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
498             }
499         }
500         if ((device2 == AUDIO_DEVICE_NONE) &&
501             (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] == AUDIO_POLICY_FORCE_SPEAKER)) {
502             device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER;
503         }
504         if (device2 == AUDIO_DEVICE_NONE) {
505             device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
506         }
507         if (device2 == AUDIO_DEVICE_NONE) {
508             device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_LINE;
509         }
510         if (device2 == AUDIO_DEVICE_NONE) {
511             device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_WIRED_HEADSET;
512         }
513         if (device2 == AUDIO_DEVICE_NONE) {
514             device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_HEADSET;
515         }
516         if (device2 == AUDIO_DEVICE_NONE) {
517             device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_ACCESSORY;
518         }
519         if (device2 == AUDIO_DEVICE_NONE) {
520             device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_DEVICE;
521         }
522         if (device2 == AUDIO_DEVICE_NONE) {
523             device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
524         }
525         if ((device2 == AUDIO_DEVICE_NONE) && (strategy != STRATEGY_SONIFICATION)) {
526             // no sonification on aux digital (e.g. HDMI)
527             device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_AUX_DIGITAL;
528         }
529         if ((device2 == AUDIO_DEVICE_NONE) &&
530                 (mForceUse[AUDIO_POLICY_FORCE_FOR_DOCK] == AUDIO_POLICY_FORCE_ANALOG_DOCK)) {
531             device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
532         }
533         if (device2 == AUDIO_DEVICE_NONE) {
534             device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER;
535         }
536         int device3 = AUDIO_DEVICE_NONE;
537         if (strategy == STRATEGY_MEDIA) {
538             // ARC, SPDIF and AUX_LINE can co-exist with others.
539             device3 = availableOutputDevicesType & AUDIO_DEVICE_OUT_HDMI_ARC;
540             device3 |= (availableOutputDevicesType & AUDIO_DEVICE_OUT_SPDIF);
541             device3 |= (availableOutputDevicesType & AUDIO_DEVICE_OUT_AUX_LINE);
542         }
543 
544         device2 |= device3;
545         // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or
546         // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise
547         device |= device2;
548 
549         // If hdmi system audio mode is on, remove speaker out of output list.
550         if ((strategy == STRATEGY_MEDIA) &&
551             (mForceUse[AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO] ==
552                 AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED)) {
553             device &= ~AUDIO_DEVICE_OUT_SPEAKER;
554         }
555         } break;
556 
557     default:
558         ALOGW("getDeviceForStrategy() unknown strategy: %d", strategy);
559         break;
560     }
561 
562     if (device == AUDIO_DEVICE_NONE) {
563         ALOGV("getDeviceForStrategy() no device found for strategy %d", strategy);
564         device = mApmObserver->getDefaultOutputDevice()->type();
565         ALOGE_IF(device == AUDIO_DEVICE_NONE,
566                  "getDeviceForStrategy() no default device defined");
567     }
568     ALOGVV("getDeviceForStrategy() strategy %d, device %x", strategy, device);
569     return device;
570 }
571 
572 
getDeviceForInputSource(audio_source_t inputSource) const573 audio_devices_t Engine::getDeviceForInputSource(audio_source_t inputSource) const
574 {
575     const DeviceVector &availableOutputDevices = mApmObserver->getAvailableOutputDevices();
576     const DeviceVector &availableInputDevices = mApmObserver->getAvailableInputDevices();
577     const SwAudioOutputCollection &outputs = mApmObserver->getOutputs();
578     audio_devices_t availableDeviceTypes = availableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
579 
580     uint32_t device = AUDIO_DEVICE_NONE;
581 
582     switch (inputSource) {
583     case AUDIO_SOURCE_VOICE_UPLINK:
584       if (availableDeviceTypes & AUDIO_DEVICE_IN_VOICE_CALL) {
585           device = AUDIO_DEVICE_IN_VOICE_CALL;
586           break;
587       }
588       break;
589 
590     case AUDIO_SOURCE_DEFAULT:
591     case AUDIO_SOURCE_MIC:
592     if (availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_A2DP) {
593         device = AUDIO_DEVICE_IN_BLUETOOTH_A2DP;
594     } else if ((mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] == AUDIO_POLICY_FORCE_BT_SCO) &&
595         (availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET)) {
596         device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
597     } else if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) {
598         device = AUDIO_DEVICE_IN_WIRED_HEADSET;
599     } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_HEADSET) {
600         device = AUDIO_DEVICE_IN_USB_HEADSET;
601     } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) {
602         device = AUDIO_DEVICE_IN_USB_DEVICE;
603     } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
604         device = AUDIO_DEVICE_IN_BUILTIN_MIC;
605     }
606     break;
607 
608     case AUDIO_SOURCE_VOICE_COMMUNICATION:
609         // Allow only use of devices on primary input if in call and HAL does not support routing
610         // to voice call path.
611         if ((getPhoneState() == AUDIO_MODE_IN_CALL) &&
612                 (availableOutputDevices.types() & AUDIO_DEVICE_OUT_TELEPHONY_TX) == 0) {
613             sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput();
614             availableDeviceTypes =
615                     availableInputDevices.getDevicesFromHwModule(primaryOutput->getModuleHandle())
616                     & ~AUDIO_DEVICE_BIT_IN;
617         }
618 
619         switch (mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]) {
620         case AUDIO_POLICY_FORCE_BT_SCO:
621             // if SCO device is requested but no SCO device is available, fall back to default case
622             if (availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
623                 device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
624                 break;
625             }
626             // FALL THROUGH
627 
628         default:    // FORCE_NONE
629             if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) {
630                 device = AUDIO_DEVICE_IN_WIRED_HEADSET;
631             } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_HEADSET) {
632                 device = AUDIO_DEVICE_IN_USB_HEADSET;
633             } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) {
634                 device = AUDIO_DEVICE_IN_USB_DEVICE;
635             } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
636                 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
637             }
638             break;
639 
640         case AUDIO_POLICY_FORCE_SPEAKER:
641             if (availableDeviceTypes & AUDIO_DEVICE_IN_BACK_MIC) {
642                 device = AUDIO_DEVICE_IN_BACK_MIC;
643             } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
644                 device = AUDIO_DEVICE_IN_BUILTIN_MIC;
645             }
646             break;
647         }
648         break;
649 
650     case AUDIO_SOURCE_VOICE_RECOGNITION:
651     case AUDIO_SOURCE_UNPROCESSED:
652     case AUDIO_SOURCE_HOTWORD:
653         if (mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] == AUDIO_POLICY_FORCE_BT_SCO &&
654                 availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
655             device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
656         } else if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) {
657             device = AUDIO_DEVICE_IN_WIRED_HEADSET;
658         } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_HEADSET) {
659             device = AUDIO_DEVICE_IN_USB_HEADSET;
660         } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) {
661             device = AUDIO_DEVICE_IN_USB_DEVICE;
662         } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
663             device = AUDIO_DEVICE_IN_BUILTIN_MIC;
664         }
665         break;
666     case AUDIO_SOURCE_CAMCORDER:
667         if (availableDeviceTypes & AUDIO_DEVICE_IN_BACK_MIC) {
668             device = AUDIO_DEVICE_IN_BACK_MIC;
669         } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
670             device = AUDIO_DEVICE_IN_BUILTIN_MIC;
671         }
672         break;
673     case AUDIO_SOURCE_VOICE_DOWNLINK:
674     case AUDIO_SOURCE_VOICE_CALL:
675         if (availableDeviceTypes & AUDIO_DEVICE_IN_VOICE_CALL) {
676             device = AUDIO_DEVICE_IN_VOICE_CALL;
677         }
678         break;
679     case AUDIO_SOURCE_REMOTE_SUBMIX:
680         if (availableDeviceTypes & AUDIO_DEVICE_IN_REMOTE_SUBMIX) {
681             device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
682         }
683         break;
684      case AUDIO_SOURCE_FM_TUNER:
685         if (availableDeviceTypes & AUDIO_DEVICE_IN_FM_TUNER) {
686             device = AUDIO_DEVICE_IN_FM_TUNER;
687         }
688         break;
689     default:
690         ALOGW("getDeviceForInputSource() invalid input source %d", inputSource);
691         break;
692     }
693     if (device == AUDIO_DEVICE_NONE) {
694         ALOGV("getDeviceForInputSource() no device found for source %d", inputSource);
695         if (availableDeviceTypes & AUDIO_DEVICE_IN_STUB) {
696             device = AUDIO_DEVICE_IN_STUB;
697         }
698         ALOGE_IF(device == AUDIO_DEVICE_NONE,
699                  "getDeviceForInputSource() no default device defined");
700     }
701     ALOGV("getDeviceForInputSource()input source %d, device %08x", inputSource, device);
702     return device;
703 }
704 
705 template <>
queryInterface()706 AudioPolicyManagerInterface *Engine::queryInterface()
707 {
708     return &mManagerInterface;
709 }
710 
711 } // namespace audio_policy
712 } // namespace android
713 
714 
715