AudioPolicyManagerBase.cpp revision fa3697d716b444bbea6be480801536c44bf69214
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
34e81531e91ecae92aff471dbff9cbeb0f95ff4a80Dima Zavinnamespace android_audio_legacy {
35f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
36f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// ----------------------------------------------------------------------------
37f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// AudioPolicyInterface implementation
38f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// ----------------------------------------------------------------------------
39f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
40f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
41f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::setDeviceConnectionState(AudioSystem::audio_devices device,
42f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                  AudioSystem::device_connection_state state,
43f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                  const char *device_address)
44f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
453cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    SortedVector <audio_io_handle_t> outputs;
46f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
476a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("setDeviceConnectionState() device: %x, state %d, address %s", device, state, device_address);
48f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
49f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // connect/disconnect only 1 device at a time
50f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (AudioSystem::popCount(device) != 1) return BAD_VALUE;
51f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
52f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (strlen(device_address) >= MAX_DEVICE_ADDRESS_LEN) {
535efbd421e0029d6fc44b1cc65c0e5e0d85e5161fSteve Block        ALOGE("setDeviceConnectionState() invalid address: %s", device_address);
54f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
55f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
56f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
57f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // handle output devices
58f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (AudioSystem::isOutputDevice(device)) {
59f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (!mHasA2dp && AudioSystem::isA2dpDevice(device)) {
615efbd421e0029d6fc44b1cc65c0e5e0d85e5161fSteve Block            ALOGE("setDeviceConnectionState() invalid device: %x", device);
62f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return BAD_VALUE;
63f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
64599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent        if (!mHasUsb && audio_is_usb_device((audio_devices_t)device)) {
65599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            ALOGE("setDeviceConnectionState() invalid device: %x", device);
66599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            return BAD_VALUE;
67599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent        }
68f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
69f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        switch (state)
70f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        {
71f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // handle output device connection
72f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        case AudioSystem::DEVICE_STATE_AVAILABLE:
73f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (mAvailableOutputDevices & device) {
7464cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block                ALOGW("setDeviceConnectionState() device already connected: %x", device);
75f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                return INVALID_OPERATION;
76f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
776a70518b93928d1c91457ff805e375c82d76b0e5Steve Block            ALOGV("setDeviceConnectionState() connecting device %x", device);
78f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
793cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            if (checkOutputsForDevice((audio_devices_t)device, state, outputs) != NO_ERROR) {
803cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                return INVALID_OPERATION;
813cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
823cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %d outputs",
833cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                  outputs.size());
84f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // register new device as available
85f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent            mAvailableOutputDevices = (audio_devices_t)(mAvailableOutputDevices | device);
86f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
873cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            if (!outputs.isEmpty()) {
883cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                String8 paramStr;
893cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                if (mHasA2dp && AudioSystem::isA2dpDevice(device)) {
903cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    // handle A2DP device connection
913cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    AudioParameter param;
923cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    param.add(String8(AUDIO_PARAMETER_A2DP_SINK_ADDRESS), String8(device_address));
933cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    paramStr = param.toString();
943cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    mA2dpDeviceAddress = String8(device_address, MAX_DEVICE_ADDRESS_LEN);
953cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    mA2dpSuspended = false;
963cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                } else if (AudioSystem::isBluetoothScoDevice(device)) {
973cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    // handle SCO device connection
983cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    mScoDeviceAddress = String8(device_address, MAX_DEVICE_ADDRESS_LEN);
993cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                } else if (mHasUsb && audio_is_usb_device((audio_devices_t)device)) {
1003cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    // handle USB device connection
1013cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    mUsbCardAndDevice = String8(device_address, MAX_DEVICE_ADDRESS_LEN);
1023cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    paramStr = mUsbCardAndDevice;
1033cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                }
1043cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                if (!paramStr.isEmpty()) {
1053cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    for (size_t i = 0; i < outputs.size(); i++) {
1063cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        mpClientInterface->setParameters(outputs[i], paramStr);
1073cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
1083cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                }
109f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
110f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            break;
111f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // handle output device disconnection
112f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        case AudioSystem::DEVICE_STATE_UNAVAILABLE: {
113f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (!(mAvailableOutputDevices & device)) {
11464cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block                ALOGW("setDeviceConnectionState() device not connected: %x", device);
115f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                return INVALID_OPERATION;
116f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
117f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1186a70518b93928d1c91457ff805e375c82d76b0e5Steve Block            ALOGV("setDeviceConnectionState() disconnecting device %x", device);
119f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // remove device from available output devices
120f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent            mAvailableOutputDevices = (audio_devices_t)(mAvailableOutputDevices & ~device);
121f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1223cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            checkOutputsForDevice((audio_devices_t)device, state, outputs);
1235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            if (mHasA2dp && AudioSystem::isA2dpDevice(device)) {
1243cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                // handle A2DP device disconnection
125b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                mA2dpDeviceAddress = "";
126b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                mA2dpSuspended = false;
127b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            } else if (AudioSystem::isBluetoothScoDevice(device)) {
1283cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                // handle SCO device disconnection
129b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                mScoDeviceAddress = "";
130599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            } else if (mHasUsb && audio_is_usb_device((audio_devices_t)device)) {
1313cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                // handle USB device disconnection
132599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent                mUsbCardAndDevice = "";
133f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
134f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            } break;
135f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
136f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        default:
1375efbd421e0029d6fc44b1cc65c0e5e0d85e5161fSteve Block            ALOGE("setDeviceConnectionState() invalid state: %x", state);
138f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return BAD_VALUE;
139f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
140f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
141f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        checkA2dpSuspend();
142f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        checkOutputForAllStrategies();
143b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        // outputs must be closed after checkOutputForAllStrategies() is executed
1443cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        if (!outputs.isEmpty()) {
1453cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            for (size_t i = 0; i < outputs.size(); i++) {
1463cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                // close unused outputs after device disconnection or direct outputs that have been
1473cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                // opened by checkOutputsForDevice() to query dynamic parameters
1483cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                if ((state == AudioSystem::DEVICE_STATE_UNAVAILABLE) ||
1493cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        (mOutputs.valueFor(outputs[i])->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
1503cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    closeOutput(outputs[i]);
1513cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                }
1523cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
153f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
155f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        updateDeviceForStrategy();
1565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        for (size_t i = 0; i < mOutputs.size(); i++) {
1575ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            setOutputDevice(mOutputs.keyAt(i), getNewDevice(mOutputs.keyAt(i), true /*fromCache*/));
1585ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
159f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
160f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (device == AudioSystem::DEVICE_OUT_WIRED_HEADSET) {
161f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            device = AudioSystem::DEVICE_IN_WIRED_HEADSET;
162f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        } else if (device == AudioSystem::DEVICE_OUT_BLUETOOTH_SCO ||
163f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                   device == AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_HEADSET ||
164f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                   device == AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_CARKIT) {
165f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            device = AudioSystem::DEVICE_IN_BLUETOOTH_SCO_HEADSET;
166f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        } else {
167f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return NO_ERROR;
168f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
169f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
170f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // handle input devices
171f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (AudioSystem::isInputDevice(device)) {
172f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
173f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        switch (state)
174f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        {
175f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // handle input device connection
176f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        case AudioSystem::DEVICE_STATE_AVAILABLE: {
177f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (mAvailableInputDevices & device) {
17864cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block                ALOGW("setDeviceConnectionState() device already connected: %d", device);
179f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                return INVALID_OPERATION;
180f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            mAvailableInputDevices = (audio_devices_t)(mAvailableInputDevices | device);
182f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
183f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            break;
184f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
185f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // handle input device disconnection
186f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        case AudioSystem::DEVICE_STATE_UNAVAILABLE: {
187f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (!(mAvailableInputDevices & device)) {
18864cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block                ALOGW("setDeviceConnectionState() device not connected: %d", device);
189f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                return INVALID_OPERATION;
190f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1915ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            mAvailableInputDevices = (audio_devices_t) (mAvailableInputDevices & ~device);
192f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            } break;
193f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
194f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        default:
1955efbd421e0029d6fc44b1cc65c0e5e0d85e5161fSteve Block            ALOGE("setDeviceConnectionState() invalid state: %x", state);
196f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return BAD_VALUE;
197f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
198f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
199f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        audio_io_handle_t activeInput = getActiveInput();
200f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (activeInput != 0) {
201f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            AudioInputDescriptor *inputDesc = mInputs.valueFor(activeInput);
202f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent            audio_devices_t newDevice = getDeviceForInputSource(inputDesc->mInputSource);
2035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            if ((newDevice != 0) && (newDevice != inputDesc->mDevice)) {
2046a70518b93928d1c91457ff805e375c82d76b0e5Steve Block                ALOGV("setDeviceConnectionState() changing device from %x to %x for input %d",
205f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        inputDesc->mDevice, newDevice, activeInput);
206f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                inputDesc->mDevice = newDevice;
207f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                AudioParameter param = AudioParameter();
208f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.addInt(String8(AudioParameter::keyRouting), (int)newDevice);
209f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mpClientInterface->setParameters(activeInput, param.toString());
210f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
211f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
212f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
213f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return NO_ERROR;
214f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
215f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
21664cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block    ALOGW("setDeviceConnectionState() invalid device: %x", device);
217f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return BAD_VALUE;
218f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
219f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
220f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima ZavinAudioSystem::device_connection_state AudioPolicyManagerBase::getDeviceConnectionState(AudioSystem::audio_devices device,
221f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                  const char *device_address)
222f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
223f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioSystem::device_connection_state state = AudioSystem::DEVICE_STATE_UNAVAILABLE;
224f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    String8 address = String8(device_address);
225f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (AudioSystem::isOutputDevice(device)) {
226f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (device & mAvailableOutputDevices) {
227f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (AudioSystem::isA2dpDevice(device) &&
2285ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                (!mHasA2dp || (address != "" && mA2dpDeviceAddress != address))) {
229f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                return state;
230f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
231f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (AudioSystem::isBluetoothScoDevice(device) &&
232f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                address != "" && mScoDeviceAddress != address) {
233f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                return state;
234f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
235599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            if (audio_is_usb_device((audio_devices_t)device) &&
236599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent                (!mHasUsb || (address != "" && mUsbCardAndDevice != address))) {
237599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent                ALOGE("setDeviceConnectionState() invalid device: %x", device);
238599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent                return state;
239599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            }
240f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            state = AudioSystem::DEVICE_STATE_AVAILABLE;
241f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
242f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else if (AudioSystem::isInputDevice(device)) {
243f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (device & mAvailableInputDevices) {
244f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            state = AudioSystem::DEVICE_STATE_AVAILABLE;
245f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
246f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
247f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
248f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return state;
249f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
250f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
251f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::setPhoneState(int state)
252f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2536a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("setPhoneState() state %d", state);
254f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent    audio_devices_t newDevice = (audio_devices_t)0;
255f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (state < 0 || state >= AudioSystem::NUM_MODES) {
25664cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("setPhoneState() invalid state %d", state);
257f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
258f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
259f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
260f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (state == mPhoneState ) {
26164cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("setPhoneState() setting same state %d", state);
262f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
263f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
264f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
265f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if leaving call state, handle special case of active streams
266f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // pertaining to sonification strategy see handleIncallSonification()
267f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (isInCall()) {
2686a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("setPhoneState() in call state management: new state is %d", state);
269f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
270f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            handleIncallSonification(stream, false, true);
271f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
272f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
273f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
274f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // store previous phone state for management of sonification strategy below
275f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    int oldState = mPhoneState;
276f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mPhoneState = state;
277f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    bool force = false;
278f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
279f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // are we entering or starting a call
280f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (!isStateInCall(oldState) && isStateInCall(state)) {
2816a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("  Entering call in setPhoneState()");
282f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // force routing command to audio hardware when starting a call
283f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // even if no device change is needed
284f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        force = true;
285f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else if (isStateInCall(oldState) && !isStateInCall(state)) {
2866a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("  Exiting call in setPhoneState()");
287f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // force routing command to audio hardware when exiting a call
288f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // even if no device change is needed
289f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        force = true;
290f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else if (isStateInCall(state) && (state != oldState)) {
2916a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("  Switching between telephony and VoIP in setPhoneState()");
292f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // force routing command to audio hardware when switching between telephony and VoIP
293f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // even if no device change is needed
294f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        force = true;
295f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
296f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
297f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // check for device and output changes triggered by new phone state
2985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    newDevice = getNewDevice(mPrimaryOutput, false /*fromCache*/);
299f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkA2dpSuspend();
300f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForAllStrategies();
301f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    updateDeviceForStrategy();
302f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
303b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    AudioOutputDescriptor *hwOutputDesc = mOutputs.valueFor(mPrimaryOutput);
304f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
305f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // force routing command to audio hardware when ending call
306f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // even if no device change is needed
307f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (isStateInCall(oldState) && newDevice == 0) {
308f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        newDevice = hwOutputDesc->device();
309f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
310f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
311f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // when changing from ring tone to in call mode, mute the ringing tone
312f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // immediately and delay the route change to avoid sending the ring tone
313f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // tail into the earpiece or headset.
314f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    int delayMs = 0;
315f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (isStateInCall(state) && oldState == AudioSystem::MODE_RINGTONE) {
316f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // delay the device change command by twice the output latency to have some margin
317f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // and be sure that audio buffers not yet affected by the mute are out when
318f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // we actually apply the route change
319f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        delayMs = hwOutputDesc->mLatency*2;
320b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        setStreamMute(AudioSystem::RING, true, mPrimaryOutput);
321f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
322f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
323f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // change routing is necessary
324b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    setOutputDevice(mPrimaryOutput, newDevice, force, delayMs);
325f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
326f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if entering in call state, handle special case of active streams
327f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // pertaining to sonification strategy see handleIncallSonification()
328f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (isStateInCall(state)) {
3296a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("setPhoneState() in call state management: new state is %d", state);
330f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // unmute the ringing tone after a sufficient delay if it was muted before
331f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // setting output device above
332f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (oldState == AudioSystem::MODE_RINGTONE) {
333b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            setStreamMute(AudioSystem::RING, false, mPrimaryOutput, MUTE_TIME_MS);
334f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
335f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
336f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            handleIncallSonification(stream, true, true);
337f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
338f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
339f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
340f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
341f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (state == AudioSystem::MODE_RINGTONE &&
342f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        isStreamActive(AudioSystem::MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
343f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mLimitRingtoneVolume = true;
344f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
345f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mLimitRingtoneVolume = false;
346f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
347f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
348f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
349f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::setForceUse(AudioSystem::force_use usage, AudioSystem::forced_config config)
350f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3516a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mPhoneState);
352f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
353f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    bool forceVolumeReeval = false;
354f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    switch(usage) {
355f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::FOR_COMMUNICATION:
356f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (config != AudioSystem::FORCE_SPEAKER && config != AudioSystem::FORCE_BT_SCO &&
357f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_NONE) {
35864cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("setForceUse() invalid config %d for FOR_COMMUNICATION", config);
359f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return;
360f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
361f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        forceVolumeReeval = true;
362f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mForceUse[usage] = config;
363f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
364f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::FOR_MEDIA:
365f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (config != AudioSystem::FORCE_HEADPHONES && config != AudioSystem::FORCE_BT_A2DP &&
366f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_WIRED_ACCESSORY &&
367f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_ANALOG_DOCK &&
3681afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent            config != AudioSystem::FORCE_DIGITAL_DOCK && config != AudioSystem::FORCE_NONE &&
3691afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent            config != AudioSystem::FORCE_NO_BT_A2DP) {
37064cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("setForceUse() invalid config %d for FOR_MEDIA", config);
371f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return;
372f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
373f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mForceUse[usage] = config;
374f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
375f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::FOR_RECORD:
376f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (config != AudioSystem::FORCE_BT_SCO && config != AudioSystem::FORCE_WIRED_ACCESSORY &&
377f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_NONE) {
37864cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("setForceUse() invalid config %d for FOR_RECORD", config);
379f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return;
380f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
381f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mForceUse[usage] = config;
382f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
383f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::FOR_DOCK:
384f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (config != AudioSystem::FORCE_NONE && config != AudioSystem::FORCE_BT_CAR_DOCK &&
385f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_BT_DESK_DOCK &&
386f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_WIRED_ACCESSORY &&
387f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_ANALOG_DOCK &&
388f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_DIGITAL_DOCK) {
38964cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("setForceUse() invalid config %d for FOR_DOCK", config);
390f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
391f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        forceVolumeReeval = true;
392f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mForceUse[usage] = config;
393f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
394f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    default:
39564cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("setForceUse() invalid usage %d", usage);
396f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
397f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
398f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // check for device and output changes triggered by new force usage
400f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkA2dpSuspend();
401f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForAllStrategies();
402f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    updateDeviceForStrategy();
4035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < mOutputs.size(); i++) {
4045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_io_handle_t output = mOutputs.keyAt(i);
4055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_devices_t newDevice = getNewDevice(output, true /*fromCache*/);
4065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        setOutputDevice(output, newDevice, true);
4075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (forceVolumeReeval) {
4085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            applyStreamVolumes(output, newDevice, 0, true);
4095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
410f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
411f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
412f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    audio_io_handle_t activeInput = getActiveInput();
413f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (activeInput != 0) {
414f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioInputDescriptor *inputDesc = mInputs.valueFor(activeInput);
4155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_devices_t newDevice = getDeviceForInputSource(inputDesc->mInputSource);
4165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if ((newDevice != 0) && (newDevice != inputDesc->mDevice)) {
4176a70518b93928d1c91457ff805e375c82d76b0e5Steve Block            ALOGV("setForceUse() changing device from %x to %x for input %d",
418f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    inputDesc->mDevice, newDevice, activeInput);
419f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            inputDesc->mDevice = newDevice;
420f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            AudioParameter param = AudioParameter();
421f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            param.addInt(String8(AudioParameter::keyRouting), (int)newDevice);
422f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mpClientInterface->setParameters(activeInput, param.toString());
423f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
424f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
425f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
426f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
427f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
428f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima ZavinAudioSystem::forced_config AudioPolicyManagerBase::getForceUse(AudioSystem::force_use usage)
429f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
430f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return mForceUse[usage];
431f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
432f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
433f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::setSystemProperty(const char* property, const char* value)
434f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
4356a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("setSystemProperty() property %s, value %s", property, value);
436f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (strcmp(property, "ro.camera.sound.forced") == 0) {
437f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (atoi(value)) {
4386a70518b93928d1c91457ff805e375c82d76b0e5Steve Block            ALOGV("ENFORCED_AUDIBLE cannot be muted");
439f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mStreams[AudioSystem::ENFORCED_AUDIBLE].mCanBeMuted = false;
440f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        } else {
4416a70518b93928d1c91457ff805e375c82d76b0e5Steve Block            ALOGV("ENFORCED_AUDIBLE can be muted");
442f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mStreams[AudioSystem::ENFORCED_AUDIBLE].mCanBeMuted = true;
443f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
444f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
445f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
446f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
4473cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric LaurentAudioPolicyManagerBase::IOProfile *AudioPolicyManagerBase::getProfileForDirectOutput(
4483cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                               audio_devices_t device,
44970c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                               uint32_t samplingRate,
45070c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                               uint32_t format,
45170c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                               uint32_t channelMask,
4520977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent                                                               audio_output_flags_t flags)
45370c236c9290732782d5267935af1475b8d5ae602Eric Laurent{
45470c236c9290732782d5267935af1475b8d5ae602Eric Laurent    for (size_t i = 0; i < mHwModules.size(); i++) {
45570c236c9290732782d5267935af1475b8d5ae602Eric Laurent        if (mHwModules[i]->mHandle == 0) {
45670c236c9290732782d5267935af1475b8d5ae602Eric Laurent            continue;
45770c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
45870c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) {
4593cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent           IOProfile *profile = mHwModules[i]->mOutputProfiles[j];
4603cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent           if (profile->isCompatibleProfile(device, samplingRate, format,
4613cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                           channelMask,
4623cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                           AUDIO_OUTPUT_FLAG_DIRECT)) {
4633cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent               if (mAvailableOutputDevices & profile->mSupportedDevices) {
4643cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                   return mHwModules[i]->mOutputProfiles[j];
4653cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent               }
46670c236c9290732782d5267935af1475b8d5ae602Eric Laurent           }
46770c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
46870c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
46970c236c9290732782d5267935af1475b8d5ae602Eric Laurent    return 0;
47070c236c9290732782d5267935af1475b8d5ae602Eric Laurent}
47170c236c9290732782d5267935af1475b8d5ae602Eric Laurent
472f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinaudio_io_handle_t AudioPolicyManagerBase::getOutput(AudioSystem::stream_type stream,
473f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    uint32_t samplingRate,
474f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    uint32_t format,
47570c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                    uint32_t channelMask,
476f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    AudioSystem::output_flags flags)
477f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
478f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    audio_io_handle_t output = 0;
479f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    uint32_t latency = 0;
480f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    routing_strategy strategy = getStrategy((AudioSystem::stream_type)stream);
4815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
48270c236c9290732782d5267935af1475b8d5ae602Eric Laurent    ALOGV("getOutput() stream %d, samplingRate %d, format %d, channelMask %x, flags %x",
48370c236c9290732782d5267935af1475b8d5ae602Eric Laurent          stream, samplingRate, format, channelMask, flags);
484f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
485f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
486f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mCurOutput != 0) {
48770c236c9290732782d5267935af1475b8d5ae602Eric Laurent        ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
488f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
489f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
490f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (mTestOutputs[mCurOutput] == 0) {
4916a70518b93928d1c91457ff805e375c82d76b0e5Steve Block            ALOGV("getOutput() opening test output");
4923cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(NULL);
493f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            outputDesc->mDevice = mTestDevice;
494f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            outputDesc->mSamplingRate = mTestSamplingRate;
495f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            outputDesc->mFormat = mTestFormat;
49670c236c9290732782d5267935af1475b8d5ae602Eric Laurent            outputDesc->mChannelMask = mTestChannels;
497f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            outputDesc->mLatency = mTestLatencyMs;
4980977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent            outputDesc->mFlags = (audio_output_flags_t)(mDirectOutput ? AudioSystem::OUTPUT_FLAG_DIRECT : 0);
499f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            outputDesc->mRefCount[stream] = 0;
50070c236c9290732782d5267935af1475b8d5ae602Eric Laurent            mTestOutputs[mCurOutput] = mpClientInterface->openOutput(0, &outputDesc->mDevice,
501f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            &outputDesc->mSamplingRate,
502f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            &outputDesc->mFormat,
50370c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                            &outputDesc->mChannelMask,
504f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            &outputDesc->mLatency,
505f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            outputDesc->mFlags);
506f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (mTestOutputs[mCurOutput]) {
507f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                AudioParameter outputCmd = AudioParameter();
508f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                outputCmd.addInt(String8("set_id"),mCurOutput);
509f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
510f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                addOutput(mTestOutputs[mCurOutput], outputDesc);
511f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
512f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
513f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return mTestOutputs[mCurOutput];
514f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
515f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
516f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
517f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // open a direct output if required by specified parameters
5183cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    IOProfile *profile = getProfileForDirectOutput(device,
5193cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                   samplingRate,
5203cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                   format,
5213cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                   channelMask,
5223cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                   (audio_output_flags_t)flags);
5233cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    if (profile != NULL) {
524f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
5256a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("getOutput() opening direct output device %x", device);
52670c236c9290732782d5267935af1475b8d5ae602Eric Laurent
5273cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(profile);
528f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mDevice = device;
529f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mSamplingRate = samplingRate;
53070c236c9290732782d5267935af1475b8d5ae602Eric Laurent        outputDesc->mFormat = (audio_format_t)format;
53170c236c9290732782d5267935af1475b8d5ae602Eric Laurent        outputDesc->mChannelMask = (audio_channel_mask_t)channelMask;
532f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mLatency = 0;
5333cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        outputDesc->mFlags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);;
534f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mRefCount[stream] = 0;
535f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mStopTime[stream] = 0;
5363cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        output = mpClientInterface->openOutput(profile->mModule->mHandle,
53770c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                        &outputDesc->mDevice,
538f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                        &outputDesc->mSamplingRate,
539f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                        &outputDesc->mFormat,
54070c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                        &outputDesc->mChannelMask,
541f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                        &outputDesc->mLatency,
542f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                        outputDesc->mFlags);
543f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
5449f1f9b509c930830f6f32e9ef6c2c8a03d6fa96eJean-Michel Trivi        // only accept an output with the requested parameters
545f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (output == 0 ||
546f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            (samplingRate != 0 && samplingRate != outputDesc->mSamplingRate) ||
547f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            (format != 0 && format != outputDesc->mFormat) ||
54870c236c9290732782d5267935af1475b8d5ae602Eric Laurent            (channelMask != 0 && channelMask != outputDesc->mChannelMask)) {
5493cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
5503cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    "format %d %d, channelMask %04x %04x", output, samplingRate,
5513cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
5523cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    outputDesc->mChannelMask);
553f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (output != 0) {
554f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mpClientInterface->closeOutput(output);
555f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
556f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            delete outputDesc;
557f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return 0;
558f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
559f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        addOutput(output, outputDesc);
5603cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        ALOGV("getOutput() returns direct output %d", output);
561f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return output;
562f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
563f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
5649f1f9b509c930830f6f32e9ef6c2c8a03d6fa96eJean-Michel Trivi    // ignoring channel mask due to downmix capability in mixer
5659f1f9b509c930830f6f32e9ef6c2c8a03d6fa96eJean-Michel Trivi
566f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // open a non direct output
567f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
568f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // get which output is suitable for the specified stream. The actual routing change will happen
569f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // when startOutput() will be called
570b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device);
571f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
5725ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    output = selectOutput(outputs, flags);
573f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
57470c236c9290732782d5267935af1475b8d5ae602Eric Laurent    ALOGW_IF((output ==0), "getOutput() could not find output for stream %d, samplingRate %d,"
57570c236c9290732782d5267935af1475b8d5ae602Eric Laurent            "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
576f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
5775ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGV("getOutput() returns output %d", output);
5785ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
579f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return output;
580f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
581f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
5825ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentaudio_io_handle_t AudioPolicyManagerBase::selectOutput(const SortedVector<audio_io_handle_t>& outputs,
5835ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                       AudioSystem::output_flags flags)
5845ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
5855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // select one output among several that provide a path to a particular device or set of
5865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // devices (the list was previously build by getOutputsForDevice()).
5875ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // The priority is as follows:
5885ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // 1: the output with the highest number of requested policy flags
5895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // 2: the primary output
5905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // 3: the first output in the list
5915ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
5925ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (outputs.size() == 0) {
5935ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return 0;
5945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
5955ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (outputs.size() == 1) {
5965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return outputs[0];
5975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
5985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
5995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    int maxCommonFlags = 0;
6005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    audio_io_handle_t outputFlags = 0;
6015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    audio_io_handle_t outputPrimary = 0;
6025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
6035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < outputs.size(); i++) {
6045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        AudioOutputDescriptor *outputDesc = mOutputs.valueFor(outputs[i]);
6055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (!outputDesc->isDuplicated()) {
6065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            int commonFlags = (int)AudioSystem::popCount(outputDesc->mProfile->mFlags & flags);
6075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            if (commonFlags > maxCommonFlags) {
6085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                outputFlags = outputs[i];
6095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                maxCommonFlags = commonFlags;
6105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags);
6115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
6120977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent            if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
6135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                outputPrimary = outputs[i];
6145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
6155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
6165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
6175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
6185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (outputFlags != 0) {
6195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return outputFlags;
6205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
6215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (outputPrimary != 0) {
6225ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return outputPrimary;
6235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
6245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
6255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return outputs[0];
6265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
6275ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
628f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::startOutput(audio_io_handle_t output,
629f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                             AudioSystem::stream_type stream,
630f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                             int session)
631f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
6326a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("startOutput() output %d, stream %d, session %d", output, stream, session);
633f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mOutputs.indexOfKey(output);
634f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
63564cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("startOutput() unknow output %d", output);
636f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
637f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
638f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
639f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index);
640f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
6415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // increment usage count for this stream on the requested output:
642f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // NOTE that the usage count is the same for duplicated output and hardware output which is
6435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
644f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    outputDesc->changeRefCount(stream, 1);
645f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
6465ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (outputDesc->mRefCount[stream] == 1) {
6475ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_devices_t prevDevice = outputDesc->device();
6485ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_devices_t newDevice = getNewDevice(output, false /*fromCache*/);
649b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        routing_strategy strategy = getStrategy(stream);
650b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
651b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                            (strategy == STRATEGY_SONIFICATION_RESPECTFUL);
652b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        uint32_t waitMs = 0;
6535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        bool force = false;
6545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        for (size_t i = 0; i < mOutputs.size(); i++) {
6555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            AudioOutputDescriptor *desc = mOutputs.valueAt(i);
656b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent            if (desc != outputDesc) {
657b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                // force a device change if any other output is managed by the same hw
658b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                // module and has a current device selection that differs from selected device.
659b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                // In this case, the audio HAL must receive the new device selection so that it can
660b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                // change the device currently selected by the other active output.
661b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                if (outputDesc->sharesHwModuleWith(desc) &&
6625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                    desc->device() != newDevice) {
663b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                    force = true;
664b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                }
665b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                // wait for audio on other active outputs to be presented when starting
666b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                // a notification so that audio focus effect can propagate.
667b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                if (shouldWait && (desc->refCount() != 0) && (waitMs < desc->latency())) {
668b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                    waitMs = desc->latency();
669b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                }
6705ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
6715ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
672b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        uint32_t muteWaitMs = setOutputDevice(output, newDevice, force);
673f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
6745ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // handle special case for sonification while in call
6755ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (isInCall()) {
6765ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            handleIncallSonification(stream, true, false);
6775ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
678c16ac09f510437e8340be691720177a490ae78f0Eric Laurent
6795ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // apply volume rules for current stream and device if necessary
6805ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        checkAndSetVolume(stream,
6815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                          mStreams[stream].getVolumeIndex((audio_devices_t)newDevice),
6825ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                          output,
6835ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                          newDevice);
68412bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi
6855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // update the outputs if starting an output with a stream that can affect notification
6865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // routing
6875ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        handleNotificationRoutingForStream(stream);
688b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        if (waitMs > muteWaitMs) {
689b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent            usleep((waitMs - muteWaitMs) * 2 * 1000);
690b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        }
6915ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
692f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
693f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
694f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
6955ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
696f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::stopOutput(audio_io_handle_t output,
697f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            AudioSystem::stream_type stream,
698f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            int session)
699f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
7006a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
701f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mOutputs.indexOfKey(output);
702f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
70364cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("stopOutput() unknow output %d", output);
704f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
705f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
706f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
707f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index);
708f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
709f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // handle special case for sonification while in call
710f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (isInCall()) {
711f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        handleIncallSonification(stream, false, false);
712f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
713f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
714f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (outputDesc->mRefCount[stream] > 0) {
715f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // decrement usage count of this stream on the output
716f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->changeRefCount(stream, -1);
717f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // store time at which the stream was stopped - see isStreamActive()
7185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (outputDesc->mRefCount[stream] == 0) {
7195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            outputDesc->mStopTime[stream] = systemTime();
7205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            audio_devices_t newDevice = getNewDevice(output, false /*fromCache*/);
7215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // delay the device switch by twice the latency because stopOutput() is executed when
7225ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // the track stop() command is received and at that time the audio track buffer can
7235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // still contain data that needs to be drained. The latency only covers the audio HAL
7245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // and kernel buffers. Also the latency does not always include additional delay in the
7255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // audio path (audio DSP, CODEC ...)
7265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            setOutputDevice(output, newDevice, false, outputDesc->mLatency*2);
7275ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
7285ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // force restoring the device selection on other active outputs if it differs from the
7295ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // one being selected for this output
7305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            for (size_t i = 0; i < mOutputs.size(); i++) {
7315ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                audio_io_handle_t curOutput = mOutputs.keyAt(i);
7325ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                AudioOutputDescriptor *desc = mOutputs.valueAt(i);
7335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                if (curOutput != output &&
7345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                        desc->refCount() != 0 &&
7355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                        outputDesc->sharesHwModuleWith(desc) &&
7365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                        newDevice != desc->device()) {
7375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                    setOutputDevice(curOutput,
7385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                    getNewDevice(curOutput, false /*fromCache*/),
7395ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                    true,
7405ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                    outputDesc->mLatency*2);
7415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                }
7425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
7435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // update the outputs if stopping one with a stream that can affect notification routing
7445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            handleNotificationRoutingForStream(stream);
745f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
746f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return NO_ERROR;
747f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
74864cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("stopOutput() refcount is already 0 for output %d", output);
749f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return INVALID_OPERATION;
750f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
751f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
752f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
753f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::releaseOutput(audio_io_handle_t output)
754f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
7556a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("releaseOutput() %d", output);
756f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mOutputs.indexOfKey(output);
757f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
75864cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("releaseOutput() releasing unknown output %d", output);
759f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
760f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
761f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
762f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
763f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    int testIndex = testOutputIndex(output);
764f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (testIndex != 0) {
765f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index);
766f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (outputDesc->refCount() == 0) {
767f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mpClientInterface->closeOutput(output);
768f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            delete mOutputs.valueAt(index);
769f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mOutputs.removeItem(output);
770f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mTestOutputs[testIndex] = 0;
771f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
772f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
773f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
774f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
775f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
776f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mOutputs.valueAt(index)->mFlags & AudioSystem::OUTPUT_FLAG_DIRECT) {
777f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mpClientInterface->closeOutput(output);
778f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        delete mOutputs.valueAt(index);
779f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mOutputs.removeItem(output);
780f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
78112bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi
782f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
783f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
784f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinaudio_io_handle_t AudioPolicyManagerBase::getInput(int inputSource,
785f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    uint32_t samplingRate,
786f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    uint32_t format,
78770c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                    uint32_t channelMask,
788f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    AudioSystem::audio_in_acoustics acoustics)
789f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
790f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    audio_io_handle_t input = 0;
791f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent    audio_devices_t device = getDeviceForInputSource(inputSource);
792f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
79370c236c9290732782d5267935af1475b8d5ae602Eric Laurent    ALOGV("getInput() inputSource %d, samplingRate %d, format %d, channelMask %x, acoustics %x",
79470c236c9290732782d5267935af1475b8d5ae602Eric Laurent          inputSource, samplingRate, format, channelMask, acoustics);
795f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
796f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (device == 0) {
7975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        ALOGW("getInput() could not find device for inputSource %d", inputSource);
798f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return 0;
799f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
800f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
801f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // adapt channel selection to input source
802f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    switch(inputSource) {
803f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_UPLINK:
80470c236c9290732782d5267935af1475b8d5ae602Eric Laurent        channelMask = AudioSystem::CHANNEL_IN_VOICE_UPLINK;
805f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
806f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_DOWNLINK:
80770c236c9290732782d5267935af1475b8d5ae602Eric Laurent        channelMask = AudioSystem::CHANNEL_IN_VOICE_DNLINK;
808f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
809f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_CALL:
81070c236c9290732782d5267935af1475b8d5ae602Eric Laurent        channelMask = (AudioSystem::CHANNEL_IN_VOICE_UPLINK | AudioSystem::CHANNEL_IN_VOICE_DNLINK);
811f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
812f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    default:
813f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
814f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
815f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
8165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    IOProfile *profile = getInputProfile(device,
8175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                         samplingRate,
8185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                         format,
81970c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                         channelMask);
8205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (profile == NULL) {
8215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        ALOGW("getInput() could not find profile for device %04x, samplingRate %d, format %d,"
82270c236c9290732782d5267935af1475b8d5ae602Eric Laurent                "channelMask %04x",
82370c236c9290732782d5267935af1475b8d5ae602Eric Laurent                device, samplingRate, format, channelMask);
82470c236c9290732782d5267935af1475b8d5ae602Eric Laurent        return 0;
82570c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
82670c236c9290732782d5267935af1475b8d5ae602Eric Laurent
82770c236c9290732782d5267935af1475b8d5ae602Eric Laurent    if (profile->mModule->mHandle == 0) {
8283cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        ALOGE("getInput(): HW module %s not opened", profile->mModule->mName);
8295ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return 0;
8305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
8315ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
8325ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    AudioInputDescriptor *inputDesc = new AudioInputDescriptor(profile);
833f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
834f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    inputDesc->mInputSource = inputSource;
835f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    inputDesc->mDevice = device;
836f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    inputDesc->mSamplingRate = samplingRate;
83770c236c9290732782d5267935af1475b8d5ae602Eric Laurent    inputDesc->mFormat = (audio_format_t)format;
83870c236c9290732782d5267935af1475b8d5ae602Eric Laurent    inputDesc->mChannelMask = (audio_channel_mask_t)channelMask;
839f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    inputDesc->mRefCount = 0;
84070c236c9290732782d5267935af1475b8d5ae602Eric Laurent    input = mpClientInterface->openInput(profile->mModule->mHandle,
84170c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                    &inputDesc->mDevice,
842f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    &inputDesc->mSamplingRate,
843f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    &inputDesc->mFormat,
84470c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                    &inputDesc->mChannelMask);
845f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
846f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // only accept input with the exact requested set of parameters
847f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (input == 0 ||
848f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        (samplingRate != inputDesc->mSamplingRate) ||
849f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        (format != inputDesc->mFormat) ||
85070c236c9290732782d5267935af1475b8d5ae602Eric Laurent        (channelMask != inputDesc->mChannelMask)) {
85170c236c9290732782d5267935af1475b8d5ae602Eric Laurent        ALOGV("getInput() failed opening input: samplingRate %d, format %d, channelMask %d",
85270c236c9290732782d5267935af1475b8d5ae602Eric Laurent                samplingRate, format, channelMask);
853f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (input != 0) {
854f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mpClientInterface->closeInput(input);
855f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
856f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        delete inputDesc;
857f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return 0;
858f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
859f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mInputs.add(input, inputDesc);
860f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return input;
861f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
862f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
863f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::startInput(audio_io_handle_t input)
864f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
8656a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("startInput() input %d", input);
866f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mInputs.indexOfKey(input);
867f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
86864cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("startInput() unknow input %d", input);
869f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
870f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
871f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioInputDescriptor *inputDesc = mInputs.valueAt(index);
872f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
873f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
874f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mTestInput == 0)
875f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
876f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    {
877f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // refuse 2 active AudioRecord clients at the same time
878f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (getActiveInput() != 0) {
87964cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("startInput() input %d failed: other input already started", input);
880f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return INVALID_OPERATION;
881f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
882f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
883f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
884f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioParameter param = AudioParameter();
885f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    param.addInt(String8(AudioParameter::keyRouting), (int)inputDesc->mDevice);
886f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
887f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    param.addInt(String8(AudioParameter::keyInputSource), (int)inputDesc->mInputSource);
8886a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("AudioPolicyManager::startInput() input source = %d", inputDesc->mInputSource);
889f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
890f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mpClientInterface->setParameters(input, param.toString());
891f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
892f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    inputDesc->mRefCount = 1;
893f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
894f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
895f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
896f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::stopInput(audio_io_handle_t input)
897f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
8986a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("stopInput() input %d", input);
899f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mInputs.indexOfKey(input);
900f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
90164cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("stopInput() unknow input %d", input);
902f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
903f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
904f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioInputDescriptor *inputDesc = mInputs.valueAt(index);
905f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
906f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (inputDesc->mRefCount == 0) {
90764cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("stopInput() input %d already stopped", input);
908f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return INVALID_OPERATION;
909f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
910f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioParameter param = AudioParameter();
911f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        param.addInt(String8(AudioParameter::keyRouting), 0);
912f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mpClientInterface->setParameters(input, param.toString());
913f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        inputDesc->mRefCount = 0;
914f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return NO_ERROR;
915f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
916f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
917f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
918f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::releaseInput(audio_io_handle_t input)
919f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
9206a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("releaseInput() %d", input);
921f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mInputs.indexOfKey(input);
922f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
92364cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("releaseInput() releasing unknown input %d", input);
924f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
925f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
926f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mpClientInterface->closeInput(input);
927f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    delete mInputs.valueAt(index);
928f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mInputs.removeItem(input);
9296a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("releaseInput() exit");
930f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
931f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
932f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::initStreamVolume(AudioSystem::stream_type stream,
933f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            int indexMin,
934f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            int indexMax)
935f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
9366a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
937f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (indexMin < 0 || indexMin >= indexMax) {
93864cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("initStreamVolume() invalid index limits for stream %d, min %d, max %d", stream , indexMin, indexMax);
939f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
940f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
941f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mStreams[stream].mIndexMin = indexMin;
942f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mStreams[stream].mIndexMax = indexMax;
943f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
944f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
945c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentstatus_t AudioPolicyManagerBase::setStreamVolumeIndex(AudioSystem::stream_type stream,
946c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                      int index,
947c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                      audio_devices_t device)
948f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
949f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
950f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if ((index < mStreams[stream].mIndexMin) || (index > mStreams[stream].mIndexMax)) {
951f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
952f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
953c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    if (!audio_is_output_device(device)) {
954c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        return BAD_VALUE;
955c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    }
956f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
957f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // Force max volume if stream cannot be muted
958f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (!mStreams[stream].mCanBeMuted) index = mStreams[stream].mIndexMax;
959f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
960b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    ALOGV("setStreamVolumeIndex() stream %d, device %04x, index %d",
961c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent          stream, device, index);
962c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
963c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    // if device is AUDIO_DEVICE_OUT_DEFAULT set default value and
964c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    // clear all device specific values
965c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    if (device == AUDIO_DEVICE_OUT_DEFAULT) {
966c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        mStreams[stream].mIndexCur.clear();
967c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    }
968c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    mStreams[stream].mIndexCur.add(device, index);
969f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
970f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // compute and apply stream volume on all outputs according to connected device
971f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    status_t status = NO_ERROR;
972f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mOutputs.size(); i++) {
973c5eb8b4a5d4395ce335bc7c3e6df2678fa47e2ddEric Laurent        audio_devices_t curDevice =
974c5eb8b4a5d4395ce335bc7c3e6df2678fa47e2ddEric Laurent                getDeviceForVolume((audio_devices_t)mOutputs.valueAt(i)->device());
975c5eb8b4a5d4395ce335bc7c3e6df2678fa47e2ddEric Laurent        if (device == curDevice) {
976c5eb8b4a5d4395ce335bc7c3e6df2678fa47e2ddEric Laurent            status_t volStatus = checkAndSetVolume(stream, index, mOutputs.keyAt(i), curDevice);
977c5eb8b4a5d4395ce335bc7c3e6df2678fa47e2ddEric Laurent            if (volStatus != NO_ERROR) {
978c5eb8b4a5d4395ce335bc7c3e6df2678fa47e2ddEric Laurent                status = volStatus;
979c5eb8b4a5d4395ce335bc7c3e6df2678fa47e2ddEric Laurent            }
980f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
981f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
982f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return status;
983f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
984f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
985c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentstatus_t AudioPolicyManagerBase::getStreamVolumeIndex(AudioSystem::stream_type stream,
986c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                      int *index,
987c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                      audio_devices_t device)
988f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
989c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    if (index == NULL) {
990f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
991f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
992c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    if (!audio_is_output_device(device)) {
993c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        return BAD_VALUE;
994c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    }
995c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    // if device is AUDIO_DEVICE_OUT_DEFAULT, return volume for device corresponding to
996c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    // the strategy the stream belongs to.
997c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    if (device == AUDIO_DEVICE_OUT_DEFAULT) {
9985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        device = (audio_devices_t)getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
999c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    }
1000c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    device = getDeviceForVolume(device);
1001c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
1002c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    *index =  mStreams[stream].getVolumeIndex(device);
1003c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index);
1004f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
1005f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1006f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1007f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinaudio_io_handle_t AudioPolicyManagerBase::getOutputForEffect(effect_descriptor_t *desc)
1008f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
10096a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("getOutputForEffect()");
1010f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // apply simple rule where global effects are attached to the same output as MUSIC streams
10114660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen
10124660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen    routing_strategy strategy = getStrategy(AudioSystem::MUSIC);
10134660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen    audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
10144660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen    SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device);
10154660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen    int outIdx = 0;
10164660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen    for (size_t i = 0; i < dstOutputs.size(); i++) {
10174660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen        AudioOutputDescriptor *desc = mOutputs.valueFor(dstOutputs[i]);
10184660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen        if (desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
10194660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen            outIdx = i;
10204660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen        }
10214660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen    }
10224660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen    return dstOutputs[outIdx];
1023f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1024f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1025f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::registerEffect(effect_descriptor_t *desc,
10261c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent                                audio_io_handle_t io,
1027f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                uint32_t strategy,
1028f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                int session,
1029f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                int id)
1030f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
10311c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent    ssize_t index = mOutputs.indexOfKey(io);
1032f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
10331c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent        index = mInputs.indexOfKey(io);
10341c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent        if (index < 0) {
103564cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("registerEffect() unknown io %d", io);
10361c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent            return INVALID_OPERATION;
10371c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent        }
1038f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1039f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1040f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mTotalEffectsMemory + desc->memoryUsage > getMaxEffectsMemory()) {
104164cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("registerEffect() memory limit exceeded for Fx %s, Memory %d KB",
1042f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                desc->name, desc->memoryUsage);
1043f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return INVALID_OPERATION;
1044f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1045f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mTotalEffectsMemory += desc->memoryUsage;
10466a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("registerEffect() effect %s, io %d, strategy %d session %d id %d",
10471c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent            desc->name, io, strategy, session, id);
10486a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("registerEffect() memory %d, total memory %d", desc->memoryUsage, mTotalEffectsMemory);
1049f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1050f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    EffectDescriptor *pDesc = new EffectDescriptor();
1051f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    memcpy (&pDesc->mDesc, desc, sizeof(effect_descriptor_t));
10521c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent    pDesc->mIo = io;
1053f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    pDesc->mStrategy = (routing_strategy)strategy;
1054f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    pDesc->mSession = session;
1055582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    pDesc->mEnabled = false;
1056582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent
1057f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mEffects.add(id, pDesc);
1058f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1059f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
1060f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1061f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1062f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::unregisterEffect(int id)
1063f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1064f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mEffects.indexOfKey(id);
1065f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
106664cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("unregisterEffect() unknown effect ID %d", id);
1067f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return INVALID_OPERATION;
1068f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1069f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1070f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    EffectDescriptor *pDesc = mEffects.valueAt(index);
1071f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1072582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    setEffectEnabled(pDesc, false);
1073582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent
1074f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mTotalEffectsMemory < pDesc->mDesc.memoryUsage) {
107564cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("unregisterEffect() memory %d too big for total %d",
1076f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                pDesc->mDesc.memoryUsage, mTotalEffectsMemory);
1077f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        pDesc->mDesc.memoryUsage = mTotalEffectsMemory;
1078f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1079f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mTotalEffectsMemory -= pDesc->mDesc.memoryUsage;
10806a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("unregisterEffect() effect %s, ID %d, memory %d total memory %d",
1081582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent            pDesc->mDesc.name, id, pDesc->mDesc.memoryUsage, mTotalEffectsMemory);
1082f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1083f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mEffects.removeItem(id);
1084f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    delete pDesc;
1085f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1086f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
1087f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1088f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1089582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurentstatus_t AudioPolicyManagerBase::setEffectEnabled(int id, bool enabled)
1090582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent{
1091582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    ssize_t index = mEffects.indexOfKey(id);
1092582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    if (index < 0) {
109364cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("unregisterEffect() unknown effect ID %d", id);
1094582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        return INVALID_OPERATION;
1095582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    }
1096582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent
1097582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    return setEffectEnabled(mEffects.valueAt(index), enabled);
1098582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent}
1099582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent
1100582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurentstatus_t AudioPolicyManagerBase::setEffectEnabled(EffectDescriptor *pDesc, bool enabled)
1101582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent{
1102582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    if (enabled == pDesc->mEnabled) {
11036a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("setEffectEnabled(%s) effect already %s",
1104582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent             enabled?"true":"false", enabled?"enabled":"disabled");
1105582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        return INVALID_OPERATION;
1106582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    }
1107582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent
1108582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    if (enabled) {
1109582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        if (mTotalEffectsCpuLoad + pDesc->mDesc.cpuLoad > getMaxEffectsCpuLoad()) {
111064cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("setEffectEnabled(true) CPU Load limit exceeded for Fx %s, CPU %f MIPS",
1111582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent                 pDesc->mDesc.name, (float)pDesc->mDesc.cpuLoad/10);
1112582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent            return INVALID_OPERATION;
1113582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        }
1114582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        mTotalEffectsCpuLoad += pDesc->mDesc.cpuLoad;
11156a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("setEffectEnabled(true) total CPU %d", mTotalEffectsCpuLoad);
1116582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    } else {
1117582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        if (mTotalEffectsCpuLoad < pDesc->mDesc.cpuLoad) {
111864cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("setEffectEnabled(false) CPU load %d too high for total %d",
1119582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent                    pDesc->mDesc.cpuLoad, mTotalEffectsCpuLoad);
1120582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent            pDesc->mDesc.cpuLoad = mTotalEffectsCpuLoad;
1121582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        }
1122582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        mTotalEffectsCpuLoad -= pDesc->mDesc.cpuLoad;
11236a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("setEffectEnabled(false) total CPU %d", mTotalEffectsCpuLoad);
1124582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    }
1125582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    pDesc->mEnabled = enabled;
1126582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    return NO_ERROR;
1127582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent}
1128582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent
1129f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinbool AudioPolicyManagerBase::isStreamActive(int stream, uint32_t inPastMs) const
1130f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1131f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    nsecs_t sysTime = systemTime();
1132f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mOutputs.size(); i++) {
1133f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (mOutputs.valueAt(i)->mRefCount[stream] != 0 ||
1134f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            ns2ms(sysTime - mOutputs.valueAt(i)->mStopTime[stream]) < inPastMs) {
1135f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return true;
1136f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1137f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1138f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return false;
1139f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1140f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1141f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1142f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::dump(int fd)
1143f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1144f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    const size_t SIZE = 256;
1145f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    char buffer[SIZE];
1146f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    String8 result;
1147f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1148f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
1149f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1150b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
115170c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, " Primary Output: %d\n", mPrimaryOutput);
1152f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1153f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " A2DP device address: %s\n", mA2dpDeviceAddress.string());
1154f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1155f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " SCO device address: %s\n", mScoDeviceAddress.string());
1156f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1157599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    snprintf(buffer, SIZE, " USB audio ALSA %s\n", mUsbCardAndDevice.string());
1158599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    result.append(buffer);
1159f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Output devices: %08x\n", mAvailableOutputDevices);
1160f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1161f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Input devices: %08x\n", mAvailableInputDevices);
1162f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1163f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Phone state: %d\n", mPhoneState);
1164f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1165f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Force use for communications %d\n", mForceUse[AudioSystem::FOR_COMMUNICATION]);
1166f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1167f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Force use for media %d\n", mForceUse[AudioSystem::FOR_MEDIA]);
1168f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1169f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Force use for record %d\n", mForceUse[AudioSystem::FOR_RECORD]);
1170f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1171f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Force use for dock %d\n", mForceUse[AudioSystem::FOR_DOCK]);
1172f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1173f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, result.string(), result.size());
1174f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
11755ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
117670c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "\nHW Modules dump:\n");
11775ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    write(fd, buffer, strlen(buffer));
117870c236c9290732782d5267935af1475b8d5ae602Eric Laurent    for (size_t i = 0; i < mHwModules.size(); i++) {
117970c236c9290732782d5267935af1475b8d5ae602Eric Laurent        snprintf(buffer, SIZE, "- HW Module %d:\n", i + 1);
11805ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        write(fd, buffer, strlen(buffer));
118170c236c9290732782d5267935af1475b8d5ae602Eric Laurent        mHwModules[i]->dump(fd);
11825ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
11835ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
1184f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "\nOutputs dump:\n");
1185f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
1186f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mOutputs.size(); i++) {
1187f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        snprintf(buffer, SIZE, "- Output %d dump:\n", mOutputs.keyAt(i));
1188f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        write(fd, buffer, strlen(buffer));
1189f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mOutputs.valueAt(i)->dump(fd);
1190f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1191f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1192f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "\nInputs dump:\n");
1193f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
1194f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mInputs.size(); i++) {
1195f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        snprintf(buffer, SIZE, "- Input %d dump:\n", mInputs.keyAt(i));
1196f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        write(fd, buffer, strlen(buffer));
1197f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mInputs.valueAt(i)->dump(fd);
1198f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1199f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1200f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "\nStreams dump:\n");
1201f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
1202c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    snprintf(buffer, SIZE,
1203c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent             " Stream  Can be muted  Index Min  Index Max  Index Cur [device : index]...\n");
1204f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
1205f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) {
1206c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        snprintf(buffer, SIZE, " %02d      ", i);
1207f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        write(fd, buffer, strlen(buffer));
1208c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        mStreams[i].dump(fd);
1209f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1210f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1211f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "\nTotal Effects CPU: %f MIPS, Total Effects memory: %d KB\n",
1212f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            (float)mTotalEffectsCpuLoad/10, mTotalEffectsMemory);
1213f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
1214f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1215f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "Registered effects:\n");
1216f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
1217f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mEffects.size(); i++) {
1218f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        snprintf(buffer, SIZE, "- Effect %d dump:\n", mEffects.keyAt(i));
1219f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        write(fd, buffer, strlen(buffer));
1220f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mEffects.valueAt(i)->dump(fd);
1221f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1222f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1223f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1224f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
1225f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1226f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1227f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// ----------------------------------------------------------------------------
1228f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// AudioPolicyManagerBase
1229f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// ----------------------------------------------------------------------------
1230f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1231f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima ZavinAudioPolicyManagerBase::AudioPolicyManagerBase(AudioPolicyClientInterface *clientInterface)
1232f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    :
1233f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
1234f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    Thread(false),
1235f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
123670c236c9290732782d5267935af1475b8d5ae602Eric Laurent    mPrimaryOutput((audio_io_handle_t)0),
1237f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent    mAvailableOutputDevices((audio_devices_t)0),
1238ca0657a1ca087a6d474a75fcfedd6aac3901d587Glenn Kasten    mPhoneState(AudioSystem::MODE_NORMAL),
1239f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
1240f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mTotalEffectsCpuLoad(0), mTotalEffectsMemory(0),
1241599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    mA2dpSuspended(false), mHasA2dp(false), mHasUsb(false)
1242f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1243f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mpClientInterface = clientInterface;
1244f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1245f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < AudioSystem::NUM_FORCE_USE; i++) {
1246f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mForceUse[i] = AudioSystem::FORCE_NONE;
1247f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1248f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1249f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    initializeVolumeCurves();
1250f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1251f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mA2dpDeviceAddress = String8("");
1252f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mScoDeviceAddress = String8("");
1253599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    mUsbCardAndDevice = String8("");
1254f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
12555ec145df7708564d385fd3fb764085321cf4c253Dima Zavin    if (loadAudioPolicyConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE) != NO_ERROR) {
12565ec145df7708564d385fd3fb764085321cf4c253Dima Zavin        if (loadAudioPolicyConfig(AUDIO_POLICY_CONFIG_FILE) != NO_ERROR) {
1257739022f26a7127ba76a98dda65411496086114a7Dima Zavin            ALOGE("could not load audio policy configuration file, setting defaults");
1258739022f26a7127ba76a98dda65411496086114a7Dima Zavin            defaultAudioPolicyConfig();
12595ec145df7708564d385fd3fb764085321cf4c253Dima Zavin        }
12605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
12615ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
1262b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    // open all output streams needed to access attached devices
126370c236c9290732782d5267935af1475b8d5ae602Eric Laurent    for (size_t i = 0; i < mHwModules.size(); i++) {
126470c236c9290732782d5267935af1475b8d5ae602Eric Laurent        mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->mName);
126570c236c9290732782d5267935af1475b8d5ae602Eric Laurent        if (mHwModules[i]->mHandle == 0) {
126670c236c9290732782d5267935af1475b8d5ae602Eric Laurent            ALOGW("could not open HW module %s", mHwModules[i]->mName);
126770c236c9290732782d5267935af1475b8d5ae602Eric Laurent            continue;
126870c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
126970c236c9290732782d5267935af1475b8d5ae602Eric Laurent        // open all output streams needed to access attached devices
127070c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
127170c236c9290732782d5267935af1475b8d5ae602Eric Laurent        {
127270c236c9290732782d5267935af1475b8d5ae602Eric Laurent            const IOProfile *outProfile = mHwModules[i]->mOutputProfiles[j];
127370c236c9290732782d5267935af1475b8d5ae602Eric Laurent
127470c236c9290732782d5267935af1475b8d5ae602Eric Laurent            if (outProfile->mSupportedDevices & mAttachedOutputDevices) {
127570c236c9290732782d5267935af1475b8d5ae602Eric Laurent                AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(outProfile);
127670c236c9290732782d5267935af1475b8d5ae602Eric Laurent                outputDesc->mDevice = (audio_devices_t)(mDefaultOutputDevice &
127770c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                            outProfile->mSupportedDevices);
127870c236c9290732782d5267935af1475b8d5ae602Eric Laurent                audio_io_handle_t output = mpClientInterface->openOutput(
127970c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                outProfile->mModule->mHandle,
128070c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mDevice,
128170c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mSamplingRate,
128270c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mFormat,
128370c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mChannelMask,
128470c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mLatency,
128570c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                outputDesc->mFlags);
128670c236c9290732782d5267935af1475b8d5ae602Eric Laurent                if (output == 0) {
128770c236c9290732782d5267935af1475b8d5ae602Eric Laurent                    delete outputDesc;
128870c236c9290732782d5267935af1475b8d5ae602Eric Laurent                } else {
128970c236c9290732782d5267935af1475b8d5ae602Eric Laurent                    mAvailableOutputDevices = (audio_devices_t)(mAvailableOutputDevices |
129070c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                            (outProfile->mSupportedDevices & mAttachedOutputDevices));
129170c236c9290732782d5267935af1475b8d5ae602Eric Laurent                    if (mPrimaryOutput == 0 &&
12920977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent                            outProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
129370c236c9290732782d5267935af1475b8d5ae602Eric Laurent                        mPrimaryOutput = output;
129470c236c9290732782d5267935af1475b8d5ae602Eric Laurent                    }
129570c236c9290732782d5267935af1475b8d5ae602Eric Laurent                    addOutput(output, outputDesc);
129670c236c9290732782d5267935af1475b8d5ae602Eric Laurent                    setOutputDevice(output,
129770c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                    (audio_devices_t)(mDefaultOutputDevice &
129870c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                        outProfile->mSupportedDevices),
129970c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                    true);
1300b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                }
1301b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            }
1302b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        }
1303f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1304f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
13055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGE_IF((mAttachedOutputDevices & ~mAvailableOutputDevices),
1306b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent             "Not output found for attached devices %08x",
13075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent             (mAttachedOutputDevices & ~mAvailableOutputDevices));
1308b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1309b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output");
1310b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1311f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    updateDeviceForStrategy();
13123cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
1313f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
1314b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (mPrimaryOutput != 0) {
1315f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioParameter outputCmd = AudioParameter();
1316f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputCmd.addInt(String8("set_id"), 0);
1317b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        mpClientInterface->setParameters(mPrimaryOutput, outputCmd.toString());
1318f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1319f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mTestDevice = AudioSystem::DEVICE_OUT_SPEAKER;
1320f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mTestSamplingRate = 44100;
1321f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mTestFormat = AudioSystem::PCM_16_BIT;
1322f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mTestChannels =  AudioSystem::CHANNEL_OUT_STEREO;
1323f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mTestLatencyMs = 0;
1324f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mCurOutput = 0;
1325f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mDirectOutput = false;
1326f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
1327f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mTestOutputs[i] = 0;
1328f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1329f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1330f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        const size_t SIZE = 256;
1331f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        char buffer[SIZE];
1332f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        snprintf(buffer, SIZE, "AudioPolicyManagerTest");
1333f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        run(buffer, ANDROID_PRIORITY_AUDIO);
1334f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1335f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
1336f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1337f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1338f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima ZavinAudioPolicyManagerBase::~AudioPolicyManagerBase()
1339f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1340f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
1341f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    exit();
1342f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
1343f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin   for (size_t i = 0; i < mOutputs.size(); i++) {
1344f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mpClientInterface->closeOutput(mOutputs.keyAt(i));
1345f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        delete mOutputs.valueAt(i);
1346f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin   }
1347f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin   for (size_t i = 0; i < mInputs.size(); i++) {
1348f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mpClientInterface->closeInput(mInputs.keyAt(i));
1349f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        delete mInputs.valueAt(i);
1350f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin   }
135170c236c9290732782d5267935af1475b8d5ae602Eric Laurent   for (size_t i = 0; i < mHwModules.size(); i++) {
135270c236c9290732782d5267935af1475b8d5ae602Eric Laurent        delete mHwModules[i];
13535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent   }
1354f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1355f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1356f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::initCheck()
1357f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1358b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    return (mPrimaryOutput == 0) ? NO_INIT : NO_ERROR;
1359f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1360f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1361f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
1362f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinbool AudioPolicyManagerBase::threadLoop()
1363f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
13646a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("entering threadLoop()");
1365f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    while (!exitPending())
1366f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    {
1367f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        String8 command;
1368f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        int valueInt;
1369f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        String8 value;
1370f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1371f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        Mutex::Autolock _l(mLock);
1372f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mWaitWorkCV.waitRelative(mLock, milliseconds(50));
1373f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1374f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        command = mpClientInterface->getParameters(0, String8("test_cmd_policy"));
1375f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioParameter param = AudioParameter(command);
1376f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1377f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR &&
1378f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            valueInt != 0) {
13796a70518b93928d1c91457ff805e375c82d76b0e5Steve Block            ALOGV("Test command %s received", command.string());
1380f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            String8 target;
1381f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.get(String8("target"), target) != NO_ERROR) {
1382f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                target = "Manager";
1383f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1384f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) {
1385f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_output"));
1386f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mCurOutput = valueInt;
1387f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1388f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) {
1389f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_direct"));
1390f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (value == "false") {
1391f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    mDirectOutput = false;
1392f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else if (value == "true") {
1393f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    mDirectOutput = true;
1394f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1395f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1396f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) {
1397f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_input"));
1398f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mTestInput = valueInt;
1399f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1400f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1401f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) {
1402f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_format"));
1403f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                int format = AudioSystem::INVALID_FORMAT;
1404f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (value == "PCM 16 bits") {
1405f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    format = AudioSystem::PCM_16_BIT;
1406f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else if (value == "PCM 8 bits") {
1407f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    format = AudioSystem::PCM_8_BIT;
1408f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else if (value == "Compressed MP3") {
1409f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    format = AudioSystem::MP3;
1410f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1411f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (format != AudioSystem::INVALID_FORMAT) {
1412f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    if (target == "Manager") {
1413f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mTestFormat = format;
1414f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    } else if (mTestOutputs[mCurOutput] != 0) {
1415f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        AudioParameter outputParam = AudioParameter();
1416f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        outputParam.addInt(String8("format"), format);
1417f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
1418f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    }
1419f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1420f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1421f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) {
1422f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_channels"));
1423f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                int channels = 0;
1424f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1425f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (value == "Channels Stereo") {
1426f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    channels =  AudioSystem::CHANNEL_OUT_STEREO;
1427f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else if (value == "Channels Mono") {
1428f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    channels =  AudioSystem::CHANNEL_OUT_MONO;
1429f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1430f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (channels != 0) {
1431f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    if (target == "Manager") {
1432f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mTestChannels = channels;
1433f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    } else if (mTestOutputs[mCurOutput] != 0) {
1434f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        AudioParameter outputParam = AudioParameter();
1435f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        outputParam.addInt(String8("channels"), channels);
1436f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
1437f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    }
1438f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1439f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1440f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) {
1441f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_sampleRate"));
1442f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (valueInt >= 0 && valueInt <= 96000) {
1443f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    int samplingRate = valueInt;
1444f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    if (target == "Manager") {
1445f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mTestSamplingRate = samplingRate;
1446f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    } else if (mTestOutputs[mCurOutput] != 0) {
1447f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        AudioParameter outputParam = AudioParameter();
1448f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        outputParam.addInt(String8("sampling_rate"), samplingRate);
1449f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
1450f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    }
1451f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1452f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1453f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1454f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) {
1455f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_reopen"));
1456f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
145770c236c9290732782d5267935af1475b8d5ae602Eric Laurent                AudioOutputDescriptor *outputDesc = mOutputs.valueFor(mPrimaryOutput);
1458b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                mpClientInterface->closeOutput(mPrimaryOutput);
145970c236c9290732782d5267935af1475b8d5ae602Eric Laurent
146070c236c9290732782d5267935af1475b8d5ae602Eric Laurent                audio_module_handle_t moduleHandle = outputDesc->mModule->mHandle;
146170c236c9290732782d5267935af1475b8d5ae602Eric Laurent
1462b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                delete mOutputs.valueFor(mPrimaryOutput);
1463b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                mOutputs.removeItem(mPrimaryOutput);
1464f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1465b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(NULL);
1466f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                outputDesc->mDevice = (uint32_t)AudioSystem::DEVICE_OUT_SPEAKER;
146770c236c9290732782d5267935af1475b8d5ae602Eric Laurent                mPrimaryOutput = mpClientInterface->openOutput(moduleHandle,
146870c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mDevice,
1469f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                &outputDesc->mSamplingRate,
1470f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                &outputDesc->mFormat,
147170c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mChannelMask,
1472f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                &outputDesc->mLatency,
1473f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                outputDesc->mFlags);
1474b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                if (mPrimaryOutput == 0) {
14755efbd421e0029d6fc44b1cc65c0e5e0d85e5161fSteve Block                    ALOGE("Failed to reopen hardware output stream, samplingRate: %d, format %d, channels %d",
147670c236c9290732782d5267935af1475b8d5ae602Eric Laurent                            outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask);
1477f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else {
1478f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    AudioParameter outputCmd = AudioParameter();
1479f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    outputCmd.addInt(String8("set_id"), 0);
1480b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                    mpClientInterface->setParameters(mPrimaryOutput, outputCmd.toString());
1481b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                    addOutput(mPrimaryOutput, outputDesc);
1482f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1483f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1484f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1485f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1486f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mpClientInterface->setParameters(0, String8("test_cmd_policy="));
1487f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1488f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1489f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return false;
1490f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1491f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1492f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::exit()
1493f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1494f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    {
1495f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AutoMutex _l(mLock);
1496f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        requestExit();
1497f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mWaitWorkCV.signal();
1498f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1499f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    requestExitAndWait();
1500f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1501f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1502f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinint AudioPolicyManagerBase::testOutputIndex(audio_io_handle_t output)
1503f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1504f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
1505f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (output == mTestOutputs[i]) return i;
1506f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1507f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return 0;
1508f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1509f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
1510f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1511f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// ---
1512f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1513f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::addOutput(audio_io_handle_t id, AudioOutputDescriptor *outputDesc)
1514f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1515f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    outputDesc->mId = id;
1516f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mOutputs.add(id, outputDesc);
1517f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1518f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1519f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
15203cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurentstatus_t AudioPolicyManagerBase::checkOutputsForDevice(audio_devices_t device,
15213cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                       AudioSystem::device_connection_state state,
15223cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                       SortedVector<audio_io_handle_t>& outputs)
1523f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
15243cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    AudioOutputDescriptor *desc;
1525b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1526b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (state == AudioSystem::DEVICE_STATE_AVAILABLE) {
15273cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        // first list already open outputs that can be routed to this device
1528b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        for (size_t i = 0; i < mOutputs.size(); i++) {
15293cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            desc = mOutputs.valueAt(i);
15303cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            if (!desc->isDuplicated() && (desc->mProfile->mSupportedDevices & device)) {
15313cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i));
15323cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                outputs.add(mOutputs.keyAt(i));
1533b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            }
1534b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        }
15353cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        // then look for output profiles that can be routed to this device
15363cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        SortedVector<IOProfile *> profiles;
153770c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (size_t i = 0; i < mHwModules.size(); i++)
1538b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        {
153970c236c9290732782d5267935af1475b8d5ae602Eric Laurent            if (mHwModules[i]->mHandle == 0) {
154070c236c9290732782d5267935af1475b8d5ae602Eric Laurent                continue;
154170c236c9290732782d5267935af1475b8d5ae602Eric Laurent            }
154270c236c9290732782d5267935af1475b8d5ae602Eric Laurent            for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
154370c236c9290732782d5267935af1475b8d5ae602Eric Laurent            {
154470c236c9290732782d5267935af1475b8d5ae602Eric Laurent                if (mHwModules[i]->mOutputProfiles[j]->mSupportedDevices & device) {
15453cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    ALOGV("checkOutputsForDevice(): adding profile %d from module %d", j, i);
15463cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    profiles.add(mHwModules[i]->mOutputProfiles[j]);
154770c236c9290732782d5267935af1475b8d5ae602Eric Laurent                }
154870c236c9290732782d5267935af1475b8d5ae602Eric Laurent            }
1549b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        }
155070c236c9290732782d5267935af1475b8d5ae602Eric Laurent
15513cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        if (profiles.isEmpty() && outputs.isEmpty()) {
15523cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
15533cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            return BAD_VALUE;
155470c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
155570c236c9290732782d5267935af1475b8d5ae602Eric Laurent
15563cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        // open outputs for matching profiles if needed. Direct outputs are also opened to
15573cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        // query for dynamic parameters and will be closed later by setDeviceConnectionState()
15583cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
15593cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            IOProfile *profile = profiles[profile_index];
1560b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
15613cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            // nothing to do if one output is already opened for this profile
15623cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            size_t j;
15633cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            for (j = 0; j < outputs.size(); j++) {
15643cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                desc = mOutputs.valueAt(j);
15653cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                if (!desc->isDuplicated() && desc->mProfile == profile) {
15663cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    break;
15673cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                }
15683cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
15693cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            if (j != outputs.size()) {
15703cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                continue;
15713cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
15723cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
15733cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            ALOGV("opening output for device %08x", device);
15743cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            desc = new AudioOutputDescriptor(profile);
15753cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            desc->mDevice = device;
15763cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            audio_io_handle_t output = mpClientInterface->openOutput(profile->mModule->mHandle,
15773cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                       &desc->mDevice,
15783cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                       &desc->mSamplingRate,
15793cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                       &desc->mFormat,
15803cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                       &desc->mChannelMask,
15813cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                       &desc->mLatency,
15823cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                       desc->mFlags);
15833cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            if (output != 0) {
15843cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
15853cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    String8 reply;
15863cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    char *value;
15873cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    if (profile->mSamplingRates[0] == 0) {
15883cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        reply = mpClientInterface->getParameters(output,
15893cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES));
15903cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        ALOGV("checkOutputsForDevice() direct output sup sampling rates %s",
15913cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                  reply.string());
15923cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        value = strpbrk((char *)reply.string(), "=");
15933cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        if (value != NULL) {
15943cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                            loadSamplingRates(value, profile);
15953cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        }
15963cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
15973cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    if (profile->mFormats[0] == 0) {
15983cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        reply = mpClientInterface->getParameters(output,
15993cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                       String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
16003cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        ALOGV("checkOutputsForDevice() direct output sup formats %s",
16013cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                  reply.string());
16023cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        value = strpbrk((char *)reply.string(), "=");
16033cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        if (value != NULL) {
16043cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                            loadFormats(value, profile);
16053cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        }
16063cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
16073cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    if (profile->mChannelMasks[0] == 0) {
16083cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        reply = mpClientInterface->getParameters(output,
16093cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                      String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS));
16103cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        ALOGV("checkOutputsForDevice() direct output sup channel masks %s",
16113cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                  reply.string());
16123cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        value = strpbrk((char *)reply.string(), "=");
16133cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        if (value != NULL) {
16143cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                            loadOutChannels(value + 1, profile);
16153cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        }
16163cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
16173cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    if (((profile->mSamplingRates[0] == 0) &&
16183cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                             (profile->mSamplingRates.size() < 2)) ||
16193cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                         ((profile->mFormats[0] == 0) &&
16203cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                             (profile->mFormats.size() < 2)) ||
16213cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                         ((profile->mFormats[0] == 0) &&
16223cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                             (profile->mChannelMasks.size() < 2))) {
16233cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        ALOGW("checkOutputsForDevice() direct output missing param");
16243cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        output = 0;
16253cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    } else {
16263cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        addOutput(output, desc);
16273cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
16283cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                } else {
16293cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    audio_io_handle_t duplicatedOutput = 0;
16303cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    // add output descriptor
16313cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    addOutput(output, desc);
16323cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    // set initial stream volume for device
16333cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    applyStreamVolumes(output, device);
16343cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
16353cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    //TODO: configure audio effect output stage here
16363cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
16373cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    // open a duplicating output thread for the new output and the primary output
16383cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    duplicatedOutput = mpClientInterface->openDuplicateOutput(output,
16393cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                              mPrimaryOutput);
16403cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    if (duplicatedOutput != 0) {
16413cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        // add duplicated output descriptor
16423cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        AudioOutputDescriptor *dupOutputDesc = new AudioOutputDescriptor(NULL);
16433cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        dupOutputDesc->mOutput1 = mOutputs.valueFor(mPrimaryOutput);
16443cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        dupOutputDesc->mOutput2 = mOutputs.valueFor(output);
16453cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        dupOutputDesc->mSamplingRate = desc->mSamplingRate;
16463cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        dupOutputDesc->mFormat = desc->mFormat;
16473cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        dupOutputDesc->mChannelMask = desc->mChannelMask;
16483cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        dupOutputDesc->mLatency = desc->mLatency;
16493cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        addOutput(duplicatedOutput, dupOutputDesc);
16503cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        applyStreamVolumes(duplicatedOutput, device);
16513cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    } else {
16523cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        ALOGW("checkOutputsForDevice() could not open dup output for %d and %d",
16533cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                mPrimaryOutput, output);
16543cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        mpClientInterface->closeOutput(output);
16553cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        mOutputs.removeItem(output);
16563cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        output = 0;
16573cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
16583cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                }
16593cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
16603cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            if (output == 0) {
16613cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                ALOGW("checkOutputsForDevice() could not open output for device %x", device);
16623cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                delete desc;
16633cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                profiles.removeAt(profile_index);
16643cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                profile_index--;
1665b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            } else {
16663cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                outputs.add(output);
16673cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                ALOGV("checkOutputsForDevice(): adding output %d", output);
1668f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
16693cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        }
16703cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
16713cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        if (profiles.isEmpty()) {
16723cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
16733cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            return BAD_VALUE;
1674f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1675f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
1676b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        // check if one opened output is not needed any more after disconnecting one device
1677b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        for (size_t i = 0; i < mOutputs.size(); i++) {
16783cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            desc = mOutputs.valueAt(i);
16793cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            if (!desc->isDuplicated() &&
16803cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    !(desc->mProfile->mSupportedDevices & mAvailableOutputDevices)) {
16813cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                ALOGV("checkOutputsForDevice(): disconnecting adding output %d", mOutputs.keyAt(i));
16823cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                outputs.add(mOutputs.keyAt(i));
16833cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
16843cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        }
16853cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        for (size_t i = 0; i < mHwModules.size(); i++)
16863cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        {
16873cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            if (mHwModules[i]->mHandle == 0) {
16883cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                continue;
16893cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
16903cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
16913cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            {
16923cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                IOProfile *profile = mHwModules[i]->mOutputProfiles[j];
16933cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                if ((profile->mSupportedDevices & device) &&
16943cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        (profile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
16953cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    ALOGV("checkOutputsForDevice(): clearing direct output profile %d on module %d",
16963cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                          j, i);
16973cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    if (profile->mSamplingRates[0] == 0) {
16983cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        profile->mSamplingRates.clear();
16993cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        profile->mSamplingRates.add(0);
17003cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
17013cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    if (profile->mFormats[0] == 0) {
17023cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        profile->mFormats.clear();
17033cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        profile->mFormats.add((audio_format_t)0);
17043cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
17053cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    if (profile->mChannelMasks[0] == 0) {
17063cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        profile->mChannelMasks.clear();
17073cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        profile->mChannelMasks.add((audio_channel_mask_t)0);
17083cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
17093cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                }
1710b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            }
1711f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1712f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
17133cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    return NO_ERROR;
1714f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1715f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1716b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurentvoid AudioPolicyManagerBase::closeOutput(audio_io_handle_t output)
1717f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1718b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    ALOGV("closeOutput(%d)", output);
1719f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1720b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
1721b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (outputDesc == NULL) {
1722b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        ALOGW("closeOutput() unknown output %d", output);
1723b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        return;
1724f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1725f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1726b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    // look for duplicated outputs connected to the output being removed.
1727b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    for (size_t i = 0; i < mOutputs.size(); i++) {
1728b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        AudioOutputDescriptor *dupOutputDesc = mOutputs.valueAt(i);
1729b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        if (dupOutputDesc->isDuplicated() &&
1730b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                (dupOutputDesc->mOutput1 == outputDesc ||
1731b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                dupOutputDesc->mOutput2 == outputDesc)) {
1732b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            AudioOutputDescriptor *outputDesc2;
1733b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            if (dupOutputDesc->mOutput1 == outputDesc) {
1734b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                outputDesc2 = dupOutputDesc->mOutput2;
1735b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            } else {
1736b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                outputDesc2 = dupOutputDesc->mOutput1;
1737b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            }
1738b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            // As all active tracks on duplicated output will be deleted,
1739b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            // and as they were also referenced on the other output, the reference
1740b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            // count for their stream type must be adjusted accordingly on
1741b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            // the other output.
1742b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            for (int j = 0; j < (int)AudioSystem::NUM_STREAM_TYPES; j++) {
1743b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                int refCount = dupOutputDesc->mRefCount[j];
1744b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                outputDesc2->changeRefCount((AudioSystem::stream_type)j,-refCount);
1745b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            }
1746b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i);
1747b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput);
1748f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1749b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            mpClientInterface->closeOutput(duplicatedOutput);
1750b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            delete mOutputs.valueFor(duplicatedOutput);
1751b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            mOutputs.removeItem(duplicatedOutput);
1752f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1753f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1754b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1755b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    AudioParameter param;
1756b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    param.add(String8("closing"), String8("true"));
1757b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    mpClientInterface->setParameters(output, param.toString());
1758b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1759b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    mpClientInterface->closeOutput(output);
1760b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    delete mOutputs.valueFor(output);
1761b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    mOutputs.removeItem(output);
1762f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1763f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1764f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric LaurentSortedVector<audio_io_handle_t> AudioPolicyManagerBase::getOutputsForDevice(audio_devices_t device)
1765f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1766b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    SortedVector<audio_io_handle_t> outputs;
1767f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
17683cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGVV("getOutputsForDevice() device %04x", device);
1769b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    for (size_t i = 0; i < mOutputs.size(); i++) {
17703cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        ALOGVV("output %d isDuplicated=%d device=%04x",
177112bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi                i, mOutputs.valueAt(i)->isDuplicated(), mOutputs.valueAt(i)->supportedDevices());
1772b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        if ((device & mOutputs.valueAt(i)->supportedDevices()) == device) {
17733cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            ALOGVV("getOutputsForDevice() found output %d", mOutputs.keyAt(i));
1774b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            outputs.add(mOutputs.keyAt(i));
1775f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1776f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1777b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    return outputs;
1778f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1779f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1780b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurentbool AudioPolicyManagerBase::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
1781b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                                   SortedVector<audio_io_handle_t>& outputs2)
1782f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1783b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (outputs1.size() != outputs2.size()) {
1784b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        return false;
1785f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1786b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    for (size_t i = 0; i < outputs1.size(); i++) {
1787b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        if (outputs1[i] != outputs2[i]) {
1788b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            return false;
1789f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1790f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1791b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    return true;
1792b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent}
1793b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1794b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurentvoid AudioPolicyManagerBase::checkOutputForStrategy(routing_strategy strategy)
1795b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent{
179601e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent    audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
179701e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent    audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
179801e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent    SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice);
179901e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent    SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice);
1800b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1801b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (!vectorsEqual(srcOutputs,dstOutputs)) {
1802b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d",
1803b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent              strategy, srcOutputs[0], dstOutputs[0]);
18045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // mute strategy while moving tracks from one output to another
18055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        for (size_t i = 0; i < srcOutputs.size(); i++) {
1806fa3697d716b444bbea6be480801536c44bf69214Eric Laurent            AudioOutputDescriptor *desc = mOutputs.valueFor(srcOutputs[i]);
1807fa3697d716b444bbea6be480801536c44bf69214Eric Laurent            if (desc->strategyRefCount(strategy) != 0) {
1808fa3697d716b444bbea6be480801536c44bf69214Eric Laurent                setStrategyMute(strategy, true, srcOutputs[i]);
1809fa3697d716b444bbea6be480801536c44bf69214Eric Laurent                setStrategyMute(strategy, false, srcOutputs[i], MUTE_TIME_MS, newDevice);
1810fa3697d716b444bbea6be480801536c44bf69214Eric Laurent            }
18115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
1812f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1813f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // Move effects associated to this strategy from previous output to new output
18144660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen        if (strategy == STRATEGY_MEDIA) {
18154660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen            int outIdx = 0;
18164660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen            for (size_t i = 0; i < dstOutputs.size(); i++) {
18174660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                AudioOutputDescriptor *desc = mOutputs.valueFor(dstOutputs[i]);
18184660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                if (desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
18194660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                    outIdx = i;
18204660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                }
18214660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen            }
18224660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen            SortedVector<audio_io_handle_t> moved;
18234660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen            for (size_t i = 0; i < mEffects.size(); i++) {
18244660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                EffectDescriptor *desc = mEffects.valueAt(i);
18254660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                if (desc->mSession == AUDIO_SESSION_OUTPUT_MIX &&
18264660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                        desc->mIo != dstOutputs[outIdx]) {
18274660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                    if (moved.indexOf(desc->mIo) < 0) {
18284660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                        ALOGV("checkOutputForStrategy() moving effect %d to output %d",
18294660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                              mEffects.keyAt(i), dstOutputs[outIdx]);
18304660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                        mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, desc->mIo,
18314660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                                                       dstOutputs[outIdx]);
18324660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                        moved.add(desc->mIo);
18334660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                    }
18344660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                    desc->mIo = dstOutputs[outIdx];
18354660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                }
18364660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen            }
18374660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen        }
18385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // Move tracks associated to this strategy from previous output to new output
1839f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        for (int i = 0; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) {
1840f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (getStrategy((AudioSystem::stream_type)i) == strategy) {
184112bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi                //FIXME see fixme on name change
18424660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                mpClientInterface->setStreamOutput((AudioSystem::stream_type)i,
18434660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                                                   dstOutputs[0] /* ignored */);
1844f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1845f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1846f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1847f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1848f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1849f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::checkOutputForAllStrategies()
1850f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1851c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
1852f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForStrategy(STRATEGY_PHONE);
1853f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForStrategy(STRATEGY_SONIFICATION);
185412bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
1855f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForStrategy(STRATEGY_MEDIA);
1856f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForStrategy(STRATEGY_DTMF);
1857f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1858f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1859b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurentaudio_io_handle_t AudioPolicyManagerBase::getA2dpOutput()
1860b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent{
18615ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (!mHasA2dp) {
1862b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        return 0;
1863b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    }
1864b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1865b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    for (size_t i = 0; i < mOutputs.size(); i++) {
1866b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        AudioOutputDescriptor *outputDesc = mOutputs.valueAt(i);
1867b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        if (!outputDesc->isDuplicated() && outputDesc->device() & AUDIO_DEVICE_OUT_ALL_A2DP) {
1868b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            return mOutputs.keyAt(i);
1869b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        }
1870b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    }
1871b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1872b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    return 0;
1873b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent}
1874b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1875f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::checkA2dpSuspend()
1876f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
18775ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (!mHasA2dp) {
1878b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        return;
1879b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    }
1880b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    audio_io_handle_t a2dpOutput = getA2dpOutput();
1881b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (a2dpOutput == 0) {
1882b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        return;
1883b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    }
1884b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1885f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // suspend A2DP output if:
1886f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      (NOT already suspended) &&
1887f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      ((SCO device is connected &&
1888f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //       (forced usage for communication || for record is SCO))) ||
1889f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      (phone state is ringing || in call)
1890f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //
1891f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // restore A2DP output if:
1892f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      (Already suspended) &&
1893f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      ((SCO device is NOT connected ||
1894f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //       (forced usage NOT for communication && NOT for record is SCO))) &&
1895f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      (phone state is NOT ringing && NOT in call)
1896f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //
1897f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mA2dpSuspended) {
1898f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (((mScoDeviceAddress == "") ||
1899f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin             ((mForceUse[AudioSystem::FOR_COMMUNICATION] != AudioSystem::FORCE_BT_SCO) &&
1900f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin              (mForceUse[AudioSystem::FOR_RECORD] != AudioSystem::FORCE_BT_SCO))) &&
1901f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin             ((mPhoneState != AudioSystem::MODE_IN_CALL) &&
1902f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin              (mPhoneState != AudioSystem::MODE_RINGTONE))) {
1903f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1904b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            mpClientInterface->restoreOutput(a2dpOutput);
1905f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mA2dpSuspended = false;
1906f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1907f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
1908f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (((mScoDeviceAddress != "") &&
1909f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin             ((mForceUse[AudioSystem::FOR_COMMUNICATION] == AudioSystem::FORCE_BT_SCO) ||
1910f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin              (mForceUse[AudioSystem::FOR_RECORD] == AudioSystem::FORCE_BT_SCO))) ||
1911f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin             ((mPhoneState == AudioSystem::MODE_IN_CALL) ||
1912f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin              (mPhoneState == AudioSystem::MODE_RINGTONE))) {
1913f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1914b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            mpClientInterface->suspendOutput(a2dpOutput);
1915f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mA2dpSuspended = true;
1916f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1917f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1918f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1919f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1920f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurentaudio_devices_t AudioPolicyManagerBase::getNewDevice(audio_io_handle_t output, bool fromCache)
1921f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1922f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent    audio_devices_t device = (audio_devices_t)0;
1923f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1924f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
1925f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // check the following by order of priority to request a routing change if necessary:
1926c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    // 1: the strategy enforced audible is active on the output:
1927c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    //      use device for strategy enforced audible
1928c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    // 2: we are in call or the strategy phone is active on the output:
1929f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      use device for strategy phone
1930c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    // 3: the strategy sonification is active on the output:
1931f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      use device for strategy sonification
193212bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    // 4: the strategy "respectful" sonification is active on the output:
193312bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    //      use device for strategy "respectful" sonification
193412bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    // 5: the strategy media is active on the output:
1935f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      use device for strategy media
193612bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    // 6: the strategy DTMF is active on the output:
1937f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      use device for strategy DTMF
1938c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    if (outputDesc->isUsedByStrategy(STRATEGY_ENFORCED_AUDIBLE)) {
1939c16ac09f510437e8340be691720177a490ae78f0Eric Laurent        device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
1940c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    } else if (isInCall() ||
1941c16ac09f510437e8340be691720177a490ae78f0Eric Laurent                    outputDesc->isUsedByStrategy(STRATEGY_PHONE)) {
1942f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
1943f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else if (outputDesc->isUsedByStrategy(STRATEGY_SONIFICATION)) {
1944f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
194512bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    } else if (outputDesc->isUsedByStrategy(STRATEGY_SONIFICATION_RESPECTFUL)) {
19465ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
1947f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else if (outputDesc->isUsedByStrategy(STRATEGY_MEDIA)) {
1948f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
1949f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else if (outputDesc->isUsedByStrategy(STRATEGY_DTMF)) {
1950f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
1951f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1952f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
19536a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("getNewDevice() selected device %x", device);
1954f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return device;
1955f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1956f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1957f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinuint32_t AudioPolicyManagerBase::getStrategyForStream(AudioSystem::stream_type stream) {
1958f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return (uint32_t)getStrategy(stream);
1959f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1960f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1961f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurentaudio_devices_t AudioPolicyManagerBase::getDevicesForStream(AudioSystem::stream_type stream) {
1962f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent    audio_devices_t devices;
1963f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // By checking the range of stream before calling getStrategy, we avoid
19645efbd421e0029d6fc44b1cc65c0e5e0d85e5161fSteve Block    // getStrategy's behavior for invalid streams.  getStrategy would do a ALOGE
1965f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // and then return STRATEGY_MEDIA, but we want to return the empty set.
1966f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (stream < (AudioSystem::stream_type) 0 || stream >= AudioSystem::NUM_STREAM_TYPES) {
1967f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent        devices = (audio_devices_t)0;
1968f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
1969f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioPolicyManagerBase::routing_strategy strategy = getStrategy(stream);
19705ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        devices = getDeviceForStrategy(strategy, true /*fromCache*/);
1971f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1972f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return devices;
1973f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1974f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1975f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima ZavinAudioPolicyManagerBase::routing_strategy AudioPolicyManagerBase::getStrategy(
1976f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioSystem::stream_type stream) {
1977f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // stream to strategy mapping
1978f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    switch (stream) {
1979f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::VOICE_CALL:
1980f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::BLUETOOTH_SCO:
1981f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return STRATEGY_PHONE;
1982f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::RING:
1983f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::ALARM:
1984f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return STRATEGY_SONIFICATION;
198512bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    case AudioSystem::NOTIFICATION:
198612bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        return STRATEGY_SONIFICATION_RESPECTFUL;
1987f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::DTMF:
1988f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return STRATEGY_DTMF;
1989f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    default:
19905efbd421e0029d6fc44b1cc65c0e5e0d85e5161fSteve Block        ALOGE("unknown stream type");
1991f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::SYSTEM:
1992f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // NOTE: SYSTEM stream uses MEDIA strategy because muting music and switching outputs
1993f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // while key clicks are played produces a poor result
1994f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::TTS:
1995f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::MUSIC:
1996f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return STRATEGY_MEDIA;
1997c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    case AudioSystem::ENFORCED_AUDIBLE:
1998c16ac09f510437e8340be691720177a490ae78f0Eric Laurent        return STRATEGY_ENFORCED_AUDIBLE;
1999f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2000f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2001f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
200212bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivivoid AudioPolicyManagerBase::handleNotificationRoutingForStream(AudioSystem::stream_type stream) {
200312bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    switch(stream) {
200412bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    case AudioSystem::MUSIC:
200512bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
200612bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        updateDeviceForStrategy();
200712bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        break;
200812bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    default:
200912bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        break;
201012bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    }
201112bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi}
201212bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi
20135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentaudio_devices_t AudioPolicyManagerBase::getDeviceForStrategy(routing_strategy strategy,
20145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                             bool fromCache)
2015f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2016f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    uint32_t device = 0;
2017f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2018f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (fromCache) {
20193cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
20205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent              strategy, mDeviceForStrategy[strategy]);
2021f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return mDeviceForStrategy[strategy];
2022f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2023f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2024f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    switch (strategy) {
202512bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi
202612bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    case STRATEGY_SONIFICATION_RESPECTFUL:
202712bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        if (isInCall()) {
20285ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
202912bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        } else if (isStreamActive(AudioSystem::MUSIC, SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
203012bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi            // while media is playing (or has recently played), use the same device
20315ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/);
203212bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        } else {
203312bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi            // when media is not playing anymore, fall back on the sonification behavior
20345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
203512bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        }
203612bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi
203712bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        break;
203812bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi
2039f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case STRATEGY_DTMF:
2040f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (!isInCall()) {
2041f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // when off call, DTMF strategy follows the same rules as MEDIA strategy
20425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/);
2043f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            break;
2044f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2045f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // when in call, DTMF and PHONE strategies follow the same rules
2046f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // FALL THROUGH
2047f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2048f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case STRATEGY_PHONE:
2049f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // for phone strategy, we first consider the forced use and then the available devices by order
2050f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // of priority
2051f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        switch (mForceUse[AudioSystem::FOR_COMMUNICATION]) {
2052f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        case AudioSystem::FORCE_BT_SCO:
2053f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (!isInCall() || strategy != STRATEGY_DTMF) {
2054f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_CARKIT;
2055f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (device) break;
2056f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2057f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_HEADSET;
2058f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (device) break;
2059f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_SCO;
2060f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (device) break;
2061f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // if SCO device is requested but no SCO device is available, fall back to default case
2062f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // FALL THROUGH
2063f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2064f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        default:    // FORCE_NONE
2065f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to A2DP
20661afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent            if (mHasA2dp && !isInCall() &&
20671afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent                    (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
20681afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent                    (getA2dpOutput() != 0) && !mA2dpSuspended) {
2069f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP;
2070f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (device) break;
2071f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
2072f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (device) break;
2073f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
20741afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent            device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_WIRED_HEADPHONE;
20751afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent            if (device) break;
20761afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent            device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_WIRED_HEADSET;
20771afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent            if (device) break;
2078599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
2079599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            if (device) break;
2080599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
2081599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            if (device) break;
2082f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_DGTL_DOCK_HEADSET;
2083f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (device) break;
208455ac23bc116ad9ae38266a5e660fd22f5104348eEric Laurent            device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_AUX_DIGITAL;
208555ac23bc116ad9ae38266a5e660fd22f5104348eEric Laurent            if (device) break;
2086f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_ANLG_DOCK_HEADSET;
2087f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (device) break;
2088f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_EARPIECE;
20895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            if (device) break;
20905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = mDefaultOutputDevice;
2091f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (device == 0) {
20925ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE");
2093f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2094f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            break;
2095f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2096f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        case AudioSystem::FORCE_SPEAKER:
2097f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to
2098f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // A2DP speaker when forcing to speaker output
20991afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent            if (mHasA2dp && !isInCall() &&
21001afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent                    (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
21011afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent                    (getA2dpOutput() != 0) && !mA2dpSuspended) {
2102f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
2103f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (device) break;
2104f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2105599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
2106599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            if (device) break;
2107599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
2108599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            if (device) break;
2109f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_DGTL_DOCK_HEADSET;
2110f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (device) break;
211155ac23bc116ad9ae38266a5e660fd22f5104348eEric Laurent            device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_AUX_DIGITAL;
211255ac23bc116ad9ae38266a5e660fd22f5104348eEric Laurent            if (device) break;
2113f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_ANLG_DOCK_HEADSET;
2114f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (device) break;
2115f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_SPEAKER;
21165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            if (device) break;
21175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = mDefaultOutputDevice;
2118f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (device == 0) {
21195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE, FORCE_SPEAKER");
2120f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2121f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            break;
2122f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2123f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    break;
2124f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2125f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case STRATEGY_SONIFICATION:
2126f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2127f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // If incall, just select the STRATEGY_PHONE device: The rest of the behavior is handled by
2128f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // handleIncallSonification().
2129f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (isInCall()) {
21305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = getDeviceForStrategy(STRATEGY_PHONE, false /*fromCache*/);
2131f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            break;
2132f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2133c16ac09f510437e8340be691720177a490ae78f0Eric Laurent        // FALL THROUGH
2134c16ac09f510437e8340be691720177a490ae78f0Eric Laurent
2135c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    case STRATEGY_ENFORCED_AUDIBLE:
2136c16ac09f510437e8340be691720177a490ae78f0Eric Laurent        // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION
2137ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        // except:
2138ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        //   - when in call where it doesn't default to STRATEGY_PHONE behavior
2139ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        //   - in countries where not enforced in which case it follows STRATEGY_MEDIA
2140c16ac09f510437e8340be691720177a490ae78f0Eric Laurent
2141ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        if (strategy == STRATEGY_SONIFICATION ||
2142ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent                !mStreams[AUDIO_STREAM_ENFORCED_AUDIBLE].mCanBeMuted) {
2143ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent            device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_SPEAKER;
2144ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent            if (device == 0) {
2145ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent                ALOGE("getDeviceForStrategy() speaker device not found for STRATEGY_SONIFICATION");
2146ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent            }
2147f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2148f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // The second device used for sonification is the same as the device used by media strategy
2149f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // FALL THROUGH
2150f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2151f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case STRATEGY_MEDIA: {
21521afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent        uint32_t device2 = 0;
21531afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent        if (mHasA2dp && (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
21541afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent                (getA2dpOutput() != 0) && !mA2dpSuspended) {
21551afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent            device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP;
2156f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (device2 == 0) {
2157f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
2158f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2159f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (device2 == 0) {
2160f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
2161f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2162f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2163f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (device2 == 0) {
21641afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent            device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_WIRED_HEADPHONE;
21651afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent        }
21661afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent        if (device2 == 0) {
21671afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent            device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_WIRED_HEADSET;
21681afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent        }
21691afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent        if (device2 == 0) {
2170599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
2171599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent        }
2172599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent        if (device2 == 0) {
2173599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
2174599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent        }
2175599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent        if (device2 == 0) {
217655ac23bc116ad9ae38266a5e660fd22f5104348eEric Laurent            device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_DGTL_DOCK_HEADSET;
2177f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2178f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (device2 == 0) {
217955ac23bc116ad9ae38266a5e660fd22f5104348eEric Laurent            device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_AUX_DIGITAL;
2180f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2181f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (device2 == 0) {
2182f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_ANLG_DOCK_HEADSET;
2183f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2184f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (device2 == 0) {
2185f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_SPEAKER;
2186f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2187f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2188c16ac09f510437e8340be691720177a490ae78f0Eric Laurent        // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or
2189c16ac09f510437e8340be691720177a490ae78f0Eric Laurent        // STRATEGY_ENFORCED_AUDIBLE, 0 otherwise
2190f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device |= device2;
21915ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (device) break;
21925ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        device = mDefaultOutputDevice;
2193f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (device == 0) {
21945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGE("getDeviceForStrategy() no device found for STRATEGY_MEDIA");
2195f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2196f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        } break;
2197f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2198f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    default:
219964cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("getDeviceForStrategy() unknown strategy: %d", strategy);
2200f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
2201f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2202f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
22033cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGVV("getDeviceForStrategy() strategy %d, device %x", strategy, device);
2204f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent    return (audio_devices_t)device;
2205f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2206f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2207f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::updateDeviceForStrategy()
2208f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2209f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < NUM_STRATEGIES; i++) {
22105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
22115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
22125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
22135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
2214b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurentuint32_t AudioPolicyManagerBase::checkDeviceMuteStrategies(AudioOutputDescriptor *outputDesc,
22159029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                                                       audio_devices_t prevDevice,
22165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                       uint32_t delayMs)
22175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
22189029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    // mute/unmute strategies using an incompatible device combination
22199029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    // if muting, wait for the audio in pcm buffer to be drained before proceeding
22209029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    // if unmuting, unmute only after the specified delay
22215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (outputDesc->isDuplicated()) {
2222b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        return 0;
22235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
22245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
22255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    uint32_t muteWaitMs = 0;
22265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    audio_devices_t device = outputDesc->device();
22275ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    bool shouldMute = (outputDesc->refCount() != 0) &&
22285ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                    (AudioSystem::popCount(device) >= 2);
22299029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    // temporary mute output if device selection changes to avoid volume bursts due to
22309029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    // different per device volumes
22319029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    bool tempMute = (outputDesc->refCount() != 0) && (device != prevDevice);
22325ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
22335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < NUM_STRATEGIES; i++) {
22345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
22355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        bool mute = shouldMute && (curDevice & device) && (curDevice != device);
22365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        bool doMute = false;
22375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
22385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (mute && !outputDesc->mStrategyMutedByDevice[i]) {
22395ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            doMute = true;
22405ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            outputDesc->mStrategyMutedByDevice[i] = true;
22415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){
22425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            doMute = true;
22435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            outputDesc->mStrategyMutedByDevice[i] = false;
22445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
22459029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent        if (doMute || tempMute) {
22465ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            for (size_t j = 0; j < mOutputs.size(); j++) {
22475ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                AudioOutputDescriptor *desc = mOutputs.valueAt(j);
22485ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                if ((desc->supportedDevices() & outputDesc->supportedDevices()) == 0) {
22495ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                    continue;
22505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                }
22515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                audio_io_handle_t curOutput = mOutputs.keyAt(j);
22523cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x) on output %d",
22535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                      mute ? "muting" : "unmuting", i, curDevice, curOutput);
22545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                setStrategyMute((routing_strategy)i, mute, curOutput, mute ? 0 : delayMs);
22559029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                if (desc->strategyRefCount((routing_strategy)i) != 0) {
22569029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                    if (tempMute) {
225701e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                        setStrategyMute((routing_strategy)i, true, curOutput);
225801e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                        setStrategyMute((routing_strategy)i, false, curOutput,
225901e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                            desc->latency() * 2, device);
22609029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                    }
22619029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                    if (tempMute || mute) {
22629029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                        if (muteWaitMs < desc->latency()) {
22639029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                            muteWaitMs = desc->latency();
22649029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                        }
22655ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                    }
22665ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                }
22675ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
22685ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
22695ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
22705ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
22715ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // FIXME: should not need to double latency if volume could be applied immediately by the
22725ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // audioflinger mixer. We must account for the delay between now and the next time
22735ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // the audioflinger thread for this output will process a buffer (which corresponds to
22745ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // one buffer size, usually 1/2 or 1/4 of the latency).
22755ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    muteWaitMs *= 2;
22765ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // wait for the PCM output buffers to empty before proceeding with the rest of the command
22775ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (muteWaitMs > delayMs) {
2278b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        muteWaitMs -= delayMs;
2279b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        usleep(muteWaitMs * 1000);
2280b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        return muteWaitMs;
2281f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2282b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent    return 0;
2283f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2284f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2285b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurentuint32_t AudioPolicyManagerBase::setOutputDevice(audio_io_handle_t output,
2286f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                                             audio_devices_t device,
2287f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                                             bool force,
2288f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                                             int delayMs)
2289f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
22905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGV("setOutputDevice() output %d device %04x delayMs %d", output, device, delayMs);
2291f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
22925ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    AudioParameter param;
2293b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent    uint32_t muteWaitMs = 0;
2294f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2295f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (outputDesc->isDuplicated()) {
2296b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        muteWaitMs = setOutputDevice(outputDesc->mOutput1->mId, device, force, delayMs);
2297b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        muteWaitMs += setOutputDevice(outputDesc->mOutput2->mId, device, force, delayMs);
2298b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        return muteWaitMs;
2299f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2300f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // filter devices according to output selected
2301f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent    device = (audio_devices_t)(device & outputDesc->mProfile->mSupportedDevices);
2302f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
23035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    audio_devices_t prevDevice = outputDesc->mDevice;
23045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
23055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGV("setOutputDevice() prevDevice %04x", prevDevice);
23065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
23075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (device != 0) {
23085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        outputDesc->mDevice = device;
23095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
23109029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs);
23115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
2312f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // Do not change the routing if:
23135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    //  - the requested device is 0
2314f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //  - the requested device is the same as current device and force is not specified.
2315f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // Doing this check here allows the caller to call setOutputDevice() without conditions
2316f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if ((device == 0 || device == prevDevice) && !force) {
23175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        ALOGV("setOutputDevice() setting same device %04x or null device for output %d", device, output);
2318b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        return muteWaitMs;
2319f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2320f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
23215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGV("setOutputDevice() changing device");
2322f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // do the routing
2323f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    param.addInt(String8(AudioParameter::keyRouting), (int)device);
23245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    mpClientInterface->setParameters(output, param.toString(), delayMs);
23255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
2326f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // update stream volumes according to new device
2327f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    applyStreamVolumes(output, device, delayMs);
2328b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent
2329b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent    return muteWaitMs;
23305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
2331f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
23325ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric LaurentAudioPolicyManagerBase::IOProfile *AudioPolicyManagerBase::getInputProfile(audio_devices_t device,
23335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                   uint32_t samplingRate,
23345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                   uint32_t format,
23355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                   uint32_t channelMask)
23365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
23375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // Choose an input profile based on the requested capture parameters: select the first available
23385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // profile supporting all requested parameters.
23395ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
234070c236c9290732782d5267935af1475b8d5ae602Eric Laurent    for (size_t i = 0; i < mHwModules.size(); i++)
23415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    {
234270c236c9290732782d5267935af1475b8d5ae602Eric Laurent        if (mHwModules[i]->mHandle == 0) {
23435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            continue;
23445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
234570c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
23465ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        {
234770c236c9290732782d5267935af1475b8d5ae602Eric Laurent            IOProfile *profile = mHwModules[i]->mInputProfiles[j];
234870c236c9290732782d5267935af1475b8d5ae602Eric Laurent            if (profile->isCompatibleProfile(device, samplingRate, format,
23490977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent                                             channelMask,(audio_output_flags_t)0)) {
235070c236c9290732782d5267935af1475b8d5ae602Eric Laurent                return profile;
23515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
23525ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
2353f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
235470c236c9290732782d5267935af1475b8d5ae602Eric Laurent    return NULL;
2355f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2356f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2357f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurentaudio_devices_t AudioPolicyManagerBase::getDeviceForInputSource(int inputSource)
2358f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
23595ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    uint32_t device = 0;
2360f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2361f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    switch(inputSource) {
2362f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_DEFAULT:
2363f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_MIC:
2364f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_RECOGNITION:
2365f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_COMMUNICATION:
2366f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (mForceUse[AudioSystem::FOR_RECORD] == AudioSystem::FORCE_BT_SCO &&
2367f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mAvailableInputDevices & AudioSystem::DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
2368f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            device = AudioSystem::DEVICE_IN_BLUETOOTH_SCO_HEADSET;
2369f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        } else if (mAvailableInputDevices & AudioSystem::DEVICE_IN_WIRED_HEADSET) {
2370f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            device = AudioSystem::DEVICE_IN_WIRED_HEADSET;
23715ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (mAvailableInputDevices & AudioSystem::DEVICE_IN_BUILTIN_MIC) {
2372f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            device = AudioSystem::DEVICE_IN_BUILTIN_MIC;
2373f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2374f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
2375f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_CAMCORDER:
23765ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (mAvailableInputDevices & AudioSystem::DEVICE_IN_BACK_MIC) {
2377f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            device = AudioSystem::DEVICE_IN_BACK_MIC;
23785ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (mAvailableInputDevices & AudioSystem::DEVICE_IN_BUILTIN_MIC) {
2379f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            device = AudioSystem::DEVICE_IN_BUILTIN_MIC;
2380f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2381f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
2382f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_UPLINK:
2383f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_DOWNLINK:
2384f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_CALL:
23855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (mAvailableInputDevices & AudioSystem::DEVICE_IN_VOICE_CALL) {
23865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = AudioSystem::DEVICE_IN_VOICE_CALL;
23875ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
2388f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
2389f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    default:
239064cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("getDeviceForInputSource() invalid input source %d", inputSource);
2391f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
2392f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
23936a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("getDeviceForInputSource()input source %d, device %08x", inputSource, device);
2394f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent    return (audio_devices_t)device;
2395f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2396f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2397f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinaudio_io_handle_t AudioPolicyManagerBase::getActiveInput()
2398f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2399f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mInputs.size(); i++) {
2400f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (mInputs.valueAt(i)->mRefCount > 0) {
2401f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return mInputs.keyAt(i);
2402f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2403f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2404f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return 0;
2405f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2406f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2407e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2408c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentaudio_devices_t AudioPolicyManagerBase::getDeviceForVolume(audio_devices_t device)
2409e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent{
2410e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    if (device == 0) {
2411e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        // this happens when forcing a route update and no track is active on an output.
2412e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        // In this case the returned category is not important.
2413c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        device =  AUDIO_DEVICE_OUT_SPEAKER;
2414c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    } else if (AudioSystem::popCount(device) > 1) {
2415e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        // Multiple device selection is either:
2416e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        //  - speaker + one other device: give priority to speaker in this case.
2417e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        //  - one A2DP device + another device: happens with duplicated output. In this case
2418e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        // retain the device on the A2DP output as the other must not correspond to an active
2419e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        // selection if not the speaker.
2420c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        if (device & AUDIO_DEVICE_OUT_SPEAKER) {
2421c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            device = AUDIO_DEVICE_OUT_SPEAKER;
2422c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        } else {
2423c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            device = (audio_devices_t)(device & AUDIO_DEVICE_OUT_ALL_A2DP);
2424c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        }
2425e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    }
2426e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
242764cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block    ALOGW_IF(AudioSystem::popCount(device) != 1,
2428c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            "getDeviceForVolume() invalid device combination: %08x",
2429e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            device);
2430e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2431c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    return device;
2432c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent}
2433c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
2434f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric LaurentAudioPolicyManagerBase::device_category AudioPolicyManagerBase::getDeviceCategory(audio_devices_t device)
2435c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent{
2436f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent    switch(getDeviceForVolume(device)) {
2437e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_EARPIECE:
2438e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            return DEVICE_CATEGORY_EARPIECE;
2439e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_WIRED_HEADSET:
2440e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_WIRED_HEADPHONE:
2441e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_BLUETOOTH_SCO:
2442e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET:
2443e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP:
2444e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES:
2445e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            return DEVICE_CATEGORY_HEADSET;
2446e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_SPEAKER:
2447e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT:
2448e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER:
2449c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        case AUDIO_DEVICE_OUT_AUX_DIGITAL:
2450599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent        case AUDIO_DEVICE_OUT_USB_ACCESSORY:
2451599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent        case AUDIO_DEVICE_OUT_USB_DEVICE:
2452e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        default:
2453e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            return DEVICE_CATEGORY_SPEAKER;
2454e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    }
2455e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent}
2456e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2457f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurentfloat AudioPolicyManagerBase::volIndexToAmpl(audio_devices_t device, const StreamDescriptor& streamDesc,
2458e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        int indexInUi)
2459e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent{
2460e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    device_category deviceCategory = getDeviceCategory(device);
2461e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    const VolumeCurvePoint *curve = streamDesc.mVolumeCurve[deviceCategory];
2462e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2463f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // the volume index in the UI is relative to the min and max volume indices for this stream type
2464e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    int nbSteps = 1 + curve[VOLMAX].mIndex -
2465e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            curve[VOLMIN].mIndex;
2466f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    int volIdx = (nbSteps * (indexInUi - streamDesc.mIndexMin)) /
2467f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            (streamDesc.mIndexMax - streamDesc.mIndexMin);
2468f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2469f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // find what part of the curve this index volume belongs to, or if it's out of bounds
2470f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    int segment = 0;
2471e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    if (volIdx < curve[VOLMIN].mIndex) {         // out of bounds
2472f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return 0.0f;
2473e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    } else if (volIdx < curve[VOLKNEE1].mIndex) {
2474f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        segment = 0;
2475e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    } else if (volIdx < curve[VOLKNEE2].mIndex) {
2476f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        segment = 1;
2477e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    } else if (volIdx <= curve[VOLMAX].mIndex) {
2478f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        segment = 2;
2479f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {                                                               // out of bounds
2480f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return 1.0f;
2481f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2482f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2483f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // linear interpolation in the attenuation table in dB
2484e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    float decibels = curve[segment].mDBAttenuation +
2485e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            ((float)(volIdx - curve[segment].mIndex)) *
2486e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent                ( (curve[segment+1].mDBAttenuation -
2487e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent                        curve[segment].mDBAttenuation) /
2488e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent                    ((float)(curve[segment+1].mIndex -
2489e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent                            curve[segment].mIndex)) );
2490f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2491f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    float amplification = exp( decibels * 0.115129f); // exp( dB * ln(10) / 20 )
2492f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
24933cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGVV("VOLUME vol index=[%d %d %d], dB=[%.1f %.1f %.1f] ampl=%.5f",
2494e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            curve[segment].mIndex, volIdx,
2495e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            curve[segment+1].mIndex,
2496e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            curve[segment].mDBAttenuation,
2497cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent            decibels,
2498e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            curve[segment+1].mDBAttenuation,
2499f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            amplification);
2500f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2501f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return amplification;
2502f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2503f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2504cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
2505e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    AudioPolicyManagerBase::sDefaultVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
2506e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    {1, -49.5f}, {33, -33.5f}, {66, -17.0f}, {100, 0.0f}
2507e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent};
2508e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2509e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
2510e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    AudioPolicyManagerBase::sDefaultMediaVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
2511e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    {1, -58.0f}, {20, -40.0f}, {60, -17.0f}, {100, 0.0f}
2512cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent};
2513cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent
2514e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
2515e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    AudioPolicyManagerBase::sSpeakerMediaVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
2516e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    {1, -56.0f}, {20, -34.0f}, {60, -11.0f}, {100, 0.0f}
2517e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent};
2518e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2519e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
2520e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    AudioPolicyManagerBase::sSpeakerSonificationVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
2521e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    {1, -29.7f}, {33, -20.1f}, {66, -10.2f}, {100, 0.0f}
2522e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent};
2523e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2524ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent// AUDIO_STREAM_SYSTEM, AUDIO_STREAM_ENFORCED_AUDIBLE and AUDIO_STREAM_DTMF volume tracks
2525ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent// AUDIO_STREAM_RING on phones and AUDIO_STREAM_MUSIC on tablets (See AudioService.java).
25267465678e0d5711ebcd78ae47b3a76821534a23eaEric Laurent// The range is constrained between -30dB and -12dB over speaker and -30dB and -18dB over headset.
2527ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
2528ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    AudioPolicyManagerBase::sDefaultSystemVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
25297465678e0d5711ebcd78ae47b3a76821534a23eaEric Laurent    {1, -30.0f}, {33, -24.0f}, {66, -18.0f}, {100, -12.0f}
2530ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent};
2531ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent
2532ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
2533ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    AudioPolicyManagerBase::sHeadsetSystemVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
25347465678e0d5711ebcd78ae47b3a76821534a23eaEric Laurent    {1, -30.0f}, {33, -26.0f}, {66, -22.0f}, {100, -18.0f}
2535ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent};
2536e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2537e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
2538ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent            *AudioPolicyManagerBase::sVolumeProfiles[AUDIO_STREAM_CNT]
2539e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent                                                   [AudioPolicyManagerBase::DEVICE_CATEGORY_CNT] = {
2540ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_VOICE_CALL
2541e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
2542e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sDefaultVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2543e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sDefaultVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2544e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    },
2545ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_SYSTEM
2546ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET
2547ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2548ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultSystemVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2549ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    },
2550ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_RING
2551e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
2552e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2553e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sDefaultVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2554e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    },
2555ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_MUSIC
2556ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultMediaVolumeCurve, // DEVICE_CATEGORY_HEADSET
2557ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sSpeakerMediaVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2558ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultMediaVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2559ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    },
2560ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_ALARM
256112bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
256212bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER
256312bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        sDefaultVolumeCurve  // DEVICE_CATEGORY_EARPIECE
256412bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    },
2565ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_NOTIFICATION
2566e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
2567ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2568e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sDefaultVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2569c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    },
2570ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_BLUETOOTH_SCO
2571c16ac09f510437e8340be691720177a490ae78f0Eric Laurent        sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
2572ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2573c16ac09f510437e8340be691720177a490ae78f0Eric Laurent        sDefaultVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2574c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    },
2575ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_ENFORCED_AUDIBLE
2576ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET
2577ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2578ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultSystemVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2579ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    },
2580ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    {  // AUDIO_STREAM_DTMF
2581ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET
2582ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2583ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultSystemVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2584ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    },
2585ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_TTS
2586ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultMediaVolumeCurve, // DEVICE_CATEGORY_HEADSET
2587ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sSpeakerMediaVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2588ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultMediaVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2589ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    },
2590e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent};
2591e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2592e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurentvoid AudioPolicyManagerBase::initializeVolumeCurves()
2593e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent{
2594ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    for (int i = 0; i < AUDIO_STREAM_CNT; i++) {
2595e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) {
2596cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent            mStreams[i].mVolumeCurve[j] =
2597ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent                    sVolumeProfiles[i][j];
2598cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent        }
2599cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent    }
2600f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2601f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2602c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentfloat AudioPolicyManagerBase::computeVolume(int stream,
2603c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                            int index,
2604c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                            audio_io_handle_t output,
2605f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                                            audio_devices_t device)
2606f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2607f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    float volume = 1.0;
2608f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
2609f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    StreamDescriptor &streamDesc = mStreams[stream];
2610f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2611f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (device == 0) {
2612f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device = outputDesc->device();
2613f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2614f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2615f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if volume is not 0 (not muted), force media volume to max on digital output
2616f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (stream == AudioSystem::MUSIC &&
2617f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        index != mStreams[stream].mIndexMin &&
2618f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent        (device == AUDIO_DEVICE_OUT_AUX_DIGITAL ||
2619599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent         device == AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET ||
2620599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent         device == AUDIO_DEVICE_OUT_USB_ACCESSORY ||
2621599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent         device == AUDIO_DEVICE_OUT_USB_DEVICE)) {
2622f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return 1.0;
2623f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2624f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2625f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    volume = volIndexToAmpl(device, streamDesc, index);
2626f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2627f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if a headset is connected, apply the following rules to ring tones and notifications
2628f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // to avoid sound level bursts in user's ears:
2629f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // - always attenuate ring tones and notifications volume by 6dB
2630f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // - if music is playing, always limit the volume to current music volume,
2631f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // with a minimum threshold at -36dB so that notification is always perceived.
263212bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    const routing_strategy stream_strategy = getStrategy((AudioSystem::stream_type)stream);
263312bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    if ((device & (AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP |
263412bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi            AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
263512bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi            AudioSystem::DEVICE_OUT_WIRED_HEADSET |
263612bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi            AudioSystem::DEVICE_OUT_WIRED_HEADPHONE)) &&
263712bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        ((stream_strategy == STRATEGY_SONIFICATION)
263812bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi                || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL)
263912bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi                || (stream == AudioSystem::SYSTEM)) &&
2640f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        streamDesc.mCanBeMuted) {
2641f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        volume *= SONIFICATION_HEADSET_VOLUME_FACTOR;
2642f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // when the phone is ringing we must consider that music could have been paused just before
2643f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // by the music application and behave as if music was active if the last music track was
2644f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // just stopped
2645f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (outputDesc->mRefCount[AudioSystem::MUSIC] || mLimitRingtoneVolume) {
2646c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            float musicVol = computeVolume(AudioSystem::MUSIC,
2647f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                               mStreams[AudioSystem::MUSIC].getVolumeIndex(device),
2648c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                               output,
2649f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                               device);
2650c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            float minVol = (musicVol > SONIFICATION_HEADSET_VOLUME_MIN) ?
2651c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                musicVol : SONIFICATION_HEADSET_VOLUME_MIN;
2652f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (volume > minVol) {
2653f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                volume = minVol;
26546a70518b93928d1c91457ff805e375c82d76b0e5Steve Block                ALOGV("computeVolume limiting volume to %f musicVol %f", minVol, musicVol);
2655f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2656f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2657f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2658f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2659f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return volume;
2660f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2661f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2662c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentstatus_t AudioPolicyManagerBase::checkAndSetVolume(int stream,
2663c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                   int index,
2664c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                   audio_io_handle_t output,
2665f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                                                   audio_devices_t device,
2666c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                   int delayMs,
2667c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                   bool force)
2668f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2669f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2670f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // do not change actual stream volume if the stream is muted
2671f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mOutputs.valueFor(output)->mMuteCount[stream] != 0) {
26723cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        ALOGVV("checkAndSetVolume() stream %d muted count %d",
26735ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent              stream, mOutputs.valueFor(output)->mMuteCount[stream]);
2674f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return NO_ERROR;
2675f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2676f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2677f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // do not change in call volume if bluetooth is connected and vice versa
2678f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if ((stream == AudioSystem::VOICE_CALL && mForceUse[AudioSystem::FOR_COMMUNICATION] == AudioSystem::FORCE_BT_SCO) ||
2679f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        (stream == AudioSystem::BLUETOOTH_SCO && mForceUse[AudioSystem::FOR_COMMUNICATION] != AudioSystem::FORCE_BT_SCO)) {
26806a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
2681f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin             stream, mForceUse[AudioSystem::FOR_COMMUNICATION]);
2682f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return INVALID_OPERATION;
2683f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2684f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2685f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    float volume = computeVolume(stream, index, output, device);
2686f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // We actually change the volume if:
2687f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // - the float value returned by computeVolume() changed
2688f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // - the force flag is set
2689f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (volume != mOutputs.valueFor(output)->mCurVolume[stream] ||
2690f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            force) {
2691f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mOutputs.valueFor(output)->mCurVolume[stream] = volume;
26923cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        ALOGVV("checkAndSetVolume() for output %d stream %d, volume %f, delay %d", output, stream, volume, delayMs);
2693f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (stream == AudioSystem::VOICE_CALL ||
2694f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            stream == AudioSystem::DTMF ||
2695f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            stream == AudioSystem::BLUETOOTH_SCO) {
2696f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // offset value to reflect actual hardware volume that never reaches 0
2697f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // 1% corresponds roughly to first step in VOICE_CALL stream volume setting (see AudioService.java)
2698f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            volume = 0.01 + 0.99 * volume;
2699f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // Force VOICE_CALL to track BLUETOOTH_SCO stream volume when bluetooth audio is
2700f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // enabled
2701f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (stream == AudioSystem::BLUETOOTH_SCO) {
2702f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mpClientInterface->setStreamVolume(AudioSystem::VOICE_CALL, volume, output, delayMs);
2703f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2704f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2705f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2706f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mpClientInterface->setStreamVolume((AudioSystem::stream_type)stream, volume, output, delayMs);
2707f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2708f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2709f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (stream == AudioSystem::VOICE_CALL ||
2710f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        stream == AudioSystem::BLUETOOTH_SCO) {
2711f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        float voiceVolume;
2712f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // Force voice volume to max for bluetooth SCO as volume is managed by the headset
2713f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (stream == AudioSystem::VOICE_CALL) {
2714f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            voiceVolume = (float)index/(float)mStreams[stream].mIndexMax;
2715f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        } else {
2716f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            voiceVolume = 1.0;
2717f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2718f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2719b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        if (voiceVolume != mLastVoiceVolume && output == mPrimaryOutput) {
2720f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
2721f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mLastVoiceVolume = voiceVolume;
2722f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2723f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2724f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2725f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
2726f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2727f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2728c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentvoid AudioPolicyManagerBase::applyStreamVolumes(audio_io_handle_t output,
2729f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                                                audio_devices_t device,
2730c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                int delayMs,
2731c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                bool force)
2732f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
27333cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGVV("applyStreamVolumes() for output %d and device %x", output, device);
2734f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2735f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
2736c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        checkAndSetVolume(stream,
2737f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                          mStreams[stream].getVolumeIndex(device),
2738c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                          output,
2739c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                          device,
2740c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                          delayMs,
2741c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                          force);
2742f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2743f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2744f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
274501e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurentvoid AudioPolicyManagerBase::setStrategyMute(routing_strategy strategy,
274601e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                             bool on,
274701e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                             audio_io_handle_t output,
274801e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                             int delayMs,
274901e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                             audio_devices_t device)
2750f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
27513cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGVV("setStrategyMute() strategy %d, mute %d, output %d", strategy, on, output);
2752f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
2753f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (getStrategy((AudioSystem::stream_type)stream) == strategy) {
275401e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent            setStreamMute(stream, on, output, delayMs, device);
2755f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2756f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2757f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2758f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
275901e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurentvoid AudioPolicyManagerBase::setStreamMute(int stream,
276001e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                           bool on,
276101e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                           audio_io_handle_t output,
276201e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                           int delayMs,
276301e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                           audio_devices_t device)
2764f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2765f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    StreamDescriptor &streamDesc = mStreams[stream];
2766f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
276701e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent    if (device == 0) {
276801e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent        device = outputDesc->device();
276901e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent    }
2770f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
27713cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGVV("setStreamMute() stream %d, mute %d, output %d, mMuteCount %d device %04x",
277201e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent          stream, on, output, outputDesc->mMuteCount[stream], device);
2773f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2774f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (on) {
2775f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (outputDesc->mMuteCount[stream] == 0) {
2776f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (streamDesc.mCanBeMuted) {
2777c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                checkAndSetVolume(stream, 0, output, device, delayMs);
2778f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2779f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2780f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
2781f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mMuteCount[stream]++;
2782f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
2783f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (outputDesc->mMuteCount[stream] == 0) {
27843cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            ALOGV("setStreamMute() unmuting non muted stream!");
2785f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return;
2786f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2787f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (--outputDesc->mMuteCount[stream] == 0) {
2788c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            checkAndSetVolume(stream,
2789c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                              streamDesc.getVolumeIndex((audio_devices_t)device),
2790c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                              output,
2791c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                              device,
2792c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                              delayMs);
2793f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2794f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2795f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2796f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2797f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::handleIncallSonification(int stream, bool starting, bool stateChange)
2798f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2799f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if the stream pertains to sonification strategy and we are in call we must
2800f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // mute the stream if it is low visibility. If it is high visibility, we must play a tone
2801f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // in the device used for phone strategy and play the tone if the selected device does not
2802f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // interfere with the device used for phone strategy
2803f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
2804f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // many times as there are active tracks on the output
280512bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    const routing_strategy stream_strategy = getStrategy((AudioSystem::stream_type)stream);
280612bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    if ((stream_strategy == STRATEGY_SONIFICATION) ||
280712bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi            ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
2808b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        AudioOutputDescriptor *outputDesc = mOutputs.valueFor(mPrimaryOutput);
28096a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
2810f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                stream, starting, outputDesc->mDevice, stateChange);
2811f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (outputDesc->mRefCount[stream]) {
2812f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            int muteCount = 1;
2813f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (stateChange) {
2814f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                muteCount = outputDesc->mRefCount[stream];
2815f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2816f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (AudioSystem::isLowVisibility((AudioSystem::stream_type)stream)) {
28176a70518b93928d1c91457ff805e375c82d76b0e5Steve Block                ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
2818f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                for (int i = 0; i < muteCount; i++) {
2819b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                    setStreamMute(stream, starting, mPrimaryOutput);
2820f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
2821f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            } else {
28226a70518b93928d1c91457ff805e375c82d76b0e5Steve Block                ALOGV("handleIncallSonification() high visibility");
28235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                if (outputDesc->device() &
28245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                        getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
28256a70518b93928d1c91457ff805e375c82d76b0e5Steve Block                    ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
2826f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    for (int i = 0; i < muteCount; i++) {
2827b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                        setStreamMute(stream, starting, mPrimaryOutput);
2828f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    }
2829f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
2830f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (starting) {
2831f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    mpClientInterface->startTone(ToneGenerator::TONE_SUP_CALL_WAITING, AudioSystem::VOICE_CALL);
2832f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else {
2833f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    mpClientInterface->stopTone();
2834f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
2835f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2836f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2837f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2838f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2839f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2840f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinbool AudioPolicyManagerBase::isInCall()
2841f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2842f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return isStateInCall(mPhoneState);
2843f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2844f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2845f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinbool AudioPolicyManagerBase::isStateInCall(int state) {
2846f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return ((state == AudioSystem::MODE_IN_CALL) ||
2847f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            (state == AudioSystem::MODE_IN_COMMUNICATION));
2848f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2849f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
285070c236c9290732782d5267935af1475b8d5ae602Eric Laurentbool AudioPolicyManagerBase::needsDirectOuput(audio_stream_type_t stream,
285170c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                              uint32_t samplingRate,
285270c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                              audio_format_t format,
285370c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                              audio_channel_mask_t channelMask,
28540977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent                                              audio_output_flags_t flags,
285570c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                              audio_devices_t device)
2856f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2857f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin   return ((flags & AudioSystem::OUTPUT_FLAG_DIRECT) ||
2858b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent          (format != 0 && !AudioSystem::isLinearPCM(format)));
2859f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2860f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2861f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinuint32_t AudioPolicyManagerBase::getMaxEffectsCpuLoad()
2862f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2863f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return MAX_EFFECTS_CPU_LOAD;
2864f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2865f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2866f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinuint32_t AudioPolicyManagerBase::getMaxEffectsMemory()
2867f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2868f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return MAX_EFFECTS_MEMORY;
2869f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2870f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2871f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// --- AudioOutputDescriptor class implementation
2872f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2873b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric LaurentAudioPolicyManagerBase::AudioOutputDescriptor::AudioOutputDescriptor(
28745ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        const IOProfile *profile)
287570c236c9290732782d5267935af1475b8d5ae602Eric Laurent    : mId(0), mSamplingRate(0), mFormat((audio_format_t)0),
287670c236c9290732782d5267935af1475b8d5ae602Eric Laurent      mChannelMask((audio_channel_mask_t)0), mLatency(0),
28770977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent    mFlags((audio_output_flags_t)0), mDevice((audio_devices_t)0),
2878f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent    mOutput1(0), mOutput2(0), mProfile(profile)
2879f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2880f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // clear usage count for all stream types
2881f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) {
2882f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mRefCount[i] = 0;
2883f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mCurVolume[i] = -1.0;
2884f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mMuteCount[i] = 0;
2885f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mStopTime[i] = 0;
2886f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
28873cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    if (profile != NULL) {
28883cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        mSamplingRate = profile->mSamplingRates[0];
28893cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        mFormat = profile->mFormats[0];
28903cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        mChannelMask = profile->mChannelMasks[0];
28913cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        mFlags = profile->mFlags;
28923cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    }
2893f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2894f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2895f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurentaudio_devices_t AudioPolicyManagerBase::AudioOutputDescriptor::device()
2896f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2897f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (isDuplicated()) {
2898f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent        return (audio_devices_t)(mOutput1->mDevice | mOutput2->mDevice);
2899f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
2900f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent        return mDevice;
2901f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2902f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2903f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
29045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentuint32_t AudioPolicyManagerBase::AudioOutputDescriptor::latency()
29055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
29065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (isDuplicated()) {
29075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return (mOutput1->mLatency > mOutput2->mLatency) ? mOutput1->mLatency : mOutput2->mLatency;
29085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    } else {
29095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return mLatency;
29105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
29115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
29125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
29135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentbool AudioPolicyManagerBase::AudioOutputDescriptor::sharesHwModuleWith(
29145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        const AudioOutputDescriptor *outputDesc)
29155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
29165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (isDuplicated()) {
29175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return mOutput1->sharesHwModuleWith(outputDesc) || mOutput2->sharesHwModuleWith(outputDesc);
29185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    } else if (outputDesc->isDuplicated()){
29195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return sharesHwModuleWith(outputDesc->mOutput1) || sharesHwModuleWith(outputDesc->mOutput2);
29205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    } else {
292170c236c9290732782d5267935af1475b8d5ae602Eric Laurent        return (mProfile->mModule == outputDesc->mProfile->mModule);
29225ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
29235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
29245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
2925f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::AudioOutputDescriptor::changeRefCount(AudioSystem::stream_type stream, int delta)
2926f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2927f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // forward usage count change to attached outputs
2928f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (isDuplicated()) {
2929f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mOutput1->changeRefCount(stream, delta);
2930f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mOutput2->changeRefCount(stream, delta);
2931f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2932f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if ((delta + (int)mRefCount[stream]) < 0) {
293364cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("changeRefCount() invalid delta %d for stream %d, refCount %d", delta, stream, mRefCount[stream]);
2934f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mRefCount[stream] = 0;
2935f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
2936f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2937f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mRefCount[stream] += delta;
29386a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("changeRefCount() stream %d, count %d", stream, mRefCount[stream]);
2939f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2940f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2941f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinuint32_t AudioPolicyManagerBase::AudioOutputDescriptor::refCount()
2942f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2943f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    uint32_t refcount = 0;
2944f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) {
2945f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        refcount += mRefCount[i];
2946f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2947f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return refcount;
2948f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2949f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2950f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinuint32_t AudioPolicyManagerBase::AudioOutputDescriptor::strategyRefCount(routing_strategy strategy)
2951f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2952f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    uint32_t refCount = 0;
2953f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) {
2954f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (getStrategy((AudioSystem::stream_type)i) == strategy) {
2955f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            refCount += mRefCount[i];
2956f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2957f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2958f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return refCount;
2959f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2960f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2961f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurentaudio_devices_t AudioPolicyManagerBase::AudioOutputDescriptor::supportedDevices()
2962b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent{
2963b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (isDuplicated()) {
2964f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent        return (audio_devices_t)(mOutput1->supportedDevices() | mOutput2->supportedDevices());
2965b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    } else {
2966b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        return mProfile->mSupportedDevices ;
2967b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    }
2968b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent}
2969b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
2970f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::AudioOutputDescriptor::dump(int fd)
2971f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2972f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    const size_t SIZE = 256;
2973f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    char buffer[SIZE];
2974f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    String8 result;
2975f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2976f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Sampling rate: %d\n", mSamplingRate);
2977f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
2978f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Format: %d\n", mFormat);
2979f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
298070c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, " Channels: %08x\n", mChannelMask);
2981f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
2982f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Latency: %d\n", mLatency);
2983f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
2984f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Flags %08x\n", mFlags);
2985f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
2986f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Devices %08x\n", device());
2987f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
2988f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Stream volume refCount muteCount\n");
2989f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
2990f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) {
2991f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        snprintf(buffer, SIZE, " %02d     %.03f     %02d       %02d\n", i, mCurVolume[i], mRefCount[i], mMuteCount[i]);
2992f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        result.append(buffer);
2993f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2994f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, result.string(), result.size());
2995f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2996f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
2997f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2998f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2999f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// --- AudioInputDescriptor class implementation
3000f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
30015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric LaurentAudioPolicyManagerBase::AudioInputDescriptor::AudioInputDescriptor(const IOProfile *profile)
300270c236c9290732782d5267935af1475b8d5ae602Eric Laurent    : mSamplingRate(0), mFormat((audio_format_t)0), mChannelMask((audio_channel_mask_t)0),
300370c236c9290732782d5267935af1475b8d5ae602Eric Laurent      mDevice((audio_devices_t)0), mRefCount(0),
30045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent      mInputSource(0), mProfile(profile)
3005f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3006f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3007f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3008f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::AudioInputDescriptor::dump(int fd)
3009f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3010f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    const size_t SIZE = 256;
3011f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    char buffer[SIZE];
3012f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    String8 result;
3013f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3014f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Sampling rate: %d\n", mSamplingRate);
3015f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3016f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Format: %d\n", mFormat);
3017f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
301870c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, " Channels: %08x\n", mChannelMask);
3019f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3020f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Devices %08x\n", mDevice);
3021f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3022f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Ref Count %d\n", mRefCount);
3023f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3024f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, result.string(), result.size());
3025f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3026f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
3027f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3028f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3029f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// --- StreamDescriptor class implementation
3030f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3031c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric LaurentAudioPolicyManagerBase::StreamDescriptor::StreamDescriptor()
3032c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    :   mIndexMin(0), mIndexMax(1), mCanBeMuted(true)
3033c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent{
3034c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    mIndexCur.add(AUDIO_DEVICE_OUT_DEFAULT, 0);
3035c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent}
3036c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
3037c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentint AudioPolicyManagerBase::StreamDescriptor::getVolumeIndex(audio_devices_t device)
3038c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent{
3039c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    device = AudioPolicyManagerBase::getDeviceForVolume(device);
3040c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    // there is always a valid entry for AUDIO_DEVICE_OUT_DEFAULT
3041c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    if (mIndexCur.indexOfKey(device) < 0) {
3042c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        device = AUDIO_DEVICE_OUT_DEFAULT;
3043c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    }
3044c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    return mIndexCur.valueFor(device);
3045c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent}
3046c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
3047c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentvoid AudioPolicyManagerBase::StreamDescriptor::dump(int fd)
3048f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3049c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    const size_t SIZE = 256;
3050c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    char buffer[SIZE];
3051c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    String8 result;
3052c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
3053c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    snprintf(buffer, SIZE, "%s         %02d         %02d         ",
3054c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent             mCanBeMuted ? "true " : "false", mIndexMin, mIndexMax);
3055c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    result.append(buffer);
3056c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    for (size_t i = 0; i < mIndexCur.size(); i++) {
3057c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        snprintf(buffer, SIZE, "%04x : %02d, ",
3058c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                 mIndexCur.keyAt(i),
3059c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                 mIndexCur.valueAt(i));
3060c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        result.append(buffer);
3061c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    }
3062c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    result.append("\n");
3063c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
3064c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    write(fd, result.string(), result.size());
3065f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3066f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3067f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// --- EffectDescriptor class implementation
3068f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3069f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::EffectDescriptor::dump(int fd)
3070f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3071f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    const size_t SIZE = 256;
3072f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    char buffer[SIZE];
3073f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    String8 result;
3074f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
30751c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent    snprintf(buffer, SIZE, " I/O: %d\n", mIo);
3076f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3077f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Strategy: %d\n", mStrategy);
3078f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3079f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Session: %d\n", mSession);
3080f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3081f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Name: %s\n",  mDesc.name);
3082f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3083582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    snprintf(buffer, SIZE, " %s\n",  mEnabled ? "Enabled" : "Disabled");
3084582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    result.append(buffer);
3085f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, result.string(), result.size());
3086f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3087f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
3088f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3089f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
30905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent// --- IOProfile class implementation
30915ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
309270c236c9290732782d5267935af1475b8d5ae602Eric LaurentAudioPolicyManagerBase::HwModule::HwModule(const char *name)
309370c236c9290732782d5267935af1475b8d5ae602Eric Laurent    : mName(strndup(name, AUDIO_HARDWARE_MODULE_ID_MAX_LEN)), mHandle(0)
309470c236c9290732782d5267935af1475b8d5ae602Eric Laurent{
309570c236c9290732782d5267935af1475b8d5ae602Eric Laurent}
309670c236c9290732782d5267935af1475b8d5ae602Eric Laurent
309770c236c9290732782d5267935af1475b8d5ae602Eric LaurentAudioPolicyManagerBase::HwModule::~HwModule()
309870c236c9290732782d5267935af1475b8d5ae602Eric Laurent{
309970c236c9290732782d5267935af1475b8d5ae602Eric Laurent    for (size_t i = 0; i < mOutputProfiles.size(); i++) {
310070c236c9290732782d5267935af1475b8d5ae602Eric Laurent         delete mOutputProfiles[i];
310170c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
310270c236c9290732782d5267935af1475b8d5ae602Eric Laurent    for (size_t i = 0; i < mInputProfiles.size(); i++) {
310370c236c9290732782d5267935af1475b8d5ae602Eric Laurent         delete mInputProfiles[i];
310470c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
310570c236c9290732782d5267935af1475b8d5ae602Eric Laurent    free((void *)mName);
310670c236c9290732782d5267935af1475b8d5ae602Eric Laurent}
310770c236c9290732782d5267935af1475b8d5ae602Eric Laurent
310870c236c9290732782d5267935af1475b8d5ae602Eric Laurentvoid AudioPolicyManagerBase::HwModule::dump(int fd)
310970c236c9290732782d5267935af1475b8d5ae602Eric Laurent{
311070c236c9290732782d5267935af1475b8d5ae602Eric Laurent    const size_t SIZE = 256;
311170c236c9290732782d5267935af1475b8d5ae602Eric Laurent    char buffer[SIZE];
311270c236c9290732782d5267935af1475b8d5ae602Eric Laurent    String8 result;
311370c236c9290732782d5267935af1475b8d5ae602Eric Laurent
311470c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "  - name: %s\n", mName);
311570c236c9290732782d5267935af1475b8d5ae602Eric Laurent    result.append(buffer);
311670c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "  - handle: %d\n", mHandle);
311770c236c9290732782d5267935af1475b8d5ae602Eric Laurent    result.append(buffer);
311870c236c9290732782d5267935af1475b8d5ae602Eric Laurent    write(fd, result.string(), result.size());
311970c236c9290732782d5267935af1475b8d5ae602Eric Laurent    if (mOutputProfiles.size()) {
312070c236c9290732782d5267935af1475b8d5ae602Eric Laurent        write(fd, "  - outputs:\n", sizeof("  - outputs:\n"));
312170c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (size_t i = 0; i < mOutputProfiles.size(); i++) {
3122599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            snprintf(buffer, SIZE, "    output %d:\n", i);
3123599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            write(fd, buffer, strlen(buffer));
312470c236c9290732782d5267935af1475b8d5ae602Eric Laurent            mOutputProfiles[i]->dump(fd);
312570c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
312670c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
312770c236c9290732782d5267935af1475b8d5ae602Eric Laurent    if (mInputProfiles.size()) {
312870c236c9290732782d5267935af1475b8d5ae602Eric Laurent        write(fd, "  - inputs:\n", sizeof("  - inputs:\n"));
312970c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (size_t i = 0; i < mInputProfiles.size(); i++) {
3130599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            snprintf(buffer, SIZE, "    input %d:\n", i);
3131599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            write(fd, buffer, strlen(buffer));
313270c236c9290732782d5267935af1475b8d5ae602Eric Laurent            mInputProfiles[i]->dump(fd);
313370c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
313470c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
313570c236c9290732782d5267935af1475b8d5ae602Eric Laurent}
313670c236c9290732782d5267935af1475b8d5ae602Eric Laurent
313770c236c9290732782d5267935af1475b8d5ae602Eric LaurentAudioPolicyManagerBase::IOProfile::IOProfile(HwModule *module)
31380977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent    : mFlags((audio_output_flags_t)0), mModule(module)
31395ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
31405ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
31415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
31425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric LaurentAudioPolicyManagerBase::IOProfile::~IOProfile()
31435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
314470c236c9290732782d5267935af1475b8d5ae602Eric Laurent}
314570c236c9290732782d5267935af1475b8d5ae602Eric Laurent
314670c236c9290732782d5267935af1475b8d5ae602Eric Laurent// checks if the IO profile is compatible with specified parameters. By convention a value of 0
314770c236c9290732782d5267935af1475b8d5ae602Eric Laurent// means a parameter is don't care
314870c236c9290732782d5267935af1475b8d5ae602Eric Laurentbool AudioPolicyManagerBase::IOProfile::isCompatibleProfile(audio_devices_t device,
314970c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                            uint32_t samplingRate,
315070c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                            uint32_t format,
315170c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                            uint32_t channelMask,
31520977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent                                                            audio_output_flags_t flags) const
315370c236c9290732782d5267935af1475b8d5ae602Eric Laurent{
315470c236c9290732782d5267935af1475b8d5ae602Eric Laurent    if ((mSupportedDevices & device) != device) {
315570c236c9290732782d5267935af1475b8d5ae602Eric Laurent        return false;
315670c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
315770c236c9290732782d5267935af1475b8d5ae602Eric Laurent    if ((mFlags & flags) != flags) {
315870c236c9290732782d5267935af1475b8d5ae602Eric Laurent        return false;
315970c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
316070c236c9290732782d5267935af1475b8d5ae602Eric Laurent    if (samplingRate != 0) {
316170c236c9290732782d5267935af1475b8d5ae602Eric Laurent        size_t i;
316270c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (i = 0; i < mSamplingRates.size(); i++)
316370c236c9290732782d5267935af1475b8d5ae602Eric Laurent        {
316470c236c9290732782d5267935af1475b8d5ae602Eric Laurent            if (mSamplingRates[i] == samplingRate) {
316570c236c9290732782d5267935af1475b8d5ae602Eric Laurent                break;
316670c236c9290732782d5267935af1475b8d5ae602Eric Laurent            }
316770c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
316870c236c9290732782d5267935af1475b8d5ae602Eric Laurent        if (i == mSamplingRates.size()) {
316970c236c9290732782d5267935af1475b8d5ae602Eric Laurent            return false;
317070c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
317170c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
317270c236c9290732782d5267935af1475b8d5ae602Eric Laurent    if (format != 0) {
317370c236c9290732782d5267935af1475b8d5ae602Eric Laurent        size_t i;
317470c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (i = 0; i < mFormats.size(); i++)
317570c236c9290732782d5267935af1475b8d5ae602Eric Laurent        {
317670c236c9290732782d5267935af1475b8d5ae602Eric Laurent            if (mFormats[i] == format) {
317770c236c9290732782d5267935af1475b8d5ae602Eric Laurent                break;
317870c236c9290732782d5267935af1475b8d5ae602Eric Laurent            }
317970c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
318070c236c9290732782d5267935af1475b8d5ae602Eric Laurent        if (i == mFormats.size()) {
318170c236c9290732782d5267935af1475b8d5ae602Eric Laurent            return false;
318270c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
318370c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
318470c236c9290732782d5267935af1475b8d5ae602Eric Laurent    if (channelMask != 0) {
318570c236c9290732782d5267935af1475b8d5ae602Eric Laurent        size_t i;
318670c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (i = 0; i < mChannelMasks.size(); i++)
318770c236c9290732782d5267935af1475b8d5ae602Eric Laurent        {
318870c236c9290732782d5267935af1475b8d5ae602Eric Laurent            if (mChannelMasks[i] == channelMask) {
318970c236c9290732782d5267935af1475b8d5ae602Eric Laurent                break;
319070c236c9290732782d5267935af1475b8d5ae602Eric Laurent            }
319170c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
319270c236c9290732782d5267935af1475b8d5ae602Eric Laurent        if (i == mChannelMasks.size()) {
319370c236c9290732782d5267935af1475b8d5ae602Eric Laurent            return false;
319470c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
319570c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
319670c236c9290732782d5267935af1475b8d5ae602Eric Laurent    return true;
31975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
31985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
31995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::IOProfile::dump(int fd)
32005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
32015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    const size_t SIZE = 256;
32025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    char buffer[SIZE];
32035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    String8 result;
32045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
320570c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "    - sampling rates: ");
32065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    result.append(buffer);
32075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < mSamplingRates.size(); i++) {
32085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        snprintf(buffer, SIZE, "%d", mSamplingRates[i]);
32095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        result.append(buffer);
32105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        result.append(i == (mSamplingRates.size() - 1) ? "\n" : ", ");
32115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
32125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
321370c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "    - channel masks: ");
32145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    result.append(buffer);
32155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < mChannelMasks.size(); i++) {
32165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        snprintf(buffer, SIZE, "%04x", mChannelMasks[i]);
32175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        result.append(buffer);
32185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        result.append(i == (mChannelMasks.size() - 1) ? "\n" : ", ");
32195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
32205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
322170c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "    - formats: ");
32225ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    result.append(buffer);
32235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < mFormats.size(); i++) {
32245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        snprintf(buffer, SIZE, "%d", mFormats[i]);
32255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        result.append(buffer);
32265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        result.append(i == (mFormats.size() - 1) ? "\n" : ", ");
32275ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
32285ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
322970c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "    - devices: %04x\n", mSupportedDevices);
32305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    result.append(buffer);
323170c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "    - flags: %04x\n", mFlags);
32325ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    result.append(buffer);
32335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
32345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    write(fd, result.string(), result.size());
32355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
32365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
32375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent// --- audio_policy.conf file parsing
32385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
32395ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentstruct StringToEnum {
32405ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    const char *name;
32415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    uint32_t value;
32425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent};
32435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
32445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent#define STRING_TO_ENUM(string) { #string, string }
32455ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
32465ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
32475ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentconst struct StringToEnum sDeviceNameToEnumTable[] = {
32485ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_EARPIECE),
32495ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_SPEAKER),
32505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_WIRED_HEADSET),
32515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_WIRED_HEADPHONE),
32525ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_ALL_SCO),
32535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_ALL_A2DP),
32545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_AUX_DIGITAL),
32555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET),
3256599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET),
3257599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_USB_DEVICE),
3258599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_USB_ACCESSORY),
3259599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_ALL_USB),
32605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_BUILTIN_MIC),
32615ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET),
32625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_WIRED_HEADSET),
32635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_AUX_DIGITAL),
32645ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_VOICE_CALL),
32655ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_BACK_MIC),
32665ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent};
32675ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
32685ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentconst struct StringToEnum sFlagNameToEnumTable[] = {
32690977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent    STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT),
32700977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent    STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY),
3271b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent    STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST),
3272b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent    STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER),
32735ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent};
32745ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
32755ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentconst struct StringToEnum sFormatNameToEnumTable[] = {
32765ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
32775ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
32785ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_FORMAT_MP3),
32795ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_FORMAT_AAC),
32805ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
32815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent};
32825ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
32835ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentconst struct StringToEnum sOutChannelsNameToEnumTable[] = {
32845ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_OUT_MONO),
32855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
32865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
32875ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
32885ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent};
32895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
32905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentconst struct StringToEnum sInChannelsNameToEnumTable[] = {
32915ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_IN_MONO),
32925ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_IN_STEREO),
32935ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent};
32945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
32955ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
32965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentuint32_t AudioPolicyManagerBase::stringToEnum(const struct StringToEnum *table,
32975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                              size_t size,
32985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                              const char *name)
32995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
33005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < size; i++) {
33015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strcmp(table[i].name, name) == 0) {
33025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("stringToEnum() found %s", table[i].name);
33035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            return table[i].value;
33045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
33055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
33065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return 0;
33075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
33085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
33090977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurentaudio_output_flags_t AudioPolicyManagerBase::parseFlagNames(char *name)
33105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
33115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    uint32_t flag = 0;
33125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
33135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // it is OK to cast name to non const here as we are not going to use it after
33145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // strtok() modifies it
33155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    char *flagName = strtok(name, "|");
33165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (flagName != NULL) {
33175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strlen(flagName) != 0) {
33185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            flag |= stringToEnum(sFlagNameToEnumTable,
33195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                               ARRAY_SIZE(sFlagNameToEnumTable),
33205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                               flagName);
33215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
33225ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        flagName = strtok(NULL, "|");
33235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
33240977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent    return (audio_output_flags_t)flag;
33255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
33265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
33275ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentaudio_devices_t AudioPolicyManagerBase::parseDeviceNames(char *name)
33285ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
33295ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    uint32_t device = 0;
33305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
33315ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    char *devName = strtok(name, "|");
33325ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (devName != NULL) {
33335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strlen(devName) != 0) {
33345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device |= stringToEnum(sDeviceNameToEnumTable,
33355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                 ARRAY_SIZE(sDeviceNameToEnumTable),
33365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                 devName);
33375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
33385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        devName = strtok(NULL, "|");
33395ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
33405ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return (audio_devices_t)device;
33415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
33425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
33435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadSamplingRates(char *name, IOProfile *profile)
33445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
33455ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    char *str = strtok(name, "|");
33465ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
33473cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    // by convention, "0' in the first entry in mSamplingRates indicates the supported sampling
33483cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    // rates should be read from the output stream after it is opened for the first time
33493cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) {
33503cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        profile->mSamplingRates.add(0);
33513cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        return;
33523cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    }
33533cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
33545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (str != NULL) {
33555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        uint32_t rate = atoi(str);
33565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (rate != 0) {
33575ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadSamplingRates() adding rate %d", rate);
33585ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mSamplingRates.add(rate);
33595ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
33605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        str = strtok(NULL, "|");
33615ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
33625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return;
33635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
33645ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
33655ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadFormats(char *name, IOProfile *profile)
33665ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
33675ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    char *str = strtok(name, "|");
33685ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
33693cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    // by convention, "0' in the first entry in mFormats indicates the supported formats
33703cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    // should be read from the output stream after it is opened for the first time
33713cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) {
33723cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        profile->mFormats.add((audio_format_t)0);
33733cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        return;
33743cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    }
33753cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
33765ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (str != NULL) {
33775ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_format_t format = (audio_format_t)stringToEnum(sFormatNameToEnumTable,
33785ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                             ARRAY_SIZE(sFormatNameToEnumTable),
33795ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                             str);
33805ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (format != 0) {
33815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mFormats.add(format);
33825ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
33835ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        str = strtok(NULL, "|");
33845ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
33855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return;
33865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
33875ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
33885ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadInChannels(char *name, IOProfile *profile)
33895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
33905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    const char *str = strtok(name, "|");
33915ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
33925ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGV("loadInChannels() %s", name);
33933cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
33943cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) {
33953cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        profile->mChannelMasks.add((audio_channel_mask_t)0);
33963cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        return;
33973cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    }
33983cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
33995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (str != NULL) {
34005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_channel_mask_t channelMask =
34015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                (audio_channel_mask_t)stringToEnum(sInChannelsNameToEnumTable,
34025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                   ARRAY_SIZE(sInChannelsNameToEnumTable),
34035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                   str);
34045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (channelMask != 0) {
34055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadInChannels() adding channelMask %04x", channelMask);
34065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mChannelMasks.add(channelMask);
34075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
34085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        str = strtok(NULL, "|");
34095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
34105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return;
34115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
34125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
34135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadOutChannels(char *name, IOProfile *profile)
34145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
34155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    const char *str = strtok(name, "|");
34163cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
34173cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGV("loadOutChannels() %s", name);
34183cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
34193cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    // by convention, "0' in the first entry in mChannelMasks indicates the supported channel
34203cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    // masks should be read from the output stream after it is opened for the first time
34213cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) {
34223cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        profile->mChannelMasks.add((audio_channel_mask_t)0);
34233cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        return;
34243cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    }
34255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
34265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (str != NULL) {
34273cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        audio_channel_mask_t channelMask =
34283cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                (audio_channel_mask_t)stringToEnum(sOutChannelsNameToEnumTable,
34293cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                   ARRAY_SIZE(sOutChannelsNameToEnumTable),
34303cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                   str);
34315ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (channelMask != 0) {
34325ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mChannelMasks.add(channelMask);
34335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
34345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        str = strtok(NULL, "|");
34355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
34365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return;
34375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
34385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
343970c236c9290732782d5267935af1475b8d5ae602Eric Laurentstatus_t AudioPolicyManagerBase::loadInput(cnode *root, HwModule *module)
34405ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
34415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    cnode *node = root->first_child;
34425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
34435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    IOProfile *profile = new IOProfile(module);
34445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
34455ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (node) {
34465ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
34475ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            loadSamplingRates((char *)node->value, profile);
34485ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, FORMATS_TAG) == 0) {
34495ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            loadFormats((char *)node->value, profile);
34505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, CHANNELS_TAG) == 0) {
34515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            loadInChannels((char *)node->value, profile);
34525ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, DEVICES_TAG) == 0) {
34535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mSupportedDevices = parseDeviceNames((char *)node->value);
34545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
34555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        node = node->next;
34565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
34575ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGW_IF(profile->mSupportedDevices == (audio_devices_t)0,
34585ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadInput() invalid supported devices");
34595ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGW_IF(profile->mChannelMasks.size() == 0,
34605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadInput() invalid supported channel masks");
34615ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGW_IF(profile->mSamplingRates.size() == 0,
34625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadInput() invalid supported sampling rates");
34635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGW_IF(profile->mFormats.size() == 0,
34645ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadInput() invalid supported formats");
34655ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if ((profile->mSupportedDevices != (audio_devices_t)0) &&
34665ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            (profile->mChannelMasks.size() != 0) &&
34675ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            (profile->mSamplingRates.size() != 0) &&
34685ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            (profile->mFormats.size() != 0)) {
34695ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
34705ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        ALOGV("loadInput() adding input mSupportedDevices %04x", profile->mSupportedDevices);
34715ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
347270c236c9290732782d5267935af1475b8d5ae602Eric Laurent        module->mInputProfiles.add(profile);
34735ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return NO_ERROR;
34745ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    } else {
34755ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        delete profile;
34765ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return BAD_VALUE;
34775ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
34785ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
34795ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
348070c236c9290732782d5267935af1475b8d5ae602Eric Laurentstatus_t AudioPolicyManagerBase::loadOutput(cnode *root, HwModule *module)
34815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
34825ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    cnode *node = root->first_child;
34835ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
34845ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    IOProfile *profile = new IOProfile(module);
34855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
34865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (node) {
34875ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
34885ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            loadSamplingRates((char *)node->value, profile);
34895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, FORMATS_TAG) == 0) {
34905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            loadFormats((char *)node->value, profile);
34915ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, CHANNELS_TAG) == 0) {
34925ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            loadOutChannels((char *)node->value, profile);
34935ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, DEVICES_TAG) == 0) {
34945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mSupportedDevices = parseDeviceNames((char *)node->value);
34955ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, FLAGS_TAG) == 0) {
34965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mFlags = parseFlagNames((char *)node->value);
34975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
34985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        node = node->next;
34995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
35005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGW_IF(profile->mSupportedDevices == (audio_devices_t)0,
35015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadOutput() invalid supported devices");
35025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGW_IF(profile->mChannelMasks.size() == 0,
35035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadOutput() invalid supported channel masks");
35045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGW_IF(profile->mSamplingRates.size() == 0,
35055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadOutput() invalid supported sampling rates");
35065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGW_IF(profile->mFormats.size() == 0,
35075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadOutput() invalid supported formats");
35085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if ((profile->mSupportedDevices != (audio_devices_t)0) &&
35095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            (profile->mChannelMasks.size() != 0) &&
35105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            (profile->mSamplingRates.size() != 0) &&
35115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            (profile->mFormats.size() != 0)) {
35125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
35135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        ALOGV("loadOutput() adding output mSupportedDevices %04x, mFlags %04x",
35145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent              profile->mSupportedDevices, profile->mFlags);
35155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
351670c236c9290732782d5267935af1475b8d5ae602Eric Laurent        module->mOutputProfiles.add(profile);
35175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return NO_ERROR;
35185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    } else {
35195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        delete profile;
35205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return BAD_VALUE;
35215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
35225ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
35235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
35245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadHwModule(cnode *root)
35255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
35265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    cnode *node = config_find(root, OUTPUTS_TAG);
35275ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    status_t status = NAME_NOT_FOUND;
352870c236c9290732782d5267935af1475b8d5ae602Eric Laurent
352970c236c9290732782d5267935af1475b8d5ae602Eric Laurent    HwModule *module = new HwModule(root->name);
353070c236c9290732782d5267935af1475b8d5ae602Eric Laurent
35315ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (node != NULL) {
35325ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strcmp(root->name, AUDIO_HARDWARE_MODULE_ID_A2DP) == 0) {
35335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            mHasA2dp = true;
3534599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent        } else if (strcmp(root->name, AUDIO_HARDWARE_MODULE_ID_USB) == 0) {
3535599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            mHasUsb = true;
35365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
3537599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent
35385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        node = node->first_child;
35395ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        while (node) {
35405ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadHwModule() loading output %s", node->name);
354170c236c9290732782d5267935af1475b8d5ae602Eric Laurent            status_t tmpStatus = loadOutput(node, module);
35425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            if (status == NAME_NOT_FOUND || status == NO_ERROR) {
35435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                status = tmpStatus;
35445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
35455ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            node = node->next;
35465ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
35475ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
35485ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    node = config_find(root, INPUTS_TAG);
35495ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (node != NULL) {
35505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        node = node->first_child;
35515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        while (node) {
35525ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadHwModule() loading input %s", node->name);
355370c236c9290732782d5267935af1475b8d5ae602Eric Laurent            status_t tmpStatus = loadInput(node, module);
35545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            if (status == NAME_NOT_FOUND || status == NO_ERROR) {
35555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                status = tmpStatus;
35565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
35575ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            node = node->next;
35585ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
35595ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
35605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (status == NO_ERROR) {
356170c236c9290732782d5267935af1475b8d5ae602Eric Laurent        mHwModules.add(module);
356270c236c9290732782d5267935af1475b8d5ae602Eric Laurent    } else {
356370c236c9290732782d5267935af1475b8d5ae602Eric Laurent        delete module;
35645ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
35655ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
35665ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
35675ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadHwModules(cnode *root)
35685ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
35695ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    cnode *node = config_find(root, AUDIO_HW_MODULE_TAG);
35705ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (node == NULL) {
35715ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return;
35725ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
35735ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
35745ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    node = node->first_child;
35755ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (node) {
35765ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        ALOGV("loadHwModules() loading module %s", node->name);
35775ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        loadHwModule(node);
35785ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        node = node->next;
35795ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
35805ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
35815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
35825ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadGlobalConfig(cnode *root)
35835ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
35845ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    cnode *node = config_find(root, GLOBAL_CONFIG_TAG);
35855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (node == NULL) {
35865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return;
35875ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
35885ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    node = node->first_child;
35895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (node) {
35905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strcmp(ATTACHED_OUTPUT_DEVICES_TAG, node->name) == 0) {
35915ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            mAttachedOutputDevices = parseDeviceNames((char *)node->value);
35925ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGW_IF(mAttachedOutputDevices == 0, "loadGlobalConfig() no attached output devices");
35935ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadGlobalConfig() mAttachedOutputDevices %04x", mAttachedOutputDevices);
35945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(DEFAULT_OUTPUT_DEVICE_TAG, node->name) == 0) {
35955ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            mDefaultOutputDevice = (audio_devices_t)stringToEnum(sDeviceNameToEnumTable,
35965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                              ARRAY_SIZE(sDeviceNameToEnumTable),
35975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                              (char *)node->value);
35985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGW_IF(mDefaultOutputDevice == 0, "loadGlobalConfig() default device not specified");
35995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadGlobalConfig() mDefaultOutputDevice %04x", mDefaultOutputDevice);
36005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(ATTACHED_INPUT_DEVICES_TAG, node->name) == 0) {
36015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            mAvailableInputDevices = parseDeviceNames((char *)node->value);
36025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadGlobalConfig() mAvailableInputDevices %04x", mAvailableInputDevices);
36035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
36045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        node = node->next;
36055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
36065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
36075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
36085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentstatus_t AudioPolicyManagerBase::loadAudioPolicyConfig(const char *path)
36095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
36105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    cnode *root;
36115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    char *data;
36125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
36135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    data = (char *)load_file(path, NULL);
36145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (data == NULL) {
36155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return -ENODEV;
36165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
36175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    root = config_node("", "");
36185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    config_load(root, data);
36195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
36205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    loadGlobalConfig(root);
36215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    loadHwModules(root);
36225ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
36235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    config_free(root);
36245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    free(root);
36255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    free(data);
36265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
36275ec145df7708564d385fd3fb764085321cf4c253Dima Zavin    ALOGI("loadAudioPolicyConfig() loaded %s\n", path);
36285ec145df7708564d385fd3fb764085321cf4c253Dima Zavin
36295ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return NO_ERROR;
36305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
3631f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3632739022f26a7127ba76a98dda65411496086114a7Dima Zavinvoid AudioPolicyManagerBase::defaultAudioPolicyConfig(void)
3633739022f26a7127ba76a98dda65411496086114a7Dima Zavin{
3634739022f26a7127ba76a98dda65411496086114a7Dima Zavin    HwModule *module;
3635739022f26a7127ba76a98dda65411496086114a7Dima Zavin    IOProfile *profile;
3636739022f26a7127ba76a98dda65411496086114a7Dima Zavin
3637739022f26a7127ba76a98dda65411496086114a7Dima Zavin    mDefaultOutputDevice = AUDIO_DEVICE_OUT_SPEAKER;
3638739022f26a7127ba76a98dda65411496086114a7Dima Zavin    mAttachedOutputDevices = AUDIO_DEVICE_OUT_SPEAKER;
3639739022f26a7127ba76a98dda65411496086114a7Dima Zavin    mAvailableInputDevices = AUDIO_DEVICE_IN_BUILTIN_MIC;
3640739022f26a7127ba76a98dda65411496086114a7Dima Zavin
3641739022f26a7127ba76a98dda65411496086114a7Dima Zavin    module = new HwModule("primary");
3642739022f26a7127ba76a98dda65411496086114a7Dima Zavin
3643739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile = new IOProfile(module);
3644739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mSamplingRates.add(44100);
3645739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
3646739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mChannelMasks.add(AUDIO_CHANNEL_OUT_STEREO);
3647739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mSupportedDevices = AUDIO_DEVICE_OUT_SPEAKER;
3648739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mFlags = AUDIO_OUTPUT_FLAG_PRIMARY;
3649739022f26a7127ba76a98dda65411496086114a7Dima Zavin    module->mOutputProfiles.add(profile);
3650739022f26a7127ba76a98dda65411496086114a7Dima Zavin
3651739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile = new IOProfile(module);
3652739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mSamplingRates.add(8000);
3653739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
3654739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mChannelMasks.add(AUDIO_CHANNEL_IN_MONO);
3655739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mSupportedDevices = AUDIO_DEVICE_IN_BUILTIN_MIC;
3656739022f26a7127ba76a98dda65411496086114a7Dima Zavin    module->mInputProfiles.add(profile);
3657739022f26a7127ba76a98dda65411496086114a7Dima Zavin
3658739022f26a7127ba76a98dda65411496086114a7Dima Zavin    mHwModules.add(module);
3659739022f26a7127ba76a98dda65411496086114a7Dima Zavin}
3660f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3661f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}; // namespace android
3662