Engine.cpp revision 0f928fab9963f15c524f4416b3d52d2df4a93eec
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
35namespace android
36{
37namespace audio_policy
38{
39
40Engine::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
50Engine::~Engine()
51{
52}
53
54void Engine::setObserver(AudioPolicyManagerObserver *observer)
55{
56    ALOG_ASSERT(observer != NULL, "Invalid Audio Policy Manager observer");
57    mApmObserver = observer;
58}
59
60status_t Engine::initCheck()
61{
62    return (mApmObserver != NULL) ?  NO_ERROR : NO_INIT;
63}
64
65status_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
94status_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
164routing_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
196routing_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_ASSISTANCE_NAVIGATION_GUIDANCE:
206    case AUDIO_USAGE_ASSISTANCE_SONIFICATION:
207        return STRATEGY_MEDIA;
208
209    case AUDIO_USAGE_VOICE_COMMUNICATION:
210        return STRATEGY_PHONE;
211
212    case AUDIO_USAGE_VOICE_COMMUNICATION_SIGNALLING:
213        return STRATEGY_DTMF;
214
215    case AUDIO_USAGE_ALARM:
216    case AUDIO_USAGE_NOTIFICATION_TELEPHONY_RINGTONE:
217        return STRATEGY_SONIFICATION;
218
219    case AUDIO_USAGE_NOTIFICATION:
220    case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_REQUEST:
221    case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_INSTANT:
222    case AUDIO_USAGE_NOTIFICATION_COMMUNICATION_DELAYED:
223    case AUDIO_USAGE_NOTIFICATION_EVENT:
224        return STRATEGY_SONIFICATION_RESPECTFUL;
225
226    case AUDIO_USAGE_UNKNOWN:
227    default:
228        return STRATEGY_MEDIA;
229    }
230}
231
232audio_devices_t Engine::getDeviceForStrategy(routing_strategy strategy) const
233{
234    DeviceVector availableOutputDevices = mApmObserver->getAvailableOutputDevices();
235    DeviceVector availableInputDevices = mApmObserver->getAvailableInputDevices();
236
237    const SwAudioOutputCollection &outputs = mApmObserver->getOutputs();
238
239    return getDeviceForStrategyInt(strategy, availableOutputDevices,
240                                   availableInputDevices, outputs);
241}
242
243
244
245audio_devices_t Engine::getDeviceForStrategyInt(routing_strategy strategy,
246                                                DeviceVector availableOutputDevices,
247                                                DeviceVector availableInputDevices,
248                                                const SwAudioOutputCollection &outputs) const
249{
250    uint32_t device = AUDIO_DEVICE_NONE;
251    uint32_t availableOutputDevicesType = availableOutputDevices.types();
252
253    switch (strategy) {
254
255    case STRATEGY_TRANSMITTED_THROUGH_SPEAKER:
256        device = availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER;
257        if (!device) {
258            ALOGE("getDeviceForStrategy() no device found for "\
259                    "STRATEGY_TRANSMITTED_THROUGH_SPEAKER");
260        }
261        break;
262
263    case STRATEGY_SONIFICATION_RESPECTFUL:
264        if (isInCall()) {
265            device = getDeviceForStrategyInt(
266                    STRATEGY_SONIFICATION, availableOutputDevices, availableInputDevices, outputs);
267        } else if (outputs.isStreamActiveRemotely(AUDIO_STREAM_MUSIC,
268                SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
269            // while media is playing on a remote device, use the the sonification behavior.
270            // Note that we test this usecase before testing if media is playing because
271            //   the isStreamActive() method only informs about the activity of a stream, not
272            //   if it's for local playback. Note also that we use the same delay between both tests
273            device = getDeviceForStrategyInt(
274                    STRATEGY_SONIFICATION, availableOutputDevices, availableInputDevices, outputs);
275            //user "safe" speaker if available instead of normal speaker to avoid triggering
276            //other acoustic safety mechanisms for notification
277            if ((device & AUDIO_DEVICE_OUT_SPEAKER) &&
278                    (availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER_SAFE)) {
279                device |= AUDIO_DEVICE_OUT_SPEAKER_SAFE;
280                device &= ~AUDIO_DEVICE_OUT_SPEAKER;
281            }
282        } else if (outputs.isStreamActive(
283                                AUDIO_STREAM_MUSIC, SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
284            // while media is playing (or has recently played), use the same device
285            device = getDeviceForStrategyInt(
286                    STRATEGY_MEDIA, availableOutputDevices, availableInputDevices, outputs);
287        } else {
288            // when media is not playing anymore, fall back on the sonification behavior
289            device = getDeviceForStrategyInt(
290                    STRATEGY_SONIFICATION, availableOutputDevices, availableInputDevices, outputs);
291            //user "safe" speaker if available instead of normal speaker to avoid triggering
292            //other acoustic safety mechanisms for notification
293            if ((device & AUDIO_DEVICE_OUT_SPEAKER) &&
294                    (availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER_SAFE)) {
295                device |= AUDIO_DEVICE_OUT_SPEAKER_SAFE;
296                device &= ~AUDIO_DEVICE_OUT_SPEAKER;
297            }
298        }
299        break;
300
301    case STRATEGY_DTMF:
302        if (!isInCall()) {
303            // when off call, DTMF strategy follows the same rules as MEDIA strategy
304            device = getDeviceForStrategyInt(
305                    STRATEGY_MEDIA, availableOutputDevices, availableInputDevices, outputs);
306            break;
307        }
308        // when in call, DTMF and PHONE strategies follow the same rules
309        // FALL THROUGH
310
311    case STRATEGY_PHONE:
312        // Force use of only devices on primary output if:
313        // - in call AND
314        //   - cannot route from voice call RX OR
315        //   - audio HAL version is < 3.0 and TX device is on the primary HW module
316        if (getPhoneState() == AUDIO_MODE_IN_CALL) {
317            audio_devices_t txDevice = getDeviceForInputSource(AUDIO_SOURCE_VOICE_COMMUNICATION);
318            sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput();
319            audio_devices_t availPrimaryInputDevices =
320                 availableInputDevices.getDevicesFromHwModule(primaryOutput->getModuleHandle());
321            audio_devices_t availPrimaryOutputDevices =
322                    primaryOutput->supportedDevices() & availableOutputDevices.types();
323
324            if (((availableInputDevices.types() &
325                    AUDIO_DEVICE_IN_TELEPHONY_RX & ~AUDIO_DEVICE_BIT_IN) == 0) ||
326                    (((txDevice & availPrimaryInputDevices & ~AUDIO_DEVICE_BIT_IN) != 0) &&
327                         (primaryOutput->getAudioPort()->getModuleVersion() <
328                             AUDIO_DEVICE_API_VERSION_3_0))) {
329                availableOutputDevicesType = availPrimaryOutputDevices;
330            }
331        }
332        // for phone strategy, we first consider the forced use and then the available devices by
333        // order of priority
334        switch (mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]) {
335        case AUDIO_POLICY_FORCE_BT_SCO:
336            if (!isInCall() || strategy != STRATEGY_DTMF) {
337                device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT;
338                if (device) break;
339            }
340            device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET;
341            if (device) break;
342            device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_SCO;
343            if (device) break;
344            // if SCO device is requested but no SCO device is available, fall back to default case
345            // FALL THROUGH
346
347        default:    // FORCE_NONE
348            // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to A2DP
349            if (!isInCall() &&
350                    (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) &&
351                    (outputs.getA2dpOutput() != 0)) {
352                device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
353                if (device) break;
354                device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
355                if (device) break;
356            }
357            device = availableOutputDevicesType & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
358            if (device) break;
359            device = availableOutputDevicesType & AUDIO_DEVICE_OUT_WIRED_HEADSET;
360            if (device) break;
361            device = availableOutputDevicesType & AUDIO_DEVICE_OUT_LINE;
362            if (device) break;
363            device = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_DEVICE;
364            if (device) break;
365            if (!isInCall()) {
366                device = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_ACCESSORY;
367                if (device) break;
368                device = availableOutputDevicesType & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
369                if (device) break;
370                device = availableOutputDevicesType & AUDIO_DEVICE_OUT_AUX_DIGITAL;
371                if (device) break;
372                device = availableOutputDevicesType & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
373                if (device) break;
374            }
375            device = availableOutputDevicesType & AUDIO_DEVICE_OUT_EARPIECE;
376            if (device) break;
377            device = mApmObserver->getDefaultOutputDevice()->type();
378            if (device == AUDIO_DEVICE_NONE) {
379                ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE");
380            }
381            break;
382
383        case AUDIO_POLICY_FORCE_SPEAKER:
384            // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to
385            // A2DP speaker when forcing to speaker output
386            if (!isInCall() &&
387                    (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) &&
388                    (outputs.getA2dpOutput() != 0)) {
389                device = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
390                if (device) break;
391            }
392            if (!isInCall()) {
393                device = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_ACCESSORY;
394                if (device) break;
395                device = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_DEVICE;
396                if (device) break;
397                device = availableOutputDevicesType & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
398                if (device) break;
399                device = availableOutputDevicesType & AUDIO_DEVICE_OUT_AUX_DIGITAL;
400                if (device) break;
401                device = availableOutputDevicesType & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
402                if (device) break;
403            }
404            device = availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER;
405            if (device) break;
406            device = mApmObserver->getDefaultOutputDevice()->type();
407            if (device == AUDIO_DEVICE_NONE) {
408                ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE, FORCE_SPEAKER");
409            }
410            break;
411        }
412    break;
413
414    case STRATEGY_SONIFICATION:
415
416        // If incall, just select the STRATEGY_PHONE device: The rest of the behavior is handled by
417        // handleIncallSonification().
418        if (isInCall()) {
419            device = getDeviceForStrategyInt(
420                    STRATEGY_PHONE, availableOutputDevices, availableInputDevices, outputs);
421            break;
422        }
423        // FALL THROUGH
424
425    case STRATEGY_ENFORCED_AUDIBLE:
426        // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION
427        // except:
428        //   - when in call where it doesn't default to STRATEGY_PHONE behavior
429        //   - in countries where not enforced in which case it follows STRATEGY_MEDIA
430
431        if ((strategy == STRATEGY_SONIFICATION) ||
432                (mForceUse[AUDIO_POLICY_FORCE_FOR_SYSTEM] == AUDIO_POLICY_FORCE_SYSTEM_ENFORCED)) {
433            device = availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER;
434            if (device == AUDIO_DEVICE_NONE) {
435                ALOGE("getDeviceForStrategy() speaker device not found for STRATEGY_SONIFICATION");
436            }
437        }
438        // The second device used for sonification is the same as the device used by media strategy
439        // FALL THROUGH
440
441    case STRATEGY_ACCESSIBILITY:
442        if (strategy == STRATEGY_ACCESSIBILITY) {
443            // do not route accessibility prompts to a digital output currently configured with a
444            // compressed format as they would likely not be mixed and dropped.
445            for (size_t i = 0; i < outputs.size(); i++) {
446                sp<AudioOutputDescriptor> desc = outputs.valueAt(i);
447                audio_devices_t devices = desc->device() &
448                    (AUDIO_DEVICE_OUT_HDMI | AUDIO_DEVICE_OUT_SPDIF | AUDIO_DEVICE_OUT_HDMI_ARC);
449                if (desc->isActive() && !audio_is_linear_pcm(desc->mFormat) &&
450                        devices != AUDIO_DEVICE_NONE) {
451                    availableOutputDevicesType = availableOutputDevices.types() & ~devices;
452                }
453            }
454            availableOutputDevices =
455                    availableOutputDevices.getDevicesFromType(availableOutputDevicesType);
456            if (outputs.isStreamActive(AUDIO_STREAM_RING) ||
457                    outputs.isStreamActive(AUDIO_STREAM_ALARM)) {
458                return getDeviceForStrategyInt(
459                    STRATEGY_SONIFICATION, availableOutputDevices, availableInputDevices, outputs);
460            }
461            if (isInCall()) {
462                return getDeviceForStrategyInt(
463                        STRATEGY_PHONE, availableOutputDevices, availableInputDevices, outputs);
464            }
465        }
466        // For other cases, STRATEGY_ACCESSIBILITY behaves like STRATEGY_MEDIA
467        // FALL THROUGH
468
469    // FIXME: STRATEGY_REROUTING follow STRATEGY_MEDIA for now
470    case STRATEGY_REROUTING:
471    case STRATEGY_MEDIA: {
472        uint32_t device2 = AUDIO_DEVICE_NONE;
473        if (strategy != STRATEGY_SONIFICATION) {
474            // no sonification on remote submix (e.g. WFD)
475            if (availableOutputDevices.getDevice(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
476                                                 String8("0")) != 0) {
477                device2 = availableOutputDevices.types() & AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
478            }
479        }
480        if (isInCall() && (strategy == STRATEGY_MEDIA)) {
481            device = getDeviceForStrategyInt(
482                    STRATEGY_PHONE, availableOutputDevices, availableInputDevices, outputs);
483            break;
484        }
485        if ((device2 == AUDIO_DEVICE_NONE) &&
486                (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] != AUDIO_POLICY_FORCE_NO_BT_A2DP) &&
487                (outputs.getA2dpOutput() != 0)) {
488            device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
489            if (device2 == AUDIO_DEVICE_NONE) {
490                device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
491            }
492            if (device2 == AUDIO_DEVICE_NONE) {
493                device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
494            }
495        }
496        if ((device2 == AUDIO_DEVICE_NONE) &&
497            (mForceUse[AUDIO_POLICY_FORCE_FOR_MEDIA] == AUDIO_POLICY_FORCE_SPEAKER)) {
498            device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER;
499        }
500        if (device2 == AUDIO_DEVICE_NONE) {
501            device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
502        }
503        if (device2 == AUDIO_DEVICE_NONE) {
504            device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_LINE;
505        }
506        if (device2 == AUDIO_DEVICE_NONE) {
507            device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_WIRED_HEADSET;
508        }
509        if (device2 == AUDIO_DEVICE_NONE) {
510            device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_ACCESSORY;
511        }
512        if (device2 == AUDIO_DEVICE_NONE) {
513            device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_USB_DEVICE;
514        }
515        if (device2 == AUDIO_DEVICE_NONE) {
516            device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
517        }
518        if ((device2 == AUDIO_DEVICE_NONE) && (strategy != STRATEGY_SONIFICATION)) {
519            // no sonification on aux digital (e.g. HDMI)
520            device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_AUX_DIGITAL;
521        }
522        if ((device2 == AUDIO_DEVICE_NONE) &&
523                (mForceUse[AUDIO_POLICY_FORCE_FOR_DOCK] == AUDIO_POLICY_FORCE_ANALOG_DOCK)) {
524            device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
525        }
526        if (device2 == AUDIO_DEVICE_NONE) {
527            device2 = availableOutputDevicesType & AUDIO_DEVICE_OUT_SPEAKER;
528        }
529        int device3 = AUDIO_DEVICE_NONE;
530        if (strategy == STRATEGY_MEDIA) {
531            // ARC, SPDIF and AUX_LINE can co-exist with others.
532            device3 = availableOutputDevicesType & AUDIO_DEVICE_OUT_HDMI_ARC;
533            device3 |= (availableOutputDevicesType & AUDIO_DEVICE_OUT_SPDIF);
534            device3 |= (availableOutputDevicesType & AUDIO_DEVICE_OUT_AUX_LINE);
535        }
536
537        device2 |= device3;
538        // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or
539        // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise
540        device |= device2;
541
542        // If hdmi system audio mode is on, remove speaker out of output list.
543        if ((strategy == STRATEGY_MEDIA) &&
544            (mForceUse[AUDIO_POLICY_FORCE_FOR_HDMI_SYSTEM_AUDIO] ==
545                AUDIO_POLICY_FORCE_HDMI_SYSTEM_AUDIO_ENFORCED)) {
546            device &= ~AUDIO_DEVICE_OUT_SPEAKER;
547        }
548
549        if (device) break;
550        device = mApmObserver->getDefaultOutputDevice()->type();
551        if (device == AUDIO_DEVICE_NONE) {
552            ALOGE("getDeviceForStrategy() no device found for STRATEGY_MEDIA");
553        }
554        } break;
555
556    default:
557        ALOGW("getDeviceForStrategy() unknown strategy: %d", strategy);
558        break;
559    }
560
561    ALOGVV("getDeviceForStrategy() strategy %d, device %x", strategy, device);
562    return device;
563}
564
565
566audio_devices_t Engine::getDeviceForInputSource(audio_source_t inputSource) const
567{
568    const DeviceVector &availableOutputDevices = mApmObserver->getAvailableOutputDevices();
569    const DeviceVector &availableInputDevices = mApmObserver->getAvailableInputDevices();
570    const SwAudioOutputCollection &outputs = mApmObserver->getOutputs();
571    audio_devices_t availableDeviceTypes = availableInputDevices.types() & ~AUDIO_DEVICE_BIT_IN;
572
573    uint32_t device = AUDIO_DEVICE_NONE;
574
575    switch (inputSource) {
576    case AUDIO_SOURCE_VOICE_UPLINK:
577      if (availableDeviceTypes & AUDIO_DEVICE_IN_VOICE_CALL) {
578          device = AUDIO_DEVICE_IN_VOICE_CALL;
579          break;
580      }
581      break;
582
583    case AUDIO_SOURCE_DEFAULT:
584    case AUDIO_SOURCE_MIC:
585    if (availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_A2DP) {
586        device = AUDIO_DEVICE_IN_BLUETOOTH_A2DP;
587    } else if ((mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] == AUDIO_POLICY_FORCE_BT_SCO) &&
588        (availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET)) {
589        device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
590    } else if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) {
591        device = AUDIO_DEVICE_IN_WIRED_HEADSET;
592    } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) {
593        device = AUDIO_DEVICE_IN_USB_DEVICE;
594    } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
595        device = AUDIO_DEVICE_IN_BUILTIN_MIC;
596    }
597    break;
598
599    case AUDIO_SOURCE_VOICE_COMMUNICATION:
600        // Allow only use of devices on primary input if in call and HAL does not support routing
601        // to voice call path.
602        if ((getPhoneState() == AUDIO_MODE_IN_CALL) &&
603                (availableOutputDevices.types() & AUDIO_DEVICE_OUT_TELEPHONY_TX) == 0) {
604            sp<AudioOutputDescriptor> primaryOutput = outputs.getPrimaryOutput();
605            availableDeviceTypes =
606                    availableInputDevices.getDevicesFromHwModule(primaryOutput->getModuleHandle())
607                    & ~AUDIO_DEVICE_BIT_IN;
608        }
609
610        switch (mForceUse[AUDIO_POLICY_FORCE_FOR_COMMUNICATION]) {
611        case AUDIO_POLICY_FORCE_BT_SCO:
612            // if SCO device is requested but no SCO device is available, fall back to default case
613            if (availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
614                device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
615                break;
616            }
617            // FALL THROUGH
618
619        default:    // FORCE_NONE
620            if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) {
621                device = AUDIO_DEVICE_IN_WIRED_HEADSET;
622            } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) {
623                device = AUDIO_DEVICE_IN_USB_DEVICE;
624            } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
625                device = AUDIO_DEVICE_IN_BUILTIN_MIC;
626            }
627            break;
628
629        case AUDIO_POLICY_FORCE_SPEAKER:
630            if (availableDeviceTypes & AUDIO_DEVICE_IN_BACK_MIC) {
631                device = AUDIO_DEVICE_IN_BACK_MIC;
632            } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
633                device = AUDIO_DEVICE_IN_BUILTIN_MIC;
634            }
635            break;
636        }
637        break;
638
639    case AUDIO_SOURCE_VOICE_RECOGNITION:
640    case AUDIO_SOURCE_UNPROCESSED:
641    case AUDIO_SOURCE_HOTWORD:
642        if (mForceUse[AUDIO_POLICY_FORCE_FOR_RECORD] == AUDIO_POLICY_FORCE_BT_SCO &&
643                availableDeviceTypes & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
644            device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
645        } else if (availableDeviceTypes & AUDIO_DEVICE_IN_WIRED_HEADSET) {
646            device = AUDIO_DEVICE_IN_WIRED_HEADSET;
647        } else if (availableDeviceTypes & AUDIO_DEVICE_IN_USB_DEVICE) {
648            device = AUDIO_DEVICE_IN_USB_DEVICE;
649        } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
650            device = AUDIO_DEVICE_IN_BUILTIN_MIC;
651        }
652        break;
653    case AUDIO_SOURCE_CAMCORDER:
654        if (availableDeviceTypes & AUDIO_DEVICE_IN_BACK_MIC) {
655            device = AUDIO_DEVICE_IN_BACK_MIC;
656        } else if (availableDeviceTypes & AUDIO_DEVICE_IN_BUILTIN_MIC) {
657            device = AUDIO_DEVICE_IN_BUILTIN_MIC;
658        }
659        break;
660    case AUDIO_SOURCE_VOICE_DOWNLINK:
661    case AUDIO_SOURCE_VOICE_CALL:
662        if (availableDeviceTypes & AUDIO_DEVICE_IN_VOICE_CALL) {
663            device = AUDIO_DEVICE_IN_VOICE_CALL;
664        }
665        break;
666    case AUDIO_SOURCE_REMOTE_SUBMIX:
667        if (availableDeviceTypes & AUDIO_DEVICE_IN_REMOTE_SUBMIX) {
668            device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
669        }
670        break;
671     case AUDIO_SOURCE_FM_TUNER:
672        if (availableDeviceTypes & AUDIO_DEVICE_IN_FM_TUNER) {
673            device = AUDIO_DEVICE_IN_FM_TUNER;
674        }
675        break;
676    default:
677        ALOGW("getDeviceForInputSource() invalid input source %d", inputSource);
678        break;
679    }
680    ALOGV("getDeviceForInputSource()input source %d, device %08x", inputSource, device);
681    return device;
682}
683
684template <>
685AudioPolicyManagerInterface *Engine::queryInterface()
686{
687    return &mManagerInterface;
688}
689
690} // namespace audio_policy
691} // namespace android
692
693
694