AudioPolicyManagerBase.cpp revision b52f373bd56fbbb07a625de15125d33672d5143f
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
276d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi// A device mask for all audio input devices that are considered "virtual" when evaluating
286d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi// active inputs in getActiveInput()
296d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi#define APM_AUDIO_IN_DEVICE_VIRTUAL_ALL  AUDIO_DEVICE_IN_REMOTE_SUBMIX
306d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi
31f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#include <utils/Log.h>
32f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#include <hardware_legacy/AudioPolicyManagerBase.h>
331c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent#include <hardware/audio_effect.h>
3408b014d9e509c9163db6b33a63852e73db4d07ccEric Laurent#include <hardware/audio.h>
35f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#include <math.h>
365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent#include <hardware_legacy/audio_policy_conf.h>
37f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
38e81531e91ecae92aff471dbff9cbeb0f95ff4a80Dima Zavinnamespace android_audio_legacy {
39f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
40f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// ----------------------------------------------------------------------------
41f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// AudioPolicyInterface implementation
42f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// ----------------------------------------------------------------------------
43f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
44f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
45c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivistatus_t AudioPolicyManagerBase::setDeviceConnectionState(audio_devices_t device,
46f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                  AudioSystem::device_connection_state state,
47f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                  const char *device_address)
48f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
493cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    SortedVector <audio_io_handle_t> outputs;
50f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
516a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("setDeviceConnectionState() device: %x, state %d, address %s", device, state, device_address);
52f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
53f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // connect/disconnect only 1 device at a time
54ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
55f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
56f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (strlen(device_address) >= MAX_DEVICE_ADDRESS_LEN) {
575efbd421e0029d6fc44b1cc65c0e5e0d85e5161fSteve Block        ALOGE("setDeviceConnectionState() invalid address: %s", device_address);
58f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
59f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
60f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
61f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // handle output devices
62c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    if (audio_is_output_device(device)) {
63f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
64c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        if (!mHasA2dp && audio_is_a2dp_device(device)) {
6548387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            ALOGE("setDeviceConnectionState() invalid A2DP device: %x", device);
66f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return BAD_VALUE;
67f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
68c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        if (!mHasUsb && audio_is_usb_device(device)) {
6948387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            ALOGE("setDeviceConnectionState() invalid USB audio device: %x", device);
7048387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            return BAD_VALUE;
7148387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi        }
7248387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi        if (!mHasRemoteSubmix && audio_is_remote_submix_device((audio_devices_t)device)) {
7348387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            ALOGE("setDeviceConnectionState() invalid remote submix audio device: %x", device);
74599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            return BAD_VALUE;
75599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent        }
76f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
77c952527e6f89d5427881462823514be9d79f13e6Eric Laurent        // save a copy of the opened output descriptors before any output is opened or closed
78c952527e6f89d5427881462823514be9d79f13e6Eric Laurent        // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies()
79c952527e6f89d5427881462823514be9d79f13e6Eric Laurent        mPreviousOutputs = mOutputs;
80f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        switch (state)
81f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        {
82f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // handle output device connection
83f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        case AudioSystem::DEVICE_STATE_AVAILABLE:
84f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (mAvailableOutputDevices & device) {
8564cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block                ALOGW("setDeviceConnectionState() device already connected: %x", device);
86f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                return INVALID_OPERATION;
87f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
886a70518b93928d1c91457ff805e375c82d76b0e5Steve Block            ALOGV("setDeviceConnectionState() connecting device %x", device);
89f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
90c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            if (checkOutputsForDevice(device, state, outputs) != NO_ERROR) {
913cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                return INVALID_OPERATION;
923cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
933cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %d outputs",
943cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                  outputs.size());
95f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // register new device as available
96f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent            mAvailableOutputDevices = (audio_devices_t)(mAvailableOutputDevices | device);
97f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
983cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            if (!outputs.isEmpty()) {
993cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                String8 paramStr;
100c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                if (mHasA2dp && audio_is_a2dp_device(device)) {
1013cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    // handle A2DP device connection
1023cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    AudioParameter param;
1033cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    param.add(String8(AUDIO_PARAMETER_A2DP_SINK_ADDRESS), String8(device_address));
1043cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    paramStr = param.toString();
1053cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    mA2dpDeviceAddress = String8(device_address, MAX_DEVICE_ADDRESS_LEN);
1063cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    mA2dpSuspended = false;
107c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                } else if (audio_is_bluetooth_sco_device(device)) {
1083cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    // handle SCO device connection
1093cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    mScoDeviceAddress = String8(device_address, MAX_DEVICE_ADDRESS_LEN);
110c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                } else if (mHasUsb && audio_is_usb_device(device)) {
1113cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    // handle USB device connection
1123cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    mUsbCardAndDevice = String8(device_address, MAX_DEVICE_ADDRESS_LEN);
1133cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    paramStr = mUsbCardAndDevice;
1143cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                }
11548387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi                // not currently handling multiple simultaneous submixes: ignoring remote submix
11648387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi                //   case and address
1173cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                if (!paramStr.isEmpty()) {
1183cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    for (size_t i = 0; i < outputs.size(); i++) {
1193cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        mpClientInterface->setParameters(outputs[i], paramStr);
1203cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
1213cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                }
122f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
123f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            break;
124f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // handle output device disconnection
125f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        case AudioSystem::DEVICE_STATE_UNAVAILABLE: {
126f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (!(mAvailableOutputDevices & device)) {
12764cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block                ALOGW("setDeviceConnectionState() device not connected: %x", device);
128f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                return INVALID_OPERATION;
129f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
130f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1316a70518b93928d1c91457ff805e375c82d76b0e5Steve Block            ALOGV("setDeviceConnectionState() disconnecting device %x", device);
132f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // remove device from available output devices
133f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent            mAvailableOutputDevices = (audio_devices_t)(mAvailableOutputDevices & ~device);
134f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
135c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            checkOutputsForDevice(device, state, outputs);
136c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            if (mHasA2dp && audio_is_a2dp_device(device)) {
1373cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                // handle A2DP device disconnection
138b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                mA2dpDeviceAddress = "";
139b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                mA2dpSuspended = false;
140c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            } else if (audio_is_bluetooth_sco_device(device)) {
1413cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                // handle SCO device disconnection
142b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                mScoDeviceAddress = "";
143c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            } else if (mHasUsb && audio_is_usb_device(device)) {
1443cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                // handle USB device disconnection
145599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent                mUsbCardAndDevice = "";
146f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
14748387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            // not currently handling multiple simultaneous submixes: ignoring remote submix
14848387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            //   case and address
149f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            } break;
150f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
151f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        default:
1525efbd421e0029d6fc44b1cc65c0e5e0d85e5161fSteve Block            ALOGE("setDeviceConnectionState() invalid state: %x", state);
153f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return BAD_VALUE;
154f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
155f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
156f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        checkA2dpSuspend();
157f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        checkOutputForAllStrategies();
158b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        // outputs must be closed after checkOutputForAllStrategies() is executed
1593cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        if (!outputs.isEmpty()) {
1603cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            for (size_t i = 0; i < outputs.size(); i++) {
1613cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                // close unused outputs after device disconnection or direct outputs that have been
1623cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                // opened by checkOutputsForDevice() to query dynamic parameters
1633cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                if ((state == AudioSystem::DEVICE_STATE_UNAVAILABLE) ||
1643cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        (mOutputs.valueFor(outputs[i])->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
1653cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    closeOutput(outputs[i]);
1663cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                }
1673cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
168f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1695ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
170c952527e6f89d5427881462823514be9d79f13e6Eric Laurent        updateDevicesAndOutputs();
1715ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        for (size_t i = 0; i < mOutputs.size(); i++) {
17276e97d3950f2654adbb0a415218b6d048200c395Eric Laurent            setOutputDevice(mOutputs.keyAt(i),
17376e97d3950f2654adbb0a415218b6d048200c395Eric Laurent                            getNewDevice(mOutputs.keyAt(i), true /*fromCache*/),
17476e97d3950f2654adbb0a415218b6d048200c395Eric Laurent                            true,
17576e97d3950f2654adbb0a415218b6d048200c395Eric Laurent                            0);
1765ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
177f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
178c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        if (device == AUDIO_DEVICE_OUT_WIRED_HEADSET) {
179c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = AUDIO_DEVICE_IN_WIRED_HEADSET;
180c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        } else if (device == AUDIO_DEVICE_OUT_BLUETOOTH_SCO ||
181c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                   device == AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET ||
182c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                   device == AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT) {
183c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
184f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        } else {
185f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return NO_ERROR;
186f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
187f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
188f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // handle input devices
189c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    if (audio_is_input_device(device)) {
190f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
191f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        switch (state)
192f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        {
193f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // handle input device connection
194f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        case AudioSystem::DEVICE_STATE_AVAILABLE: {
195f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (mAvailableInputDevices & device) {
19664cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block                ALOGW("setDeviceConnectionState() device already connected: %d", device);
197f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                return INVALID_OPERATION;
198f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
199ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            mAvailableInputDevices = mAvailableInputDevices | (device & ~AUDIO_DEVICE_BIT_IN);
200f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
201f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            break;
202f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
203f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // handle input device disconnection
204f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        case AudioSystem::DEVICE_STATE_UNAVAILABLE: {
205f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (!(mAvailableInputDevices & device)) {
20664cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block                ALOGW("setDeviceConnectionState() device not connected: %d", device);
207f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                return INVALID_OPERATION;
208f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            mAvailableInputDevices = (audio_devices_t) (mAvailableInputDevices & ~device);
210f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            } break;
211f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
212f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        default:
2135efbd421e0029d6fc44b1cc65c0e5e0d85e5161fSteve Block            ALOGE("setDeviceConnectionState() invalid state: %x", state);
214f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return BAD_VALUE;
215f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
216f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
217f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        audio_io_handle_t activeInput = getActiveInput();
218f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (activeInput != 0) {
219f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            AudioInputDescriptor *inputDesc = mInputs.valueFor(activeInput);
220f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent            audio_devices_t newDevice = getDeviceForInputSource(inputDesc->mInputSource);
221ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            if ((newDevice != AUDIO_DEVICE_NONE) && (newDevice != inputDesc->mDevice)) {
2226a70518b93928d1c91457ff805e375c82d76b0e5Steve Block                ALOGV("setDeviceConnectionState() changing device from %x to %x for input %d",
223f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        inputDesc->mDevice, newDevice, activeInput);
224f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                inputDesc->mDevice = newDevice;
225f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                AudioParameter param = AudioParameter();
226f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.addInt(String8(AudioParameter::keyRouting), (int)newDevice);
227f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mpClientInterface->setParameters(activeInput, param.toString());
228f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
229f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
230f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
231f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return NO_ERROR;
232f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
233f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
23464cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block    ALOGW("setDeviceConnectionState() invalid device: %x", device);
235f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return BAD_VALUE;
236f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
237f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
238c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel TriviAudioSystem::device_connection_state AudioPolicyManagerBase::getDeviceConnectionState(audio_devices_t device,
239f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                  const char *device_address)
240f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
241f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioSystem::device_connection_state state = AudioSystem::DEVICE_STATE_UNAVAILABLE;
242f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    String8 address = String8(device_address);
243c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    if (audio_is_output_device(device)) {
244f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (device & mAvailableOutputDevices) {
245c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            if (audio_is_a2dp_device(device) &&
2465ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                (!mHasA2dp || (address != "" && mA2dpDeviceAddress != address))) {
247f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                return state;
248f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
249c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            if (audio_is_bluetooth_sco_device(device) &&
250f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                address != "" && mScoDeviceAddress != address) {
251f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                return state;
252f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
253c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            if (audio_is_usb_device(device) &&
254599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent                (!mHasUsb || (address != "" && mUsbCardAndDevice != address))) {
25548387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi                ALOGE("getDeviceConnectionState() invalid device: %x", device);
25648387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi                return state;
25748387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            }
25848387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            if (audio_is_remote_submix_device((audio_devices_t)device) && !mHasRemoteSubmix) {
259599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent                return state;
260599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            }
261f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            state = AudioSystem::DEVICE_STATE_AVAILABLE;
262f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
263c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    } else if (audio_is_input_device(device)) {
264f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (device & mAvailableInputDevices) {
265f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            state = AudioSystem::DEVICE_STATE_AVAILABLE;
266f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
267f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
268f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
269f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return state;
270f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
271f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
272f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::setPhoneState(int state)
273f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2746a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("setPhoneState() state %d", state);
275ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    audio_devices_t newDevice = AUDIO_DEVICE_NONE;
276f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (state < 0 || state >= AudioSystem::NUM_MODES) {
27764cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("setPhoneState() invalid state %d", state);
278f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
279f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
280f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
281f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (state == mPhoneState ) {
28264cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("setPhoneState() setting same state %d", state);
283f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
284f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
285f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
286f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if leaving call state, handle special case of active streams
287f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // pertaining to sonification strategy see handleIncallSonification()
288f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (isInCall()) {
2896a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("setPhoneState() in call state management: new state is %d", state);
290f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
291f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            handleIncallSonification(stream, false, true);
292f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
293f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
294f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
295f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // store previous phone state for management of sonification strategy below
296f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    int oldState = mPhoneState;
297f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mPhoneState = state;
298f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    bool force = false;
299f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
300f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // are we entering or starting a call
301f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (!isStateInCall(oldState) && isStateInCall(state)) {
3026a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("  Entering call in setPhoneState()");
303f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // force routing command to audio hardware when starting a call
304f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // even if no device change is needed
305f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        force = true;
306f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else if (isStateInCall(oldState) && !isStateInCall(state)) {
3076a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("  Exiting call in setPhoneState()");
308f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // force routing command to audio hardware when exiting a call
309f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // even if no device change is needed
310f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        force = true;
311f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else if (isStateInCall(state) && (state != oldState)) {
3126a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("  Switching between telephony and VoIP in setPhoneState()");
313f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // force routing command to audio hardware when switching between telephony and VoIP
314f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // even if no device change is needed
315f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        force = true;
316f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
317f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
318f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // check for device and output changes triggered by new phone state
3195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    newDevice = getNewDevice(mPrimaryOutput, false /*fromCache*/);
320f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkA2dpSuspend();
321f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForAllStrategies();
322c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    updateDevicesAndOutputs();
323f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
324b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    AudioOutputDescriptor *hwOutputDesc = mOutputs.valueFor(mPrimaryOutput);
325f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
326f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // force routing command to audio hardware when ending call
327f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // even if no device change is needed
328ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if (isStateInCall(oldState) && newDevice == AUDIO_DEVICE_NONE) {
329f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        newDevice = hwOutputDesc->device();
330f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
331f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
332f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // when changing from ring tone to in call mode, mute the ringing tone
333f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // immediately and delay the route change to avoid sending the ring tone
334f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // tail into the earpiece or headset.
335f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    int delayMs = 0;
336f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (isStateInCall(state) && oldState == AudioSystem::MODE_RINGTONE) {
337f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // delay the device change command by twice the output latency to have some margin
338f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // and be sure that audio buffers not yet affected by the mute are out when
339f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // we actually apply the route change
340f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        delayMs = hwOutputDesc->mLatency*2;
341b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        setStreamMute(AudioSystem::RING, true, mPrimaryOutput);
342f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
343f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
344f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // change routing is necessary
345b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    setOutputDevice(mPrimaryOutput, newDevice, force, delayMs);
346f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
347f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if entering in call state, handle special case of active streams
348f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // pertaining to sonification strategy see handleIncallSonification()
349f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (isStateInCall(state)) {
3506a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("setPhoneState() in call state management: new state is %d", state);
351f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // unmute the ringing tone after a sufficient delay if it was muted before
352f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // setting output device above
353f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (oldState == AudioSystem::MODE_RINGTONE) {
354b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            setStreamMute(AudioSystem::RING, false, mPrimaryOutput, MUTE_TIME_MS);
355f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
356f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
357f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            handleIncallSonification(stream, true, true);
358f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
359f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
360f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
361f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
362f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (state == AudioSystem::MODE_RINGTONE &&
363f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        isStreamActive(AudioSystem::MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
364f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mLimitRingtoneVolume = true;
365f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
366f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mLimitRingtoneVolume = false;
367f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
368f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
369f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
370f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::setForceUse(AudioSystem::force_use usage, AudioSystem::forced_config config)
371f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3726a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mPhoneState);
373f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
374f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    bool forceVolumeReeval = false;
375f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    switch(usage) {
376f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::FOR_COMMUNICATION:
377f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (config != AudioSystem::FORCE_SPEAKER && config != AudioSystem::FORCE_BT_SCO &&
378f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_NONE) {
37964cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("setForceUse() invalid config %d for FOR_COMMUNICATION", config);
380f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return;
381f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
382f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        forceVolumeReeval = true;
383f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mForceUse[usage] = config;
384f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
385f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::FOR_MEDIA:
386f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (config != AudioSystem::FORCE_HEADPHONES && config != AudioSystem::FORCE_BT_A2DP &&
387f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_WIRED_ACCESSORY &&
388f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_ANALOG_DOCK &&
3891afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent            config != AudioSystem::FORCE_DIGITAL_DOCK && config != AudioSystem::FORCE_NONE &&
39048387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            config != AudioSystem::FORCE_NO_BT_A2DP &&
39148387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            config != AudioSystem::FORCE_REMOTE_SUBMIX) {
39264cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("setForceUse() invalid config %d for FOR_MEDIA", config);
393f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return;
394f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
395f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mForceUse[usage] = config;
396f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
397f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::FOR_RECORD:
398f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (config != AudioSystem::FORCE_BT_SCO && config != AudioSystem::FORCE_WIRED_ACCESSORY &&
399f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_NONE) {
40064cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("setForceUse() invalid config %d for FOR_RECORD", config);
401f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return;
402f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
403f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mForceUse[usage] = config;
404f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
405f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::FOR_DOCK:
406f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (config != AudioSystem::FORCE_NONE && config != AudioSystem::FORCE_BT_CAR_DOCK &&
407f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_BT_DESK_DOCK &&
408f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_WIRED_ACCESSORY &&
409f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_ANALOG_DOCK &&
410f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_DIGITAL_DOCK) {
41164cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("setForceUse() invalid config %d for FOR_DOCK", config);
412f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
413f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        forceVolumeReeval = true;
414f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mForceUse[usage] = config;
415f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
416738207def5f691d605ae33d041116829a74513a9Eric Laurent    case AudioSystem::FOR_SYSTEM:
417738207def5f691d605ae33d041116829a74513a9Eric Laurent        if (config != AudioSystem::FORCE_NONE &&
418738207def5f691d605ae33d041116829a74513a9Eric Laurent            config != AudioSystem::FORCE_SYSTEM_ENFORCED) {
419738207def5f691d605ae33d041116829a74513a9Eric Laurent            ALOGW("setForceUse() invalid config %d for FOR_SYSTEM", config);
420738207def5f691d605ae33d041116829a74513a9Eric Laurent        }
421738207def5f691d605ae33d041116829a74513a9Eric Laurent        forceVolumeReeval = true;
422738207def5f691d605ae33d041116829a74513a9Eric Laurent        mForceUse[usage] = config;
423738207def5f691d605ae33d041116829a74513a9Eric Laurent        break;
424f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    default:
42564cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("setForceUse() invalid usage %d", usage);
426f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
427f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
428f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
4295ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // check for device and output changes triggered by new force usage
430f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkA2dpSuspend();
431f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForAllStrategies();
432c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    updateDevicesAndOutputs();
4335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < mOutputs.size(); i++) {
4345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_io_handle_t output = mOutputs.keyAt(i);
4355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_devices_t newDevice = getNewDevice(output, true /*fromCache*/);
436ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        setOutputDevice(output, newDevice, (newDevice != AUDIO_DEVICE_NONE));
437ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
4385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            applyStreamVolumes(output, newDevice, 0, true);
4395ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
440f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
441f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
442f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    audio_io_handle_t activeInput = getActiveInput();
443f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (activeInput != 0) {
444f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioInputDescriptor *inputDesc = mInputs.valueFor(activeInput);
4455ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_devices_t newDevice = getDeviceForInputSource(inputDesc->mInputSource);
446ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if ((newDevice != AUDIO_DEVICE_NONE) && (newDevice != inputDesc->mDevice)) {
4476a70518b93928d1c91457ff805e375c82d76b0e5Steve Block            ALOGV("setForceUse() changing device from %x to %x for input %d",
448f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    inputDesc->mDevice, newDevice, activeInput);
449f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            inputDesc->mDevice = newDevice;
450f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            AudioParameter param = AudioParameter();
451f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            param.addInt(String8(AudioParameter::keyRouting), (int)newDevice);
452f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mpClientInterface->setParameters(activeInput, param.toString());
453f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
454f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
455f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
456f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
457f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
458f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima ZavinAudioSystem::forced_config AudioPolicyManagerBase::getForceUse(AudioSystem::force_use usage)
459f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
460f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return mForceUse[usage];
461f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
462f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
463f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::setSystemProperty(const char* property, const char* value)
464f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
4656a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("setSystemProperty() property %s, value %s", property, value);
466f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
467f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
4683cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric LaurentAudioPolicyManagerBase::IOProfile *AudioPolicyManagerBase::getProfileForDirectOutput(
4693cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                               audio_devices_t device,
47070c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                               uint32_t samplingRate,
47170c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                               uint32_t format,
47270c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                               uint32_t channelMask,
4730977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent                                                               audio_output_flags_t flags)
47470c236c9290732782d5267935af1475b8d5ae602Eric Laurent{
47570c236c9290732782d5267935af1475b8d5ae602Eric Laurent    for (size_t i = 0; i < mHwModules.size(); i++) {
47670c236c9290732782d5267935af1475b8d5ae602Eric Laurent        if (mHwModules[i]->mHandle == 0) {
47770c236c9290732782d5267935af1475b8d5ae602Eric Laurent            continue;
47870c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
47970c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) {
4803cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent           IOProfile *profile = mHwModules[i]->mOutputProfiles[j];
4813cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent           if (profile->isCompatibleProfile(device, samplingRate, format,
4823cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                           channelMask,
4833cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                           AUDIO_OUTPUT_FLAG_DIRECT)) {
4843cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent               if (mAvailableOutputDevices & profile->mSupportedDevices) {
4853cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                   return mHwModules[i]->mOutputProfiles[j];
4863cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent               }
48770c236c9290732782d5267935af1475b8d5ae602Eric Laurent           }
48870c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
48970c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
49070c236c9290732782d5267935af1475b8d5ae602Eric Laurent    return 0;
49170c236c9290732782d5267935af1475b8d5ae602Eric Laurent}
49270c236c9290732782d5267935af1475b8d5ae602Eric Laurent
493f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinaudio_io_handle_t AudioPolicyManagerBase::getOutput(AudioSystem::stream_type stream,
494f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    uint32_t samplingRate,
495f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    uint32_t format,
49670c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                    uint32_t channelMask,
497f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    AudioSystem::output_flags flags)
498f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
499f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    audio_io_handle_t output = 0;
500f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    uint32_t latency = 0;
501f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    routing_strategy strategy = getStrategy((AudioSystem::stream_type)stream);
5025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
50370c236c9290732782d5267935af1475b8d5ae602Eric Laurent    ALOGV("getOutput() stream %d, samplingRate %d, format %d, channelMask %x, flags %x",
50470c236c9290732782d5267935af1475b8d5ae602Eric Laurent          stream, samplingRate, format, channelMask, flags);
505f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
506f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
507f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mCurOutput != 0) {
50870c236c9290732782d5267935af1475b8d5ae602Eric Laurent        ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
509f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
510f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
511f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (mTestOutputs[mCurOutput] == 0) {
5126a70518b93928d1c91457ff805e375c82d76b0e5Steve Block            ALOGV("getOutput() opening test output");
5133cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(NULL);
514f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            outputDesc->mDevice = mTestDevice;
515f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            outputDesc->mSamplingRate = mTestSamplingRate;
516f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            outputDesc->mFormat = mTestFormat;
51770c236c9290732782d5267935af1475b8d5ae602Eric Laurent            outputDesc->mChannelMask = mTestChannels;
518f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            outputDesc->mLatency = mTestLatencyMs;
5190977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent            outputDesc->mFlags = (audio_output_flags_t)(mDirectOutput ? AudioSystem::OUTPUT_FLAG_DIRECT : 0);
520f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            outputDesc->mRefCount[stream] = 0;
52170c236c9290732782d5267935af1475b8d5ae602Eric Laurent            mTestOutputs[mCurOutput] = mpClientInterface->openOutput(0, &outputDesc->mDevice,
522f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            &outputDesc->mSamplingRate,
523f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            &outputDesc->mFormat,
52470c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                            &outputDesc->mChannelMask,
525f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            &outputDesc->mLatency,
526f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            outputDesc->mFlags);
527f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (mTestOutputs[mCurOutput]) {
528f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                AudioParameter outputCmd = AudioParameter();
529f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                outputCmd.addInt(String8("set_id"),mCurOutput);
530f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
531f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                addOutput(mTestOutputs[mCurOutput], outputDesc);
532f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
533f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
534f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return mTestOutputs[mCurOutput];
535f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
536f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
537f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
538f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // open a direct output if required by specified parameters
5393cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    IOProfile *profile = getProfileForDirectOutput(device,
5403cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                   samplingRate,
5413cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                   format,
5423cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                   channelMask,
5433cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                   (audio_output_flags_t)flags);
5443cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    if (profile != NULL) {
545f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
5466a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("getOutput() opening direct output device %x", device);
54770c236c9290732782d5267935af1475b8d5ae602Eric Laurent
5483cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(profile);
549f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mDevice = device;
550f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mSamplingRate = samplingRate;
55170c236c9290732782d5267935af1475b8d5ae602Eric Laurent        outputDesc->mFormat = (audio_format_t)format;
55270c236c9290732782d5267935af1475b8d5ae602Eric Laurent        outputDesc->mChannelMask = (audio_channel_mask_t)channelMask;
553f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mLatency = 0;
5543cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        outputDesc->mFlags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);;
555f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mRefCount[stream] = 0;
556f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mStopTime[stream] = 0;
5573cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        output = mpClientInterface->openOutput(profile->mModule->mHandle,
55870c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                        &outputDesc->mDevice,
559f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                        &outputDesc->mSamplingRate,
560f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                        &outputDesc->mFormat,
56170c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                        &outputDesc->mChannelMask,
562f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                        &outputDesc->mLatency,
563f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                        outputDesc->mFlags);
564f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
5659f1f9b509c930830f6f32e9ef6c2c8a03d6fa96eJean-Michel Trivi        // only accept an output with the requested parameters
566f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (output == 0 ||
567f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            (samplingRate != 0 && samplingRate != outputDesc->mSamplingRate) ||
568f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            (format != 0 && format != outputDesc->mFormat) ||
56970c236c9290732782d5267935af1475b8d5ae602Eric Laurent            (channelMask != 0 && channelMask != outputDesc->mChannelMask)) {
5703cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
5713cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    "format %d %d, channelMask %04x %04x", output, samplingRate,
5723cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
5733cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    outputDesc->mChannelMask);
574f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (output != 0) {
575f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mpClientInterface->closeOutput(output);
576f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
577f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            delete outputDesc;
578f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return 0;
579f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
580f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        addOutput(output, outputDesc);
5813cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        ALOGV("getOutput() returns direct output %d", output);
582f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return output;
583f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
584f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
5859f1f9b509c930830f6f32e9ef6c2c8a03d6fa96eJean-Michel Trivi    // ignoring channel mask due to downmix capability in mixer
5869f1f9b509c930830f6f32e9ef6c2c8a03d6fa96eJean-Michel Trivi
587f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // open a non direct output
588f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
589f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // get which output is suitable for the specified stream. The actual routing change will happen
590f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // when startOutput() will be called
591c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
592f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
5935ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    output = selectOutput(outputs, flags);
594f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
59570c236c9290732782d5267935af1475b8d5ae602Eric Laurent    ALOGW_IF((output ==0), "getOutput() could not find output for stream %d, samplingRate %d,"
59670c236c9290732782d5267935af1475b8d5ae602Eric Laurent            "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
597f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
5985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGV("getOutput() returns output %d", output);
5995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
600f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return output;
601f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
602f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
6035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentaudio_io_handle_t AudioPolicyManagerBase::selectOutput(const SortedVector<audio_io_handle_t>& outputs,
6045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                       AudioSystem::output_flags flags)
6055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
6065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // select one output among several that provide a path to a particular device or set of
6075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // devices (the list was previously build by getOutputsForDevice()).
6085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // The priority is as follows:
6095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // 1: the output with the highest number of requested policy flags
6105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // 2: the primary output
6115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // 3: the first output in the list
6125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
6135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (outputs.size() == 0) {
6145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return 0;
6155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
6165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (outputs.size() == 1) {
6175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return outputs[0];
6185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
6195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
6205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    int maxCommonFlags = 0;
6215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    audio_io_handle_t outputFlags = 0;
6225ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    audio_io_handle_t outputPrimary = 0;
6235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
6245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < outputs.size(); i++) {
6255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        AudioOutputDescriptor *outputDesc = mOutputs.valueFor(outputs[i]);
6265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (!outputDesc->isDuplicated()) {
6275ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            int commonFlags = (int)AudioSystem::popCount(outputDesc->mProfile->mFlags & flags);
6285ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            if (commonFlags > maxCommonFlags) {
6295ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                outputFlags = outputs[i];
6305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                maxCommonFlags = commonFlags;
6315ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags);
6325ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
6330977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent            if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
6345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                outputPrimary = outputs[i];
6355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
6365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
6375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
6385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
6395ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (outputFlags != 0) {
6405ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return outputFlags;
6415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
6425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (outputPrimary != 0) {
6435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return outputPrimary;
6445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
6455ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
6465ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return outputs[0];
6475ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
6485ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
649f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::startOutput(audio_io_handle_t output,
650f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                             AudioSystem::stream_type stream,
651f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                             int session)
652f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
6536a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("startOutput() output %d, stream %d, session %d", output, stream, session);
654f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mOutputs.indexOfKey(output);
655f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
65664cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("startOutput() unknow output %d", output);
657f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
658f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
659f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
660f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index);
661f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
6625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // increment usage count for this stream on the requested output:
663f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // NOTE that the usage count is the same for duplicated output and hardware output which is
6645ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
665f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    outputDesc->changeRefCount(stream, 1);
666f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
6675ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (outputDesc->mRefCount[stream] == 1) {
6685ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_devices_t newDevice = getNewDevice(output, false /*fromCache*/);
669b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        routing_strategy strategy = getStrategy(stream);
670b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
671b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                            (strategy == STRATEGY_SONIFICATION_RESPECTFUL);
672b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        uint32_t waitMs = 0;
6735ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        bool force = false;
6745ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        for (size_t i = 0; i < mOutputs.size(); i++) {
6755ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            AudioOutputDescriptor *desc = mOutputs.valueAt(i);
676b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent            if (desc != outputDesc) {
677b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                // force a device change if any other output is managed by the same hw
678b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                // module and has a current device selection that differs from selected device.
679b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                // In this case, the audio HAL must receive the new device selection so that it can
680b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                // change the device currently selected by the other active output.
681b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                if (outputDesc->sharesHwModuleWith(desc) &&
6825ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                    desc->device() != newDevice) {
683b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                    force = true;
684b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                }
685b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                // wait for audio on other active outputs to be presented when starting
686b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                // a notification so that audio focus effect can propagate.
687b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                if (shouldWait && (desc->refCount() != 0) && (waitMs < desc->latency())) {
688b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                    waitMs = desc->latency();
689b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                }
6905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
6915ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
692b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        uint32_t muteWaitMs = setOutputDevice(output, newDevice, force);
693f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
6945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // handle special case for sonification while in call
6955ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (isInCall()) {
6965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            handleIncallSonification(stream, true, false);
6975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
698c16ac09f510437e8340be691720177a490ae78f0Eric Laurent
6995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // apply volume rules for current stream and device if necessary
7005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        checkAndSetVolume(stream,
701c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                          mStreams[stream].getVolumeIndex(newDevice),
7025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                          output,
7035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                          newDevice);
70412bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi
7055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // update the outputs if starting an output with a stream that can affect notification
7065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // routing
7075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        handleNotificationRoutingForStream(stream);
708b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        if (waitMs > muteWaitMs) {
709b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent            usleep((waitMs - muteWaitMs) * 2 * 1000);
710b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        }
7115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
712f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
713f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
714f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
7155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
716f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::stopOutput(audio_io_handle_t output,
717f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            AudioSystem::stream_type stream,
718f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            int session)
719f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
7206a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
721f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mOutputs.indexOfKey(output);
722f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
72364cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("stopOutput() unknow output %d", output);
724f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
725f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
726f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
727f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index);
728f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
729f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // handle special case for sonification while in call
730f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (isInCall()) {
731f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        handleIncallSonification(stream, false, false);
732f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
733f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
734f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (outputDesc->mRefCount[stream] > 0) {
735f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // decrement usage count of this stream on the output
736f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->changeRefCount(stream, -1);
737f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // store time at which the stream was stopped - see isStreamActive()
7385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (outputDesc->mRefCount[stream] == 0) {
7395ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            outputDesc->mStopTime[stream] = systemTime();
7405ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            audio_devices_t newDevice = getNewDevice(output, false /*fromCache*/);
7415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // delay the device switch by twice the latency because stopOutput() is executed when
7425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // the track stop() command is received and at that time the audio track buffer can
7435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // still contain data that needs to be drained. The latency only covers the audio HAL
7445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // and kernel buffers. Also the latency does not always include additional delay in the
7455ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // audio path (audio DSP, CODEC ...)
7465ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            setOutputDevice(output, newDevice, false, outputDesc->mLatency*2);
7475ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
7485ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // force restoring the device selection on other active outputs if it differs from the
7495ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // one being selected for this output
7505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            for (size_t i = 0; i < mOutputs.size(); i++) {
7515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                audio_io_handle_t curOutput = mOutputs.keyAt(i);
7525ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                AudioOutputDescriptor *desc = mOutputs.valueAt(i);
7535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                if (curOutput != output &&
7545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                        desc->refCount() != 0 &&
7555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                        outputDesc->sharesHwModuleWith(desc) &&
7565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                        newDevice != desc->device()) {
7575ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                    setOutputDevice(curOutput,
7585ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                    getNewDevice(curOutput, false /*fromCache*/),
7595ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                    true,
7605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                    outputDesc->mLatency*2);
7615ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                }
7625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
7635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // update the outputs if stopping one with a stream that can affect notification routing
7645ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            handleNotificationRoutingForStream(stream);
765f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
766f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return NO_ERROR;
767f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
76864cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("stopOutput() refcount is already 0 for output %d", output);
769f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return INVALID_OPERATION;
770f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
771f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
772f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
773f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::releaseOutput(audio_io_handle_t output)
774f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
7756a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("releaseOutput() %d", output);
776f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mOutputs.indexOfKey(output);
777f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
77864cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("releaseOutput() releasing unknown output %d", output);
779f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
780f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
781f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
782f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
783f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    int testIndex = testOutputIndex(output);
784f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (testIndex != 0) {
785f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index);
786f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (outputDesc->refCount() == 0) {
787f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mpClientInterface->closeOutput(output);
788f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            delete mOutputs.valueAt(index);
789f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mOutputs.removeItem(output);
790f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mTestOutputs[testIndex] = 0;
791f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
792f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
793f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
794f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
795f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
796f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mOutputs.valueAt(index)->mFlags & AudioSystem::OUTPUT_FLAG_DIRECT) {
797f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mpClientInterface->closeOutput(output);
798f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        delete mOutputs.valueAt(index);
799f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mOutputs.removeItem(output);
800be13d2ebd0ee66e8c09d978107c14a4221b04dffEric Laurent        mPreviousOutputs = mOutputs;
801f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
80212bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi
803f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
804f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
805f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinaudio_io_handle_t AudioPolicyManagerBase::getInput(int inputSource,
806f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    uint32_t samplingRate,
807f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    uint32_t format,
80870c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                    uint32_t channelMask,
809f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    AudioSystem::audio_in_acoustics acoustics)
810f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
811f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    audio_io_handle_t input = 0;
812f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent    audio_devices_t device = getDeviceForInputSource(inputSource);
813f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
81470c236c9290732782d5267935af1475b8d5ae602Eric Laurent    ALOGV("getInput() inputSource %d, samplingRate %d, format %d, channelMask %x, acoustics %x",
81570c236c9290732782d5267935af1475b8d5ae602Eric Laurent          inputSource, samplingRate, format, channelMask, acoustics);
816f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
817ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if (device == AUDIO_DEVICE_NONE) {
8185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        ALOGW("getInput() could not find device for inputSource %d", inputSource);
819f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return 0;
820f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
821f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
822f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // adapt channel selection to input source
823f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    switch(inputSource) {
824f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_UPLINK:
82570c236c9290732782d5267935af1475b8d5ae602Eric Laurent        channelMask = AudioSystem::CHANNEL_IN_VOICE_UPLINK;
826f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
827f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_DOWNLINK:
82870c236c9290732782d5267935af1475b8d5ae602Eric Laurent        channelMask = AudioSystem::CHANNEL_IN_VOICE_DNLINK;
829f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
830f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_CALL:
83170c236c9290732782d5267935af1475b8d5ae602Eric Laurent        channelMask = (AudioSystem::CHANNEL_IN_VOICE_UPLINK | AudioSystem::CHANNEL_IN_VOICE_DNLINK);
832f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
833f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    default:
834f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
835f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
836f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
8375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    IOProfile *profile = getInputProfile(device,
8385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                         samplingRate,
8395ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                         format,
84070c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                         channelMask);
8415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (profile == NULL) {
8425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        ALOGW("getInput() could not find profile for device %04x, samplingRate %d, format %d,"
84370c236c9290732782d5267935af1475b8d5ae602Eric Laurent                "channelMask %04x",
84470c236c9290732782d5267935af1475b8d5ae602Eric Laurent                device, samplingRate, format, channelMask);
84570c236c9290732782d5267935af1475b8d5ae602Eric Laurent        return 0;
84670c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
84770c236c9290732782d5267935af1475b8d5ae602Eric Laurent
84870c236c9290732782d5267935af1475b8d5ae602Eric Laurent    if (profile->mModule->mHandle == 0) {
8493cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        ALOGE("getInput(): HW module %s not opened", profile->mModule->mName);
8505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return 0;
8515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
8525ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
8535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    AudioInputDescriptor *inputDesc = new AudioInputDescriptor(profile);
854f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
855f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    inputDesc->mInputSource = inputSource;
856f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    inputDesc->mDevice = device;
857f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    inputDesc->mSamplingRate = samplingRate;
85870c236c9290732782d5267935af1475b8d5ae602Eric Laurent    inputDesc->mFormat = (audio_format_t)format;
85970c236c9290732782d5267935af1475b8d5ae602Eric Laurent    inputDesc->mChannelMask = (audio_channel_mask_t)channelMask;
860f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    inputDesc->mRefCount = 0;
86170c236c9290732782d5267935af1475b8d5ae602Eric Laurent    input = mpClientInterface->openInput(profile->mModule->mHandle,
86270c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                    &inputDesc->mDevice,
863f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    &inputDesc->mSamplingRate,
864f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    &inputDesc->mFormat,
86570c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                    &inputDesc->mChannelMask);
866f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
867f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // only accept input with the exact requested set of parameters
868f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (input == 0 ||
869f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        (samplingRate != inputDesc->mSamplingRate) ||
870f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        (format != inputDesc->mFormat) ||
87170c236c9290732782d5267935af1475b8d5ae602Eric Laurent        (channelMask != inputDesc->mChannelMask)) {
87270c236c9290732782d5267935af1475b8d5ae602Eric Laurent        ALOGV("getInput() failed opening input: samplingRate %d, format %d, channelMask %d",
87370c236c9290732782d5267935af1475b8d5ae602Eric Laurent                samplingRate, format, channelMask);
874f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (input != 0) {
875f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mpClientInterface->closeInput(input);
876f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
877f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        delete inputDesc;
878f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return 0;
879f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
880f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mInputs.add(input, inputDesc);
881f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return input;
882f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
883f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
884f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::startInput(audio_io_handle_t input)
885f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
8866a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("startInput() input %d", input);
887f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mInputs.indexOfKey(input);
888f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
88964cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("startInput() unknow input %d", input);
890f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
891f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
892f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioInputDescriptor *inputDesc = mInputs.valueAt(index);
893f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
894f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
895f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mTestInput == 0)
896f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
897f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    {
898f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // refuse 2 active AudioRecord clients at the same time
899f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (getActiveInput() != 0) {
90064cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("startInput() input %d failed: other input already started", input);
901f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return INVALID_OPERATION;
902f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
903f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
904f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
905f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioParameter param = AudioParameter();
906f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    param.addInt(String8(AudioParameter::keyRouting), (int)inputDesc->mDevice);
907f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
908f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    param.addInt(String8(AudioParameter::keyInputSource), (int)inputDesc->mInputSource);
9096a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("AudioPolicyManager::startInput() input source = %d", inputDesc->mInputSource);
910f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
911f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mpClientInterface->setParameters(input, param.toString());
912f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
913f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    inputDesc->mRefCount = 1;
914f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
915f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
916f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
917f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::stopInput(audio_io_handle_t input)
918f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
9196a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("stopInput() input %d", input);
920f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mInputs.indexOfKey(input);
921f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
92264cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("stopInput() unknow input %d", input);
923f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
924f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
925f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioInputDescriptor *inputDesc = mInputs.valueAt(index);
926f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
927f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (inputDesc->mRefCount == 0) {
92864cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("stopInput() input %d already stopped", input);
929f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return INVALID_OPERATION;
930f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
931f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioParameter param = AudioParameter();
932f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        param.addInt(String8(AudioParameter::keyRouting), 0);
933f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mpClientInterface->setParameters(input, param.toString());
934f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        inputDesc->mRefCount = 0;
935f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return NO_ERROR;
936f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
937f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
938f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
939f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::releaseInput(audio_io_handle_t input)
940f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
9416a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("releaseInput() %d", input);
942f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mInputs.indexOfKey(input);
943f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
94464cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("releaseInput() releasing unknown input %d", input);
945f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
946f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
947f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mpClientInterface->closeInput(input);
948f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    delete mInputs.valueAt(index);
949f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mInputs.removeItem(input);
9506a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("releaseInput() exit");
951f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
952f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
953f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::initStreamVolume(AudioSystem::stream_type stream,
954f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            int indexMin,
955f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            int indexMax)
956f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
9576a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
958f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (indexMin < 0 || indexMin >= indexMax) {
95964cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("initStreamVolume() invalid index limits for stream %d, min %d, max %d", stream , indexMin, indexMax);
960f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
961f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
962f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mStreams[stream].mIndexMin = indexMin;
963f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mStreams[stream].mIndexMax = indexMax;
964f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
965f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
966c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentstatus_t AudioPolicyManagerBase::setStreamVolumeIndex(AudioSystem::stream_type stream,
967c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                      int index,
968c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                      audio_devices_t device)
969f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
970f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
971f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if ((index < mStreams[stream].mIndexMin) || (index > mStreams[stream].mIndexMax)) {
972f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
973f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
974c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    if (!audio_is_output_device(device)) {
975c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        return BAD_VALUE;
976c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    }
977f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
978f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // Force max volume if stream cannot be muted
979f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (!mStreams[stream].mCanBeMuted) index = mStreams[stream].mIndexMax;
980f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
981b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    ALOGV("setStreamVolumeIndex() stream %d, device %04x, index %d",
982c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent          stream, device, index);
983c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
984c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    // if device is AUDIO_DEVICE_OUT_DEFAULT set default value and
985c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    // clear all device specific values
986c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    if (device == AUDIO_DEVICE_OUT_DEFAULT) {
987c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        mStreams[stream].mIndexCur.clear();
988c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    }
989c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    mStreams[stream].mIndexCur.add(device, index);
990f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
991f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // compute and apply stream volume on all outputs according to connected device
992f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    status_t status = NO_ERROR;
993f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mOutputs.size(); i++) {
994c5eb8b4a5d4395ce335bc7c3e6df2678fa47e2ddEric Laurent        audio_devices_t curDevice =
995c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                getDeviceForVolume(mOutputs.valueAt(i)->device());
996c5eb8b4a5d4395ce335bc7c3e6df2678fa47e2ddEric Laurent        if (device == curDevice) {
997c5eb8b4a5d4395ce335bc7c3e6df2678fa47e2ddEric Laurent            status_t volStatus = checkAndSetVolume(stream, index, mOutputs.keyAt(i), curDevice);
998c5eb8b4a5d4395ce335bc7c3e6df2678fa47e2ddEric Laurent            if (volStatus != NO_ERROR) {
999c5eb8b4a5d4395ce335bc7c3e6df2678fa47e2ddEric Laurent                status = volStatus;
1000c5eb8b4a5d4395ce335bc7c3e6df2678fa47e2ddEric Laurent            }
1001f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1002f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1003f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return status;
1004f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1005f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1006c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentstatus_t AudioPolicyManagerBase::getStreamVolumeIndex(AudioSystem::stream_type stream,
1007c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                      int *index,
1008c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                      audio_devices_t device)
1009f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1010c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    if (index == NULL) {
1011f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
1012f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1013c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    if (!audio_is_output_device(device)) {
1014c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        return BAD_VALUE;
1015c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    }
1016c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    // if device is AUDIO_DEVICE_OUT_DEFAULT, return volume for device corresponding to
1017c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    // the strategy the stream belongs to.
1018c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    if (device == AUDIO_DEVICE_OUT_DEFAULT) {
1019c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
1020c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    }
1021c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    device = getDeviceForVolume(device);
1022c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
1023c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    *index =  mStreams[stream].getVolumeIndex(device);
1024c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index);
1025f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
1026f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1027f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1028c94dccc97cc3ed5171b45f46a0f7f8762d37156fGlenn Kastenaudio_io_handle_t AudioPolicyManagerBase::getOutputForEffect(const effect_descriptor_t *desc)
1029f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
10306a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("getOutputForEffect()");
1031f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // apply simple rule where global effects are attached to the same output as MUSIC streams
10324660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen
10334660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen    routing_strategy strategy = getStrategy(AudioSystem::MUSIC);
10344660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen    audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
1035c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs);
10364660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen    int outIdx = 0;
10374660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen    for (size_t i = 0; i < dstOutputs.size(); i++) {
10384660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen        AudioOutputDescriptor *desc = mOutputs.valueFor(dstOutputs[i]);
10394660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen        if (desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
10404660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen            outIdx = i;
10414660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen        }
10424660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen    }
10434660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen    return dstOutputs[outIdx];
1044f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1045f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1046c94dccc97cc3ed5171b45f46a0f7f8762d37156fGlenn Kastenstatus_t AudioPolicyManagerBase::registerEffect(const effect_descriptor_t *desc,
10471c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent                                audio_io_handle_t io,
1048f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                uint32_t strategy,
1049f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                int session,
1050f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                int id)
1051f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
10521c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent    ssize_t index = mOutputs.indexOfKey(io);
1053f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
10541c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent        index = mInputs.indexOfKey(io);
10551c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent        if (index < 0) {
105664cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("registerEffect() unknown io %d", io);
10571c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent            return INVALID_OPERATION;
10581c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent        }
1059f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1060f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1061f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mTotalEffectsMemory + desc->memoryUsage > getMaxEffectsMemory()) {
106264cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("registerEffect() memory limit exceeded for Fx %s, Memory %d KB",
1063f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                desc->name, desc->memoryUsage);
1064f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return INVALID_OPERATION;
1065f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1066f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mTotalEffectsMemory += desc->memoryUsage;
10676a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("registerEffect() effect %s, io %d, strategy %d session %d id %d",
10681c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent            desc->name, io, strategy, session, id);
10696a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("registerEffect() memory %d, total memory %d", desc->memoryUsage, mTotalEffectsMemory);
1070f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1071f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    EffectDescriptor *pDesc = new EffectDescriptor();
1072f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    memcpy (&pDesc->mDesc, desc, sizeof(effect_descriptor_t));
10731c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent    pDesc->mIo = io;
1074f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    pDesc->mStrategy = (routing_strategy)strategy;
1075f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    pDesc->mSession = session;
1076582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    pDesc->mEnabled = false;
1077582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent
1078f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mEffects.add(id, pDesc);
1079f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1080f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
1081f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1082f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1083f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::unregisterEffect(int id)
1084f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1085f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mEffects.indexOfKey(id);
1086f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
108764cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("unregisterEffect() unknown effect ID %d", id);
1088f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return INVALID_OPERATION;
1089f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1090f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1091f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    EffectDescriptor *pDesc = mEffects.valueAt(index);
1092f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1093582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    setEffectEnabled(pDesc, false);
1094582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent
1095f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mTotalEffectsMemory < pDesc->mDesc.memoryUsage) {
109664cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("unregisterEffect() memory %d too big for total %d",
1097f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                pDesc->mDesc.memoryUsage, mTotalEffectsMemory);
1098f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        pDesc->mDesc.memoryUsage = mTotalEffectsMemory;
1099f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1100f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mTotalEffectsMemory -= pDesc->mDesc.memoryUsage;
11016a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("unregisterEffect() effect %s, ID %d, memory %d total memory %d",
1102582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent            pDesc->mDesc.name, id, pDesc->mDesc.memoryUsage, mTotalEffectsMemory);
1103f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1104f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mEffects.removeItem(id);
1105f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    delete pDesc;
1106f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1107f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
1108f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1109f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1110582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurentstatus_t AudioPolicyManagerBase::setEffectEnabled(int id, bool enabled)
1111582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent{
1112582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    ssize_t index = mEffects.indexOfKey(id);
1113582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    if (index < 0) {
111464cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("unregisterEffect() unknown effect ID %d", id);
1115582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        return INVALID_OPERATION;
1116582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    }
1117582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent
1118582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    return setEffectEnabled(mEffects.valueAt(index), enabled);
1119582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent}
1120582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent
1121582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurentstatus_t AudioPolicyManagerBase::setEffectEnabled(EffectDescriptor *pDesc, bool enabled)
1122582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent{
1123582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    if (enabled == pDesc->mEnabled) {
11246a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("setEffectEnabled(%s) effect already %s",
1125582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent             enabled?"true":"false", enabled?"enabled":"disabled");
1126582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        return INVALID_OPERATION;
1127582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    }
1128582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent
1129582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    if (enabled) {
1130582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        if (mTotalEffectsCpuLoad + pDesc->mDesc.cpuLoad > getMaxEffectsCpuLoad()) {
113164cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("setEffectEnabled(true) CPU Load limit exceeded for Fx %s, CPU %f MIPS",
1132582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent                 pDesc->mDesc.name, (float)pDesc->mDesc.cpuLoad/10);
1133582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent            return INVALID_OPERATION;
1134582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        }
1135582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        mTotalEffectsCpuLoad += pDesc->mDesc.cpuLoad;
11366a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("setEffectEnabled(true) total CPU %d", mTotalEffectsCpuLoad);
1137582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    } else {
1138582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        if (mTotalEffectsCpuLoad < pDesc->mDesc.cpuLoad) {
113964cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("setEffectEnabled(false) CPU load %d too high for total %d",
1140582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent                    pDesc->mDesc.cpuLoad, mTotalEffectsCpuLoad);
1141582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent            pDesc->mDesc.cpuLoad = mTotalEffectsCpuLoad;
1142582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        }
1143582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        mTotalEffectsCpuLoad -= pDesc->mDesc.cpuLoad;
11446a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("setEffectEnabled(false) total CPU %d", mTotalEffectsCpuLoad);
1145582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    }
1146582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    pDesc->mEnabled = enabled;
1147582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    return NO_ERROR;
1148582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent}
1149582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent
1150f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinbool AudioPolicyManagerBase::isStreamActive(int stream, uint32_t inPastMs) const
1151f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1152f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    nsecs_t sysTime = systemTime();
1153f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mOutputs.size(); i++) {
1154f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (mOutputs.valueAt(i)->mRefCount[stream] != 0 ||
1155f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            ns2ms(sysTime - mOutputs.valueAt(i)->mStopTime[stream]) < inPastMs) {
1156f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return true;
1157f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1158f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1159f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return false;
1160f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1161f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1162abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivibool AudioPolicyManagerBase::isSourceActive(audio_source_t source) const
1163abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi{
1164abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi    for (size_t i = 0; i < mInputs.size(); i++) {
1165abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi        const AudioInputDescriptor * inputDescriptor = mInputs.valueAt(i);
1166abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi        if ((inputDescriptor->mInputSource == (int) source)
1167abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi                && (inputDescriptor->mRefCount > 0)) {
1168abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi            return true;
1169abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi        }
1170abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi    }
1171abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi    return false;
1172abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi}
1173abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi
1174abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi
1175f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1176f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::dump(int fd)
1177f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1178f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    const size_t SIZE = 256;
1179f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    char buffer[SIZE];
1180f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    String8 result;
1181f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1182f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
1183f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1184b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
118570c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, " Primary Output: %d\n", mPrimaryOutput);
1186f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1187f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " A2DP device address: %s\n", mA2dpDeviceAddress.string());
1188f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1189f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " SCO device address: %s\n", mScoDeviceAddress.string());
1190f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1191599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    snprintf(buffer, SIZE, " USB audio ALSA %s\n", mUsbCardAndDevice.string());
1192599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    result.append(buffer);
1193f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Output devices: %08x\n", mAvailableOutputDevices);
1194f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1195f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Input devices: %08x\n", mAvailableInputDevices);
1196f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1197f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Phone state: %d\n", mPhoneState);
1198f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1199f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Force use for communications %d\n", mForceUse[AudioSystem::FOR_COMMUNICATION]);
1200f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1201f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Force use for media %d\n", mForceUse[AudioSystem::FOR_MEDIA]);
1202f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1203f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Force use for record %d\n", mForceUse[AudioSystem::FOR_RECORD]);
1204f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1205f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Force use for dock %d\n", mForceUse[AudioSystem::FOR_DOCK]);
1206f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1207738207def5f691d605ae33d041116829a74513a9Eric Laurent    snprintf(buffer, SIZE, " Force use for system %d\n", mForceUse[AudioSystem::FOR_SYSTEM]);
1208738207def5f691d605ae33d041116829a74513a9Eric Laurent    result.append(buffer);
1209f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, result.string(), result.size());
1210f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
12115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
121270c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "\nHW Modules dump:\n");
12135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    write(fd, buffer, strlen(buffer));
121470c236c9290732782d5267935af1475b8d5ae602Eric Laurent    for (size_t i = 0; i < mHwModules.size(); i++) {
121570c236c9290732782d5267935af1475b8d5ae602Eric Laurent        snprintf(buffer, SIZE, "- HW Module %d:\n", i + 1);
12165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        write(fd, buffer, strlen(buffer));
121770c236c9290732782d5267935af1475b8d5ae602Eric Laurent        mHwModules[i]->dump(fd);
12185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
12195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
1220f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "\nOutputs dump:\n");
1221f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
1222f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mOutputs.size(); i++) {
1223f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        snprintf(buffer, SIZE, "- Output %d dump:\n", mOutputs.keyAt(i));
1224f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        write(fd, buffer, strlen(buffer));
1225f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mOutputs.valueAt(i)->dump(fd);
1226f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1227f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1228f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "\nInputs dump:\n");
1229f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
1230f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mInputs.size(); i++) {
1231f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        snprintf(buffer, SIZE, "- Input %d dump:\n", mInputs.keyAt(i));
1232f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        write(fd, buffer, strlen(buffer));
1233f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mInputs.valueAt(i)->dump(fd);
1234f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1235f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1236f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "\nStreams dump:\n");
1237f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
1238c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    snprintf(buffer, SIZE,
1239c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent             " Stream  Can be muted  Index Min  Index Max  Index Cur [device : index]...\n");
1240f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
1241f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) {
1242c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        snprintf(buffer, SIZE, " %02d      ", i);
1243f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        write(fd, buffer, strlen(buffer));
1244c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        mStreams[i].dump(fd);
1245f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1246f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1247f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "\nTotal Effects CPU: %f MIPS, Total Effects memory: %d KB\n",
1248f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            (float)mTotalEffectsCpuLoad/10, mTotalEffectsMemory);
1249f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
1250f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1251f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "Registered effects:\n");
1252f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
1253f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mEffects.size(); i++) {
1254f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        snprintf(buffer, SIZE, "- Effect %d dump:\n", mEffects.keyAt(i));
1255f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        write(fd, buffer, strlen(buffer));
1256f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mEffects.valueAt(i)->dump(fd);
1257f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1258f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1259f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1260f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
1261f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1262f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1263f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// ----------------------------------------------------------------------------
1264f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// AudioPolicyManagerBase
1265f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// ----------------------------------------------------------------------------
1266f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1267f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima ZavinAudioPolicyManagerBase::AudioPolicyManagerBase(AudioPolicyClientInterface *clientInterface)
1268f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    :
1269f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
1270f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    Thread(false),
1271f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
127270c236c9290732782d5267935af1475b8d5ae602Eric Laurent    mPrimaryOutput((audio_io_handle_t)0),
1273ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    mAvailableOutputDevices(AUDIO_DEVICE_NONE),
1274ca0657a1ca087a6d474a75fcfedd6aac3901d587Glenn Kasten    mPhoneState(AudioSystem::MODE_NORMAL),
1275f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
1276f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mTotalEffectsCpuLoad(0), mTotalEffectsMemory(0),
127748387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi    mA2dpSuspended(false), mHasA2dp(false), mHasUsb(false), mHasRemoteSubmix(false)
1278f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1279f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mpClientInterface = clientInterface;
1280f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1281f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < AudioSystem::NUM_FORCE_USE; i++) {
1282f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mForceUse[i] = AudioSystem::FORCE_NONE;
1283f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1284f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1285f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    initializeVolumeCurves();
1286f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1287f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mA2dpDeviceAddress = String8("");
1288f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mScoDeviceAddress = String8("");
1289599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    mUsbCardAndDevice = String8("");
1290f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
12915ec145df7708564d385fd3fb764085321cf4c253Dima Zavin    if (loadAudioPolicyConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE) != NO_ERROR) {
12925ec145df7708564d385fd3fb764085321cf4c253Dima Zavin        if (loadAudioPolicyConfig(AUDIO_POLICY_CONFIG_FILE) != NO_ERROR) {
1293739022f26a7127ba76a98dda65411496086114a7Dima Zavin            ALOGE("could not load audio policy configuration file, setting defaults");
1294739022f26a7127ba76a98dda65411496086114a7Dima Zavin            defaultAudioPolicyConfig();
12955ec145df7708564d385fd3fb764085321cf4c253Dima Zavin        }
12965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
12975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
1298b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    // open all output streams needed to access attached devices
129970c236c9290732782d5267935af1475b8d5ae602Eric Laurent    for (size_t i = 0; i < mHwModules.size(); i++) {
130070c236c9290732782d5267935af1475b8d5ae602Eric Laurent        mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->mName);
130170c236c9290732782d5267935af1475b8d5ae602Eric Laurent        if (mHwModules[i]->mHandle == 0) {
130270c236c9290732782d5267935af1475b8d5ae602Eric Laurent            ALOGW("could not open HW module %s", mHwModules[i]->mName);
130370c236c9290732782d5267935af1475b8d5ae602Eric Laurent            continue;
130470c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
130570c236c9290732782d5267935af1475b8d5ae602Eric Laurent        // open all output streams needed to access attached devices
130670c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
130770c236c9290732782d5267935af1475b8d5ae602Eric Laurent        {
130870c236c9290732782d5267935af1475b8d5ae602Eric Laurent            const IOProfile *outProfile = mHwModules[i]->mOutputProfiles[j];
130970c236c9290732782d5267935af1475b8d5ae602Eric Laurent
131070c236c9290732782d5267935af1475b8d5ae602Eric Laurent            if (outProfile->mSupportedDevices & mAttachedOutputDevices) {
131170c236c9290732782d5267935af1475b8d5ae602Eric Laurent                AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(outProfile);
131270c236c9290732782d5267935af1475b8d5ae602Eric Laurent                outputDesc->mDevice = (audio_devices_t)(mDefaultOutputDevice &
131370c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                            outProfile->mSupportedDevices);
131470c236c9290732782d5267935af1475b8d5ae602Eric Laurent                audio_io_handle_t output = mpClientInterface->openOutput(
131570c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                outProfile->mModule->mHandle,
131670c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mDevice,
131770c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mSamplingRate,
131870c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mFormat,
131970c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mChannelMask,
132070c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mLatency,
132170c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                outputDesc->mFlags);
132270c236c9290732782d5267935af1475b8d5ae602Eric Laurent                if (output == 0) {
132370c236c9290732782d5267935af1475b8d5ae602Eric Laurent                    delete outputDesc;
132470c236c9290732782d5267935af1475b8d5ae602Eric Laurent                } else {
132570c236c9290732782d5267935af1475b8d5ae602Eric Laurent                    mAvailableOutputDevices = (audio_devices_t)(mAvailableOutputDevices |
132670c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                            (outProfile->mSupportedDevices & mAttachedOutputDevices));
132770c236c9290732782d5267935af1475b8d5ae602Eric Laurent                    if (mPrimaryOutput == 0 &&
13280977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent                            outProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
132970c236c9290732782d5267935af1475b8d5ae602Eric Laurent                        mPrimaryOutput = output;
133070c236c9290732782d5267935af1475b8d5ae602Eric Laurent                    }
133170c236c9290732782d5267935af1475b8d5ae602Eric Laurent                    addOutput(output, outputDesc);
133270c236c9290732782d5267935af1475b8d5ae602Eric Laurent                    setOutputDevice(output,
133370c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                    (audio_devices_t)(mDefaultOutputDevice &
133470c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                        outProfile->mSupportedDevices),
133570c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                    true);
1336b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                }
1337b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            }
1338b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        }
1339f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1340f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
13415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGE_IF((mAttachedOutputDevices & ~mAvailableOutputDevices),
1342b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent             "Not output found for attached devices %08x",
13435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent             (mAttachedOutputDevices & ~mAvailableOutputDevices));
1344b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1345b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output");
1346b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1347c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    updateDevicesAndOutputs();
13483cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
1349f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
1350b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (mPrimaryOutput != 0) {
1351f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioParameter outputCmd = AudioParameter();
1352f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputCmd.addInt(String8("set_id"), 0);
1353b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        mpClientInterface->setParameters(mPrimaryOutput, outputCmd.toString());
1354f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1355c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        mTestDevice = AUDIO_DEVICE_OUT_SPEAKER;
1356f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mTestSamplingRate = 44100;
1357f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mTestFormat = AudioSystem::PCM_16_BIT;
1358f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mTestChannels =  AudioSystem::CHANNEL_OUT_STEREO;
1359f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mTestLatencyMs = 0;
1360f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mCurOutput = 0;
1361f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mDirectOutput = false;
1362f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
1363f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mTestOutputs[i] = 0;
1364f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1365f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1366f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        const size_t SIZE = 256;
1367f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        char buffer[SIZE];
1368f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        snprintf(buffer, SIZE, "AudioPolicyManagerTest");
1369f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        run(buffer, ANDROID_PRIORITY_AUDIO);
1370f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1371f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
1372f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1373f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1374f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima ZavinAudioPolicyManagerBase::~AudioPolicyManagerBase()
1375f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1376f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
1377f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    exit();
1378f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
1379f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin   for (size_t i = 0; i < mOutputs.size(); i++) {
1380f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mpClientInterface->closeOutput(mOutputs.keyAt(i));
1381f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        delete mOutputs.valueAt(i);
1382f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin   }
1383f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin   for (size_t i = 0; i < mInputs.size(); i++) {
1384f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mpClientInterface->closeInput(mInputs.keyAt(i));
1385f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        delete mInputs.valueAt(i);
1386f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin   }
138770c236c9290732782d5267935af1475b8d5ae602Eric Laurent   for (size_t i = 0; i < mHwModules.size(); i++) {
138870c236c9290732782d5267935af1475b8d5ae602Eric Laurent        delete mHwModules[i];
13895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent   }
1390f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1391f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1392f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::initCheck()
1393f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1394b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    return (mPrimaryOutput == 0) ? NO_INIT : NO_ERROR;
1395f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1396f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1397f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
1398f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinbool AudioPolicyManagerBase::threadLoop()
1399f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
14006a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("entering threadLoop()");
1401f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    while (!exitPending())
1402f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    {
1403f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        String8 command;
1404f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        int valueInt;
1405f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        String8 value;
1406f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1407f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        Mutex::Autolock _l(mLock);
1408f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mWaitWorkCV.waitRelative(mLock, milliseconds(50));
1409f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1410f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        command = mpClientInterface->getParameters(0, String8("test_cmd_policy"));
1411f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioParameter param = AudioParameter(command);
1412f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1413f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR &&
1414f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            valueInt != 0) {
14156a70518b93928d1c91457ff805e375c82d76b0e5Steve Block            ALOGV("Test command %s received", command.string());
1416f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            String8 target;
1417f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.get(String8("target"), target) != NO_ERROR) {
1418f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                target = "Manager";
1419f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1420f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) {
1421f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_output"));
1422f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mCurOutput = valueInt;
1423f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1424f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) {
1425f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_direct"));
1426f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (value == "false") {
1427f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    mDirectOutput = false;
1428f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else if (value == "true") {
1429f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    mDirectOutput = true;
1430f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1431f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1432f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) {
1433f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_input"));
1434f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mTestInput = valueInt;
1435f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1436f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1437f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) {
1438f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_format"));
1439f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                int format = AudioSystem::INVALID_FORMAT;
1440f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (value == "PCM 16 bits") {
1441f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    format = AudioSystem::PCM_16_BIT;
1442f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else if (value == "PCM 8 bits") {
1443f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    format = AudioSystem::PCM_8_BIT;
1444f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else if (value == "Compressed MP3") {
1445f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    format = AudioSystem::MP3;
1446f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1447f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (format != AudioSystem::INVALID_FORMAT) {
1448f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    if (target == "Manager") {
1449f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mTestFormat = format;
1450f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    } else if (mTestOutputs[mCurOutput] != 0) {
1451f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        AudioParameter outputParam = AudioParameter();
1452f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        outputParam.addInt(String8("format"), format);
1453f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
1454f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    }
1455f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1456f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1457f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) {
1458f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_channels"));
1459f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                int channels = 0;
1460f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1461f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (value == "Channels Stereo") {
1462f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    channels =  AudioSystem::CHANNEL_OUT_STEREO;
1463f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else if (value == "Channels Mono") {
1464f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    channels =  AudioSystem::CHANNEL_OUT_MONO;
1465f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1466f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (channels != 0) {
1467f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    if (target == "Manager") {
1468f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mTestChannels = channels;
1469f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    } else if (mTestOutputs[mCurOutput] != 0) {
1470f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        AudioParameter outputParam = AudioParameter();
1471f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        outputParam.addInt(String8("channels"), channels);
1472f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
1473f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    }
1474f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1475f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1476f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) {
1477f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_sampleRate"));
1478f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (valueInt >= 0 && valueInt <= 96000) {
1479f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    int samplingRate = valueInt;
1480f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    if (target == "Manager") {
1481f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mTestSamplingRate = samplingRate;
1482f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    } else if (mTestOutputs[mCurOutput] != 0) {
1483f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        AudioParameter outputParam = AudioParameter();
1484f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        outputParam.addInt(String8("sampling_rate"), samplingRate);
1485f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
1486f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    }
1487f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1488f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1489f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1490f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) {
1491f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_reopen"));
1492f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
149370c236c9290732782d5267935af1475b8d5ae602Eric Laurent                AudioOutputDescriptor *outputDesc = mOutputs.valueFor(mPrimaryOutput);
1494b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                mpClientInterface->closeOutput(mPrimaryOutput);
149570c236c9290732782d5267935af1475b8d5ae602Eric Laurent
149670c236c9290732782d5267935af1475b8d5ae602Eric Laurent                audio_module_handle_t moduleHandle = outputDesc->mModule->mHandle;
149770c236c9290732782d5267935af1475b8d5ae602Eric Laurent
1498b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                delete mOutputs.valueFor(mPrimaryOutput);
1499b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                mOutputs.removeItem(mPrimaryOutput);
1500f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1501b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(NULL);
1502c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER;
150370c236c9290732782d5267935af1475b8d5ae602Eric Laurent                mPrimaryOutput = mpClientInterface->openOutput(moduleHandle,
150470c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mDevice,
1505f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                &outputDesc->mSamplingRate,
1506f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                &outputDesc->mFormat,
150770c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mChannelMask,
1508f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                &outputDesc->mLatency,
1509f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                outputDesc->mFlags);
1510b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                if (mPrimaryOutput == 0) {
15115efbd421e0029d6fc44b1cc65c0e5e0d85e5161fSteve Block                    ALOGE("Failed to reopen hardware output stream, samplingRate: %d, format %d, channels %d",
151270c236c9290732782d5267935af1475b8d5ae602Eric Laurent                            outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask);
1513f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else {
1514f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    AudioParameter outputCmd = AudioParameter();
1515f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    outputCmd.addInt(String8("set_id"), 0);
1516b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                    mpClientInterface->setParameters(mPrimaryOutput, outputCmd.toString());
1517b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                    addOutput(mPrimaryOutput, outputDesc);
1518f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1519f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1520f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1521f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1522f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mpClientInterface->setParameters(0, String8("test_cmd_policy="));
1523f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1524f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1525f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return false;
1526f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1527f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1528f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::exit()
1529f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1530f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    {
1531f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AutoMutex _l(mLock);
1532f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        requestExit();
1533f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mWaitWorkCV.signal();
1534f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1535f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    requestExitAndWait();
1536f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1537f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1538f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinint AudioPolicyManagerBase::testOutputIndex(audio_io_handle_t output)
1539f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1540f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
1541f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (output == mTestOutputs[i]) return i;
1542f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1543f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return 0;
1544f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1545f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
1546f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1547f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// ---
1548f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1549f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::addOutput(audio_io_handle_t id, AudioOutputDescriptor *outputDesc)
1550f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1551f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    outputDesc->mId = id;
1552f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mOutputs.add(id, outputDesc);
1553f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1554f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1555f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
15563cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurentstatus_t AudioPolicyManagerBase::checkOutputsForDevice(audio_devices_t device,
15573cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                       AudioSystem::device_connection_state state,
15583cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                       SortedVector<audio_io_handle_t>& outputs)
1559f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
15603cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    AudioOutputDescriptor *desc;
1561b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1562b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (state == AudioSystem::DEVICE_STATE_AVAILABLE) {
15633cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        // first list already open outputs that can be routed to this device
1564b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        for (size_t i = 0; i < mOutputs.size(); i++) {
15653cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            desc = mOutputs.valueAt(i);
15663cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            if (!desc->isDuplicated() && (desc->mProfile->mSupportedDevices & device)) {
15673cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i));
15683cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                outputs.add(mOutputs.keyAt(i));
1569b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            }
1570b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        }
15713cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        // then look for output profiles that can be routed to this device
15723cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        SortedVector<IOProfile *> profiles;
157370c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (size_t i = 0; i < mHwModules.size(); i++)
1574b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        {
157570c236c9290732782d5267935af1475b8d5ae602Eric Laurent            if (mHwModules[i]->mHandle == 0) {
157670c236c9290732782d5267935af1475b8d5ae602Eric Laurent                continue;
157770c236c9290732782d5267935af1475b8d5ae602Eric Laurent            }
157870c236c9290732782d5267935af1475b8d5ae602Eric Laurent            for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
157970c236c9290732782d5267935af1475b8d5ae602Eric Laurent            {
158070c236c9290732782d5267935af1475b8d5ae602Eric Laurent                if (mHwModules[i]->mOutputProfiles[j]->mSupportedDevices & device) {
15813cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    ALOGV("checkOutputsForDevice(): adding profile %d from module %d", j, i);
15823cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    profiles.add(mHwModules[i]->mOutputProfiles[j]);
158370c236c9290732782d5267935af1475b8d5ae602Eric Laurent                }
158470c236c9290732782d5267935af1475b8d5ae602Eric Laurent            }
1585b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        }
158670c236c9290732782d5267935af1475b8d5ae602Eric Laurent
15873cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        if (profiles.isEmpty() && outputs.isEmpty()) {
15883cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
15893cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            return BAD_VALUE;
159070c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
159170c236c9290732782d5267935af1475b8d5ae602Eric Laurent
15923cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        // open outputs for matching profiles if needed. Direct outputs are also opened to
15933cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        // query for dynamic parameters and will be closed later by setDeviceConnectionState()
15943cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
15953cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            IOProfile *profile = profiles[profile_index];
1596b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
15973cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            // nothing to do if one output is already opened for this profile
15983cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            size_t j;
15993cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            for (j = 0; j < outputs.size(); j++) {
16003cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                desc = mOutputs.valueAt(j);
16013cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                if (!desc->isDuplicated() && desc->mProfile == profile) {
16023cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    break;
16033cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                }
16043cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
16053cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            if (j != outputs.size()) {
16063cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                continue;
16073cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
16083cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
16093cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            ALOGV("opening output for device %08x", device);
16103cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            desc = new AudioOutputDescriptor(profile);
16113cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            desc->mDevice = device;
16123cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            audio_io_handle_t output = mpClientInterface->openOutput(profile->mModule->mHandle,
16133cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                       &desc->mDevice,
16143cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                       &desc->mSamplingRate,
16153cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                       &desc->mFormat,
16163cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                       &desc->mChannelMask,
16173cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                       &desc->mLatency,
16183cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                       desc->mFlags);
16193cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            if (output != 0) {
16203cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
16213cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    String8 reply;
16223cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    char *value;
16233cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    if (profile->mSamplingRates[0] == 0) {
16243cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        reply = mpClientInterface->getParameters(output,
16253cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES));
16263cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        ALOGV("checkOutputsForDevice() direct output sup sampling rates %s",
16273cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                  reply.string());
16283cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        value = strpbrk((char *)reply.string(), "=");
16293cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        if (value != NULL) {
16303cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                            loadSamplingRates(value, profile);
16313cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        }
16323cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
16333cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    if (profile->mFormats[0] == 0) {
16343cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        reply = mpClientInterface->getParameters(output,
16353cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                       String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
16363cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        ALOGV("checkOutputsForDevice() direct output sup formats %s",
16373cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                  reply.string());
16383cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        value = strpbrk((char *)reply.string(), "=");
16393cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        if (value != NULL) {
16403cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                            loadFormats(value, profile);
16413cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        }
16423cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
16433cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    if (profile->mChannelMasks[0] == 0) {
16443cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        reply = mpClientInterface->getParameters(output,
16453cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                      String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS));
16463cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        ALOGV("checkOutputsForDevice() direct output sup channel masks %s",
16473cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                  reply.string());
16483cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        value = strpbrk((char *)reply.string(), "=");
16493cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        if (value != NULL) {
16503cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                            loadOutChannels(value + 1, profile);
16513cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        }
16523cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
16533cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    if (((profile->mSamplingRates[0] == 0) &&
16543cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                             (profile->mSamplingRates.size() < 2)) ||
16553cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                         ((profile->mFormats[0] == 0) &&
16563cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                             (profile->mFormats.size() < 2)) ||
16573cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                         ((profile->mFormats[0] == 0) &&
16583cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                             (profile->mChannelMasks.size() < 2))) {
16593cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        ALOGW("checkOutputsForDevice() direct output missing param");
16603cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        output = 0;
16613cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    } else {
16623cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        addOutput(output, desc);
16633cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
16643cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                } else {
16653cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    audio_io_handle_t duplicatedOutput = 0;
16663cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    // add output descriptor
16673cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    addOutput(output, desc);
16683cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    // set initial stream volume for device
16694366b4a6735e5da342b56773073f0b41197c777fEric Laurent                    applyStreamVolumes(output, device, 0, true);
16703cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
16713cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    //TODO: configure audio effect output stage here
16723cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
16733cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    // open a duplicating output thread for the new output and the primary output
16743cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    duplicatedOutput = mpClientInterface->openDuplicateOutput(output,
16753cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                              mPrimaryOutput);
16763cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    if (duplicatedOutput != 0) {
16773cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        // add duplicated output descriptor
16783cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        AudioOutputDescriptor *dupOutputDesc = new AudioOutputDescriptor(NULL);
16793cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        dupOutputDesc->mOutput1 = mOutputs.valueFor(mPrimaryOutput);
16803cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        dupOutputDesc->mOutput2 = mOutputs.valueFor(output);
16813cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        dupOutputDesc->mSamplingRate = desc->mSamplingRate;
16823cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        dupOutputDesc->mFormat = desc->mFormat;
16833cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        dupOutputDesc->mChannelMask = desc->mChannelMask;
16843cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        dupOutputDesc->mLatency = desc->mLatency;
16853cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        addOutput(duplicatedOutput, dupOutputDesc);
16864366b4a6735e5da342b56773073f0b41197c777fEric Laurent                        applyStreamVolumes(duplicatedOutput, device, 0, true);
16873cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    } else {
16883cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        ALOGW("checkOutputsForDevice() could not open dup output for %d and %d",
16893cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                mPrimaryOutput, output);
16903cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        mpClientInterface->closeOutput(output);
16913cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        mOutputs.removeItem(output);
16923cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        output = 0;
16933cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
16943cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                }
16953cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
16963cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            if (output == 0) {
16973cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                ALOGW("checkOutputsForDevice() could not open output for device %x", device);
16983cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                delete desc;
16993cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                profiles.removeAt(profile_index);
17003cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                profile_index--;
1701b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            } else {
17023cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                outputs.add(output);
17033cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                ALOGV("checkOutputsForDevice(): adding output %d", output);
1704f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
17053cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        }
17063cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
17073cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        if (profiles.isEmpty()) {
17083cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
17093cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            return BAD_VALUE;
1710f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1711f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
1712b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        // check if one opened output is not needed any more after disconnecting one device
1713b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        for (size_t i = 0; i < mOutputs.size(); i++) {
17143cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            desc = mOutputs.valueAt(i);
17153cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            if (!desc->isDuplicated() &&
17163cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    !(desc->mProfile->mSupportedDevices & mAvailableOutputDevices)) {
17173cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                ALOGV("checkOutputsForDevice(): disconnecting adding output %d", mOutputs.keyAt(i));
17183cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                outputs.add(mOutputs.keyAt(i));
17193cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
17203cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        }
17213cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        for (size_t i = 0; i < mHwModules.size(); i++)
17223cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        {
17233cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            if (mHwModules[i]->mHandle == 0) {
17243cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                continue;
17253cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
17263cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
17273cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            {
17283cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                IOProfile *profile = mHwModules[i]->mOutputProfiles[j];
17293cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                if ((profile->mSupportedDevices & device) &&
17303cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        (profile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
17313cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    ALOGV("checkOutputsForDevice(): clearing direct output profile %d on module %d",
17323cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                          j, i);
17333cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    if (profile->mSamplingRates[0] == 0) {
17343cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        profile->mSamplingRates.clear();
17353cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        profile->mSamplingRates.add(0);
17363cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
17373cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    if (profile->mFormats[0] == 0) {
17383cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        profile->mFormats.clear();
17393cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        profile->mFormats.add((audio_format_t)0);
17403cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
17413cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    if (profile->mChannelMasks[0] == 0) {
17423cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        profile->mChannelMasks.clear();
17433cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        profile->mChannelMasks.add((audio_channel_mask_t)0);
17443cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
17453cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                }
1746b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            }
1747f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1748f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
17493cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    return NO_ERROR;
1750f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1751f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1752b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurentvoid AudioPolicyManagerBase::closeOutput(audio_io_handle_t output)
1753f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1754b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    ALOGV("closeOutput(%d)", output);
1755f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1756b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
1757b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (outputDesc == NULL) {
1758b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        ALOGW("closeOutput() unknown output %d", output);
1759b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        return;
1760f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1761f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1762b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    // look for duplicated outputs connected to the output being removed.
1763b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    for (size_t i = 0; i < mOutputs.size(); i++) {
1764b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        AudioOutputDescriptor *dupOutputDesc = mOutputs.valueAt(i);
1765b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        if (dupOutputDesc->isDuplicated() &&
1766b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                (dupOutputDesc->mOutput1 == outputDesc ||
1767b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                dupOutputDesc->mOutput2 == outputDesc)) {
1768b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            AudioOutputDescriptor *outputDesc2;
1769b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            if (dupOutputDesc->mOutput1 == outputDesc) {
1770b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                outputDesc2 = dupOutputDesc->mOutput2;
1771b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            } else {
1772b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                outputDesc2 = dupOutputDesc->mOutput1;
1773b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            }
1774b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            // As all active tracks on duplicated output will be deleted,
1775b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            // and as they were also referenced on the other output, the reference
1776b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            // count for their stream type must be adjusted accordingly on
1777b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            // the other output.
1778b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            for (int j = 0; j < (int)AudioSystem::NUM_STREAM_TYPES; j++) {
1779b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                int refCount = dupOutputDesc->mRefCount[j];
1780b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                outputDesc2->changeRefCount((AudioSystem::stream_type)j,-refCount);
1781b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            }
1782b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i);
1783b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput);
1784f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1785b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            mpClientInterface->closeOutput(duplicatedOutput);
1786b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            delete mOutputs.valueFor(duplicatedOutput);
1787b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            mOutputs.removeItem(duplicatedOutput);
1788f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1789f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1790b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1791b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    AudioParameter param;
1792b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    param.add(String8("closing"), String8("true"));
1793b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    mpClientInterface->setParameters(output, param.toString());
1794b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1795b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    mpClientInterface->closeOutput(output);
1796b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    delete mOutputs.valueFor(output);
1797b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    mOutputs.removeItem(output);
1798f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1799f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1800c952527e6f89d5427881462823514be9d79f13e6Eric LaurentSortedVector<audio_io_handle_t> AudioPolicyManagerBase::getOutputsForDevice(audio_devices_t device,
1801c952527e6f89d5427881462823514be9d79f13e6Eric Laurent                        DefaultKeyedVector<audio_io_handle_t, AudioOutputDescriptor *> openOutputs)
1802f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1803b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    SortedVector<audio_io_handle_t> outputs;
1804f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
18053cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGVV("getOutputsForDevice() device %04x", device);
1806c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    for (size_t i = 0; i < openOutputs.size(); i++) {
18073cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        ALOGVV("output %d isDuplicated=%d device=%04x",
1808c952527e6f89d5427881462823514be9d79f13e6Eric Laurent                i, openOutputs.valueAt(i)->isDuplicated(), openOutputs.valueAt(i)->supportedDevices());
1809c952527e6f89d5427881462823514be9d79f13e6Eric Laurent        if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) {
1810c952527e6f89d5427881462823514be9d79f13e6Eric Laurent            ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i));
1811c952527e6f89d5427881462823514be9d79f13e6Eric Laurent            outputs.add(openOutputs.keyAt(i));
1812f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1813f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1814b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    return outputs;
1815f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1816f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1817b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurentbool AudioPolicyManagerBase::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
1818b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                                   SortedVector<audio_io_handle_t>& outputs2)
1819f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1820b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (outputs1.size() != outputs2.size()) {
1821b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        return false;
1822f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1823b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    for (size_t i = 0; i < outputs1.size(); i++) {
1824b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        if (outputs1[i] != outputs2[i]) {
1825b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            return false;
1826f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1827f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1828b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    return true;
1829b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent}
1830b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1831b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurentvoid AudioPolicyManagerBase::checkOutputForStrategy(routing_strategy strategy)
1832b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent{
183301e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent    audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
183401e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent    audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
1835c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs);
1836c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
1837b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1838b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (!vectorsEqual(srcOutputs,dstOutputs)) {
1839b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d",
1840b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent              strategy, srcOutputs[0], dstOutputs[0]);
18415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // mute strategy while moving tracks from one output to another
18425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        for (size_t i = 0; i < srcOutputs.size(); i++) {
1843fa3697d716b444bbea6be480801536c44bf69214Eric Laurent            AudioOutputDescriptor *desc = mOutputs.valueFor(srcOutputs[i]);
1844fa3697d716b444bbea6be480801536c44bf69214Eric Laurent            if (desc->strategyRefCount(strategy) != 0) {
1845fa3697d716b444bbea6be480801536c44bf69214Eric Laurent                setStrategyMute(strategy, true, srcOutputs[i]);
1846fa3697d716b444bbea6be480801536c44bf69214Eric Laurent                setStrategyMute(strategy, false, srcOutputs[i], MUTE_TIME_MS, newDevice);
1847fa3697d716b444bbea6be480801536c44bf69214Eric Laurent            }
18485ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
1849f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1850f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // Move effects associated to this strategy from previous output to new output
18514660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen        if (strategy == STRATEGY_MEDIA) {
18524660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen            int outIdx = 0;
18534660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen            for (size_t i = 0; i < dstOutputs.size(); i++) {
18544660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                AudioOutputDescriptor *desc = mOutputs.valueFor(dstOutputs[i]);
18554660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                if (desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
18564660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                    outIdx = i;
18574660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                }
18584660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen            }
18594660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen            SortedVector<audio_io_handle_t> moved;
18604660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen            for (size_t i = 0; i < mEffects.size(); i++) {
18614660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                EffectDescriptor *desc = mEffects.valueAt(i);
18624660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                if (desc->mSession == AUDIO_SESSION_OUTPUT_MIX &&
18634660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                        desc->mIo != dstOutputs[outIdx]) {
18644660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                    if (moved.indexOf(desc->mIo) < 0) {
18654660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                        ALOGV("checkOutputForStrategy() moving effect %d to output %d",
18664660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                              mEffects.keyAt(i), dstOutputs[outIdx]);
18674660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                        mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, desc->mIo,
18684660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                                                       dstOutputs[outIdx]);
18694660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                        moved.add(desc->mIo);
18704660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                    }
18714660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                    desc->mIo = dstOutputs[outIdx];
18724660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                }
18734660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen            }
18744660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen        }
18755ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // Move tracks associated to this strategy from previous output to new output
1876f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        for (int i = 0; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) {
1877f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (getStrategy((AudioSystem::stream_type)i) == strategy) {
187812bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi                //FIXME see fixme on name change
18794660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                mpClientInterface->setStreamOutput((AudioSystem::stream_type)i,
18804660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                                                   dstOutputs[0] /* ignored */);
1881f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1882f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1883f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1884f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1885f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1886f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::checkOutputForAllStrategies()
1887f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1888c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
1889f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForStrategy(STRATEGY_PHONE);
1890f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForStrategy(STRATEGY_SONIFICATION);
189112bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
1892f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForStrategy(STRATEGY_MEDIA);
1893f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForStrategy(STRATEGY_DTMF);
1894f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1895f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1896b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurentaudio_io_handle_t AudioPolicyManagerBase::getA2dpOutput()
1897b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent{
18985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (!mHasA2dp) {
1899b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        return 0;
1900b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    }
1901b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1902b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    for (size_t i = 0; i < mOutputs.size(); i++) {
1903b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        AudioOutputDescriptor *outputDesc = mOutputs.valueAt(i);
1904b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        if (!outputDesc->isDuplicated() && outputDesc->device() & AUDIO_DEVICE_OUT_ALL_A2DP) {
1905b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            return mOutputs.keyAt(i);
1906b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        }
1907b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    }
1908b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1909b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    return 0;
1910b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent}
1911b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1912f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::checkA2dpSuspend()
1913f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
19145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (!mHasA2dp) {
1915b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        return;
1916b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    }
1917b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    audio_io_handle_t a2dpOutput = getA2dpOutput();
1918b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (a2dpOutput == 0) {
1919b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        return;
1920b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    }
1921b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1922f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // suspend A2DP output if:
1923f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      (NOT already suspended) &&
1924f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      ((SCO device is connected &&
1925f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //       (forced usage for communication || for record is SCO))) ||
1926f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      (phone state is ringing || in call)
1927f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //
1928f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // restore A2DP output if:
1929f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      (Already suspended) &&
1930f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      ((SCO device is NOT connected ||
1931f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //       (forced usage NOT for communication && NOT for record is SCO))) &&
1932f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      (phone state is NOT ringing && NOT in call)
1933f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //
1934f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mA2dpSuspended) {
1935f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (((mScoDeviceAddress == "") ||
1936f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin             ((mForceUse[AudioSystem::FOR_COMMUNICATION] != AudioSystem::FORCE_BT_SCO) &&
1937f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin              (mForceUse[AudioSystem::FOR_RECORD] != AudioSystem::FORCE_BT_SCO))) &&
1938f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin             ((mPhoneState != AudioSystem::MODE_IN_CALL) &&
1939f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin              (mPhoneState != AudioSystem::MODE_RINGTONE))) {
1940f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1941b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            mpClientInterface->restoreOutput(a2dpOutput);
1942f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mA2dpSuspended = false;
1943f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1944f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
1945f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (((mScoDeviceAddress != "") &&
1946f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin             ((mForceUse[AudioSystem::FOR_COMMUNICATION] == AudioSystem::FORCE_BT_SCO) ||
1947f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin              (mForceUse[AudioSystem::FOR_RECORD] == AudioSystem::FORCE_BT_SCO))) ||
1948f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin             ((mPhoneState == AudioSystem::MODE_IN_CALL) ||
1949f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin              (mPhoneState == AudioSystem::MODE_RINGTONE))) {
1950f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1951b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            mpClientInterface->suspendOutput(a2dpOutput);
1952f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mA2dpSuspended = true;
1953f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1954f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1955f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1956f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1957f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurentaudio_devices_t AudioPolicyManagerBase::getNewDevice(audio_io_handle_t output, bool fromCache)
1958f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1959ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    audio_devices_t device = AUDIO_DEVICE_NONE;
1960f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1961f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
1962f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // check the following by order of priority to request a routing change if necessary:
1963c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    // 1: the strategy enforced audible is active on the output:
1964c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    //      use device for strategy enforced audible
1965c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    // 2: we are in call or the strategy phone is active on the output:
1966f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      use device for strategy phone
1967c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    // 3: the strategy sonification is active on the output:
1968f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      use device for strategy sonification
196912bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    // 4: the strategy "respectful" sonification is active on the output:
197012bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    //      use device for strategy "respectful" sonification
197112bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    // 5: the strategy media is active on the output:
1972f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      use device for strategy media
197312bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    // 6: the strategy DTMF is active on the output:
1974f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      use device for strategy DTMF
1975c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    if (outputDesc->isUsedByStrategy(STRATEGY_ENFORCED_AUDIBLE)) {
1976c16ac09f510437e8340be691720177a490ae78f0Eric Laurent        device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
1977c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    } else if (isInCall() ||
1978c16ac09f510437e8340be691720177a490ae78f0Eric Laurent                    outputDesc->isUsedByStrategy(STRATEGY_PHONE)) {
1979f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
1980f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else if (outputDesc->isUsedByStrategy(STRATEGY_SONIFICATION)) {
1981f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
198212bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    } else if (outputDesc->isUsedByStrategy(STRATEGY_SONIFICATION_RESPECTFUL)) {
19835ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
1984f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else if (outputDesc->isUsedByStrategy(STRATEGY_MEDIA)) {
1985f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
1986f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else if (outputDesc->isUsedByStrategy(STRATEGY_DTMF)) {
1987f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
1988f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1989f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
19906a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("getNewDevice() selected device %x", device);
1991f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return device;
1992f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1993f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1994f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinuint32_t AudioPolicyManagerBase::getStrategyForStream(AudioSystem::stream_type stream) {
1995f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return (uint32_t)getStrategy(stream);
1996f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1997f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1998f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurentaudio_devices_t AudioPolicyManagerBase::getDevicesForStream(AudioSystem::stream_type stream) {
1999f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent    audio_devices_t devices;
2000f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // By checking the range of stream before calling getStrategy, we avoid
20015efbd421e0029d6fc44b1cc65c0e5e0d85e5161fSteve Block    // getStrategy's behavior for invalid streams.  getStrategy would do a ALOGE
2002f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // and then return STRATEGY_MEDIA, but we want to return the empty set.
2003f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (stream < (AudioSystem::stream_type) 0 || stream >= AudioSystem::NUM_STREAM_TYPES) {
2004ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        devices = AUDIO_DEVICE_NONE;
2005f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
2006f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioPolicyManagerBase::routing_strategy strategy = getStrategy(stream);
20075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        devices = getDeviceForStrategy(strategy, true /*fromCache*/);
2008f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2009f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return devices;
2010f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2011f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2012f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima ZavinAudioPolicyManagerBase::routing_strategy AudioPolicyManagerBase::getStrategy(
2013f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioSystem::stream_type stream) {
2014f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // stream to strategy mapping
2015f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    switch (stream) {
2016f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::VOICE_CALL:
2017f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::BLUETOOTH_SCO:
2018f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return STRATEGY_PHONE;
2019f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::RING:
2020f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::ALARM:
2021f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return STRATEGY_SONIFICATION;
202212bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    case AudioSystem::NOTIFICATION:
202312bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        return STRATEGY_SONIFICATION_RESPECTFUL;
2024f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::DTMF:
2025f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return STRATEGY_DTMF;
2026f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    default:
20275efbd421e0029d6fc44b1cc65c0e5e0d85e5161fSteve Block        ALOGE("unknown stream type");
2028f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::SYSTEM:
2029f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // NOTE: SYSTEM stream uses MEDIA strategy because muting music and switching outputs
2030f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // while key clicks are played produces a poor result
2031f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::TTS:
2032f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::MUSIC:
2033f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return STRATEGY_MEDIA;
2034c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    case AudioSystem::ENFORCED_AUDIBLE:
2035c16ac09f510437e8340be691720177a490ae78f0Eric Laurent        return STRATEGY_ENFORCED_AUDIBLE;
2036f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2037f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2038f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
203912bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivivoid AudioPolicyManagerBase::handleNotificationRoutingForStream(AudioSystem::stream_type stream) {
204012bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    switch(stream) {
204112bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    case AudioSystem::MUSIC:
204212bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
2043c952527e6f89d5427881462823514be9d79f13e6Eric Laurent        updateDevicesAndOutputs();
204412bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        break;
204512bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    default:
204612bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        break;
204712bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    }
204812bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi}
204912bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi
20505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentaudio_devices_t AudioPolicyManagerBase::getDeviceForStrategy(routing_strategy strategy,
20515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                             bool fromCache)
2052f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2053ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    uint32_t device = AUDIO_DEVICE_NONE;
2054f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2055f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (fromCache) {
20563cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
20575ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent              strategy, mDeviceForStrategy[strategy]);
2058f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return mDeviceForStrategy[strategy];
2059f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2060f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2061f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    switch (strategy) {
206212bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi
206312bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    case STRATEGY_SONIFICATION_RESPECTFUL:
206412bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        if (isInCall()) {
20655ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
206612bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        } else if (isStreamActive(AudioSystem::MUSIC, SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
206712bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi            // while media is playing (or has recently played), use the same device
20685ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/);
206912bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        } else {
207012bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi            // when media is not playing anymore, fall back on the sonification behavior
20715ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
207212bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        }
207312bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi
207412bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        break;
207512bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi
2076f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case STRATEGY_DTMF:
2077f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (!isInCall()) {
2078f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // when off call, DTMF strategy follows the same rules as MEDIA strategy
20795ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/);
2080f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            break;
2081f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2082f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // when in call, DTMF and PHONE strategies follow the same rules
2083f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // FALL THROUGH
2084f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2085f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case STRATEGY_PHONE:
2086f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // for phone strategy, we first consider the forced use and then the available devices by order
2087f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // of priority
2088f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        switch (mForceUse[AudioSystem::FOR_COMMUNICATION]) {
2089f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        case AudioSystem::FORCE_BT_SCO:
2090f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (!isInCall() || strategy != STRATEGY_DTMF) {
2091c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT;
2092f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (device) break;
2093f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2094c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET;
2095f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (device) break;
2096c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO;
2097f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (device) break;
2098f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // if SCO device is requested but no SCO device is available, fall back to default case
2099f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // FALL THROUGH
2100f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2101f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        default:    // FORCE_NONE
2102f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to A2DP
21031afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent            if (mHasA2dp && !isInCall() &&
21041afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent                    (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
2105ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent                    (getA2dpOutput() != 0) && !mA2dpSuspended) {
2106c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
2107f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (device) break;
2108c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
2109f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (device) break;
2110f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2111c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
21121afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent            if (device) break;
2113c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADSET;
21141afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent            if (device) break;
2115b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent            if (mPhoneState != AudioSystem::MODE_IN_CALL) {
2116b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
2117b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2118b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
2119b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2120b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
2121b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2122b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
2123b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2124b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
2125b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2126b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent            }
2127c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_EARPIECE;
21285ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            if (device) break;
21295ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = mDefaultOutputDevice;
2130ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            if (device == AUDIO_DEVICE_NONE) {
21315ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE");
2132f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2133f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            break;
2134f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2135f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        case AudioSystem::FORCE_SPEAKER:
2136f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to
2137f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // A2DP speaker when forcing to speaker output
21381afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent            if (mHasA2dp && !isInCall() &&
21391afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent                    (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
2140ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent                    (getA2dpOutput() != 0) && !mA2dpSuspended) {
2141c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
2142f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (device) break;
2143f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2144b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent            if (mPhoneState != AudioSystem::MODE_IN_CALL) {
2145b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
2146b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2147b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
2148b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2149b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
2150b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2151b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
2152b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2153b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
2154b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2155b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent            }
2156c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER;
21575ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            if (device) break;
21585ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = mDefaultOutputDevice;
2159ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            if (device == AUDIO_DEVICE_NONE) {
21605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE, FORCE_SPEAKER");
2161f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2162f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            break;
2163f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2164f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    break;
2165f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2166f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case STRATEGY_SONIFICATION:
2167f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2168f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // If incall, just select the STRATEGY_PHONE device: The rest of the behavior is handled by
2169f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // handleIncallSonification().
2170f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (isInCall()) {
21715ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = getDeviceForStrategy(STRATEGY_PHONE, false /*fromCache*/);
2172f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            break;
2173f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2174c16ac09f510437e8340be691720177a490ae78f0Eric Laurent        // FALL THROUGH
2175c16ac09f510437e8340be691720177a490ae78f0Eric Laurent
2176c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    case STRATEGY_ENFORCED_AUDIBLE:
2177c16ac09f510437e8340be691720177a490ae78f0Eric Laurent        // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION
2178ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        // except:
2179ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        //   - when in call where it doesn't default to STRATEGY_PHONE behavior
2180ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        //   - in countries where not enforced in which case it follows STRATEGY_MEDIA
2181c16ac09f510437e8340be691720177a490ae78f0Eric Laurent
2182738207def5f691d605ae33d041116829a74513a9Eric Laurent        if ((strategy == STRATEGY_SONIFICATION) ||
2183738207def5f691d605ae33d041116829a74513a9Eric Laurent                (mForceUse[AudioSystem::FOR_SYSTEM] == AudioSystem::FORCE_SYSTEM_ENFORCED)) {
2184c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER;
2185ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            if (device == AUDIO_DEVICE_NONE) {
2186ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent                ALOGE("getDeviceForStrategy() speaker device not found for STRATEGY_SONIFICATION");
2187ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent            }
2188f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2189f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // The second device used for sonification is the same as the device used by media strategy
2190f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // FALL THROUGH
2191f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2192f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case STRATEGY_MEDIA: {
2193ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        uint32_t device2 = AUDIO_DEVICE_NONE;
219448387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi        if (mHasRemoteSubmix
219548387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi                && mForceUse[AudioSystem::FOR_MEDIA] == AudioSystem::FORCE_REMOTE_SUBMIX) {
219648387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
219748387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi        }
2198ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if ((device2 == AUDIO_DEVICE_NONE) &&
219948387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi                mHasA2dp && (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
2200ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent                (getA2dpOutput() != 0) && !mA2dpSuspended) {
2201c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
2202ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            if (device2 == AUDIO_DEVICE_NONE) {
2203c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
2204f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2205ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            if (device2 == AUDIO_DEVICE_NONE) {
2206c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
2207f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2208f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2209ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (device2 == AUDIO_DEVICE_NONE) {
2210c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
22111afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent        }
2212ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (device2 == AUDIO_DEVICE_NONE) {
2213c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADSET;
22141afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent        }
2215ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (device2 == AUDIO_DEVICE_NONE) {
2216599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
2217599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent        }
2218ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (device2 == AUDIO_DEVICE_NONE) {
2219599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
2220599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent        }
2221ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (device2 == AUDIO_DEVICE_NONE) {
2222c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
2223f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2224ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (device2 == AUDIO_DEVICE_NONE) {
2225c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
2226f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2227ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (device2 == AUDIO_DEVICE_NONE) {
2228c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
2229f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2230ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (device2 == AUDIO_DEVICE_NONE) {
2231c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER;
2232f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2233f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2234c16ac09f510437e8340be691720177a490ae78f0Eric Laurent        // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or
2235ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise
2236f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device |= device2;
22375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (device) break;
22385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        device = mDefaultOutputDevice;
2239ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (device == AUDIO_DEVICE_NONE) {
22405ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGE("getDeviceForStrategy() no device found for STRATEGY_MEDIA");
2241f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2242f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        } break;
2243f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2244f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    default:
224564cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("getDeviceForStrategy() unknown strategy: %d", strategy);
2246f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
2247f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2248f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
22493cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGVV("getDeviceForStrategy() strategy %d, device %x", strategy, device);
2250c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    return device;
2251f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2252f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2253c952527e6f89d5427881462823514be9d79f13e6Eric Laurentvoid AudioPolicyManagerBase::updateDevicesAndOutputs()
2254f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2255f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < NUM_STRATEGIES; i++) {
22565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
22575ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
2258c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    mPreviousOutputs = mOutputs;
22595ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
22605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
2261b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurentuint32_t AudioPolicyManagerBase::checkDeviceMuteStrategies(AudioOutputDescriptor *outputDesc,
22629029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                                                       audio_devices_t prevDevice,
22635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                       uint32_t delayMs)
22645ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
22659029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    // mute/unmute strategies using an incompatible device combination
22669029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    // if muting, wait for the audio in pcm buffer to be drained before proceeding
22679029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    // if unmuting, unmute only after the specified delay
22685ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (outputDesc->isDuplicated()) {
2269b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        return 0;
22705ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
22715ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
22725ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    uint32_t muteWaitMs = 0;
22735ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    audio_devices_t device = outputDesc->device();
22745ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    bool shouldMute = (outputDesc->refCount() != 0) &&
22755ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                    (AudioSystem::popCount(device) >= 2);
22769029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    // temporary mute output if device selection changes to avoid volume bursts due to
22779029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    // different per device volumes
22789029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    bool tempMute = (outputDesc->refCount() != 0) && (device != prevDevice);
22795ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
22805ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < NUM_STRATEGIES; i++) {
22815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
22825ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        bool mute = shouldMute && (curDevice & device) && (curDevice != device);
22835ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        bool doMute = false;
22845ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
22855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (mute && !outputDesc->mStrategyMutedByDevice[i]) {
22865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            doMute = true;
22875ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            outputDesc->mStrategyMutedByDevice[i] = true;
22885ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){
22895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            doMute = true;
22905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            outputDesc->mStrategyMutedByDevice[i] = false;
22915ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
22929029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent        if (doMute || tempMute) {
22935ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            for (size_t j = 0; j < mOutputs.size(); j++) {
22945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                AudioOutputDescriptor *desc = mOutputs.valueAt(j);
2295c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                if ((desc->supportedDevices() & outputDesc->supportedDevices())
2296ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent                        == AUDIO_DEVICE_NONE) {
22975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                    continue;
22985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                }
22995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                audio_io_handle_t curOutput = mOutputs.keyAt(j);
23003cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x) on output %d",
23015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                      mute ? "muting" : "unmuting", i, curDevice, curOutput);
23025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                setStrategyMute((routing_strategy)i, mute, curOutput, mute ? 0 : delayMs);
23039029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                if (desc->strategyRefCount((routing_strategy)i) != 0) {
23049029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                    if (tempMute) {
230501e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                        setStrategyMute((routing_strategy)i, true, curOutput);
230601e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                        setStrategyMute((routing_strategy)i, false, curOutput,
230701e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                            desc->latency() * 2, device);
23089029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                    }
23099029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                    if (tempMute || mute) {
23109029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                        if (muteWaitMs < desc->latency()) {
23119029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                            muteWaitMs = desc->latency();
23129029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                        }
23135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                    }
23145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                }
23155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
23165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
23175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
23185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
23195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // FIXME: should not need to double latency if volume could be applied immediately by the
23205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // audioflinger mixer. We must account for the delay between now and the next time
23215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // the audioflinger thread for this output will process a buffer (which corresponds to
23225ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // one buffer size, usually 1/2 or 1/4 of the latency).
23235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    muteWaitMs *= 2;
23245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // wait for the PCM output buffers to empty before proceeding with the rest of the command
23255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (muteWaitMs > delayMs) {
2326b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        muteWaitMs -= delayMs;
2327b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        usleep(muteWaitMs * 1000);
2328b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        return muteWaitMs;
2329f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2330b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent    return 0;
2331f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2332f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2333b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurentuint32_t AudioPolicyManagerBase::setOutputDevice(audio_io_handle_t output,
2334f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                                             audio_devices_t device,
2335f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                                             bool force,
2336f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                                             int delayMs)
2337f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
23385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGV("setOutputDevice() output %d device %04x delayMs %d", output, device, delayMs);
2339f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
23405ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    AudioParameter param;
2341b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent    uint32_t muteWaitMs = 0;
2342f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2343f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (outputDesc->isDuplicated()) {
2344b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        muteWaitMs = setOutputDevice(outputDesc->mOutput1->mId, device, force, delayMs);
2345b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        muteWaitMs += setOutputDevice(outputDesc->mOutput2->mId, device, force, delayMs);
2346b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        return muteWaitMs;
2347f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2348f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // filter devices according to output selected
2349f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent    device = (audio_devices_t)(device & outputDesc->mProfile->mSupportedDevices);
2350f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
23515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    audio_devices_t prevDevice = outputDesc->mDevice;
23525ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
23535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGV("setOutputDevice() prevDevice %04x", prevDevice);
23545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
2355ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if (device != AUDIO_DEVICE_NONE) {
23565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        outputDesc->mDevice = device;
23575ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
23589029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs);
23595ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
2360f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // Do not change the routing if:
2361ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    //  - the requested device is AUDIO_DEVICE_NONE
2362f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //  - the requested device is the same as current device and force is not specified.
2363f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // Doing this check here allows the caller to call setOutputDevice() without conditions
2364ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if ((device == AUDIO_DEVICE_NONE || device == prevDevice) && !force) {
23655ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        ALOGV("setOutputDevice() setting same device %04x or null device for output %d", device, output);
2366b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        return muteWaitMs;
2367f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2368f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
23695ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGV("setOutputDevice() changing device");
2370f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // do the routing
2371f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    param.addInt(String8(AudioParameter::keyRouting), (int)device);
23725ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    mpClientInterface->setParameters(output, param.toString(), delayMs);
23735ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
2374f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // update stream volumes according to new device
2375f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    applyStreamVolumes(output, device, delayMs);
2376b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent
2377b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent    return muteWaitMs;
23785ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
2379f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
23805ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric LaurentAudioPolicyManagerBase::IOProfile *AudioPolicyManagerBase::getInputProfile(audio_devices_t device,
23815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                   uint32_t samplingRate,
23825ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                   uint32_t format,
23835ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                   uint32_t channelMask)
23845ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
23855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // Choose an input profile based on the requested capture parameters: select the first available
23865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // profile supporting all requested parameters.
23875ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
238870c236c9290732782d5267935af1475b8d5ae602Eric Laurent    for (size_t i = 0; i < mHwModules.size(); i++)
23895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    {
239070c236c9290732782d5267935af1475b8d5ae602Eric Laurent        if (mHwModules[i]->mHandle == 0) {
23915ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            continue;
23925ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
239370c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
23945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        {
239570c236c9290732782d5267935af1475b8d5ae602Eric Laurent            IOProfile *profile = mHwModules[i]->mInputProfiles[j];
239670c236c9290732782d5267935af1475b8d5ae602Eric Laurent            if (profile->isCompatibleProfile(device, samplingRate, format,
23970977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent                                             channelMask,(audio_output_flags_t)0)) {
239870c236c9290732782d5267935af1475b8d5ae602Eric Laurent                return profile;
23995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
24005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
2401f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
240270c236c9290732782d5267935af1475b8d5ae602Eric Laurent    return NULL;
2403f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2404f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2405f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurentaudio_devices_t AudioPolicyManagerBase::getDeviceForInputSource(int inputSource)
2406f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2407ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    uint32_t device = AUDIO_DEVICE_NONE;
2408f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2409f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    switch(inputSource) {
2410f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_DEFAULT:
2411f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_MIC:
2412f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_RECOGNITION:
2413f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_COMMUNICATION:
2414f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (mForceUse[AudioSystem::FOR_RECORD] == AudioSystem::FORCE_BT_SCO &&
2415c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            mAvailableInputDevices & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
2416c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
2417c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_WIRED_HEADSET) {
2418c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = AUDIO_DEVICE_IN_WIRED_HEADSET;
2419c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_BUILTIN_MIC) {
2420c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = AUDIO_DEVICE_IN_BUILTIN_MIC;
2421f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2422f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
2423f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_CAMCORDER:
2424c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        if (mAvailableInputDevices & AUDIO_DEVICE_IN_BACK_MIC) {
2425c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = AUDIO_DEVICE_IN_BACK_MIC;
2426c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_BUILTIN_MIC) {
2427c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = AUDIO_DEVICE_IN_BUILTIN_MIC;
2428f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2429f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
2430f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_UPLINK:
2431f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_DOWNLINK:
2432f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_CALL:
2433c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        if (mAvailableInputDevices & AUDIO_DEVICE_IN_VOICE_CALL) {
2434c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = AUDIO_DEVICE_IN_VOICE_CALL;
24355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
2436f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
243748387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi    case AUDIO_SOURCE_REMOTE_SUBMIX:
243848387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi        if (mAvailableInputDevices & AUDIO_DEVICE_IN_REMOTE_SUBMIX) {
243948387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
244048387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi        }
244148387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi        break;
2442f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    default:
244364cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("getDeviceForInputSource() invalid input source %d", inputSource);
2444f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
2445f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
24466a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("getDeviceForInputSource()input source %d, device %08x", inputSource, device);
2447c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    return device;
2448f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2449f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
24506d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivibool AudioPolicyManagerBase::isVirtualInputDevice(audio_devices_t device)
24516d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi{
24526d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi    if ((device & AUDIO_DEVICE_BIT_IN) != 0) {
24536d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi        device &= ~AUDIO_DEVICE_BIT_IN;
24546d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi        if ((popcount(device) == 1) && ((device & ~APM_AUDIO_IN_DEVICE_VIRTUAL_ALL) == 0))
24556d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi            return true;
24566d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi    }
24576d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi    return false;
24586d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi}
24596d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi
24606d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Triviaudio_io_handle_t AudioPolicyManagerBase::getActiveInput(bool ignoreVirtualInputs)
2461f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2462f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mInputs.size(); i++) {
24636d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi        const AudioInputDescriptor * input_descriptor = mInputs.valueAt(i);
24646d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi        if ((input_descriptor->mRefCount > 0)
24656d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi                && (!ignoreVirtualInputs || !isVirtualInputDevice(input_descriptor->mDevice))) {
2466f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return mInputs.keyAt(i);
2467f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2468f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2469f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return 0;
2470f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2471f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2472e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2473c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentaudio_devices_t AudioPolicyManagerBase::getDeviceForVolume(audio_devices_t device)
2474e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent{
2475ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if (device == AUDIO_DEVICE_NONE) {
2476e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        // this happens when forcing a route update and no track is active on an output.
2477e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        // In this case the returned category is not important.
2478c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        device =  AUDIO_DEVICE_OUT_SPEAKER;
2479c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    } else if (AudioSystem::popCount(device) > 1) {
2480e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        // Multiple device selection is either:
2481e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        //  - speaker + one other device: give priority to speaker in this case.
2482e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        //  - one A2DP device + another device: happens with duplicated output. In this case
2483e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        // retain the device on the A2DP output as the other must not correspond to an active
2484e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        // selection if not the speaker.
2485c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        if (device & AUDIO_DEVICE_OUT_SPEAKER) {
2486c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            device = AUDIO_DEVICE_OUT_SPEAKER;
2487c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        } else {
2488c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            device = (audio_devices_t)(device & AUDIO_DEVICE_OUT_ALL_A2DP);
2489c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        }
2490e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    }
2491e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
249264cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block    ALOGW_IF(AudioSystem::popCount(device) != 1,
2493c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            "getDeviceForVolume() invalid device combination: %08x",
2494e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            device);
2495e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2496c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    return device;
2497c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent}
2498c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
2499f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric LaurentAudioPolicyManagerBase::device_category AudioPolicyManagerBase::getDeviceCategory(audio_devices_t device)
2500c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent{
2501f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent    switch(getDeviceForVolume(device)) {
2502e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_EARPIECE:
2503e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            return DEVICE_CATEGORY_EARPIECE;
2504e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_WIRED_HEADSET:
2505e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_WIRED_HEADPHONE:
2506e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_BLUETOOTH_SCO:
2507e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET:
2508e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP:
2509e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES:
2510e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            return DEVICE_CATEGORY_HEADSET;
2511e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_SPEAKER:
2512e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT:
2513e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER:
2514c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        case AUDIO_DEVICE_OUT_AUX_DIGITAL:
2515599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent        case AUDIO_DEVICE_OUT_USB_ACCESSORY:
2516599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent        case AUDIO_DEVICE_OUT_USB_DEVICE:
251748387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi        case AUDIO_DEVICE_OUT_REMOTE_SUBMIX:
2518e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        default:
2519e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            return DEVICE_CATEGORY_SPEAKER;
2520e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    }
2521e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent}
2522e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2523f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurentfloat AudioPolicyManagerBase::volIndexToAmpl(audio_devices_t device, const StreamDescriptor& streamDesc,
2524e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        int indexInUi)
2525e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent{
2526e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    device_category deviceCategory = getDeviceCategory(device);
2527e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    const VolumeCurvePoint *curve = streamDesc.mVolumeCurve[deviceCategory];
2528e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2529f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // the volume index in the UI is relative to the min and max volume indices for this stream type
2530e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    int nbSteps = 1 + curve[VOLMAX].mIndex -
2531e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            curve[VOLMIN].mIndex;
2532f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    int volIdx = (nbSteps * (indexInUi - streamDesc.mIndexMin)) /
2533f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            (streamDesc.mIndexMax - streamDesc.mIndexMin);
2534f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2535f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // find what part of the curve this index volume belongs to, or if it's out of bounds
2536f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    int segment = 0;
2537e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    if (volIdx < curve[VOLMIN].mIndex) {         // out of bounds
2538f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return 0.0f;
2539e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    } else if (volIdx < curve[VOLKNEE1].mIndex) {
2540f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        segment = 0;
2541e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    } else if (volIdx < curve[VOLKNEE2].mIndex) {
2542f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        segment = 1;
2543e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    } else if (volIdx <= curve[VOLMAX].mIndex) {
2544f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        segment = 2;
2545f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {                                                               // out of bounds
2546f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return 1.0f;
2547f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2548f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2549f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // linear interpolation in the attenuation table in dB
2550e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    float decibels = curve[segment].mDBAttenuation +
2551e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            ((float)(volIdx - curve[segment].mIndex)) *
2552e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent                ( (curve[segment+1].mDBAttenuation -
2553e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent                        curve[segment].mDBAttenuation) /
2554e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent                    ((float)(curve[segment+1].mIndex -
2555e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent                            curve[segment].mIndex)) );
2556f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2557f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    float amplification = exp( decibels * 0.115129f); // exp( dB * ln(10) / 20 )
2558f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
25593cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGVV("VOLUME vol index=[%d %d %d], dB=[%.1f %.1f %.1f] ampl=%.5f",
2560e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            curve[segment].mIndex, volIdx,
2561e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            curve[segment+1].mIndex,
2562e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            curve[segment].mDBAttenuation,
2563cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent            decibels,
2564e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            curve[segment+1].mDBAttenuation,
2565f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            amplification);
2566f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2567f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return amplification;
2568f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2569f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2570cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
2571e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    AudioPolicyManagerBase::sDefaultVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
2572e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    {1, -49.5f}, {33, -33.5f}, {66, -17.0f}, {100, 0.0f}
2573e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent};
2574e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2575e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
2576e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    AudioPolicyManagerBase::sDefaultMediaVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
2577e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    {1, -58.0f}, {20, -40.0f}, {60, -17.0f}, {100, 0.0f}
2578cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent};
2579cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent
2580e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
2581e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    AudioPolicyManagerBase::sSpeakerMediaVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
2582e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    {1, -56.0f}, {20, -34.0f}, {60, -11.0f}, {100, 0.0f}
2583e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent};
2584e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2585e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
2586e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    AudioPolicyManagerBase::sSpeakerSonificationVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
2587e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    {1, -29.7f}, {33, -20.1f}, {66, -10.2f}, {100, 0.0f}
2588e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent};
2589e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2590ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent// AUDIO_STREAM_SYSTEM, AUDIO_STREAM_ENFORCED_AUDIBLE and AUDIO_STREAM_DTMF volume tracks
2591ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent// AUDIO_STREAM_RING on phones and AUDIO_STREAM_MUSIC on tablets (See AudioService.java).
2592123897874418f9f0e48bb89386d8c470e6975f28Jean-Michel Trivi// The range is constrained between -24dB and -6dB over speaker and -30dB and -18dB over headset.
2593ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
2594ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    AudioPolicyManagerBase::sDefaultSystemVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
2595123897874418f9f0e48bb89386d8c470e6975f28Jean-Michel Trivi    {1, -24.0f}, {33, -18.0f}, {66, -12.0f}, {100, -6.0f}
2596ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent};
2597ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent
2598ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
2599ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    AudioPolicyManagerBase::sHeadsetSystemVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
26007465678e0d5711ebcd78ae47b3a76821534a23eaEric Laurent    {1, -30.0f}, {33, -26.0f}, {66, -22.0f}, {100, -18.0f}
2601ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent};
2602e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2603e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
2604ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent            *AudioPolicyManagerBase::sVolumeProfiles[AUDIO_STREAM_CNT]
2605e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent                                                   [AudioPolicyManagerBase::DEVICE_CATEGORY_CNT] = {
2606ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_VOICE_CALL
2607e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
2608e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sDefaultVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2609e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sDefaultVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2610e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    },
2611ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_SYSTEM
2612ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET
2613ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2614ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultSystemVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2615ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    },
2616ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_RING
2617e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
2618e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2619e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sDefaultVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2620e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    },
2621ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_MUSIC
2622ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultMediaVolumeCurve, // DEVICE_CATEGORY_HEADSET
2623ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sSpeakerMediaVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2624ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultMediaVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2625ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    },
2626ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_ALARM
262712bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
262812bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER
262912bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        sDefaultVolumeCurve  // DEVICE_CATEGORY_EARPIECE
263012bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    },
2631ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_NOTIFICATION
2632e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
2633ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2634e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sDefaultVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2635c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    },
2636ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_BLUETOOTH_SCO
2637c16ac09f510437e8340be691720177a490ae78f0Eric Laurent        sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
2638ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2639c16ac09f510437e8340be691720177a490ae78f0Eric Laurent        sDefaultVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2640c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    },
2641ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_ENFORCED_AUDIBLE
2642ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET
2643ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2644ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultSystemVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2645ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    },
2646ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    {  // AUDIO_STREAM_DTMF
2647ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET
2648ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2649ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultSystemVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2650ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    },
2651ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_TTS
2652ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultMediaVolumeCurve, // DEVICE_CATEGORY_HEADSET
2653ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sSpeakerMediaVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2654ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultMediaVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2655ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    },
2656e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent};
2657e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2658e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurentvoid AudioPolicyManagerBase::initializeVolumeCurves()
2659e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent{
2660ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    for (int i = 0; i < AUDIO_STREAM_CNT; i++) {
2661e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) {
2662cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent            mStreams[i].mVolumeCurve[j] =
2663ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent                    sVolumeProfiles[i][j];
2664cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent        }
2665cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent    }
2666f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2667f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2668c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentfloat AudioPolicyManagerBase::computeVolume(int stream,
2669c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                            int index,
2670c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                            audio_io_handle_t output,
2671f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                                            audio_devices_t device)
2672f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2673f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    float volume = 1.0;
2674f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
2675f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    StreamDescriptor &streamDesc = mStreams[stream];
2676f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2677ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if (device == AUDIO_DEVICE_NONE) {
2678f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device = outputDesc->device();
2679f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2680f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2681f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if volume is not 0 (not muted), force media volume to max on digital output
2682f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (stream == AudioSystem::MUSIC &&
2683f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        index != mStreams[stream].mIndexMin &&
2684f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent        (device == AUDIO_DEVICE_OUT_AUX_DIGITAL ||
2685599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent         device == AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET ||
2686599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent         device == AUDIO_DEVICE_OUT_USB_ACCESSORY ||
2687599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent         device == AUDIO_DEVICE_OUT_USB_DEVICE)) {
2688f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return 1.0;
2689f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2690f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2691f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    volume = volIndexToAmpl(device, streamDesc, index);
2692f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2693f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if a headset is connected, apply the following rules to ring tones and notifications
2694f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // to avoid sound level bursts in user's ears:
2695f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // - always attenuate ring tones and notifications volume by 6dB
2696f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // - if music is playing, always limit the volume to current music volume,
2697f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // with a minimum threshold at -36dB so that notification is always perceived.
269812bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    const routing_strategy stream_strategy = getStrategy((AudioSystem::stream_type)stream);
2699c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
2700c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
2701c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            AUDIO_DEVICE_OUT_WIRED_HEADSET |
2702c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) &&
270312bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        ((stream_strategy == STRATEGY_SONIFICATION)
270412bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi                || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL)
2705738207def5f691d605ae33d041116829a74513a9Eric Laurent                || (stream == AudioSystem::SYSTEM)
2706738207def5f691d605ae33d041116829a74513a9Eric Laurent                || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) &&
2707738207def5f691d605ae33d041116829a74513a9Eric Laurent                    (mForceUse[AudioSystem::FOR_SYSTEM] == AudioSystem::FORCE_NONE))) &&
2708f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        streamDesc.mCanBeMuted) {
2709f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        volume *= SONIFICATION_HEADSET_VOLUME_FACTOR;
2710f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // when the phone is ringing we must consider that music could have been paused just before
2711f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // by the music application and behave as if music was active if the last music track was
2712f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // just stopped
2713ac3cf10ef6de12e69540a1244ac7255f93fa7502Eric Laurent        if (isStreamActive(AudioSystem::MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) ||
2714ac3cf10ef6de12e69540a1244ac7255f93fa7502Eric Laurent                mLimitRingtoneVolume) {
271517a73c3394547692457299dc512b5c2312ea0344Eric Laurent            audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/);
2716c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            float musicVol = computeVolume(AudioSystem::MUSIC,
271717a73c3394547692457299dc512b5c2312ea0344Eric Laurent                               mStreams[AudioSystem::MUSIC].getVolumeIndex(musicDevice),
2718c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                               output,
271917a73c3394547692457299dc512b5c2312ea0344Eric Laurent                               musicDevice);
2720c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            float minVol = (musicVol > SONIFICATION_HEADSET_VOLUME_MIN) ?
2721c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                musicVol : SONIFICATION_HEADSET_VOLUME_MIN;
2722f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (volume > minVol) {
2723f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                volume = minVol;
27246a70518b93928d1c91457ff805e375c82d76b0e5Steve Block                ALOGV("computeVolume limiting volume to %f musicVol %f", minVol, musicVol);
2725f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2726f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2727f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2728f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2729f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return volume;
2730f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2731f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2732c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentstatus_t AudioPolicyManagerBase::checkAndSetVolume(int stream,
2733c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                   int index,
2734c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                   audio_io_handle_t output,
2735f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                                                   audio_devices_t device,
2736c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                   int delayMs,
2737c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                   bool force)
2738f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2739f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2740f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // do not change actual stream volume if the stream is muted
2741f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mOutputs.valueFor(output)->mMuteCount[stream] != 0) {
27423cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        ALOGVV("checkAndSetVolume() stream %d muted count %d",
27435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent              stream, mOutputs.valueFor(output)->mMuteCount[stream]);
2744f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return NO_ERROR;
2745f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2746f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2747f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // do not change in call volume if bluetooth is connected and vice versa
2748f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if ((stream == AudioSystem::VOICE_CALL && mForceUse[AudioSystem::FOR_COMMUNICATION] == AudioSystem::FORCE_BT_SCO) ||
2749f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        (stream == AudioSystem::BLUETOOTH_SCO && mForceUse[AudioSystem::FOR_COMMUNICATION] != AudioSystem::FORCE_BT_SCO)) {
27506a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
2751f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin             stream, mForceUse[AudioSystem::FOR_COMMUNICATION]);
2752f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return INVALID_OPERATION;
2753f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2754f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2755f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    float volume = computeVolume(stream, index, output, device);
2756f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // We actually change the volume if:
2757f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // - the float value returned by computeVolume() changed
2758f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // - the force flag is set
2759f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (volume != mOutputs.valueFor(output)->mCurVolume[stream] ||
2760f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            force) {
2761f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mOutputs.valueFor(output)->mCurVolume[stream] = volume;
27623cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        ALOGVV("checkAndSetVolume() for output %d stream %d, volume %f, delay %d", output, stream, volume, delayMs);
2763f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (stream == AudioSystem::VOICE_CALL ||
2764f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            stream == AudioSystem::DTMF ||
2765f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            stream == AudioSystem::BLUETOOTH_SCO) {
2766f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // offset value to reflect actual hardware volume that never reaches 0
2767f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // 1% corresponds roughly to first step in VOICE_CALL stream volume setting (see AudioService.java)
2768f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            volume = 0.01 + 0.99 * volume;
2769f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // Force VOICE_CALL to track BLUETOOTH_SCO stream volume when bluetooth audio is
2770f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // enabled
2771f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (stream == AudioSystem::BLUETOOTH_SCO) {
2772f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mpClientInterface->setStreamVolume(AudioSystem::VOICE_CALL, volume, output, delayMs);
2773f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2774f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2775f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2776f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mpClientInterface->setStreamVolume((AudioSystem::stream_type)stream, volume, output, delayMs);
2777f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2778f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2779f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (stream == AudioSystem::VOICE_CALL ||
2780f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        stream == AudioSystem::BLUETOOTH_SCO) {
2781f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        float voiceVolume;
2782f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // Force voice volume to max for bluetooth SCO as volume is managed by the headset
2783f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (stream == AudioSystem::VOICE_CALL) {
2784f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            voiceVolume = (float)index/(float)mStreams[stream].mIndexMax;
2785f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        } else {
2786f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            voiceVolume = 1.0;
2787f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2788f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2789b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        if (voiceVolume != mLastVoiceVolume && output == mPrimaryOutput) {
2790f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
2791f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mLastVoiceVolume = voiceVolume;
2792f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2793f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2794f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2795f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
2796f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2797f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2798c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentvoid AudioPolicyManagerBase::applyStreamVolumes(audio_io_handle_t output,
2799f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                                                audio_devices_t device,
2800c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                int delayMs,
2801c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                bool force)
2802f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
28033cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGVV("applyStreamVolumes() for output %d and device %x", output, device);
2804f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2805f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
2806c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        checkAndSetVolume(stream,
2807f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                          mStreams[stream].getVolumeIndex(device),
2808c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                          output,
2809c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                          device,
2810c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                          delayMs,
2811c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                          force);
2812f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2813f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2814f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
281501e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurentvoid AudioPolicyManagerBase::setStrategyMute(routing_strategy strategy,
281601e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                             bool on,
281701e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                             audio_io_handle_t output,
281801e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                             int delayMs,
281901e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                             audio_devices_t device)
2820f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
28213cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGVV("setStrategyMute() strategy %d, mute %d, output %d", strategy, on, output);
2822f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
2823f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (getStrategy((AudioSystem::stream_type)stream) == strategy) {
282401e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent            setStreamMute(stream, on, output, delayMs, device);
2825f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2826f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2827f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2828f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
282901e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurentvoid AudioPolicyManagerBase::setStreamMute(int stream,
283001e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                           bool on,
283101e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                           audio_io_handle_t output,
283201e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                           int delayMs,
283301e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                           audio_devices_t device)
2834f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2835f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    StreamDescriptor &streamDesc = mStreams[stream];
2836f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
2837ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if (device == AUDIO_DEVICE_NONE) {
283801e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent        device = outputDesc->device();
283901e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent    }
2840f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
28413cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGVV("setStreamMute() stream %d, mute %d, output %d, mMuteCount %d device %04x",
284201e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent          stream, on, output, outputDesc->mMuteCount[stream], device);
2843f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2844f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (on) {
2845f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (outputDesc->mMuteCount[stream] == 0) {
2846738207def5f691d605ae33d041116829a74513a9Eric Laurent            if (streamDesc.mCanBeMuted &&
2847738207def5f691d605ae33d041116829a74513a9Eric Laurent                    ((stream != AudioSystem::ENFORCED_AUDIBLE) ||
2848738207def5f691d605ae33d041116829a74513a9Eric Laurent                     (mForceUse[AudioSystem::FOR_SYSTEM] == AudioSystem::FORCE_NONE))) {
2849c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                checkAndSetVolume(stream, 0, output, device, delayMs);
2850f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2851f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2852f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
2853f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mMuteCount[stream]++;
2854f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
2855f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (outputDesc->mMuteCount[stream] == 0) {
28563cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            ALOGV("setStreamMute() unmuting non muted stream!");
2857f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return;
2858f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2859f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (--outputDesc->mMuteCount[stream] == 0) {
2860c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            checkAndSetVolume(stream,
2861c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                              streamDesc.getVolumeIndex(device),
2862c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                              output,
2863c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                              device,
2864c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                              delayMs);
2865f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2866f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2867f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2868f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2869f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::handleIncallSonification(int stream, bool starting, bool stateChange)
2870f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2871f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if the stream pertains to sonification strategy and we are in call we must
2872f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // mute the stream if it is low visibility. If it is high visibility, we must play a tone
2873f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // in the device used for phone strategy and play the tone if the selected device does not
2874f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // interfere with the device used for phone strategy
2875f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
2876f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // many times as there are active tracks on the output
287712bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    const routing_strategy stream_strategy = getStrategy((AudioSystem::stream_type)stream);
287812bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    if ((stream_strategy == STRATEGY_SONIFICATION) ||
287912bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi            ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
2880b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        AudioOutputDescriptor *outputDesc = mOutputs.valueFor(mPrimaryOutput);
28816a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
2882f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                stream, starting, outputDesc->mDevice, stateChange);
2883f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (outputDesc->mRefCount[stream]) {
2884f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            int muteCount = 1;
2885f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (stateChange) {
2886f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                muteCount = outputDesc->mRefCount[stream];
2887f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2888f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (AudioSystem::isLowVisibility((AudioSystem::stream_type)stream)) {
28896a70518b93928d1c91457ff805e375c82d76b0e5Steve Block                ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
2890f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                for (int i = 0; i < muteCount; i++) {
2891b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                    setStreamMute(stream, starting, mPrimaryOutput);
2892f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
2893f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            } else {
28946a70518b93928d1c91457ff805e375c82d76b0e5Steve Block                ALOGV("handleIncallSonification() high visibility");
28955ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                if (outputDesc->device() &
28965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                        getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
28976a70518b93928d1c91457ff805e375c82d76b0e5Steve Block                    ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
2898f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    for (int i = 0; i < muteCount; i++) {
2899b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                        setStreamMute(stream, starting, mPrimaryOutput);
2900f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    }
2901f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
2902f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (starting) {
2903f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    mpClientInterface->startTone(ToneGenerator::TONE_SUP_CALL_WAITING, AudioSystem::VOICE_CALL);
2904f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else {
2905f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    mpClientInterface->stopTone();
2906f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
2907f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2908f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2909f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2910f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2911f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2912f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinbool AudioPolicyManagerBase::isInCall()
2913f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2914f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return isStateInCall(mPhoneState);
2915f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2916f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2917f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinbool AudioPolicyManagerBase::isStateInCall(int state) {
2918f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return ((state == AudioSystem::MODE_IN_CALL) ||
2919f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            (state == AudioSystem::MODE_IN_COMMUNICATION));
2920f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2921f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
292270c236c9290732782d5267935af1475b8d5ae602Eric Laurentbool AudioPolicyManagerBase::needsDirectOuput(audio_stream_type_t stream,
292370c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                              uint32_t samplingRate,
292470c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                              audio_format_t format,
292570c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                              audio_channel_mask_t channelMask,
29260977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent                                              audio_output_flags_t flags,
292770c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                              audio_devices_t device)
2928f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2929f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin   return ((flags & AudioSystem::OUTPUT_FLAG_DIRECT) ||
2930b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent          (format != 0 && !AudioSystem::isLinearPCM(format)));
2931f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2932f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2933f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinuint32_t AudioPolicyManagerBase::getMaxEffectsCpuLoad()
2934f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2935f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return MAX_EFFECTS_CPU_LOAD;
2936f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2937f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2938f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinuint32_t AudioPolicyManagerBase::getMaxEffectsMemory()
2939f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2940f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return MAX_EFFECTS_MEMORY;
2941f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2942f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2943f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// --- AudioOutputDescriptor class implementation
2944f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2945b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric LaurentAudioPolicyManagerBase::AudioOutputDescriptor::AudioOutputDescriptor(
29465ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        const IOProfile *profile)
294770c236c9290732782d5267935af1475b8d5ae602Eric Laurent    : mId(0), mSamplingRate(0), mFormat((audio_format_t)0),
294870c236c9290732782d5267935af1475b8d5ae602Eric Laurent      mChannelMask((audio_channel_mask_t)0), mLatency(0),
2949ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    mFlags((audio_output_flags_t)0), mDevice(AUDIO_DEVICE_NONE),
2950f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent    mOutput1(0), mOutput2(0), mProfile(profile)
2951f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2952f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // clear usage count for all stream types
2953f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) {
2954f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mRefCount[i] = 0;
2955f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mCurVolume[i] = -1.0;
2956f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mMuteCount[i] = 0;
2957f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mStopTime[i] = 0;
2958f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
29593cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    if (profile != NULL) {
29603cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        mSamplingRate = profile->mSamplingRates[0];
29613cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        mFormat = profile->mFormats[0];
29623cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        mChannelMask = profile->mChannelMasks[0];
29633cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        mFlags = profile->mFlags;
29643cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    }
2965f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2966f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2967f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurentaudio_devices_t AudioPolicyManagerBase::AudioOutputDescriptor::device()
2968f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2969f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (isDuplicated()) {
2970f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent        return (audio_devices_t)(mOutput1->mDevice | mOutput2->mDevice);
2971f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
2972f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent        return mDevice;
2973f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2974f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2975f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
29765ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentuint32_t AudioPolicyManagerBase::AudioOutputDescriptor::latency()
29775ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
29785ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (isDuplicated()) {
29795ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return (mOutput1->mLatency > mOutput2->mLatency) ? mOutput1->mLatency : mOutput2->mLatency;
29805ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    } else {
29815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return mLatency;
29825ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
29835ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
29845ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
29855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentbool AudioPolicyManagerBase::AudioOutputDescriptor::sharesHwModuleWith(
29865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        const AudioOutputDescriptor *outputDesc)
29875ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
29885ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (isDuplicated()) {
29895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return mOutput1->sharesHwModuleWith(outputDesc) || mOutput2->sharesHwModuleWith(outputDesc);
29905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    } else if (outputDesc->isDuplicated()){
29915ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return sharesHwModuleWith(outputDesc->mOutput1) || sharesHwModuleWith(outputDesc->mOutput2);
29925ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    } else {
299370c236c9290732782d5267935af1475b8d5ae602Eric Laurent        return (mProfile->mModule == outputDesc->mProfile->mModule);
29945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
29955ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
29965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
2997f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::AudioOutputDescriptor::changeRefCount(AudioSystem::stream_type stream, int delta)
2998f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2999f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // forward usage count change to attached outputs
3000f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (isDuplicated()) {
3001f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mOutput1->changeRefCount(stream, delta);
3002f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mOutput2->changeRefCount(stream, delta);
3003f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3004f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if ((delta + (int)mRefCount[stream]) < 0) {
300564cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("changeRefCount() invalid delta %d for stream %d, refCount %d", delta, stream, mRefCount[stream]);
3006f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mRefCount[stream] = 0;
3007f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
3008f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3009f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mRefCount[stream] += delta;
30106a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("changeRefCount() stream %d, count %d", stream, mRefCount[stream]);
3011f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3012f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3013f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinuint32_t AudioPolicyManagerBase::AudioOutputDescriptor::refCount()
3014f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3015f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    uint32_t refcount = 0;
3016f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) {
3017f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        refcount += mRefCount[i];
3018f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3019f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return refcount;
3020f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3021f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3022f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinuint32_t AudioPolicyManagerBase::AudioOutputDescriptor::strategyRefCount(routing_strategy strategy)
3023f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3024f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    uint32_t refCount = 0;
3025f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) {
3026f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (getStrategy((AudioSystem::stream_type)i) == strategy) {
3027f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            refCount += mRefCount[i];
3028f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
3029f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3030f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return refCount;
3031f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3032f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3033f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurentaudio_devices_t AudioPolicyManagerBase::AudioOutputDescriptor::supportedDevices()
3034b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent{
3035b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (isDuplicated()) {
3036f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent        return (audio_devices_t)(mOutput1->supportedDevices() | mOutput2->supportedDevices());
3037b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    } else {
3038b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        return mProfile->mSupportedDevices ;
3039b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    }
3040b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent}
3041b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
3042f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::AudioOutputDescriptor::dump(int fd)
3043f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3044f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    const size_t SIZE = 256;
3045f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    char buffer[SIZE];
3046f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    String8 result;
3047f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3048f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Sampling rate: %d\n", mSamplingRate);
3049f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3050f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Format: %d\n", mFormat);
3051f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
305270c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, " Channels: %08x\n", mChannelMask);
3053f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3054f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Latency: %d\n", mLatency);
3055f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3056f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Flags %08x\n", mFlags);
3057f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3058f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Devices %08x\n", device());
3059f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3060f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Stream volume refCount muteCount\n");
3061f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3062f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) {
3063f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        snprintf(buffer, SIZE, " %02d     %.03f     %02d       %02d\n", i, mCurVolume[i], mRefCount[i], mMuteCount[i]);
3064f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        result.append(buffer);
3065f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3066f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, result.string(), result.size());
3067f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3068f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
3069f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3070f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3071f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// --- AudioInputDescriptor class implementation
3072f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
30735ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric LaurentAudioPolicyManagerBase::AudioInputDescriptor::AudioInputDescriptor(const IOProfile *profile)
307470c236c9290732782d5267935af1475b8d5ae602Eric Laurent    : mSamplingRate(0), mFormat((audio_format_t)0), mChannelMask((audio_channel_mask_t)0),
3075ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent      mDevice(AUDIO_DEVICE_NONE), mRefCount(0),
30765ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent      mInputSource(0), mProfile(profile)
3077f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3078f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3079f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3080f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::AudioInputDescriptor::dump(int fd)
3081f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3082f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    const size_t SIZE = 256;
3083f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    char buffer[SIZE];
3084f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    String8 result;
3085f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3086f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Sampling rate: %d\n", mSamplingRate);
3087f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3088f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Format: %d\n", mFormat);
3089f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
309070c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, " Channels: %08x\n", mChannelMask);
3091f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3092f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Devices %08x\n", mDevice);
3093f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3094f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Ref Count %d\n", mRefCount);
3095f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3096f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, result.string(), result.size());
3097f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3098f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
3099f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3100f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3101f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// --- StreamDescriptor class implementation
3102f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3103c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric LaurentAudioPolicyManagerBase::StreamDescriptor::StreamDescriptor()
3104c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    :   mIndexMin(0), mIndexMax(1), mCanBeMuted(true)
3105c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent{
3106c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    mIndexCur.add(AUDIO_DEVICE_OUT_DEFAULT, 0);
3107c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent}
3108c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
3109c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentint AudioPolicyManagerBase::StreamDescriptor::getVolumeIndex(audio_devices_t device)
3110c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent{
3111c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    device = AudioPolicyManagerBase::getDeviceForVolume(device);
3112c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    // there is always a valid entry for AUDIO_DEVICE_OUT_DEFAULT
3113c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    if (mIndexCur.indexOfKey(device) < 0) {
3114c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        device = AUDIO_DEVICE_OUT_DEFAULT;
3115c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    }
3116c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    return mIndexCur.valueFor(device);
3117c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent}
3118c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
3119c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentvoid AudioPolicyManagerBase::StreamDescriptor::dump(int fd)
3120f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3121c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    const size_t SIZE = 256;
3122c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    char buffer[SIZE];
3123c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    String8 result;
3124c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
3125c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    snprintf(buffer, SIZE, "%s         %02d         %02d         ",
3126c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent             mCanBeMuted ? "true " : "false", mIndexMin, mIndexMax);
3127c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    result.append(buffer);
3128c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    for (size_t i = 0; i < mIndexCur.size(); i++) {
3129c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        snprintf(buffer, SIZE, "%04x : %02d, ",
3130c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                 mIndexCur.keyAt(i),
3131c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                 mIndexCur.valueAt(i));
3132c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        result.append(buffer);
3133c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    }
3134c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    result.append("\n");
3135c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
3136c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    write(fd, result.string(), result.size());
3137f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3138f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3139f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// --- EffectDescriptor class implementation
3140f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3141f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::EffectDescriptor::dump(int fd)
3142f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3143f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    const size_t SIZE = 256;
3144f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    char buffer[SIZE];
3145f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    String8 result;
3146f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
31471c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent    snprintf(buffer, SIZE, " I/O: %d\n", mIo);
3148f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3149f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Strategy: %d\n", mStrategy);
3150f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3151f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Session: %d\n", mSession);
3152f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3153f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Name: %s\n",  mDesc.name);
3154f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3155582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    snprintf(buffer, SIZE, " %s\n",  mEnabled ? "Enabled" : "Disabled");
3156582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    result.append(buffer);
3157f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, result.string(), result.size());
3158f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3159f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
3160f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3161f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
31625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent// --- IOProfile class implementation
31635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
316470c236c9290732782d5267935af1475b8d5ae602Eric LaurentAudioPolicyManagerBase::HwModule::HwModule(const char *name)
316570c236c9290732782d5267935af1475b8d5ae602Eric Laurent    : mName(strndup(name, AUDIO_HARDWARE_MODULE_ID_MAX_LEN)), mHandle(0)
316670c236c9290732782d5267935af1475b8d5ae602Eric Laurent{
316770c236c9290732782d5267935af1475b8d5ae602Eric Laurent}
316870c236c9290732782d5267935af1475b8d5ae602Eric Laurent
316970c236c9290732782d5267935af1475b8d5ae602Eric LaurentAudioPolicyManagerBase::HwModule::~HwModule()
317070c236c9290732782d5267935af1475b8d5ae602Eric Laurent{
317170c236c9290732782d5267935af1475b8d5ae602Eric Laurent    for (size_t i = 0; i < mOutputProfiles.size(); i++) {
317270c236c9290732782d5267935af1475b8d5ae602Eric Laurent         delete mOutputProfiles[i];
317370c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
317470c236c9290732782d5267935af1475b8d5ae602Eric Laurent    for (size_t i = 0; i < mInputProfiles.size(); i++) {
317570c236c9290732782d5267935af1475b8d5ae602Eric Laurent         delete mInputProfiles[i];
317670c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
317770c236c9290732782d5267935af1475b8d5ae602Eric Laurent    free((void *)mName);
317870c236c9290732782d5267935af1475b8d5ae602Eric Laurent}
317970c236c9290732782d5267935af1475b8d5ae602Eric Laurent
318070c236c9290732782d5267935af1475b8d5ae602Eric Laurentvoid AudioPolicyManagerBase::HwModule::dump(int fd)
318170c236c9290732782d5267935af1475b8d5ae602Eric Laurent{
318270c236c9290732782d5267935af1475b8d5ae602Eric Laurent    const size_t SIZE = 256;
318370c236c9290732782d5267935af1475b8d5ae602Eric Laurent    char buffer[SIZE];
318470c236c9290732782d5267935af1475b8d5ae602Eric Laurent    String8 result;
318570c236c9290732782d5267935af1475b8d5ae602Eric Laurent
318670c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "  - name: %s\n", mName);
318770c236c9290732782d5267935af1475b8d5ae602Eric Laurent    result.append(buffer);
318870c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "  - handle: %d\n", mHandle);
318970c236c9290732782d5267935af1475b8d5ae602Eric Laurent    result.append(buffer);
319070c236c9290732782d5267935af1475b8d5ae602Eric Laurent    write(fd, result.string(), result.size());
319170c236c9290732782d5267935af1475b8d5ae602Eric Laurent    if (mOutputProfiles.size()) {
319270c236c9290732782d5267935af1475b8d5ae602Eric Laurent        write(fd, "  - outputs:\n", sizeof("  - outputs:\n"));
319370c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (size_t i = 0; i < mOutputProfiles.size(); i++) {
3194599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            snprintf(buffer, SIZE, "    output %d:\n", i);
3195599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            write(fd, buffer, strlen(buffer));
319670c236c9290732782d5267935af1475b8d5ae602Eric Laurent            mOutputProfiles[i]->dump(fd);
319770c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
319870c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
319970c236c9290732782d5267935af1475b8d5ae602Eric Laurent    if (mInputProfiles.size()) {
320070c236c9290732782d5267935af1475b8d5ae602Eric Laurent        write(fd, "  - inputs:\n", sizeof("  - inputs:\n"));
320170c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (size_t i = 0; i < mInputProfiles.size(); i++) {
3202599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            snprintf(buffer, SIZE, "    input %d:\n", i);
3203599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            write(fd, buffer, strlen(buffer));
320470c236c9290732782d5267935af1475b8d5ae602Eric Laurent            mInputProfiles[i]->dump(fd);
320570c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
320670c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
320770c236c9290732782d5267935af1475b8d5ae602Eric Laurent}
320870c236c9290732782d5267935af1475b8d5ae602Eric Laurent
320970c236c9290732782d5267935af1475b8d5ae602Eric LaurentAudioPolicyManagerBase::IOProfile::IOProfile(HwModule *module)
32100977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent    : mFlags((audio_output_flags_t)0), mModule(module)
32115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
32125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
32135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
32145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric LaurentAudioPolicyManagerBase::IOProfile::~IOProfile()
32155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
321670c236c9290732782d5267935af1475b8d5ae602Eric Laurent}
321770c236c9290732782d5267935af1475b8d5ae602Eric Laurent
321870c236c9290732782d5267935af1475b8d5ae602Eric Laurent// checks if the IO profile is compatible with specified parameters. By convention a value of 0
321970c236c9290732782d5267935af1475b8d5ae602Eric Laurent// means a parameter is don't care
322070c236c9290732782d5267935af1475b8d5ae602Eric Laurentbool AudioPolicyManagerBase::IOProfile::isCompatibleProfile(audio_devices_t device,
322170c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                            uint32_t samplingRate,
322270c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                            uint32_t format,
322370c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                            uint32_t channelMask,
32240977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent                                                            audio_output_flags_t flags) const
322570c236c9290732782d5267935af1475b8d5ae602Eric Laurent{
322670c236c9290732782d5267935af1475b8d5ae602Eric Laurent    if ((mSupportedDevices & device) != device) {
322770c236c9290732782d5267935af1475b8d5ae602Eric Laurent        return false;
322870c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
322970c236c9290732782d5267935af1475b8d5ae602Eric Laurent    if ((mFlags & flags) != flags) {
323070c236c9290732782d5267935af1475b8d5ae602Eric Laurent        return false;
323170c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
323270c236c9290732782d5267935af1475b8d5ae602Eric Laurent    if (samplingRate != 0) {
323370c236c9290732782d5267935af1475b8d5ae602Eric Laurent        size_t i;
323470c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (i = 0; i < mSamplingRates.size(); i++)
323570c236c9290732782d5267935af1475b8d5ae602Eric Laurent        {
323670c236c9290732782d5267935af1475b8d5ae602Eric Laurent            if (mSamplingRates[i] == samplingRate) {
323770c236c9290732782d5267935af1475b8d5ae602Eric Laurent                break;
323870c236c9290732782d5267935af1475b8d5ae602Eric Laurent            }
323970c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
324070c236c9290732782d5267935af1475b8d5ae602Eric Laurent        if (i == mSamplingRates.size()) {
324170c236c9290732782d5267935af1475b8d5ae602Eric Laurent            return false;
324270c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
324370c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
324470c236c9290732782d5267935af1475b8d5ae602Eric Laurent    if (format != 0) {
324570c236c9290732782d5267935af1475b8d5ae602Eric Laurent        size_t i;
324670c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (i = 0; i < mFormats.size(); i++)
324770c236c9290732782d5267935af1475b8d5ae602Eric Laurent        {
324870c236c9290732782d5267935af1475b8d5ae602Eric Laurent            if (mFormats[i] == format) {
324970c236c9290732782d5267935af1475b8d5ae602Eric Laurent                break;
325070c236c9290732782d5267935af1475b8d5ae602Eric Laurent            }
325170c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
325270c236c9290732782d5267935af1475b8d5ae602Eric Laurent        if (i == mFormats.size()) {
325370c236c9290732782d5267935af1475b8d5ae602Eric Laurent            return false;
325470c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
325570c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
325670c236c9290732782d5267935af1475b8d5ae602Eric Laurent    if (channelMask != 0) {
325770c236c9290732782d5267935af1475b8d5ae602Eric Laurent        size_t i;
325870c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (i = 0; i < mChannelMasks.size(); i++)
325970c236c9290732782d5267935af1475b8d5ae602Eric Laurent        {
326070c236c9290732782d5267935af1475b8d5ae602Eric Laurent            if (mChannelMasks[i] == channelMask) {
326170c236c9290732782d5267935af1475b8d5ae602Eric Laurent                break;
326270c236c9290732782d5267935af1475b8d5ae602Eric Laurent            }
326370c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
326470c236c9290732782d5267935af1475b8d5ae602Eric Laurent        if (i == mChannelMasks.size()) {
326570c236c9290732782d5267935af1475b8d5ae602Eric Laurent            return false;
326670c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
326770c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
326870c236c9290732782d5267935af1475b8d5ae602Eric Laurent    return true;
32695ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
32705ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
32715ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::IOProfile::dump(int fd)
32725ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
32735ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    const size_t SIZE = 256;
32745ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    char buffer[SIZE];
32755ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    String8 result;
32765ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
327770c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "    - sampling rates: ");
32785ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    result.append(buffer);
32795ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < mSamplingRates.size(); i++) {
32805ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        snprintf(buffer, SIZE, "%d", mSamplingRates[i]);
32815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        result.append(buffer);
32825ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        result.append(i == (mSamplingRates.size() - 1) ? "\n" : ", ");
32835ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
32845ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
328570c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "    - channel masks: ");
32865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    result.append(buffer);
32875ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < mChannelMasks.size(); i++) {
32885ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        snprintf(buffer, SIZE, "%04x", mChannelMasks[i]);
32895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        result.append(buffer);
32905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        result.append(i == (mChannelMasks.size() - 1) ? "\n" : ", ");
32915ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
32925ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
329370c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "    - formats: ");
32945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    result.append(buffer);
32955ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < mFormats.size(); i++) {
32965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        snprintf(buffer, SIZE, "%d", mFormats[i]);
32975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        result.append(buffer);
32985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        result.append(i == (mFormats.size() - 1) ? "\n" : ", ");
32995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
33005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
330170c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "    - devices: %04x\n", mSupportedDevices);
33025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    result.append(buffer);
330370c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "    - flags: %04x\n", mFlags);
33045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    result.append(buffer);
33055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
33065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    write(fd, result.string(), result.size());
33075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
33085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
33095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent// --- audio_policy.conf file parsing
33105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
33115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentstruct StringToEnum {
33125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    const char *name;
33135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    uint32_t value;
33145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent};
33155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
33165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent#define STRING_TO_ENUM(string) { #string, string }
33175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
33185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
33195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentconst struct StringToEnum sDeviceNameToEnumTable[] = {
33205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_EARPIECE),
33215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_SPEAKER),
33225ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_WIRED_HEADSET),
33235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_WIRED_HEADPHONE),
33245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_ALL_SCO),
33255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_ALL_A2DP),
33265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_AUX_DIGITAL),
33275ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET),
3328599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET),
3329599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_USB_DEVICE),
3330599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_USB_ACCESSORY),
3331599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_ALL_USB),
333248387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi    STRING_TO_ENUM(AUDIO_DEVICE_OUT_REMOTE_SUBMIX),
33335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_BUILTIN_MIC),
33345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET),
33355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_WIRED_HEADSET),
33365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_AUX_DIGITAL),
33375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_VOICE_CALL),
33385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_BACK_MIC),
333948387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi    STRING_TO_ENUM(AUDIO_DEVICE_IN_REMOTE_SUBMIX),
3340ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET),
3341ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_DGTL_DOCK_HEADSET),
3342ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_USB_ACCESSORY),
33435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent};
33445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
33455ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentconst struct StringToEnum sFlagNameToEnumTable[] = {
33460977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent    STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT),
33470977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent    STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY),
3348b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent    STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST),
3349b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent    STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER),
33505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent};
33515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
33525ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentconst struct StringToEnum sFormatNameToEnumTable[] = {
33535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
33545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
33555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_FORMAT_MP3),
33565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_FORMAT_AAC),
33575ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
33585ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent};
33595ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
33605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentconst struct StringToEnum sOutChannelsNameToEnumTable[] = {
33615ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_OUT_MONO),
33625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
33635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
33645ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
33655ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent};
33665ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
33675ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentconst struct StringToEnum sInChannelsNameToEnumTable[] = {
33685ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_IN_MONO),
33695ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_IN_STEREO),
33705ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent};
33715ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
33725ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
33735ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentuint32_t AudioPolicyManagerBase::stringToEnum(const struct StringToEnum *table,
33745ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                              size_t size,
33755ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                              const char *name)
33765ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
33775ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < size; i++) {
33785ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strcmp(table[i].name, name) == 0) {
33795ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("stringToEnum() found %s", table[i].name);
33805ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            return table[i].value;
33815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
33825ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
33835ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return 0;
33845ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
33855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
33860977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurentaudio_output_flags_t AudioPolicyManagerBase::parseFlagNames(char *name)
33875ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
33885ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    uint32_t flag = 0;
33895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
33905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // it is OK to cast name to non const here as we are not going to use it after
33915ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // strtok() modifies it
33925ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    char *flagName = strtok(name, "|");
33935ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (flagName != NULL) {
33945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strlen(flagName) != 0) {
33955ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            flag |= stringToEnum(sFlagNameToEnumTable,
33965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                               ARRAY_SIZE(sFlagNameToEnumTable),
33975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                               flagName);
33985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
33995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        flagName = strtok(NULL, "|");
34005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
34010977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent    return (audio_output_flags_t)flag;
34025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
34035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
34045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentaudio_devices_t AudioPolicyManagerBase::parseDeviceNames(char *name)
34055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
34065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    uint32_t device = 0;
34075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
34085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    char *devName = strtok(name, "|");
34095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (devName != NULL) {
34105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strlen(devName) != 0) {
34115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device |= stringToEnum(sDeviceNameToEnumTable,
34125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                 ARRAY_SIZE(sDeviceNameToEnumTable),
34135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                 devName);
34145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
34155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        devName = strtok(NULL, "|");
34165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
3417c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    return device;
34185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
34195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
34205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadSamplingRates(char *name, IOProfile *profile)
34215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
34225ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    char *str = strtok(name, "|");
34235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
34243cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    // by convention, "0' in the first entry in mSamplingRates indicates the supported sampling
34253cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    // rates should be read from the output stream after it is opened for the first time
34263cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) {
34273cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        profile->mSamplingRates.add(0);
34283cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        return;
34293cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    }
34303cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
34315ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (str != NULL) {
34325ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        uint32_t rate = atoi(str);
34335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (rate != 0) {
34345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadSamplingRates() adding rate %d", rate);
34355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mSamplingRates.add(rate);
34365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
34375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        str = strtok(NULL, "|");
34385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
34395ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return;
34405ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
34415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
34425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadFormats(char *name, IOProfile *profile)
34435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
34445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    char *str = strtok(name, "|");
34455ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
34463cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    // by convention, "0' in the first entry in mFormats indicates the supported formats
34473cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    // should be read from the output stream after it is opened for the first time
34483cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) {
34493cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        profile->mFormats.add((audio_format_t)0);
34503cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        return;
34513cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    }
34523cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
34535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (str != NULL) {
34545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_format_t format = (audio_format_t)stringToEnum(sFormatNameToEnumTable,
34555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                             ARRAY_SIZE(sFormatNameToEnumTable),
34565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                             str);
34575ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (format != 0) {
34585ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mFormats.add(format);
34595ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
34605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        str = strtok(NULL, "|");
34615ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
34625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return;
34635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
34645ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
34655ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadInChannels(char *name, IOProfile *profile)
34665ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
34675ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    const char *str = strtok(name, "|");
34685ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
34695ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGV("loadInChannels() %s", name);
34703cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
34713cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) {
34723cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        profile->mChannelMasks.add((audio_channel_mask_t)0);
34733cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        return;
34743cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    }
34753cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
34765ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (str != NULL) {
34775ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_channel_mask_t channelMask =
34785ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                (audio_channel_mask_t)stringToEnum(sInChannelsNameToEnumTable,
34795ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                   ARRAY_SIZE(sInChannelsNameToEnumTable),
34805ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                   str);
34815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (channelMask != 0) {
34825ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadInChannels() adding channelMask %04x", channelMask);
34835ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mChannelMasks.add(channelMask);
34845ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
34855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        str = strtok(NULL, "|");
34865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
34875ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return;
34885ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
34895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
34905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadOutChannels(char *name, IOProfile *profile)
34915ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
34925ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    const char *str = strtok(name, "|");
34933cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
34943cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGV("loadOutChannels() %s", name);
34953cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
34963cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    // by convention, "0' in the first entry in mChannelMasks indicates the supported channel
34973cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    // masks should be read from the output stream after it is opened for the first time
34983cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) {
34993cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        profile->mChannelMasks.add((audio_channel_mask_t)0);
35003cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        return;
35013cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    }
35025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
35035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (str != NULL) {
35043cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        audio_channel_mask_t channelMask =
35053cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                (audio_channel_mask_t)stringToEnum(sOutChannelsNameToEnumTable,
35063cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                   ARRAY_SIZE(sOutChannelsNameToEnumTable),
35073cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                   str);
35085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (channelMask != 0) {
35095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mChannelMasks.add(channelMask);
35105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
35115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        str = strtok(NULL, "|");
35125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
35135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return;
35145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
35155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
351670c236c9290732782d5267935af1475b8d5ae602Eric Laurentstatus_t AudioPolicyManagerBase::loadInput(cnode *root, HwModule *module)
35175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
35185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    cnode *node = root->first_child;
35195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
35205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    IOProfile *profile = new IOProfile(module);
35215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
35225ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (node) {
35235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
35245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            loadSamplingRates((char *)node->value, profile);
35255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, FORMATS_TAG) == 0) {
35265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            loadFormats((char *)node->value, profile);
35275ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, CHANNELS_TAG) == 0) {
35285ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            loadInChannels((char *)node->value, profile);
35295ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, DEVICES_TAG) == 0) {
35305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mSupportedDevices = parseDeviceNames((char *)node->value);
35315ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
35325ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        node = node->next;
35335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
3534ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    ALOGW_IF(profile->mSupportedDevices == AUDIO_DEVICE_NONE,
35355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadInput() invalid supported devices");
35365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGW_IF(profile->mChannelMasks.size() == 0,
35375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadInput() invalid supported channel masks");
35385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGW_IF(profile->mSamplingRates.size() == 0,
35395ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadInput() invalid supported sampling rates");
35405ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGW_IF(profile->mFormats.size() == 0,
35415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadInput() invalid supported formats");
3542ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if ((profile->mSupportedDevices != AUDIO_DEVICE_NONE) &&
35435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            (profile->mChannelMasks.size() != 0) &&
35445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            (profile->mSamplingRates.size() != 0) &&
35455ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            (profile->mFormats.size() != 0)) {
35465ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
35475ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        ALOGV("loadInput() adding input mSupportedDevices %04x", profile->mSupportedDevices);
35485ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
354970c236c9290732782d5267935af1475b8d5ae602Eric Laurent        module->mInputProfiles.add(profile);
35505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return NO_ERROR;
35515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    } else {
35525ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        delete profile;
35535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return BAD_VALUE;
35545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
35555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
35565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
355770c236c9290732782d5267935af1475b8d5ae602Eric Laurentstatus_t AudioPolicyManagerBase::loadOutput(cnode *root, HwModule *module)
35585ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
35595ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    cnode *node = root->first_child;
35605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
35615ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    IOProfile *profile = new IOProfile(module);
35625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
35635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (node) {
35645ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
35655ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            loadSamplingRates((char *)node->value, profile);
35665ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, FORMATS_TAG) == 0) {
35675ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            loadFormats((char *)node->value, profile);
35685ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, CHANNELS_TAG) == 0) {
35695ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            loadOutChannels((char *)node->value, profile);
35705ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, DEVICES_TAG) == 0) {
35715ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mSupportedDevices = parseDeviceNames((char *)node->value);
35725ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, FLAGS_TAG) == 0) {
35735ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mFlags = parseFlagNames((char *)node->value);
35745ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
35755ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        node = node->next;
35765ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
3577ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    ALOGW_IF(profile->mSupportedDevices == AUDIO_DEVICE_NONE,
35785ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadOutput() invalid supported devices");
35795ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGW_IF(profile->mChannelMasks.size() == 0,
35805ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadOutput() invalid supported channel masks");
35815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGW_IF(profile->mSamplingRates.size() == 0,
35825ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadOutput() invalid supported sampling rates");
35835ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGW_IF(profile->mFormats.size() == 0,
35845ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadOutput() invalid supported formats");
3585ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if ((profile->mSupportedDevices != AUDIO_DEVICE_NONE) &&
35865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            (profile->mChannelMasks.size() != 0) &&
35875ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            (profile->mSamplingRates.size() != 0) &&
35885ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            (profile->mFormats.size() != 0)) {
35895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
35905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        ALOGV("loadOutput() adding output mSupportedDevices %04x, mFlags %04x",
35915ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent              profile->mSupportedDevices, profile->mFlags);
35925ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
359370c236c9290732782d5267935af1475b8d5ae602Eric Laurent        module->mOutputProfiles.add(profile);
35945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return NO_ERROR;
35955ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    } else {
35965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        delete profile;
35975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return BAD_VALUE;
35985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
35995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
36005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
36015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadHwModule(cnode *root)
36025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
36035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    cnode *node = config_find(root, OUTPUTS_TAG);
36045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    status_t status = NAME_NOT_FOUND;
360570c236c9290732782d5267935af1475b8d5ae602Eric Laurent
360670c236c9290732782d5267935af1475b8d5ae602Eric Laurent    HwModule *module = new HwModule(root->name);
360770c236c9290732782d5267935af1475b8d5ae602Eric Laurent
36085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (node != NULL) {
36095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strcmp(root->name, AUDIO_HARDWARE_MODULE_ID_A2DP) == 0) {
36105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            mHasA2dp = true;
3611599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent        } else if (strcmp(root->name, AUDIO_HARDWARE_MODULE_ID_USB) == 0) {
3612599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            mHasUsb = true;
361348387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi        } else if (strcmp(root->name, AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX) == 0) {
361448387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            mHasRemoteSubmix = true;
36155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
3616599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent
36175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        node = node->first_child;
36185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        while (node) {
36195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadHwModule() loading output %s", node->name);
362070c236c9290732782d5267935af1475b8d5ae602Eric Laurent            status_t tmpStatus = loadOutput(node, module);
36215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            if (status == NAME_NOT_FOUND || status == NO_ERROR) {
36225ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                status = tmpStatus;
36235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
36245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            node = node->next;
36255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
36265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
36275ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    node = config_find(root, INPUTS_TAG);
36285ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (node != NULL) {
36295ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        node = node->first_child;
36305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        while (node) {
36315ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadHwModule() loading input %s", node->name);
363270c236c9290732782d5267935af1475b8d5ae602Eric Laurent            status_t tmpStatus = loadInput(node, module);
36335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            if (status == NAME_NOT_FOUND || status == NO_ERROR) {
36345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                status = tmpStatus;
36355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
36365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            node = node->next;
36375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
36385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
36395ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (status == NO_ERROR) {
364070c236c9290732782d5267935af1475b8d5ae602Eric Laurent        mHwModules.add(module);
364170c236c9290732782d5267935af1475b8d5ae602Eric Laurent    } else {
364270c236c9290732782d5267935af1475b8d5ae602Eric Laurent        delete module;
36435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
36445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
36455ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
36465ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadHwModules(cnode *root)
36475ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
36485ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    cnode *node = config_find(root, AUDIO_HW_MODULE_TAG);
36495ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (node == NULL) {
36505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return;
36515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
36525ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
36535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    node = node->first_child;
36545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (node) {
36555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        ALOGV("loadHwModules() loading module %s", node->name);
36565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        loadHwModule(node);
36575ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        node = node->next;
36585ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
36595ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
36605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
36615ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadGlobalConfig(cnode *root)
36625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
36635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    cnode *node = config_find(root, GLOBAL_CONFIG_TAG);
36645ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (node == NULL) {
36655ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return;
36665ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
36675ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    node = node->first_child;
36685ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (node) {
36695ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strcmp(ATTACHED_OUTPUT_DEVICES_TAG, node->name) == 0) {
36705ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            mAttachedOutputDevices = parseDeviceNames((char *)node->value);
3671ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            ALOGW_IF(mAttachedOutputDevices == AUDIO_DEVICE_NONE,
3672c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                    "loadGlobalConfig() no attached output devices");
36735ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadGlobalConfig() mAttachedOutputDevices %04x", mAttachedOutputDevices);
36745ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(DEFAULT_OUTPUT_DEVICE_TAG, node->name) == 0) {
36755ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            mDefaultOutputDevice = (audio_devices_t)stringToEnum(sDeviceNameToEnumTable,
36765ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                              ARRAY_SIZE(sDeviceNameToEnumTable),
36775ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                              (char *)node->value);
3678ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            ALOGW_IF(mDefaultOutputDevice == AUDIO_DEVICE_NONE,
3679c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                    "loadGlobalConfig() default device not specified");
36805ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadGlobalConfig() mDefaultOutputDevice %04x", mDefaultOutputDevice);
36815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(ATTACHED_INPUT_DEVICES_TAG, node->name) == 0) {
3682ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            mAvailableInputDevices = parseDeviceNames((char *)node->value) & ~AUDIO_DEVICE_BIT_IN;
36835ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadGlobalConfig() mAvailableInputDevices %04x", mAvailableInputDevices);
36845ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
36855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        node = node->next;
36865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
36875ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
36885ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
36895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentstatus_t AudioPolicyManagerBase::loadAudioPolicyConfig(const char *path)
36905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
36915ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    cnode *root;
36925ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    char *data;
36935ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
36945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    data = (char *)load_file(path, NULL);
36955ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (data == NULL) {
36965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return -ENODEV;
36975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
36985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    root = config_node("", "");
36995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    config_load(root, data);
37005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
37015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    loadGlobalConfig(root);
37025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    loadHwModules(root);
37035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
37045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    config_free(root);
37055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    free(root);
37065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    free(data);
37075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
37085ec145df7708564d385fd3fb764085321cf4c253Dima Zavin    ALOGI("loadAudioPolicyConfig() loaded %s\n", path);
37095ec145df7708564d385fd3fb764085321cf4c253Dima Zavin
37105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return NO_ERROR;
37115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
3712f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3713739022f26a7127ba76a98dda65411496086114a7Dima Zavinvoid AudioPolicyManagerBase::defaultAudioPolicyConfig(void)
3714739022f26a7127ba76a98dda65411496086114a7Dima Zavin{
3715739022f26a7127ba76a98dda65411496086114a7Dima Zavin    HwModule *module;
3716739022f26a7127ba76a98dda65411496086114a7Dima Zavin    IOProfile *profile;
3717739022f26a7127ba76a98dda65411496086114a7Dima Zavin
3718739022f26a7127ba76a98dda65411496086114a7Dima Zavin    mDefaultOutputDevice = AUDIO_DEVICE_OUT_SPEAKER;
3719739022f26a7127ba76a98dda65411496086114a7Dima Zavin    mAttachedOutputDevices = AUDIO_DEVICE_OUT_SPEAKER;
3720ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    mAvailableInputDevices = AUDIO_DEVICE_IN_BUILTIN_MIC & ~AUDIO_DEVICE_BIT_IN;
3721739022f26a7127ba76a98dda65411496086114a7Dima Zavin
3722739022f26a7127ba76a98dda65411496086114a7Dima Zavin    module = new HwModule("primary");
3723739022f26a7127ba76a98dda65411496086114a7Dima Zavin
3724739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile = new IOProfile(module);
3725739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mSamplingRates.add(44100);
3726739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
3727739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mChannelMasks.add(AUDIO_CHANNEL_OUT_STEREO);
3728739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mSupportedDevices = AUDIO_DEVICE_OUT_SPEAKER;
3729739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mFlags = AUDIO_OUTPUT_FLAG_PRIMARY;
3730739022f26a7127ba76a98dda65411496086114a7Dima Zavin    module->mOutputProfiles.add(profile);
3731739022f26a7127ba76a98dda65411496086114a7Dima Zavin
3732739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile = new IOProfile(module);
3733739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mSamplingRates.add(8000);
3734739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
3735739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mChannelMasks.add(AUDIO_CHANNEL_IN_MONO);
3736739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mSupportedDevices = AUDIO_DEVICE_IN_BUILTIN_MIC;
3737739022f26a7127ba76a98dda65411496086114a7Dima Zavin    module->mInputProfiles.add(profile);
3738739022f26a7127ba76a98dda65411496086114a7Dima Zavin
3739739022f26a7127ba76a98dda65411496086114a7Dima Zavin    mHwModules.add(module);
3740739022f26a7127ba76a98dda65411496086114a7Dima Zavin}
3741f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3742f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}; // namespace android
3743