AudioPolicyManagerBase.cpp revision 772686310a3bc44d93591349e174a60360a3d30d
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
344772686310a3bc44d93591349e174a60360a3d30dSathishKumar Mani    if (isStateInCall(state)) {
345772686310a3bc44d93591349e174a60360a3d30dSathishKumar Mani        for (size_t i = 0; i < mOutputs.size(); i++) {
346772686310a3bc44d93591349e174a60360a3d30dSathishKumar Mani            AudioOutputDescriptor *desc = mOutputs.valueAt(i);
347772686310a3bc44d93591349e174a60360a3d30dSathishKumar Mani            //take the biggest latency for all outputs
348772686310a3bc44d93591349e174a60360a3d30dSathishKumar Mani            if (delayMs < desc->mLatency*2) {
349772686310a3bc44d93591349e174a60360a3d30dSathishKumar Mani                delayMs = desc->mLatency*2;
350772686310a3bc44d93591349e174a60360a3d30dSathishKumar Mani            }
351772686310a3bc44d93591349e174a60360a3d30dSathishKumar Mani            //mute STRATEGY_MEDIA on all outputs
352772686310a3bc44d93591349e174a60360a3d30dSathishKumar Mani            if (desc->strategyRefCount(STRATEGY_MEDIA) != 0) {
353772686310a3bc44d93591349e174a60360a3d30dSathishKumar Mani                setStrategyMute(STRATEGY_MEDIA, true, mOutputs.keyAt(i));
354772686310a3bc44d93591349e174a60360a3d30dSathishKumar Mani                setStrategyMute(STRATEGY_MEDIA, false, mOutputs.keyAt(i), MUTE_TIME_MS,
355772686310a3bc44d93591349e174a60360a3d30dSathishKumar Mani                    getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
356772686310a3bc44d93591349e174a60360a3d30dSathishKumar Mani            }
357772686310a3bc44d93591349e174a60360a3d30dSathishKumar Mani        }
358772686310a3bc44d93591349e174a60360a3d30dSathishKumar Mani    }
359772686310a3bc44d93591349e174a60360a3d30dSathishKumar Mani
360f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // change routing is necessary
361b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    setOutputDevice(mPrimaryOutput, newDevice, force, delayMs);
362f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
363f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if entering in call state, handle special case of active streams
364f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // pertaining to sonification strategy see handleIncallSonification()
365f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (isStateInCall(state)) {
3666a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("setPhoneState() in call state management: new state is %d", state);
367f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // unmute the ringing tone after a sufficient delay if it was muted before
368f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // setting output device above
369f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (oldState == AudioSystem::MODE_RINGTONE) {
370b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            setStreamMute(AudioSystem::RING, false, mPrimaryOutput, MUTE_TIME_MS);
371f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
372f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
373f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            handleIncallSonification(stream, true, true);
374f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
375f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
376f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
377f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
378f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (state == AudioSystem::MODE_RINGTONE &&
379f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        isStreamActive(AudioSystem::MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
380f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mLimitRingtoneVolume = true;
381f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
382f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mLimitRingtoneVolume = false;
383f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
384f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
385f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
386f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::setForceUse(AudioSystem::force_use usage, AudioSystem::forced_config config)
387f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3886a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mPhoneState);
389f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
390f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    bool forceVolumeReeval = false;
391f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    switch(usage) {
392f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::FOR_COMMUNICATION:
393f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (config != AudioSystem::FORCE_SPEAKER && config != AudioSystem::FORCE_BT_SCO &&
394f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_NONE) {
39564cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("setForceUse() invalid config %d for FOR_COMMUNICATION", config);
396f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return;
397f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
398f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        forceVolumeReeval = true;
399f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mForceUse[usage] = config;
400f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
401f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::FOR_MEDIA:
402f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (config != AudioSystem::FORCE_HEADPHONES && config != AudioSystem::FORCE_BT_A2DP &&
403f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_WIRED_ACCESSORY &&
404f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_ANALOG_DOCK &&
4051afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent            config != AudioSystem::FORCE_DIGITAL_DOCK && config != AudioSystem::FORCE_NONE &&
40631363a9cb94e80330c335fede0b92b1953a09517Jean-Michel Trivi            config != AudioSystem::FORCE_NO_BT_A2DP) {
40764cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("setForceUse() invalid config %d for FOR_MEDIA", config);
408f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return;
409f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
410f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mForceUse[usage] = config;
411f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
412f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::FOR_RECORD:
413f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (config != AudioSystem::FORCE_BT_SCO && config != AudioSystem::FORCE_WIRED_ACCESSORY &&
414f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_NONE) {
41564cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("setForceUse() invalid config %d for FOR_RECORD", config);
416f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return;
417f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
418f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mForceUse[usage] = config;
419f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
420f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::FOR_DOCK:
421f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (config != AudioSystem::FORCE_NONE && config != AudioSystem::FORCE_BT_CAR_DOCK &&
422f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_BT_DESK_DOCK &&
423f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_WIRED_ACCESSORY &&
424f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_ANALOG_DOCK &&
425f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_DIGITAL_DOCK) {
42664cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("setForceUse() invalid config %d for FOR_DOCK", config);
427f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
428f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        forceVolumeReeval = true;
429f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mForceUse[usage] = config;
430f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
431738207def5f691d605ae33d041116829a74513a9Eric Laurent    case AudioSystem::FOR_SYSTEM:
432738207def5f691d605ae33d041116829a74513a9Eric Laurent        if (config != AudioSystem::FORCE_NONE &&
433738207def5f691d605ae33d041116829a74513a9Eric Laurent            config != AudioSystem::FORCE_SYSTEM_ENFORCED) {
434738207def5f691d605ae33d041116829a74513a9Eric Laurent            ALOGW("setForceUse() invalid config %d for FOR_SYSTEM", config);
435738207def5f691d605ae33d041116829a74513a9Eric Laurent        }
436738207def5f691d605ae33d041116829a74513a9Eric Laurent        forceVolumeReeval = true;
437738207def5f691d605ae33d041116829a74513a9Eric Laurent        mForceUse[usage] = config;
438738207def5f691d605ae33d041116829a74513a9Eric Laurent        break;
439f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    default:
44064cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("setForceUse() invalid usage %d", usage);
441f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
442f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
443f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
4445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // check for device and output changes triggered by new force usage
445f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkA2dpSuspend();
446f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForAllStrategies();
447c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    updateDevicesAndOutputs();
4485ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < mOutputs.size(); i++) {
4495ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_io_handle_t output = mOutputs.keyAt(i);
4505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_devices_t newDevice = getNewDevice(output, true /*fromCache*/);
451ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        setOutputDevice(output, newDevice, (newDevice != AUDIO_DEVICE_NONE));
452ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
4535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            applyStreamVolumes(output, newDevice, 0, true);
4545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
455f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
456f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
457f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    audio_io_handle_t activeInput = getActiveInput();
458f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (activeInput != 0) {
459f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioInputDescriptor *inputDesc = mInputs.valueFor(activeInput);
4605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_devices_t newDevice = getDeviceForInputSource(inputDesc->mInputSource);
461ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if ((newDevice != AUDIO_DEVICE_NONE) && (newDevice != inputDesc->mDevice)) {
4626a70518b93928d1c91457ff805e375c82d76b0e5Steve Block            ALOGV("setForceUse() changing device from %x to %x for input %d",
463f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    inputDesc->mDevice, newDevice, activeInput);
464f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            inputDesc->mDevice = newDevice;
465f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            AudioParameter param = AudioParameter();
466f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            param.addInt(String8(AudioParameter::keyRouting), (int)newDevice);
467f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mpClientInterface->setParameters(activeInput, param.toString());
468f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
469f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
470f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
471f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
472f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
473f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima ZavinAudioSystem::forced_config AudioPolicyManagerBase::getForceUse(AudioSystem::force_use usage)
474f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
475f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return mForceUse[usage];
476f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
477f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
478f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::setSystemProperty(const char* property, const char* value)
479f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
4806a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("setSystemProperty() property %s, value %s", property, value);
481f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
482f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
4833cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric LaurentAudioPolicyManagerBase::IOProfile *AudioPolicyManagerBase::getProfileForDirectOutput(
4843cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                               audio_devices_t device,
48570c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                               uint32_t samplingRate,
48670c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                               uint32_t format,
48770c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                               uint32_t channelMask,
4880977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent                                                               audio_output_flags_t flags)
48970c236c9290732782d5267935af1475b8d5ae602Eric Laurent{
49070c236c9290732782d5267935af1475b8d5ae602Eric Laurent    for (size_t i = 0; i < mHwModules.size(); i++) {
49170c236c9290732782d5267935af1475b8d5ae602Eric Laurent        if (mHwModules[i]->mHandle == 0) {
49270c236c9290732782d5267935af1475b8d5ae602Eric Laurent            continue;
49370c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
49470c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) {
4953cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent           IOProfile *profile = mHwModules[i]->mOutputProfiles[j];
4963cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent           if (profile->isCompatibleProfile(device, samplingRate, format,
4973cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                           channelMask,
4983cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                           AUDIO_OUTPUT_FLAG_DIRECT)) {
4993cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent               if (mAvailableOutputDevices & profile->mSupportedDevices) {
5003cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                   return mHwModules[i]->mOutputProfiles[j];
5013cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent               }
50270c236c9290732782d5267935af1475b8d5ae602Eric Laurent           }
50370c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
50470c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
50570c236c9290732782d5267935af1475b8d5ae602Eric Laurent    return 0;
50670c236c9290732782d5267935af1475b8d5ae602Eric Laurent}
50770c236c9290732782d5267935af1475b8d5ae602Eric Laurent
508f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinaudio_io_handle_t AudioPolicyManagerBase::getOutput(AudioSystem::stream_type stream,
509f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    uint32_t samplingRate,
510f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    uint32_t format,
51170c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                    uint32_t channelMask,
512f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    AudioSystem::output_flags flags)
513f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
514f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    audio_io_handle_t output = 0;
515f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    uint32_t latency = 0;
516f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    routing_strategy strategy = getStrategy((AudioSystem::stream_type)stream);
5175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
51870c236c9290732782d5267935af1475b8d5ae602Eric Laurent    ALOGV("getOutput() stream %d, samplingRate %d, format %d, channelMask %x, flags %x",
51970c236c9290732782d5267935af1475b8d5ae602Eric Laurent          stream, samplingRate, format, channelMask, flags);
520f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
521f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
522f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mCurOutput != 0) {
52370c236c9290732782d5267935af1475b8d5ae602Eric Laurent        ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
524f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
525f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
526f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (mTestOutputs[mCurOutput] == 0) {
5276a70518b93928d1c91457ff805e375c82d76b0e5Steve Block            ALOGV("getOutput() opening test output");
5283cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(NULL);
529f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            outputDesc->mDevice = mTestDevice;
530f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            outputDesc->mSamplingRate = mTestSamplingRate;
531f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            outputDesc->mFormat = mTestFormat;
53270c236c9290732782d5267935af1475b8d5ae602Eric Laurent            outputDesc->mChannelMask = mTestChannels;
533f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            outputDesc->mLatency = mTestLatencyMs;
5340977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent            outputDesc->mFlags = (audio_output_flags_t)(mDirectOutput ? AudioSystem::OUTPUT_FLAG_DIRECT : 0);
535f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            outputDesc->mRefCount[stream] = 0;
53670c236c9290732782d5267935af1475b8d5ae602Eric Laurent            mTestOutputs[mCurOutput] = mpClientInterface->openOutput(0, &outputDesc->mDevice,
537f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            &outputDesc->mSamplingRate,
538f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            &outputDesc->mFormat,
53970c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                            &outputDesc->mChannelMask,
540f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            &outputDesc->mLatency,
541f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            outputDesc->mFlags);
542f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (mTestOutputs[mCurOutput]) {
543f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                AudioParameter outputCmd = AudioParameter();
544f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                outputCmd.addInt(String8("set_id"),mCurOutput);
545f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
546f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                addOutput(mTestOutputs[mCurOutput], outputDesc);
547f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
548f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
549f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return mTestOutputs[mCurOutput];
550f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
551f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
552f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
553f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // open a direct output if required by specified parameters
5543cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    IOProfile *profile = getProfileForDirectOutput(device,
5553cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                   samplingRate,
5563cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                   format,
5573cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                   channelMask,
5583cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                   (audio_output_flags_t)flags);
5593cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    if (profile != NULL) {
560f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
5616a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("getOutput() opening direct output device %x", device);
56270c236c9290732782d5267935af1475b8d5ae602Eric Laurent
5633cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(profile);
564f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mDevice = device;
565f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mSamplingRate = samplingRate;
56670c236c9290732782d5267935af1475b8d5ae602Eric Laurent        outputDesc->mFormat = (audio_format_t)format;
56770c236c9290732782d5267935af1475b8d5ae602Eric Laurent        outputDesc->mChannelMask = (audio_channel_mask_t)channelMask;
568f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mLatency = 0;
5693cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        outputDesc->mFlags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);;
570f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mRefCount[stream] = 0;
571f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mStopTime[stream] = 0;
5723cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        output = mpClientInterface->openOutput(profile->mModule->mHandle,
57370c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                        &outputDesc->mDevice,
574f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                        &outputDesc->mSamplingRate,
575f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                        &outputDesc->mFormat,
57670c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                        &outputDesc->mChannelMask,
577f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                        &outputDesc->mLatency,
578f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                        outputDesc->mFlags);
579f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
5809f1f9b509c930830f6f32e9ef6c2c8a03d6fa96eJean-Michel Trivi        // only accept an output with the requested parameters
581f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (output == 0 ||
582f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            (samplingRate != 0 && samplingRate != outputDesc->mSamplingRate) ||
583f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            (format != 0 && format != outputDesc->mFormat) ||
58470c236c9290732782d5267935af1475b8d5ae602Eric Laurent            (channelMask != 0 && channelMask != outputDesc->mChannelMask)) {
5853cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
5863cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    "format %d %d, channelMask %04x %04x", output, samplingRate,
5873cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
5883cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    outputDesc->mChannelMask);
589f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (output != 0) {
590f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mpClientInterface->closeOutput(output);
591f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
592f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            delete outputDesc;
593f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return 0;
594f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
595f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        addOutput(output, outputDesc);
5963cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        ALOGV("getOutput() returns direct output %d", output);
597f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return output;
598f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
599f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
6009f1f9b509c930830f6f32e9ef6c2c8a03d6fa96eJean-Michel Trivi    // ignoring channel mask due to downmix capability in mixer
6019f1f9b509c930830f6f32e9ef6c2c8a03d6fa96eJean-Michel Trivi
602f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // open a non direct output
603f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
604f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // get which output is suitable for the specified stream. The actual routing change will happen
605f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // when startOutput() will be called
606c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
607f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
6085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    output = selectOutput(outputs, flags);
609f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
61070c236c9290732782d5267935af1475b8d5ae602Eric Laurent    ALOGW_IF((output ==0), "getOutput() could not find output for stream %d, samplingRate %d,"
61170c236c9290732782d5267935af1475b8d5ae602Eric Laurent            "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
612f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
6135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGV("getOutput() returns output %d", output);
6145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
615f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return output;
616f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
617f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
6185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentaudio_io_handle_t AudioPolicyManagerBase::selectOutput(const SortedVector<audio_io_handle_t>& outputs,
6195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                       AudioSystem::output_flags flags)
6205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
6215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // select one output among several that provide a path to a particular device or set of
6225ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // devices (the list was previously build by getOutputsForDevice()).
6235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // The priority is as follows:
6245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // 1: the output with the highest number of requested policy flags
6255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // 2: the primary output
6265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // 3: the first output in the list
6275ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
6285ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (outputs.size() == 0) {
6295ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return 0;
6305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
6315ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (outputs.size() == 1) {
6325ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return outputs[0];
6335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
6345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
6355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    int maxCommonFlags = 0;
6365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    audio_io_handle_t outputFlags = 0;
6375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    audio_io_handle_t outputPrimary = 0;
6385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
6395ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < outputs.size(); i++) {
6405ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        AudioOutputDescriptor *outputDesc = mOutputs.valueFor(outputs[i]);
6415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (!outputDesc->isDuplicated()) {
6425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            int commonFlags = (int)AudioSystem::popCount(outputDesc->mProfile->mFlags & flags);
6435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            if (commonFlags > maxCommonFlags) {
6445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                outputFlags = outputs[i];
6455ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                maxCommonFlags = commonFlags;
6465ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags);
6475ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
6480977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent            if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
6495ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                outputPrimary = outputs[i];
6505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
6515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
6525ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
6535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
6545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (outputFlags != 0) {
6555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return outputFlags;
6565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
6575ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (outputPrimary != 0) {
6585ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return outputPrimary;
6595ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
6605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
6615ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return outputs[0];
6625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
6635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
664f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::startOutput(audio_io_handle_t output,
665f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                             AudioSystem::stream_type stream,
666f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                             int session)
667f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
6686a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("startOutput() output %d, stream %d, session %d", output, stream, session);
669f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mOutputs.indexOfKey(output);
670f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
67164cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("startOutput() unknow output %d", output);
672f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
673f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
674f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
675f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index);
676f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
6775ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // increment usage count for this stream on the requested output:
678f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // NOTE that the usage count is the same for duplicated output and hardware output which is
6795ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
680f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    outputDesc->changeRefCount(stream, 1);
681f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
6825ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (outputDesc->mRefCount[stream] == 1) {
6835ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_devices_t newDevice = getNewDevice(output, false /*fromCache*/);
684b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        routing_strategy strategy = getStrategy(stream);
685b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
686b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                            (strategy == STRATEGY_SONIFICATION_RESPECTFUL);
687b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        uint32_t waitMs = 0;
6885ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        bool force = false;
6895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        for (size_t i = 0; i < mOutputs.size(); i++) {
6905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            AudioOutputDescriptor *desc = mOutputs.valueAt(i);
691b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent            if (desc != outputDesc) {
692b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                // force a device change if any other output is managed by the same hw
693b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                // module and has a current device selection that differs from selected device.
694b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                // In this case, the audio HAL must receive the new device selection so that it can
695b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                // change the device currently selected by the other active output.
696b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                if (outputDesc->sharesHwModuleWith(desc) &&
6975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                    desc->device() != newDevice) {
698b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                    force = true;
699b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                }
700b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                // wait for audio on other active outputs to be presented when starting
701b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                // a notification so that audio focus effect can propagate.
702b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                if (shouldWait && (desc->refCount() != 0) && (waitMs < desc->latency())) {
703b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                    waitMs = desc->latency();
704b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                }
7055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
7065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
707b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        uint32_t muteWaitMs = setOutputDevice(output, newDevice, force);
708f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
7095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // handle special case for sonification while in call
7105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (isInCall()) {
7115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            handleIncallSonification(stream, true, false);
7125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
713c16ac09f510437e8340be691720177a490ae78f0Eric Laurent
7145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // apply volume rules for current stream and device if necessary
7155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        checkAndSetVolume(stream,
716c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                          mStreams[stream].getVolumeIndex(newDevice),
7175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                          output,
7185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                          newDevice);
71912bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi
7205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // update the outputs if starting an output with a stream that can affect notification
7215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // routing
7225ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        handleNotificationRoutingForStream(stream);
723b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        if (waitMs > muteWaitMs) {
724b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent            usleep((waitMs - muteWaitMs) * 2 * 1000);
725b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        }
7265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
727f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
728f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
729f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
7305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
731f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::stopOutput(audio_io_handle_t output,
732f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            AudioSystem::stream_type stream,
733f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            int session)
734f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
7356a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
736f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mOutputs.indexOfKey(output);
737f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
73864cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("stopOutput() unknow output %d", output);
739f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
740f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
741f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
742f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index);
743f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
744f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // handle special case for sonification while in call
745f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (isInCall()) {
746f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        handleIncallSonification(stream, false, false);
747f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
748f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
749f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (outputDesc->mRefCount[stream] > 0) {
750f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // decrement usage count of this stream on the output
751f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->changeRefCount(stream, -1);
752f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // store time at which the stream was stopped - see isStreamActive()
7535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (outputDesc->mRefCount[stream] == 0) {
7545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            outputDesc->mStopTime[stream] = systemTime();
7555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            audio_devices_t newDevice = getNewDevice(output, false /*fromCache*/);
7565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // delay the device switch by twice the latency because stopOutput() is executed when
7575ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // the track stop() command is received and at that time the audio track buffer can
7585ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // still contain data that needs to be drained. The latency only covers the audio HAL
7595ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // and kernel buffers. Also the latency does not always include additional delay in the
7605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // audio path (audio DSP, CODEC ...)
7615ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            setOutputDevice(output, newDevice, false, outputDesc->mLatency*2);
7625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
7635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // force restoring the device selection on other active outputs if it differs from the
7645ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // one being selected for this output
7655ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            for (size_t i = 0; i < mOutputs.size(); i++) {
7665ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                audio_io_handle_t curOutput = mOutputs.keyAt(i);
7675ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                AudioOutputDescriptor *desc = mOutputs.valueAt(i);
7685ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                if (curOutput != output &&
7695ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                        desc->refCount() != 0 &&
7705ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                        outputDesc->sharesHwModuleWith(desc) &&
7715ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                        newDevice != desc->device()) {
7725ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                    setOutputDevice(curOutput,
7735ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                    getNewDevice(curOutput, false /*fromCache*/),
7745ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                    true,
7755ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                    outputDesc->mLatency*2);
7765ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                }
7775ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
7785ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // update the outputs if stopping one with a stream that can affect notification routing
7795ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            handleNotificationRoutingForStream(stream);
780f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
781f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return NO_ERROR;
782f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
78364cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("stopOutput() refcount is already 0 for output %d", output);
784f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return INVALID_OPERATION;
785f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
786f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
787f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
788f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::releaseOutput(audio_io_handle_t output)
789f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
7906a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("releaseOutput() %d", output);
791f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mOutputs.indexOfKey(output);
792f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
79364cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("releaseOutput() releasing unknown output %d", output);
794f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
795f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
796f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
797f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
798f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    int testIndex = testOutputIndex(output);
799f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (testIndex != 0) {
800f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index);
801f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (outputDesc->refCount() == 0) {
802f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mpClientInterface->closeOutput(output);
803f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            delete mOutputs.valueAt(index);
804f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mOutputs.removeItem(output);
805f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mTestOutputs[testIndex] = 0;
806f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
807f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
808f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
809f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
810f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
811f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mOutputs.valueAt(index)->mFlags & AudioSystem::OUTPUT_FLAG_DIRECT) {
812f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mpClientInterface->closeOutput(output);
813f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        delete mOutputs.valueAt(index);
814f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mOutputs.removeItem(output);
815be13d2ebd0ee66e8c09d978107c14a4221b04dffEric Laurent        mPreviousOutputs = mOutputs;
816f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
81712bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi
818f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
819f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
820f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinaudio_io_handle_t AudioPolicyManagerBase::getInput(int inputSource,
821f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    uint32_t samplingRate,
822f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    uint32_t format,
82370c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                    uint32_t channelMask,
824f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    AudioSystem::audio_in_acoustics acoustics)
825f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
826f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    audio_io_handle_t input = 0;
827f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent    audio_devices_t device = getDeviceForInputSource(inputSource);
828f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
82970c236c9290732782d5267935af1475b8d5ae602Eric Laurent    ALOGV("getInput() inputSource %d, samplingRate %d, format %d, channelMask %x, acoustics %x",
83070c236c9290732782d5267935af1475b8d5ae602Eric Laurent          inputSource, samplingRate, format, channelMask, acoustics);
831f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
832ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if (device == AUDIO_DEVICE_NONE) {
8335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        ALOGW("getInput() could not find device for inputSource %d", inputSource);
834f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return 0;
835f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
836f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
837f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // adapt channel selection to input source
838f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    switch(inputSource) {
839f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_UPLINK:
84070c236c9290732782d5267935af1475b8d5ae602Eric Laurent        channelMask = AudioSystem::CHANNEL_IN_VOICE_UPLINK;
841f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
842f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_DOWNLINK:
84370c236c9290732782d5267935af1475b8d5ae602Eric Laurent        channelMask = AudioSystem::CHANNEL_IN_VOICE_DNLINK;
844f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
845f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_CALL:
84670c236c9290732782d5267935af1475b8d5ae602Eric Laurent        channelMask = (AudioSystem::CHANNEL_IN_VOICE_UPLINK | AudioSystem::CHANNEL_IN_VOICE_DNLINK);
847f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
848f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    default:
849f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
850f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
851f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
8525ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    IOProfile *profile = getInputProfile(device,
8535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                         samplingRate,
8545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                         format,
85570c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                         channelMask);
8565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (profile == NULL) {
8575ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        ALOGW("getInput() could not find profile for device %04x, samplingRate %d, format %d,"
85870c236c9290732782d5267935af1475b8d5ae602Eric Laurent                "channelMask %04x",
85970c236c9290732782d5267935af1475b8d5ae602Eric Laurent                device, samplingRate, format, channelMask);
86070c236c9290732782d5267935af1475b8d5ae602Eric Laurent        return 0;
86170c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
86270c236c9290732782d5267935af1475b8d5ae602Eric Laurent
86370c236c9290732782d5267935af1475b8d5ae602Eric Laurent    if (profile->mModule->mHandle == 0) {
8643cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        ALOGE("getInput(): HW module %s not opened", profile->mModule->mName);
8655ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return 0;
8665ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
8675ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
8685ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    AudioInputDescriptor *inputDesc = new AudioInputDescriptor(profile);
869f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
870f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    inputDesc->mInputSource = inputSource;
871f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    inputDesc->mDevice = device;
872f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    inputDesc->mSamplingRate = samplingRate;
87370c236c9290732782d5267935af1475b8d5ae602Eric Laurent    inputDesc->mFormat = (audio_format_t)format;
87470c236c9290732782d5267935af1475b8d5ae602Eric Laurent    inputDesc->mChannelMask = (audio_channel_mask_t)channelMask;
875f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    inputDesc->mRefCount = 0;
87670c236c9290732782d5267935af1475b8d5ae602Eric Laurent    input = mpClientInterface->openInput(profile->mModule->mHandle,
87770c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                    &inputDesc->mDevice,
878f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    &inputDesc->mSamplingRate,
879f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    &inputDesc->mFormat,
88070c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                    &inputDesc->mChannelMask);
881f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
882f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // only accept input with the exact requested set of parameters
883f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (input == 0 ||
884f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        (samplingRate != inputDesc->mSamplingRate) ||
885f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        (format != inputDesc->mFormat) ||
88670c236c9290732782d5267935af1475b8d5ae602Eric Laurent        (channelMask != inputDesc->mChannelMask)) {
88770c236c9290732782d5267935af1475b8d5ae602Eric Laurent        ALOGV("getInput() failed opening input: samplingRate %d, format %d, channelMask %d",
88870c236c9290732782d5267935af1475b8d5ae602Eric Laurent                samplingRate, format, channelMask);
889f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (input != 0) {
890f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mpClientInterface->closeInput(input);
891f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
892f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        delete inputDesc;
893f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return 0;
894f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
895f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mInputs.add(input, inputDesc);
896f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return input;
897f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
898f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
899f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::startInput(audio_io_handle_t input)
900f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
9016a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("startInput() input %d", input);
902f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mInputs.indexOfKey(input);
903f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
90464cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("startInput() unknow input %d", input);
905f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
906f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
907f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioInputDescriptor *inputDesc = mInputs.valueAt(index);
908f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
909f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
910f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mTestInput == 0)
911f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
912f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    {
913f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // refuse 2 active AudioRecord clients at the same time
914f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (getActiveInput() != 0) {
91564cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("startInput() input %d failed: other input already started", input);
916f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return INVALID_OPERATION;
917f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
918f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
919f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
920f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioParameter param = AudioParameter();
921f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    param.addInt(String8(AudioParameter::keyRouting), (int)inputDesc->mDevice);
922f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
923f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    param.addInt(String8(AudioParameter::keyInputSource), (int)inputDesc->mInputSource);
9246a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("AudioPolicyManager::startInput() input source = %d", inputDesc->mInputSource);
925f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
926f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mpClientInterface->setParameters(input, param.toString());
927f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
928f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    inputDesc->mRefCount = 1;
929f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
930f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
931f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
932f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::stopInput(audio_io_handle_t input)
933f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
9346a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("stopInput() input %d", input);
935f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mInputs.indexOfKey(input);
936f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
93764cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("stopInput() unknow input %d", input);
938f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
939f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
940f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioInputDescriptor *inputDesc = mInputs.valueAt(index);
941f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
942f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (inputDesc->mRefCount == 0) {
94364cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("stopInput() input %d already stopped", input);
944f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return INVALID_OPERATION;
945f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
946f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioParameter param = AudioParameter();
947f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        param.addInt(String8(AudioParameter::keyRouting), 0);
948f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mpClientInterface->setParameters(input, param.toString());
949f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        inputDesc->mRefCount = 0;
950f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return NO_ERROR;
951f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
952f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
953f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
954f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::releaseInput(audio_io_handle_t input)
955f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
9566a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("releaseInput() %d", input);
957f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mInputs.indexOfKey(input);
958f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
95964cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("releaseInput() releasing unknown input %d", input);
960f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
961f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
962f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mpClientInterface->closeInput(input);
963f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    delete mInputs.valueAt(index);
964f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mInputs.removeItem(input);
9656a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("releaseInput() exit");
966f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
967f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
968f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::initStreamVolume(AudioSystem::stream_type stream,
969f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            int indexMin,
970f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            int indexMax)
971f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
9726a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
973f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (indexMin < 0 || indexMin >= indexMax) {
97464cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("initStreamVolume() invalid index limits for stream %d, min %d, max %d", stream , indexMin, indexMax);
975f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
976f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
977f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mStreams[stream].mIndexMin = indexMin;
978f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mStreams[stream].mIndexMax = indexMax;
979f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
980f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
981c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentstatus_t AudioPolicyManagerBase::setStreamVolumeIndex(AudioSystem::stream_type stream,
982c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                      int index,
983c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                      audio_devices_t device)
984f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
985f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
986f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if ((index < mStreams[stream].mIndexMin) || (index > mStreams[stream].mIndexMax)) {
987f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
988f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
989c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    if (!audio_is_output_device(device)) {
990c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        return BAD_VALUE;
991c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    }
992f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
993f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // Force max volume if stream cannot be muted
994f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (!mStreams[stream].mCanBeMuted) index = mStreams[stream].mIndexMax;
995f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
996b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    ALOGV("setStreamVolumeIndex() stream %d, device %04x, index %d",
997c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent          stream, device, index);
998c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
999c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    // if device is AUDIO_DEVICE_OUT_DEFAULT set default value and
1000c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    // clear all device specific values
1001c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    if (device == AUDIO_DEVICE_OUT_DEFAULT) {
1002c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        mStreams[stream].mIndexCur.clear();
1003c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    }
1004c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    mStreams[stream].mIndexCur.add(device, index);
1005f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1006f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // compute and apply stream volume on all outputs according to connected device
1007f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    status_t status = NO_ERROR;
1008f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mOutputs.size(); i++) {
1009c5eb8b4a5d4395ce335bc7c3e6df2678fa47e2ddEric Laurent        audio_devices_t curDevice =
1010c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                getDeviceForVolume(mOutputs.valueAt(i)->device());
1011c5eb8b4a5d4395ce335bc7c3e6df2678fa47e2ddEric Laurent        if (device == curDevice) {
1012c5eb8b4a5d4395ce335bc7c3e6df2678fa47e2ddEric Laurent            status_t volStatus = checkAndSetVolume(stream, index, mOutputs.keyAt(i), curDevice);
1013c5eb8b4a5d4395ce335bc7c3e6df2678fa47e2ddEric Laurent            if (volStatus != NO_ERROR) {
1014c5eb8b4a5d4395ce335bc7c3e6df2678fa47e2ddEric Laurent                status = volStatus;
1015c5eb8b4a5d4395ce335bc7c3e6df2678fa47e2ddEric Laurent            }
1016f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1017f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1018f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return status;
1019f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1020f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1021c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentstatus_t AudioPolicyManagerBase::getStreamVolumeIndex(AudioSystem::stream_type stream,
1022c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                      int *index,
1023c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                      audio_devices_t device)
1024f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1025c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    if (index == NULL) {
1026f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
1027f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1028c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    if (!audio_is_output_device(device)) {
1029c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        return BAD_VALUE;
1030c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    }
1031c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    // if device is AUDIO_DEVICE_OUT_DEFAULT, return volume for device corresponding to
1032c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    // the strategy the stream belongs to.
1033c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    if (device == AUDIO_DEVICE_OUT_DEFAULT) {
1034c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
1035c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    }
1036c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    device = getDeviceForVolume(device);
1037c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
1038c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    *index =  mStreams[stream].getVolumeIndex(device);
1039c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index);
1040f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
1041f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1042f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1043c94dccc97cc3ed5171b45f46a0f7f8762d37156fGlenn Kastenaudio_io_handle_t AudioPolicyManagerBase::getOutputForEffect(const effect_descriptor_t *desc)
1044f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
10456a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("getOutputForEffect()");
1046f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // apply simple rule where global effects are attached to the same output as MUSIC streams
10474660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen
10484660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen    routing_strategy strategy = getStrategy(AudioSystem::MUSIC);
10494660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen    audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
1050c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs);
10514660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen    int outIdx = 0;
10524660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen    for (size_t i = 0; i < dstOutputs.size(); i++) {
10534660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen        AudioOutputDescriptor *desc = mOutputs.valueFor(dstOutputs[i]);
10544660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen        if (desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
10554660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen            outIdx = i;
10564660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen        }
10574660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen    }
10584660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen    return dstOutputs[outIdx];
1059f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1060f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1061c94dccc97cc3ed5171b45f46a0f7f8762d37156fGlenn Kastenstatus_t AudioPolicyManagerBase::registerEffect(const effect_descriptor_t *desc,
10621c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent                                audio_io_handle_t io,
1063f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                uint32_t strategy,
1064f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                int session,
1065f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                int id)
1066f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
10671c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent    ssize_t index = mOutputs.indexOfKey(io);
1068f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
10691c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent        index = mInputs.indexOfKey(io);
10701c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent        if (index < 0) {
107164cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("registerEffect() unknown io %d", io);
10721c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent            return INVALID_OPERATION;
10731c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent        }
1074f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1075f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1076f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mTotalEffectsMemory + desc->memoryUsage > getMaxEffectsMemory()) {
107764cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("registerEffect() memory limit exceeded for Fx %s, Memory %d KB",
1078f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                desc->name, desc->memoryUsage);
1079f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return INVALID_OPERATION;
1080f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1081f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mTotalEffectsMemory += desc->memoryUsage;
10826a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("registerEffect() effect %s, io %d, strategy %d session %d id %d",
10831c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent            desc->name, io, strategy, session, id);
10846a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("registerEffect() memory %d, total memory %d", desc->memoryUsage, mTotalEffectsMemory);
1085f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1086f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    EffectDescriptor *pDesc = new EffectDescriptor();
1087f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    memcpy (&pDesc->mDesc, desc, sizeof(effect_descriptor_t));
10881c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent    pDesc->mIo = io;
1089f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    pDesc->mStrategy = (routing_strategy)strategy;
1090f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    pDesc->mSession = session;
1091582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    pDesc->mEnabled = false;
1092582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent
1093f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mEffects.add(id, pDesc);
1094f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1095f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
1096f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1097f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1098f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::unregisterEffect(int id)
1099f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1100f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mEffects.indexOfKey(id);
1101f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
110264cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("unregisterEffect() unknown effect ID %d", id);
1103f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return INVALID_OPERATION;
1104f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1105f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1106f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    EffectDescriptor *pDesc = mEffects.valueAt(index);
1107f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1108582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    setEffectEnabled(pDesc, false);
1109582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent
1110f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mTotalEffectsMemory < pDesc->mDesc.memoryUsage) {
111164cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("unregisterEffect() memory %d too big for total %d",
1112f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                pDesc->mDesc.memoryUsage, mTotalEffectsMemory);
1113f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        pDesc->mDesc.memoryUsage = mTotalEffectsMemory;
1114f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1115f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mTotalEffectsMemory -= pDesc->mDesc.memoryUsage;
11166a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("unregisterEffect() effect %s, ID %d, memory %d total memory %d",
1117582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent            pDesc->mDesc.name, id, pDesc->mDesc.memoryUsage, mTotalEffectsMemory);
1118f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1119f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mEffects.removeItem(id);
1120f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    delete pDesc;
1121f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1122f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
1123f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1124f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1125582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurentstatus_t AudioPolicyManagerBase::setEffectEnabled(int id, bool enabled)
1126582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent{
1127582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    ssize_t index = mEffects.indexOfKey(id);
1128582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    if (index < 0) {
112964cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("unregisterEffect() unknown effect ID %d", id);
1130582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        return INVALID_OPERATION;
1131582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    }
1132582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent
1133582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    return setEffectEnabled(mEffects.valueAt(index), enabled);
1134582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent}
1135582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent
1136582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurentstatus_t AudioPolicyManagerBase::setEffectEnabled(EffectDescriptor *pDesc, bool enabled)
1137582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent{
1138582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    if (enabled == pDesc->mEnabled) {
11396a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("setEffectEnabled(%s) effect already %s",
1140582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent             enabled?"true":"false", enabled?"enabled":"disabled");
1141582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        return INVALID_OPERATION;
1142582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    }
1143582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent
1144582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    if (enabled) {
1145582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        if (mTotalEffectsCpuLoad + pDesc->mDesc.cpuLoad > getMaxEffectsCpuLoad()) {
114664cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("setEffectEnabled(true) CPU Load limit exceeded for Fx %s, CPU %f MIPS",
1147582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent                 pDesc->mDesc.name, (float)pDesc->mDesc.cpuLoad/10);
1148582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent            return INVALID_OPERATION;
1149582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        }
1150582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        mTotalEffectsCpuLoad += pDesc->mDesc.cpuLoad;
11516a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("setEffectEnabled(true) total CPU %d", mTotalEffectsCpuLoad);
1152582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    } else {
1153582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        if (mTotalEffectsCpuLoad < pDesc->mDesc.cpuLoad) {
115464cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("setEffectEnabled(false) CPU load %d too high for total %d",
1155582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent                    pDesc->mDesc.cpuLoad, mTotalEffectsCpuLoad);
1156582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent            pDesc->mDesc.cpuLoad = mTotalEffectsCpuLoad;
1157582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        }
1158582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        mTotalEffectsCpuLoad -= pDesc->mDesc.cpuLoad;
11596a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("setEffectEnabled(false) total CPU %d", mTotalEffectsCpuLoad);
1160582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    }
1161582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    pDesc->mEnabled = enabled;
1162582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    return NO_ERROR;
1163582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent}
1164582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent
1165f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinbool AudioPolicyManagerBase::isStreamActive(int stream, uint32_t inPastMs) const
1166f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1167f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    nsecs_t sysTime = systemTime();
1168f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mOutputs.size(); i++) {
1169f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (mOutputs.valueAt(i)->mRefCount[stream] != 0 ||
1170f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            ns2ms(sysTime - mOutputs.valueAt(i)->mStopTime[stream]) < inPastMs) {
1171f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return true;
1172f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1173f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1174f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return false;
1175f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1176f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1177abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivibool AudioPolicyManagerBase::isSourceActive(audio_source_t source) const
1178abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi{
1179abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi    for (size_t i = 0; i < mInputs.size(); i++) {
1180abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi        const AudioInputDescriptor * inputDescriptor = mInputs.valueAt(i);
1181abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi        if ((inputDescriptor->mInputSource == (int) source)
1182abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi                && (inputDescriptor->mRefCount > 0)) {
1183abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi            return true;
1184abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi        }
1185abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi    }
1186abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi    return false;
1187abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi}
1188abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi
1189abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi
1190f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1191f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::dump(int fd)
1192f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1193f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    const size_t SIZE = 256;
1194f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    char buffer[SIZE];
1195f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    String8 result;
1196f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1197f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
1198f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1199b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
120070c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, " Primary Output: %d\n", mPrimaryOutput);
1201f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1202f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " A2DP device address: %s\n", mA2dpDeviceAddress.string());
1203f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1204f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " SCO device address: %s\n", mScoDeviceAddress.string());
1205f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1206599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    snprintf(buffer, SIZE, " USB audio ALSA %s\n", mUsbCardAndDevice.string());
1207599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    result.append(buffer);
1208f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Output devices: %08x\n", mAvailableOutputDevices);
1209f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1210f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Input devices: %08x\n", mAvailableInputDevices);
1211f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1212f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Phone state: %d\n", mPhoneState);
1213f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1214f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Force use for communications %d\n", mForceUse[AudioSystem::FOR_COMMUNICATION]);
1215f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1216f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Force use for media %d\n", mForceUse[AudioSystem::FOR_MEDIA]);
1217f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1218f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Force use for record %d\n", mForceUse[AudioSystem::FOR_RECORD]);
1219f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1220f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Force use for dock %d\n", mForceUse[AudioSystem::FOR_DOCK]);
1221f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1222738207def5f691d605ae33d041116829a74513a9Eric Laurent    snprintf(buffer, SIZE, " Force use for system %d\n", mForceUse[AudioSystem::FOR_SYSTEM]);
1223738207def5f691d605ae33d041116829a74513a9Eric Laurent    result.append(buffer);
1224f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, result.string(), result.size());
1225f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
12265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
122770c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "\nHW Modules dump:\n");
12285ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    write(fd, buffer, strlen(buffer));
122970c236c9290732782d5267935af1475b8d5ae602Eric Laurent    for (size_t i = 0; i < mHwModules.size(); i++) {
123070c236c9290732782d5267935af1475b8d5ae602Eric Laurent        snprintf(buffer, SIZE, "- HW Module %d:\n", i + 1);
12315ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        write(fd, buffer, strlen(buffer));
123270c236c9290732782d5267935af1475b8d5ae602Eric Laurent        mHwModules[i]->dump(fd);
12335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
12345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
1235f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "\nOutputs dump:\n");
1236f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
1237f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mOutputs.size(); i++) {
1238f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        snprintf(buffer, SIZE, "- Output %d dump:\n", mOutputs.keyAt(i));
1239f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        write(fd, buffer, strlen(buffer));
1240f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mOutputs.valueAt(i)->dump(fd);
1241f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1242f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1243f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "\nInputs dump:\n");
1244f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
1245f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mInputs.size(); i++) {
1246f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        snprintf(buffer, SIZE, "- Input %d dump:\n", mInputs.keyAt(i));
1247f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        write(fd, buffer, strlen(buffer));
1248f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mInputs.valueAt(i)->dump(fd);
1249f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1250f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1251f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "\nStreams dump:\n");
1252f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
1253c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    snprintf(buffer, SIZE,
1254c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent             " Stream  Can be muted  Index Min  Index Max  Index Cur [device : index]...\n");
1255f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
1256f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) {
1257c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        snprintf(buffer, SIZE, " %02d      ", i);
1258f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        write(fd, buffer, strlen(buffer));
1259c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        mStreams[i].dump(fd);
1260f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1261f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1262f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "\nTotal Effects CPU: %f MIPS, Total Effects memory: %d KB\n",
1263f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            (float)mTotalEffectsCpuLoad/10, mTotalEffectsMemory);
1264f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
1265f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1266f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "Registered effects:\n");
1267f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
1268f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mEffects.size(); i++) {
1269f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        snprintf(buffer, SIZE, "- Effect %d dump:\n", mEffects.keyAt(i));
1270f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        write(fd, buffer, strlen(buffer));
1271f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mEffects.valueAt(i)->dump(fd);
1272f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1273f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1274f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1275f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
1276f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1277f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1278f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// ----------------------------------------------------------------------------
1279f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// AudioPolicyManagerBase
1280f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// ----------------------------------------------------------------------------
1281f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1282f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima ZavinAudioPolicyManagerBase::AudioPolicyManagerBase(AudioPolicyClientInterface *clientInterface)
1283f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    :
1284f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
1285f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    Thread(false),
1286f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
128770c236c9290732782d5267935af1475b8d5ae602Eric Laurent    mPrimaryOutput((audio_io_handle_t)0),
1288ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    mAvailableOutputDevices(AUDIO_DEVICE_NONE),
1289ca0657a1ca087a6d474a75fcfedd6aac3901d587Glenn Kasten    mPhoneState(AudioSystem::MODE_NORMAL),
1290f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
1291f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mTotalEffectsCpuLoad(0), mTotalEffectsMemory(0),
129248387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi    mA2dpSuspended(false), mHasA2dp(false), mHasUsb(false), mHasRemoteSubmix(false)
1293f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1294f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mpClientInterface = clientInterface;
1295f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1296f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < AudioSystem::NUM_FORCE_USE; i++) {
1297f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mForceUse[i] = AudioSystem::FORCE_NONE;
1298f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1299f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1300f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    initializeVolumeCurves();
1301f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1302f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mA2dpDeviceAddress = String8("");
1303f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mScoDeviceAddress = String8("");
1304599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    mUsbCardAndDevice = String8("");
1305f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
13065ec145df7708564d385fd3fb764085321cf4c253Dima Zavin    if (loadAudioPolicyConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE) != NO_ERROR) {
13075ec145df7708564d385fd3fb764085321cf4c253Dima Zavin        if (loadAudioPolicyConfig(AUDIO_POLICY_CONFIG_FILE) != NO_ERROR) {
1308739022f26a7127ba76a98dda65411496086114a7Dima Zavin            ALOGE("could not load audio policy configuration file, setting defaults");
1309739022f26a7127ba76a98dda65411496086114a7Dima Zavin            defaultAudioPolicyConfig();
13105ec145df7708564d385fd3fb764085321cf4c253Dima Zavin        }
13115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
13125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
1313b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    // open all output streams needed to access attached devices
131470c236c9290732782d5267935af1475b8d5ae602Eric Laurent    for (size_t i = 0; i < mHwModules.size(); i++) {
131570c236c9290732782d5267935af1475b8d5ae602Eric Laurent        mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->mName);
131670c236c9290732782d5267935af1475b8d5ae602Eric Laurent        if (mHwModules[i]->mHandle == 0) {
131770c236c9290732782d5267935af1475b8d5ae602Eric Laurent            ALOGW("could not open HW module %s", mHwModules[i]->mName);
131870c236c9290732782d5267935af1475b8d5ae602Eric Laurent            continue;
131970c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
132070c236c9290732782d5267935af1475b8d5ae602Eric Laurent        // open all output streams needed to access attached devices
132170c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
132270c236c9290732782d5267935af1475b8d5ae602Eric Laurent        {
132370c236c9290732782d5267935af1475b8d5ae602Eric Laurent            const IOProfile *outProfile = mHwModules[i]->mOutputProfiles[j];
132470c236c9290732782d5267935af1475b8d5ae602Eric Laurent
132570c236c9290732782d5267935af1475b8d5ae602Eric Laurent            if (outProfile->mSupportedDevices & mAttachedOutputDevices) {
132670c236c9290732782d5267935af1475b8d5ae602Eric Laurent                AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(outProfile);
132770c236c9290732782d5267935af1475b8d5ae602Eric Laurent                outputDesc->mDevice = (audio_devices_t)(mDefaultOutputDevice &
132870c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                            outProfile->mSupportedDevices);
132970c236c9290732782d5267935af1475b8d5ae602Eric Laurent                audio_io_handle_t output = mpClientInterface->openOutput(
133070c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                outProfile->mModule->mHandle,
133170c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mDevice,
133270c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mSamplingRate,
133370c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mFormat,
133470c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mChannelMask,
133570c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mLatency,
133670c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                outputDesc->mFlags);
133770c236c9290732782d5267935af1475b8d5ae602Eric Laurent                if (output == 0) {
133870c236c9290732782d5267935af1475b8d5ae602Eric Laurent                    delete outputDesc;
133970c236c9290732782d5267935af1475b8d5ae602Eric Laurent                } else {
134070c236c9290732782d5267935af1475b8d5ae602Eric Laurent                    mAvailableOutputDevices = (audio_devices_t)(mAvailableOutputDevices |
134170c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                            (outProfile->mSupportedDevices & mAttachedOutputDevices));
134270c236c9290732782d5267935af1475b8d5ae602Eric Laurent                    if (mPrimaryOutput == 0 &&
13430977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent                            outProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
134470c236c9290732782d5267935af1475b8d5ae602Eric Laurent                        mPrimaryOutput = output;
134570c236c9290732782d5267935af1475b8d5ae602Eric Laurent                    }
134670c236c9290732782d5267935af1475b8d5ae602Eric Laurent                    addOutput(output, outputDesc);
134770c236c9290732782d5267935af1475b8d5ae602Eric Laurent                    setOutputDevice(output,
134870c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                    (audio_devices_t)(mDefaultOutputDevice &
134970c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                        outProfile->mSupportedDevices),
135070c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                    true);
1351b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                }
1352b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            }
1353b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        }
1354f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1355f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
13565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGE_IF((mAttachedOutputDevices & ~mAvailableOutputDevices),
1357b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent             "Not output found for attached devices %08x",
13585ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent             (mAttachedOutputDevices & ~mAvailableOutputDevices));
1359b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1360b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output");
1361b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1362c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    updateDevicesAndOutputs();
13633cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
1364f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
1365b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (mPrimaryOutput != 0) {
1366f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioParameter outputCmd = AudioParameter();
1367f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputCmd.addInt(String8("set_id"), 0);
1368b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        mpClientInterface->setParameters(mPrimaryOutput, outputCmd.toString());
1369f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1370c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        mTestDevice = AUDIO_DEVICE_OUT_SPEAKER;
1371f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mTestSamplingRate = 44100;
1372f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mTestFormat = AudioSystem::PCM_16_BIT;
1373f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mTestChannels =  AudioSystem::CHANNEL_OUT_STEREO;
1374f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mTestLatencyMs = 0;
1375f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mCurOutput = 0;
1376f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mDirectOutput = false;
1377f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
1378f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mTestOutputs[i] = 0;
1379f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1380f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1381f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        const size_t SIZE = 256;
1382f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        char buffer[SIZE];
1383f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        snprintf(buffer, SIZE, "AudioPolicyManagerTest");
1384f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        run(buffer, ANDROID_PRIORITY_AUDIO);
1385f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1386f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
1387f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1388f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1389f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima ZavinAudioPolicyManagerBase::~AudioPolicyManagerBase()
1390f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1391f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
1392f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    exit();
1393f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
1394f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin   for (size_t i = 0; i < mOutputs.size(); i++) {
1395f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mpClientInterface->closeOutput(mOutputs.keyAt(i));
1396f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        delete mOutputs.valueAt(i);
1397f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin   }
1398f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin   for (size_t i = 0; i < mInputs.size(); i++) {
1399f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mpClientInterface->closeInput(mInputs.keyAt(i));
1400f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        delete mInputs.valueAt(i);
1401f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin   }
140270c236c9290732782d5267935af1475b8d5ae602Eric Laurent   for (size_t i = 0; i < mHwModules.size(); i++) {
140370c236c9290732782d5267935af1475b8d5ae602Eric Laurent        delete mHwModules[i];
14045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent   }
1405f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1406f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1407f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::initCheck()
1408f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1409b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    return (mPrimaryOutput == 0) ? NO_INIT : NO_ERROR;
1410f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1411f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1412f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
1413f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinbool AudioPolicyManagerBase::threadLoop()
1414f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
14156a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("entering threadLoop()");
1416f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    while (!exitPending())
1417f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    {
1418f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        String8 command;
1419f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        int valueInt;
1420f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        String8 value;
1421f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1422f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        Mutex::Autolock _l(mLock);
1423f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mWaitWorkCV.waitRelative(mLock, milliseconds(50));
1424f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1425f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        command = mpClientInterface->getParameters(0, String8("test_cmd_policy"));
1426f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioParameter param = AudioParameter(command);
1427f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1428f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR &&
1429f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            valueInt != 0) {
14306a70518b93928d1c91457ff805e375c82d76b0e5Steve Block            ALOGV("Test command %s received", command.string());
1431f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            String8 target;
1432f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.get(String8("target"), target) != NO_ERROR) {
1433f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                target = "Manager";
1434f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1435f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) {
1436f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_output"));
1437f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mCurOutput = valueInt;
1438f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1439f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) {
1440f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_direct"));
1441f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (value == "false") {
1442f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    mDirectOutput = false;
1443f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else if (value == "true") {
1444f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    mDirectOutput = true;
1445f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1446f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1447f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) {
1448f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_input"));
1449f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mTestInput = valueInt;
1450f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1451f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1452f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) {
1453f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_format"));
1454f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                int format = AudioSystem::INVALID_FORMAT;
1455f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (value == "PCM 16 bits") {
1456f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    format = AudioSystem::PCM_16_BIT;
1457f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else if (value == "PCM 8 bits") {
1458f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    format = AudioSystem::PCM_8_BIT;
1459f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else if (value == "Compressed MP3") {
1460f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    format = AudioSystem::MP3;
1461f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1462f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (format != AudioSystem::INVALID_FORMAT) {
1463f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    if (target == "Manager") {
1464f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mTestFormat = format;
1465f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    } else if (mTestOutputs[mCurOutput] != 0) {
1466f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        AudioParameter outputParam = AudioParameter();
1467f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        outputParam.addInt(String8("format"), format);
1468f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
1469f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    }
1470f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1471f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1472f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) {
1473f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_channels"));
1474f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                int channels = 0;
1475f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1476f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (value == "Channels Stereo") {
1477f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    channels =  AudioSystem::CHANNEL_OUT_STEREO;
1478f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else if (value == "Channels Mono") {
1479f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    channels =  AudioSystem::CHANNEL_OUT_MONO;
1480f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1481f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (channels != 0) {
1482f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    if (target == "Manager") {
1483f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mTestChannels = channels;
1484f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    } else if (mTestOutputs[mCurOutput] != 0) {
1485f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        AudioParameter outputParam = AudioParameter();
1486f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        outputParam.addInt(String8("channels"), channels);
1487f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
1488f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    }
1489f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1490f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1491f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) {
1492f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_sampleRate"));
1493f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (valueInt >= 0 && valueInt <= 96000) {
1494f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    int samplingRate = valueInt;
1495f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    if (target == "Manager") {
1496f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mTestSamplingRate = samplingRate;
1497f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    } else if (mTestOutputs[mCurOutput] != 0) {
1498f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        AudioParameter outputParam = AudioParameter();
1499f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        outputParam.addInt(String8("sampling_rate"), samplingRate);
1500f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
1501f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    }
1502f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1503f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1504f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1505f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) {
1506f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_reopen"));
1507f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
150870c236c9290732782d5267935af1475b8d5ae602Eric Laurent                AudioOutputDescriptor *outputDesc = mOutputs.valueFor(mPrimaryOutput);
1509b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                mpClientInterface->closeOutput(mPrimaryOutput);
151070c236c9290732782d5267935af1475b8d5ae602Eric Laurent
151170c236c9290732782d5267935af1475b8d5ae602Eric Laurent                audio_module_handle_t moduleHandle = outputDesc->mModule->mHandle;
151270c236c9290732782d5267935af1475b8d5ae602Eric Laurent
1513b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                delete mOutputs.valueFor(mPrimaryOutput);
1514b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                mOutputs.removeItem(mPrimaryOutput);
1515f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1516b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(NULL);
1517c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER;
151870c236c9290732782d5267935af1475b8d5ae602Eric Laurent                mPrimaryOutput = mpClientInterface->openOutput(moduleHandle,
151970c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mDevice,
1520f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                &outputDesc->mSamplingRate,
1521f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                &outputDesc->mFormat,
152270c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mChannelMask,
1523f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                &outputDesc->mLatency,
1524f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                outputDesc->mFlags);
1525b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                if (mPrimaryOutput == 0) {
15265efbd421e0029d6fc44b1cc65c0e5e0d85e5161fSteve Block                    ALOGE("Failed to reopen hardware output stream, samplingRate: %d, format %d, channels %d",
152770c236c9290732782d5267935af1475b8d5ae602Eric Laurent                            outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask);
1528f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else {
1529f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    AudioParameter outputCmd = AudioParameter();
1530f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    outputCmd.addInt(String8("set_id"), 0);
1531b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                    mpClientInterface->setParameters(mPrimaryOutput, outputCmd.toString());
1532b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                    addOutput(mPrimaryOutput, outputDesc);
1533f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1534f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1535f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1536f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1537f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mpClientInterface->setParameters(0, String8("test_cmd_policy="));
1538f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1539f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1540f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return false;
1541f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1542f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1543f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::exit()
1544f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1545f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    {
1546f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AutoMutex _l(mLock);
1547f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        requestExit();
1548f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mWaitWorkCV.signal();
1549f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1550f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    requestExitAndWait();
1551f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1552f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1553f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinint AudioPolicyManagerBase::testOutputIndex(audio_io_handle_t output)
1554f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1555f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
1556f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (output == mTestOutputs[i]) return i;
1557f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1558f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return 0;
1559f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1560f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
1561f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1562f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// ---
1563f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1564f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::addOutput(audio_io_handle_t id, AudioOutputDescriptor *outputDesc)
1565f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1566f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    outputDesc->mId = id;
1567f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mOutputs.add(id, outputDesc);
1568f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1569f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1570f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
15713cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurentstatus_t AudioPolicyManagerBase::checkOutputsForDevice(audio_devices_t device,
15723cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                       AudioSystem::device_connection_state state,
15733cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                       SortedVector<audio_io_handle_t>& outputs)
1574f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
15753cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    AudioOutputDescriptor *desc;
1576b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1577b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (state == AudioSystem::DEVICE_STATE_AVAILABLE) {
15783cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        // first list already open outputs that can be routed to this device
1579b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        for (size_t i = 0; i < mOutputs.size(); i++) {
15803cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            desc = mOutputs.valueAt(i);
15813cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            if (!desc->isDuplicated() && (desc->mProfile->mSupportedDevices & device)) {
15823cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i));
15833cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                outputs.add(mOutputs.keyAt(i));
1584b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            }
1585b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        }
15863cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        // then look for output profiles that can be routed to this device
15873cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        SortedVector<IOProfile *> profiles;
158870c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (size_t i = 0; i < mHwModules.size(); i++)
1589b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        {
159070c236c9290732782d5267935af1475b8d5ae602Eric Laurent            if (mHwModules[i]->mHandle == 0) {
159170c236c9290732782d5267935af1475b8d5ae602Eric Laurent                continue;
159270c236c9290732782d5267935af1475b8d5ae602Eric Laurent            }
159370c236c9290732782d5267935af1475b8d5ae602Eric Laurent            for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
159470c236c9290732782d5267935af1475b8d5ae602Eric Laurent            {
159570c236c9290732782d5267935af1475b8d5ae602Eric Laurent                if (mHwModules[i]->mOutputProfiles[j]->mSupportedDevices & device) {
15963cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    ALOGV("checkOutputsForDevice(): adding profile %d from module %d", j, i);
15973cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    profiles.add(mHwModules[i]->mOutputProfiles[j]);
159870c236c9290732782d5267935af1475b8d5ae602Eric Laurent                }
159970c236c9290732782d5267935af1475b8d5ae602Eric Laurent            }
1600b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        }
160170c236c9290732782d5267935af1475b8d5ae602Eric Laurent
16023cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        if (profiles.isEmpty() && outputs.isEmpty()) {
16033cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
16043cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            return BAD_VALUE;
160570c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
160670c236c9290732782d5267935af1475b8d5ae602Eric Laurent
16073cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        // open outputs for matching profiles if needed. Direct outputs are also opened to
16083cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        // query for dynamic parameters and will be closed later by setDeviceConnectionState()
16093cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
16103cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            IOProfile *profile = profiles[profile_index];
1611b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
16123cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            // nothing to do if one output is already opened for this profile
16133cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            size_t j;
16143cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            for (j = 0; j < outputs.size(); j++) {
16153cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                desc = mOutputs.valueAt(j);
16163cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                if (!desc->isDuplicated() && desc->mProfile == profile) {
16173cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    break;
16183cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                }
16193cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
16203cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            if (j != outputs.size()) {
16213cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                continue;
16223cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
16233cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
16243cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            ALOGV("opening output for device %08x", device);
16253cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            desc = new AudioOutputDescriptor(profile);
16263cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            desc->mDevice = device;
16273cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            audio_io_handle_t output = mpClientInterface->openOutput(profile->mModule->mHandle,
16283cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                       &desc->mDevice,
16293cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                       &desc->mSamplingRate,
16303cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                       &desc->mFormat,
16313cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                       &desc->mChannelMask,
16323cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                       &desc->mLatency,
16333cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                       desc->mFlags);
16343cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            if (output != 0) {
16353cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
16363cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    String8 reply;
16373cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    char *value;
16383cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    if (profile->mSamplingRates[0] == 0) {
16393cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        reply = mpClientInterface->getParameters(output,
16403cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES));
16413cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        ALOGV("checkOutputsForDevice() direct output sup sampling rates %s",
16423cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                  reply.string());
16433cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        value = strpbrk((char *)reply.string(), "=");
16443cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        if (value != NULL) {
16453cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                            loadSamplingRates(value, profile);
16463cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        }
16473cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
16483cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    if (profile->mFormats[0] == 0) {
16493cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        reply = mpClientInterface->getParameters(output,
16503cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                       String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
16513cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        ALOGV("checkOutputsForDevice() direct output sup formats %s",
16523cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                  reply.string());
16533cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        value = strpbrk((char *)reply.string(), "=");
16543cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        if (value != NULL) {
16553cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                            loadFormats(value, profile);
16563cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        }
16573cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
16583cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    if (profile->mChannelMasks[0] == 0) {
16593cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        reply = mpClientInterface->getParameters(output,
16603cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                      String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS));
16613cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        ALOGV("checkOutputsForDevice() direct output sup channel masks %s",
16623cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                  reply.string());
16633cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        value = strpbrk((char *)reply.string(), "=");
16643cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        if (value != NULL) {
16653cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                            loadOutChannels(value + 1, profile);
16663cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        }
16673cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
16683cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    if (((profile->mSamplingRates[0] == 0) &&
16693cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                             (profile->mSamplingRates.size() < 2)) ||
16703cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                         ((profile->mFormats[0] == 0) &&
16713cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                             (profile->mFormats.size() < 2)) ||
16723cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                         ((profile->mFormats[0] == 0) &&
16733cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                             (profile->mChannelMasks.size() < 2))) {
16743cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        ALOGW("checkOutputsForDevice() direct output missing param");
16753cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        output = 0;
16763cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    } else {
16773cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        addOutput(output, desc);
16783cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
16793cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                } else {
16803cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    audio_io_handle_t duplicatedOutput = 0;
16813cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    // add output descriptor
16823cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    addOutput(output, desc);
16833cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    // set initial stream volume for device
16844366b4a6735e5da342b56773073f0b41197c777fEric Laurent                    applyStreamVolumes(output, device, 0, true);
16853cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
16863cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    //TODO: configure audio effect output stage here
16873cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
16883cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    // open a duplicating output thread for the new output and the primary output
16893cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    duplicatedOutput = mpClientInterface->openDuplicateOutput(output,
16903cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                              mPrimaryOutput);
16913cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    if (duplicatedOutput != 0) {
16923cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        // add duplicated output descriptor
16933cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        AudioOutputDescriptor *dupOutputDesc = new AudioOutputDescriptor(NULL);
16943cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        dupOutputDesc->mOutput1 = mOutputs.valueFor(mPrimaryOutput);
16953cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        dupOutputDesc->mOutput2 = mOutputs.valueFor(output);
16963cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        dupOutputDesc->mSamplingRate = desc->mSamplingRate;
16973cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        dupOutputDesc->mFormat = desc->mFormat;
16983cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        dupOutputDesc->mChannelMask = desc->mChannelMask;
16993cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        dupOutputDesc->mLatency = desc->mLatency;
17003cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        addOutput(duplicatedOutput, dupOutputDesc);
17014366b4a6735e5da342b56773073f0b41197c777fEric Laurent                        applyStreamVolumes(duplicatedOutput, device, 0, true);
17023cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    } else {
17033cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        ALOGW("checkOutputsForDevice() could not open dup output for %d and %d",
17043cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                mPrimaryOutput, output);
17053cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        mpClientInterface->closeOutput(output);
17063cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        mOutputs.removeItem(output);
17073cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        output = 0;
17083cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
17093cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                }
17103cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
17113cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            if (output == 0) {
17123cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                ALOGW("checkOutputsForDevice() could not open output for device %x", device);
17133cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                delete desc;
17143cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                profiles.removeAt(profile_index);
17153cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                profile_index--;
1716b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            } else {
17173cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                outputs.add(output);
17183cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                ALOGV("checkOutputsForDevice(): adding output %d", output);
1719f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
17203cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        }
17213cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
17223cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        if (profiles.isEmpty()) {
17233cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
17243cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            return BAD_VALUE;
1725f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1726f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
1727b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        // check if one opened output is not needed any more after disconnecting one device
1728b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        for (size_t i = 0; i < mOutputs.size(); i++) {
17293cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            desc = mOutputs.valueAt(i);
17303cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            if (!desc->isDuplicated() &&
17313cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    !(desc->mProfile->mSupportedDevices & mAvailableOutputDevices)) {
17323cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                ALOGV("checkOutputsForDevice(): disconnecting adding output %d", mOutputs.keyAt(i));
17333cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                outputs.add(mOutputs.keyAt(i));
17343cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
17353cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        }
17363cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        for (size_t i = 0; i < mHwModules.size(); i++)
17373cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        {
17383cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            if (mHwModules[i]->mHandle == 0) {
17393cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                continue;
17403cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
17413cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
17423cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            {
17433cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                IOProfile *profile = mHwModules[i]->mOutputProfiles[j];
17443cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                if ((profile->mSupportedDevices & device) &&
17453cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        (profile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
17463cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    ALOGV("checkOutputsForDevice(): clearing direct output profile %d on module %d",
17473cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                          j, i);
17483cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    if (profile->mSamplingRates[0] == 0) {
17493cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        profile->mSamplingRates.clear();
17503cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        profile->mSamplingRates.add(0);
17513cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
17523cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    if (profile->mFormats[0] == 0) {
17533cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        profile->mFormats.clear();
17543cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        profile->mFormats.add((audio_format_t)0);
17553cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
17563cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    if (profile->mChannelMasks[0] == 0) {
17573cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        profile->mChannelMasks.clear();
17583cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        profile->mChannelMasks.add((audio_channel_mask_t)0);
17593cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
17603cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                }
1761b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            }
1762f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1763f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
17643cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    return NO_ERROR;
1765f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1766f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1767b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurentvoid AudioPolicyManagerBase::closeOutput(audio_io_handle_t output)
1768f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1769b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    ALOGV("closeOutput(%d)", output);
1770f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1771b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
1772b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (outputDesc == NULL) {
1773b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        ALOGW("closeOutput() unknown output %d", output);
1774b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        return;
1775f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1776f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1777b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    // look for duplicated outputs connected to the output being removed.
1778b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    for (size_t i = 0; i < mOutputs.size(); i++) {
1779b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        AudioOutputDescriptor *dupOutputDesc = mOutputs.valueAt(i);
1780b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        if (dupOutputDesc->isDuplicated() &&
1781b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                (dupOutputDesc->mOutput1 == outputDesc ||
1782b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                dupOutputDesc->mOutput2 == outputDesc)) {
1783b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            AudioOutputDescriptor *outputDesc2;
1784b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            if (dupOutputDesc->mOutput1 == outputDesc) {
1785b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                outputDesc2 = dupOutputDesc->mOutput2;
1786b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            } else {
1787b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                outputDesc2 = dupOutputDesc->mOutput1;
1788b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            }
1789b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            // As all active tracks on duplicated output will be deleted,
1790b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            // and as they were also referenced on the other output, the reference
1791b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            // count for their stream type must be adjusted accordingly on
1792b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            // the other output.
1793b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            for (int j = 0; j < (int)AudioSystem::NUM_STREAM_TYPES; j++) {
1794b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                int refCount = dupOutputDesc->mRefCount[j];
1795b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                outputDesc2->changeRefCount((AudioSystem::stream_type)j,-refCount);
1796b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            }
1797b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i);
1798b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput);
1799f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1800b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            mpClientInterface->closeOutput(duplicatedOutput);
1801b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            delete mOutputs.valueFor(duplicatedOutput);
1802b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            mOutputs.removeItem(duplicatedOutput);
1803f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1804f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1805b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1806b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    AudioParameter param;
1807b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    param.add(String8("closing"), String8("true"));
1808b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    mpClientInterface->setParameters(output, param.toString());
1809b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1810b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    mpClientInterface->closeOutput(output);
1811b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    delete mOutputs.valueFor(output);
1812b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    mOutputs.removeItem(output);
1813f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1814f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1815c952527e6f89d5427881462823514be9d79f13e6Eric LaurentSortedVector<audio_io_handle_t> AudioPolicyManagerBase::getOutputsForDevice(audio_devices_t device,
1816c952527e6f89d5427881462823514be9d79f13e6Eric Laurent                        DefaultKeyedVector<audio_io_handle_t, AudioOutputDescriptor *> openOutputs)
1817f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1818b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    SortedVector<audio_io_handle_t> outputs;
1819f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
18203cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGVV("getOutputsForDevice() device %04x", device);
1821c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    for (size_t i = 0; i < openOutputs.size(); i++) {
18223cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        ALOGVV("output %d isDuplicated=%d device=%04x",
1823c952527e6f89d5427881462823514be9d79f13e6Eric Laurent                i, openOutputs.valueAt(i)->isDuplicated(), openOutputs.valueAt(i)->supportedDevices());
1824c952527e6f89d5427881462823514be9d79f13e6Eric Laurent        if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) {
1825c952527e6f89d5427881462823514be9d79f13e6Eric Laurent            ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i));
1826c952527e6f89d5427881462823514be9d79f13e6Eric Laurent            outputs.add(openOutputs.keyAt(i));
1827f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1828f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1829b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    return outputs;
1830f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1831f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1832b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurentbool AudioPolicyManagerBase::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
1833b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                                   SortedVector<audio_io_handle_t>& outputs2)
1834f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1835b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (outputs1.size() != outputs2.size()) {
1836b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        return false;
1837f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1838b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    for (size_t i = 0; i < outputs1.size(); i++) {
1839b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        if (outputs1[i] != outputs2[i]) {
1840b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            return false;
1841f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1842f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1843b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    return true;
1844b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent}
1845b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1846b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurentvoid AudioPolicyManagerBase::checkOutputForStrategy(routing_strategy strategy)
1847b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent{
184801e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent    audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
184901e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent    audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
1850c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs);
1851c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
1852b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1853b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (!vectorsEqual(srcOutputs,dstOutputs)) {
1854b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d",
1855b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent              strategy, srcOutputs[0], dstOutputs[0]);
18565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // mute strategy while moving tracks from one output to another
18575ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        for (size_t i = 0; i < srcOutputs.size(); i++) {
1858fa3697d716b444bbea6be480801536c44bf69214Eric Laurent            AudioOutputDescriptor *desc = mOutputs.valueFor(srcOutputs[i]);
1859fa3697d716b444bbea6be480801536c44bf69214Eric Laurent            if (desc->strategyRefCount(strategy) != 0) {
1860fa3697d716b444bbea6be480801536c44bf69214Eric Laurent                setStrategyMute(strategy, true, srcOutputs[i]);
1861fa3697d716b444bbea6be480801536c44bf69214Eric Laurent                setStrategyMute(strategy, false, srcOutputs[i], MUTE_TIME_MS, newDevice);
1862fa3697d716b444bbea6be480801536c44bf69214Eric Laurent            }
18635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
1864f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1865f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // Move effects associated to this strategy from previous output to new output
18664660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen        if (strategy == STRATEGY_MEDIA) {
18674660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen            int outIdx = 0;
18684660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen            for (size_t i = 0; i < dstOutputs.size(); i++) {
18694660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                AudioOutputDescriptor *desc = mOutputs.valueFor(dstOutputs[i]);
18704660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                if (desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
18714660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                    outIdx = i;
18724660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                }
18734660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen            }
18744660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen            SortedVector<audio_io_handle_t> moved;
18754660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen            for (size_t i = 0; i < mEffects.size(); i++) {
18764660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                EffectDescriptor *desc = mEffects.valueAt(i);
18774660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                if (desc->mSession == AUDIO_SESSION_OUTPUT_MIX &&
18784660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                        desc->mIo != dstOutputs[outIdx]) {
18794660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                    if (moved.indexOf(desc->mIo) < 0) {
18804660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                        ALOGV("checkOutputForStrategy() moving effect %d to output %d",
18814660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                              mEffects.keyAt(i), dstOutputs[outIdx]);
18824660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                        mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, desc->mIo,
18834660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                                                       dstOutputs[outIdx]);
18844660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                        moved.add(desc->mIo);
18854660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                    }
18864660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                    desc->mIo = dstOutputs[outIdx];
18874660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                }
18884660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen            }
18894660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen        }
18905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // Move tracks associated to this strategy from previous output to new output
1891f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        for (int i = 0; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) {
1892f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (getStrategy((AudioSystem::stream_type)i) == strategy) {
189312bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi                //FIXME see fixme on name change
18944660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                mpClientInterface->setStreamOutput((AudioSystem::stream_type)i,
18954660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                                                   dstOutputs[0] /* ignored */);
1896f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1897f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1898f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1899f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1900f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1901f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::checkOutputForAllStrategies()
1902f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1903c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
1904f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForStrategy(STRATEGY_PHONE);
1905f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForStrategy(STRATEGY_SONIFICATION);
190612bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
1907f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForStrategy(STRATEGY_MEDIA);
1908f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForStrategy(STRATEGY_DTMF);
1909f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1910f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1911b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurentaudio_io_handle_t AudioPolicyManagerBase::getA2dpOutput()
1912b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent{
19135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (!mHasA2dp) {
1914b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        return 0;
1915b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    }
1916b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1917b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    for (size_t i = 0; i < mOutputs.size(); i++) {
1918b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        AudioOutputDescriptor *outputDesc = mOutputs.valueAt(i);
1919b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        if (!outputDesc->isDuplicated() && outputDesc->device() & AUDIO_DEVICE_OUT_ALL_A2DP) {
1920b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            return mOutputs.keyAt(i);
1921b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        }
1922b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    }
1923b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1924b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    return 0;
1925b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent}
1926b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1927f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::checkA2dpSuspend()
1928f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
19295ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (!mHasA2dp) {
1930b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        return;
1931b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    }
1932b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    audio_io_handle_t a2dpOutput = getA2dpOutput();
1933b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (a2dpOutput == 0) {
1934b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        return;
1935b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    }
1936b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1937f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // suspend A2DP output if:
1938f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      (NOT already suspended) &&
1939f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      ((SCO device is connected &&
1940f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //       (forced usage for communication || for record is SCO))) ||
1941f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      (phone state is ringing || in call)
1942f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //
1943f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // restore A2DP output if:
1944f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      (Already suspended) &&
1945f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      ((SCO device is NOT connected ||
1946f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //       (forced usage NOT for communication && NOT for record is SCO))) &&
1947f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      (phone state is NOT ringing && NOT in call)
1948f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //
1949f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mA2dpSuspended) {
1950f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (((mScoDeviceAddress == "") ||
1951f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin             ((mForceUse[AudioSystem::FOR_COMMUNICATION] != AudioSystem::FORCE_BT_SCO) &&
1952f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin              (mForceUse[AudioSystem::FOR_RECORD] != AudioSystem::FORCE_BT_SCO))) &&
1953f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin             ((mPhoneState != AudioSystem::MODE_IN_CALL) &&
1954f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin              (mPhoneState != AudioSystem::MODE_RINGTONE))) {
1955f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1956b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            mpClientInterface->restoreOutput(a2dpOutput);
1957f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mA2dpSuspended = false;
1958f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1959f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
1960f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (((mScoDeviceAddress != "") &&
1961f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin             ((mForceUse[AudioSystem::FOR_COMMUNICATION] == AudioSystem::FORCE_BT_SCO) ||
1962f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin              (mForceUse[AudioSystem::FOR_RECORD] == AudioSystem::FORCE_BT_SCO))) ||
1963f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin             ((mPhoneState == AudioSystem::MODE_IN_CALL) ||
1964f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin              (mPhoneState == AudioSystem::MODE_RINGTONE))) {
1965f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1966b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            mpClientInterface->suspendOutput(a2dpOutput);
1967f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mA2dpSuspended = true;
1968f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1969f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1970f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1971f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1972f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurentaudio_devices_t AudioPolicyManagerBase::getNewDevice(audio_io_handle_t output, bool fromCache)
1973f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1974ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    audio_devices_t device = AUDIO_DEVICE_NONE;
1975f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1976f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
1977f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // check the following by order of priority to request a routing change if necessary:
1978c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    // 1: the strategy enforced audible is active on the output:
1979c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    //      use device for strategy enforced audible
1980c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    // 2: we are in call or the strategy phone is active on the output:
1981f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      use device for strategy phone
1982c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    // 3: the strategy sonification is active on the output:
1983f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      use device for strategy sonification
198412bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    // 4: the strategy "respectful" sonification is active on the output:
198512bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    //      use device for strategy "respectful" sonification
198612bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    // 5: the strategy media is active on the output:
1987f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      use device for strategy media
198812bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    // 6: the strategy DTMF is active on the output:
1989f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      use device for strategy DTMF
1990c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    if (outputDesc->isUsedByStrategy(STRATEGY_ENFORCED_AUDIBLE)) {
1991c16ac09f510437e8340be691720177a490ae78f0Eric Laurent        device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
1992c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    } else if (isInCall() ||
1993c16ac09f510437e8340be691720177a490ae78f0Eric Laurent                    outputDesc->isUsedByStrategy(STRATEGY_PHONE)) {
1994f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
1995f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else if (outputDesc->isUsedByStrategy(STRATEGY_SONIFICATION)) {
1996f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
199712bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    } else if (outputDesc->isUsedByStrategy(STRATEGY_SONIFICATION_RESPECTFUL)) {
19985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
1999f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else if (outputDesc->isUsedByStrategy(STRATEGY_MEDIA)) {
2000f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
2001f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else if (outputDesc->isUsedByStrategy(STRATEGY_DTMF)) {
2002f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
2003f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2004f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
20056a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("getNewDevice() selected device %x", device);
2006f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return device;
2007f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2008f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2009f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinuint32_t AudioPolicyManagerBase::getStrategyForStream(AudioSystem::stream_type stream) {
2010f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return (uint32_t)getStrategy(stream);
2011f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2012f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2013f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurentaudio_devices_t AudioPolicyManagerBase::getDevicesForStream(AudioSystem::stream_type stream) {
2014f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent    audio_devices_t devices;
2015f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // By checking the range of stream before calling getStrategy, we avoid
20165efbd421e0029d6fc44b1cc65c0e5e0d85e5161fSteve Block    // getStrategy's behavior for invalid streams.  getStrategy would do a ALOGE
2017f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // and then return STRATEGY_MEDIA, but we want to return the empty set.
2018f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (stream < (AudioSystem::stream_type) 0 || stream >= AudioSystem::NUM_STREAM_TYPES) {
2019ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        devices = AUDIO_DEVICE_NONE;
2020f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
2021f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioPolicyManagerBase::routing_strategy strategy = getStrategy(stream);
20225ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        devices = getDeviceForStrategy(strategy, true /*fromCache*/);
2023f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2024f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return devices;
2025f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2026f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2027f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima ZavinAudioPolicyManagerBase::routing_strategy AudioPolicyManagerBase::getStrategy(
2028f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioSystem::stream_type stream) {
2029f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // stream to strategy mapping
2030f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    switch (stream) {
2031f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::VOICE_CALL:
2032f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::BLUETOOTH_SCO:
2033f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return STRATEGY_PHONE;
2034f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::RING:
2035f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::ALARM:
2036f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return STRATEGY_SONIFICATION;
203712bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    case AudioSystem::NOTIFICATION:
203812bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        return STRATEGY_SONIFICATION_RESPECTFUL;
2039f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::DTMF:
2040f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return STRATEGY_DTMF;
2041f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    default:
20425efbd421e0029d6fc44b1cc65c0e5e0d85e5161fSteve Block        ALOGE("unknown stream type");
2043f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::SYSTEM:
2044f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // NOTE: SYSTEM stream uses MEDIA strategy because muting music and switching outputs
2045f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // while key clicks are played produces a poor result
2046f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::TTS:
2047f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::MUSIC:
2048f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return STRATEGY_MEDIA;
2049c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    case AudioSystem::ENFORCED_AUDIBLE:
2050c16ac09f510437e8340be691720177a490ae78f0Eric Laurent        return STRATEGY_ENFORCED_AUDIBLE;
2051f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2052f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2053f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
205412bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivivoid AudioPolicyManagerBase::handleNotificationRoutingForStream(AudioSystem::stream_type stream) {
205512bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    switch(stream) {
205612bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    case AudioSystem::MUSIC:
205712bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
2058c952527e6f89d5427881462823514be9d79f13e6Eric Laurent        updateDevicesAndOutputs();
205912bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        break;
206012bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    default:
206112bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        break;
206212bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    }
206312bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi}
206412bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi
20655ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentaudio_devices_t AudioPolicyManagerBase::getDeviceForStrategy(routing_strategy strategy,
20665ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                             bool fromCache)
2067f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2068ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    uint32_t device = AUDIO_DEVICE_NONE;
2069f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2070f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (fromCache) {
20713cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
20725ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent              strategy, mDeviceForStrategy[strategy]);
2073f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return mDeviceForStrategy[strategy];
2074f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2075f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2076f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    switch (strategy) {
207712bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi
207812bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    case STRATEGY_SONIFICATION_RESPECTFUL:
207912bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        if (isInCall()) {
20805ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
208112bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        } else if (isStreamActive(AudioSystem::MUSIC, SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
208212bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi            // while media is playing (or has recently played), use the same device
20835ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/);
208412bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        } else {
208512bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi            // when media is not playing anymore, fall back on the sonification behavior
20865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
208712bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        }
208812bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi
208912bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        break;
209012bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi
2091f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case STRATEGY_DTMF:
2092f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (!isInCall()) {
2093f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // when off call, DTMF strategy follows the same rules as MEDIA strategy
20945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/);
2095f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            break;
2096f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2097f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // when in call, DTMF and PHONE strategies follow the same rules
2098f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // FALL THROUGH
2099f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2100f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case STRATEGY_PHONE:
2101f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // for phone strategy, we first consider the forced use and then the available devices by order
2102f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // of priority
2103f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        switch (mForceUse[AudioSystem::FOR_COMMUNICATION]) {
2104f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        case AudioSystem::FORCE_BT_SCO:
2105f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (!isInCall() || strategy != STRATEGY_DTMF) {
2106c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT;
2107f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (device) break;
2108f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2109c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET;
2110f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (device) break;
2111c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO;
2112f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (device) break;
2113f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // if SCO device is requested but no SCO device is available, fall back to default case
2114f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // FALL THROUGH
2115f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2116f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        default:    // FORCE_NONE
2117f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to A2DP
21181afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent            if (mHasA2dp && !isInCall() &&
21191afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent                    (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
2120ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent                    (getA2dpOutput() != 0) && !mA2dpSuspended) {
2121c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
2122f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (device) break;
2123c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
2124f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (device) break;
2125f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2126c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
21271afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent            if (device) break;
2128c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADSET;
21291afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent            if (device) break;
2130b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent            if (mPhoneState != AudioSystem::MODE_IN_CALL) {
2131b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
2132b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2133b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
2134b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2135b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
2136b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2137b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
2138b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2139b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
2140b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2141b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent            }
2142c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_EARPIECE;
21435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            if (device) break;
21445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = mDefaultOutputDevice;
2145ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            if (device == AUDIO_DEVICE_NONE) {
21465ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE");
2147f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2148f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            break;
2149f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2150f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        case AudioSystem::FORCE_SPEAKER:
2151f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to
2152f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // A2DP speaker when forcing to speaker output
21531afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent            if (mHasA2dp && !isInCall() &&
21541afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent                    (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
2155ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent                    (getA2dpOutput() != 0) && !mA2dpSuspended) {
2156c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
2157f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (device) break;
2158f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2159b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent            if (mPhoneState != AudioSystem::MODE_IN_CALL) {
2160b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
2161b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2162b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
2163b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2164b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
2165b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2166b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
2167b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2168b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
2169b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2170b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent            }
2171c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER;
21725ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            if (device) break;
21735ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = mDefaultOutputDevice;
2174ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            if (device == AUDIO_DEVICE_NONE) {
21755ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE, FORCE_SPEAKER");
2176f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2177f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            break;
2178f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2179f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    break;
2180f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2181f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case STRATEGY_SONIFICATION:
2182f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2183f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // If incall, just select the STRATEGY_PHONE device: The rest of the behavior is handled by
2184f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // handleIncallSonification().
2185f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (isInCall()) {
21865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = getDeviceForStrategy(STRATEGY_PHONE, false /*fromCache*/);
2187f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            break;
2188f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2189c16ac09f510437e8340be691720177a490ae78f0Eric Laurent        // FALL THROUGH
2190c16ac09f510437e8340be691720177a490ae78f0Eric Laurent
2191c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    case STRATEGY_ENFORCED_AUDIBLE:
2192c16ac09f510437e8340be691720177a490ae78f0Eric Laurent        // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION
2193ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        // except:
2194ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        //   - when in call where it doesn't default to STRATEGY_PHONE behavior
2195ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        //   - in countries where not enforced in which case it follows STRATEGY_MEDIA
2196c16ac09f510437e8340be691720177a490ae78f0Eric Laurent
2197738207def5f691d605ae33d041116829a74513a9Eric Laurent        if ((strategy == STRATEGY_SONIFICATION) ||
2198738207def5f691d605ae33d041116829a74513a9Eric Laurent                (mForceUse[AudioSystem::FOR_SYSTEM] == AudioSystem::FORCE_SYSTEM_ENFORCED)) {
2199c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER;
2200ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            if (device == AUDIO_DEVICE_NONE) {
2201ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent                ALOGE("getDeviceForStrategy() speaker device not found for STRATEGY_SONIFICATION");
2202ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent            }
2203f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2204f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // The second device used for sonification is the same as the device used by media strategy
2205f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // FALL THROUGH
2206f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2207f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case STRATEGY_MEDIA: {
2208ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        uint32_t device2 = AUDIO_DEVICE_NONE;
220931363a9cb94e80330c335fede0b92b1953a09517Jean-Michel Trivi        if (strategy != STRATEGY_SONIFICATION) {
221031363a9cb94e80330c335fede0b92b1953a09517Jean-Michel Trivi            // no sonification on remote submix (e.g. WFD)
221148387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
221248387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi        }
2213ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if ((device2 == AUDIO_DEVICE_NONE) &&
221448387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi                mHasA2dp && (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
2215ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent                (getA2dpOutput() != 0) && !mA2dpSuspended) {
2216c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
2217ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            if (device2 == AUDIO_DEVICE_NONE) {
2218c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
2219f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2220ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            if (device2 == AUDIO_DEVICE_NONE) {
2221c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
2222f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2223f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2224ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (device2 == AUDIO_DEVICE_NONE) {
2225c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
22261afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent        }
2227ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (device2 == AUDIO_DEVICE_NONE) {
2228c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADSET;
22291afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent        }
2230ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (device2 == AUDIO_DEVICE_NONE) {
2231599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
2232599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent        }
2233ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (device2 == AUDIO_DEVICE_NONE) {
2234599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
2235599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent        }
2236ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (device2 == AUDIO_DEVICE_NONE) {
2237c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
2238f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
223931363a9cb94e80330c335fede0b92b1953a09517Jean-Michel Trivi        if ((device2 == AUDIO_DEVICE_NONE) && (strategy != STRATEGY_SONIFICATION)) {
224031363a9cb94e80330c335fede0b92b1953a09517Jean-Michel Trivi            // no sonification on aux digital (e.g. HDMI)
2241c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
2242f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2243ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (device2 == AUDIO_DEVICE_NONE) {
2244c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
2245f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2246ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (device2 == AUDIO_DEVICE_NONE) {
2247c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER;
2248f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2249f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2250c16ac09f510437e8340be691720177a490ae78f0Eric Laurent        // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or
2251ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise
2252f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device |= device2;
22535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (device) break;
22545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        device = mDefaultOutputDevice;
2255ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (device == AUDIO_DEVICE_NONE) {
22565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGE("getDeviceForStrategy() no device found for STRATEGY_MEDIA");
2257f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2258f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        } break;
2259f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2260f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    default:
226164cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("getDeviceForStrategy() unknown strategy: %d", strategy);
2262f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
2263f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2264f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
22653cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGVV("getDeviceForStrategy() strategy %d, device %x", strategy, device);
2266c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    return device;
2267f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2268f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2269c952527e6f89d5427881462823514be9d79f13e6Eric Laurentvoid AudioPolicyManagerBase::updateDevicesAndOutputs()
2270f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2271f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < NUM_STRATEGIES; i++) {
22725ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
22735ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
2274c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    mPreviousOutputs = mOutputs;
22755ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
22765ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
2277b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurentuint32_t AudioPolicyManagerBase::checkDeviceMuteStrategies(AudioOutputDescriptor *outputDesc,
22789029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                                                       audio_devices_t prevDevice,
22795ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                       uint32_t delayMs)
22805ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
22819029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    // mute/unmute strategies using an incompatible device combination
22829029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    // if muting, wait for the audio in pcm buffer to be drained before proceeding
22839029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    // if unmuting, unmute only after the specified delay
22845ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (outputDesc->isDuplicated()) {
2285b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        return 0;
22865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
22875ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
22885ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    uint32_t muteWaitMs = 0;
22895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    audio_devices_t device = outputDesc->device();
22905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    bool shouldMute = (outputDesc->refCount() != 0) &&
22915ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                    (AudioSystem::popCount(device) >= 2);
22929029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    // temporary mute output if device selection changes to avoid volume bursts due to
22939029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    // different per device volumes
22949029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    bool tempMute = (outputDesc->refCount() != 0) && (device != prevDevice);
22955ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
22965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < NUM_STRATEGIES; i++) {
22975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
22985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        bool mute = shouldMute && (curDevice & device) && (curDevice != device);
22995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        bool doMute = false;
23005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
23015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (mute && !outputDesc->mStrategyMutedByDevice[i]) {
23025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            doMute = true;
23035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            outputDesc->mStrategyMutedByDevice[i] = true;
23045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){
23055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            doMute = true;
23065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            outputDesc->mStrategyMutedByDevice[i] = false;
23075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
23089029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent        if (doMute || tempMute) {
23095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            for (size_t j = 0; j < mOutputs.size(); j++) {
23105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                AudioOutputDescriptor *desc = mOutputs.valueAt(j);
2311c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                if ((desc->supportedDevices() & outputDesc->supportedDevices())
2312ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent                        == AUDIO_DEVICE_NONE) {
23135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                    continue;
23145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                }
23155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                audio_io_handle_t curOutput = mOutputs.keyAt(j);
23163cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x) on output %d",
23175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                      mute ? "muting" : "unmuting", i, curDevice, curOutput);
23185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                setStrategyMute((routing_strategy)i, mute, curOutput, mute ? 0 : delayMs);
23199029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                if (desc->strategyRefCount((routing_strategy)i) != 0) {
23209029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                    if (tempMute) {
232101e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                        setStrategyMute((routing_strategy)i, true, curOutput);
232201e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                        setStrategyMute((routing_strategy)i, false, curOutput,
232301e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                            desc->latency() * 2, device);
23249029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                    }
23259029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                    if (tempMute || mute) {
23269029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                        if (muteWaitMs < desc->latency()) {
23279029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                            muteWaitMs = desc->latency();
23289029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                        }
23295ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                    }
23305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                }
23315ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
23325ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
23335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
23345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
23355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // FIXME: should not need to double latency if volume could be applied immediately by the
23365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // audioflinger mixer. We must account for the delay between now and the next time
23375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // the audioflinger thread for this output will process a buffer (which corresponds to
23385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // one buffer size, usually 1/2 or 1/4 of the latency).
23395ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    muteWaitMs *= 2;
23405ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // wait for the PCM output buffers to empty before proceeding with the rest of the command
23415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (muteWaitMs > delayMs) {
2342b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        muteWaitMs -= delayMs;
2343b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        usleep(muteWaitMs * 1000);
2344b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        return muteWaitMs;
2345f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2346b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent    return 0;
2347f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2348f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2349b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurentuint32_t AudioPolicyManagerBase::setOutputDevice(audio_io_handle_t output,
2350f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                                             audio_devices_t device,
2351f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                                             bool force,
2352f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                                             int delayMs)
2353f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
23545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGV("setOutputDevice() output %d device %04x delayMs %d", output, device, delayMs);
2355f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
23565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    AudioParameter param;
2357b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent    uint32_t muteWaitMs = 0;
2358f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2359f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (outputDesc->isDuplicated()) {
2360b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        muteWaitMs = setOutputDevice(outputDesc->mOutput1->mId, device, force, delayMs);
2361b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        muteWaitMs += setOutputDevice(outputDesc->mOutput2->mId, device, force, delayMs);
2362b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        return muteWaitMs;
2363f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2364f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // filter devices according to output selected
2365f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent    device = (audio_devices_t)(device & outputDesc->mProfile->mSupportedDevices);
2366f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
23675ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    audio_devices_t prevDevice = outputDesc->mDevice;
23685ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
23695ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGV("setOutputDevice() prevDevice %04x", prevDevice);
23705ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
2371ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if (device != AUDIO_DEVICE_NONE) {
23725ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        outputDesc->mDevice = device;
23735ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
23749029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs);
23755ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
2376f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // Do not change the routing if:
2377ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    //  - the requested device is AUDIO_DEVICE_NONE
2378f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //  - the requested device is the same as current device and force is not specified.
2379f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // Doing this check here allows the caller to call setOutputDevice() without conditions
2380ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if ((device == AUDIO_DEVICE_NONE || device == prevDevice) && !force) {
23815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        ALOGV("setOutputDevice() setting same device %04x or null device for output %d", device, output);
2382b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        return muteWaitMs;
2383f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2384f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
23855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGV("setOutputDevice() changing device");
2386f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // do the routing
2387f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    param.addInt(String8(AudioParameter::keyRouting), (int)device);
23885ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    mpClientInterface->setParameters(output, param.toString(), delayMs);
23895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
2390f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // update stream volumes according to new device
2391f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    applyStreamVolumes(output, device, delayMs);
2392b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent
2393b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent    return muteWaitMs;
23945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
2395f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
23965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric LaurentAudioPolicyManagerBase::IOProfile *AudioPolicyManagerBase::getInputProfile(audio_devices_t device,
23975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                   uint32_t samplingRate,
23985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                   uint32_t format,
23995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                   uint32_t channelMask)
24005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
24015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // Choose an input profile based on the requested capture parameters: select the first available
24025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // profile supporting all requested parameters.
24035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
240470c236c9290732782d5267935af1475b8d5ae602Eric Laurent    for (size_t i = 0; i < mHwModules.size(); i++)
24055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    {
240670c236c9290732782d5267935af1475b8d5ae602Eric Laurent        if (mHwModules[i]->mHandle == 0) {
24075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            continue;
24085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
240970c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
24105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        {
241170c236c9290732782d5267935af1475b8d5ae602Eric Laurent            IOProfile *profile = mHwModules[i]->mInputProfiles[j];
241270c236c9290732782d5267935af1475b8d5ae602Eric Laurent            if (profile->isCompatibleProfile(device, samplingRate, format,
24130977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent                                             channelMask,(audio_output_flags_t)0)) {
241470c236c9290732782d5267935af1475b8d5ae602Eric Laurent                return profile;
24155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
24165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
2417f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
241870c236c9290732782d5267935af1475b8d5ae602Eric Laurent    return NULL;
2419f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2420f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2421f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurentaudio_devices_t AudioPolicyManagerBase::getDeviceForInputSource(int inputSource)
2422f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2423ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    uint32_t device = AUDIO_DEVICE_NONE;
2424f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2425f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    switch(inputSource) {
2426f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_DEFAULT:
2427f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_MIC:
2428f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_RECOGNITION:
2429f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_COMMUNICATION:
2430f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (mForceUse[AudioSystem::FOR_RECORD] == AudioSystem::FORCE_BT_SCO &&
2431c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            mAvailableInputDevices & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
2432c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
2433c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_WIRED_HEADSET) {
2434c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = AUDIO_DEVICE_IN_WIRED_HEADSET;
2435c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_BUILTIN_MIC) {
2436c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = AUDIO_DEVICE_IN_BUILTIN_MIC;
2437f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2438f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
2439f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_CAMCORDER:
2440c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        if (mAvailableInputDevices & AUDIO_DEVICE_IN_BACK_MIC) {
2441c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = AUDIO_DEVICE_IN_BACK_MIC;
2442c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_BUILTIN_MIC) {
2443c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = AUDIO_DEVICE_IN_BUILTIN_MIC;
2444f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2445f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
2446f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_UPLINK:
2447f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_DOWNLINK:
2448f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_CALL:
2449c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        if (mAvailableInputDevices & AUDIO_DEVICE_IN_VOICE_CALL) {
2450c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = AUDIO_DEVICE_IN_VOICE_CALL;
24515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
2452f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
245348387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi    case AUDIO_SOURCE_REMOTE_SUBMIX:
245448387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi        if (mAvailableInputDevices & AUDIO_DEVICE_IN_REMOTE_SUBMIX) {
245548387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
245648387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi        }
245748387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi        break;
2458f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    default:
245964cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("getDeviceForInputSource() invalid input source %d", inputSource);
2460f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
2461f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
24626a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("getDeviceForInputSource()input source %d, device %08x", inputSource, device);
2463c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    return device;
2464f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2465f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
24666d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivibool AudioPolicyManagerBase::isVirtualInputDevice(audio_devices_t device)
24676d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi{
24686d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi    if ((device & AUDIO_DEVICE_BIT_IN) != 0) {
24696d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi        device &= ~AUDIO_DEVICE_BIT_IN;
24706d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi        if ((popcount(device) == 1) && ((device & ~APM_AUDIO_IN_DEVICE_VIRTUAL_ALL) == 0))
24716d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi            return true;
24726d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi    }
24736d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi    return false;
24746d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi}
24756d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi
24766d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Triviaudio_io_handle_t AudioPolicyManagerBase::getActiveInput(bool ignoreVirtualInputs)
2477f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2478f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mInputs.size(); i++) {
24796d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi        const AudioInputDescriptor * input_descriptor = mInputs.valueAt(i);
24806d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi        if ((input_descriptor->mRefCount > 0)
24816d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi                && (!ignoreVirtualInputs || !isVirtualInputDevice(input_descriptor->mDevice))) {
2482f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return mInputs.keyAt(i);
2483f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2484f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2485f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return 0;
2486f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2487f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2488e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2489c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentaudio_devices_t AudioPolicyManagerBase::getDeviceForVolume(audio_devices_t device)
2490e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent{
2491ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if (device == AUDIO_DEVICE_NONE) {
2492e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        // this happens when forcing a route update and no track is active on an output.
2493e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        // In this case the returned category is not important.
2494c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        device =  AUDIO_DEVICE_OUT_SPEAKER;
2495c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    } else if (AudioSystem::popCount(device) > 1) {
2496e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        // Multiple device selection is either:
2497e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        //  - speaker + one other device: give priority to speaker in this case.
2498e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        //  - one A2DP device + another device: happens with duplicated output. In this case
2499e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        // retain the device on the A2DP output as the other must not correspond to an active
2500e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        // selection if not the speaker.
2501c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        if (device & AUDIO_DEVICE_OUT_SPEAKER) {
2502c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            device = AUDIO_DEVICE_OUT_SPEAKER;
2503c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        } else {
2504c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            device = (audio_devices_t)(device & AUDIO_DEVICE_OUT_ALL_A2DP);
2505c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        }
2506e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    }
2507e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
250864cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block    ALOGW_IF(AudioSystem::popCount(device) != 1,
2509c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            "getDeviceForVolume() invalid device combination: %08x",
2510e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            device);
2511e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2512c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    return device;
2513c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent}
2514c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
2515f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric LaurentAudioPolicyManagerBase::device_category AudioPolicyManagerBase::getDeviceCategory(audio_devices_t device)
2516c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent{
2517f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent    switch(getDeviceForVolume(device)) {
2518e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_EARPIECE:
2519e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            return DEVICE_CATEGORY_EARPIECE;
2520e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_WIRED_HEADSET:
2521e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_WIRED_HEADPHONE:
2522e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_BLUETOOTH_SCO:
2523e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET:
2524e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP:
2525e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES:
2526e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            return DEVICE_CATEGORY_HEADSET;
2527e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_SPEAKER:
2528e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT:
2529e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER:
2530c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        case AUDIO_DEVICE_OUT_AUX_DIGITAL:
2531599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent        case AUDIO_DEVICE_OUT_USB_ACCESSORY:
2532599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent        case AUDIO_DEVICE_OUT_USB_DEVICE:
253348387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi        case AUDIO_DEVICE_OUT_REMOTE_SUBMIX:
2534e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        default:
2535e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            return DEVICE_CATEGORY_SPEAKER;
2536e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    }
2537e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent}
2538e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2539f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurentfloat AudioPolicyManagerBase::volIndexToAmpl(audio_devices_t device, const StreamDescriptor& streamDesc,
2540e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        int indexInUi)
2541e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent{
2542e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    device_category deviceCategory = getDeviceCategory(device);
2543e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    const VolumeCurvePoint *curve = streamDesc.mVolumeCurve[deviceCategory];
2544e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2545f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // the volume index in the UI is relative to the min and max volume indices for this stream type
2546e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    int nbSteps = 1 + curve[VOLMAX].mIndex -
2547e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            curve[VOLMIN].mIndex;
2548f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    int volIdx = (nbSteps * (indexInUi - streamDesc.mIndexMin)) /
2549f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            (streamDesc.mIndexMax - streamDesc.mIndexMin);
2550f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2551f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // find what part of the curve this index volume belongs to, or if it's out of bounds
2552f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    int segment = 0;
2553e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    if (volIdx < curve[VOLMIN].mIndex) {         // out of bounds
2554f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return 0.0f;
2555e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    } else if (volIdx < curve[VOLKNEE1].mIndex) {
2556f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        segment = 0;
2557e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    } else if (volIdx < curve[VOLKNEE2].mIndex) {
2558f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        segment = 1;
2559e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    } else if (volIdx <= curve[VOLMAX].mIndex) {
2560f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        segment = 2;
2561f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {                                                               // out of bounds
2562f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return 1.0f;
2563f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2564f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2565f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // linear interpolation in the attenuation table in dB
2566e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    float decibels = curve[segment].mDBAttenuation +
2567e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            ((float)(volIdx - curve[segment].mIndex)) *
2568e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent                ( (curve[segment+1].mDBAttenuation -
2569e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent                        curve[segment].mDBAttenuation) /
2570e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent                    ((float)(curve[segment+1].mIndex -
2571e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent                            curve[segment].mIndex)) );
2572f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2573f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    float amplification = exp( decibels * 0.115129f); // exp( dB * ln(10) / 20 )
2574f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
25753cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGVV("VOLUME vol index=[%d %d %d], dB=[%.1f %.1f %.1f] ampl=%.5f",
2576e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            curve[segment].mIndex, volIdx,
2577e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            curve[segment+1].mIndex,
2578e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            curve[segment].mDBAttenuation,
2579cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent            decibels,
2580e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            curve[segment+1].mDBAttenuation,
2581f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            amplification);
2582f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2583f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return amplification;
2584f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2585f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2586cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
2587e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    AudioPolicyManagerBase::sDefaultVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
2588e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    {1, -49.5f}, {33, -33.5f}, {66, -17.0f}, {100, 0.0f}
2589e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent};
2590e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2591e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
2592e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    AudioPolicyManagerBase::sDefaultMediaVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
2593e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    {1, -58.0f}, {20, -40.0f}, {60, -17.0f}, {100, 0.0f}
2594cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent};
2595cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent
2596e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
2597e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    AudioPolicyManagerBase::sSpeakerMediaVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
2598e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    {1, -56.0f}, {20, -34.0f}, {60, -11.0f}, {100, 0.0f}
2599e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent};
2600e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2601e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
2602e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    AudioPolicyManagerBase::sSpeakerSonificationVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
2603e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    {1, -29.7f}, {33, -20.1f}, {66, -10.2f}, {100, 0.0f}
2604e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent};
2605e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2606ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent// AUDIO_STREAM_SYSTEM, AUDIO_STREAM_ENFORCED_AUDIBLE and AUDIO_STREAM_DTMF volume tracks
2607ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent// AUDIO_STREAM_RING on phones and AUDIO_STREAM_MUSIC on tablets (See AudioService.java).
2608123897874418f9f0e48bb89386d8c470e6975f28Jean-Michel Trivi// The range is constrained between -24dB and -6dB over speaker and -30dB and -18dB over headset.
2609ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
2610ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    AudioPolicyManagerBase::sDefaultSystemVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
2611123897874418f9f0e48bb89386d8c470e6975f28Jean-Michel Trivi    {1, -24.0f}, {33, -18.0f}, {66, -12.0f}, {100, -6.0f}
2612ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent};
2613ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent
2614ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
2615ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    AudioPolicyManagerBase::sHeadsetSystemVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
26167465678e0d5711ebcd78ae47b3a76821534a23eaEric Laurent    {1, -30.0f}, {33, -26.0f}, {66, -22.0f}, {100, -18.0f}
2617ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent};
2618e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2619e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
2620ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent            *AudioPolicyManagerBase::sVolumeProfiles[AUDIO_STREAM_CNT]
2621e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent                                                   [AudioPolicyManagerBase::DEVICE_CATEGORY_CNT] = {
2622ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_VOICE_CALL
2623e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
2624e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sDefaultVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2625e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sDefaultVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2626e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    },
2627ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_SYSTEM
2628ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET
2629ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2630ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultSystemVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2631ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    },
2632ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_RING
2633e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
2634e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2635e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sDefaultVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2636e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    },
2637ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_MUSIC
2638ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultMediaVolumeCurve, // DEVICE_CATEGORY_HEADSET
2639ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sSpeakerMediaVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2640ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultMediaVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2641ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    },
2642ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_ALARM
264312bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
264412bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER
264512bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        sDefaultVolumeCurve  // DEVICE_CATEGORY_EARPIECE
264612bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    },
2647ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_NOTIFICATION
2648e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
2649ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2650e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sDefaultVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2651c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    },
2652ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_BLUETOOTH_SCO
2653c16ac09f510437e8340be691720177a490ae78f0Eric Laurent        sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
2654ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2655c16ac09f510437e8340be691720177a490ae78f0Eric Laurent        sDefaultVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2656c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    },
2657ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_ENFORCED_AUDIBLE
2658ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET
2659ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2660ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultSystemVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2661ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    },
2662ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    {  // AUDIO_STREAM_DTMF
2663ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET
2664ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2665ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultSystemVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2666ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    },
2667ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_TTS
2668ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultMediaVolumeCurve, // DEVICE_CATEGORY_HEADSET
2669ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sSpeakerMediaVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2670ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultMediaVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2671ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    },
2672e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent};
2673e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2674e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurentvoid AudioPolicyManagerBase::initializeVolumeCurves()
2675e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent{
2676ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    for (int i = 0; i < AUDIO_STREAM_CNT; i++) {
2677e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) {
2678cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent            mStreams[i].mVolumeCurve[j] =
2679ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent                    sVolumeProfiles[i][j];
2680cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent        }
2681cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent    }
2682f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2683f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2684c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentfloat AudioPolicyManagerBase::computeVolume(int stream,
2685c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                            int index,
2686c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                            audio_io_handle_t output,
2687f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                                            audio_devices_t device)
2688f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2689f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    float volume = 1.0;
2690f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
2691f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    StreamDescriptor &streamDesc = mStreams[stream];
2692f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2693ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if (device == AUDIO_DEVICE_NONE) {
2694f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device = outputDesc->device();
2695f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2696f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2697f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if volume is not 0 (not muted), force media volume to max on digital output
2698f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (stream == AudioSystem::MUSIC &&
2699f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        index != mStreams[stream].mIndexMin &&
2700f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent        (device == AUDIO_DEVICE_OUT_AUX_DIGITAL ||
2701599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent         device == AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET ||
2702599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent         device == AUDIO_DEVICE_OUT_USB_ACCESSORY ||
2703599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent         device == AUDIO_DEVICE_OUT_USB_DEVICE)) {
2704f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return 1.0;
2705f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2706f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2707f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    volume = volIndexToAmpl(device, streamDesc, index);
2708f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2709f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if a headset is connected, apply the following rules to ring tones and notifications
2710f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // to avoid sound level bursts in user's ears:
2711f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // - always attenuate ring tones and notifications volume by 6dB
2712f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // - if music is playing, always limit the volume to current music volume,
2713f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // with a minimum threshold at -36dB so that notification is always perceived.
271412bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    const routing_strategy stream_strategy = getStrategy((AudioSystem::stream_type)stream);
2715c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
2716c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
2717c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            AUDIO_DEVICE_OUT_WIRED_HEADSET |
2718c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) &&
271912bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        ((stream_strategy == STRATEGY_SONIFICATION)
272012bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi                || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL)
2721738207def5f691d605ae33d041116829a74513a9Eric Laurent                || (stream == AudioSystem::SYSTEM)
2722738207def5f691d605ae33d041116829a74513a9Eric Laurent                || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) &&
2723738207def5f691d605ae33d041116829a74513a9Eric Laurent                    (mForceUse[AudioSystem::FOR_SYSTEM] == AudioSystem::FORCE_NONE))) &&
2724f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        streamDesc.mCanBeMuted) {
2725f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        volume *= SONIFICATION_HEADSET_VOLUME_FACTOR;
2726f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // when the phone is ringing we must consider that music could have been paused just before
2727f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // by the music application and behave as if music was active if the last music track was
2728f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // just stopped
2729ac3cf10ef6de12e69540a1244ac7255f93fa7502Eric Laurent        if (isStreamActive(AudioSystem::MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) ||
2730ac3cf10ef6de12e69540a1244ac7255f93fa7502Eric Laurent                mLimitRingtoneVolume) {
273117a73c3394547692457299dc512b5c2312ea0344Eric Laurent            audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/);
2732c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            float musicVol = computeVolume(AudioSystem::MUSIC,
273317a73c3394547692457299dc512b5c2312ea0344Eric Laurent                               mStreams[AudioSystem::MUSIC].getVolumeIndex(musicDevice),
2734c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                               output,
273517a73c3394547692457299dc512b5c2312ea0344Eric Laurent                               musicDevice);
2736c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            float minVol = (musicVol > SONIFICATION_HEADSET_VOLUME_MIN) ?
2737c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                musicVol : SONIFICATION_HEADSET_VOLUME_MIN;
2738f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (volume > minVol) {
2739f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                volume = minVol;
27406a70518b93928d1c91457ff805e375c82d76b0e5Steve Block                ALOGV("computeVolume limiting volume to %f musicVol %f", minVol, musicVol);
2741f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2742f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2743f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2744f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2745f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return volume;
2746f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2747f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2748c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentstatus_t AudioPolicyManagerBase::checkAndSetVolume(int stream,
2749c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                   int index,
2750c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                   audio_io_handle_t output,
2751f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                                                   audio_devices_t device,
2752c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                   int delayMs,
2753c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                   bool force)
2754f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2755f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2756f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // do not change actual stream volume if the stream is muted
2757f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mOutputs.valueFor(output)->mMuteCount[stream] != 0) {
27583cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        ALOGVV("checkAndSetVolume() stream %d muted count %d",
27595ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent              stream, mOutputs.valueFor(output)->mMuteCount[stream]);
2760f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return NO_ERROR;
2761f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2762f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2763f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // do not change in call volume if bluetooth is connected and vice versa
2764f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if ((stream == AudioSystem::VOICE_CALL && mForceUse[AudioSystem::FOR_COMMUNICATION] == AudioSystem::FORCE_BT_SCO) ||
2765f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        (stream == AudioSystem::BLUETOOTH_SCO && mForceUse[AudioSystem::FOR_COMMUNICATION] != AudioSystem::FORCE_BT_SCO)) {
27666a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
2767f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin             stream, mForceUse[AudioSystem::FOR_COMMUNICATION]);
2768f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return INVALID_OPERATION;
2769f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2770f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2771f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    float volume = computeVolume(stream, index, output, device);
2772f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // We actually change the volume if:
2773f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // - the float value returned by computeVolume() changed
2774f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // - the force flag is set
2775f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (volume != mOutputs.valueFor(output)->mCurVolume[stream] ||
2776f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            force) {
2777f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mOutputs.valueFor(output)->mCurVolume[stream] = volume;
27783cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        ALOGVV("checkAndSetVolume() for output %d stream %d, volume %f, delay %d", output, stream, volume, delayMs);
2779f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (stream == AudioSystem::VOICE_CALL ||
2780f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            stream == AudioSystem::DTMF ||
2781f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            stream == AudioSystem::BLUETOOTH_SCO) {
2782f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // offset value to reflect actual hardware volume that never reaches 0
2783f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // 1% corresponds roughly to first step in VOICE_CALL stream volume setting (see AudioService.java)
2784f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            volume = 0.01 + 0.99 * volume;
2785f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // Force VOICE_CALL to track BLUETOOTH_SCO stream volume when bluetooth audio is
2786f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // enabled
2787f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (stream == AudioSystem::BLUETOOTH_SCO) {
2788f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mpClientInterface->setStreamVolume(AudioSystem::VOICE_CALL, volume, output, delayMs);
2789f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2790f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2791f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2792f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mpClientInterface->setStreamVolume((AudioSystem::stream_type)stream, volume, output, delayMs);
2793f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2794f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2795f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (stream == AudioSystem::VOICE_CALL ||
2796f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        stream == AudioSystem::BLUETOOTH_SCO) {
2797f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        float voiceVolume;
2798f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // Force voice volume to max for bluetooth SCO as volume is managed by the headset
2799f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (stream == AudioSystem::VOICE_CALL) {
2800f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            voiceVolume = (float)index/(float)mStreams[stream].mIndexMax;
2801f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        } else {
2802f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            voiceVolume = 1.0;
2803f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2804f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2805b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        if (voiceVolume != mLastVoiceVolume && output == mPrimaryOutput) {
2806f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
2807f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mLastVoiceVolume = voiceVolume;
2808f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2809f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2810f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2811f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
2812f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2813f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2814c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentvoid AudioPolicyManagerBase::applyStreamVolumes(audio_io_handle_t output,
2815f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                                                audio_devices_t device,
2816c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                int delayMs,
2817c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                bool force)
2818f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
28193cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGVV("applyStreamVolumes() for output %d and device %x", output, device);
2820f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2821f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
2822c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        checkAndSetVolume(stream,
2823f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                          mStreams[stream].getVolumeIndex(device),
2824c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                          output,
2825c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                          device,
2826c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                          delayMs,
2827c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                          force);
2828f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2829f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2830f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
283101e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurentvoid AudioPolicyManagerBase::setStrategyMute(routing_strategy strategy,
283201e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                             bool on,
283301e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                             audio_io_handle_t output,
283401e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                             int delayMs,
283501e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                             audio_devices_t device)
2836f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
28373cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGVV("setStrategyMute() strategy %d, mute %d, output %d", strategy, on, output);
2838f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
2839f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (getStrategy((AudioSystem::stream_type)stream) == strategy) {
284001e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent            setStreamMute(stream, on, output, delayMs, device);
2841f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2842f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2843f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2844f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
284501e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurentvoid AudioPolicyManagerBase::setStreamMute(int stream,
284601e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                           bool on,
284701e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                           audio_io_handle_t output,
284801e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                           int delayMs,
284901e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                           audio_devices_t device)
2850f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2851f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    StreamDescriptor &streamDesc = mStreams[stream];
2852f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
2853ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if (device == AUDIO_DEVICE_NONE) {
285401e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent        device = outputDesc->device();
285501e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent    }
2856f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
28573cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGVV("setStreamMute() stream %d, mute %d, output %d, mMuteCount %d device %04x",
285801e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent          stream, on, output, outputDesc->mMuteCount[stream], device);
2859f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2860f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (on) {
2861f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (outputDesc->mMuteCount[stream] == 0) {
2862738207def5f691d605ae33d041116829a74513a9Eric Laurent            if (streamDesc.mCanBeMuted &&
2863738207def5f691d605ae33d041116829a74513a9Eric Laurent                    ((stream != AudioSystem::ENFORCED_AUDIBLE) ||
2864738207def5f691d605ae33d041116829a74513a9Eric Laurent                     (mForceUse[AudioSystem::FOR_SYSTEM] == AudioSystem::FORCE_NONE))) {
2865c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                checkAndSetVolume(stream, 0, output, device, delayMs);
2866f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2867f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2868f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
2869f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mMuteCount[stream]++;
2870f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
2871f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (outputDesc->mMuteCount[stream] == 0) {
28723cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            ALOGV("setStreamMute() unmuting non muted stream!");
2873f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return;
2874f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2875f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (--outputDesc->mMuteCount[stream] == 0) {
2876c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            checkAndSetVolume(stream,
2877c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                              streamDesc.getVolumeIndex(device),
2878c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                              output,
2879c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                              device,
2880c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                              delayMs);
2881f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2882f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2883f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2884f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2885f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::handleIncallSonification(int stream, bool starting, bool stateChange)
2886f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2887f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if the stream pertains to sonification strategy and we are in call we must
2888f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // mute the stream if it is low visibility. If it is high visibility, we must play a tone
2889f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // in the device used for phone strategy and play the tone if the selected device does not
2890f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // interfere with the device used for phone strategy
2891f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
2892f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // many times as there are active tracks on the output
289312bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    const routing_strategy stream_strategy = getStrategy((AudioSystem::stream_type)stream);
289412bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    if ((stream_strategy == STRATEGY_SONIFICATION) ||
289512bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi            ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
2896b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        AudioOutputDescriptor *outputDesc = mOutputs.valueFor(mPrimaryOutput);
28976a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
2898f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                stream, starting, outputDesc->mDevice, stateChange);
2899f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (outputDesc->mRefCount[stream]) {
2900f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            int muteCount = 1;
2901f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (stateChange) {
2902f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                muteCount = outputDesc->mRefCount[stream];
2903f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2904f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (AudioSystem::isLowVisibility((AudioSystem::stream_type)stream)) {
29056a70518b93928d1c91457ff805e375c82d76b0e5Steve Block                ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
2906f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                for (int i = 0; i < muteCount; i++) {
2907b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                    setStreamMute(stream, starting, mPrimaryOutput);
2908f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
2909f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            } else {
29106a70518b93928d1c91457ff805e375c82d76b0e5Steve Block                ALOGV("handleIncallSonification() high visibility");
29115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                if (outputDesc->device() &
29125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                        getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
29136a70518b93928d1c91457ff805e375c82d76b0e5Steve Block                    ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
2914f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    for (int i = 0; i < muteCount; i++) {
2915b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                        setStreamMute(stream, starting, mPrimaryOutput);
2916f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    }
2917f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
2918f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (starting) {
2919f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    mpClientInterface->startTone(ToneGenerator::TONE_SUP_CALL_WAITING, AudioSystem::VOICE_CALL);
2920f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else {
2921f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    mpClientInterface->stopTone();
2922f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
2923f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2924f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2925f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2926f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2927f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2928f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinbool AudioPolicyManagerBase::isInCall()
2929f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2930f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return isStateInCall(mPhoneState);
2931f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2932f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2933f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinbool AudioPolicyManagerBase::isStateInCall(int state) {
2934f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return ((state == AudioSystem::MODE_IN_CALL) ||
2935f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            (state == AudioSystem::MODE_IN_COMMUNICATION));
2936f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2937f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
293870c236c9290732782d5267935af1475b8d5ae602Eric Laurentbool AudioPolicyManagerBase::needsDirectOuput(audio_stream_type_t stream,
293970c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                              uint32_t samplingRate,
294070c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                              audio_format_t format,
294170c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                              audio_channel_mask_t channelMask,
29420977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent                                              audio_output_flags_t flags,
294370c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                              audio_devices_t device)
2944f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2945f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin   return ((flags & AudioSystem::OUTPUT_FLAG_DIRECT) ||
2946b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent          (format != 0 && !AudioSystem::isLinearPCM(format)));
2947f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2948f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2949f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinuint32_t AudioPolicyManagerBase::getMaxEffectsCpuLoad()
2950f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2951f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return MAX_EFFECTS_CPU_LOAD;
2952f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2953f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2954f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinuint32_t AudioPolicyManagerBase::getMaxEffectsMemory()
2955f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2956f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return MAX_EFFECTS_MEMORY;
2957f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2958f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2959f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// --- AudioOutputDescriptor class implementation
2960f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2961b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric LaurentAudioPolicyManagerBase::AudioOutputDescriptor::AudioOutputDescriptor(
29625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        const IOProfile *profile)
296370c236c9290732782d5267935af1475b8d5ae602Eric Laurent    : mId(0), mSamplingRate(0), mFormat((audio_format_t)0),
296470c236c9290732782d5267935af1475b8d5ae602Eric Laurent      mChannelMask((audio_channel_mask_t)0), mLatency(0),
2965ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    mFlags((audio_output_flags_t)0), mDevice(AUDIO_DEVICE_NONE),
2966f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent    mOutput1(0), mOutput2(0), mProfile(profile)
2967f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2968f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // clear usage count for all stream types
2969f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) {
2970f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mRefCount[i] = 0;
2971f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mCurVolume[i] = -1.0;
2972f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mMuteCount[i] = 0;
2973f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mStopTime[i] = 0;
2974f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
297585ad78f27ca032e90af0f2100659d12c16322c76Marco Nelissen    for (int i = 0; i < NUM_STRATEGIES; i++) {
297685ad78f27ca032e90af0f2100659d12c16322c76Marco Nelissen        mStrategyMutedByDevice[i] = false;
297785ad78f27ca032e90af0f2100659d12c16322c76Marco Nelissen    }
29783cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    if (profile != NULL) {
29793cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        mSamplingRate = profile->mSamplingRates[0];
29803cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        mFormat = profile->mFormats[0];
29813cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        mChannelMask = profile->mChannelMasks[0];
29823cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        mFlags = profile->mFlags;
29833cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    }
2984f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2985f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2986f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurentaudio_devices_t AudioPolicyManagerBase::AudioOutputDescriptor::device()
2987f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2988f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (isDuplicated()) {
2989f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent        return (audio_devices_t)(mOutput1->mDevice | mOutput2->mDevice);
2990f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
2991f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent        return mDevice;
2992f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2993f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2994f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
29955ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentuint32_t AudioPolicyManagerBase::AudioOutputDescriptor::latency()
29965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
29975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (isDuplicated()) {
29985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return (mOutput1->mLatency > mOutput2->mLatency) ? mOutput1->mLatency : mOutput2->mLatency;
29995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    } else {
30005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return mLatency;
30015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
30025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
30035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
30045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentbool AudioPolicyManagerBase::AudioOutputDescriptor::sharesHwModuleWith(
30055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        const AudioOutputDescriptor *outputDesc)
30065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
30075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (isDuplicated()) {
30085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return mOutput1->sharesHwModuleWith(outputDesc) || mOutput2->sharesHwModuleWith(outputDesc);
30095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    } else if (outputDesc->isDuplicated()){
30105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return sharesHwModuleWith(outputDesc->mOutput1) || sharesHwModuleWith(outputDesc->mOutput2);
30115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    } else {
301270c236c9290732782d5267935af1475b8d5ae602Eric Laurent        return (mProfile->mModule == outputDesc->mProfile->mModule);
30135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
30145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
30155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
3016f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::AudioOutputDescriptor::changeRefCount(AudioSystem::stream_type stream, int delta)
3017f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3018f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // forward usage count change to attached outputs
3019f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (isDuplicated()) {
3020f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mOutput1->changeRefCount(stream, delta);
3021f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mOutput2->changeRefCount(stream, delta);
3022f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3023f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if ((delta + (int)mRefCount[stream]) < 0) {
302464cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("changeRefCount() invalid delta %d for stream %d, refCount %d", delta, stream, mRefCount[stream]);
3025f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mRefCount[stream] = 0;
3026f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
3027f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3028f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mRefCount[stream] += delta;
30296a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("changeRefCount() stream %d, count %d", stream, mRefCount[stream]);
3030f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3031f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3032f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinuint32_t AudioPolicyManagerBase::AudioOutputDescriptor::refCount()
3033f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3034f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    uint32_t refcount = 0;
3035f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) {
3036f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        refcount += mRefCount[i];
3037f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3038f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return refcount;
3039f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3040f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3041f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinuint32_t AudioPolicyManagerBase::AudioOutputDescriptor::strategyRefCount(routing_strategy strategy)
3042f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3043f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    uint32_t refCount = 0;
3044f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) {
3045f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (getStrategy((AudioSystem::stream_type)i) == strategy) {
3046f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            refCount += mRefCount[i];
3047f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
3048f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3049f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return refCount;
3050f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3051f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3052f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurentaudio_devices_t AudioPolicyManagerBase::AudioOutputDescriptor::supportedDevices()
3053b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent{
3054b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (isDuplicated()) {
3055f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent        return (audio_devices_t)(mOutput1->supportedDevices() | mOutput2->supportedDevices());
3056b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    } else {
3057b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        return mProfile->mSupportedDevices ;
3058b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    }
3059b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent}
3060b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
3061f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::AudioOutputDescriptor::dump(int fd)
3062f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3063f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    const size_t SIZE = 256;
3064f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    char buffer[SIZE];
3065f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    String8 result;
3066f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3067f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Sampling rate: %d\n", mSamplingRate);
3068f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3069f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Format: %d\n", mFormat);
3070f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
307170c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, " Channels: %08x\n", mChannelMask);
3072f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3073f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Latency: %d\n", mLatency);
3074f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3075f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Flags %08x\n", mFlags);
3076f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3077f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Devices %08x\n", device());
3078f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3079f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Stream volume refCount muteCount\n");
3080f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3081f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) {
3082f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        snprintf(buffer, SIZE, " %02d     %.03f     %02d       %02d\n", i, mCurVolume[i], mRefCount[i], mMuteCount[i]);
3083f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        result.append(buffer);
3084f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3085f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, result.string(), result.size());
3086f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3087f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
3088f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3089f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3090f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// --- AudioInputDescriptor class implementation
3091f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
30925ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric LaurentAudioPolicyManagerBase::AudioInputDescriptor::AudioInputDescriptor(const IOProfile *profile)
309370c236c9290732782d5267935af1475b8d5ae602Eric Laurent    : mSamplingRate(0), mFormat((audio_format_t)0), mChannelMask((audio_channel_mask_t)0),
3094ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent      mDevice(AUDIO_DEVICE_NONE), mRefCount(0),
30955ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent      mInputSource(0), mProfile(profile)
3096f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3097f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3098f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3099f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::AudioInputDescriptor::dump(int fd)
3100f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3101f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    const size_t SIZE = 256;
3102f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    char buffer[SIZE];
3103f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    String8 result;
3104f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3105f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Sampling rate: %d\n", mSamplingRate);
3106f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3107f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Format: %d\n", mFormat);
3108f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
310970c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, " Channels: %08x\n", mChannelMask);
3110f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3111f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Devices %08x\n", mDevice);
3112f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3113f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Ref Count %d\n", mRefCount);
3114f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3115f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, result.string(), result.size());
3116f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3117f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
3118f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3119f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3120f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// --- StreamDescriptor class implementation
3121f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3122c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric LaurentAudioPolicyManagerBase::StreamDescriptor::StreamDescriptor()
3123c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    :   mIndexMin(0), mIndexMax(1), mCanBeMuted(true)
3124c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent{
3125c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    mIndexCur.add(AUDIO_DEVICE_OUT_DEFAULT, 0);
3126c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent}
3127c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
3128c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentint AudioPolicyManagerBase::StreamDescriptor::getVolumeIndex(audio_devices_t device)
3129c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent{
3130c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    device = AudioPolicyManagerBase::getDeviceForVolume(device);
3131c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    // there is always a valid entry for AUDIO_DEVICE_OUT_DEFAULT
3132c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    if (mIndexCur.indexOfKey(device) < 0) {
3133c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        device = AUDIO_DEVICE_OUT_DEFAULT;
3134c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    }
3135c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    return mIndexCur.valueFor(device);
3136c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent}
3137c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
3138c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentvoid AudioPolicyManagerBase::StreamDescriptor::dump(int fd)
3139f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3140c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    const size_t SIZE = 256;
3141c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    char buffer[SIZE];
3142c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    String8 result;
3143c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
3144c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    snprintf(buffer, SIZE, "%s         %02d         %02d         ",
3145c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent             mCanBeMuted ? "true " : "false", mIndexMin, mIndexMax);
3146c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    result.append(buffer);
3147c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    for (size_t i = 0; i < mIndexCur.size(); i++) {
3148c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        snprintf(buffer, SIZE, "%04x : %02d, ",
3149c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                 mIndexCur.keyAt(i),
3150c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                 mIndexCur.valueAt(i));
3151c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        result.append(buffer);
3152c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    }
3153c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    result.append("\n");
3154c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
3155c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    write(fd, result.string(), result.size());
3156f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3157f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3158f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// --- EffectDescriptor class implementation
3159f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3160f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::EffectDescriptor::dump(int fd)
3161f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3162f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    const size_t SIZE = 256;
3163f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    char buffer[SIZE];
3164f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    String8 result;
3165f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
31661c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent    snprintf(buffer, SIZE, " I/O: %d\n", mIo);
3167f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3168f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Strategy: %d\n", mStrategy);
3169f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3170f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Session: %d\n", mSession);
3171f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3172f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Name: %s\n",  mDesc.name);
3173f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3174582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    snprintf(buffer, SIZE, " %s\n",  mEnabled ? "Enabled" : "Disabled");
3175582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    result.append(buffer);
3176f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, result.string(), result.size());
3177f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3178f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
3179f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3180f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
31815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent// --- IOProfile class implementation
31825ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
318370c236c9290732782d5267935af1475b8d5ae602Eric LaurentAudioPolicyManagerBase::HwModule::HwModule(const char *name)
318470c236c9290732782d5267935af1475b8d5ae602Eric Laurent    : mName(strndup(name, AUDIO_HARDWARE_MODULE_ID_MAX_LEN)), mHandle(0)
318570c236c9290732782d5267935af1475b8d5ae602Eric Laurent{
318670c236c9290732782d5267935af1475b8d5ae602Eric Laurent}
318770c236c9290732782d5267935af1475b8d5ae602Eric Laurent
318870c236c9290732782d5267935af1475b8d5ae602Eric LaurentAudioPolicyManagerBase::HwModule::~HwModule()
318970c236c9290732782d5267935af1475b8d5ae602Eric Laurent{
319070c236c9290732782d5267935af1475b8d5ae602Eric Laurent    for (size_t i = 0; i < mOutputProfiles.size(); i++) {
319170c236c9290732782d5267935af1475b8d5ae602Eric Laurent         delete mOutputProfiles[i];
319270c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
319370c236c9290732782d5267935af1475b8d5ae602Eric Laurent    for (size_t i = 0; i < mInputProfiles.size(); i++) {
319470c236c9290732782d5267935af1475b8d5ae602Eric Laurent         delete mInputProfiles[i];
319570c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
319670c236c9290732782d5267935af1475b8d5ae602Eric Laurent    free((void *)mName);
319770c236c9290732782d5267935af1475b8d5ae602Eric Laurent}
319870c236c9290732782d5267935af1475b8d5ae602Eric Laurent
319970c236c9290732782d5267935af1475b8d5ae602Eric Laurentvoid AudioPolicyManagerBase::HwModule::dump(int fd)
320070c236c9290732782d5267935af1475b8d5ae602Eric Laurent{
320170c236c9290732782d5267935af1475b8d5ae602Eric Laurent    const size_t SIZE = 256;
320270c236c9290732782d5267935af1475b8d5ae602Eric Laurent    char buffer[SIZE];
320370c236c9290732782d5267935af1475b8d5ae602Eric Laurent    String8 result;
320470c236c9290732782d5267935af1475b8d5ae602Eric Laurent
320570c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "  - name: %s\n", mName);
320670c236c9290732782d5267935af1475b8d5ae602Eric Laurent    result.append(buffer);
320770c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "  - handle: %d\n", mHandle);
320870c236c9290732782d5267935af1475b8d5ae602Eric Laurent    result.append(buffer);
320970c236c9290732782d5267935af1475b8d5ae602Eric Laurent    write(fd, result.string(), result.size());
321070c236c9290732782d5267935af1475b8d5ae602Eric Laurent    if (mOutputProfiles.size()) {
321170c236c9290732782d5267935af1475b8d5ae602Eric Laurent        write(fd, "  - outputs:\n", sizeof("  - outputs:\n"));
321270c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (size_t i = 0; i < mOutputProfiles.size(); i++) {
3213599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            snprintf(buffer, SIZE, "    output %d:\n", i);
3214599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            write(fd, buffer, strlen(buffer));
321570c236c9290732782d5267935af1475b8d5ae602Eric Laurent            mOutputProfiles[i]->dump(fd);
321670c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
321770c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
321870c236c9290732782d5267935af1475b8d5ae602Eric Laurent    if (mInputProfiles.size()) {
321970c236c9290732782d5267935af1475b8d5ae602Eric Laurent        write(fd, "  - inputs:\n", sizeof("  - inputs:\n"));
322070c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (size_t i = 0; i < mInputProfiles.size(); i++) {
3221599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            snprintf(buffer, SIZE, "    input %d:\n", i);
3222599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            write(fd, buffer, strlen(buffer));
322370c236c9290732782d5267935af1475b8d5ae602Eric Laurent            mInputProfiles[i]->dump(fd);
322470c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
322570c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
322670c236c9290732782d5267935af1475b8d5ae602Eric Laurent}
322770c236c9290732782d5267935af1475b8d5ae602Eric Laurent
322870c236c9290732782d5267935af1475b8d5ae602Eric LaurentAudioPolicyManagerBase::IOProfile::IOProfile(HwModule *module)
32290977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent    : mFlags((audio_output_flags_t)0), mModule(module)
32305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
32315ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
32325ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
32335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric LaurentAudioPolicyManagerBase::IOProfile::~IOProfile()
32345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
323570c236c9290732782d5267935af1475b8d5ae602Eric Laurent}
323670c236c9290732782d5267935af1475b8d5ae602Eric Laurent
323770c236c9290732782d5267935af1475b8d5ae602Eric Laurent// checks if the IO profile is compatible with specified parameters. By convention a value of 0
323870c236c9290732782d5267935af1475b8d5ae602Eric Laurent// means a parameter is don't care
323970c236c9290732782d5267935af1475b8d5ae602Eric Laurentbool AudioPolicyManagerBase::IOProfile::isCompatibleProfile(audio_devices_t device,
324070c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                            uint32_t samplingRate,
324170c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                            uint32_t format,
324270c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                            uint32_t channelMask,
32430977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent                                                            audio_output_flags_t flags) const
324470c236c9290732782d5267935af1475b8d5ae602Eric Laurent{
324570c236c9290732782d5267935af1475b8d5ae602Eric Laurent    if ((mSupportedDevices & device) != device) {
324670c236c9290732782d5267935af1475b8d5ae602Eric Laurent        return false;
324770c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
324870c236c9290732782d5267935af1475b8d5ae602Eric Laurent    if ((mFlags & flags) != flags) {
324970c236c9290732782d5267935af1475b8d5ae602Eric Laurent        return false;
325070c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
325170c236c9290732782d5267935af1475b8d5ae602Eric Laurent    if (samplingRate != 0) {
325270c236c9290732782d5267935af1475b8d5ae602Eric Laurent        size_t i;
325370c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (i = 0; i < mSamplingRates.size(); i++)
325470c236c9290732782d5267935af1475b8d5ae602Eric Laurent        {
325570c236c9290732782d5267935af1475b8d5ae602Eric Laurent            if (mSamplingRates[i] == samplingRate) {
325670c236c9290732782d5267935af1475b8d5ae602Eric Laurent                break;
325770c236c9290732782d5267935af1475b8d5ae602Eric Laurent            }
325870c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
325970c236c9290732782d5267935af1475b8d5ae602Eric Laurent        if (i == mSamplingRates.size()) {
326070c236c9290732782d5267935af1475b8d5ae602Eric Laurent            return false;
326170c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
326270c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
326370c236c9290732782d5267935af1475b8d5ae602Eric Laurent    if (format != 0) {
326470c236c9290732782d5267935af1475b8d5ae602Eric Laurent        size_t i;
326570c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (i = 0; i < mFormats.size(); i++)
326670c236c9290732782d5267935af1475b8d5ae602Eric Laurent        {
326770c236c9290732782d5267935af1475b8d5ae602Eric Laurent            if (mFormats[i] == format) {
326870c236c9290732782d5267935af1475b8d5ae602Eric Laurent                break;
326970c236c9290732782d5267935af1475b8d5ae602Eric Laurent            }
327070c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
327170c236c9290732782d5267935af1475b8d5ae602Eric Laurent        if (i == mFormats.size()) {
327270c236c9290732782d5267935af1475b8d5ae602Eric Laurent            return false;
327370c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
327470c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
327570c236c9290732782d5267935af1475b8d5ae602Eric Laurent    if (channelMask != 0) {
327670c236c9290732782d5267935af1475b8d5ae602Eric Laurent        size_t i;
327770c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (i = 0; i < mChannelMasks.size(); i++)
327870c236c9290732782d5267935af1475b8d5ae602Eric Laurent        {
327970c236c9290732782d5267935af1475b8d5ae602Eric Laurent            if (mChannelMasks[i] == channelMask) {
328070c236c9290732782d5267935af1475b8d5ae602Eric Laurent                break;
328170c236c9290732782d5267935af1475b8d5ae602Eric Laurent            }
328270c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
328370c236c9290732782d5267935af1475b8d5ae602Eric Laurent        if (i == mChannelMasks.size()) {
328470c236c9290732782d5267935af1475b8d5ae602Eric Laurent            return false;
328570c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
328670c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
328770c236c9290732782d5267935af1475b8d5ae602Eric Laurent    return true;
32885ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
32895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
32905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::IOProfile::dump(int fd)
32915ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
32925ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    const size_t SIZE = 256;
32935ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    char buffer[SIZE];
32945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    String8 result;
32955ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
329670c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "    - sampling rates: ");
32975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    result.append(buffer);
32985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < mSamplingRates.size(); i++) {
32995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        snprintf(buffer, SIZE, "%d", mSamplingRates[i]);
33005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        result.append(buffer);
33015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        result.append(i == (mSamplingRates.size() - 1) ? "\n" : ", ");
33025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
33035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
330470c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "    - channel masks: ");
33055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    result.append(buffer);
33065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < mChannelMasks.size(); i++) {
33075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        snprintf(buffer, SIZE, "%04x", mChannelMasks[i]);
33085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        result.append(buffer);
33095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        result.append(i == (mChannelMasks.size() - 1) ? "\n" : ", ");
33105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
33115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
331270c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "    - formats: ");
33135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    result.append(buffer);
33145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < mFormats.size(); i++) {
33155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        snprintf(buffer, SIZE, "%d", mFormats[i]);
33165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        result.append(buffer);
33175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        result.append(i == (mFormats.size() - 1) ? "\n" : ", ");
33185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
33195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
332070c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "    - devices: %04x\n", mSupportedDevices);
33215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    result.append(buffer);
332270c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "    - flags: %04x\n", mFlags);
33235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    result.append(buffer);
33245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
33255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    write(fd, result.string(), result.size());
33265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
33275ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
33285ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent// --- audio_policy.conf file parsing
33295ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
33305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentstruct StringToEnum {
33315ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    const char *name;
33325ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    uint32_t value;
33335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent};
33345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
33355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent#define STRING_TO_ENUM(string) { #string, string }
33365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
33375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
33385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentconst struct StringToEnum sDeviceNameToEnumTable[] = {
33395ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_EARPIECE),
33405ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_SPEAKER),
33415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_WIRED_HEADSET),
33425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_WIRED_HEADPHONE),
33435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_ALL_SCO),
33445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_ALL_A2DP),
33455ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_AUX_DIGITAL),
33465ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET),
3347599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET),
3348599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_USB_DEVICE),
3349599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_USB_ACCESSORY),
3350599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_ALL_USB),
335148387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi    STRING_TO_ENUM(AUDIO_DEVICE_OUT_REMOTE_SUBMIX),
33525ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_BUILTIN_MIC),
33535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET),
33545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_WIRED_HEADSET),
33555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_AUX_DIGITAL),
33565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_VOICE_CALL),
33575ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_BACK_MIC),
335848387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi    STRING_TO_ENUM(AUDIO_DEVICE_IN_REMOTE_SUBMIX),
3359ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET),
3360ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_DGTL_DOCK_HEADSET),
3361ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_USB_ACCESSORY),
33625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent};
33635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
33645ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentconst struct StringToEnum sFlagNameToEnumTable[] = {
33650977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent    STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT),
33660977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent    STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY),
3367b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent    STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST),
3368b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent    STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER),
33695ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent};
33705ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
33715ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentconst struct StringToEnum sFormatNameToEnumTable[] = {
33725ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
33735ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
33745ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_FORMAT_MP3),
33755ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_FORMAT_AAC),
33765ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
33775ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent};
33785ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
33795ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentconst struct StringToEnum sOutChannelsNameToEnumTable[] = {
33805ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_OUT_MONO),
33815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
33825ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
33835ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
33845ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent};
33855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
33865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentconst struct StringToEnum sInChannelsNameToEnumTable[] = {
33875ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_IN_MONO),
33885ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_IN_STEREO),
33895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent};
33905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
33915ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
33925ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentuint32_t AudioPolicyManagerBase::stringToEnum(const struct StringToEnum *table,
33935ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                              size_t size,
33945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                              const char *name)
33955ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
33965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < size; i++) {
33975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strcmp(table[i].name, name) == 0) {
33985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("stringToEnum() found %s", table[i].name);
33995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            return table[i].value;
34005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
34015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
34025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return 0;
34035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
34045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
34050977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurentaudio_output_flags_t AudioPolicyManagerBase::parseFlagNames(char *name)
34065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
34075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    uint32_t flag = 0;
34085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
34095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // it is OK to cast name to non const here as we are not going to use it after
34105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // strtok() modifies it
34115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    char *flagName = strtok(name, "|");
34125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (flagName != NULL) {
34135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strlen(flagName) != 0) {
34145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            flag |= stringToEnum(sFlagNameToEnumTable,
34155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                               ARRAY_SIZE(sFlagNameToEnumTable),
34165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                               flagName);
34175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
34185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        flagName = strtok(NULL, "|");
34195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
34200977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent    return (audio_output_flags_t)flag;
34215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
34225ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
34235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentaudio_devices_t AudioPolicyManagerBase::parseDeviceNames(char *name)
34245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
34255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    uint32_t device = 0;
34265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
34275ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    char *devName = strtok(name, "|");
34285ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (devName != NULL) {
34295ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strlen(devName) != 0) {
34305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device |= stringToEnum(sDeviceNameToEnumTable,
34315ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                 ARRAY_SIZE(sDeviceNameToEnumTable),
34325ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                 devName);
34335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
34345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        devName = strtok(NULL, "|");
34355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
3436c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    return device;
34375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
34385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
34395ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadSamplingRates(char *name, IOProfile *profile)
34405ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
34415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    char *str = strtok(name, "|");
34425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
34433cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    // by convention, "0' in the first entry in mSamplingRates indicates the supported sampling
34443cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    // rates should be read from the output stream after it is opened for the first time
34453cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) {
34463cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        profile->mSamplingRates.add(0);
34473cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        return;
34483cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    }
34493cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
34505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (str != NULL) {
34515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        uint32_t rate = atoi(str);
34525ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (rate != 0) {
34535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadSamplingRates() adding rate %d", rate);
34545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mSamplingRates.add(rate);
34555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
34565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        str = strtok(NULL, "|");
34575ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
34585ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return;
34595ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
34605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
34615ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadFormats(char *name, IOProfile *profile)
34625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
34635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    char *str = strtok(name, "|");
34645ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
34653cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    // by convention, "0' in the first entry in mFormats indicates the supported formats
34663cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    // should be read from the output stream after it is opened for the first time
34673cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) {
34683cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        profile->mFormats.add((audio_format_t)0);
34693cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        return;
34703cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    }
34713cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
34725ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (str != NULL) {
34735ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_format_t format = (audio_format_t)stringToEnum(sFormatNameToEnumTable,
34745ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                             ARRAY_SIZE(sFormatNameToEnumTable),
34755ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                             str);
34765ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (format != 0) {
34775ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mFormats.add(format);
34785ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
34795ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        str = strtok(NULL, "|");
34805ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
34815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return;
34825ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
34835ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
34845ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadInChannels(char *name, IOProfile *profile)
34855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
34865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    const char *str = strtok(name, "|");
34875ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
34885ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGV("loadInChannels() %s", name);
34893cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
34903cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) {
34913cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        profile->mChannelMasks.add((audio_channel_mask_t)0);
34923cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        return;
34933cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    }
34943cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
34955ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (str != NULL) {
34965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_channel_mask_t channelMask =
34975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                (audio_channel_mask_t)stringToEnum(sInChannelsNameToEnumTable,
34985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                   ARRAY_SIZE(sInChannelsNameToEnumTable),
34995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                   str);
35005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (channelMask != 0) {
35015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadInChannels() adding channelMask %04x", channelMask);
35025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mChannelMasks.add(channelMask);
35035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
35045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        str = strtok(NULL, "|");
35055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
35065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return;
35075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
35085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
35095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadOutChannels(char *name, IOProfile *profile)
35105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
35115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    const char *str = strtok(name, "|");
35123cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
35133cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGV("loadOutChannels() %s", name);
35143cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
35153cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    // by convention, "0' in the first entry in mChannelMasks indicates the supported channel
35163cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    // masks should be read from the output stream after it is opened for the first time
35173cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) {
35183cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        profile->mChannelMasks.add((audio_channel_mask_t)0);
35193cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        return;
35203cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    }
35215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
35225ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (str != NULL) {
35233cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        audio_channel_mask_t channelMask =
35243cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                (audio_channel_mask_t)stringToEnum(sOutChannelsNameToEnumTable,
35253cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                   ARRAY_SIZE(sOutChannelsNameToEnumTable),
35263cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                   str);
35275ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (channelMask != 0) {
35285ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mChannelMasks.add(channelMask);
35295ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
35305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        str = strtok(NULL, "|");
35315ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
35325ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return;
35335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
35345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
353570c236c9290732782d5267935af1475b8d5ae602Eric Laurentstatus_t AudioPolicyManagerBase::loadInput(cnode *root, HwModule *module)
35365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
35375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    cnode *node = root->first_child;
35385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
35395ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    IOProfile *profile = new IOProfile(module);
35405ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
35415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (node) {
35425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
35435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            loadSamplingRates((char *)node->value, profile);
35445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, FORMATS_TAG) == 0) {
35455ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            loadFormats((char *)node->value, profile);
35465ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, CHANNELS_TAG) == 0) {
35475ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            loadInChannels((char *)node->value, profile);
35485ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, DEVICES_TAG) == 0) {
35495ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mSupportedDevices = parseDeviceNames((char *)node->value);
35505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
35515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        node = node->next;
35525ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
3553ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    ALOGW_IF(profile->mSupportedDevices == AUDIO_DEVICE_NONE,
35545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadInput() invalid supported devices");
35555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGW_IF(profile->mChannelMasks.size() == 0,
35565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadInput() invalid supported channel masks");
35575ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGW_IF(profile->mSamplingRates.size() == 0,
35585ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadInput() invalid supported sampling rates");
35595ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGW_IF(profile->mFormats.size() == 0,
35605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadInput() invalid supported formats");
3561ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if ((profile->mSupportedDevices != AUDIO_DEVICE_NONE) &&
35625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            (profile->mChannelMasks.size() != 0) &&
35635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            (profile->mSamplingRates.size() != 0) &&
35645ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            (profile->mFormats.size() != 0)) {
35655ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
35665ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        ALOGV("loadInput() adding input mSupportedDevices %04x", profile->mSupportedDevices);
35675ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
356870c236c9290732782d5267935af1475b8d5ae602Eric Laurent        module->mInputProfiles.add(profile);
35695ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return NO_ERROR;
35705ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    } else {
35715ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        delete profile;
35725ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return BAD_VALUE;
35735ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
35745ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
35755ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
357670c236c9290732782d5267935af1475b8d5ae602Eric Laurentstatus_t AudioPolicyManagerBase::loadOutput(cnode *root, HwModule *module)
35775ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
35785ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    cnode *node = root->first_child;
35795ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
35805ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    IOProfile *profile = new IOProfile(module);
35815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
35825ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (node) {
35835ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
35845ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            loadSamplingRates((char *)node->value, profile);
35855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, FORMATS_TAG) == 0) {
35865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            loadFormats((char *)node->value, profile);
35875ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, CHANNELS_TAG) == 0) {
35885ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            loadOutChannels((char *)node->value, profile);
35895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, DEVICES_TAG) == 0) {
35905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mSupportedDevices = parseDeviceNames((char *)node->value);
35915ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, FLAGS_TAG) == 0) {
35925ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mFlags = parseFlagNames((char *)node->value);
35935ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
35945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        node = node->next;
35955ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
3596ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    ALOGW_IF(profile->mSupportedDevices == AUDIO_DEVICE_NONE,
35975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadOutput() invalid supported devices");
35985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGW_IF(profile->mChannelMasks.size() == 0,
35995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadOutput() invalid supported channel masks");
36005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGW_IF(profile->mSamplingRates.size() == 0,
36015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadOutput() invalid supported sampling rates");
36025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGW_IF(profile->mFormats.size() == 0,
36035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadOutput() invalid supported formats");
3604ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if ((profile->mSupportedDevices != AUDIO_DEVICE_NONE) &&
36055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            (profile->mChannelMasks.size() != 0) &&
36065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            (profile->mSamplingRates.size() != 0) &&
36075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            (profile->mFormats.size() != 0)) {
36085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
36095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        ALOGV("loadOutput() adding output mSupportedDevices %04x, mFlags %04x",
36105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent              profile->mSupportedDevices, profile->mFlags);
36115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
361270c236c9290732782d5267935af1475b8d5ae602Eric Laurent        module->mOutputProfiles.add(profile);
36135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return NO_ERROR;
36145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    } else {
36155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        delete profile;
36165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return BAD_VALUE;
36175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
36185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
36195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
36205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadHwModule(cnode *root)
36215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
36225ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    cnode *node = config_find(root, OUTPUTS_TAG);
36235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    status_t status = NAME_NOT_FOUND;
362470c236c9290732782d5267935af1475b8d5ae602Eric Laurent
362570c236c9290732782d5267935af1475b8d5ae602Eric Laurent    HwModule *module = new HwModule(root->name);
362670c236c9290732782d5267935af1475b8d5ae602Eric Laurent
36275ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (node != NULL) {
36285ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strcmp(root->name, AUDIO_HARDWARE_MODULE_ID_A2DP) == 0) {
36295ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            mHasA2dp = true;
3630599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent        } else if (strcmp(root->name, AUDIO_HARDWARE_MODULE_ID_USB) == 0) {
3631599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            mHasUsb = true;
363248387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi        } else if (strcmp(root->name, AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX) == 0) {
363348387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            mHasRemoteSubmix = true;
36345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
3635599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent
36365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        node = node->first_child;
36375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        while (node) {
36385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadHwModule() loading output %s", node->name);
363970c236c9290732782d5267935af1475b8d5ae602Eric Laurent            status_t tmpStatus = loadOutput(node, module);
36405ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            if (status == NAME_NOT_FOUND || status == NO_ERROR) {
36415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                status = tmpStatus;
36425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
36435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            node = node->next;
36445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
36455ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
36465ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    node = config_find(root, INPUTS_TAG);
36475ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (node != NULL) {
36485ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        node = node->first_child;
36495ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        while (node) {
36505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadHwModule() loading input %s", node->name);
365170c236c9290732782d5267935af1475b8d5ae602Eric Laurent            status_t tmpStatus = loadInput(node, module);
36525ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            if (status == NAME_NOT_FOUND || status == NO_ERROR) {
36535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                status = tmpStatus;
36545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
36555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            node = node->next;
36565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
36575ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
36585ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (status == NO_ERROR) {
365970c236c9290732782d5267935af1475b8d5ae602Eric Laurent        mHwModules.add(module);
366070c236c9290732782d5267935af1475b8d5ae602Eric Laurent    } else {
366170c236c9290732782d5267935af1475b8d5ae602Eric Laurent        delete module;
36625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
36635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
36645ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
36655ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadHwModules(cnode *root)
36665ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
36675ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    cnode *node = config_find(root, AUDIO_HW_MODULE_TAG);
36685ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (node == NULL) {
36695ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return;
36705ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
36715ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
36725ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    node = node->first_child;
36735ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (node) {
36745ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        ALOGV("loadHwModules() loading module %s", node->name);
36755ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        loadHwModule(node);
36765ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        node = node->next;
36775ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
36785ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
36795ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
36805ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadGlobalConfig(cnode *root)
36815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
36825ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    cnode *node = config_find(root, GLOBAL_CONFIG_TAG);
36835ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (node == NULL) {
36845ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return;
36855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
36865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    node = node->first_child;
36875ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (node) {
36885ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strcmp(ATTACHED_OUTPUT_DEVICES_TAG, node->name) == 0) {
36895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            mAttachedOutputDevices = parseDeviceNames((char *)node->value);
3690ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            ALOGW_IF(mAttachedOutputDevices == AUDIO_DEVICE_NONE,
3691c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                    "loadGlobalConfig() no attached output devices");
36925ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadGlobalConfig() mAttachedOutputDevices %04x", mAttachedOutputDevices);
36935ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(DEFAULT_OUTPUT_DEVICE_TAG, node->name) == 0) {
36945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            mDefaultOutputDevice = (audio_devices_t)stringToEnum(sDeviceNameToEnumTable,
36955ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                              ARRAY_SIZE(sDeviceNameToEnumTable),
36965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                              (char *)node->value);
3697ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            ALOGW_IF(mDefaultOutputDevice == AUDIO_DEVICE_NONE,
3698c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                    "loadGlobalConfig() default device not specified");
36995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadGlobalConfig() mDefaultOutputDevice %04x", mDefaultOutputDevice);
37005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(ATTACHED_INPUT_DEVICES_TAG, node->name) == 0) {
3701ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            mAvailableInputDevices = parseDeviceNames((char *)node->value) & ~AUDIO_DEVICE_BIT_IN;
37025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadGlobalConfig() mAvailableInputDevices %04x", mAvailableInputDevices);
37035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
37045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        node = node->next;
37055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
37065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
37075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
37085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentstatus_t AudioPolicyManagerBase::loadAudioPolicyConfig(const char *path)
37095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
37105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    cnode *root;
37115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    char *data;
37125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
37135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    data = (char *)load_file(path, NULL);
37145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (data == NULL) {
37155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return -ENODEV;
37165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
37175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    root = config_node("", "");
37185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    config_load(root, data);
37195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
37205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    loadGlobalConfig(root);
37215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    loadHwModules(root);
37225ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
37235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    config_free(root);
37245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    free(root);
37255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    free(data);
37265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
37275ec145df7708564d385fd3fb764085321cf4c253Dima Zavin    ALOGI("loadAudioPolicyConfig() loaded %s\n", path);
37285ec145df7708564d385fd3fb764085321cf4c253Dima Zavin
37295ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return NO_ERROR;
37305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
3731f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3732739022f26a7127ba76a98dda65411496086114a7Dima Zavinvoid AudioPolicyManagerBase::defaultAudioPolicyConfig(void)
3733739022f26a7127ba76a98dda65411496086114a7Dima Zavin{
3734739022f26a7127ba76a98dda65411496086114a7Dima Zavin    HwModule *module;
3735739022f26a7127ba76a98dda65411496086114a7Dima Zavin    IOProfile *profile;
3736739022f26a7127ba76a98dda65411496086114a7Dima Zavin
3737739022f26a7127ba76a98dda65411496086114a7Dima Zavin    mDefaultOutputDevice = AUDIO_DEVICE_OUT_SPEAKER;
3738739022f26a7127ba76a98dda65411496086114a7Dima Zavin    mAttachedOutputDevices = AUDIO_DEVICE_OUT_SPEAKER;
3739ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    mAvailableInputDevices = AUDIO_DEVICE_IN_BUILTIN_MIC & ~AUDIO_DEVICE_BIT_IN;
3740739022f26a7127ba76a98dda65411496086114a7Dima Zavin
3741739022f26a7127ba76a98dda65411496086114a7Dima Zavin    module = new HwModule("primary");
3742739022f26a7127ba76a98dda65411496086114a7Dima Zavin
3743739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile = new IOProfile(module);
3744739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mSamplingRates.add(44100);
3745739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
3746739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mChannelMasks.add(AUDIO_CHANNEL_OUT_STEREO);
3747739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mSupportedDevices = AUDIO_DEVICE_OUT_SPEAKER;
3748739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mFlags = AUDIO_OUTPUT_FLAG_PRIMARY;
3749739022f26a7127ba76a98dda65411496086114a7Dima Zavin    module->mOutputProfiles.add(profile);
3750739022f26a7127ba76a98dda65411496086114a7Dima Zavin
3751739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile = new IOProfile(module);
3752739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mSamplingRates.add(8000);
3753739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
3754739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mChannelMasks.add(AUDIO_CHANNEL_IN_MONO);
3755739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mSupportedDevices = AUDIO_DEVICE_IN_BUILTIN_MIC;
3756739022f26a7127ba76a98dda65411496086114a7Dima Zavin    module->mInputProfiles.add(profile);
3757739022f26a7127ba76a98dda65411496086114a7Dima Zavin
3758739022f26a7127ba76a98dda65411496086114a7Dima Zavin    mHwModules.add(module);
3759739022f26a7127ba76a98dda65411496086114a7Dima Zavin}
3760f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3761f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}; // namespace android
3762