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 Lauren