AudioPolicyManagerBase.cpp revision 17a73c3394547692457299dc512b5c2312ea0344
1f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin/*
2f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin * Copyright (C) 2009 The Android Open Source Project
3f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin *
4f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin * Licensed under the Apache License, Version 2.0 (the "License");
5f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin * you may not use this file except in compliance with the License.
6f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin * You may obtain a copy of the License at
7f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin *
8f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin *      http://www.apache.org/licenses/LICENSE-2.0
9f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin *
10f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin * Unless required by applicable law or agreed to in writing, software
11f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin * distributed under the License is distributed on an "AS IS" BASIS,
12f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin * See the License for the specific language governing permissions and
14f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin * limitations under the License.
15f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin */
16f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
17f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#define LOG_TAG "AudioPolicyManagerBase"
18f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin//#define LOG_NDEBUG 0
193cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
203cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent//#define VERY_VERBOSE_LOGGING
213cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent#ifdef VERY_VERBOSE_LOGGING
223cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent#define ALOGVV ALOGV
233cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent#else
243cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent#define ALOGVV(a...) do { } while(0)
253cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent#endif
263cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
27f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#include <utils/Log.h>
28f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#include <hardware_legacy/AudioPolicyManagerBase.h>
291c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent#include <hardware/audio_effect.h>
3008b014d9e509c9163db6b33a63852e73db4d07ccEric Laurent#include <hardware/audio.h>
31f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#include <math.h>
325ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent#include <hardware_legacy/audio_policy_conf.h>
33f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
34c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi// For code clarity in AudioPolicyManager: a value to indicate no audio device (both for input
35c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi//    and output) when comparing audio_devices_t mask values
36c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi#define APM_AUDIO_DEVICE_NONE 0
37c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi
38e81531e91ecae92aff471dbff9cbeb0f95ff4a80Dima Zavinnamespace android_audio_legacy {
39f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
40f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// ----------------------------------------------------------------------------
41f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// AudioPolicyInterface implementation
42f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// ----------------------------------------------------------------------------
43f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
44f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
45c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivistatus_t AudioPolicyManagerBase::setDeviceConnectionState(audio_devices_t device,
46f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                  AudioSystem::device_connection_state state,
47f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                  const char *device_address)
48f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
493cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    SortedVector <audio_io_handle_t> outputs;
50f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
516a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("setDeviceConnectionState() device: %x, state %d, address %s", device, state, device_address);
52f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
53f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // connect/disconnect only 1 device at a time
54f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (AudioSystem::popCount(device) != 1) return BAD_VALUE;
55f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
56f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (strlen(device_address) >= MAX_DEVICE_ADDRESS_LEN) {
575efbd421e0029d6fc44b1cc65c0e5e0d85e5161fSteve Block        ALOGE("setDeviceConnectionState() invalid address: %s", device_address);
58f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
59f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
60f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
61f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // handle output devices
62c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    if (audio_is_output_device(device)) {
63f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
64c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        if (!mHasA2dp && audio_is_a2dp_device(device)) {
655efbd421e0029d6fc44b1cc65c0e5e0d85e5161fSteve Block            ALOGE("setDeviceConnectionState() invalid device: %x", device);
66f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return BAD_VALUE;
67f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
68c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        if (!mHasUsb && audio_is_usb_device(device)) {
69599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            ALOGE("setDeviceConnectionState() invalid device: %x", device);
70599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            return BAD_VALUE;
71599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent        }
72f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
73c952527e6f89d5427881462823514be9d79f13e6Eric Laurent        // save a copy of the opened output descriptors before any output is opened or closed
74c952527e6f89d5427881462823514be9d79f13e6Eric Laurent        // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies()
75c952527e6f89d5427881462823514be9d79f13e6Eric Laurent        mPreviousOutputs = mOutputs;
76f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        switch (state)
77f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        {
78f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // handle output device connection
79f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        case AudioSystem::DEVICE_STATE_AVAILABLE:
80f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (mAvailableOutputDevices & device) {
8164cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block                ALOGW("setDeviceConnectionState() device already connected: %x", device);
82f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                return INVALID_OPERATION;
83f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
846a70518b93928d1c91457ff805e375c82d76b0e5Steve Block            ALOGV("setDeviceConnectionState() connecting device %x", device);
85f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
86c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            if (checkOutputsForDevice(device, state, outputs) != NO_ERROR) {
873cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                return INVALID_OPERATION;
883cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
893cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %d outputs",
903cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                  outputs.size());
91f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // register new device as available
92f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent            mAvailableOutputDevices = (audio_devices_t)(mAvailableOutputDevices | device);
93f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
943cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            if (!outputs.isEmpty()) {
953cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                String8 paramStr;
96c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                if (mHasA2dp && audio_is_a2dp_device(device)) {
973cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    // handle A2DP device connection
983cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    AudioParameter param;
993cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    param.add(String8(AUDIO_PARAMETER_A2DP_SINK_ADDRESS), String8(device_address));
1003cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    paramStr = param.toString();
1013cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    mA2dpDeviceAddress = String8(device_address, MAX_DEVICE_ADDRESS_LEN);
1023cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    mA2dpSuspended = false;
103c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                } else if (audio_is_bluetooth_sco_device(device)) {
1043cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    // handle SCO device connection
1053cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    mScoDeviceAddress = String8(device_address, MAX_DEVICE_ADDRESS_LEN);
106c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                } else if (mHasUsb && audio_is_usb_device(device)) {
1073cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    // handle USB device connection
1083cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    mUsbCardAndDevice = String8(device_address, MAX_DEVICE_ADDRESS_LEN);
1093cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    paramStr = mUsbCardAndDevice;
1103cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                }
1113cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                if (!paramStr.isEmpty()) {
1123cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    for (size_t i = 0; i < outputs.size(); i++) {
1133cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        mpClientInterface->setParameters(outputs[i], paramStr);
1143cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
1153cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                }
116f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
117f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            break;
118f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // handle output device disconnection
119f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        case AudioSystem::DEVICE_STATE_UNAVAILABLE: {
120f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (!(mAvailableOutputDevices & device)) {
12164cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block                ALOGW("setDeviceConnectionState() device not connected: %x", device);
122f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                return INVALID_OPERATION;
123f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
124f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1256a70518b93928d1c91457ff805e375c82d76b0e5Steve Block            ALOGV("setDeviceConnectionState() disconnecting device %x", device);
126f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // remove device from available output devices
127f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent            mAvailableOutputDevices = (audio_devices_t)(mAvailableOutputDevices & ~device);
128f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
129c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            checkOutputsForDevice(device, state, outputs);
130c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            if (mHasA2dp && audio_is_a2dp_device(device)) {
1313cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                // handle A2DP device disconnection
132b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                mA2dpDeviceAddress = "";
133b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                mA2dpSuspended = false;
134c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            } else if (audio_is_bluetooth_sco_device(device)) {
1353cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                // handle SCO device disconnection
136b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                mScoDeviceAddress = "";
137c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            } else if (mHasUsb && audio_is_usb_device(device)) {
1383cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                // handle USB device disconnection
139599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent                mUsbCardAndDevice = "";
140f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
141f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            } break;
142f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
143f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        default:
1445efbd421e0029d6fc44b1cc65c0e5e0d85e5161fSteve Block            ALOGE("setDeviceConnectionState() invalid state: %x", state);
145f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return BAD_VALUE;
146f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
147f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
148f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        checkA2dpSuspend();
149f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        checkOutputForAllStrategies();
150b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        // outputs must be closed after checkOutputForAllStrategies() is executed
1513cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        if (!outputs.isEmpty()) {
1523cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            for (size_t i = 0; i < outputs.size(); i++) {
1533cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                // close unused outputs after device disconnection or direct outputs that have been
1543cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                // opened by checkOutputsForDevice() to query dynamic parameters
1553cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                if ((state == AudioSystem::DEVICE_STATE_UNAVAILABLE) ||
1563cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        (mOutputs.valueFor(outputs[i])->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
1573cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    closeOutput(outputs[i]);
1583cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                }
1593cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
160f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1615ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
162c952527e6f89d5427881462823514be9d79f13e6Eric Laurent        updateDevicesAndOutputs();
1635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        for (size_t i = 0; i < mOutputs.size(); i++) {
1645ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            setOutputDevice(mOutputs.keyAt(i), getNewDevice(mOutputs.keyAt(i), true /*fromCache*/));
1655ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
166f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
167c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        if (device == AUDIO_DEVICE_OUT_WIRED_HEADSET) {
168c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = AUDIO_DEVICE_IN_WIRED_HEADSET;
169c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        } else if (device == AUDIO_DEVICE_OUT_BLUETOOTH_SCO ||
170c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                   device == AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET ||
171c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                   device == AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT) {
172c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
173f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        } else {
174f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return NO_ERROR;
175f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
176f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
177f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // handle input devices
178c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    if (audio_is_input_device(device)) {
179f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
180f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        switch (state)
181f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        {
182f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // handle input device connection
183f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        case AudioSystem::DEVICE_STATE_AVAILABLE: {
184f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (mAvailableInputDevices & device) {
18564cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block                ALOGW("setDeviceConnectionState() device already connected: %d", device);
186f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                return INVALID_OPERATION;
187f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1885ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            mAvailableInputDevices = (audio_devices_t)(mAvailableInputDevices | device);
189f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
190f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            break;
191f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
192f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // handle input device disconnection
193f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        case AudioSystem::DEVICE_STATE_UNAVAILABLE: {
194f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (!(mAvailableInputDevices & device)) {
19564cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block                ALOGW("setDeviceConnectionState() device not connected: %d", device);
196f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                return INVALID_OPERATION;
197f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            mAvailableInputDevices = (audio_devices_t) (mAvailableInputDevices & ~device);
199f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            } break;
200f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
201f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        default:
2025efbd421e0029d6fc44b1cc65c0e5e0d85e5161fSteve Block            ALOGE("setDeviceConnectionState() invalid state: %x", state);
203f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return BAD_VALUE;
204f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
205f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
206f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        audio_io_handle_t activeInput = getActiveInput();
207f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (activeInput != 0) {
208f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            AudioInputDescriptor *inputDesc = mInputs.valueFor(activeInput);
209f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent            audio_devices_t newDevice = getDeviceForInputSource(inputDesc->mInputSource);
210c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            if ((newDevice != APM_AUDIO_DEVICE_NONE) && (newDevice != inputDesc->mDevice)) {
2116a70518b93928d1c91457ff805e375c82d76b0e5Steve Block                ALOGV("setDeviceConnectionState() changing device from %x to %x for input %d",
212f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        inputDesc->mDevice, newDevice, activeInput);
213f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                inputDesc->mDevice = newDevice;
214f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                AudioParameter param = AudioParameter();
215f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.addInt(String8(AudioParameter::keyRouting), (int)newDevice);
216f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mpClientInterface->setParameters(activeInput, param.toString());
217f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
218f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
219f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
220f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return NO_ERROR;
221f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
222f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
22364cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block    ALOGW("setDeviceConnectionState() invalid device: %x", device);
224f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return BAD_VALUE;
225f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
226f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
227c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel TriviAudioSystem::device_connection_state AudioPolicyManagerBase::getDeviceConnectionState(audio_devices_t device,
228f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                  const char *device_address)
229f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
230f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioSystem::device_connection_state state = AudioSystem::DEVICE_STATE_UNAVAILABLE;
231f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    String8 address = String8(device_address);
232c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    if (audio_is_output_device(device)) {
233f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (device & mAvailableOutputDevices) {
234c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            if (audio_is_a2dp_device(device) &&
2355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                (!mHasA2dp || (address != "" && mA2dpDeviceAddress != address))) {
236f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                return state;
237f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
238c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            if (audio_is_bluetooth_sco_device(device) &&
239f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                address != "" && mScoDeviceAddress != address) {
240f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                return state;
241f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
242c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            if (audio_is_usb_device(device) &&
243599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent                (!mHasUsb || (address != "" && mUsbCardAndDevice != address))) {
244599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent                ALOGE("setDeviceConnectionState() invalid device: %x", device);
245599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent                return state;
246599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            }
247f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            state = AudioSystem::DEVICE_STATE_AVAILABLE;
248f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
249c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    } else if (audio_is_input_device(device)) {
250f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (device & mAvailableInputDevices) {
251f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            state = AudioSystem::DEVICE_STATE_AVAILABLE;
252f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
253f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
254f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
255f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return state;
256f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
257f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
258f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::setPhoneState(int state)
259f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2606a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("setPhoneState() state %d", state);
261c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    audio_devices_t newDevice = APM_AUDIO_DEVICE_NONE;
262f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (state < 0 || state >= AudioSystem::NUM_MODES) {
26364cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("setPhoneState() invalid state %d", state);
264f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
265f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
266f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
267f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (state == mPhoneState ) {
26864cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("setPhoneState() setting same state %d", state);
269f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
270f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
271f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
272f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if leaving call state, handle special case of active streams
273f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // pertaining to sonification strategy see handleIncallSonification()
274f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (isInCall()) {
2756a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("setPhoneState() in call state management: new state is %d", state);
276f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
277f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            handleIncallSonification(stream, false, true);
278f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
279f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
280f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
281f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // store previous phone state for management of sonification strategy below
282f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    int oldState = mPhoneState;
283f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mPhoneState = state;
284f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    bool force = false;
285f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
286f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // are we entering or starting a call
287f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (!isStateInCall(oldState) && isStateInCall(state)) {
2886a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("  Entering call in setPhoneState()");
289f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // force routing command to audio hardware when starting a call
290f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // even if no device change is needed
291f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        force = true;
292f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else if (isStateInCall(oldState) && !isStateInCall(state)) {
2936a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("  Exiting call in setPhoneState()");
294f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // force routing command to audio hardware when exiting a call
295f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // even if no device change is needed
296f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        force = true;
297f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else if (isStateInCall(state) && (state != oldState)) {
2986a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("  Switching between telephony and VoIP in setPhoneState()");
299f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // force routing command to audio hardware when switching between telephony and VoIP
300f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // even if no device change is needed
301f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        force = true;
302f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
303f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
304f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // check for device and output changes triggered by new phone state
3055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    newDevice = getNewDevice(mPrimaryOutput, false /*fromCache*/);
306f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkA2dpSuspend();
307f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForAllStrategies();
308c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    updateDevicesAndOutputs();
309f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
310b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    AudioOutputDescriptor *hwOutputDesc = mOutputs.valueFor(mPrimaryOutput);
311f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
312f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // force routing command to audio hardware when ending call
313f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // even if no device change is needed
314c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    if (isStateInCall(oldState) && newDevice == APM_AUDIO_DEVICE_NONE) {
315f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        newDevice = hwOutputDesc->device();
316f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
317f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
318f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // when changing from ring tone to in call mode, mute the ringing tone
319f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // immediately and delay the route change to avoid sending the ring tone
320f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // tail into the earpiece or headset.
321f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    int delayMs = 0;
322f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (isStateInCall(state) && oldState == AudioSystem::MODE_RINGTONE) {
323f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // delay the device change command by twice the output latency to have some margin
324f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // and be sure that audio buffers not yet affected by the mute are out when
325f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // we actually apply the route change
326f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        delayMs = hwOutputDesc->mLatency*2;
327b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        setStreamMute(AudioSystem::RING, true, mPrimaryOutput);
328f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
329f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
330f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // change routing is necessary
331b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    setOutputDevice(mPrimaryOutput, newDevice, force, delayMs);
332f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
333f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if entering in call state, handle special case of active streams
334f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // pertaining to sonification strategy see handleIncallSonification()
335f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (isStateInCall(state)) {
3366a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("setPhoneState() in call state management: new state is %d", state);
337f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // unmute the ringing tone after a sufficient delay if it was muted before
338f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // setting output device above
339f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (oldState == AudioSystem::MODE_RINGTONE) {
340b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            setStreamMute(AudioSystem::RING, false, mPrimaryOutput, MUTE_TIME_MS);
341f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
342f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
343f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            handleIncallSonification(stream, true, true);
344f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
345f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
346f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
347f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
348f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (state == AudioSystem::MODE_RINGTONE &&
349f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        isStreamActive(AudioSystem::MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
350f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mLimitRingtoneVolume = true;
351f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
352f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mLimitRingtoneVolume = false;
353f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
354f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
355f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
356f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::setForceUse(AudioSystem::force_use usage, AudioSystem::forced_config config)
357f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3586a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mPhoneState);
359f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
360f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    bool forceVolumeReeval = false;
361f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    switch(usage) {
362f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::FOR_COMMUNICATION:
363f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (config != AudioSystem::FORCE_SPEAKER && config != AudioSystem::FORCE_BT_SCO &&
364f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_NONE) {
36564cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("setForceUse() invalid config %d for FOR_COMMUNICATION", config);
366f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return;
367f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
368f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        forceVolumeReeval = true;
369f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mForceUse[usage] = config;
370f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
371f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::FOR_MEDIA:
372f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (config != AudioSystem::FORCE_HEADPHONES && config != AudioSystem::FORCE_BT_A2DP &&
373f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_WIRED_ACCESSORY &&
374f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_ANALOG_DOCK &&
3751afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent            config != AudioSystem::FORCE_DIGITAL_DOCK && config != AudioSystem::FORCE_NONE &&
3761afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent            config != AudioSystem::FORCE_NO_BT_A2DP) {
37764cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("setForceUse() invalid config %d for FOR_MEDIA", config);
378f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return;
379f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
380f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mForceUse[usage] = config;
381f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
382f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::FOR_RECORD:
383f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (config != AudioSystem::FORCE_BT_SCO && config != AudioSystem::FORCE_WIRED_ACCESSORY &&
384f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_NONE) {
38564cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("setForceUse() invalid config %d for FOR_RECORD", config);
386f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return;
387f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
388f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mForceUse[usage] = config;
389f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
390f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::FOR_DOCK:
391f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (config != AudioSystem::FORCE_NONE && config != AudioSystem::FORCE_BT_CAR_DOCK &&
392f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_BT_DESK_DOCK &&
393f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_WIRED_ACCESSORY &&
394f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_ANALOG_DOCK &&
395f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_DIGITAL_DOCK) {
39664cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("setForceUse() invalid config %d for FOR_DOCK", config);
397f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
398f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        forceVolumeReeval = true;
399f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mForceUse[usage] = config;
400f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
401f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    default:
40264cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("setForceUse() invalid usage %d", usage);
403f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
404f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
405f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
4065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // check for device and output changes triggered by new force usage
407f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkA2dpSuspend();
408f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForAllStrategies();
409c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    updateDevicesAndOutputs();
4105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < mOutputs.size(); i++) {
4115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_io_handle_t output = mOutputs.keyAt(i);
4125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_devices_t newDevice = getNewDevice(output, true /*fromCache*/);
413c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        setOutputDevice(output, newDevice, (newDevice != APM_AUDIO_DEVICE_NONE));
414c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        if (forceVolumeReeval && (newDevice != APM_AUDIO_DEVICE_NONE)) {
4155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            applyStreamVolumes(output, newDevice, 0, true);
4165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
417f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
418f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
419f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    audio_io_handle_t activeInput = getActiveInput();
420f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (activeInput != 0) {
421f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioInputDescriptor *inputDesc = mInputs.valueFor(activeInput);
4225ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_devices_t newDevice = getDeviceForInputSource(inputDesc->mInputSource);
423c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        if ((newDevice != APM_AUDIO_DEVICE_NONE) && (newDevice != inputDesc->mDevice)) {
4246a70518b93928d1c91457ff805e375c82d76b0e5Steve Block            ALOGV("setForceUse() changing device from %x to %x for input %d",
425f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    inputDesc->mDevice, newDevice, activeInput);
426f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            inputDesc->mDevice = newDevice;
427f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            AudioParameter param = AudioParameter();
428f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            param.addInt(String8(AudioParameter::keyRouting), (int)newDevice);
429f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mpClientInterface->setParameters(activeInput, param.toString());
430f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
431f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
432f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
433f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
434f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
435f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima ZavinAudioSystem::forced_config AudioPolicyManagerBase::getForceUse(AudioSystem::force_use usage)
436f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
437f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return mForceUse[usage];
438f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
439f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
440f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::setSystemProperty(const char* property, const char* value)
441f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
4426a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("setSystemProperty() property %s, value %s", property, value);
443f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (strcmp(property, "ro.camera.sound.forced") == 0) {
444f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (atoi(value)) {
4456a70518b93928d1c91457ff805e375c82d76b0e5Steve Block            ALOGV("ENFORCED_AUDIBLE cannot be muted");
446f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mStreams[AudioSystem::ENFORCED_AUDIBLE].mCanBeMuted = false;
447f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        } else {
4486a70518b93928d1c91457ff805e375c82d76b0e5Steve Block            ALOGV("ENFORCED_AUDIBLE can be muted");
449f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mStreams[AudioSystem::ENFORCED_AUDIBLE].mCanBeMuted = true;
450f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
451f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
452f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
453f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
4543cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric LaurentAudioPolicyManagerBase::IOProfile *AudioPolicyManagerBase::getProfileForDirectOutput(
4553cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                               audio_devices_t device,
45670c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                               uint32_t samplingRate,
45770c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                               uint32_t format,
45870c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                               uint32_t channelMask,
4590977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent                                                               audio_output_flags_t flags)
46070c236c9290732782d5267935af1475b8d5ae602Eric Laurent{
46170c236c9290732782d5267935af1475b8d5ae602Eric Laurent    for (size_t i = 0; i < mHwModules.size(); i++) {
46270c236c9290732782d5267935af1475b8d5ae602Eric Laurent        if (mHwModules[i]->mHandle == 0) {
46370c236c9290732782d5267935af1475b8d5ae602Eric Laurent            continue;
46470c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
46570c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) {
4663cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent           IOProfile *profile = mHwModules[i]->mOutputProfiles[j];
4673cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent           if (profile->isCompatibleProfile(device, samplingRate, format,
4683cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                           channelMask,
4693cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                           AUDIO_OUTPUT_FLAG_DIRECT)) {
4703cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent               if (mAvailableOutputDevices & profile->mSupportedDevices) {
4713cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                   return mHwModules[i]->mOutputProfiles[j];
4723cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent               }
47370c236c9290732782d5267935af1475b8d5ae602Eric Laurent           }
47470c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
47570c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
47670c236c9290732782d5267935af1475b8d5ae602Eric Laurent    return 0;
47770c236c9290732782d5267935af1475b8d5ae602Eric Laurent}
47870c236c9290732782d5267935af1475b8d5ae602Eric Laurent
479f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinaudio_io_handle_t AudioPolicyManagerBase::getOutput(AudioSystem::stream_type stream,
480f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    uint32_t samplingRate,
481f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    uint32_t format,
48270c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                    uint32_t channelMask,
483f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    AudioSystem::output_flags flags)
484f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
485f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    audio_io_handle_t output = 0;
486f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    uint32_t latency = 0;
487f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    routing_strategy strategy = getStrategy((AudioSystem::stream_type)stream);
4885ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
48970c236c9290732782d5267935af1475b8d5ae602Eric Laurent    ALOGV("getOutput() stream %d, samplingRate %d, format %d, channelMask %x, flags %x",
49070c236c9290732782d5267935af1475b8d5ae602Eric Laurent          stream, samplingRate, format, channelMask, flags);
491f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
492f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
493f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mCurOutput != 0) {
49470c236c9290732782d5267935af1475b8d5ae602Eric Laurent        ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
495f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
496f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
497f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (mTestOutputs[mCurOutput] == 0) {
4986a70518b93928d1c91457ff805e375c82d76b0e5Steve Block            ALOGV("getOutput() opening test output");
4993cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(NULL);
500f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            outputDesc->mDevice = mTestDevice;
501f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            outputDesc->mSamplingRate = mTestSamplingRate;
502f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            outputDesc->mFormat = mTestFormat;
50370c236c9290732782d5267935af1475b8d5ae602Eric Laurent            outputDesc->mChannelMask = mTestChannels;
504f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            outputDesc->mLatency = mTestLatencyMs;
5050977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent            outputDesc->mFlags = (audio_output_flags_t)(mDirectOutput ? AudioSystem::OUTPUT_FLAG_DIRECT : 0);
506f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            outputDesc->mRefCount[stream] = 0;
50770c236c9290732782d5267935af1475b8d5ae602Eric Laurent            mTestOutputs[mCurOutput] = mpClientInterface->openOutput(0, &outputDesc->mDevice,
508f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            &outputDesc->mSamplingRate,
509f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            &outputDesc->mFormat,
51070c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                            &outputDesc->mChannelMask,
511f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            &outputDesc->mLatency,
512f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            outputDesc->mFlags);
513f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (mTestOutputs[mCurOutput]) {
514f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                AudioParameter outputCmd = AudioParameter();
515f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                outputCmd.addInt(String8("set_id"),mCurOutput);
516f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
517f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                addOutput(mTestOutputs[mCurOutput], outputDesc);
518f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
519f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
520f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return mTestOutputs[mCurOutput];
521f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
522f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
523f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
524f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // open a direct output if required by specified parameters
5253cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    IOProfile *profile = getProfileForDirectOutput(device,
5263cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                   samplingRate,
5273cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                   format,
5283cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                   channelMask,
5293cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                   (audio_output_flags_t)flags);
5303cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    if (profile != NULL) {
531f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
5326a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("getOutput() opening direct output device %x", device);
53370c236c9290732782d5267935af1475b8d5ae602Eric Laurent
5343cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(profile);
535f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mDevice = device;
536f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mSamplingRate = samplingRate;
53770c236c9290732782d5267935af1475b8d5ae602Eric Laurent        outputDesc->mFormat = (audio_format_t)format;
53870c236c9290732782d5267935af1475b8d5ae602Eric Laurent        outputDesc->mChannelMask = (audio_channel_mask_t)channelMask;
539f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mLatency = 0;
5403cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        outputDesc->mFlags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);;
541f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mRefCount[stream] = 0;
542f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mStopTime[stream] = 0;
5433cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        output = mpClientInterface->openOutput(profile->mModule->mHandle,
54470c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                        &outputDesc->mDevice,
545f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                        &outputDesc->mSamplingRate,
546f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                        &outputDesc->mFormat,
54770c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                        &outputDesc->mChannelMask,
548f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                        &outputDesc->mLatency,
549f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                        outputDesc->mFlags);
550f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
5519f1f9b509c930830f6f32e9ef6c2c8a03d6fa96eJean-Michel Trivi        // only accept an output with the requested parameters
552f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (output == 0 ||
553f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            (samplingRate != 0 && samplingRate != outputDesc->mSamplingRate) ||
554f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            (format != 0 && format != outputDesc->mFormat) ||
55570c236c9290732782d5267935af1475b8d5ae602Eric Laurent            (channelMask != 0 && channelMask != outputDesc->mChannelMask)) {
5563cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
5573cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    "format %d %d, channelMask %04x %04x", output, samplingRate,
5583cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
5593cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    outputDesc->mChannelMask);
560f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (output != 0) {
561f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mpClientInterface->closeOutput(output);
562f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
563f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            delete outputDesc;
564f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return 0;
565f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
566f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        addOutput(output, outputDesc);
5673cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        ALOGV("getOutput() returns direct output %d", output);
568f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return output;
569f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
570f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
5719f1f9b509c930830f6f32e9ef6c2c8a03d6fa96eJean-Michel Trivi    // ignoring channel mask due to downmix capability in mixer
5729f1f9b509c930830f6f32e9ef6c2c8a03d6fa96eJean-Michel Trivi
573f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // open a non direct output
574f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
575f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // get which output is suitable for the specified stream. The actual routing change will happen
576f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // when startOutput() will be called
577c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
578f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
5795ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    output = selectOutput(outputs, flags);
580f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
58170c236c9290732782d5267935af1475b8d5ae602Eric Laurent    ALOGW_IF((output ==0), "getOutput() could not find output for stream %d, samplingRate %d,"
58270c236c9290732782d5267935af1475b8d5ae602Eric Laurent            "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
583f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
5845ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGV("getOutput() returns output %d", output);
5855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
586f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return output;
587f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
588f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
5895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentaudio_io_handle_t AudioPolicyManagerBase::selectOutput(const SortedVector<audio_io_handle_t>& outputs,
5905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                       AudioSystem::output_flags flags)
5915ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
5925ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // select one output among several that provide a path to a particular device or set of
5935ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // devices (the list was previously build by getOutputsForDevice()).
5945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // The priority is as follows:
5955ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // 1: the output with the highest number of requested policy flags
5965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // 2: the primary output
5975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // 3: the first output in the list
5985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
5995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (outputs.size() == 0) {
6005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return 0;
6015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
6025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (outputs.size() == 1) {
6035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return outputs[0];
6045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
6055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
6065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    int maxCommonFlags = 0;
6075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    audio_io_handle_t outputFlags = 0;
6085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    audio_io_handle_t outputPrimary = 0;
6095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
6105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < outputs.size(); i++) {
6115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        AudioOutputDescriptor *outputDesc = mOutputs.valueFor(outputs[i]);
6125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (!outputDesc->isDuplicated()) {
6135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            int commonFlags = (int)AudioSystem::popCount(outputDesc->mProfile->mFlags & flags);
6145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            if (commonFlags > maxCommonFlags) {
6155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                outputFlags = outputs[i];
6165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                maxCommonFlags = commonFlags;
6175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags);
6185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
6190977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent            if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
6205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                outputPrimary = outputs[i];
6215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
6225ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
6235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
6245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
6255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (outputFlags != 0) {
6265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return outputFlags;
6275ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
6285ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (outputPrimary != 0) {
6295ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return outputPrimary;
6305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
6315ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
6325ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return outputs[0];
6335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
6345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
635f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::startOutput(audio_io_handle_t output,
636f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                             AudioSystem::stream_type stream,
637f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                             int session)
638f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
6396a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("startOutput() output %d, stream %d, session %d", output, stream, session);
640f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mOutputs.indexOfKey(output);
641f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
64264cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("startOutput() unknow output %d", output);
643f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
644f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
645f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
646f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index);
647f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
6485ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // increment usage count for this stream on the requested output:
649f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // NOTE that the usage count is the same for duplicated output and hardware output which is
6505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
651f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    outputDesc->changeRefCount(stream, 1);
652f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
6535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (outputDesc->mRefCount[stream] == 1) {
6545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_devices_t newDevice = getNewDevice(output, false /*fromCache*/);
655b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        routing_strategy strategy = getStrategy(stream);
656b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
657b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                            (strategy == STRATEGY_SONIFICATION_RESPECTFUL);
658b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        uint32_t waitMs = 0;
6595ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        bool force = false;
6605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        for (size_t i = 0; i < mOutputs.size(); i++) {
6615ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            AudioOutputDescriptor *desc = mOutputs.valueAt(i);
662b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent            if (desc != outputDesc) {
663b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                // force a device change if any other output is managed by the same hw
664b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                // module and has a current device selection that differs from selected device.
665b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                // In this case, the audio HAL must receive the new device selection so that it can
666b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                // change the device currently selected by the other active output.
667b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                if (outputDesc->sharesHwModuleWith(desc) &&
6685ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                    desc->device() != newDevice) {
669b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                    force = true;
670b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                }
671b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                // wait for audio on other active outputs to be presented when starting
672b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                // a notification so that audio focus effect can propagate.
673b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                if (shouldWait && (desc->refCount() != 0) && (waitMs < desc->latency())) {
674b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                    waitMs = desc->latency();
675b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                }
6765ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
6775ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
678b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        uint32_t muteWaitMs = setOutputDevice(output, newDevice, force);
679f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
6805ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // handle special case for sonification while in call
6815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (isInCall()) {
6825ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            handleIncallSonification(stream, true, false);
6835ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
684c16ac09f510437e8340be691720177a490ae78f0Eric Laurent
6855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // apply volume rules for current stream and device if necessary
6865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        checkAndSetVolume(stream,
687c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                          mStreams[stream].getVolumeIndex(newDevice),
6885ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                          output,
6895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                          newDevice);
69012bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi
6915ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // update the outputs if starting an output with a stream that can affect notification
6925ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // routing
6935ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        handleNotificationRoutingForStream(stream);
694b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        if (waitMs > muteWaitMs) {
695b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent            usleep((waitMs - muteWaitMs) * 2 * 1000);
696b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        }
6975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
698f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
699f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
700f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
7015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
702f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::stopOutput(audio_io_handle_t output,
703f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            AudioSystem::stream_type stream,
704f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            int session)
705f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
7066a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
707f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mOutputs.indexOfKey(output);
708f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
70964cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("stopOutput() unknow output %d", output);
710f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
711f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
712f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
713f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index);
714f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
715f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // handle special case for sonification while in call
716f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (isInCall()) {
717f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        handleIncallSonification(stream, false, false);
718f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
719f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
720f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (outputDesc->mRefCount[stream] > 0) {
721f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // decrement usage count of this stream on the output
722f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->changeRefCount(stream, -1);
723f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // store time at which the stream was stopped - see isStreamActive()
7245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (outputDesc->mRefCount[stream] == 0) {
7255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            outputDesc->mStopTime[stream] = systemTime();
7265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            audio_devices_t newDevice = getNewDevice(output, false /*fromCache*/);
7275ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // delay the device switch by twice the latency because stopOutput() is executed when
7285ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // the track stop() command is received and at that time the audio track buffer can
7295ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // still contain data that needs to be drained. The latency only covers the audio HAL
7305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // and kernel buffers. Also the latency does not always include additional delay in the
7315ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // audio path (audio DSP, CODEC ...)
7325ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            setOutputDevice(output, newDevice, false, outputDesc->mLatency*2);
7335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
7345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // force restoring the device selection on other active outputs if it differs from the
7355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // one being selected for this output
7365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            for (size_t i = 0; i < mOutputs.size(); i++) {
7375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                audio_io_handle_t curOutput = mOutputs.keyAt(i);
7385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                AudioOutputDescriptor *desc = mOutputs.valueAt(i);
7395ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                if (curOutput != output &&
7405ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                        desc->refCount() != 0 &&
7415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                        outputDesc->sharesHwModuleWith(desc) &&
7425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                        newDevice != desc->device()) {
7435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                    setOutputDevice(curOutput,
7445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                    getNewDevice(curOutput, false /*fromCache*/),
7455ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                    true,
7465ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                    outputDesc->mLatency*2);
7475ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                }
7485ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
7495ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // update the outputs if stopping one with a stream that can affect notification routing
7505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            handleNotificationRoutingForStream(stream);
751f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
752f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return NO_ERROR;
753f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
75464cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("stopOutput() refcount is already 0 for output %d", output);
755f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return INVALID_OPERATION;
756f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
757f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
758f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
759f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::releaseOutput(audio_io_handle_t output)
760f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
7616a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("releaseOutput() %d", output);
762f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mOutputs.indexOfKey(output);
763f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
76464cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("releaseOutput() releasing unknown output %d", output);
765f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
766f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
767f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
768f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
769f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    int testIndex = testOutputIndex(output);
770f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (testIndex != 0) {
771f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index);
772f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (outputDesc->refCount() == 0) {
773f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mpClientInterface->closeOutput(output);
774f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            delete mOutputs.valueAt(index);
775f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mOutputs.removeItem(output);
776f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mTestOutputs[testIndex] = 0;
777f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
778f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
779f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
780f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
781f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
782f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mOutputs.valueAt(index)->mFlags & AudioSystem::OUTPUT_FLAG_DIRECT) {
783f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mpClientInterface->closeOutput(output);
784f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        delete mOutputs.valueAt(index);
785f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mOutputs.removeItem(output);
786f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
78712bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi
788f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
789f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
790f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinaudio_io_handle_t AudioPolicyManagerBase::getInput(int inputSource,
791f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    uint32_t samplingRate,
792f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    uint32_t format,
79370c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                    uint32_t channelMask,
794f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    AudioSystem::audio_in_acoustics acoustics)
795f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
796f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    audio_io_handle_t input = 0;
797f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent    audio_devices_t device = getDeviceForInputSource(inputSource);
798f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
79970c236c9290732782d5267935af1475b8d5ae602Eric Laurent    ALOGV("getInput() inputSource %d, samplingRate %d, format %d, channelMask %x, acoustics %x",
80070c236c9290732782d5267935af1475b8d5ae602Eric Laurent          inputSource, samplingRate, format, channelMask, acoustics);
801f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
802c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    if (device == APM_AUDIO_DEVICE_NONE) {
8035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        ALOGW("getInput() could not find device for inputSource %d", inputSource);
804f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return 0;
805f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
806f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
807f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // adapt channel selection to input source
808f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    switch(inputSource) {
809f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_UPLINK:
81070c236c9290732782d5267935af1475b8d5ae602Eric Laurent        channelMask = AudioSystem::CHANNEL_IN_VOICE_UPLINK;
811f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
812f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_DOWNLINK:
81370c236c9290732782d5267935af1475b8d5ae602Eric Laurent        channelMask = AudioSystem::CHANNEL_IN_VOICE_DNLINK;
814f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
815f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_CALL:
81670c236c9290732782d5267935af1475b8d5ae602Eric Laurent        channelMask = (AudioSystem::CHANNEL_IN_VOICE_UPLINK | AudioSystem::CHANNEL_IN_VOICE_DNLINK);
817f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
818f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    default:
819f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
820f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
821f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
8225ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    IOProfile *profile = getInputProfile(device,
8235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                         samplingRate,
8245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                         format,
82570c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                         channelMask);
8265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (profile == NULL) {
8275ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        ALOGW("getInput() could not find profile for device %04x, samplingRate %d, format %d,"
82870c236c9290732782d5267935af1475b8d5ae602Eric Laurent                "channelMask %04x",
82970c236c9290732782d5267935af1475b8d5ae602Eric Laurent                device, samplingRate, format, channelMask);
83070c236c9290732782d5267935af1475b8d5ae602Eric Laurent        return 0;
83170c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
83270c236c9290732782d5267935af1475b8d5ae602Eric Laurent
83370c236c9290732782d5267935af1475b8d5ae602Eric Laurent    if (profile->mModule->mHandle == 0) {
8343cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        ALOGE("getInput(): HW module %s not opened", profile->mModule->mName);
8355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return 0;
8365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
8375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
8385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    AudioInputDescriptor *inputDesc = new AudioInputDescriptor(profile);
839f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
840f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    inputDesc->mInputSource = inputSource;
841f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    inputDesc->mDevice = device;
842f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    inputDesc->mSamplingRate = samplingRate;
84370c236c9290732782d5267935af1475b8d5ae602Eric Laurent    inputDesc->mFormat = (audio_format_t)format;
84470c236c9290732782d5267935af1475b8d5ae602Eric Laurent    inputDesc->mChannelMask = (audio_channel_mask_t)channelMask;
845f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    inputDesc->mRefCount = 0;
84670c236c9290732782d5267935af1475b8d5ae602Eric Laurent    input = mpClientInterface->openInput(profile->mModule->mHandle,
84770c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                    &inputDesc->mDevice,
848f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    &inputDesc->mSamplingRate,
849f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    &inputDesc->mFormat,
85070c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                    &inputDesc->mChannelMask);
851f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
852f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // only accept input with the exact requested set of parameters
853f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (input == 0 ||
854f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        (samplingRate != inputDesc->mSamplingRate) ||
855f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        (format != inputDesc->mFormat) ||
85670c236c9290732782d5267935af1475b8d5ae602Eric Laurent        (channelMask != inputDesc->mChannelMask)) {
85770c236c9290732782d5267935af1475b8d5ae602Eric Laurent        ALOGV("getInput() failed opening input: samplingRate %d, format %d, channelMask %d",
85870c236c9290732782d5267935af1475b8d5ae602Eric Laurent                samplingRate, format, channelMask);
859f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (input != 0) {
860f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mpClientInterface->closeInput(input);
861f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
862f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        delete inputDesc;
863f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return 0;
864f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
865f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mInputs.add(input, inputDesc);
866f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return input;
867f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
868f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
869f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::startInput(audio_io_handle_t input)
870f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
8716a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("startInput() input %d", input);
872f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mInputs.indexOfKey(input);
873f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
87464cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("startInput() unknow input %d", input);
875f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
876f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
877f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioInputDescriptor *inputDesc = mInputs.valueAt(index);
878f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
879f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
880f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mTestInput == 0)
881f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
882f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    {
883f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // refuse 2 active AudioRecord clients at the same time
884f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (getActiveInput() != 0) {
88564cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("startInput() input %d failed: other input already started", input);
886f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return INVALID_OPERATION;
887f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
888f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
889f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
890f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioParameter param = AudioParameter();
891f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    param.addInt(String8(AudioParameter::keyRouting), (int)inputDesc->mDevice);
892f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
893f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    param.addInt(String8(AudioParameter::keyInputSource), (int)inputDesc->mInputSource);
8946a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("AudioPolicyManager::startInput() input source = %d", inputDesc->mInputSource);
895f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
896f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mpClientInterface->setParameters(input, param.toString());
897f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
898f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    inputDesc->mRefCount = 1;
899f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
900f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
901f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
902f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::stopInput(audio_io_handle_t input)
903f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
9046a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("stopInput() input %d", input);
905f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mInputs.indexOfKey(input);
906f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
90764cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("stopInput() unknow input %d", input);
908f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
909f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
910f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioInputDescriptor *inputDesc = mInputs.valueAt(index);
911f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
912f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (inputDesc->mRefCount == 0) {
91364cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("stopInput() input %d already stopped", input);
914f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return INVALID_OPERATION;
915f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
916f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioParameter param = AudioParameter();
917f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        param.addInt(String8(AudioParameter::keyRouting), 0);
918f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mpClientInterface->setParameters(input, param.toString());
919f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        inputDesc->mRefCount = 0;
920f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return NO_ERROR;
921f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
922f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
923f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
924f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::releaseInput(audio_io_handle_t input)
925f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
9266a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("releaseInput() %d", input);
927f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mInputs.indexOfKey(input);
928f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
92964cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("releaseInput() releasing unknown input %d", input);
930f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
931f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
932f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mpClientInterface->closeInput(input);
933f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    delete mInputs.valueAt(index);
934f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mInputs.removeItem(input);
9356a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("releaseInput() exit");
936f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
937f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
938f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::initStreamVolume(AudioSystem::stream_type stream,
939f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            int indexMin,
940f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            int indexMax)
941f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
9426a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
943f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (indexMin < 0 || indexMin >= indexMax) {
94464cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("initStreamVolume() invalid index limits for stream %d, min %d, max %d", stream , indexMin, indexMax);
945f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
946f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
947f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mStreams[stream].mIndexMin = indexMin;
948f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mStreams[stream].mIndexMax = indexMax;
949f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
950f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
951c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentstatus_t AudioPolicyManagerBase::setStreamVolumeIndex(AudioSystem::stream_type stream,
952c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                      int index,
953c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                      audio_devices_t device)
954f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
955f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
956f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if ((index < mStreams[stream].mIndexMin) || (index > mStreams[stream].mIndexMax)) {
957f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
958f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
959c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    if (!audio_is_output_device(device)) {
960c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        return BAD_VALUE;
961c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    }
962f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
963f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // Force max volume if stream cannot be muted
964f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (!mStreams[stream].mCanBeMuted) index = mStreams[stream].mIndexMax;
965f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
966b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    ALOGV("setStreamVolumeIndex() stream %d, device %04x, index %d",
967c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent          stream, device, index);
968c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
969c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    // if device is AUDIO_DEVICE_OUT_DEFAULT set default value and
970c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    // clear all device specific values
971c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    if (device == AUDIO_DEVICE_OUT_DEFAULT) {
972c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        mStreams[stream].mIndexCur.clear();
973c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    }
974c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    mStreams[stream].mIndexCur.add(device, index);
975f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
976f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // compute and apply stream volume on all outputs according to connected device
977f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    status_t status = NO_ERROR;
978f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mOutputs.size(); i++) {
979c5eb8b4a5d4395ce335bc7c3e6df2678fa47e2ddEric Laurent        audio_devices_t curDevice =
980c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                getDeviceForVolume(mOutputs.valueAt(i)->device());
981c5eb8b4a5d4395ce335bc7c3e6df2678fa47e2ddEric Laurent        if (device == curDevice) {
982c5eb8b4a5d4395ce335bc7c3e6df2678fa47e2ddEric Laurent            status_t volStatus = checkAndSetVolume(stream, index, mOutputs.keyAt(i), curDevice);
983c5eb8b4a5d4395ce335bc7c3e6df2678fa47e2ddEric Laurent            if (volStatus != NO_ERROR) {
984c5eb8b4a5d4395ce335bc7c3e6df2678fa47e2ddEric Laurent                status = volStatus;
985c5eb8b4a5d4395ce335bc7c3e6df2678fa47e2ddEric Laurent            }
986f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
987f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
988f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return status;
989f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
990f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
991c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentstatus_t AudioPolicyManagerBase::getStreamVolumeIndex(AudioSystem::stream_type stream,
992c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                      int *index,
993c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                      audio_devices_t device)
994f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
995c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    if (index == NULL) {
996f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
997f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
998c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    if (!audio_is_output_device(device)) {
999c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        return BAD_VALUE;
1000c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    }
1001c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    // if device is AUDIO_DEVICE_OUT_DEFAULT, return volume for device corresponding to
1002c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    // the strategy the stream belongs to.
1003c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    if (device == AUDIO_DEVICE_OUT_DEFAULT) {
1004c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
1005c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    }
1006c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    device = getDeviceForVolume(device);
1007c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
1008c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    *index =  mStreams[stream].getVolumeIndex(device);
1009c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index);
1010f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
1011f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1012f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1013c94dccc97cc3ed5171b45f46a0f7f8762d37156fGlenn Kastenaudio_io_handle_t AudioPolicyManagerBase::getOutputForEffect(const effect_descriptor_t *desc)
1014f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
10156a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("getOutputForEffect()");
1016f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // apply simple rule where global effects are attached to the same output as MUSIC streams
10174660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen
10184660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen    routing_strategy strategy = getStrategy(AudioSystem::MUSIC);
10194660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen    audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
1020c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs);
10214660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen    int outIdx = 0;
10224660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen    for (size_t i = 0; i < dstOutputs.size(); i++) {
10234660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen        AudioOutputDescriptor *desc = mOutputs.valueFor(dstOutputs[i]);
10244660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen        if (desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
10254660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen            outIdx = i;
10264660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen        }
10274660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen    }
10284660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen    return dstOutputs[outIdx];
1029f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1030f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1031c94dccc97cc3ed5171b45f46a0f7f8762d37156fGlenn Kastenstatus_t AudioPolicyManagerBase::registerEffect(const effect_descriptor_t *desc,
10321c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent                                audio_io_handle_t io,
1033f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                uint32_t strategy,
1034f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                int session,
1035f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                int id)
1036f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
10371c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent    ssize_t index = mOutputs.indexOfKey(io);
1038f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
10391c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent        index = mInputs.indexOfKey(io);
10401c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent        if (index < 0) {
104164cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("registerEffect() unknown io %d", io);
10421c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent            return INVALID_OPERATION;
10431c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent        }
1044f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1045f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1046f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mTotalEffectsMemory + desc->memoryUsage > getMaxEffectsMemory()) {
104764cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("registerEffect() memory limit exceeded for Fx %s, Memory %d KB",
1048f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                desc->name, desc->memoryUsage);
1049f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return INVALID_OPERATION;
1050f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1051f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mTotalEffectsMemory += desc->memoryUsage;
10526a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("registerEffect() effect %s, io %d, strategy %d session %d id %d",
10531c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent            desc->name, io, strategy, session, id);
10546a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("registerEffect() memory %d, total memory %d", desc->memoryUsage, mTotalEffectsMemory);
1055f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1056f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    EffectDescriptor *pDesc = new EffectDescriptor();
1057f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    memcpy (&pDesc->mDesc, desc, sizeof(effect_descriptor_t));
10581c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent    pDesc->mIo = io;
1059f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    pDesc->mStrategy = (routing_strategy)strategy;
1060f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    pDesc->mSession = session;
1061582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    pDesc->mEnabled = false;
1062582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent
1063f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mEffects.add(id, pDesc);
1064f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1065f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
1066f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1067f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1068f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::unregisterEffect(int id)
1069f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1070f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mEffects.indexOfKey(id);
1071f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
107264cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("unregisterEffect() unknown effect ID %d", id);
1073f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return INVALID_OPERATION;
1074f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1075f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1076f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    EffectDescriptor *pDesc = mEffects.valueAt(index);
1077f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1078582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    setEffectEnabled(pDesc, false);
1079582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent
1080f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mTotalEffectsMemory < pDesc->mDesc.memoryUsage) {
108164cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("unregisterEffect() memory %d too big for total %d",
1082f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                pDesc->mDesc.memoryUsage, mTotalEffectsMemory);
1083f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        pDesc->mDesc.memoryUsage = mTotalEffectsMemory;
1084f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1085f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mTotalEffectsMemory -= pDesc->mDesc.memoryUsage;
10866a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("unregisterEffect() effect %s, ID %d, memory %d total memory %d",
1087582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent            pDesc->mDesc.name, id, pDesc->mDesc.memoryUsage, mTotalEffectsMemory);
1088f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1089f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mEffects.removeItem(id);
1090f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    delete pDesc;
1091f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1092f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
1093f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1094f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1095582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurentstatus_t AudioPolicyManagerBase::setEffectEnabled(int id, bool enabled)
1096582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent{
1097582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    ssize_t index = mEffects.indexOfKey(id);
1098582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    if (index < 0) {
109964cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("unregisterEffect() unknown effect ID %d", id);
1100582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        return INVALID_OPERATION;
1101582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    }
1102582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent
1103582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    return setEffectEnabled(mEffects.valueAt(index), enabled);
1104582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent}
1105582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent
1106582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurentstatus_t AudioPolicyManagerBase::setEffectEnabled(EffectDescriptor *pDesc, bool enabled)
1107582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent{
1108582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    if (enabled == pDesc->mEnabled) {
11096a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("setEffectEnabled(%s) effect already %s",
1110582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent             enabled?"true":"false", enabled?"enabled":"disabled");
1111582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        return INVALID_OPERATION;
1112582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    }
1113582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent
1114582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    if (enabled) {
1115582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        if (mTotalEffectsCpuLoad + pDesc->mDesc.cpuLoad > getMaxEffectsCpuLoad()) {
111664cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("setEffectEnabled(true) CPU Load limit exceeded for Fx %s, CPU %f MIPS",
1117582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent                 pDesc->mDesc.name, (float)pDesc->mDesc.cpuLoad/10);
1118582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent            return INVALID_OPERATION;
1119582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        }
1120582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        mTotalEffectsCpuLoad += pDesc->mDesc.cpuLoad;
11216a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("setEffectEnabled(true) total CPU %d", mTotalEffectsCpuLoad);
1122582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    } else {
1123582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        if (mTotalEffectsCpuLoad < pDesc->mDesc.cpuLoad) {
112464cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("setEffectEnabled(false) CPU load %d too high for total %d",
1125582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent                    pDesc->mDesc.cpuLoad, mTotalEffectsCpuLoad);
1126582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent            pDesc->mDesc.cpuLoad = mTotalEffectsCpuLoad;
1127582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        }
1128582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        mTotalEffectsCpuLoad -= pDesc->mDesc.cpuLoad;
11296a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("setEffectEnabled(false) total CPU %d", mTotalEffectsCpuLoad);
1130582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    }
1131582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    pDesc->mEnabled = enabled;
1132582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    return NO_ERROR;
1133582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent}
1134582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent
1135f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinbool AudioPolicyManagerBase::isStreamActive(int stream, uint32_t inPastMs) const
1136f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1137f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    nsecs_t sysTime = systemTime();
1138f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mOutputs.size(); i++) {
1139f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (mOutputs.valueAt(i)->mRefCount[stream] != 0 ||
1140f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            ns2ms(sysTime - mOutputs.valueAt(i)->mStopTime[stream]) < inPastMs) {
1141f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return true;
1142f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1143f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1144f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return false;
1145f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1146f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1147f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1148f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::dump(int fd)
1149f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1150f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    const size_t SIZE = 256;
1151f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    char buffer[SIZE];
1152f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    String8 result;
1153f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1154f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
1155f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1156b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
115770c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, " Primary Output: %d\n", mPrimaryOutput);
1158f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1159f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " A2DP device address: %s\n", mA2dpDeviceAddress.string());
1160f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1161f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " SCO device address: %s\n", mScoDeviceAddress.string());
1162f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1163599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    snprintf(buffer, SIZE, " USB audio ALSA %s\n", mUsbCardAndDevice.string());
1164599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    result.append(buffer);
1165f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Output devices: %08x\n", mAvailableOutputDevices);
1166f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1167f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Input devices: %08x\n", mAvailableInputDevices);
1168f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1169f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Phone state: %d\n", mPhoneState);
1170f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1171f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Force use for communications %d\n", mForceUse[AudioSystem::FOR_COMMUNICATION]);
1172f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1173f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Force use for media %d\n", mForceUse[AudioSystem::FOR_MEDIA]);
1174f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1175f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Force use for record %d\n", mForceUse[AudioSystem::FOR_RECORD]);
1176f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1177f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Force use for dock %d\n", mForceUse[AudioSystem::FOR_DOCK]);
1178f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1179f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, result.string(), result.size());
1180f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
11815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
118270c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "\nHW Modules dump:\n");
11835ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    write(fd, buffer, strlen(buffer));
118470c236c9290732782d5267935af1475b8d5ae602Eric Laurent    for (size_t i = 0; i < mHwModules.size(); i++) {
118570c236c9290732782d5267935af1475b8d5ae602Eric Laurent        snprintf(buffer, SIZE, "- HW Module %d:\n", i + 1);
11865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        write(fd, buffer, strlen(buffer));
118770c236c9290732782d5267935af1475b8d5ae602Eric Laurent        mHwModules[i]->dump(fd);
11885ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
11895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
1190f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "\nOutputs dump:\n");
1191f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
1192f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mOutputs.size(); i++) {
1193f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        snprintf(buffer, SIZE, "- Output %d dump:\n", mOutputs.keyAt(i));
1194f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        write(fd, buffer, strlen(buffer));
1195f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mOutputs.valueAt(i)->dump(fd);
1196f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1197f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1198f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "\nInputs dump:\n");
1199f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
1200f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mInputs.size(); i++) {
1201f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        snprintf(buffer, SIZE, "- Input %d dump:\n", mInputs.keyAt(i));
1202f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        write(fd, buffer, strlen(buffer));
1203f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mInputs.valueAt(i)->dump(fd);
1204f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1205f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1206f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "\nStreams dump:\n");
1207f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
1208c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    snprintf(buffer, SIZE,
1209c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent             " Stream  Can be muted  Index Min  Index Max  Index Cur [device : index]...\n");
1210f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
1211f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) {
1212c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        snprintf(buffer, SIZE, " %02d      ", i);
1213f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        write(fd, buffer, strlen(buffer));
1214c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        mStreams[i].dump(fd);
1215f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1216f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1217f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "\nTotal Effects CPU: %f MIPS, Total Effects memory: %d KB\n",
1218f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            (float)mTotalEffectsCpuLoad/10, mTotalEffectsMemory);
1219f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
1220f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1221f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "Registered effects:\n");
1222f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
1223f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mEffects.size(); i++) {
1224f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        snprintf(buffer, SIZE, "- Effect %d dump:\n", mEffects.keyAt(i));
1225f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        write(fd, buffer, strlen(buffer));
1226f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mEffects.valueAt(i)->dump(fd);
1227f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1228f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1229f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1230f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
1231f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1232f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1233f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// ----------------------------------------------------------------------------
1234f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// AudioPolicyManagerBase
1235f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// ----------------------------------------------------------------------------
1236f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1237f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima ZavinAudioPolicyManagerBase::AudioPolicyManagerBase(AudioPolicyClientInterface *clientInterface)
1238f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    :
1239f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
1240f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    Thread(false),
1241f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
124270c236c9290732782d5267935af1475b8d5ae602Eric Laurent    mPrimaryOutput((audio_io_handle_t)0),
1243c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    mAvailableOutputDevices(APM_AUDIO_DEVICE_NONE),
1244ca0657a1ca087a6d474a75fcfedd6aac3901d587Glenn Kasten    mPhoneState(AudioSystem::MODE_NORMAL),
1245f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
1246f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mTotalEffectsCpuLoad(0), mTotalEffectsMemory(0),
1247599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    mA2dpSuspended(false), mHasA2dp(false), mHasUsb(false)
1248f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1249f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mpClientInterface = clientInterface;
1250f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1251f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < AudioSystem::NUM_FORCE_USE; i++) {
1252f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mForceUse[i] = AudioSystem::FORCE_NONE;
1253f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1254f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1255f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    initializeVolumeCurves();
1256f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1257f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mA2dpDeviceAddress = String8("");
1258f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mScoDeviceAddress = String8("");
1259599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    mUsbCardAndDevice = String8("");
1260f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
12615ec145df7708564d385fd3fb764085321cf4c253Dima Zavin    if (loadAudioPolicyConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE) != NO_ERROR) {
12625ec145df7708564d385fd3fb764085321cf4c253Dima Zavin        if (loadAudioPolicyConfig(AUDIO_POLICY_CONFIG_FILE) != NO_ERROR) {
1263739022f26a7127ba76a98dda65411496086114a7Dima Zavin            ALOGE("could not load audio policy configuration file, setting defaults");
1264739022f26a7127ba76a98dda65411496086114a7Dima Zavin            defaultAudioPolicyConfig();
12655ec145df7708564d385fd3fb764085321cf4c253Dima Zavin        }
12665ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
12675ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
1268b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    // open all output streams needed to access attached devices
126970c236c9290732782d5267935af1475b8d5ae602Eric Laurent    for (size_t i = 0; i < mHwModules.size(); i++) {
127070c236c9290732782d5267935af1475b8d5ae602Eric Laurent        mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->mName);
127170c236c9290732782d5267935af1475b8d5ae602Eric Laurent        if (mHwModules[i]->mHandle == 0) {
127270c236c9290732782d5267935af1475b8d5ae602Eric Laurent            ALOGW("could not open HW module %s", mHwModules[i]->mName);
127370c236c9290732782d5267935af1475b8d5ae602Eric Laurent            continue;
127470c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
127570c236c9290732782d5267935af1475b8d5ae602Eric Laurent        // open all output streams needed to access attached devices
127670c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
127770c236c9290732782d5267935af1475b8d5ae602Eric Laurent        {
127870c236c9290732782d5267935af1475b8d5ae602Eric Laurent            const IOProfile *outProfile = mHwModules[i]->mOutputProfiles[j];
127970c236c9290732782d5267935af1475b8d5ae602Eric Laurent
128070c236c9290732782d5267935af1475b8d5ae602Eric Laurent            if (outProfile->mSupportedDevices & mAttachedOutputDevices) {
128170c236c9290732782d5267935af1475b8d5ae602Eric Laurent                AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(outProfile);
128270c236c9290732782d5267935af1475b8d5ae602Eric Laurent                outputDesc->mDevice = (audio_devices_t)(mDefaultOutputDevice &
128370c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                            outProfile->mSupportedDevices);
128470c236c9290732782d5267935af1475b8d5ae602Eric Laurent                audio_io_handle_t output = mpClientInterface->openOutput(
128570c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                outProfile->mModule->mHandle,
128670c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mDevice,
128770c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mSamplingRate,
128870c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mFormat,
128970c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mChannelMask,
129070c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mLatency,
129170c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                outputDesc->mFlags);
129270c236c9290732782d5267935af1475b8d5ae602Eric Laurent                if (output == 0) {
129370c236c9290732782d5267935af1475b8d5ae602Eric Laurent                    delete outputDesc;
129470c236c9290732782d5267935af1475b8d5ae602Eric Laurent                } else {
129570c236c9290732782d5267935af1475b8d5ae602Eric Laurent                    mAvailableOutputDevices = (audio_devices_t)(mAvailableOutputDevices |
129670c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                            (outProfile->mSupportedDevices & mAttachedOutputDevices));
129770c236c9290732782d5267935af1475b8d5ae602Eric Laurent                    if (mPrimaryOutput == 0 &&
12980977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent                            outProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
129970c236c9290732782d5267935af1475b8d5ae602Eric Laurent                        mPrimaryOutput = output;
130070c236c9290732782d5267935af1475b8d5ae602Eric Laurent                    }
130170c236c9290732782d5267935af1475b8d5ae602Eric Laurent                    addOutput(output, outputDesc);
130270c236c9290732782d5267935af1475b8d5ae602Eric Laurent                    setOutputDevice(output,
130370c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                    (audio_devices_t)(mDefaultOutputDevice &
130470c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                        outProfile->mSupportedDevices),
130570c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                    true);
1306b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                }
1307b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            }
1308b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        }
1309f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1310f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
13115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGE_IF((mAttachedOutputDevices & ~mAvailableOutputDevices),
1312b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent             "Not output found for attached devices %08x",
13135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent             (mAttachedOutputDevices & ~mAvailableOutputDevices));
1314b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1315b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output");
1316b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1317c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    updateDevicesAndOutputs();
13183cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
1319f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
1320b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (mPrimaryOutput != 0) {
1321f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioParameter outputCmd = AudioParameter();
1322f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputCmd.addInt(String8("set_id"), 0);
1323b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        mpClientInterface->setParameters(mPrimaryOutput, outputCmd.toString());
1324f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1325c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        mTestDevice = AUDIO_DEVICE_OUT_SPEAKER;
1326f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mTestSamplingRate = 44100;
1327f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mTestFormat = AudioSystem::PCM_16_BIT;
1328f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mTestChannels =  AudioSystem::CHANNEL_OUT_STEREO;
1329f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mTestLatencyMs = 0;
1330f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mCurOutput = 0;
1331f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mDirectOutput = false;
1332f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
1333f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mTestOutputs[i] = 0;
1334f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1335f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1336f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        const size_t SIZE = 256;
1337f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        char buffer[SIZE];
1338f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        snprintf(buffer, SIZE, "AudioPolicyManagerTest");
1339f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        run(buffer, ANDROID_PRIORITY_AUDIO);
1340f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1341f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
1342f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1343f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1344f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima ZavinAudioPolicyManagerBase::~AudioPolicyManagerBase()
1345f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1346f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
1347f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    exit();
1348f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
1349f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin   for (size_t i = 0; i < mOutputs.size(); i++) {
1350f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mpClientInterface->closeOutput(mOutputs.keyAt(i));
1351f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        delete mOutputs.valueAt(i);
1352f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin   }
1353f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin   for (size_t i = 0; i < mInputs.size(); i++) {
1354f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mpClientInterface->closeInput(mInputs.keyAt(i));
1355f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        delete mInputs.valueAt(i);
1356f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin   }
135770c236c9290732782d5267935af1475b8d5ae602Eric Laurent   for (size_t i = 0; i < mHwModules.size(); i++) {
135870c236c9290732782d5267935af1475b8d5ae602Eric Laurent        delete mHwModules[i];
13595ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent   }
1360f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1361f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1362f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::initCheck()
1363f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1364b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    return (mPrimaryOutput == 0) ? NO_INIT : NO_ERROR;
1365f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1366f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1367f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
1368f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinbool AudioPolicyManagerBase::threadLoop()
1369f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
13706a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("entering threadLoop()");
1371f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    while (!exitPending())
1372f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    {
1373f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        String8 command;
1374f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        int valueInt;
1375f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        String8 value;
1376f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1377f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        Mutex::Autolock _l(mLock);
1378f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mWaitWorkCV.waitRelative(mLock, milliseconds(50));
1379f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1380f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        command = mpClientInterface->getParameters(0, String8("test_cmd_policy"));
1381f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioParameter param = AudioParameter(command);
1382f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1383f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR &&
1384f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            valueInt != 0) {
13856a70518b93928d1c91457ff805e375c82d76b0e5Steve Block            ALOGV("Test command %s received", command.string());
1386f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            String8 target;
1387f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.get(String8("target"), target) != NO_ERROR) {
1388f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                target = "Manager";
1389f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1390f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) {
1391f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_output"));
1392f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mCurOutput = valueInt;
1393f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1394f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) {
1395f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_direct"));
1396f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (value == "false") {
1397f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    mDirectOutput = false;
1398f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else if (value == "true") {
1399f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    mDirectOutput = true;
1400f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1401f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1402f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) {
1403f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_input"));
1404f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mTestInput = valueInt;
1405f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1406f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1407f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) {
1408f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_format"));
1409f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                int format = AudioSystem::INVALID_FORMAT;
1410f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (value == "PCM 16 bits") {
1411f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    format = AudioSystem::PCM_16_BIT;
1412f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else if (value == "PCM 8 bits") {
1413f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    format = AudioSystem::PCM_8_BIT;
1414f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else if (value == "Compressed MP3") {
1415f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    format = AudioSystem::MP3;
1416f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1417f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (format != AudioSystem::INVALID_FORMAT) {
1418f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    if (target == "Manager") {
1419f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mTestFormat = format;
1420f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    } else if (mTestOutputs[mCurOutput] != 0) {
1421f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        AudioParameter outputParam = AudioParameter();
1422f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        outputParam.addInt(String8("format"), format);
1423f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
1424f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    }
1425f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1426f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1427f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) {
1428f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_channels"));
1429f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                int channels = 0;
1430f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1431f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (value == "Channels Stereo") {
1432f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    channels =  AudioSystem::CHANNEL_OUT_STEREO;
1433f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else if (value == "Channels Mono") {
1434f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    channels =  AudioSystem::CHANNEL_OUT_MONO;
1435f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1436f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (channels != 0) {
1437f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    if (target == "Manager") {
1438f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mTestChannels = channels;
1439f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    } else if (mTestOutputs[mCurOutput] != 0) {
1440f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        AudioParameter outputParam = AudioParameter();
1441f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        outputParam.addInt(String8("channels"), channels);
1442f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
1443f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    }
1444f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1445f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1446f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) {
1447f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_sampleRate"));
1448f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (valueInt >= 0 && valueInt <= 96000) {
1449f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    int samplingRate = valueInt;
1450f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    if (target == "Manager") {
1451f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mTestSamplingRate = samplingRate;
1452f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    } else if (mTestOutputs[mCurOutput] != 0) {
1453f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        AudioParameter outputParam = AudioParameter();
1454f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        outputParam.addInt(String8("sampling_rate"), samplingRate);
1455f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
1456f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    }
1457f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1458f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1459f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1460f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) {
1461f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_reopen"));
1462f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
146370c236c9290732782d5267935af1475b8d5ae602Eric Laurent                AudioOutputDescriptor *outputDesc = mOutputs.valueFor(mPrimaryOutput);
1464b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                mpClientInterface->closeOutput(mPrimaryOutput);
146570c236c9290732782d5267935af1475b8d5ae602Eric Laurent
146670c236c9290732782d5267935af1475b8d5ae602Eric Laurent                audio_module_handle_t moduleHandle = outputDesc->mModule->mHandle;
146770c236c9290732782d5267935af1475b8d5ae602Eric Laurent
1468b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                delete mOutputs.valueFor(mPrimaryOutput);
1469b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                mOutputs.removeItem(mPrimaryOutput);
1470f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1471b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(NULL);
1472c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER;
147370c236c9290732782d5267935af1475b8d5ae602Eric Laurent                mPrimaryOutput = mpClientInterface->openOutput(moduleHandle,
147470c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mDevice,
1475f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                &outputDesc->mSamplingRate,
1476f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                &outputDesc->mFormat,
147770c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mChannelMask,
1478f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                &outputDesc->mLatency,
1479f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                outputDesc->mFlags);
1480b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                if (mPrimaryOutput == 0) {
14815efbd421e0029d6fc44b1cc65c0e5e0d85e5161fSteve Block                    ALOGE("Failed to reopen hardware output stream, samplingRate: %d, format %d, channels %d",
148270c236c9290732782d5267935af1475b8d5ae602Eric Laurent                            outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask);
1483f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else {
1484f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    AudioParameter outputCmd = AudioParameter();
1485f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    outputCmd.addInt(String8("set_id"), 0);
1486b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                    mpClientInterface->setParameters(mPrimaryOutput, outputCmd.toString());
1487b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                    addOutput(mPrimaryOutput, outputDesc);
1488f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1489f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1490f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1491f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1492f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mpClientInterface->setParameters(0, String8("test_cmd_policy="));
1493f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1494f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1495f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return false;
1496f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1497f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1498f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::exit()
1499f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1500f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    {
1501f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AutoMutex _l(mLock);
1502f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        requestExit();
1503f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mWaitWorkCV.signal();
1504f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1505f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    requestExitAndWait();
1506f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1507f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1508f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinint AudioPolicyManagerBase::testOutputIndex(audio_io_handle_t output)
1509f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1510f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
1511f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (output == mTestOutputs[i]) return i;
1512f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1513f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return 0;
1514f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1515f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
1516f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1517f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// ---
1518f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1519f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::addOutput(audio_io_handle_t id, AudioOutputDescriptor *outputDesc)
1520f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1521f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    outputDesc->mId = id;
1522f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mOutputs.add(id, outputDesc);
1523f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1524f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1525f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
15263cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurentstatus_t AudioPolicyManagerBase::checkOutputsForDevice(audio_devices_t device,
15273cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                       AudioSystem::device_connection_state state,
15283cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                       SortedVector<audio_io_handle_t>& outputs)
1529f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
15303cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    AudioOutputDescriptor *desc;
1531b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1532b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (state == AudioSystem::DEVICE_STATE_AVAILABLE) {
15333cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        // first list already open outputs that can be routed to this device
1534b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        for (size_t i = 0; i < mOutputs.size(); i++) {
15353cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            desc = mOutputs.valueAt(i);
15363cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            if (!desc->isDuplicated() && (desc->mProfile->mSupportedDevices & device)) {
15373cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i));
15383cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                outputs.add(mOutputs.keyAt(i));
1539b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            }
1540b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        }
15413cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        // then look for output profiles that can be routed to this device
15423cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        SortedVector<IOProfile *> profiles;
154370c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (size_t i = 0; i < mHwModules.size(); i++)
1544b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        {
154570c236c9290732782d5267935af1475b8d5ae602Eric Laurent            if (mHwModules[i]->mHandle == 0) {
154670c236c9290732782d5267935af1475b8d5ae602Eric Laurent                continue;
154770c236c9290732782d5267935af1475b8d5ae602Eric Laurent            }
154870c236c9290732782d5267935af1475b8d5ae602Eric Laurent            for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
154970c236c9290732782d5267935af1475b8d5ae602Eric Laurent            {
155070c236c9290732782d5267935af1475b8d5ae602Eric Laurent                if (mHwModules[i]->mOutputProfiles[j]->mSupportedDevices & device) {
15513cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    ALOGV("checkOutputsForDevice(): adding profile %d from module %d", j, i);
15523cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    profiles.add(mHwModules[i]->mOutputProfiles[j]);
155370c236c9290732782d5267935af1475b8d5ae602Eric Laurent                }
155470c236c9290732782d5267935af1475b8d5ae602Eric Laurent            }
1555b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        }
155670c236c9290732782d5267935af1475b8d5ae602Eric Laurent
15573cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        if (profiles.isEmpty() && outputs.isEmpty()) {
15583cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
15593cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            return BAD_VALUE;
156070c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
156170c236c9290732782d5267935af1475b8d5ae602Eric Laurent
15623cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        // open outputs for matching profiles if needed. Direct outputs are also opened to
15633cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        // query for dynamic parameters and will be closed later by setDeviceConnectionState()
15643cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
15653cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            IOProfile *profile = profiles[profile_index];
1566b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
15673cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            // nothing to do if one output is already opened for this profile
15683cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            size_t j;
15693cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            for (j = 0; j < outputs.size(); j++) {
15703cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                desc = mOutputs.valueAt(j);
15713cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                if (!desc->isDuplicated() && desc->mProfile == profile) {
15723cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    break;
15733cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                }
15743cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
15753cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            if (j != outputs.size()) {
15763cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                continue;
15773cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
15783cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
15793cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            ALOGV("opening output for device %08x", device);
15803cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            desc = new AudioOutputDescriptor(profile);
15813cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            desc->mDevice = device;
15823cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            audio_io_handle_t output = mpClientInterface->openOutput(profile->mModule->mHandle,
15833cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                       &desc->mDevice,
15843cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                       &desc->mSamplingRate,
15853cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                       &desc->mFormat,
15863cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                       &desc->mChannelMask,
15873cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                       &desc->mLatency,
15883cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                       desc->mFlags);
15893cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            if (output != 0) {
15903cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
15913cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    String8 reply;
15923cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    char *value;
15933cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    if (profile->mSamplingRates[0] == 0) {
15943cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        reply = mpClientInterface->getParameters(output,
15953cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES));
15963cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        ALOGV("checkOutputsForDevice() direct output sup sampling rates %s",
15973cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                  reply.string());
15983cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        value = strpbrk((char *)reply.string(), "=");
15993cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        if (value != NULL) {
16003cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                            loadSamplingRates(value, profile);
16013cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        }
16023cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
16033cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    if (profile->mFormats[0] == 0) {
16043cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        reply = mpClientInterface->getParameters(output,
16053cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                       String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
16063cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        ALOGV("checkOutputsForDevice() direct output sup formats %s",
16073cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                  reply.string());
16083cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        value = strpbrk((char *)reply.string(), "=");
16093cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        if (value != NULL) {
16103cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                            loadFormats(value, profile);
16113cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        }
16123cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
16133cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    if (profile->mChannelMasks[0] == 0) {
16143cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        reply = mpClientInterface->getParameters(output,
16153cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                      String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS));
16163cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        ALOGV("checkOutputsForDevice() direct output sup channel masks %s",
16173cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                  reply.string());
16183cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        value = strpbrk((char *)reply.string(), "=");
16193cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        if (value != NULL) {
16203cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                            loadOutChannels(value + 1, profile);
16213cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        }
16223cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
16233cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    if (((profile->mSamplingRates[0] == 0) &&
16243cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                             (profile->mSamplingRates.size() < 2)) ||
16253cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                         ((profile->mFormats[0] == 0) &&
16263cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                             (profile->mFormats.size() < 2)) ||
16273cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                         ((profile->mFormats[0] == 0) &&
16283cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                             (profile->mChannelMasks.size() < 2))) {
16293cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        ALOGW("checkOutputsForDevice() direct output missing param");
16303cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        output = 0;
16313cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    } else {
16323cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        addOutput(output, desc);
16333cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
16343cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                } else {
16353cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    audio_io_handle_t duplicatedOutput = 0;
16363cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    // add output descriptor
16373cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    addOutput(output, desc);
16383cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    // set initial stream volume for device
16394366b4a6735e5da342b56773073f0b41197c777fEric Laurent                    applyStreamVolumes(output, device, 0, true);
16403cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
16413cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    //TODO: configure audio effect output stage here
16423cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
16433cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    // open a duplicating output thread for the new output and the primary output
16443cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    duplicatedOutput = mpClientInterface->openDuplicateOutput(output,
16453cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                              mPrimaryOutput);
16463cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    if (duplicatedOutput != 0) {
16473cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        // add duplicated output descriptor
16483cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        AudioOutputDescriptor *dupOutputDesc = new AudioOutputDescriptor(NULL);
16493cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        dupOutputDesc->mOutput1 = mOutputs.valueFor(mPrimaryOutput);
16503cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        dupOutputDesc->mOutput2 = mOutputs.valueFor(output);
16513cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        dupOutputDesc->mSamplingRate = desc->mSamplingRate;
16523cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        dupOutputDesc->mFormat = desc->mFormat;
16533cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        dupOutputDesc->mChannelMask = desc->mChannelMask;
16543cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        dupOutputDesc->mLatency = desc->mLatency;
16553cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        addOutput(duplicatedOutput, dupOutputDesc);
16564366b4a6735e5da342b56773073f0b41197c777fEric Laurent                        applyStreamVolumes(duplicatedOutput, device, 0, true);
16573cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    } else {
16583cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        ALOGW("checkOutputsForDevice() could not open dup output for %d and %d",
16593cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                mPrimaryOutput, output);
16603cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        mpClientInterface->closeOutput(output);
16613cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        mOutputs.removeItem(output);
16623cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        output = 0;
16633cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
16643cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                }
16653cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
16663cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            if (output == 0) {
16673cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                ALOGW("checkOutputsForDevice() could not open output for device %x", device);
16683cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                delete desc;
16693cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                profiles.removeAt(profile_index);
16703cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                profile_index--;
1671b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            } else {
16723cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                outputs.add(output);
16733cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                ALOGV("checkOutputsForDevice(): adding output %d", output);
1674f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
16753cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        }
16763cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
16773cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        if (profiles.isEmpty()) {
16783cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
16793cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            return BAD_VALUE;
1680f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1681f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
1682b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        // check if one opened output is not needed any more after disconnecting one device
1683b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        for (size_t i = 0; i < mOutputs.size(); i++) {
16843cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            desc = mOutputs.valueAt(i);
16853cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            if (!desc->isDuplicated() &&
16863cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    !(desc->mProfile->mSupportedDevices & mAvailableOutputDevices)) {
16873cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                ALOGV("checkOutputsForDevice(): disconnecting adding output %d", mOutputs.keyAt(i));
16883cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                outputs.add(mOutputs.keyAt(i));
16893cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
16903cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        }
16913cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        for (size_t i = 0; i < mHwModules.size(); i++)
16923cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        {
16933cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            if (mHwModules[i]->mHandle == 0) {
16943cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                continue;
16953cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
16963cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
16973cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            {
16983cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                IOProfile *profile = mHwModules[i]->mOutputProfiles[j];
16993cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                if ((profile->mSupportedDevices & device) &&
17003cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        (profile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
17013cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    ALOGV("checkOutputsForDevice(): clearing direct output profile %d on module %d",
17023cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                          j, i);
17033cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    if (profile->mSamplingRates[0] == 0) {
17043cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        profile->mSamplingRates.clear();
17053cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        profile->mSamplingRates.add(0);
17063cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
17073cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    if (profile->mFormats[0] == 0) {
17083cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        profile->mFormats.clear();
17093cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        profile->mFormats.add((audio_format_t)0);
17103cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
17113cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    if (profile->mChannelMasks[0] == 0) {
17123cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        profile->mChannelMasks.clear();
17133cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        profile->mChannelMasks.add((audio_channel_mask_t)0);
17143cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
17153cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                }
1716b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            }
1717f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1718f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
17193cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    return NO_ERROR;
1720f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1721f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1722b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurentvoid AudioPolicyManagerBase::closeOutput(audio_io_handle_t output)
1723f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1724b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    ALOGV("closeOutput(%d)", output);
1725f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1726b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
1727b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (outputDesc == NULL) {
1728b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        ALOGW("closeOutput() unknown output %d", output);
1729b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        return;
1730f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1731f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1732b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    // look for duplicated outputs connected to the output being removed.
1733b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    for (size_t i = 0; i < mOutputs.size(); i++) {
1734b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        AudioOutputDescriptor *dupOutputDesc = mOutputs.valueAt(i);
1735b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        if (dupOutputDesc->isDuplicated() &&
1736b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                (dupOutputDesc->mOutput1 == outputDesc ||
1737b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                dupOutputDesc->mOutput2 == outputDesc)) {
1738b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            AudioOutputDescriptor *outputDesc2;
1739b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            if (dupOutputDesc->mOutput1 == outputDesc) {
1740b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                outputDesc2 = dupOutputDesc->mOutput2;
1741b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            } else {
1742b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                outputDesc2 = dupOutputDesc->mOutput1;
1743b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            }
1744b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            // As all active tracks on duplicated output will be deleted,
1745b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            // and as they were also referenced on the other output, the reference
1746b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            // count for their stream type must be adjusted accordingly on
1747b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            // the other output.
1748b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            for (int j = 0; j < (int)AudioSystem::NUM_STREAM_TYPES; j++) {
1749b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                int refCount = dupOutputDesc->mRefCount[j];
1750b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                outputDesc2->changeRefCount((AudioSystem::stream_type)j,-refCount);
1751b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            }
1752b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i);
1753b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput);
1754f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1755b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            mpClientInterface->closeOutput(duplicatedOutput);
1756b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            delete mOutputs.valueFor(duplicatedOutput);
1757b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            mOutputs.removeItem(duplicatedOutput);
1758f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1759f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1760b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1761b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    AudioParameter param;
1762b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    param.add(String8("closing"), String8("true"));
1763b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    mpClientInterface->setParameters(output, param.toString());
1764b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1765b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    mpClientInterface->closeOutput(output);
1766b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    delete mOutputs.valueFor(output);
1767b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    mOutputs.removeItem(output);
1768f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1769f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1770c952527e6f89d5427881462823514be9d79f13e6Eric LaurentSortedVector<audio_io_handle_t> AudioPolicyManagerBase::getOutputsForDevice(audio_devices_t device,
1771c952527e6f89d5427881462823514be9d79f13e6Eric Laurent                        DefaultKeyedVector<audio_io_handle_t, AudioOutputDescriptor *> openOutputs)
1772f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1773b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    SortedVector<audio_io_handle_t> outputs;
1774f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
17753cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGVV("getOutputsForDevice() device %04x", device);
1776c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    for (size_t i = 0; i < openOutputs.size(); i++) {
17773cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        ALOGVV("output %d isDuplicated=%d device=%04x",
1778c952527e6f89d5427881462823514be9d79f13e6Eric Laurent                i, openOutputs.valueAt(i)->isDuplicated(), openOutputs.valueAt(i)->supportedDevices());
1779c952527e6f89d5427881462823514be9d79f13e6Eric Laurent        if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) {
1780c952527e6f89d5427881462823514be9d79f13e6Eric Laurent            ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i));
1781c952527e6f89d5427881462823514be9d79f13e6Eric Laurent            outputs.add(openOutputs.keyAt(i));
1782f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1783f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1784b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    return outputs;
1785f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1786f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1787b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurentbool AudioPolicyManagerBase::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
1788b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                                   SortedVector<audio_io_handle_t>& outputs2)
1789f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1790b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (outputs1.size() != outputs2.size()) {
1791b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        return false;
1792f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1793b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    for (size_t i = 0; i < outputs1.size(); i++) {
1794b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        if (outputs1[i] != outputs2[i]) {
1795b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            return false;
1796f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1797f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1798b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    return true;
1799b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent}
1800b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1801b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurentvoid AudioPolicyManagerBase::checkOutputForStrategy(routing_strategy strategy)
1802b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent{
180301e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent    audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
180401e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent    audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
1805c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs);
1806c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
1807b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1808b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (!vectorsEqual(srcOutputs,dstOutputs)) {
1809b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d",
1810b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent              strategy, srcOutputs[0], dstOutputs[0]);
18115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // mute strategy while moving tracks from one output to another
18125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        for (size_t i = 0; i < srcOutputs.size(); i++) {
1813fa3697d716b444bbea6be480801536c44bf69214Eric Laurent            AudioOutputDescriptor *desc = mOutputs.valueFor(srcOutputs[i]);
1814fa3697d716b444bbea6be480801536c44bf69214Eric Laurent            if (desc->strategyRefCount(strategy) != 0) {
1815fa3697d716b444bbea6be480801536c44bf69214Eric Laurent                setStrategyMute(strategy, true, srcOutputs[i]);
1816fa3697d716b444bbea6be480801536c44bf69214Eric Laurent                setStrategyMute(strategy, false, srcOutputs[i], MUTE_TIME_MS, newDevice);
1817fa3697d716b444bbea6be480801536c44bf69214Eric Laurent            }
18185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
1819f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1820f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // Move effects associated to this strategy from previous output to new output
18214660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen        if (strategy == STRATEGY_MEDIA) {
18224660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen            int outIdx = 0;
18234660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen            for (size_t i = 0; i < dstOutputs.size(); i++) {
18244660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                AudioOutputDescriptor *desc = mOutputs.valueFor(dstOutputs[i]);
18254660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                if (desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
18264660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                    outIdx = i;
18274660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                }
18284660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen            }
18294660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen            SortedVector<audio_io_handle_t> moved;
18304660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen            for (size_t i = 0; i < mEffects.size(); i++) {
18314660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                EffectDescriptor *desc = mEffects.valueAt(i);
18324660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                if (desc->mSession == AUDIO_SESSION_OUTPUT_MIX &&
18334660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                        desc->mIo != dstOutputs[outIdx]) {
18344660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                    if (moved.indexOf(desc->mIo) < 0) {
18354660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                        ALOGV("checkOutputForStrategy() moving effect %d to output %d",
18364660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                              mEffects.keyAt(i), dstOutputs[outIdx]);
18374660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                        mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, desc->mIo,
18384660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                                                       dstOutputs[outIdx]);
18394660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                        moved.add(desc->mIo);
18404660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                    }
18414660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                    desc->mIo = dstOutputs[outIdx];
18424660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                }
18434660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen            }
18444660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen        }
18455ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // Move tracks associated to this strategy from previous output to new output
1846f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        for (int i = 0; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) {
1847f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (getStrategy((AudioSystem::stream_type)i) == strategy) {
184812bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi                //FIXME see fixme on name change
18494660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                mpClientInterface->setStreamOutput((AudioSystem::stream_type)i,
18504660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                                                   dstOutputs[0] /* ignored */);
1851f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1852f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1853f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1854f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1855f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1856f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::checkOutputForAllStrategies()
1857f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1858c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
1859f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForStrategy(STRATEGY_PHONE);
1860f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForStrategy(STRATEGY_SONIFICATION);
186112bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
1862f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForStrategy(STRATEGY_MEDIA);
1863f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForStrategy(STRATEGY_DTMF);
1864f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1865f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1866b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurentaudio_io_handle_t AudioPolicyManagerBase::getA2dpOutput()
1867b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent{
18685ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (!mHasA2dp) {
1869b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        return 0;
1870b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    }
1871b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1872b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    for (size_t i = 0; i < mOutputs.size(); i++) {
1873b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        AudioOutputDescriptor *outputDesc = mOutputs.valueAt(i);
1874b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        if (!outputDesc->isDuplicated() && outputDesc->device() & AUDIO_DEVICE_OUT_ALL_A2DP) {
1875b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            return mOutputs.keyAt(i);
1876b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        }
1877b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    }
1878b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1879b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    return 0;
1880b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent}
1881b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1882f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::checkA2dpSuspend()
1883f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
18845ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (!mHasA2dp) {
1885b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        return;
1886b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    }
1887b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    audio_io_handle_t a2dpOutput = getA2dpOutput();
1888b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (a2dpOutput == 0) {
1889b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        return;
1890b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    }
1891b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1892f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // suspend A2DP output if:
1893f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      (NOT already suspended) &&
1894f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      ((SCO device is connected &&
1895f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //       (forced usage for communication || for record is SCO))) ||
1896f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      (phone state is ringing || in call)
1897f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //
1898f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // restore A2DP output if:
1899f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      (Already suspended) &&
1900f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      ((SCO device is NOT connected ||
1901f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //       (forced usage NOT for communication && NOT for record is SCO))) &&
1902f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      (phone state is NOT ringing && NOT in call)
1903f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //
1904f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mA2dpSuspended) {
1905f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (((mScoDeviceAddress == "") ||
1906f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin             ((mForceUse[AudioSystem::FOR_COMMUNICATION] != AudioSystem::FORCE_BT_SCO) &&
1907f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin              (mForceUse[AudioSystem::FOR_RECORD] != AudioSystem::FORCE_BT_SCO))) &&
1908f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin             ((mPhoneState != AudioSystem::MODE_IN_CALL) &&
1909f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin              (mPhoneState != AudioSystem::MODE_RINGTONE))) {
1910f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1911b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            mpClientInterface->restoreOutput(a2dpOutput);
1912f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mA2dpSuspended = false;
1913f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1914f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
1915f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (((mScoDeviceAddress != "") &&
1916f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin             ((mForceUse[AudioSystem::FOR_COMMUNICATION] == AudioSystem::FORCE_BT_SCO) ||
1917f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin              (mForceUse[AudioSystem::FOR_RECORD] == AudioSystem::FORCE_BT_SCO))) ||
1918f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin             ((mPhoneState == AudioSystem::MODE_IN_CALL) ||
1919f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin              (mPhoneState == AudioSystem::MODE_RINGTONE))) {
1920f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1921b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            mpClientInterface->suspendOutput(a2dpOutput);
1922f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mA2dpSuspended = true;
1923f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1924f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1925f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1926f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1927f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurentaudio_devices_t AudioPolicyManagerBase::getNewDevice(audio_io_handle_t output, bool fromCache)
1928f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1929c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    audio_devices_t device = APM_AUDIO_DEVICE_NONE;
1930f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1931f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
1932f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // check the following by order of priority to request a routing change if necessary:
1933c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    // 1: the strategy enforced audible is active on the output:
1934c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    //      use device for strategy enforced audible
1935c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    // 2: we are in call or the strategy phone is active on the output:
1936f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      use device for strategy phone
1937c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    // 3: the strategy sonification is active on the output:
1938f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      use device for strategy sonification
193912bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    // 4: the strategy "respectful" sonification is active on the output:
194012bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    //      use device for strategy "respectful" sonification
194112bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    // 5: the strategy media is active on the output:
1942f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      use device for strategy media
194312bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    // 6: the strategy DTMF is active on the output:
1944f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      use device for strategy DTMF
1945c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    if (outputDesc->isUsedByStrategy(STRATEGY_ENFORCED_AUDIBLE)) {
1946c16ac09f510437e8340be691720177a490ae78f0Eric Laurent        device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
1947c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    } else if (isInCall() ||
1948c16ac09f510437e8340be691720177a490ae78f0Eric Laurent                    outputDesc->isUsedByStrategy(STRATEGY_PHONE)) {
1949f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
1950f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else if (outputDesc->isUsedByStrategy(STRATEGY_SONIFICATION)) {
1951f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
195212bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    } else if (outputDesc->isUsedByStrategy(STRATEGY_SONIFICATION_RESPECTFUL)) {
19535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
1954f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else if (outputDesc->isUsedByStrategy(STRATEGY_MEDIA)) {
1955f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
1956f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else if (outputDesc->isUsedByStrategy(STRATEGY_DTMF)) {
1957f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
1958f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1959f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
19606a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("getNewDevice() selected device %x", device);
1961f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return device;
1962f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1963f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1964f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinuint32_t AudioPolicyManagerBase::getStrategyForStream(AudioSystem::stream_type stream) {
1965f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return (uint32_t)getStrategy(stream);
1966f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1967f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1968f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurentaudio_devices_t AudioPolicyManagerBase::getDevicesForStream(AudioSystem::stream_type stream) {
1969f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent    audio_devices_t devices;
1970f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // By checking the range of stream before calling getStrategy, we avoid
19715efbd421e0029d6fc44b1cc65c0e5e0d85e5161fSteve Block    // getStrategy's behavior for invalid streams.  getStrategy would do a ALOGE
1972f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // and then return STRATEGY_MEDIA, but we want to return the empty set.
1973f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (stream < (AudioSystem::stream_type) 0 || stream >= AudioSystem::NUM_STREAM_TYPES) {
1974c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        devices = APM_AUDIO_DEVICE_NONE;
1975f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
1976f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioPolicyManagerBase::routing_strategy strategy = getStrategy(stream);
19775ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        devices = getDeviceForStrategy(strategy, true /*fromCache*/);
1978f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1979f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return devices;
1980f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1981f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1982f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima ZavinAudioPolicyManagerBase::routing_strategy AudioPolicyManagerBase::getStrategy(
1983f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioSystem::stream_type stream) {
1984f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // stream to strategy mapping
1985f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    switch (stream) {
1986f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::VOICE_CALL:
1987f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::BLUETOOTH_SCO:
1988f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return STRATEGY_PHONE;
1989f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::RING:
1990f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::ALARM:
1991f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return STRATEGY_SONIFICATION;
199212bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    case AudioSystem::NOTIFICATION:
199312bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        return STRATEGY_SONIFICATION_RESPECTFUL;
1994f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::DTMF:
1995f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return STRATEGY_DTMF;
1996f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    default:
19975efbd421e0029d6fc44b1cc65c0e5e0d85e5161fSteve Block        ALOGE("unknown stream type");
1998f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::SYSTEM:
1999f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // NOTE: SYSTEM stream uses MEDIA strategy because muting music and switching outputs
2000f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // while key clicks are played produces a poor result
2001f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::TTS:
2002f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::MUSIC:
2003f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return STRATEGY_MEDIA;
2004c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    case AudioSystem::ENFORCED_AUDIBLE:
2005c16ac09f510437e8340be691720177a490ae78f0Eric Laurent        return STRATEGY_ENFORCED_AUDIBLE;
2006f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2007f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2008f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
200912bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivivoid AudioPolicyManagerBase::handleNotificationRoutingForStream(AudioSystem::stream_type stream) {
201012bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    switch(stream) {
201112bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    case AudioSystem::MUSIC:
201212bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
2013c952527e6f89d5427881462823514be9d79f13e6Eric Laurent        updateDevicesAndOutputs();
201412bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        break;
201512bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    default:
201612bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        break;
201712bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    }
201812bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi}
201912bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi
20205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentaudio_devices_t AudioPolicyManagerBase::getDeviceForStrategy(routing_strategy strategy,
20215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                             bool fromCache)
2022f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2023c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    uint32_t device = APM_AUDIO_DEVICE_NONE;
2024f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2025f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (fromCache) {
20263cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
20275ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent              strategy, mDeviceForStrategy[strategy]);
2028f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return mDeviceForStrategy[strategy];
2029f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2030f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2031f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    switch (strategy) {
203212bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi
203312bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    case STRATEGY_SONIFICATION_RESPECTFUL:
203412bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        if (isInCall()) {
20355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
203612bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        } else if (isStreamActive(AudioSystem::MUSIC, SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
203712bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi            // while media is playing (or has recently played), use the same device
20385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/);
203912bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        } else {
204012bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi            // when media is not playing anymore, fall back on the sonification behavior
20415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
204212bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        }
204312bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi
204412bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        break;
204512bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi
2046f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case STRATEGY_DTMF:
2047f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (!isInCall()) {
2048f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // when off call, DTMF strategy follows the same rules as MEDIA strategy
20495ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/);
2050f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            break;
2051f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2052f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // when in call, DTMF and PHONE strategies follow the same rules
2053f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // FALL THROUGH
2054f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2055f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case STRATEGY_PHONE:
2056f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // for phone strategy, we first consider the forced use and then the available devices by order
2057f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // of priority
2058f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        switch (mForceUse[AudioSystem::FOR_COMMUNICATION]) {
2059f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        case AudioSystem::FORCE_BT_SCO:
2060f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (!isInCall() || strategy != STRATEGY_DTMF) {
2061c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT;
2062f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (device) break;
2063f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2064c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET;
2065f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (device) break;
2066c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO;
2067f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (device) break;
2068f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // if SCO device is requested but no SCO device is available, fall back to default case
2069f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // FALL THROUGH
2070f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2071f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        default:    // FORCE_NONE
2072f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to A2DP
20731afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent            if (mHasA2dp && !isInCall() &&
20741afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent                    (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
2075c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                    (getA2dpOutput() != APM_AUDIO_DEVICE_NONE) && !mA2dpSuspended) {
2076c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
2077f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (device) break;
2078c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
2079f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (device) break;
2080f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2081c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
20821afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent            if (device) break;
2083c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADSET;
20841afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent            if (device) break;
2085599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
2086599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            if (device) break;
2087599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
2088599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            if (device) break;
2089c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
2090f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (device) break;
2091c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
209255ac23bc116ad9ae38266a5e660fd22f5104348eEric Laurent            if (device) break;
2093c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
2094f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (device) break;
2095c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_EARPIECE;
20965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            if (device) break;
20975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = mDefaultOutputDevice;
2098c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            if (device == APM_AUDIO_DEVICE_NONE) {
20995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE");
2100f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2101f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            break;
2102f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2103f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        case AudioSystem::FORCE_SPEAKER:
2104f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to
2105f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // A2DP speaker when forcing to speaker output
21061afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent            if (mHasA2dp && !isInCall() &&
21071afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent                    (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
2108c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                    (getA2dpOutput() != APM_AUDIO_DEVICE_NONE) && !mA2dpSuspended) {
2109c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
2110f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (device) break;
2111f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2112599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
2113599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            if (device) break;
2114599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
2115599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            if (device) break;
2116c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
2117f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (device) break;
2118c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
211955ac23bc116ad9ae38266a5e660fd22f5104348eEric Laurent            if (device) break;
2120c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
2121f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (device) break;
2122c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER;
21235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            if (device) break;
21245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = mDefaultOutputDevice;
2125c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            if (device == APM_AUDIO_DEVICE_NONE) {
21265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE, FORCE_SPEAKER");
2127f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2128f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            break;
2129f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2130f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    break;
2131f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2132f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case STRATEGY_SONIFICATION:
2133f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2134f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // If incall, just select the STRATEGY_PHONE device: The rest of the behavior is handled by
2135f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // handleIncallSonification().
2136f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (isInCall()) {
21375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = getDeviceForStrategy(STRATEGY_PHONE, false /*fromCache*/);
2138f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            break;
2139f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2140c16ac09f510437e8340be691720177a490ae78f0Eric Laurent        // FALL THROUGH
2141c16ac09f510437e8340be691720177a490ae78f0Eric Laurent
2142c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    case STRATEGY_ENFORCED_AUDIBLE:
2143c16ac09f510437e8340be691720177a490ae78f0Eric Laurent        // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION
2144ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        // except:
2145ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        //   - when in call where it doesn't default to STRATEGY_PHONE behavior
2146ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        //   - in countries where not enforced in which case it follows STRATEGY_MEDIA
2147c16ac09f510437e8340be691720177a490ae78f0Eric Laurent
2148ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        if (strategy == STRATEGY_SONIFICATION ||
2149ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent                !mStreams[AUDIO_STREAM_ENFORCED_AUDIBLE].mCanBeMuted) {
2150c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER;
2151c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            if (device == APM_AUDIO_DEVICE_NONE) {
2152ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent                ALOGE("getDeviceForStrategy() speaker device not found for STRATEGY_SONIFICATION");
2153ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent            }
2154f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2155f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // The second device used for sonification is the same as the device used by media strategy
2156f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // FALL THROUGH
2157f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2158f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case STRATEGY_MEDIA: {
2159c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        uint32_t device2 = APM_AUDIO_DEVICE_NONE;
21601afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent        if (mHasA2dp && (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
2161c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                (getA2dpOutput() != APM_AUDIO_DEVICE_NONE) && !mA2dpSuspended) {
2162c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
2163c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            if (device2 == APM_AUDIO_DEVICE_NONE) {
2164c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
2165f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2166c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            if (device2 == APM_AUDIO_DEVICE_NONE) {
2167c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
2168f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2169f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2170c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        if (device2 == APM_AUDIO_DEVICE_NONE) {
2171c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
21721afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent        }
2173c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        if (device2 == APM_AUDIO_DEVICE_NONE) {
2174c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADSET;
21751afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent        }
2176c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        if (device2 == APM_AUDIO_DEVICE_NONE) {
2177599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
2178599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent        }
2179c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        if (device2 == APM_AUDIO_DEVICE_NONE) {
2180599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
2181599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent        }
2182c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        if (device2 == APM_AUDIO_DEVICE_NONE) {
2183c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
2184f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2185c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        if (device2 == APM_AUDIO_DEVICE_NONE) {
2186c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
2187f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2188c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        if (device2 == APM_AUDIO_DEVICE_NONE) {
2189c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
2190f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2191c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        if (device2 == APM_AUDIO_DEVICE_NONE) {
2192c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER;
2193f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2194f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2195c16ac09f510437e8340be691720177a490ae78f0Eric Laurent        // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or
2196c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        // STRATEGY_ENFORCED_AUDIBLE, APM_AUDIO_DEVICE_NONE otherwise
2197f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device |= device2;
21985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (device) break;
21995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        device = mDefaultOutputDevice;
2200c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        if (device == APM_AUDIO_DEVICE_NONE) {
22015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGE("getDeviceForStrategy() no device found for STRATEGY_MEDIA");
2202f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2203f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        } break;
2204f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2205f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    default:
220664cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("getDeviceForStrategy() unknown strategy: %d", strategy);
2207f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
2208f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2209f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
22103cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGVV("getDeviceForStrategy() strategy %d, device %x", strategy, device);
2211c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    return device;
2212f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2213f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2214c952527e6f89d5427881462823514be9d79f13e6Eric Laurentvoid AudioPolicyManagerBase::updateDevicesAndOutputs()
2215f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2216f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < NUM_STRATEGIES; i++) {
22175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
22185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
2219c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    mPreviousOutputs = mOutputs;
22205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
22215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
2222b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurentuint32_t AudioPolicyManagerBase::checkDeviceMuteStrategies(AudioOutputDescriptor *outputDesc,
22239029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                                                       audio_devices_t prevDevice,
22245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                       uint32_t delayMs)
22255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
22269029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    // mute/unmute strategies using an incompatible device combination
22279029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    // if muting, wait for the audio in pcm buffer to be drained before proceeding
22289029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    // if unmuting, unmute only after the specified delay
22295ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (outputDesc->isDuplicated()) {
2230b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        return 0;
22315ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
22325ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
22335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    uint32_t muteWaitMs = 0;
22345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    audio_devices_t device = outputDesc->device();
22355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    bool shouldMute = (outputDesc->refCount() != 0) &&
22365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                    (AudioSystem::popCount(device) >= 2);
22379029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    // temporary mute output if device selection changes to avoid volume bursts due to
22389029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    // different per device volumes
22399029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    bool tempMute = (outputDesc->refCount() != 0) && (device != prevDevice);
22405ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
22415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < NUM_STRATEGIES; i++) {
22425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
22435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        bool mute = shouldMute && (curDevice & device) && (curDevice != device);
22445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        bool doMute = false;
22455ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
22465ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (mute && !outputDesc->mStrategyMutedByDevice[i]) {
22475ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            doMute = true;
22485ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            outputDesc->mStrategyMutedByDevice[i] = true;
22495ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){
22505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            doMute = true;
22515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            outputDesc->mStrategyMutedByDevice[i] = false;
22525ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
22539029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent        if (doMute || tempMute) {
22545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            for (size_t j = 0; j < mOutputs.size(); j++) {
22555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                AudioOutputDescriptor *desc = mOutputs.valueAt(j);
2256c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                if ((desc->supportedDevices() & outputDesc->supportedDevices())
2257c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                        == APM_AUDIO_DEVICE_NONE) {
22585ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                    continue;
22595ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                }
22605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                audio_io_handle_t curOutput = mOutputs.keyAt(j);
22613cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x) on output %d",
22625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                      mute ? "muting" : "unmuting", i, curDevice, curOutput);
22635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                setStrategyMute((routing_strategy)i, mute, curOutput, mute ? 0 : delayMs);
22649029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                if (desc->strategyRefCount((routing_strategy)i) != 0) {
22659029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                    if (tempMute) {
226601e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                        setStrategyMute((routing_strategy)i, true, curOutput);
226701e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                        setStrategyMute((routing_strategy)i, false, curOutput,
226801e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                            desc->latency() * 2, device);
22699029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                    }
22709029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                    if (tempMute || mute) {
22719029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                        if (muteWaitMs < desc->latency()) {
22729029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                            muteWaitMs = desc->latency();
22739029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                        }
22745ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                    }
22755ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                }
22765ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
22775ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
22785ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
22795ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
22805ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // FIXME: should not need to double latency if volume could be applied immediately by the
22815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // audioflinger mixer. We must account for the delay between now and the next time
22825ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // the audioflinger thread for this output will process a buffer (which corresponds to
22835ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // one buffer size, usually 1/2 or 1/4 of the latency).
22845ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    muteWaitMs *= 2;
22855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // wait for the PCM output buffers to empty before proceeding with the rest of the command
22865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (muteWaitMs > delayMs) {
2287b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        muteWaitMs -= delayMs;
2288b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        usleep(muteWaitMs * 1000);
2289b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        return muteWaitMs;
2290f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2291b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent    return 0;
2292f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2293f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2294b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurentuint32_t AudioPolicyManagerBase::setOutputDevice(audio_io_handle_t output,
2295f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                                             audio_devices_t device,
2296f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                                             bool force,
2297f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                                             int delayMs)
2298f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
22995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGV("setOutputDevice() output %d device %04x delayMs %d", output, device, delayMs);
2300f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
23015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    AudioParameter param;
2302b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent    uint32_t muteWaitMs = 0;
2303f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2304f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (outputDesc->isDuplicated()) {
2305b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        muteWaitMs = setOutputDevice(outputDesc->mOutput1->mId, device, force, delayMs);
2306b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        muteWaitMs += setOutputDevice(outputDesc->mOutput2->mId, device, force, delayMs);
2307b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        return muteWaitMs;
2308f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2309f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // filter devices according to output selected
2310f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent    device = (audio_devices_t)(device & outputDesc->mProfile->mSupportedDevices);
2311f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
23125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    audio_devices_t prevDevice = outputDesc->mDevice;
23135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
23145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGV("setOutputDevice() prevDevice %04x", prevDevice);
23155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
2316c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    if (device != APM_AUDIO_DEVICE_NONE) {
23175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        outputDesc->mDevice = device;
23185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
23199029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs);
23205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
2321f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // Do not change the routing if:
2322c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    //  - the requested device is APM_AUDIO_DEVICE_NONE
2323f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //  - the requested device is the same as current device and force is not specified.
2324f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // Doing this check here allows the caller to call setOutputDevice() without conditions
2325c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    if ((device == APM_AUDIO_DEVICE_NONE || device == prevDevice) && !force) {
23265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        ALOGV("setOutputDevice() setting same device %04x or null device for output %d", device, output);
2327b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        return muteWaitMs;
2328f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2329f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
23305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGV("setOutputDevice() changing device");
2331f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // do the routing
2332f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    param.addInt(String8(AudioParameter::keyRouting), (int)device);
23335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    mpClientInterface->setParameters(output, param.toString(), delayMs);
23345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
2335f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // update stream volumes according to new device
2336f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    applyStreamVolumes(output, device, delayMs);
2337b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent
2338b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent    return muteWaitMs;
23395ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
2340f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
23415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric LaurentAudioPolicyManagerBase::IOProfile *AudioPolicyManagerBase::getInputProfile(audio_devices_t device,
23425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                   uint32_t samplingRate,
23435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                   uint32_t format,
23445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                   uint32_t channelMask)
23455ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
23465ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // Choose an input profile based on the requested capture parameters: select the first available
23475ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // profile supporting all requested parameters.
23485ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
234970c236c9290732782d5267935af1475b8d5ae602Eric Laurent    for (size_t i = 0; i < mHwModules.size(); i++)
23505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    {
235170c236c9290732782d5267935af1475b8d5ae602Eric Laurent        if (mHwModules[i]->mHandle == 0) {
23525ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            continue;
23535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
235470c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
23555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        {
235670c236c9290732782d5267935af1475b8d5ae602Eric Laurent            IOProfile *profile = mHwModules[i]->mInputProfiles[j];
235770c236c9290732782d5267935af1475b8d5ae602Eric Laurent            if (profile->isCompatibleProfile(device, samplingRate, format,
23580977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent                                             channelMask,(audio_output_flags_t)0)) {
235970c236c9290732782d5267935af1475b8d5ae602Eric Laurent                return profile;
23605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
23615ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
2362f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
236370c236c9290732782d5267935af1475b8d5ae602Eric Laurent    return NULL;
2364f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2365f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2366f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurentaudio_devices_t AudioPolicyManagerBase::getDeviceForInputSource(int inputSource)
2367f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2368c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    uint32_t device = APM_AUDIO_DEVICE_NONE;
2369f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2370f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    switch(inputSource) {
2371f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_DEFAULT:
2372f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_MIC:
2373f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_RECOGNITION:
2374f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_COMMUNICATION:
2375f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (mForceUse[AudioSystem::FOR_RECORD] == AudioSystem::FORCE_BT_SCO &&
2376c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            mAvailableInputDevices & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
2377c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
2378c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_WIRED_HEADSET) {
2379c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = AUDIO_DEVICE_IN_WIRED_HEADSET;
2380c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_BUILTIN_MIC) {
2381c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = AUDIO_DEVICE_IN_BUILTIN_MIC;
2382f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2383f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
2384f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_CAMCORDER:
2385c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        if (mAvailableInputDevices & AUDIO_DEVICE_IN_BACK_MIC) {
2386c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = AUDIO_DEVICE_IN_BACK_MIC;
2387c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_BUILTIN_MIC) {
2388c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = AUDIO_DEVICE_IN_BUILTIN_MIC;
2389f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2390f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
2391f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_UPLINK:
2392f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_DOWNLINK:
2393f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_CALL:
2394c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        if (mAvailableInputDevices & AUDIO_DEVICE_IN_VOICE_CALL) {
2395c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = AUDIO_DEVICE_IN_VOICE_CALL;
23965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
2397f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
2398f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    default:
239964cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("getDeviceForInputSource() invalid input source %d", inputSource);
2400f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
2401f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
24026a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("getDeviceForInputSource()input source %d, device %08x", inputSource, device);
2403c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    return device;
2404f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2405f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2406f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinaudio_io_handle_t AudioPolicyManagerBase::getActiveInput()
2407f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2408f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mInputs.size(); i++) {
2409f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (mInputs.valueAt(i)->mRefCount > 0) {
2410f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return mInputs.keyAt(i);
2411f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2412f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2413f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return 0;
2414f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2415f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2416e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2417c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentaudio_devices_t AudioPolicyManagerBase::getDeviceForVolume(audio_devices_t device)
2418e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent{
2419c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    if (device == APM_AUDIO_DEVICE_NONE) {
2420e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        // this happens when forcing a route update and no track is active on an output.
2421e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        // In this case the returned category is not important.
2422c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        device =  AUDIO_DEVICE_OUT_SPEAKER;
2423c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    } else if (AudioSystem::popCount(device) > 1) {
2424e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        // Multiple device selection is either:
2425e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        //  - speaker + one other device: give priority to speaker in this case.
2426e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        //  - one A2DP device + another device: happens with duplicated output. In this case
2427e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        // retain the device on the A2DP output as the other must not correspond to an active
2428e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        // selection if not the speaker.
2429c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        if (device & AUDIO_DEVICE_OUT_SPEAKER) {
2430c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            device = AUDIO_DEVICE_OUT_SPEAKER;
2431c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        } else {
2432c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            device = (audio_devices_t)(device & AUDIO_DEVICE_OUT_ALL_A2DP);
2433c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        }
2434e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    }
2435e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
243664cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block    ALOGW_IF(AudioSystem::popCount(device) != 1,
2437c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            "getDeviceForVolume() invalid device combination: %08x",
2438e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            device);
2439e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2440c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    return device;
2441c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent}
2442c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
2443f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric LaurentAudioPolicyManagerBase::device_category AudioPolicyManagerBase::getDeviceCategory(audio_devices_t device)
2444c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent{
2445f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent    switch(getDeviceForVolume(device)) {
2446e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_EARPIECE:
2447e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            return DEVICE_CATEGORY_EARPIECE;
2448e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_WIRED_HEADSET:
2449e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_WIRED_HEADPHONE:
2450e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_BLUETOOTH_SCO:
2451e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET:
2452e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP:
2453e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES:
2454e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            return DEVICE_CATEGORY_HEADSET;
2455e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_SPEAKER:
2456e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT:
2457e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER:
2458c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        case AUDIO_DEVICE_OUT_AUX_DIGITAL:
2459599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent        case AUDIO_DEVICE_OUT_USB_ACCESSORY:
2460599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent        case AUDIO_DEVICE_OUT_USB_DEVICE:
2461e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        default:
2462e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            return DEVICE_CATEGORY_SPEAKER;
2463e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    }
2464e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent}
2465e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2466f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurentfloat AudioPolicyManagerBase::volIndexToAmpl(audio_devices_t device, const StreamDescriptor& streamDesc,
2467e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        int indexInUi)
2468e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent{
2469e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    device_category deviceCategory = getDeviceCategory(device);
2470e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    const VolumeCurvePoint *curve = streamDesc.mVolumeCurve[deviceCategory];
2471e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2472f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // the volume index in the UI is relative to the min and max volume indices for this stream type
2473e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    int nbSteps = 1 + curve[VOLMAX].mIndex -
2474e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            curve[VOLMIN].mIndex;
2475f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    int volIdx = (nbSteps * (indexInUi - streamDesc.mIndexMin)) /
2476f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            (streamDesc.mIndexMax - streamDesc.mIndexMin);
2477f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2478f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // find what part of the curve this index volume belongs to, or if it's out of bounds
2479f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    int segment = 0;
2480e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    if (volIdx < curve[VOLMIN].mIndex) {         // out of bounds
2481f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return 0.0f;
2482e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    } else if (volIdx < curve[VOLKNEE1].mIndex) {
2483f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        segment = 0;
2484e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    } else if (volIdx < curve[VOLKNEE2].mIndex) {
2485f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        segment = 1;
2486e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    } else if (volIdx <= curve[VOLMAX].mIndex) {
2487f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        segment = 2;
2488f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {                                                               // out of bounds
2489f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return 1.0f;
2490f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2491f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2492f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // linear interpolation in the attenuation table in dB
2493e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    float decibels = curve[segment].mDBAttenuation +
2494e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            ((float)(volIdx - curve[segment].mIndex)) *
2495e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent                ( (curve[segment+1].mDBAttenuation -
2496e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent                        curve[segment].mDBAttenuation) /
2497e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent                    ((float)(curve[segment+1].mIndex -
2498e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent                            curve[segment].mIndex)) );
2499f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2500f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    float amplification = exp( decibels * 0.115129f); // exp( dB * ln(10) / 20 )
2501f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
25023cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGVV("VOLUME vol index=[%d %d %d], dB=[%.1f %.1f %.1f] ampl=%.5f",
2503e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            curve[segment].mIndex, volIdx,
2504e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            curve[segment+1].mIndex,
2505e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            curve[segment].mDBAttenuation,
2506cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent            decibels,
2507e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            curve[segment+1].mDBAttenuation,
2508f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            amplification);
2509f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2510f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return amplification;
2511f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2512f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2513cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
2514e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    AudioPolicyManagerBase::sDefaultVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
2515e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    {1, -49.5f}, {33, -33.5f}, {66, -17.0f}, {100, 0.0f}
2516e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent};
2517e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2518e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
2519e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    AudioPolicyManagerBase::sDefaultMediaVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
2520e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    {1, -58.0f}, {20, -40.0f}, {60, -17.0f}, {100, 0.0f}
2521cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent};
2522cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent
2523e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
2524e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    AudioPolicyManagerBase::sSpeakerMediaVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
2525e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    {1, -56.0f}, {20, -34.0f}, {60, -11.0f}, {100, 0.0f}
2526e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent};
2527e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2528e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
2529e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    AudioPolicyManagerBase::sSpeakerSonificationVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
2530e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    {1, -29.7f}, {33, -20.1f}, {66, -10.2f}, {100, 0.0f}
2531e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent};
2532e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2533ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent// AUDIO_STREAM_SYSTEM, AUDIO_STREAM_ENFORCED_AUDIBLE and AUDIO_STREAM_DTMF volume tracks
2534ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent// AUDIO_STREAM_RING on phones and AUDIO_STREAM_MUSIC on tablets (See AudioService.java).
2535123897874418f9f0e48bb89386d8c470e6975f28Jean-Michel Trivi// The range is constrained between -24dB and -6dB over speaker and -30dB and -18dB over headset.
2536ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
2537ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    AudioPolicyManagerBase::sDefaultSystemVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
2538123897874418f9f0e48bb89386d8c470e6975f28Jean-Michel Trivi    {1, -24.0f}, {33, -18.0f}, {66, -12.0f}, {100, -6.0f}
2539ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent};
2540ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent
2541ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
2542ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    AudioPolicyManagerBase::sHeadsetSystemVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
25437465678e0d5711ebcd78ae47b3a76821534a23eaEric Laurent    {1, -30.0f}, {33, -26.0f}, {66, -22.0f}, {100, -18.0f}
2544ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent};
2545e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2546e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
2547ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent            *AudioPolicyManagerBase::sVolumeProfiles[AUDIO_STREAM_CNT]
2548e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent                                                   [AudioPolicyManagerBase::DEVICE_CATEGORY_CNT] = {
2549ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_VOICE_CALL
2550e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
2551e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sDefaultVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2552e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sDefaultVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2553e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    },
2554ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_SYSTEM
2555ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET
2556ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2557ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultSystemVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2558ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    },
2559ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_RING
2560e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
2561e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2562e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sDefaultVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2563e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    },
2564ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_MUSIC
2565ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultMediaVolumeCurve, // DEVICE_CATEGORY_HEADSET
2566ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sSpeakerMediaVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2567ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultMediaVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2568ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    },
2569ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_ALARM
257012bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
257112bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER
257212bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        sDefaultVolumeCurve  // DEVICE_CATEGORY_EARPIECE
257312bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    },
2574ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_NOTIFICATION
2575e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
2576ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2577e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sDefaultVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2578c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    },
2579ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_BLUETOOTH_SCO
2580c16ac09f510437e8340be691720177a490ae78f0Eric Laurent        sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
2581ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2582c16ac09f510437e8340be691720177a490ae78f0Eric Laurent        sDefaultVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2583c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    },
2584ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_ENFORCED_AUDIBLE
2585ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET
2586ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2587ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultSystemVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2588ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    },
2589ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    {  // AUDIO_STREAM_DTMF
2590ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET
2591ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2592ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultSystemVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2593ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    },
2594ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_TTS
2595ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultMediaVolumeCurve, // DEVICE_CATEGORY_HEADSET
2596ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sSpeakerMediaVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2597ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultMediaVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2598ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    },
2599e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent};
2600e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2601e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurentvoid AudioPolicyManagerBase::initializeVolumeCurves()
2602e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent{
2603ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    for (int i = 0; i < AUDIO_STREAM_CNT; i++) {
2604e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) {
2605cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent            mStreams[i].mVolumeCurve[j] =
2606ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent                    sVolumeProfiles[i][j];
2607cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent        }
2608cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent    }
2609f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2610f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2611c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentfloat AudioPolicyManagerBase::computeVolume(int stream,
2612c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                            int index,
2613c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                            audio_io_handle_t output,
2614f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                                            audio_devices_t device)
2615f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2616f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    float volume = 1.0;
2617f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
2618f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    StreamDescriptor &streamDesc = mStreams[stream];
2619f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2620c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    if (device == APM_AUDIO_DEVICE_NONE) {
2621f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device = outputDesc->device();
2622f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2623f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2624f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if volume is not 0 (not muted), force media volume to max on digital output
2625f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (stream == AudioSystem::MUSIC &&
2626f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        index != mStreams[stream].mIndexMin &&
2627f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent        (device == AUDIO_DEVICE_OUT_AUX_DIGITAL ||
2628599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent         device == AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET ||
2629599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent         device == AUDIO_DEVICE_OUT_USB_ACCESSORY ||
2630599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent         device == AUDIO_DEVICE_OUT_USB_DEVICE)) {
2631f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return 1.0;
2632f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2633f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2634f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    volume = volIndexToAmpl(device, streamDesc, index);
2635f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2636f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if a headset is connected, apply the following rules to ring tones and notifications
2637f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // to avoid sound level bursts in user's ears:
2638f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // - always attenuate ring tones and notifications volume by 6dB
2639f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // - if music is playing, always limit the volume to current music volume,
2640f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // with a minimum threshold at -36dB so that notification is always perceived.
264112bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    const routing_strategy stream_strategy = getStrategy((AudioSystem::stream_type)stream);
2642c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
2643c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
2644c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            AUDIO_DEVICE_OUT_WIRED_HEADSET |
2645c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) &&
264612bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        ((stream_strategy == STRATEGY_SONIFICATION)
264712bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi                || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL)
264812bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi                || (stream == AudioSystem::SYSTEM)) &&
2649f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        streamDesc.mCanBeMuted) {
2650f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        volume *= SONIFICATION_HEADSET_VOLUME_FACTOR;
2651f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // when the phone is ringing we must consider that music could have been paused just before
2652f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // by the music application and behave as if music was active if the last music track was
2653f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // just stopped
2654ac3cf10ef6de12e69540a1244ac7255f93fa7502Eric Laurent        if (isStreamActive(AudioSystem::MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) ||
2655ac3cf10ef6de12e69540a1244ac7255f93fa7502Eric Laurent                mLimitRingtoneVolume) {
265617a73c3394547692457299dc512b5c2312ea0344Eric Laurent            audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/);
2657c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            float musicVol = computeVolume(AudioSystem::MUSIC,
265817a73c3394547692457299dc512b5c2312ea0344Eric Laurent                               mStreams[AudioSystem::MUSIC].getVolumeIndex(musicDevice),
2659c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                               output,
266017a73c3394547692457299dc512b5c2312ea0344Eric Laurent                               musicDevice);
2661c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            float minVol = (musicVol > SONIFICATION_HEADSET_VOLUME_MIN) ?
2662c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                musicVol : SONIFICATION_HEADSET_VOLUME_MIN;
2663f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (volume > minVol) {
2664f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                volume = minVol;
26656a70518b93928d1c91457ff805e375c82d76b0e5Steve Block                ALOGV("computeVolume limiting volume to %f musicVol %f", minVol, musicVol);
2666f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2667f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2668f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2669f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2670f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return volume;
2671f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2672f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2673c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentstatus_t AudioPolicyManagerBase::checkAndSetVolume(int stream,
2674c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                   int index,
2675c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                   audio_io_handle_t output,
2676f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                                                   audio_devices_t device,
2677c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                   int delayMs,
2678c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                   bool force)
2679f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2680f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2681f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // do not change actual stream volume if the stream is muted
2682f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mOutputs.valueFor(output)->mMuteCount[stream] != 0) {
26833cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        ALOGVV("checkAndSetVolume() stream %d muted count %d",
26845ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent              stream, mOutputs.valueFor(output)->mMuteCount[stream]);
2685f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return NO_ERROR;
2686f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2687f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2688f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // do not change in call volume if bluetooth is connected and vice versa
2689f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if ((stream == AudioSystem::VOICE_CALL && mForceUse[AudioSystem::FOR_COMMUNICATION] == AudioSystem::FORCE_BT_SCO) ||
2690f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        (stream == AudioSystem::BLUETOOTH_SCO && mForceUse[AudioSystem::FOR_COMMUNICATION] != AudioSystem::FORCE_BT_SCO)) {
26916a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
2692f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin             stream, mForceUse[AudioSystem::FOR_COMMUNICATION]);
2693f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return INVALID_OPERATION;
2694f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2695f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2696f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    float volume = computeVolume(stream, index, output, device);
2697f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // We actually change the volume if:
2698f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // - the float value returned by computeVolume() changed
2699f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // - the force flag is set
2700f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (volume != mOutputs.valueFor(output)->mCurVolume[stream] ||
2701f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            force) {
2702f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mOutputs.valueFor(output)->mCurVolume[stream] = volume;
27033cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        ALOGVV("checkAndSetVolume() for output %d stream %d, volume %f, delay %d", output, stream, volume, delayMs);
2704f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (stream == AudioSystem::VOICE_CALL ||
2705f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            stream == AudioSystem::DTMF ||
2706f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            stream == AudioSystem::BLUETOOTH_SCO) {
2707f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // offset value to reflect actual hardware volume that never reaches 0
2708f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // 1% corresponds roughly to first step in VOICE_CALL stream volume setting (see AudioService.java)
2709f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            volume = 0.01 + 0.99 * volume;
2710f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // Force VOICE_CALL to track BLUETOOTH_SCO stream volume when bluetooth audio is
2711f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // enabled
2712f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (stream == AudioSystem::BLUETOOTH_SCO) {
2713f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mpClientInterface->setStreamVolume(AudioSystem::VOICE_CALL, volume, output, delayMs);
2714f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2715f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2716f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2717f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mpClientInterface->setStreamVolume((AudioSystem::stream_type)stream, volume, output, delayMs);
2718f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2719f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2720f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (stream == AudioSystem::VOICE_CALL ||
2721f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        stream == AudioSystem::BLUETOOTH_SCO) {
2722f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        float voiceVolume;
2723f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // Force voice volume to max for bluetooth SCO as volume is managed by the headset
2724f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (stream == AudioSystem::VOICE_CALL) {
2725f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            voiceVolume = (float)index/(float)mStreams[stream].mIndexMax;
2726f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        } else {
2727f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            voiceVolume = 1.0;
2728f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2729f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2730b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        if (voiceVolume != mLastVoiceVolume && output == mPrimaryOutput) {
2731f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
2732f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mLastVoiceVolume = voiceVolume;
2733f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2734f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2735f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2736f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
2737f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2738f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2739c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentvoid AudioPolicyManagerBase::applyStreamVolumes(audio_io_handle_t output,
2740f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                                                audio_devices_t device,
2741c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                int delayMs,
2742c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                bool force)
2743f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
27443cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGVV("applyStreamVolumes() for output %d and device %x", output, device);
2745f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2746f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
2747c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        checkAndSetVolume(stream,
2748f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                          mStreams[stream].getVolumeIndex(device),
2749c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                          output,
2750c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                          device,
2751c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                          delayMs,
2752c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                          force);
2753f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2754f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2755f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
275601e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurentvoid AudioPolicyManagerBase::setStrategyMute(routing_strategy strategy,
275701e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                             bool on,
275801e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                             audio_io_handle_t output,
275901e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                             int delayMs,
276001e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                             audio_devices_t device)
2761f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
27623cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGVV("setStrategyMute() strategy %d, mute %d, output %d", strategy, on, output);
2763f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
2764f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (getStrategy((AudioSystem::stream_type)stream) == strategy) {
276501e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent            setStreamMute(stream, on, output, delayMs, device);
2766f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2767f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2768f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2769f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
277001e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurentvoid AudioPolicyManagerBase::setStreamMute(int stream,
277101e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                           bool on,
277201e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                           audio_io_handle_t output,
277301e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                           int delayMs,
277401e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                           audio_devices_t device)
2775f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2776f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    StreamDescriptor &streamDesc = mStreams[stream];
2777f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
2778c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    if (device == APM_AUDIO_DEVICE_NONE) {
277901e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent        device = outputDesc->device();
278001e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent    }
2781f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
27823cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGVV("setStreamMute() stream %d, mute %d, output %d, mMuteCount %d device %04x",
278301e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent          stream, on, output, outputDesc->mMuteCount[stream], device);
2784f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2785f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (on) {
2786f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (outputDesc->mMuteCount[stream] == 0) {
2787f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (streamDesc.mCanBeMuted) {
2788c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                checkAndSetVolume(stream, 0, output, device, delayMs);
2789f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2790f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2791f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
2792f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mMuteCount[stream]++;
2793f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
2794f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (outputDesc->mMuteCount[stream] == 0) {
27953cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            ALOGV("setStreamMute() unmuting non muted stream!");
2796f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return;
2797f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2798f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (--outputDesc->mMuteCount[stream] == 0) {
2799c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            checkAndSetVolume(stream,
2800c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                              streamDesc.getVolumeIndex(device),
2801c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                              output,
2802c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                              device,
2803c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                              delayMs);
2804f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2805f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2806f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2807f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2808f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::handleIncallSonification(int stream, bool starting, bool stateChange)
2809f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2810f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if the stream pertains to sonification strategy and we are in call we must
2811f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // mute the stream if it is low visibility. If it is high visibility, we must play a tone
2812f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // in the device used for phone strategy and play the tone if the selected device does not
2813f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // interfere with the device used for phone strategy
2814f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
2815f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // many times as there are active tracks on the output
281612bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    const routing_strategy stream_strategy = getStrategy((AudioSystem::stream_type)stream);
281712bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    if ((stream_strategy == STRATEGY_SONIFICATION) ||
281812bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi            ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
2819b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        AudioOutputDescriptor *outputDesc = mOutputs.valueFor(mPrimaryOutput);
28206a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
2821f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                stream, starting, outputDesc->mDevice, stateChange);
2822f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (outputDesc->mRefCount[stream]) {
2823f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            int muteCount = 1;
2824f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (stateChange) {
2825f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                muteCount = outputDesc->mRefCount[stream];
2826f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2827f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (AudioSystem::isLowVisibility((AudioSystem::stream_type)stream)) {
28286a70518b93928d1c91457ff805e375c82d76b0e5Steve Block                ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
2829f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                for (int i = 0; i < muteCount; i++) {
2830b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                    setStreamMute(stream, starting, mPrimaryOutput);
2831f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
2832f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            } else {
28336a70518b93928d1c91457ff805e375c82d76b0e5Steve Block                ALOGV("handleIncallSonification() high visibility");
28345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                if (outputDesc->device() &
28355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                        getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
28366a70518b93928d1c91457ff805e375c82d76b0e5Steve Block                    ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
2837f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    for (int i = 0; i < muteCount; i++) {
2838b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                        setStreamMute(stream, starting, mPrimaryOutput);
2839f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    }
2840f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
2841f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (starting) {
2842f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    mpClientInterface->startTone(ToneGenerator::TONE_SUP_CALL_WAITING, AudioSystem::VOICE_CALL);
2843f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else {
2844f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    mpClientInterface->stopTone();
2845f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
2846f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2847f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2848f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2849f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2850f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2851f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinbool AudioPolicyManagerBase::isInCall()
2852f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2853f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return isStateInCall(mPhoneState);
2854f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2855f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2856f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinbool AudioPolicyManagerBase::isStateInCall(int state) {
2857f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return ((state == AudioSystem::MODE_IN_CALL) ||
2858f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            (state == AudioSystem::MODE_IN_COMMUNICATION));
2859f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2860f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
286170c236c9290732782d5267935af1475b8d5ae602Eric Laurentbool AudioPolicyManagerBase::needsDirectOuput(audio_stream_type_t stream,
286270c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                              uint32_t samplingRate,
286370c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                              audio_format_t format,
286470c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                              audio_channel_mask_t channelMask,
28650977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent                                              audio_output_flags_t flags,
286670c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                              audio_devices_t device)
2867f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2868f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin   return ((flags & AudioSystem::OUTPUT_FLAG_DIRECT) ||
2869b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent          (format != 0 && !AudioSystem::isLinearPCM(format)));
2870f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2871f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2872f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinuint32_t AudioPolicyManagerBase::getMaxEffectsCpuLoad()
2873f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2874f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return MAX_EFFECTS_CPU_LOAD;
2875f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2876f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2877f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinuint32_t AudioPolicyManagerBase::getMaxEffectsMemory()
2878f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2879f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return MAX_EFFECTS_MEMORY;
2880f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2881f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2882f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// --- AudioOutputDescriptor class implementation
2883f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2884b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric LaurentAudioPolicyManagerBase::AudioOutputDescriptor::AudioOutputDescriptor(
28855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        const IOProfile *profile)
288670c236c9290732782d5267935af1475b8d5ae602Eric Laurent    : mId(0), mSamplingRate(0), mFormat((audio_format_t)0),
288770c236c9290732782d5267935af1475b8d5ae602Eric Laurent      mChannelMask((audio_channel_mask_t)0), mLatency(0),
2888c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    mFlags((audio_output_flags_t)0), mDevice(APM_AUDIO_DEVICE_NONE),
2889f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent    mOutput1(0), mOutput2(0), mProfile(profile)
2890f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2891f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // clear usage count for all stream types
2892f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) {
2893f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mRefCount[i] = 0;
2894f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mCurVolume[i] = -1.0;
2895f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mMuteCount[i] = 0;
2896f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mStopTime[i] = 0;
2897f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
28983cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    if (profile != NULL) {
28993cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        mSamplingRate = profile->mSamplingRates[0];
29003cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        mFormat = profile->mFormats[0];
29013cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        mChannelMask = profile->mChannelMasks[0];
29023cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        mFlags = profile->mFlags;
29033cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    }
2904f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2905f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2906f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurentaudio_devices_t AudioPolicyManagerBase::AudioOutputDescriptor::device()
2907f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2908f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (isDuplicated()) {
2909f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent        return (audio_devices_t)(mOutput1->mDevice | mOutput2->mDevice);
2910f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
2911f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent        return mDevice;
2912f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2913f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2914f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
29155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentuint32_t AudioPolicyManagerBase::AudioOutputDescriptor::latency()
29165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
29175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (isDuplicated()) {
29185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return (mOutput1->mLatency > mOutput2->mLatency) ? mOutput1->mLatency : mOutput2->mLatency;
29195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    } else {
29205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return mLatency;
29215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
29225ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
29235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
29245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentbool AudioPolicyManagerBase::AudioOutputDescriptor::sharesHwModuleWith(
29255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        const AudioOutputDescriptor *outputDesc)
29265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
29275ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (isDuplicated()) {
29285ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return mOutput1->sharesHwModuleWith(outputDesc) || mOutput2->sharesHwModuleWith(outputDesc);
29295ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    } else if (outputDesc->isDuplicated()){
29305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return sharesHwModuleWith(outputDesc->mOutput1) || sharesHwModuleWith(outputDesc->mOutput2);
29315ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    } else {
293270c236c9290732782d5267935af1475b8d5ae602Eric Laurent        return (mProfile->mModule == outputDesc->mProfile->mModule);
29335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
29345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
29355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
2936f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::AudioOutputDescriptor::changeRefCount(AudioSystem::stream_type stream, int delta)
2937f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2938f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // forward usage count change to attached outputs
2939f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (isDuplicated()) {
2940f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mOutput1->changeRefCount(stream, delta);
2941f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mOutput2->changeRefCount(stream, delta);
2942f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2943f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if ((delta + (int)mRefCount[stream]) < 0) {
294464cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("changeRefCount() invalid delta %d for stream %d, refCount %d", delta, stream, mRefCount[stream]);
2945f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mRefCount[stream] = 0;
2946f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
2947f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2948f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mRefCount[stream] += delta;
29496a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("changeRefCount() stream %d, count %d", stream, mRefCount[stream]);
2950f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2951f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2952f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinuint32_t AudioPolicyManagerBase::AudioOutputDescriptor::refCount()
2953f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2954f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    uint32_t refcount = 0;
2955f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) {
2956f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        refcount += mRefCount[i];
2957f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2958f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return refcount;
2959f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2960f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2961f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinuint32_t AudioPolicyManagerBase::AudioOutputDescriptor::strategyRefCount(routing_strategy strategy)
2962f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2963f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    uint32_t refCount = 0;
2964f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) {
2965f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (getStrategy((AudioSystem::stream_type)i) == strategy) {
2966f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            refCount += mRefCount[i];
2967f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2968f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2969f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return refCount;
2970f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2971f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2972f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurentaudio_devices_t AudioPolicyManagerBase::AudioOutputDescriptor::supportedDevices()
2973b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent{
2974b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (isDuplicated()) {
2975f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent        return (audio_devices_t)(mOutput1->supportedDevices() | mOutput2->supportedDevices());
2976b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    } else {
2977b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        return mProfile->mSupportedDevices ;
2978b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    }
2979b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent}
2980b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
2981f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::AudioOutputDescriptor::dump(int fd)
2982f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2983f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    const size_t SIZE = 256;
2984f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    char buffer[SIZE];
2985f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    String8 result;
2986f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2987f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Sampling rate: %d\n", mSamplingRate);
2988f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
2989f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Format: %d\n", mFormat);
2990f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
299170c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, " Channels: %08x\n", mChannelMask);
2992f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
2993f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Latency: %d\n", mLatency);
2994f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
2995f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Flags %08x\n", mFlags);
2996f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
2997f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Devices %08x\n", device());
2998f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
2999f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Stream volume refCount muteCount\n");
3000f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3001f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) {
3002f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        snprintf(buffer, SIZE, " %02d     %.03f     %02d       %02d\n", i, mCurVolume[i], mRefCount[i], mMuteCount[i]);
3003f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        result.append(buffer);
3004f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3005f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, result.string(), result.size());
3006f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3007f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
3008f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3009f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3010f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// --- AudioInputDescriptor class implementation
3011f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
30125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric LaurentAudioPolicyManagerBase::AudioInputDescriptor::AudioInputDescriptor(const IOProfile *profile)
301370c236c9290732782d5267935af1475b8d5ae602Eric Laurent    : mSamplingRate(0), mFormat((audio_format_t)0), mChannelMask((audio_channel_mask_t)0),
3014c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi      mDevice(APM_AUDIO_DEVICE_NONE), mRefCount(0),
30155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent      mInputSource(0), mProfile(profile)
3016f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3017f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3018f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3019f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::AudioInputDescriptor::dump(int fd)
3020f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3021f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    const size_t SIZE = 256;
3022f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    char buffer[SIZE];
3023f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    String8 result;
3024f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3025f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Sampling rate: %d\n", mSamplingRate);
3026f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3027f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Format: %d\n", mFormat);
3028f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
302970c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, " Channels: %08x\n", mChannelMask);
3030f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3031f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Devices %08x\n", mDevice);
3032f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3033f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Ref Count %d\n", mRefCount);
3034f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3035f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, result.string(), result.size());
3036f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3037f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
3038f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3039f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3040f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// --- StreamDescriptor class implementation
3041f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3042c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric LaurentAudioPolicyManagerBase::StreamDescriptor::StreamDescriptor()
3043c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    :   mIndexMin(0), mIndexMax(1), mCanBeMuted(true)
3044c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent{
3045c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    mIndexCur.add(AUDIO_DEVICE_OUT_DEFAULT, 0);
3046c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent}
3047c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
3048c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentint AudioPolicyManagerBase::StreamDescriptor::getVolumeIndex(audio_devices_t device)
3049c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent{
3050c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    device = AudioPolicyManagerBase::getDeviceForVolume(device);
3051c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    // there is always a valid entry for AUDIO_DEVICE_OUT_DEFAULT
3052c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    if (mIndexCur.indexOfKey(device) < 0) {
3053c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        device = AUDIO_DEVICE_OUT_DEFAULT;
3054c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    }
3055c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    return mIndexCur.valueFor(device);
3056c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent}
3057c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
3058c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentvoid AudioPolicyManagerBase::StreamDescriptor::dump(int fd)
3059f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3060c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    const size_t SIZE = 256;
3061c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    char buffer[SIZE];
3062c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    String8 result;
3063c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
3064c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    snprintf(buffer, SIZE, "%s         %02d         %02d         ",
3065c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent             mCanBeMuted ? "true " : "false", mIndexMin, mIndexMax);
3066c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    result.append(buffer);
3067c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    for (size_t i = 0; i < mIndexCur.size(); i++) {
3068c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        snprintf(buffer, SIZE, "%04x : %02d, ",
3069c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                 mIndexCur.keyAt(i),
3070c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                 mIndexCur.valueAt(i));
3071c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        result.append(buffer);
3072c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    }
3073c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    result.append("\n");
3074c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
3075c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    write(fd, result.string(), result.size());
3076f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3077f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3078f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// --- EffectDescriptor class implementation
3079f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3080f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::EffectDescriptor::dump(int fd)
3081f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3082f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    const size_t SIZE = 256;
3083f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    char buffer[SIZE];
3084f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    String8 result;
3085f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
30861c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent    snprintf(buffer, SIZE, " I/O: %d\n", mIo);
3087f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3088f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Strategy: %d\n", mStrategy);
3089f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3090f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Session: %d\n", mSession);
3091f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3092f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Name: %s\n",  mDesc.name);
3093f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3094582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    snprintf(buffer, SIZE, " %s\n",  mEnabled ? "Enabled" : "Disabled");
3095582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    result.append(buffer);
3096f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, result.string(), result.size());
3097f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3098f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
3099f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3100f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
31015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent// --- IOProfile class implementation
31025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
310370c236c9290732782d5267935af1475b8d5ae602Eric LaurentAudioPolicyManagerBase::HwModule::HwModule(const char *name)
310470c236c9290732782d5267935af1475b8d5ae602Eric Laurent    : mName(strndup(name, AUDIO_HARDWARE_MODULE_ID_MAX_LEN)), mHandle(0)
310570c236c9290732782d5267935af1475b8d5ae602Eric Laurent{
310670c236c9290732782d5267935af1475b8d5ae602Eric Laurent}
310770c236c9290732782d5267935af1475b8d5ae602Eric Laurent
310870c236c9290732782d5267935af1475b8d5ae602Eric LaurentAudioPolicyManagerBase::HwModule::~HwModule()
310970c236c9290732782d5267935af1475b8d5ae602Eric Laurent{
311070c236c9290732782d5267935af1475b8d5ae602Eric Laurent    for (size_t i = 0; i < mOutputProfiles.size(); i++) {
311170c236c9290732782d5267935af1475b8d5ae602Eric Laurent         delete mOutputProfiles[i];
311270c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
311370c236c9290732782d5267935af1475b8d5ae602Eric Laurent    for (size_t i = 0; i < mInputProfiles.size(); i++) {
311470c236c9290732782d5267935af1475b8d5ae602Eric Laurent         delete mInputProfiles[i];
311570c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
311670c236c9290732782d5267935af1475b8d5ae602Eric Laurent    free((void *)mName);
311770c236c9290732782d5267935af1475b8d5ae602Eric Laurent}
311870c236c9290732782d5267935af1475b8d5ae602Eric Laurent
311970c236c9290732782d5267935af1475b8d5ae602Eric Laurentvoid AudioPolicyManagerBase::HwModule::dump(int fd)
312070c236c9290732782d5267935af1475b8d5ae602Eric Laurent{
312170c236c9290732782d5267935af1475b8d5ae602Eric Laurent    const size_t SIZE = 256;
312270c236c9290732782d5267935af1475b8d5ae602Eric Laurent    char buffer[SIZE];
312370c236c9290732782d5267935af1475b8d5ae602Eric Laurent    String8 result;
312470c236c9290732782d5267935af1475b8d5ae602Eric Laurent
312570c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "  - name: %s\n", mName);
312670c236c9290732782d5267935af1475b8d5ae602Eric Laurent    result.append(buffer);
312770c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "  - handle: %d\n", mHandle);
312870c236c9290732782d5267935af1475b8d5ae602Eric Laurent    result.append(buffer);
312970c236c9290732782d5267935af1475b8d5ae602Eric Laurent    write(fd, result.string(), result.size());
313070c236c9290732782d5267935af1475b8d5ae602Eric Laurent    if (mOutputProfiles.size()) {
313170c236c9290732782d5267935af1475b8d5ae602Eric Laurent        write(fd, "  - outputs:\n", sizeof("  - outputs:\n"));
313270c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (size_t i = 0; i < mOutputProfiles.size(); i++) {
3133599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            snprintf(buffer, SIZE, "    output %d:\n", i);
3134599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            write(fd, buffer, strlen(buffer));
313570c236c9290732782d5267935af1475b8d5ae602Eric Laurent            mOutputProfiles[i]->dump(fd);
313670c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
313770c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
313870c236c9290732782d5267935af1475b8d5ae602Eric Laurent    if (mInputProfiles.size()) {
313970c236c9290732782d5267935af1475b8d5ae602Eric Laurent        write(fd, "  - inputs:\n", sizeof("  - inputs:\n"));
314070c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (size_t i = 0; i < mInputProfiles.size(); i++) {
3141599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            snprintf(buffer, SIZE, "    input %d:\n", i);
3142599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            write(fd, buffer, strlen(buffer));
314370c236c9290732782d5267935af1475b8d5ae602Eric Laurent            mInputProfiles[i]->dump(fd);
314470c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
314570c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
314670c236c9290732782d5267935af1475b8d5ae602Eric Laurent}
314770c236c9290732782d5267935af1475b8d5ae602Eric Laurent
314870c236c9290732782d5267935af1475b8d5ae602Eric LaurentAudioPolicyManagerBase::IOProfile::IOProfile(HwModule *module)
31490977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent    : mFlags((audio_output_flags_t)0), mModule(module)
31505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
31515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
31525ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
31535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric LaurentAudioPolicyManagerBase::IOProfile::~IOProfile()
31545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
315570c236c9290732782d5267935af1475b8d5ae602Eric Laurent}
315670c236c9290732782d5267935af1475b8d5ae602Eric Laurent
315770c236c9290732782d5267935af1475b8d5ae602Eric Laurent// checks if the IO profile is compatible with specified parameters. By convention a value of 0
315870c236c9290732782d5267935af1475b8d5ae602Eric Laurent// means a parameter is don't care
315970c236c9290732782d5267935af1475b8d5ae602Eric Laurentbool AudioPolicyManagerBase::IOProfile::isCompatibleProfile(audio_devices_t device,
316070c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                            uint32_t samplingRate,
316170c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                            uint32_t format,
316270c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                            uint32_t channelMask,
31630977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent                                                            audio_output_flags_t flags) const
316470c236c9290732782d5267935af1475b8d5ae602Eric Laurent{
316570c236c9290732782d5267935af1475b8d5ae602Eric Laurent    if ((mSupportedDevices & device) != device) {
316670c236c9290732782d5267935af1475b8d5ae602Eric Laurent        return false;
316770c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
316870c236c9290732782d5267935af1475b8d5ae602Eric Laurent    if ((mFlags & flags) != flags) {
316970c236c9290732782d5267935af1475b8d5ae602Eric Laurent        return false;
317070c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
317170c236c9290732782d5267935af1475b8d5ae602Eric Laurent    if (samplingRate != 0) {
317270c236c9290732782d5267935af1475b8d5ae602Eric Laurent        size_t i;
317370c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (i = 0; i < mSamplingRates.size(); i++)
317470c236c9290732782d5267935af1475b8d5ae602Eric Laurent        {
317570c236c9290732782d5267935af1475b8d5ae602Eric Laurent            if (mSamplingRates[i] == samplingRate) {
317670c236c9290732782d5267935af1475b8d5ae602Eric Laurent                break;
317770c236c9290732782d5267935af1475b8d5ae602Eric Laurent            }
317870c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
317970c236c9290732782d5267935af1475b8d5ae602Eric Laurent        if (i == mSamplingRates.size()) {
318070c236c9290732782d5267935af1475b8d5ae602Eric Laurent            return false;
318170c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
318270c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
318370c236c9290732782d5267935af1475b8d5ae602Eric Laurent    if (format != 0) {
318470c236c9290732782d5267935af1475b8d5ae602Eric Laurent        size_t i;
318570c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (i = 0; i < mFormats.size(); i++)
318670c236c9290732782d5267935af1475b8d5ae602Eric Laurent        {
318770c236c9290732782d5267935af1475b8d5ae602Eric Laurent            if (mFormats[i] == format) {
318870c236c9290732782d5267935af1475b8d5ae602Eric Laurent                break;
318970c236c9290732782d5267935af1475b8d5ae602Eric Laurent            }
319070c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
319170c236c9290732782d5267935af1475b8d5ae602Eric Laurent        if (i == mFormats.size()) {
319270c236c9290732782d5267935af1475b8d5ae602Eric Laurent            return false;
319370c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
319470c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
319570c236c9290732782d5267935af1475b8d5ae602Eric Laurent    if (channelMask != 0) {
319670c236c9290732782d5267935af1475b8d5ae602Eric Laurent        size_t i;
319770c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (i = 0; i < mChannelMasks.size(); i++)
319870c236c9290732782d5267935af1475b8d5ae602Eric Laurent        {
319970c236c9290732782d5267935af1475b8d5ae602Eric Laurent            if (mChannelMasks[i] == channelMask) {
320070c236c9290732782d5267935af1475b8d5ae602Eric Laurent                break;
320170c236c9290732782d5267935af1475b8d5ae602Eric Laurent            }
320270c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
320370c236c9290732782d5267935af1475b8d5ae602Eric Laurent        if (i == mChannelMasks.size()) {
320470c236c9290732782d5267935af1475b8d5ae602Eric Laurent            return false;
320570c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
320670c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
320770c236c9290732782d5267935af1475b8d5ae602Eric Laurent    return true;
32085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
32095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
32105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::IOProfile::dump(int fd)
32115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
32125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    const size_t SIZE = 256;
32135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    char buffer[SIZE];
32145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    String8 result;
32155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
321670c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "    - sampling rates: ");
32175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    result.append(buffer);
32185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < mSamplingRates.size(); i++) {
32195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        snprintf(buffer, SIZE, "%d", mSamplingRates[i]);
32205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        result.append(buffer);
32215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        result.append(i == (mSamplingRates.size() - 1) ? "\n" : ", ");
32225ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
32235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
322470c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "    - channel masks: ");
32255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    result.append(buffer);
32265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < mChannelMasks.size(); i++) {
32275ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        snprintf(buffer, SIZE, "%04x", mChannelMasks[i]);
32285ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        result.append(buffer);
32295ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        result.append(i == (mChannelMasks.size() - 1) ? "\n" : ", ");
32305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
32315ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
323270c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "    - formats: ");
32335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    result.append(buffer);
32345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < mFormats.size(); i++) {
32355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        snprintf(buffer, SIZE, "%d", mFormats[i]);
32365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        result.append(buffer);
32375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        result.append(i == (mFormats.size() - 1) ? "\n" : ", ");
32385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
32395ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
324070c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "    - devices: %04x\n", mSupportedDevices);
32415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    result.append(buffer);
324270c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "    - flags: %04x\n", mFlags);
32435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    result.append(buffer);
32445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
32455ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    write(fd, result.string(), result.size());
32465ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
32475ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
32485ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent// --- audio_policy.conf file parsing
32495ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
32505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentstruct StringToEnum {
32515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    const char *name;
32525ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    uint32_t value;
32535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent};
32545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
32555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent#define STRING_TO_ENUM(string) { #string, string }
32565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
32575ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
32585ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentconst struct StringToEnum sDeviceNameToEnumTable[] = {
32595ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_EARPIECE),
32605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_SPEAKER),
32615ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_WIRED_HEADSET),
32625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_WIRED_HEADPHONE),
32635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_ALL_SCO),
32645ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_ALL_A2DP),
32655ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_AUX_DIGITAL),
32665ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET),
3267599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET),
3268599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_USB_DEVICE),
3269599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_USB_ACCESSORY),
3270599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_ALL_USB),
32715ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_BUILTIN_MIC),
32725ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET),
32735ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_WIRED_HEADSET),
32745ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_AUX_DIGITAL),
32755ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_VOICE_CALL),
32765ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_BACK_MIC),
32775ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent};
32785ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
32795ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentconst struct StringToEnum sFlagNameToEnumTable[] = {
32800977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent    STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT),
32810977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent    STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY),
3282b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent    STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST),
3283b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent    STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER),
32845ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent};
32855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
32865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentconst struct StringToEnum sFormatNameToEnumTable[] = {
32875ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
32885ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
32895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_FORMAT_MP3),
32905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_FORMAT_AAC),
32915ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
32925ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent};
32935ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
32945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentconst struct StringToEnum sOutChannelsNameToEnumTable[] = {
32955ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_OUT_MONO),
32965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
32975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
32985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
32995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent};
33005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
33015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentconst struct StringToEnum sInChannelsNameToEnumTable[] = {
33025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_IN_MONO),
33035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_IN_STEREO),
33045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent};
33055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
33065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
33075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentuint32_t AudioPolicyManagerBase::stringToEnum(const struct StringToEnum *table,
33085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                              size_t size,
33095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                              const char *name)
33105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
33115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < size; i++) {
33125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strcmp(table[i].name, name) == 0) {
33135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("stringToEnum() found %s", table[i].name);
33145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            return table[i].value;
33155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
33165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
33175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return 0;
33185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
33195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
33200977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurentaudio_output_flags_t AudioPolicyManagerBase::parseFlagNames(char *name)
33215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
33225ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    uint32_t flag = 0;
33235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
33245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // it is OK to cast name to non const here as we are not going to use it after
33255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // strtok() modifies it
33265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    char *flagName = strtok(name, "|");
33275ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (flagName != NULL) {
33285ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strlen(flagName) != 0) {
33295ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            flag |= stringToEnum(sFlagNameToEnumTable,
33305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                               ARRAY_SIZE(sFlagNameToEnumTable),
33315ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                               flagName);
33325ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
33335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        flagName = strtok(NULL, "|");
33345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
33350977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent    return (audio_output_flags_t)flag;
33365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
33375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
33385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentaudio_devices_t AudioPolicyManagerBase::parseDeviceNames(char *name)
33395ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
33405ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    uint32_t device = 0;
33415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
33425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    char *devName = strtok(name, "|");
33435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (devName != NULL) {
33445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strlen(devName) != 0) {
33455ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device |= stringToEnum(sDeviceNameToEnumTable,
33465ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                 ARRAY_SIZE(sDeviceNameToEnumTable),
33475ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                 devName);
33485ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
33495ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        devName = strtok(NULL, "|");
33505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
3351c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    return device;
33525ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
33535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
33545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadSamplingRates(char *name, IOProfile *profile)
33555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
33565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    char *str = strtok(name, "|");
33575ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
33583cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    // by convention, "0' in the first entry in mSamplingRates indicates the supported sampling
33593cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    // rates should be read from the output stream after it is opened for the first time
33603cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) {
33613cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        profile->mSamplingRates.add(0);
33623cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        return;
33633cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    }
33643cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
33655ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (str != NULL) {
33665ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        uint32_t rate = atoi(str);
33675ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (rate != 0) {
33685ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadSamplingRates() adding rate %d", rate);
33695ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mSamplingRates.add(rate);
33705ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
33715ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        str = strtok(NULL, "|");
33725ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
33735ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return;
33745ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
33755ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
33765ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadFormats(char *name, IOProfile *profile)
33775ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
33785ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    char *str = strtok(name, "|");
33795ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
33803cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    // by convention, "0' in the first entry in mFormats indicates the supported formats
33813cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    // should be read from the output stream after it is opened for the first time
33823cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) {
33833cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        profile->mFormats.add((audio_format_t)0);
33843cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        return;
33853cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    }
33863cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
33875ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (str != NULL) {
33885ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_format_t format = (audio_format_t)stringToEnum(sFormatNameToEnumTable,
33895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                             ARRAY_SIZE(sFormatNameToEnumTable),
33905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                             str);
33915ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (format != 0) {
33925ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mFormats.add(format);
33935ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
33945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        str = strtok(NULL, "|");
33955ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
33965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return;
33975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
33985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
33995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadInChannels(char *name, IOProfile *profile)
34005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
34015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    const char *str = strtok(name, "|");
34025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
34035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGV("loadInChannels() %s", name);
34043cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
34053cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) {
34063cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        profile->mChannelMasks.add((audio_channel_mask_t)0);
34073cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        return;
34083cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    }
34093cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
34105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (str != NULL) {
34115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_channel_mask_t channelMask =
34125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                (audio_channel_mask_t)stringToEnum(sInChannelsNameToEnumTable,
34135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                   ARRAY_SIZE(sInChannelsNameToEnumTable),
34145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                   str);
34155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (channelMask != 0) {
34165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadInChannels() adding channelMask %04x", channelMask);
34175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mChannelMasks.add(channelMask);
34185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
34195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        str = strtok(NULL, "|");
34205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
34215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return;
34225ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
34235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
34245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadOutChannels(char *name, IOProfile *profile)
34255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
34265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    const char *str = strtok(name, "|");
34273cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
34283cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGV("loadOutChannels() %s", name);
34293cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
34303cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    // by convention, "0' in the first entry in mChannelMasks indicates the supported channel
34313cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    // masks should be read from the output stream after it is opened for the first time
34323cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) {
34333cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        profile->mChannelMasks.add((audio_channel_mask_t)0);
34343cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        return;
34353cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    }
34365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
34375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (str != NULL) {
34383cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        audio_channel_mask_t channelMask =
34393cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                (audio_channel_mask_t)stringToEnum(sOutChannelsNameToEnumTable,
34403cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                   ARRAY_SIZE(sOutChannelsNameToEnumTable),
34413cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                   str);
34425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (channelMask != 0) {
34435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mChannelMasks.add(channelMask);
34445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
34455ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        str = strtok(NULL, "|");
34465ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
34475ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return;
34485ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
34495ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
345070c236c9290732782d5267935af1475b8d5ae602Eric Laurentstatus_t AudioPolicyManagerBase::loadInput(cnode *root, HwModule *module)
34515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
34525ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    cnode *node = root->first_child;
34535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
34545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    IOProfile *profile = new IOProfile(module);
34555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
34565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (node) {
34575ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
34585ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            loadSamplingRates((char *)node->value, profile);
34595ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, FORMATS_TAG) == 0) {
34605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            loadFormats((char *)node->value, profile);
34615ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, CHANNELS_TAG) == 0) {
34625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            loadInChannels((char *)node->value, profile);
34635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, DEVICES_TAG) == 0) {
34645ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mSupportedDevices = parseDeviceNames((char *)node->value);
34655ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
34665ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        node = node->next;
34675ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
3468c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    ALOGW_IF(profile->mSupportedDevices == APM_AUDIO_DEVICE_NONE,
34695ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadInput() invalid supported devices");
34705ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGW_IF(profile->mChannelMasks.size() == 0,
34715ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadInput() invalid supported channel masks");
34725ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGW_IF(profile->mSamplingRates.size() == 0,
34735ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadInput() invalid supported sampling rates");
34745ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGW_IF(profile->mFormats.size() == 0,
34755ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadInput() invalid supported formats");
3476c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    if ((profile->mSupportedDevices != APM_AUDIO_DEVICE_NONE) &&
34775ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            (profile->mChannelMasks.size() != 0) &&
34785ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            (profile->mSamplingRates.size() != 0) &&
34795ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            (profile->mFormats.size() != 0)) {
34805ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
34815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        ALOGV("loadInput() adding input mSupportedDevices %04x", profile->mSupportedDevices);
34825ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
348370c236c9290732782d5267935af1475b8d5ae602Eric Laurent        module->mInputProfiles.add(profile);
34845ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return NO_ERROR;
34855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    } else {
34865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        delete profile;
34875ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return BAD_VALUE;
34885ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
34895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
34905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
349170c236c9290732782d5267935af1475b8d5ae602Eric Laurentstatus_t AudioPolicyManagerBase::loadOutput(cnode *root, HwModule *module)
34925ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
34935ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    cnode *node = root->first_child;
34945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
34955ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    IOProfile *profile = new IOProfile(module);
34965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
34975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (node) {
34985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
34995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            loadSamplingRates((char *)node->value, profile);
35005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, FORMATS_TAG) == 0) {
35015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            loadFormats((char *)node->value, profile);
35025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, CHANNELS_TAG) == 0) {
35035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            loadOutChannels((char *)node->value, profile);
35045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, DEVICES_TAG) == 0) {
35055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mSupportedDevices = parseDeviceNames((char *)node->value);
35065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, FLAGS_TAG) == 0) {
35075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mFlags = parseFlagNames((char *)node->value);
35085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
35095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        node = node->next;
35105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
3511c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    ALOGW_IF(profile->mSupportedDevices == APM_AUDIO_DEVICE_NONE,
35125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadOutput() invalid supported devices");
35135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGW_IF(profile->mChannelMasks.size() == 0,
35145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadOutput() invalid supported channel masks");
35155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGW_IF(profile->mSamplingRates.size() == 0,
35165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadOutput() invalid supported sampling rates");
35175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGW_IF(profile->mFormats.size() == 0,
35185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadOutput() invalid supported formats");
3519c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    if ((profile->mSupportedDevices != APM_AUDIO_DEVICE_NONE) &&
35205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            (profile->mChannelMasks.size() != 0) &&
35215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            (profile->mSamplingRates.size() != 0) &&
35225ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            (profile->mFormats.size() != 0)) {
35235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
35245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        ALOGV("loadOutput() adding output mSupportedDevices %04x, mFlags %04x",
35255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent              profile->mSupportedDevices, profile->mFlags);
35265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
352770c236c9290732782d5267935af1475b8d5ae602Eric Laurent        module->mOutputProfiles.add(profile);
35285ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return NO_ERROR;
35295ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    } else {
35305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        delete profile;
35315ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return BAD_VALUE;
35325ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
35335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
35345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
35355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadHwModule(cnode *root)
35365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
35375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    cnode *node = config_find(root, OUTPUTS_TAG);
35385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    status_t status = NAME_NOT_FOUND;
353970c236c9290732782d5267935af1475b8d5ae602Eric Laurent
354070c236c9290732782d5267935af1475b8d5ae602Eric Laurent    HwModule *module = new HwModule(root->name);
354170c236c9290732782d5267935af1475b8d5ae602Eric Laurent
35425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (node != NULL) {
35435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strcmp(root->name, AUDIO_HARDWARE_MODULE_ID_A2DP) == 0) {
35445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            mHasA2dp = true;
3545599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent        } else if (strcmp(root->name, AUDIO_HARDWARE_MODULE_ID_USB) == 0) {
3546599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            mHasUsb = true;
35475ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
3548599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent
35495ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        node = node->first_child;
35505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        while (node) {
35515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadHwModule() loading output %s", node->name);
355270c236c9290732782d5267935af1475b8d5ae602Eric Laurent            status_t tmpStatus = loadOutput(node, module);
35535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            if (status == NAME_NOT_FOUND || status == NO_ERROR) {
35545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                status = tmpStatus;
35555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
35565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            node = node->next;
35575ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
35585ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
35595ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    node = config_find(root, INPUTS_TAG);
35605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (node != NULL) {
35615ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        node = node->first_child;
35625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        while (node) {
35635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadHwModule() loading input %s", node->name);
356470c236c9290732782d5267935af1475b8d5ae602Eric Laurent            status_t tmpStatus = loadInput(node, module);
35655ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            if (status == NAME_NOT_FOUND || status == NO_ERROR) {
35665ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                status = tmpStatus;
35675ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
35685ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            node = node->next;
35695ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
35705ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
35715ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (status == NO_ERROR) {
357270c236c9290732782d5267935af1475b8d5ae602Eric Laurent        mHwModules.add(module);
357370c236c9290732782d5267935af1475b8d5ae602Eric Laurent    } else {
357470c236c9290732782d5267935af1475b8d5ae602Eric Laurent        delete module;
35755ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
35765ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
35775ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
35785ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadHwModules(cnode *root)
35795ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
35805ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    cnode *node = config_find(root, AUDIO_HW_MODULE_TAG);
35815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (node == NULL) {
35825ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return;
35835ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
35845ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
35855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    node = node->first_child;
35865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (node) {
35875ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        ALOGV("loadHwModules() loading module %s", node->name);
35885ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        loadHwModule(node);
35895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        node = node->next;
35905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
35915ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
35925ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
35935ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadGlobalConfig(cnode *root)
35945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
35955ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    cnode *node = config_find(root, GLOBAL_CONFIG_TAG);
35965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (node == NULL) {
35975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return;
35985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
35995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    node = node->first_child;
36005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (node) {
36015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strcmp(ATTACHED_OUTPUT_DEVICES_TAG, node->name) == 0) {
36025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            mAttachedOutputDevices = parseDeviceNames((char *)node->value);
3603c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            ALOGW_IF(mAttachedOutputDevices == APM_AUDIO_DEVICE_NONE,
3604c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                    "loadGlobalConfig() no attached output devices");
36055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadGlobalConfig() mAttachedOutputDevices %04x", mAttachedOutputDevices);
36065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(DEFAULT_OUTPUT_DEVICE_TAG, node->name) == 0) {
36075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            mDefaultOutputDevice = (audio_devices_t)stringToEnum(sDeviceNameToEnumTable,
36085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                              ARRAY_SIZE(sDeviceNameToEnumTable),
36095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                              (char *)node->value);
3610c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            ALOGW_IF(mDefaultOutputDevice == APM_AUDIO_DEVICE_NONE,
3611c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                    "loadGlobalConfig() default device not specified");
36125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadGlobalConfig() mDefaultOutputDevice %04x", mDefaultOutputDevice);
36135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(ATTACHED_INPUT_DEVICES_TAG, node->name) == 0) {
36145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            mAvailableInputDevices = parseDeviceNames((char *)node->value);
36155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadGlobalConfig() mAvailableInputDevices %04x", mAvailableInputDevices);
36165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
36175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        node = node->next;
36185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
36195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
36205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
36215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentstatus_t AudioPolicyManagerBase::loadAudioPolicyConfig(const char *path)
36225ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
36235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    cnode *root;
36245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    char *data;
36255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
36265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    data = (char *)load_file(path, NULL);
36275ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (data == NULL) {
36285ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return -ENODEV;
36295ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
36305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    root = config_node("", "");
36315ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    config_load(root, data);
36325ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
36335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    loadGlobalConfig(root);
36345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    loadHwModules(root);
36355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
36365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    config_free(root);
36375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    free(root);
36385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    free(data);
36395ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
36405ec145df7708564d385fd3fb764085321cf4c253Dima Zavin    ALOGI("loadAudioPolicyConfig() loaded %s\n", path);
36415ec145df7708564d385fd3fb764085321cf4c253Dima Zavin
36425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return NO_ERROR;
36435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
3644f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3645739022f26a7127ba76a98dda65411496086114a7Dima Zavinvoid AudioPolicyManagerBase::defaultAudioPolicyConfig(void)
3646739022f26a7127ba76a98dda65411496086114a7Dima Zavin{
3647739022f26a7127ba76a98dda65411496086114a7Dima Zavin    HwModule *module;
3648739022f26a7127ba76a98dda65411496086114a7Dima Zavin    IOProfile *profile;
3649739022f26a7127ba76a98dda65411496086114a7Dima Zavin
3650739022f26a7127ba76a98dda65411496086114a7Dima Zavin    mDefaultOutputDevice = AUDIO_DEVICE_OUT_SPEAKER;
3651739022f26a7127ba76a98dda65411496086114a7Dima Zavin    mAttachedOutputDevices = AUDIO_DEVICE_OUT_SPEAKER;
3652739022f26a7127ba76a98dda65411496086114a7Dima Zavin    mAvailableInputDevices = AUDIO_DEVICE_IN_BUILTIN_MIC;
3653739022f26a7127ba76a98dda65411496086114a7Dima Zavin
3654739022f26a7127ba76a98dda65411496086114a7Dima Zavin    module = new HwModule("primary");
3655739022f26a7127ba76a98dda65411496086114a7Dima Zavin
3656739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile = new IOProfile(module);
3657739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mSamplingRates.add(44100);
3658739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
3659739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mChannelMasks.add(AUDIO_CHANNEL_OUT_STEREO);
3660739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mSupportedDevices = AUDIO_DEVICE_OUT_SPEAKER;
3661739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mFlags = AUDIO_OUTPUT_FLAG_PRIMARY;
3662739022f26a7127ba76a98dda65411496086114a7Dima Zavin    module->mOutputProfiles.add(profile);
3663739022f26a7127ba76a98dda65411496086114a7Dima Zavin
3664739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile = new IOProfile(module);
3665739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mSamplingRates.add(8000);
3666739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
3667739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mChannelMasks.add(AUDIO_CHANNEL_IN_MONO);
3668739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mSupportedDevices = AUDIO_DEVICE_IN_BUILTIN_MIC;
3669739022f26a7127ba76a98dda65411496086114a7Dima Zavin    module->mInputProfiles.add(profile);
3670739022f26a7127ba76a98dda65411496086114a7Dima Zavin
3671739022f26a7127ba76a98dda65411496086114a7Dima Zavin    mHwModules.add(module);
3672739022f26a7127ba76a98dda65411496086114a7Dima Zavin}
3673f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3674f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}; // namespace android
3675