AudioPolicyManagerBase.cpp revision fc9b2457ce4721538dca0903de63c7766e97498a
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"
184e57a1d4979c2644dd8999dc5759b10ed0af56e9Eric Laurent//#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
30dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi// A device mask for all audio output devices that are considered "remote" when evaluating
31dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi// active output devices in isStreamActiveRemotely()
32dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi#define APM_AUDIO_OUT_DEVICE_REMOTE_ALL  AUDIO_DEVICE_OUT_REMOTE_SUBMIX
336d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi
34f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#include <utils/Log.h>
35f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#include <hardware_legacy/AudioPolicyManagerBase.h>
361c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent#include <hardware/audio_effect.h>
3708b014d9e509c9163db6b33a63852e73db4d07ccEric Laurent#include <hardware/audio.h>
38f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#include <math.h>
395ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent#include <hardware_legacy/audio_policy_conf.h>
40a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald#include <cutils/properties.h>
41f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
42e81531e91ecae92aff471dbff9cbeb0f95ff4a80Dima Zavinnamespace android_audio_legacy {
43f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
44f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// ----------------------------------------------------------------------------
45f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// AudioPolicyInterface implementation
46f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// ----------------------------------------------------------------------------
47f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
48f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
49c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivistatus_t AudioPolicyManagerBase::setDeviceConnectionState(audio_devices_t device,
50f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                  AudioSystem::device_connection_state state,
51f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                  const char *device_address)
52f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
533cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    SortedVector <audio_io_handle_t> outputs;
54f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
556a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("setDeviceConnectionState() device: %x, state %d, address %s", device, state, device_address);
56f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
57f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // connect/disconnect only 1 device at a time
58ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
59f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
60f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (strlen(device_address) >= MAX_DEVICE_ADDRESS_LEN) {
615efbd421e0029d6fc44b1cc65c0e5e0d85e5161fSteve Block        ALOGE("setDeviceConnectionState() invalid address: %s", device_address);
62f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
63f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
64f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
65f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // handle output devices
66c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    if (audio_is_output_device(device)) {
67f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
68c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        if (!mHasA2dp && audio_is_a2dp_device(device)) {
6948387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            ALOGE("setDeviceConnectionState() invalid A2DP device: %x", device);
70f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return BAD_VALUE;
71f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
72c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        if (!mHasUsb && audio_is_usb_device(device)) {
7348387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            ALOGE("setDeviceConnectionState() invalid USB audio device: %x", device);
7448387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            return BAD_VALUE;
7548387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi        }
7648387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi        if (!mHasRemoteSubmix && audio_is_remote_submix_device((audio_devices_t)device)) {
7748387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            ALOGE("setDeviceConnectionState() invalid remote submix audio device: %x", device);
78599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            return BAD_VALUE;
79599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent        }
80f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
81c952527e6f89d5427881462823514be9d79f13e6Eric Laurent        // save a copy of the opened output descriptors before any output is opened or closed
82c952527e6f89d5427881462823514be9d79f13e6Eric Laurent        // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies()
83c952527e6f89d5427881462823514be9d79f13e6Eric Laurent        mPreviousOutputs = mOutputs;
84f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        switch (state)
85f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        {
86f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // handle output device connection
87f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        case AudioSystem::DEVICE_STATE_AVAILABLE:
88f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (mAvailableOutputDevices & device) {
8964cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block                ALOGW("setDeviceConnectionState() device already connected: %x", device);
90f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                return INVALID_OPERATION;
91f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
926a70518b93928d1c91457ff805e375c82d76b0e5Steve Block            ALOGV("setDeviceConnectionState() connecting device %x", device);
93f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
94c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            if (checkOutputsForDevice(device, state, outputs) != NO_ERROR) {
953cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                return INVALID_OPERATION;
963cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
973cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %d outputs",
983cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                  outputs.size());
99f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // register new device as available
100f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent            mAvailableOutputDevices = (audio_devices_t)(mAvailableOutputDevices | device);
101f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1023cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            if (!outputs.isEmpty()) {
1033cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                String8 paramStr;
104c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                if (mHasA2dp && audio_is_a2dp_device(device)) {
1053cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    // handle A2DP device connection
1063cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    AudioParameter param;
1073cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    param.add(String8(AUDIO_PARAMETER_A2DP_SINK_ADDRESS), String8(device_address));
1083cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    paramStr = param.toString();
1093cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    mA2dpDeviceAddress = String8(device_address, MAX_DEVICE_ADDRESS_LEN);
1103cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    mA2dpSuspended = false;
111c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                } else if (audio_is_bluetooth_sco_device(device)) {
1123cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    // handle SCO device connection
1133cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    mScoDeviceAddress = String8(device_address, MAX_DEVICE_ADDRESS_LEN);
114c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                } else if (mHasUsb && audio_is_usb_device(device)) {
1153cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    // handle USB device connection
1163cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    mUsbCardAndDevice = String8(device_address, MAX_DEVICE_ADDRESS_LEN);
1173cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    paramStr = mUsbCardAndDevice;
1183cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                }
11948387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi                // not currently handling multiple simultaneous submixes: ignoring remote submix
12048387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi                //   case and address
1213cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                if (!paramStr.isEmpty()) {
1223cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    for (size_t i = 0; i < outputs.size(); i++) {
1233cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        mpClientInterface->setParameters(outputs[i], paramStr);
1243cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
1253cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                }
126f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
127f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            break;
128f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // handle output device disconnection
129f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        case AudioSystem::DEVICE_STATE_UNAVAILABLE: {
130f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (!(mAvailableOutputDevices & device)) {
13164cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block                ALOGW("setDeviceConnectionState() device not connected: %x", device);
132f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                return INVALID_OPERATION;
133f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
134f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1356a70518b93928d1c91457ff805e375c82d76b0e5Steve Block            ALOGV("setDeviceConnectionState() disconnecting device %x", device);
136f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // remove device from available output devices
137f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent            mAvailableOutputDevices = (audio_devices_t)(mAvailableOutputDevices & ~device);
138f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
139c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            checkOutputsForDevice(device, state, outputs);
140c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            if (mHasA2dp && audio_is_a2dp_device(device)) {
1413cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                // handle A2DP device disconnection
142b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                mA2dpDeviceAddress = "";
143b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                mA2dpSuspended = false;
144c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            } else if (audio_is_bluetooth_sco_device(device)) {
1453cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                // handle SCO device disconnection
146b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                mScoDeviceAddress = "";
147c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            } else if (mHasUsb && audio_is_usb_device(device)) {
1483cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                // handle USB device disconnection
149599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent                mUsbCardAndDevice = "";
150f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
15148387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            // not currently handling multiple simultaneous submixes: ignoring remote submix
15248387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            //   case and address
153f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            } break;
154f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
155f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        default:
1565efbd421e0029d6fc44b1cc65c0e5e0d85e5161fSteve Block            ALOGE("setDeviceConnectionState() invalid state: %x", state);
157f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return BAD_VALUE;
158f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
159f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
160f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        checkA2dpSuspend();
161f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        checkOutputForAllStrategies();
162b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        // outputs must be closed after checkOutputForAllStrategies() is executed
1633cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        if (!outputs.isEmpty()) {
1643cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            for (size_t i = 0; i < outputs.size(); i++) {
165a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                AudioOutputDescriptor *desc = mOutputs.valueFor(outputs[i]);
1663cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                // close unused outputs after device disconnection or direct outputs that have been
1673cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                // opened by checkOutputsForDevice() to query dynamic parameters
1683cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                if ((state == AudioSystem::DEVICE_STATE_UNAVAILABLE) ||
169a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                        (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) &&
170a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                         (desc->mDirectOpenCount == 0))) {
1713cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    closeOutput(outputs[i]);
1723cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                }
1733cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
174f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1755ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
176c952527e6f89d5427881462823514be9d79f13e6Eric Laurent        updateDevicesAndOutputs();
1775ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        for (size_t i = 0; i < mOutputs.size(); i++) {
1782c72e9faa5ad5e324b85b78e383dd85c8bdc04a9Eric Laurent            // do not force device change on duplicated output because if device is 0, it will
1792c72e9faa5ad5e324b85b78e383dd85c8bdc04a9Eric Laurent            // also force a device 0 for the two outputs it is duplicated to which may override
1802c72e9faa5ad5e324b85b78e383dd85c8bdc04a9Eric Laurent            // a valid device selection on those outputs.
18176e97d3950f2654adbb0a415218b6d048200c395Eric Laurent            setOutputDevice(mOutputs.keyAt(i),
18276e97d3950f2654adbb0a415218b6d048200c395Eric Laurent                            getNewDevice(mOutputs.keyAt(i), true /*fromCache*/),
1832c72e9faa5ad5e324b85b78e383dd85c8bdc04a9Eric Laurent                            !mOutputs.valueAt(i)->isDuplicated(),
18476e97d3950f2654adbb0a415218b6d048200c395Eric Laurent                            0);
1855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
186f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
187c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        if (device == AUDIO_DEVICE_OUT_WIRED_HEADSET) {
188c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = AUDIO_DEVICE_IN_WIRED_HEADSET;
189c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        } else if (device == AUDIO_DEVICE_OUT_BLUETOOTH_SCO ||
190c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                   device == AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET ||
191c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                   device == AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT) {
192c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
193f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        } else {
194f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return NO_ERROR;
195f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
196f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
197f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // handle input devices
198c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    if (audio_is_input_device(device)) {
199f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
200f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        switch (state)
201f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        {
202f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // handle input device connection
203f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        case AudioSystem::DEVICE_STATE_AVAILABLE: {
204f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (mAvailableInputDevices & device) {
20564cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block                ALOGW("setDeviceConnectionState() device already connected: %d", device);
206f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                return INVALID_OPERATION;
207f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
208ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            mAvailableInputDevices = mAvailableInputDevices | (device & ~AUDIO_DEVICE_BIT_IN);
209f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
210f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            break;
211f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
212f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // handle input device disconnection
213f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        case AudioSystem::DEVICE_STATE_UNAVAILABLE: {
214f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (!(mAvailableInputDevices & device)) {
21564cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block                ALOGW("setDeviceConnectionState() device not connected: %d", device);
216f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                return INVALID_OPERATION;
217f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            mAvailableInputDevices = (audio_devices_t) (mAvailableInputDevices & ~device);
219f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            } break;
220f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
221f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        default:
2225efbd421e0029d6fc44b1cc65c0e5e0d85e5161fSteve Block            ALOGE("setDeviceConnectionState() invalid state: %x", state);
223f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return BAD_VALUE;
224f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
225f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
226f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        audio_io_handle_t activeInput = getActiveInput();
227f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (activeInput != 0) {
228f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            AudioInputDescriptor *inputDesc = mInputs.valueFor(activeInput);
229f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent            audio_devices_t newDevice = getDeviceForInputSource(inputDesc->mInputSource);
230ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            if ((newDevice != AUDIO_DEVICE_NONE) && (newDevice != inputDesc->mDevice)) {
2316a70518b93928d1c91457ff805e375c82d76b0e5Steve Block                ALOGV("setDeviceConnectionState() changing device from %x to %x for input %d",
232f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        inputDesc->mDevice, newDevice, activeInput);
233f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                inputDesc->mDevice = newDevice;
234f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                AudioParameter param = AudioParameter();
235f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.addInt(String8(AudioParameter::keyRouting), (int)newDevice);
236f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mpClientInterface->setParameters(activeInput, param.toString());
237f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
238f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
239f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
240f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return NO_ERROR;
241f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
242f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
24364cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block    ALOGW("setDeviceConnectionState() invalid device: %x", device);
244f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return BAD_VALUE;
245f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
246f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
247c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel TriviAudioSystem::device_connection_state AudioPolicyManagerBase::getDeviceConnectionState(audio_devices_t device,
248f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                  const char *device_address)
249f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
250f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioSystem::device_connection_state state = AudioSystem::DEVICE_STATE_UNAVAILABLE;
251f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    String8 address = String8(device_address);
252c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    if (audio_is_output_device(device)) {
253f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (device & mAvailableOutputDevices) {
254c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            if (audio_is_a2dp_device(device) &&
2555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                (!mHasA2dp || (address != "" && mA2dpDeviceAddress != address))) {
256f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                return state;
257f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
258c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            if (audio_is_bluetooth_sco_device(device) &&
259f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                address != "" && mScoDeviceAddress != address) {
260f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                return state;
261f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
262c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            if (audio_is_usb_device(device) &&
263599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent                (!mHasUsb || (address != "" && mUsbCardAndDevice != address))) {
26448387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi                ALOGE("getDeviceConnectionState() invalid device: %x", device);
26548387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi                return state;
26648387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            }
26748387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            if (audio_is_remote_submix_device((audio_devices_t)device) && !mHasRemoteSubmix) {
268599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent                return state;
269599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            }
270f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            state = AudioSystem::DEVICE_STATE_AVAILABLE;
271f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
272c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    } else if (audio_is_input_device(device)) {
273f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (device & mAvailableInputDevices) {
274f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            state = AudioSystem::DEVICE_STATE_AVAILABLE;
275f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
276f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
277f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
278f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return state;
279f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
280f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
281f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::setPhoneState(int state)
282f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2836a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("setPhoneState() state %d", state);
284ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    audio_devices_t newDevice = AUDIO_DEVICE_NONE;
285f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (state < 0 || state >= AudioSystem::NUM_MODES) {
28664cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("setPhoneState() invalid state %d", state);
287f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
288f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
289f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
290f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (state == mPhoneState ) {
29164cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("setPhoneState() setting same state %d", state);
292f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
293f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
294f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
295f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if leaving call state, handle special case of active streams
296f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // pertaining to sonification strategy see handleIncallSonification()
297f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (isInCall()) {
2986a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("setPhoneState() in call state management: new state is %d", state);
299f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
300f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            handleIncallSonification(stream, false, true);
301f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
302f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
303f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
304f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // store previous phone state for management of sonification strategy below
305f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    int oldState = mPhoneState;
306f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mPhoneState = state;
307f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    bool force = false;
308f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
309f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // are we entering or starting a call
310f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (!isStateInCall(oldState) && isStateInCall(state)) {
3116a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("  Entering call in setPhoneState()");
312f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // force routing command to audio hardware when starting a call
313f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // even if no device change is needed
314f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        force = true;
315f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else if (isStateInCall(oldState) && !isStateInCall(state)) {
3166a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("  Exiting call in setPhoneState()");
317f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // force routing command to audio hardware when exiting a call
318f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // even if no device change is needed
319f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        force = true;
320f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else if (isStateInCall(state) && (state != oldState)) {
3216a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("  Switching between telephony and VoIP in setPhoneState()");
322f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // force routing command to audio hardware when switching between telephony and VoIP
323f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // even if no device change is needed
324f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        force = true;
325f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
326f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
327f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // check for device and output changes triggered by new phone state
3285ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    newDevice = getNewDevice(mPrimaryOutput, false /*fromCache*/);
329f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkA2dpSuspend();
330f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForAllStrategies();
331c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    updateDevicesAndOutputs();
332f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
333b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    AudioOutputDescriptor *hwOutputDesc = mOutputs.valueFor(mPrimaryOutput);
334f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
335f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // force routing command to audio hardware when ending call
336f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // even if no device change is needed
337ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if (isStateInCall(oldState) && newDevice == AUDIO_DEVICE_NONE) {
338f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        newDevice = hwOutputDesc->device();
339f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
340f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
341f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    int delayMs = 0;
342772686310a3bc44d93591349e174a60360a3d30dSathishKumar Mani    if (isStateInCall(state)) {
34380f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent        nsecs_t sysTime = systemTime();
344772686310a3bc44d93591349e174a60360a3d30dSathishKumar Mani        for (size_t i = 0; i < mOutputs.size(); i++) {
345772686310a3bc44d93591349e174a60360a3d30dSathishKumar Mani            AudioOutputDescriptor *desc = mOutputs.valueAt(i);
34680f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent            // mute media and sonification strategies and delay device switch by the largest
34780f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent            // latency of any output where either strategy is active.
34880f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent            // This avoid sending the ring tone or music tail into the earpiece or headset.
34980f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent            if ((desc->isStrategyActive(STRATEGY_MEDIA,
35080f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                                     SONIFICATION_HEADSET_MUSIC_DELAY,
35180f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                                     sysTime) ||
35280f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                    desc->isStrategyActive(STRATEGY_SONIFICATION,
35380f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                                         SONIFICATION_HEADSET_MUSIC_DELAY,
35480f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                                         sysTime)) &&
35580f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                    (delayMs < (int)desc->mLatency*2)) {
35680f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                delayMs = desc->mLatency*2;
357772686310a3bc44d93591349e174a60360a3d30dSathishKumar Mani            }
35880f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent            setStrategyMute(STRATEGY_MEDIA, true, mOutputs.keyAt(i));
35980f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent            setStrategyMute(STRATEGY_MEDIA, false, mOutputs.keyAt(i), MUTE_TIME_MS,
36080f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
36180f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent            setStrategyMute(STRATEGY_SONIFICATION, true, mOutputs.keyAt(i));
36280f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent            setStrategyMute(STRATEGY_SONIFICATION, false, mOutputs.keyAt(i), MUTE_TIME_MS,
36380f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
364772686310a3bc44d93591349e174a60360a3d30dSathishKumar Mani        }
365772686310a3bc44d93591349e174a60360a3d30dSathishKumar Mani    }
366772686310a3bc44d93591349e174a60360a3d30dSathishKumar Mani
367f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // change routing is necessary
368b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    setOutputDevice(mPrimaryOutput, newDevice, force, delayMs);
369f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
370f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if entering in call state, handle special case of active streams
371f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // pertaining to sonification strategy see handleIncallSonification()
372f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (isStateInCall(state)) {
3736a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("setPhoneState() in call state management: new state is %d", state);
374f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
375f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            handleIncallSonification(stream, true, true);
376f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
377f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
378f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
379f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
380f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (state == AudioSystem::MODE_RINGTONE &&
381f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        isStreamActive(AudioSystem::MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
382f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mLimitRingtoneVolume = true;
383f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
384f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mLimitRingtoneVolume = false;
385f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
386f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
387f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
388f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::setForceUse(AudioSystem::force_use usage, AudioSystem::forced_config config)
389f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3906a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mPhoneState);
391f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
392f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    bool forceVolumeReeval = false;
393f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    switch(usage) {
394f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::FOR_COMMUNICATION:
395f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (config != AudioSystem::FORCE_SPEAKER && config != AudioSystem::FORCE_BT_SCO &&
396f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_NONE) {
39764cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("setForceUse() invalid config %d for FOR_COMMUNICATION", config);
398f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return;
399f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
400f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        forceVolumeReeval = true;
401f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mForceUse[usage] = config;
402f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
403f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::FOR_MEDIA:
404f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (config != AudioSystem::FORCE_HEADPHONES && config != AudioSystem::FORCE_BT_A2DP &&
405f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_WIRED_ACCESSORY &&
406f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_ANALOG_DOCK &&
4071afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent            config != AudioSystem::FORCE_DIGITAL_DOCK && config != AudioSystem::FORCE_NONE &&
40831363a9cb94e80330c335fede0b92b1953a09517Jean-Michel Trivi            config != AudioSystem::FORCE_NO_BT_A2DP) {
40964cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("setForceUse() invalid config %d for FOR_MEDIA", config);
410f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return;
411f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
412f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mForceUse[usage] = config;
413f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
414f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::FOR_RECORD:
415f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (config != AudioSystem::FORCE_BT_SCO && config != AudioSystem::FORCE_WIRED_ACCESSORY &&
416f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_NONE) {
41764cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("setForceUse() invalid config %d for FOR_RECORD", config);
418f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return;
419f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
420f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mForceUse[usage] = config;
421f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
422f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::FOR_DOCK:
423f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (config != AudioSystem::FORCE_NONE && config != AudioSystem::FORCE_BT_CAR_DOCK &&
424f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_BT_DESK_DOCK &&
425f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_WIRED_ACCESSORY &&
426f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_ANALOG_DOCK &&
427f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_DIGITAL_DOCK) {
42864cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("setForceUse() invalid config %d for FOR_DOCK", config);
429f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
430f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        forceVolumeReeval = true;
431f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mForceUse[usage] = config;
432f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
433738207def5f691d605ae33d041116829a74513a9Eric Laurent    case AudioSystem::FOR_SYSTEM:
434738207def5f691d605ae33d041116829a74513a9Eric Laurent        if (config != AudioSystem::FORCE_NONE &&
435738207def5f691d605ae33d041116829a74513a9Eric Laurent            config != AudioSystem::FORCE_SYSTEM_ENFORCED) {
436738207def5f691d605ae33d041116829a74513a9Eric Laurent            ALOGW("setForceUse() invalid config %d for FOR_SYSTEM", config);
437738207def5f691d605ae33d041116829a74513a9Eric Laurent        }
438738207def5f691d605ae33d041116829a74513a9Eric Laurent        forceVolumeReeval = true;
439738207def5f691d605ae33d041116829a74513a9Eric Laurent        mForceUse[usage] = config;
440738207def5f691d605ae33d041116829a74513a9Eric Laurent        break;
441f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    default:
44264cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("setForceUse() invalid usage %d", usage);
443f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
444f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
445f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
4465ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // check for device and output changes triggered by new force usage
447f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkA2dpSuspend();
448f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForAllStrategies();
449c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    updateDevicesAndOutputs();
4505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < mOutputs.size(); i++) {
4515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_io_handle_t output = mOutputs.keyAt(i);
4525ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_devices_t newDevice = getNewDevice(output, true /*fromCache*/);
453ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        setOutputDevice(output, newDevice, (newDevice != AUDIO_DEVICE_NONE));
454ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
4555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            applyStreamVolumes(output, newDevice, 0, true);
4565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
457f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
458f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
459f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    audio_io_handle_t activeInput = getActiveInput();
460f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (activeInput != 0) {
461f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioInputDescriptor *inputDesc = mInputs.valueFor(activeInput);
4625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_devices_t newDevice = getDeviceForInputSource(inputDesc->mInputSource);
463ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if ((newDevice != AUDIO_DEVICE_NONE) && (newDevice != inputDesc->mDevice)) {
4646a70518b93928d1c91457ff805e375c82d76b0e5Steve Block            ALOGV("setForceUse() changing device from %x to %x for input %d",
465f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    inputDesc->mDevice, newDevice, activeInput);
466f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            inputDesc->mDevice = newDevice;
467f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            AudioParameter param = AudioParameter();
468f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            param.addInt(String8(AudioParameter::keyRouting), (int)newDevice);
469f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mpClientInterface->setParameters(activeInput, param.toString());
470f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
471f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
472f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
473f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
474f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
475f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima ZavinAudioSystem::forced_config AudioPolicyManagerBase::getForceUse(AudioSystem::force_use usage)
476f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
477f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return mForceUse[usage];
478f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
479f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
480f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::setSystemProperty(const char* property, const char* value)
481f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
4826a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("setSystemProperty() property %s, value %s", property, value);
483f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
484f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
4853cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric LaurentAudioPolicyManagerBase::IOProfile *AudioPolicyManagerBase::getProfileForDirectOutput(
4863cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                               audio_devices_t device,
48770c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                               uint32_t samplingRate,
48870c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                               uint32_t format,
48970c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                               uint32_t channelMask,
4900977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent                                                               audio_output_flags_t flags)
49170c236c9290732782d5267935af1475b8d5ae602Eric Laurent{
49270c236c9290732782d5267935af1475b8d5ae602Eric Laurent    for (size_t i = 0; i < mHwModules.size(); i++) {
49370c236c9290732782d5267935af1475b8d5ae602Eric Laurent        if (mHwModules[i]->mHandle == 0) {
49470c236c9290732782d5267935af1475b8d5ae602Eric Laurent            continue;
49570c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
49670c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) {
497a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald            IOProfile *profile = mHwModules[i]->mOutputProfiles[j];
498a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald            if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
499a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                if (profile->isCompatibleProfile(device, samplingRate, format,
500a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                                           channelMask,
501a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                                           AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
502a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                    if (mAvailableOutputDevices & profile->mSupportedDevices) {
503a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                        return mHwModules[i]->mOutputProfiles[j];
504a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                    }
505a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                }
506a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald            } else if (flags & AUDIO_OUTPUT_FLAG_DIRECT) {
507a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                if (profile->isCompatibleProfile(device, samplingRate, format,
5083cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                           channelMask,
5093cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                           AUDIO_OUTPUT_FLAG_DIRECT)) {
510a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                    if (mAvailableOutputDevices & profile->mSupportedDevices) {
511a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                        return mHwModules[i]->mOutputProfiles[j];
512a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                    }
513a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                }
514a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald            }
51570c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
51670c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
51770c236c9290732782d5267935af1475b8d5ae602Eric Laurent    return 0;
51870c236c9290732782d5267935af1475b8d5ae602Eric Laurent}
51970c236c9290732782d5267935af1475b8d5ae602Eric Laurent
520f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinaudio_io_handle_t AudioPolicyManagerBase::getOutput(AudioSystem::stream_type stream,
521f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    uint32_t samplingRate,
522f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    uint32_t format,
52370c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                    uint32_t channelMask,
524b4d07b97d23cfaffe22c7859ad7c45e168a7df0eRichard Fitzgerald                                    AudioSystem::output_flags flags,
525b4d07b97d23cfaffe22c7859ad7c45e168a7df0eRichard Fitzgerald                                    const audio_offload_info_t *offloadInfo)
526f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
527f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    audio_io_handle_t output = 0;
528f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    uint32_t latency = 0;
529f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    routing_strategy strategy = getStrategy((AudioSystem::stream_type)stream);
5305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
531a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    ALOGV("getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x",
532a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald          device, stream, samplingRate, format, channelMask, flags);
533f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
534f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
535f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mCurOutput != 0) {
53670c236c9290732782d5267935af1475b8d5ae602Eric Laurent        ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
537f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
538f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
539f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (mTestOutputs[mCurOutput] == 0) {
5406a70518b93928d1c91457ff805e375c82d76b0e5Steve Block            ALOGV("getOutput() opening test output");
5413cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(NULL);
542f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            outputDesc->mDevice = mTestDevice;
543f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            outputDesc->mSamplingRate = mTestSamplingRate;
544f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            outputDesc->mFormat = mTestFormat;
54570c236c9290732782d5267935af1475b8d5ae602Eric Laurent            outputDesc->mChannelMask = mTestChannels;
546f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            outputDesc->mLatency = mTestLatencyMs;
5470977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent            outputDesc->mFlags = (audio_output_flags_t)(mDirectOutput ? AudioSystem::OUTPUT_FLAG_DIRECT : 0);
548f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            outputDesc->mRefCount[stream] = 0;
54970c236c9290732782d5267935af1475b8d5ae602Eric Laurent            mTestOutputs[mCurOutput] = mpClientInterface->openOutput(0, &outputDesc->mDevice,
550f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            &outputDesc->mSamplingRate,
551f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            &outputDesc->mFormat,
55270c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                            &outputDesc->mChannelMask,
553f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            &outputDesc->mLatency,
554b4d07b97d23cfaffe22c7859ad7c45e168a7df0eRichard Fitzgerald                                            outputDesc->mFlags,
555b4d07b97d23cfaffe22c7859ad7c45e168a7df0eRichard Fitzgerald                                            offloadInfo);
556f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (mTestOutputs[mCurOutput]) {
557f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                AudioParameter outputCmd = AudioParameter();
558f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                outputCmd.addInt(String8("set_id"),mCurOutput);
559f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
560f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                addOutput(mTestOutputs[mCurOutput], outputDesc);
561f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
562f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
563f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return mTestOutputs[mCurOutput];
564f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
565f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
566f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
567f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // open a direct output if required by specified parameters
568a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    //force direct flag if offload flag is set: offloading implies a direct output stream
569a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    // and all common behaviors are driven by checking only the direct flag
570a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    // this should normally be set appropriately in the policy configuration file
571a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
572a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        flags = (AudioSystem::output_flags)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
573a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    }
574a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald
5753cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    IOProfile *profile = getProfileForDirectOutput(device,
5763cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                   samplingRate,
5773cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                   format,
5783cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                   channelMask,
5793cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                   (audio_output_flags_t)flags);
5803cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    if (profile != NULL) {
5815a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        AudioOutputDescriptor *outputDesc = NULL;
582f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
5835a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        for (size_t i = 0; i < mOutputs.size(); i++) {
5845a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent            AudioOutputDescriptor *desc = mOutputs.valueAt(i);
5855a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent            if (!desc->isDuplicated() && (profile == desc->mProfile)) {
5865a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent                outputDesc = desc;
5875a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent                // reuse direct output if currently open and configured with same parameters
5885a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent                if ((samplingRate == outputDesc->mSamplingRate) &&
5895a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent                        (format == outputDesc->mFormat) &&
5905a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent                        (channelMask == outputDesc->mChannelMask)) {
5915a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent                    outputDesc->mDirectOpenCount++;
592a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                    ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
5935a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent                    return mOutputs.keyAt(i);
5945a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent                }
5955a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent            }
5965a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        }
5975a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        // close direct output if currently open and configured with different parameters
5985a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        if (outputDesc != NULL) {
5995a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent            closeOutput(outputDesc->mId);
6005a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        }
6015a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        outputDesc = new AudioOutputDescriptor(profile);
602f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mDevice = device;
603f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mSamplingRate = samplingRate;
60470c236c9290732782d5267935af1475b8d5ae602Eric Laurent        outputDesc->mFormat = (audio_format_t)format;
60570c236c9290732782d5267935af1475b8d5ae602Eric Laurent        outputDesc->mChannelMask = (audio_channel_mask_t)channelMask;
606f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mLatency = 0;
607a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        outputDesc->mFlags =(audio_output_flags_t) (outputDesc->mFlags | flags);
608f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mRefCount[stream] = 0;
609f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mStopTime[stream] = 0;
6105a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        outputDesc->mDirectOpenCount = 1;
6113cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        output = mpClientInterface->openOutput(profile->mModule->mHandle,
61270c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                        &outputDesc->mDevice,
613f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                        &outputDesc->mSamplingRate,
614f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                        &outputDesc->mFormat,
61570c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                        &outputDesc->mChannelMask,
616f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                        &outputDesc->mLatency,
617a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                                        outputDesc->mFlags,
618a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                                        offloadInfo);
619f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
6209f1f9b509c930830f6f32e9ef6c2c8a03d6fa96eJean-Michel Trivi        // only accept an output with the requested parameters
621f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (output == 0 ||
622f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            (samplingRate != 0 && samplingRate != outputDesc->mSamplingRate) ||
623f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            (format != 0 && format != outputDesc->mFormat) ||
62470c236c9290732782d5267935af1475b8d5ae602Eric Laurent            (channelMask != 0 && channelMask != outputDesc->mChannelMask)) {
6253cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
6263cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    "format %d %d, channelMask %04x %04x", output, samplingRate,
6273cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
6283cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    outputDesc->mChannelMask);
629f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (output != 0) {
630f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mpClientInterface->closeOutput(output);
631f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
632f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            delete outputDesc;
633f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return 0;
634f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
635f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        addOutput(output, outputDesc);
6365a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        mPreviousOutputs = mOutputs;
6375a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        ALOGV("getOutput() returns new direct output %d", output);
638f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return output;
639f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
640f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
6419f1f9b509c930830f6f32e9ef6c2c8a03d6fa96eJean-Michel Trivi    // ignoring channel mask due to downmix capability in mixer
6429f1f9b509c930830f6f32e9ef6c2c8a03d6fa96eJean-Michel Trivi
643f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // open a non direct output
644f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
645a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    // for non direct outputs, only PCM is supported
646a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    if (audio_is_linear_pcm((audio_format_t)format)) {
647a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        // get which output is suitable for the specified stream. The actual
648a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        // routing change will happen when startOutput() will be called
649a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
650f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
651a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        output = selectOutput(outputs, flags);
652a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    }
653a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
65470c236c9290732782d5267935af1475b8d5ae602Eric Laurent            "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
655f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
6565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGV("getOutput() returns output %d", output);
6575ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
658f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return output;
659f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
660f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
6615ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentaudio_io_handle_t AudioPolicyManagerBase::selectOutput(const SortedVector<audio_io_handle_t>& outputs,
6625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                       AudioSystem::output_flags flags)
6635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
6645ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // select one output among several that provide a path to a particular device or set of
6655ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // devices (the list was previously build by getOutputsForDevice()).
6665ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // The priority is as follows:
6675ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // 1: the output with the highest number of requested policy flags
6685ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // 2: the primary output
6695ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // 3: the first output in the list
6705ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
6715ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (outputs.size() == 0) {
6725ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return 0;
6735ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
6745ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (outputs.size() == 1) {
6755ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return outputs[0];
6765ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
6775ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
6785ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    int maxCommonFlags = 0;
6795ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    audio_io_handle_t outputFlags = 0;
6805ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    audio_io_handle_t outputPrimary = 0;
6815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
6825ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < outputs.size(); i++) {
6835ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        AudioOutputDescriptor *outputDesc = mOutputs.valueFor(outputs[i]);
6845ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (!outputDesc->isDuplicated()) {
6855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            int commonFlags = (int)AudioSystem::popCount(outputDesc->mProfile->mFlags & flags);
6865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            if (commonFlags > maxCommonFlags) {
6875ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                outputFlags = outputs[i];
6885ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                maxCommonFlags = commonFlags;
6895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags);
6905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
6910977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent            if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
6925ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                outputPrimary = outputs[i];
6935ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
6945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
6955ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
6965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
6975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (outputFlags != 0) {
6985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return outputFlags;
6995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
7005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (outputPrimary != 0) {
7015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return outputPrimary;
7025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
7035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
7045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return outputs[0];
7055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
7065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
707f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::startOutput(audio_io_handle_t output,
708f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                             AudioSystem::stream_type stream,
709f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                             int session)
710f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
7116a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("startOutput() output %d, stream %d, session %d", output, stream, session);
712f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mOutputs.indexOfKey(output);
713f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
71464cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("startOutput() unknow output %d", output);
715f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
716f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
717f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
718f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index);
719f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
7205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // increment usage count for this stream on the requested output:
721f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // NOTE that the usage count is the same for duplicated output and hardware output which is
7225ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
723f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    outputDesc->changeRefCount(stream, 1);
724f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
7255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (outputDesc->mRefCount[stream] == 1) {
7265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_devices_t newDevice = getNewDevice(output, false /*fromCache*/);
727b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        routing_strategy strategy = getStrategy(stream);
728b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
729b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                            (strategy == STRATEGY_SONIFICATION_RESPECTFUL);
730b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        uint32_t waitMs = 0;
7315ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        bool force = false;
7325ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        for (size_t i = 0; i < mOutputs.size(); i++) {
7335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            AudioOutputDescriptor *desc = mOutputs.valueAt(i);
734b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent            if (desc != outputDesc) {
735b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                // force a device change if any other output is managed by the same hw
736b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                // module and has a current device selection that differs from selected device.
737b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                // In this case, the audio HAL must receive the new device selection so that it can
738b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                // change the device currently selected by the other active output.
739b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                if (outputDesc->sharesHwModuleWith(desc) &&
7405ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                    desc->device() != newDevice) {
741b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                    force = true;
742b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                }
743b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                // wait for audio on other active outputs to be presented when starting
744b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                // a notification so that audio focus effect can propagate.
74542fa8215a763822c0d03fc936e4cac1eb864c9ccEric Laurent                uint32_t latency = desc->latency();
74642fa8215a763822c0d03fc936e4cac1eb864c9ccEric Laurent                if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
74742fa8215a763822c0d03fc936e4cac1eb864c9ccEric Laurent                    waitMs = latency;
748b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                }
7495ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
7505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
751b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        uint32_t muteWaitMs = setOutputDevice(output, newDevice, force);
752f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
7535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // handle special case for sonification while in call
7545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (isInCall()) {
7555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            handleIncallSonification(stream, true, false);
7565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
757c16ac09f510437e8340be691720177a490ae78f0Eric Laurent
7585ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // apply volume rules for current stream and device if necessary
7595ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        checkAndSetVolume(stream,
760c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                          mStreams[stream].getVolumeIndex(newDevice),
7615ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                          output,
7625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                          newDevice);
76312bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi
7645ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // update the outputs if starting an output with a stream that can affect notification
7655ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // routing
7665ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        handleNotificationRoutingForStream(stream);
767b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        if (waitMs > muteWaitMs) {
768b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent            usleep((waitMs - muteWaitMs) * 2 * 1000);
769b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        }
7705ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
771f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
772f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
773f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
7745ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
775f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::stopOutput(audio_io_handle_t output,
776f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            AudioSystem::stream_type stream,
777f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            int session)
778f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
7796a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
780f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mOutputs.indexOfKey(output);
781f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
78264cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("stopOutput() unknow output %d", output);
783f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
784f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
785f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
786f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index);
787f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
788f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // handle special case for sonification while in call
789f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (isInCall()) {
790f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        handleIncallSonification(stream, false, false);
791f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
792f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
793f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (outputDesc->mRefCount[stream] > 0) {
794f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // decrement usage count of this stream on the output
795f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->changeRefCount(stream, -1);
796f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // store time at which the stream was stopped - see isStreamActive()
7975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (outputDesc->mRefCount[stream] == 0) {
7985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            outputDesc->mStopTime[stream] = systemTime();
7995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            audio_devices_t newDevice = getNewDevice(output, false /*fromCache*/);
8005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // delay the device switch by twice the latency because stopOutput() is executed when
8015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // the track stop() command is received and at that time the audio track buffer can
8025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // still contain data that needs to be drained. The latency only covers the audio HAL
8035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // and kernel buffers. Also the latency does not always include additional delay in the
8045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // audio path (audio DSP, CODEC ...)
8055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            setOutputDevice(output, newDevice, false, outputDesc->mLatency*2);
8065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
8075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // force restoring the device selection on other active outputs if it differs from the
8085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // one being selected for this output
8095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            for (size_t i = 0; i < mOutputs.size(); i++) {
8105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                audio_io_handle_t curOutput = mOutputs.keyAt(i);
8115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                AudioOutputDescriptor *desc = mOutputs.valueAt(i);
8125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                if (curOutput != output &&
81380f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                        desc->isActive() &&
8145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                        outputDesc->sharesHwModuleWith(desc) &&
815a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                        (newDevice != desc->device())) {
8165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                    setOutputDevice(curOutput,
8175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                    getNewDevice(curOutput, false /*fromCache*/),
8185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                    true,
8195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                    outputDesc->mLatency*2);
8205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                }
8215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
8225ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // update the outputs if stopping one with a stream that can affect notification routing
8235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            handleNotificationRoutingForStream(stream);
824f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
825f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return NO_ERROR;
826f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
82764cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("stopOutput() refcount is already 0 for output %d", output);
828f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return INVALID_OPERATION;
829f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
830f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
831f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
832f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::releaseOutput(audio_io_handle_t output)
833f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
8346a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("releaseOutput() %d", output);
835f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mOutputs.indexOfKey(output);
836f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
83764cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("releaseOutput() releasing unknown output %d", output);
838f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
839f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
840f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
841f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
842f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    int testIndex = testOutputIndex(output);
843f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (testIndex != 0) {
844f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index);
84580f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent        if (outputDesc->isActive()) {
846f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mpClientInterface->closeOutput(output);
847f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            delete mOutputs.valueAt(index);
848f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mOutputs.removeItem(output);
849f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mTestOutputs[testIndex] = 0;
850f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
851f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
852f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
853f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
854f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
8555a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent    AudioOutputDescriptor *desc = mOutputs.valueAt(index);
8565a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent    if (desc->mFlags & AudioSystem::OUTPUT_FLAG_DIRECT) {
8575a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        if (desc->mDirectOpenCount <= 0) {
8585a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent            ALOGW("releaseOutput() invalid open count %d for output %d",
8595a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent                                                              desc->mDirectOpenCount, output);
8605a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent            return;
8615a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        }
8625a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        if (--desc->mDirectOpenCount == 0) {
8635a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent            closeOutput(output);
8645a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        }
865f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
866f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
867f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
868f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinaudio_io_handle_t AudioPolicyManagerBase::getInput(int inputSource,
869f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    uint32_t samplingRate,
870f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    uint32_t format,
87170c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                    uint32_t channelMask,
872f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    AudioSystem::audio_in_acoustics acoustics)
873f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
874f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    audio_io_handle_t input = 0;
875f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent    audio_devices_t device = getDeviceForInputSource(inputSource);
876f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
87770c236c9290732782d5267935af1475b8d5ae602Eric Laurent    ALOGV("getInput() inputSource %d, samplingRate %d, format %d, channelMask %x, acoustics %x",
87870c236c9290732782d5267935af1475b8d5ae602Eric Laurent          inputSource, samplingRate, format, channelMask, acoustics);
879f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
880ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if (device == AUDIO_DEVICE_NONE) {
8815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        ALOGW("getInput() could not find device for inputSource %d", inputSource);
882f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return 0;
883f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
884f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
885f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // adapt channel selection to input source
886f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    switch(inputSource) {
887f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_UPLINK:
88870c236c9290732782d5267935af1475b8d5ae602Eric Laurent        channelMask = AudioSystem::CHANNEL_IN_VOICE_UPLINK;
889f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
890f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_DOWNLINK:
89170c236c9290732782d5267935af1475b8d5ae602Eric Laurent        channelMask = AudioSystem::CHANNEL_IN_VOICE_DNLINK;
892f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
893f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_CALL:
89470c236c9290732782d5267935af1475b8d5ae602Eric Laurent        channelMask = (AudioSystem::CHANNEL_IN_VOICE_UPLINK | AudioSystem::CHANNEL_IN_VOICE_DNLINK);
895f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
896f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    default:
897f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
898f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
899f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
9005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    IOProfile *profile = getInputProfile(device,
9015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                         samplingRate,
9025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                         format,
90370c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                         channelMask);
9045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (profile == NULL) {
9055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        ALOGW("getInput() could not find profile for device %04x, samplingRate %d, format %d,"
90670c236c9290732782d5267935af1475b8d5ae602Eric Laurent                "channelMask %04x",
90770c236c9290732782d5267935af1475b8d5ae602Eric Laurent                device, samplingRate, format, channelMask);
90870c236c9290732782d5267935af1475b8d5ae602Eric Laurent        return 0;
90970c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
91070c236c9290732782d5267935af1475b8d5ae602Eric Laurent
91170c236c9290732782d5267935af1475b8d5ae602Eric Laurent    if (profile->mModule->mHandle == 0) {
9123cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        ALOGE("getInput(): HW module %s not opened", profile->mModule->mName);
9135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return 0;
9145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
9155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
9165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    AudioInputDescriptor *inputDesc = new AudioInputDescriptor(profile);
917f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
918f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    inputDesc->mInputSource = inputSource;
919f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    inputDesc->mDevice = device;
920f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    inputDesc->mSamplingRate = samplingRate;
92170c236c9290732782d5267935af1475b8d5ae602Eric Laurent    inputDesc->mFormat = (audio_format_t)format;
92270c236c9290732782d5267935af1475b8d5ae602Eric Laurent    inputDesc->mChannelMask = (audio_channel_mask_t)channelMask;
923f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    inputDesc->mRefCount = 0;
92470c236c9290732782d5267935af1475b8d5ae602Eric Laurent    input = mpClientInterface->openInput(profile->mModule->mHandle,
92570c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                    &inputDesc->mDevice,
926f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    &inputDesc->mSamplingRate,
927f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    &inputDesc->mFormat,
92870c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                    &inputDesc->mChannelMask);
929f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
930f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // only accept input with the exact requested set of parameters
931f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (input == 0 ||
932f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        (samplingRate != inputDesc->mSamplingRate) ||
933f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        (format != inputDesc->mFormat) ||
93470c236c9290732782d5267935af1475b8d5ae602Eric Laurent        (channelMask != inputDesc->mChannelMask)) {
93570c236c9290732782d5267935af1475b8d5ae602Eric Laurent        ALOGV("getInput() failed opening input: samplingRate %d, format %d, channelMask %d",
93670c236c9290732782d5267935af1475b8d5ae602Eric Laurent                samplingRate, format, channelMask);
937f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (input != 0) {
938f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mpClientInterface->closeInput(input);
939f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
940f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        delete inputDesc;
941f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return 0;
942f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
943f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mInputs.add(input, inputDesc);
944f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return input;
945f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
946f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
947f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::startInput(audio_io_handle_t input)
948f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
9496a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("startInput() input %d", input);
950f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mInputs.indexOfKey(input);
951f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
95264cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("startInput() unknow input %d", input);
953f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
954f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
955f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioInputDescriptor *inputDesc = mInputs.valueAt(index);
956f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
957f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
958f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mTestInput == 0)
959f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
960f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    {
961fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent        // refuse 2 active AudioRecord clients at the same time except if the active input
962fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent        // uses AUDIO_SOURCE_HOTWORD in which case it is closed.
963fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent        audio_io_handle_t activeInput = getActiveInput();
964fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent        if (!isVirtualInputDevice(inputDesc->mDevice) && activeInput != 0) {
965fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent            AudioInputDescriptor *activeDesc = mInputs.valueFor(activeInput);
966fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent            if (activeDesc->mInputSource == AUDIO_SOURCE_HOTWORD) {
967fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent                ALOGW("startInput() preempting already started low-priority input %d", activeInput);
968fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent                stopInput(activeInput);
969fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent                releaseInput(activeInput);
970fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent            } else {
971fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent                ALOGW("startInput() input %d failed: other input already started..", input);
972fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent                return INVALID_OPERATION;
973fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent            }
974f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
975f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
976f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
97766707435156d8d99d795271a7bd54943065b4c2dEric Laurent    audio_devices_t newDevice = getDeviceForInputSource(inputDesc->mInputSource);
97866707435156d8d99d795271a7bd54943065b4c2dEric Laurent    if ((newDevice != AUDIO_DEVICE_NONE) && (newDevice != inputDesc->mDevice)) {
97966707435156d8d99d795271a7bd54943065b4c2dEric Laurent        inputDesc->mDevice = newDevice;
98066707435156d8d99d795271a7bd54943065b4c2dEric Laurent    }
981b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown
982b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown    // automatically enable the remote submix output when input is started
983b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown    if (audio_is_remote_submix_device(inputDesc->mDevice)) {
984b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown        setDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
985b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown                AudioSystem::DEVICE_STATE_AVAILABLE, AUDIO_REMOTE_SUBMIX_DEVICE_ADDRESS);
986b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown    }
987b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown
988f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioParameter param = AudioParameter();
989f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    param.addInt(String8(AudioParameter::keyRouting), (int)inputDesc->mDevice);
990f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
991fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent    int aliasSource = (inputDesc->mInputSource == AUDIO_SOURCE_HOTWORD) ?
992fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent                                        AUDIO_SOURCE_VOICE_RECOGNITION : inputDesc->mInputSource;
993fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent
994fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent    param.addInt(String8(AudioParameter::keyInputSource), aliasSource);
9956a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("AudioPolicyManager::startInput() input source = %d", inputDesc->mInputSource);
996f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
997f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mpClientInterface->setParameters(input, param.toString());
998f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
999f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    inputDesc->mRefCount = 1;
1000f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
1001f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1002f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1003f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::stopInput(audio_io_handle_t input)
1004f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
10056a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("stopInput() input %d", input);
1006f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mInputs.indexOfKey(input);
1007f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
100864cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("stopInput() unknow input %d", input);
1009f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
1010f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1011f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioInputDescriptor *inputDesc = mInputs.valueAt(index);
1012f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1013f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (inputDesc->mRefCount == 0) {
101464cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("stopInput() input %d already stopped", input);
1015f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return INVALID_OPERATION;
1016f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
1017b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown        // automatically disable the remote submix output when input is stopped
1018b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown        if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1019b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown            setDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1020b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown                    AudioSystem::DEVICE_STATE_UNAVAILABLE, AUDIO_REMOTE_SUBMIX_DEVICE_ADDRESS);
1021b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown        }
1022b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown
1023f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioParameter param = AudioParameter();
1024f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        param.addInt(String8(AudioParameter::keyRouting), 0);
1025f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mpClientInterface->setParameters(input, param.toString());
1026f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        inputDesc->mRefCount = 0;
1027f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return NO_ERROR;
1028f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1029f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1030f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1031f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::releaseInput(audio_io_handle_t input)
1032f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
10336a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("releaseInput() %d", input);
1034f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mInputs.indexOfKey(input);
1035f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
103664cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("releaseInput() releasing unknown input %d", input);
1037f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
1038f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1039f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mpClientInterface->closeInput(input);
1040f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    delete mInputs.valueAt(index);
1041f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mInputs.removeItem(input);
10426a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("releaseInput() exit");
1043f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1044f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1045f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::initStreamVolume(AudioSystem::stream_type stream,
1046f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            int indexMin,
1047f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            int indexMax)
1048f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
10496a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
1050f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (indexMin < 0 || indexMin >= indexMax) {
105164cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("initStreamVolume() invalid index limits for stream %d, min %d, max %d", stream , indexMin, indexMax);
1052f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
1053f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1054f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mStreams[stream].mIndexMin = indexMin;
1055f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mStreams[stream].mIndexMax = indexMax;
1056f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1057f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1058c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentstatus_t AudioPolicyManagerBase::setStreamVolumeIndex(AudioSystem::stream_type stream,
1059c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                      int index,
1060c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                      audio_devices_t device)
1061f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1062f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1063f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if ((index < mStreams[stream].mIndexMin) || (index > mStreams[stream].mIndexMax)) {
1064f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
1065f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1066c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    if (!audio_is_output_device(device)) {
1067c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        return BAD_VALUE;
1068c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    }
1069f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1070f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // Force max volume if stream cannot be muted
1071f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (!mStreams[stream].mCanBeMuted) index = mStreams[stream].mIndexMax;
1072f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1073b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    ALOGV("setStreamVolumeIndex() stream %d, device %04x, index %d",
1074c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent          stream, device, index);
1075c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
1076c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    // if device is AUDIO_DEVICE_OUT_DEFAULT set default value and
1077c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    // clear all device specific values
1078c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    if (device == AUDIO_DEVICE_OUT_DEFAULT) {
1079c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        mStreams[stream].mIndexCur.clear();
1080c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    }
1081c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    mStreams[stream].mIndexCur.add(device, index);
1082f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1083f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // compute and apply stream volume on all outputs according to connected device
1084f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    status_t status = NO_ERROR;
1085f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mOutputs.size(); i++) {
1086c5eb8b4a5d4395ce335bc7c3e6df2678fa47e2ddEric Laurent        audio_devices_t curDevice =
1087c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                getDeviceForVolume(mOutputs.valueAt(i)->device());
1088e92d623811f3fd3e7cc5e5dd8bc93c0c0a8fdf50Eric Laurent        if ((device == AUDIO_DEVICE_OUT_DEFAULT) || (device == curDevice)) {
1089c5eb8b4a5d4395ce335bc7c3e6df2678fa47e2ddEric Laurent            status_t volStatus = checkAndSetVolume(stream, index, mOutputs.keyAt(i), curDevice);
1090c5eb8b4a5d4395ce335bc7c3e6df2678fa47e2ddEric Laurent            if (volStatus != NO_ERROR) {
1091c5eb8b4a5d4395ce335bc7c3e6df2678fa47e2ddEric Laurent                status = volStatus;
1092c5eb8b4a5d4395ce335bc7c3e6df2678fa47e2ddEric Laurent            }
1093f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1094f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1095f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return status;
1096f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1097f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1098c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentstatus_t AudioPolicyManagerBase::getStreamVolumeIndex(AudioSystem::stream_type stream,
1099c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                      int *index,
1100c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                      audio_devices_t device)
1101f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1102c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    if (index == NULL) {
1103f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
1104f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1105c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    if (!audio_is_output_device(device)) {
1106c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        return BAD_VALUE;
1107c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    }
1108c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    // if device is AUDIO_DEVICE_OUT_DEFAULT, return volume for device corresponding to
1109c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    // the strategy the stream belongs to.
1110c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    if (device == AUDIO_DEVICE_OUT_DEFAULT) {
1111c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
1112c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    }
1113c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    device = getDeviceForVolume(device);
1114c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
1115c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    *index =  mStreams[stream].getVolumeIndex(device);
1116c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index);
1117f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
1118f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1119f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1120c94dccc97cc3ed5171b45f46a0f7f8762d37156fGlenn Kastenaudio_io_handle_t AudioPolicyManagerBase::getOutputForEffect(const effect_descriptor_t *desc)
1121f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
11226a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("getOutputForEffect()");
1123f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // apply simple rule where global effects are attached to the same output as MUSIC streams
11244660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen
11254660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen    routing_strategy strategy = getStrategy(AudioSystem::MUSIC);
11264660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen    audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
1127c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs);
11284660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen    int outIdx = 0;
11294660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen    for (size_t i = 0; i < dstOutputs.size(); i++) {
11304660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen        AudioOutputDescriptor *desc = mOutputs.valueFor(dstOutputs[i]);
11314660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen        if (desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
11324660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen            outIdx = i;
11334660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen        }
11344660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen    }
11354660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen    return dstOutputs[outIdx];
1136f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1137f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1138c94dccc97cc3ed5171b45f46a0f7f8762d37156fGlenn Kastenstatus_t AudioPolicyManagerBase::registerEffect(const effect_descriptor_t *desc,
11391c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent                                audio_io_handle_t io,
1140f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                uint32_t strategy,
1141f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                int session,
1142f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                int id)
1143f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
11441c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent    ssize_t index = mOutputs.indexOfKey(io);
1145f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
11461c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent        index = mInputs.indexOfKey(io);
11471c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent        if (index < 0) {
114864cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("registerEffect() unknown io %d", io);
11491c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent            return INVALID_OPERATION;
11501c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent        }
1151f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1152f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1153f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mTotalEffectsMemory + desc->memoryUsage > getMaxEffectsMemory()) {
115464cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("registerEffect() memory limit exceeded for Fx %s, Memory %d KB",
1155f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                desc->name, desc->memoryUsage);
1156f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return INVALID_OPERATION;
1157f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1158f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mTotalEffectsMemory += desc->memoryUsage;
11596a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("registerEffect() effect %s, io %d, strategy %d session %d id %d",
11601c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent            desc->name, io, strategy, session, id);
11616a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("registerEffect() memory %d, total memory %d", desc->memoryUsage, mTotalEffectsMemory);
1162f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1163f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    EffectDescriptor *pDesc = new EffectDescriptor();
1164f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    memcpy (&pDesc->mDesc, desc, sizeof(effect_descriptor_t));
11651c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent    pDesc->mIo = io;
1166f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    pDesc->mStrategy = (routing_strategy)strategy;
1167f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    pDesc->mSession = session;
1168582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    pDesc->mEnabled = false;
1169582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent
1170f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mEffects.add(id, pDesc);
1171f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1172f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
1173f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1174f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1175f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::unregisterEffect(int id)
1176f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1177f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mEffects.indexOfKey(id);
1178f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
117964cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("unregisterEffect() unknown effect ID %d", id);
1180f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return INVALID_OPERATION;
1181f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1182f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1183f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    EffectDescriptor *pDesc = mEffects.valueAt(index);
1184f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1185582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    setEffectEnabled(pDesc, false);
1186582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent
1187f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mTotalEffectsMemory < pDesc->mDesc.memoryUsage) {
118864cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("unregisterEffect() memory %d too big for total %d",
1189f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                pDesc->mDesc.memoryUsage, mTotalEffectsMemory);
1190f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        pDesc->mDesc.memoryUsage = mTotalEffectsMemory;
1191f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1192f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mTotalEffectsMemory -= pDesc->mDesc.memoryUsage;
11936a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("unregisterEffect() effect %s, ID %d, memory %d total memory %d",
1194582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent            pDesc->mDesc.name, id, pDesc->mDesc.memoryUsage, mTotalEffectsMemory);
1195f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1196f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mEffects.removeItem(id);
1197f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    delete pDesc;
1198f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1199f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
1200f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1201f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1202582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurentstatus_t AudioPolicyManagerBase::setEffectEnabled(int id, bool enabled)
1203582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent{
1204582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    ssize_t index = mEffects.indexOfKey(id);
1205582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    if (index < 0) {
120664cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("unregisterEffect() unknown effect ID %d", id);
1207582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        return INVALID_OPERATION;
1208582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    }
1209582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent
1210582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    return setEffectEnabled(mEffects.valueAt(index), enabled);
1211582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent}
1212582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent
1213582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurentstatus_t AudioPolicyManagerBase::setEffectEnabled(EffectDescriptor *pDesc, bool enabled)
1214582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent{
1215582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    if (enabled == pDesc->mEnabled) {
12166a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("setEffectEnabled(%s) effect already %s",
1217582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent             enabled?"true":"false", enabled?"enabled":"disabled");
1218582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        return INVALID_OPERATION;
1219582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    }
1220582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent
1221582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    if (enabled) {
1222582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        if (mTotalEffectsCpuLoad + pDesc->mDesc.cpuLoad > getMaxEffectsCpuLoad()) {
122364cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("setEffectEnabled(true) CPU Load limit exceeded for Fx %s, CPU %f MIPS",
1224582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent                 pDesc->mDesc.name, (float)pDesc->mDesc.cpuLoad/10);
1225582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent            return INVALID_OPERATION;
1226582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        }
1227582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        mTotalEffectsCpuLoad += pDesc->mDesc.cpuLoad;
12286a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("setEffectEnabled(true) total CPU %d", mTotalEffectsCpuLoad);
1229582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    } else {
1230582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        if (mTotalEffectsCpuLoad < pDesc->mDesc.cpuLoad) {
123164cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("setEffectEnabled(false) CPU load %d too high for total %d",
1232582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent                    pDesc->mDesc.cpuLoad, mTotalEffectsCpuLoad);
1233582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent            pDesc->mDesc.cpuLoad = mTotalEffectsCpuLoad;
1234582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        }
1235582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        mTotalEffectsCpuLoad -= pDesc->mDesc.cpuLoad;
12366a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("setEffectEnabled(false) total CPU %d", mTotalEffectsCpuLoad);
1237582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    }
1238582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    pDesc->mEnabled = enabled;
1239582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    return NO_ERROR;
1240582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent}
1241582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent
1242f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinbool AudioPolicyManagerBase::isStreamActive(int stream, uint32_t inPastMs) const
1243f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1244f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    nsecs_t sysTime = systemTime();
1245f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mOutputs.size(); i++) {
124680f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent        const AudioOutputDescriptor *outputDesc = mOutputs.valueAt(i);
124780f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent        if (outputDesc->isStreamActive((AudioSystem::stream_type)stream, inPastMs, sysTime)) {
1248f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return true;
1249f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1250f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1251f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return false;
1252f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1253f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1254dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivibool AudioPolicyManagerBase::isStreamActiveRemotely(int stream, uint32_t inPastMs) const
1255dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi{
1256dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi    nsecs_t sysTime = systemTime();
1257dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi    for (size_t i = 0; i < mOutputs.size(); i++) {
1258dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi        const AudioOutputDescriptor *outputDesc = mOutputs.valueAt(i);
125980f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent        if (((outputDesc->device() & APM_AUDIO_OUT_DEVICE_REMOTE_ALL) != 0) &&
126080f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                outputDesc->isStreamActive((AudioSystem::stream_type)stream, inPastMs, sysTime)) {
1261dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi            return true;
1262dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi        }
1263dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi    }
1264dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi    return false;
1265dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi}
1266dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi
1267abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivibool AudioPolicyManagerBase::isSourceActive(audio_source_t source) const
1268abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi{
1269abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi    for (size_t i = 0; i < mInputs.size(); i++) {
1270abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi        const AudioInputDescriptor * inputDescriptor = mInputs.valueAt(i);
1271fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent        if ((inputDescriptor->mInputSource == (int)source ||
1272fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent                (source == (audio_source_t)AUDIO_SOURCE_VOICE_RECOGNITION &&
1273fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent                 inputDescriptor->mInputSource == AUDIO_SOURCE_HOTWORD))
1274fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent             && (inputDescriptor->mRefCount > 0)) {
1275abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi            return true;
1276abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi        }
1277abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi    }
1278abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi    return false;
1279abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi}
1280abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi
1281abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi
1282f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::dump(int fd)
1283f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1284f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    const size_t SIZE = 256;
1285f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    char buffer[SIZE];
1286f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    String8 result;
1287f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1288f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
1289f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1290b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
129170c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, " Primary Output: %d\n", mPrimaryOutput);
1292f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1293f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " A2DP device address: %s\n", mA2dpDeviceAddress.string());
1294f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1295f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " SCO device address: %s\n", mScoDeviceAddress.string());
1296f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1297599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    snprintf(buffer, SIZE, " USB audio ALSA %s\n", mUsbCardAndDevice.string());
1298599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    result.append(buffer);
1299f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Output devices: %08x\n", mAvailableOutputDevices);
1300f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1301f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Input devices: %08x\n", mAvailableInputDevices);
1302f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1303f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Phone state: %d\n", mPhoneState);
1304f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1305f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Force use for communications %d\n", mForceUse[AudioSystem::FOR_COMMUNICATION]);
1306f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1307f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Force use for media %d\n", mForceUse[AudioSystem::FOR_MEDIA]);
1308f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1309f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Force use for record %d\n", mForceUse[AudioSystem::FOR_RECORD]);
1310f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1311f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Force use for dock %d\n", mForceUse[AudioSystem::FOR_DOCK]);
1312f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1313738207def5f691d605ae33d041116829a74513a9Eric Laurent    snprintf(buffer, SIZE, " Force use for system %d\n", mForceUse[AudioSystem::FOR_SYSTEM]);
1314738207def5f691d605ae33d041116829a74513a9Eric Laurent    result.append(buffer);
1315f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, result.string(), result.size());
1316f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
13175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
131870c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "\nHW Modules dump:\n");
13195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    write(fd, buffer, strlen(buffer));
132070c236c9290732782d5267935af1475b8d5ae602Eric Laurent    for (size_t i = 0; i < mHwModules.size(); i++) {
132170c236c9290732782d5267935af1475b8d5ae602Eric Laurent        snprintf(buffer, SIZE, "- HW Module %d:\n", i + 1);
13225ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        write(fd, buffer, strlen(buffer));
132370c236c9290732782d5267935af1475b8d5ae602Eric Laurent        mHwModules[i]->dump(fd);
13245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
13255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
1326f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "\nOutputs dump:\n");
1327f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
1328f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mOutputs.size(); i++) {
1329f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        snprintf(buffer, SIZE, "- Output %d dump:\n", mOutputs.keyAt(i));
1330f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        write(fd, buffer, strlen(buffer));
1331f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mOutputs.valueAt(i)->dump(fd);
1332f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1333f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1334f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "\nInputs dump:\n");
1335f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
1336f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mInputs.size(); i++) {
1337f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        snprintf(buffer, SIZE, "- Input %d dump:\n", mInputs.keyAt(i));
1338f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        write(fd, buffer, strlen(buffer));
1339f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mInputs.valueAt(i)->dump(fd);
1340f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1341f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1342f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "\nStreams dump:\n");
1343f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
1344c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    snprintf(buffer, SIZE,
1345c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent             " Stream  Can be muted  Index Min  Index Max  Index Cur [device : index]...\n");
1346f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
1347f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) {
1348c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        snprintf(buffer, SIZE, " %02d      ", i);
1349f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        write(fd, buffer, strlen(buffer));
1350c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        mStreams[i].dump(fd);
1351f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1352f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1353f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "\nTotal Effects CPU: %f MIPS, Total Effects memory: %d KB\n",
1354f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            (float)mTotalEffectsCpuLoad/10, mTotalEffectsMemory);
1355f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
1356f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1357f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "Registered effects:\n");
1358f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
1359f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mEffects.size(); i++) {
1360f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        snprintf(buffer, SIZE, "- Effect %d dump:\n", mEffects.keyAt(i));
1361f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        write(fd, buffer, strlen(buffer));
1362f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mEffects.valueAt(i)->dump(fd);
1363f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1364f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1365f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1366f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
1367f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1368f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1369a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald// This function checks for the parameters which can be offloaded.
1370a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald// This can be enhanced depending on the capability of the DSP and policy
1371a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald// of the system.
1372b4d07b97d23cfaffe22c7859ad7c45e168a7df0eRichard Fitzgeraldbool AudioPolicyManagerBase::isOffloadSupported(const audio_offload_info_t& offloadInfo)
1373b4d07b97d23cfaffe22c7859ad7c45e168a7df0eRichard Fitzgerald{
1374a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
1375a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald     " BitRate=%u, duration=%lld us, has_video=%d",
1376a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald     offloadInfo.sample_rate, offloadInfo.channel_mask,
1377a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald     offloadInfo.format,
1378a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald     offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
1379a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald     offloadInfo.has_video);
1380a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald
1381a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    // Check if offload has been disabled
1382a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    char propValue[PROPERTY_VALUE_MAX];
1383a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    if (property_get("audio.offload.disable", propValue, "0")) {
1384a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        if (atoi(propValue) != 0) {
1385a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald            ALOGV("offload disabled by audio.offload.disable=%s", propValue );
1386a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald            return false;
1387a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        }
1388a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    }
1389a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald
1390a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    // Check if stream type is music, then only allow offload as of now.
1391a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
1392a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    {
139341b150b659d4ba33a9be9f99fd9f0ee1f2787541Eric Laurent        ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
139441b150b659d4ba33a9be9f99fd9f0ee1f2787541Eric Laurent        return false;
139541b150b659d4ba33a9be9f99fd9f0ee1f2787541Eric Laurent    }
139641b150b659d4ba33a9be9f99fd9f0ee1f2787541Eric Laurent
139741b150b659d4ba33a9be9f99fd9f0ee1f2787541Eric Laurent    //TODO: enable audio offloading with video when ready
139841b150b659d4ba33a9be9f99fd9f0ee1f2787541Eric Laurent    if (offloadInfo.has_video)
139941b150b659d4ba33a9be9f99fd9f0ee1f2787541Eric Laurent    {
140041b150b659d4ba33a9be9f99fd9f0ee1f2787541Eric Laurent        ALOGV("isOffloadSupported: has_video == true, returning false");
1401a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        return false;
1402a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    }
1403a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald
1404a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    //If duration is less than minimum value defined in property, return false
1405a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
1406a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
1407a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald            ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
1408a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald            return false;
1409a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        }
1410a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
1411a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
1412a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        return false;
1413a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    }
1414a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald
1415a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    // See if there is a profile to support this.
1416a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    // AUDIO_DEVICE_NONE
1417a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    IOProfile *profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
1418a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                                            offloadInfo.sample_rate,
1419a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                                            offloadInfo.format,
1420a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                                            offloadInfo.channel_mask,
1421a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                                            AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
1422a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    ALOGV("isOffloadSupported() profile %sfound", profile != NULL ? "" : "NOT ");
1423a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    return (profile != NULL);
1424b4d07b97d23cfaffe22c7859ad7c45e168a7df0eRichard Fitzgerald}
1425b4d07b97d23cfaffe22c7859ad7c45e168a7df0eRichard Fitzgerald
1426f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// ----------------------------------------------------------------------------
1427f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// AudioPolicyManagerBase
1428f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// ----------------------------------------------------------------------------
1429f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1430f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima ZavinAudioPolicyManagerBase::AudioPolicyManagerBase(AudioPolicyClientInterface *clientInterface)
1431f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    :
1432f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
1433f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    Thread(false),
1434f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
143570c236c9290732782d5267935af1475b8d5ae602Eric Laurent    mPrimaryOutput((audio_io_handle_t)0),
1436ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    mAvailableOutputDevices(AUDIO_DEVICE_NONE),
1437ca0657a1ca087a6d474a75fcfedd6aac3901d587Glenn Kasten    mPhoneState(AudioSystem::MODE_NORMAL),
1438f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
1439f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mTotalEffectsCpuLoad(0), mTotalEffectsMemory(0),
144048387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi    mA2dpSuspended(false), mHasA2dp(false), mHasUsb(false), mHasRemoteSubmix(false)
1441f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1442f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mpClientInterface = clientInterface;
1443f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1444f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < AudioSystem::NUM_FORCE_USE; i++) {
1445f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mForceUse[i] = AudioSystem::FORCE_NONE;
1446f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1447f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1448f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    initializeVolumeCurves();
1449f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1450f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mA2dpDeviceAddress = String8("");
1451f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mScoDeviceAddress = String8("");
1452599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    mUsbCardAndDevice = String8("");
1453f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
14545ec145df7708564d385fd3fb764085321cf4c253Dima Zavin    if (loadAudioPolicyConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE) != NO_ERROR) {
14555ec145df7708564d385fd3fb764085321cf4c253Dima Zavin        if (loadAudioPolicyConfig(AUDIO_POLICY_CONFIG_FILE) != NO_ERROR) {
1456739022f26a7127ba76a98dda65411496086114a7Dima Zavin            ALOGE("could not load audio policy configuration file, setting defaults");
1457739022f26a7127ba76a98dda65411496086114a7Dima Zavin            defaultAudioPolicyConfig();
14585ec145df7708564d385fd3fb764085321cf4c253Dima Zavin        }
14595ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
14605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
1461b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    // open all output streams needed to access attached devices
146270c236c9290732782d5267935af1475b8d5ae602Eric Laurent    for (size_t i = 0; i < mHwModules.size(); i++) {
146370c236c9290732782d5267935af1475b8d5ae602Eric Laurent        mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->mName);
146470c236c9290732782d5267935af1475b8d5ae602Eric Laurent        if (mHwModules[i]->mHandle == 0) {
146570c236c9290732782d5267935af1475b8d5ae602Eric Laurent            ALOGW("could not open HW module %s", mHwModules[i]->mName);
146670c236c9290732782d5267935af1475b8d5ae602Eric Laurent            continue;
146770c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
146870c236c9290732782d5267935af1475b8d5ae602Eric Laurent        // open all output streams needed to access attached devices
1469a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        // except for direct output streams that are only opened when they are actually
1470a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        // required by an app.
147170c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
147270c236c9290732782d5267935af1475b8d5ae602Eric Laurent        {
147370c236c9290732782d5267935af1475b8d5ae602Eric Laurent            const IOProfile *outProfile = mHwModules[i]->mOutputProfiles[j];
147470c236c9290732782d5267935af1475b8d5ae602Eric Laurent
1475a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald            if ((outProfile->mSupportedDevices & mAttachedOutputDevices) &&
1476a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                    ((outProfile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0)) {
147770c236c9290732782d5267935af1475b8d5ae602Eric Laurent                AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(outProfile);
147870c236c9290732782d5267935af1475b8d5ae602Eric Laurent                outputDesc->mDevice = (audio_devices_t)(mDefaultOutputDevice &
147970c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                            outProfile->mSupportedDevices);
148070c236c9290732782d5267935af1475b8d5ae602Eric Laurent                audio_io_handle_t output = mpClientInterface->openOutput(
148170c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                outProfile->mModule->mHandle,
148270c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mDevice,
148370c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mSamplingRate,
148470c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mFormat,
148570c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mChannelMask,
148670c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mLatency,
148770c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                outputDesc->mFlags);
148870c236c9290732782d5267935af1475b8d5ae602Eric Laurent                if (output == 0) {
148970c236c9290732782d5267935af1475b8d5ae602Eric Laurent                    delete outputDesc;
149070c236c9290732782d5267935af1475b8d5ae602Eric Laurent                } else {
149170c236c9290732782d5267935af1475b8d5ae602Eric Laurent                    mAvailableOutputDevices = (audio_devices_t)(mAvailableOutputDevices |
149270c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                            (outProfile->mSupportedDevices & mAttachedOutputDevices));
149370c236c9290732782d5267935af1475b8d5ae602Eric Laurent                    if (mPrimaryOutput == 0 &&
14940977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent                            outProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
149570c236c9290732782d5267935af1475b8d5ae602Eric Laurent                        mPrimaryOutput = output;
149670c236c9290732782d5267935af1475b8d5ae602Eric Laurent                    }
149770c236c9290732782d5267935af1475b8d5ae602Eric Laurent                    addOutput(output, outputDesc);
149870c236c9290732782d5267935af1475b8d5ae602Eric Laurent                    setOutputDevice(output,
149970c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                    (audio_devices_t)(mDefaultOutputDevice &
150070c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                        outProfile->mSupportedDevices),
150170c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                    true);
1502b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                }
1503b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            }
1504b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        }
1505f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1506f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
15075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGE_IF((mAttachedOutputDevices & ~mAvailableOutputDevices),
1508b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent             "Not output found for attached devices %08x",
15095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent             (mAttachedOutputDevices & ~mAvailableOutputDevices));
1510b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1511b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output");
1512b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1513c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    updateDevicesAndOutputs();
15143cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
1515f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
1516b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (mPrimaryOutput != 0) {
1517f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioParameter outputCmd = AudioParameter();
1518f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputCmd.addInt(String8("set_id"), 0);
1519b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        mpClientInterface->setParameters(mPrimaryOutput, outputCmd.toString());
1520f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1521c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        mTestDevice = AUDIO_DEVICE_OUT_SPEAKER;
1522f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mTestSamplingRate = 44100;
1523f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mTestFormat = AudioSystem::PCM_16_BIT;
1524f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mTestChannels =  AudioSystem::CHANNEL_OUT_STEREO;
1525f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mTestLatencyMs = 0;
1526f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mCurOutput = 0;
1527f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mDirectOutput = false;
1528f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
1529f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mTestOutputs[i] = 0;
1530f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1531f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1532f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        const size_t SIZE = 256;
1533f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        char buffer[SIZE];
1534f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        snprintf(buffer, SIZE, "AudioPolicyManagerTest");
1535f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        run(buffer, ANDROID_PRIORITY_AUDIO);
1536f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1537f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
1538f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1539f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1540f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima ZavinAudioPolicyManagerBase::~AudioPolicyManagerBase()
1541f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1542f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
1543f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    exit();
1544f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
1545f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin   for (size_t i = 0; i < mOutputs.size(); i++) {
1546f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mpClientInterface->closeOutput(mOutputs.keyAt(i));
1547f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        delete mOutputs.valueAt(i);
1548f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin   }
1549f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin   for (size_t i = 0; i < mInputs.size(); i++) {
1550f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mpClientInterface->closeInput(mInputs.keyAt(i));
1551f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        delete mInputs.valueAt(i);
1552f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin   }
155370c236c9290732782d5267935af1475b8d5ae602Eric Laurent   for (size_t i = 0; i < mHwModules.size(); i++) {
155470c236c9290732782d5267935af1475b8d5ae602Eric Laurent        delete mHwModules[i];
15555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent   }
1556f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1557f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1558f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::initCheck()
1559f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1560b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    return (mPrimaryOutput == 0) ? NO_INIT : NO_ERROR;
1561f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1562f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1563f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
1564f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinbool AudioPolicyManagerBase::threadLoop()
1565f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
15666a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("entering threadLoop()");
1567f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    while (!exitPending())
1568f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    {
1569f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        String8 command;
1570f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        int valueInt;
1571f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        String8 value;
1572f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1573f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        Mutex::Autolock _l(mLock);
1574f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mWaitWorkCV.waitRelative(mLock, milliseconds(50));
1575f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1576f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        command = mpClientInterface->getParameters(0, String8("test_cmd_policy"));
1577f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioParameter param = AudioParameter(command);
1578f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1579f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR &&
1580f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            valueInt != 0) {
15816a70518b93928d1c91457ff805e375c82d76b0e5Steve Block            ALOGV("Test command %s received", command.string());
1582f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            String8 target;
1583f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.get(String8("target"), target) != NO_ERROR) {
1584f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                target = "Manager";
1585f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1586f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) {
1587f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_output"));
1588f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mCurOutput = valueInt;
1589f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1590f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) {
1591f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_direct"));
1592f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (value == "false") {
1593f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    mDirectOutput = false;
1594f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else if (value == "true") {
1595f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    mDirectOutput = true;
1596f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1597f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1598f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) {
1599f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_input"));
1600f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mTestInput = valueInt;
1601f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1602f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1603f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) {
1604f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_format"));
1605f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                int format = AudioSystem::INVALID_FORMAT;
1606f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (value == "PCM 16 bits") {
1607f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    format = AudioSystem::PCM_16_BIT;
1608f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else if (value == "PCM 8 bits") {
1609f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    format = AudioSystem::PCM_8_BIT;
1610f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else if (value == "Compressed MP3") {
1611f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    format = AudioSystem::MP3;
1612f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1613f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (format != AudioSystem::INVALID_FORMAT) {
1614f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    if (target == "Manager") {
1615f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mTestFormat = format;
1616f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    } else if (mTestOutputs[mCurOutput] != 0) {
1617f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        AudioParameter outputParam = AudioParameter();
1618f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        outputParam.addInt(String8("format"), format);
1619f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
1620f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    }
1621f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1622f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1623f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) {
1624f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_channels"));
1625f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                int channels = 0;
1626f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1627f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (value == "Channels Stereo") {
1628f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    channels =  AudioSystem::CHANNEL_OUT_STEREO;
1629f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else if (value == "Channels Mono") {
1630f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    channels =  AudioSystem::CHANNEL_OUT_MONO;
1631f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1632f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (channels != 0) {
1633f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    if (target == "Manager") {
1634f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mTestChannels = channels;
1635f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    } else if (mTestOutputs[mCurOutput] != 0) {
1636f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        AudioParameter outputParam = AudioParameter();
1637f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        outputParam.addInt(String8("channels"), channels);
1638f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
1639f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    }
1640f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1641f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1642f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) {
1643f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_sampleRate"));
1644f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (valueInt >= 0 && valueInt <= 96000) {
1645f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    int samplingRate = valueInt;
1646f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    if (target == "Manager") {
1647f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mTestSamplingRate = samplingRate;
1648f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    } else if (mTestOutputs[mCurOutput] != 0) {
1649f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        AudioParameter outputParam = AudioParameter();
1650f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        outputParam.addInt(String8("sampling_rate"), samplingRate);
1651f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
1652f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    }
1653f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1654f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1655f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1656f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) {
1657f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_reopen"));
1658f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
165970c236c9290732782d5267935af1475b8d5ae602Eric Laurent                AudioOutputDescriptor *outputDesc = mOutputs.valueFor(mPrimaryOutput);
1660b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                mpClientInterface->closeOutput(mPrimaryOutput);
166170c236c9290732782d5267935af1475b8d5ae602Eric Laurent
166270c236c9290732782d5267935af1475b8d5ae602Eric Laurent                audio_module_handle_t moduleHandle = outputDesc->mModule->mHandle;
166370c236c9290732782d5267935af1475b8d5ae602Eric Laurent
1664b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                delete mOutputs.valueFor(mPrimaryOutput);
1665b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                mOutputs.removeItem(mPrimaryOutput);
1666f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1667b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(NULL);
1668c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER;
166970c236c9290732782d5267935af1475b8d5ae602Eric Laurent                mPrimaryOutput = mpClientInterface->openOutput(moduleHandle,
167070c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mDevice,
1671f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                &outputDesc->mSamplingRate,
1672f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                &outputDesc->mFormat,
167370c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mChannelMask,
1674f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                &outputDesc->mLatency,
1675f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                outputDesc->mFlags);
1676b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                if (mPrimaryOutput == 0) {
16775efbd421e0029d6fc44b1cc65c0e5e0d85e5161fSteve Block                    ALOGE("Failed to reopen hardware output stream, samplingRate: %d, format %d, channels %d",
167870c236c9290732782d5267935af1475b8d5ae602Eric Laurent                            outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask);
1679f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else {
1680f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    AudioParameter outputCmd = AudioParameter();
1681f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    outputCmd.addInt(String8("set_id"), 0);
1682b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                    mpClientInterface->setParameters(mPrimaryOutput, outputCmd.toString());
1683b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                    addOutput(mPrimaryOutput, outputDesc);
1684f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1685f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1686f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1687f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1688f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mpClientInterface->setParameters(0, String8("test_cmd_policy="));
1689f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1690f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1691f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return false;
1692f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1693f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1694f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::exit()
1695f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1696f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    {
1697f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AutoMutex _l(mLock);
1698f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        requestExit();
1699f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mWaitWorkCV.signal();
1700f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1701f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    requestExitAndWait();
1702f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1703f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1704f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinint AudioPolicyManagerBase::testOutputIndex(audio_io_handle_t output)
1705f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1706f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
1707f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (output == mTestOutputs[i]) return i;
1708f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1709f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return 0;
1710f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1711f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
1712f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1713f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// ---
1714f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1715f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::addOutput(audio_io_handle_t id, AudioOutputDescriptor *outputDesc)
1716f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1717f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    outputDesc->mId = id;
1718f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mOutputs.add(id, outputDesc);
1719f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1720f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1721f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
17223cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurentstatus_t AudioPolicyManagerBase::checkOutputsForDevice(audio_devices_t device,
17233cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                       AudioSystem::device_connection_state state,
17243cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                       SortedVector<audio_io_handle_t>& outputs)
1725f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
17263cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    AudioOutputDescriptor *desc;
1727b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1728b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (state == AudioSystem::DEVICE_STATE_AVAILABLE) {
17293cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        // first list already open outputs that can be routed to this device
1730b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        for (size_t i = 0; i < mOutputs.size(); i++) {
17313cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            desc = mOutputs.valueAt(i);
17323cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            if (!desc->isDuplicated() && (desc->mProfile->mSupportedDevices & device)) {
17333cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i));
17343cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                outputs.add(mOutputs.keyAt(i));
1735b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            }
1736b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        }
17373cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        // then look for output profiles that can be routed to this device
17383cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        SortedVector<IOProfile *> profiles;
173970c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (size_t i = 0; i < mHwModules.size(); i++)
1740b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        {
174170c236c9290732782d5267935af1475b8d5ae602Eric Laurent            if (mHwModules[i]->mHandle == 0) {
174270c236c9290732782d5267935af1475b8d5ae602Eric Laurent                continue;
174370c236c9290732782d5267935af1475b8d5ae602Eric Laurent            }
174470c236c9290732782d5267935af1475b8d5ae602Eric Laurent            for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
174570c236c9290732782d5267935af1475b8d5ae602Eric Laurent            {
174670c236c9290732782d5267935af1475b8d5ae602Eric Laurent                if (mHwModules[i]->mOutputProfiles[j]->mSupportedDevices & device) {
17473cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    ALOGV("checkOutputsForDevice(): adding profile %d from module %d", j, i);
17483cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    profiles.add(mHwModules[i]->mOutputProfiles[j]);
174970c236c9290732782d5267935af1475b8d5ae602Eric Laurent                }
175070c236c9290732782d5267935af1475b8d5ae602Eric Laurent            }
1751b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        }
175270c236c9290732782d5267935af1475b8d5ae602Eric Laurent
17533cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        if (profiles.isEmpty() && outputs.isEmpty()) {
17543cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
17553cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            return BAD_VALUE;
175670c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
175770c236c9290732782d5267935af1475b8d5ae602Eric Laurent
17583cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        // open outputs for matching profiles if needed. Direct outputs are also opened to
17593cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        // query for dynamic parameters and will be closed later by setDeviceConnectionState()
17603cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
17613cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            IOProfile *profile = profiles[profile_index];
1762b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
17633cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            // nothing to do if one output is already opened for this profile
17643cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            size_t j;
1765fd8cecbee6843b444d56a1db40af76027e2b19f1Eric Laurent            for (j = 0; j < mOutputs.size(); j++) {
17663cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                desc = mOutputs.valueAt(j);
17673cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                if (!desc->isDuplicated() && desc->mProfile == profile) {
17683cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    break;
17693cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                }
17703cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
1771fd8cecbee6843b444d56a1db40af76027e2b19f1Eric Laurent            if (j != mOutputs.size()) {
17723cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                continue;
17733cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
17743cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
17753cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            ALOGV("opening output for device %08x", device);
17763cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            desc = new AudioOutputDescriptor(profile);
17773cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            desc->mDevice = device;
17783cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            audio_io_handle_t output = mpClientInterface->openOutput(profile->mModule->mHandle,
17793cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                       &desc->mDevice,
17803cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                       &desc->mSamplingRate,
17813cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                       &desc->mFormat,
17823cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                       &desc->mChannelMask,
17833cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                       &desc->mLatency,
17843cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                       desc->mFlags);
17853cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            if (output != 0) {
17863cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
17873cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    String8 reply;
17883cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    char *value;
17893cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    if (profile->mSamplingRates[0] == 0) {
17903cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        reply = mpClientInterface->getParameters(output,
17913cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES));
17923cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        ALOGV("checkOutputsForDevice() direct output sup sampling rates %s",
17933cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                  reply.string());
17943cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        value = strpbrk((char *)reply.string(), "=");
17953cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        if (value != NULL) {
17965debe92591363de3d73bc32b62f23df707b1aecfJohn Grossman                            loadSamplingRates(value + 1, profile);
17973cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        }
17983cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
17993cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    if (profile->mFormats[0] == 0) {
18003cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        reply = mpClientInterface->getParameters(output,
18013cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                       String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
18023cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        ALOGV("checkOutputsForDevice() direct output sup formats %s",
18033cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                  reply.string());
18043cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        value = strpbrk((char *)reply.string(), "=");
18053cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        if (value != NULL) {
18065debe92591363de3d73bc32b62f23df707b1aecfJohn Grossman                            loadFormats(value + 1, profile);
18073cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        }
18083cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
18093cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    if (profile->mChannelMasks[0] == 0) {
18103cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        reply = mpClientInterface->getParameters(output,
18113cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                      String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS));
18123cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        ALOGV("checkOutputsForDevice() direct output sup channel masks %s",
18133cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                  reply.string());
18143cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        value = strpbrk((char *)reply.string(), "=");
18153cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        if (value != NULL) {
18163cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                            loadOutChannels(value + 1, profile);
18173cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        }
18183cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
18193cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    if (((profile->mSamplingRates[0] == 0) &&
18203cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                             (profile->mSamplingRates.size() < 2)) ||
18213cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                         ((profile->mFormats[0] == 0) &&
18223cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                             (profile->mFormats.size() < 2)) ||
18233cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                         ((profile->mFormats[0] == 0) &&
18243cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                             (profile->mChannelMasks.size() < 2))) {
18253cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        ALOGW("checkOutputsForDevice() direct output missing param");
182610705c404c48ec9ac4ce47912e7731f9249cce1dJason Simmons                        mpClientInterface->closeOutput(output);
18273cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        output = 0;
18283cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    } else {
18293cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        addOutput(output, desc);
18303cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
18313cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                } else {
18323cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    audio_io_handle_t duplicatedOutput = 0;
18333cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    // add output descriptor
18343cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    addOutput(output, desc);
18353cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    // set initial stream volume for device
18364366b4a6735e5da342b56773073f0b41197c777fEric Laurent                    applyStreamVolumes(output, device, 0, true);
18373cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
18383cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    //TODO: configure audio effect output stage here
18393cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
18403cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    // open a duplicating output thread for the new output and the primary output
18413cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    duplicatedOutput = mpClientInterface->openDuplicateOutput(output,
18423cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                              mPrimaryOutput);
18433cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    if (duplicatedOutput != 0) {
18443cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        // add duplicated output descriptor
18453cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        AudioOutputDescriptor *dupOutputDesc = new AudioOutputDescriptor(NULL);
18463cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        dupOutputDesc->mOutput1 = mOutputs.valueFor(mPrimaryOutput);
18473cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        dupOutputDesc->mOutput2 = mOutputs.valueFor(output);
18483cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        dupOutputDesc->mSamplingRate = desc->mSamplingRate;
18493cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        dupOutputDesc->mFormat = desc->mFormat;
18503cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        dupOutputDesc->mChannelMask = desc->mChannelMask;
18513cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        dupOutputDesc->mLatency = desc->mLatency;
18523cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        addOutput(duplicatedOutput, dupOutputDesc);
18534366b4a6735e5da342b56773073f0b41197c777fEric Laurent                        applyStreamVolumes(duplicatedOutput, device, 0, true);
18543cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    } else {
18553cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        ALOGW("checkOutputsForDevice() could not open dup output for %d and %d",
18563cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                mPrimaryOutput, output);
18573cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        mpClientInterface->closeOutput(output);
18583cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        mOutputs.removeItem(output);
18593cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        output = 0;
18603cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
18613cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                }
18623cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
18633cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            if (output == 0) {
18643cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                ALOGW("checkOutputsForDevice() could not open output for device %x", device);
18653cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                delete desc;
18663cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                profiles.removeAt(profile_index);
18673cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                profile_index--;
1868b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            } else {
18693cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                outputs.add(output);
18703cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                ALOGV("checkOutputsForDevice(): adding output %d", output);
1871f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
18723cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        }
18733cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
18743cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        if (profiles.isEmpty()) {
18753cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
18763cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            return BAD_VALUE;
1877f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1878f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
1879b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        // check if one opened output is not needed any more after disconnecting one device
1880b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        for (size_t i = 0; i < mOutputs.size(); i++) {
18813cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            desc = mOutputs.valueAt(i);
18823cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            if (!desc->isDuplicated() &&
18833cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    !(desc->mProfile->mSupportedDevices & mAvailableOutputDevices)) {
18843cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                ALOGV("checkOutputsForDevice(): disconnecting adding output %d", mOutputs.keyAt(i));
18853cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                outputs.add(mOutputs.keyAt(i));
18863cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
18873cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        }
18883cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        for (size_t i = 0; i < mHwModules.size(); i++)
18893cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        {
18903cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            if (mHwModules[i]->mHandle == 0) {
18913cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                continue;
18923cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
18933cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
18943cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            {
18953cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                IOProfile *profile = mHwModules[i]->mOutputProfiles[j];
18963cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                if ((profile->mSupportedDevices & device) &&
18973cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        (profile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
18983cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    ALOGV("checkOutputsForDevice(): clearing direct output profile %d on module %d",
18993cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                          j, i);
19003cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    if (profile->mSamplingRates[0] == 0) {
19013cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        profile->mSamplingRates.clear();
19023cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        profile->mSamplingRates.add(0);
19033cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
19043cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    if (profile->mFormats[0] == 0) {
19053cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        profile->mFormats.clear();
19063cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        profile->mFormats.add((audio_format_t)0);
19073cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
19083cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    if (profile->mChannelMasks[0] == 0) {
19093cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        profile->mChannelMasks.clear();
19103cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        profile->mChannelMasks.add((audio_channel_mask_t)0);
19113cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
19123cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                }
1913b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            }
1914f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1915f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
19163cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    return NO_ERROR;
1917f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1918f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1919b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurentvoid AudioPolicyManagerBase::closeOutput(audio_io_handle_t output)
1920f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1921b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    ALOGV("closeOutput(%d)", output);
1922f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1923b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
1924b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (outputDesc == NULL) {
1925b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        ALOGW("closeOutput() unknown output %d", output);
1926b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        return;
1927f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1928f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1929b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    // look for duplicated outputs connected to the output being removed.
1930b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    for (size_t i = 0; i < mOutputs.size(); i++) {
1931b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        AudioOutputDescriptor *dupOutputDesc = mOutputs.valueAt(i);
1932b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        if (dupOutputDesc->isDuplicated() &&
1933b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                (dupOutputDesc->mOutput1 == outputDesc ||
1934b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                dupOutputDesc->mOutput2 == outputDesc)) {
1935b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            AudioOutputDescriptor *outputDesc2;
1936b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            if (dupOutputDesc->mOutput1 == outputDesc) {
1937b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                outputDesc2 = dupOutputDesc->mOutput2;
1938b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            } else {
1939b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                outputDesc2 = dupOutputDesc->mOutput1;
1940b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            }
1941b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            // As all active tracks on duplicated output will be deleted,
1942b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            // and as they were also referenced on the other output, the reference
1943b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            // count for their stream type must be adjusted accordingly on
1944b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            // the other output.
1945b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            for (int j = 0; j < (int)AudioSystem::NUM_STREAM_TYPES; j++) {
1946b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                int refCount = dupOutputDesc->mRefCount[j];
1947b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                outputDesc2->changeRefCount((AudioSystem::stream_type)j,-refCount);
1948b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            }
1949b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i);
1950b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput);
1951f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1952b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            mpClientInterface->closeOutput(duplicatedOutput);
1953b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            delete mOutputs.valueFor(duplicatedOutput);
1954b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            mOutputs.removeItem(duplicatedOutput);
1955f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1956f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1957b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1958b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    AudioParameter param;
1959b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    param.add(String8("closing"), String8("true"));
1960b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    mpClientInterface->setParameters(output, param.toString());
1961b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1962b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    mpClientInterface->closeOutput(output);
19635a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent    delete outputDesc;
1964b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    mOutputs.removeItem(output);
19655a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent    mPreviousOutputs = mOutputs;
1966f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1967f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1968c952527e6f89d5427881462823514be9d79f13e6Eric LaurentSortedVector<audio_io_handle_t> AudioPolicyManagerBase::getOutputsForDevice(audio_devices_t device,
1969c952527e6f89d5427881462823514be9d79f13e6Eric Laurent                        DefaultKeyedVector<audio_io_handle_t, AudioOutputDescriptor *> openOutputs)
1970f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1971b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    SortedVector<audio_io_handle_t> outputs;
1972f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
19733cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGVV("getOutputsForDevice() device %04x", device);
1974c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    for (size_t i = 0; i < openOutputs.size(); i++) {
19753cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        ALOGVV("output %d isDuplicated=%d device=%04x",
1976c952527e6f89d5427881462823514be9d79f13e6Eric Laurent                i, openOutputs.valueAt(i)->isDuplicated(), openOutputs.valueAt(i)->supportedDevices());
1977c952527e6f89d5427881462823514be9d79f13e6Eric Laurent        if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) {
1978c952527e6f89d5427881462823514be9d79f13e6Eric Laurent            ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i));
1979c952527e6f89d5427881462823514be9d79f13e6Eric Laurent            outputs.add(openOutputs.keyAt(i));
1980f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1981f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1982b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    return outputs;
1983f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1984f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1985b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurentbool AudioPolicyManagerBase::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
1986b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                                   SortedVector<audio_io_handle_t>& outputs2)
1987f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1988b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (outputs1.size() != outputs2.size()) {
1989b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        return false;
1990f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1991b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    for (size_t i = 0; i < outputs1.size(); i++) {
1992b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        if (outputs1[i] != outputs2[i]) {
1993b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            return false;
1994f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1995f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1996b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    return true;
1997b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent}
1998b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1999b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurentvoid AudioPolicyManagerBase::checkOutputForStrategy(routing_strategy strategy)
2000b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent{
200101e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent    audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
200201e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent    audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
2003c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs);
2004c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
2005b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
2006b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (!vectorsEqual(srcOutputs,dstOutputs)) {
2007b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d",
2008b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent              strategy, srcOutputs[0], dstOutputs[0]);
20095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // mute strategy while moving tracks from one output to another
20105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        for (size_t i = 0; i < srcOutputs.size(); i++) {
2011fa3697d716b444bbea6be480801536c44bf69214Eric Laurent            AudioOutputDescriptor *desc = mOutputs.valueFor(srcOutputs[i]);
201280f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent            if (desc->isStrategyActive(strategy)) {
2013fa3697d716b444bbea6be480801536c44bf69214Eric Laurent                setStrategyMute(strategy, true, srcOutputs[i]);
2014fa3697d716b444bbea6be480801536c44bf69214Eric Laurent                setStrategyMute(strategy, false, srcOutputs[i], MUTE_TIME_MS, newDevice);
2015fa3697d716b444bbea6be480801536c44bf69214Eric Laurent            }
20165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
2017f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2018f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // Move effects associated to this strategy from previous output to new output
20194660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen        if (strategy == STRATEGY_MEDIA) {
20204660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen            int outIdx = 0;
20214660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen            for (size_t i = 0; i < dstOutputs.size(); i++) {
20224660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                AudioOutputDescriptor *desc = mOutputs.valueFor(dstOutputs[i]);
20234660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                if (desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
20244660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                    outIdx = i;
20254660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                }
20264660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen            }
20274660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen            SortedVector<audio_io_handle_t> moved;
20284660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen            for (size_t i = 0; i < mEffects.size(); i++) {
20294660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                EffectDescriptor *desc = mEffects.valueAt(i);
20304660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                if (desc->mSession == AUDIO_SESSION_OUTPUT_MIX &&
20314660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                        desc->mIo != dstOutputs[outIdx]) {
20324660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                    if (moved.indexOf(desc->mIo) < 0) {
20334660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                        ALOGV("checkOutputForStrategy() moving effect %d to output %d",
20344660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                              mEffects.keyAt(i), dstOutputs[outIdx]);
20354660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                        mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, desc->mIo,
20364660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                                                       dstOutputs[outIdx]);
20374660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                        moved.add(desc->mIo);
20384660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                    }
20394660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                    desc->mIo = dstOutputs[outIdx];
20404660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                }
20414660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen            }
20424660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen        }
20435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // Move tracks associated to this strategy from previous output to new output
2044f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        for (int i = 0; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) {
2045f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (getStrategy((AudioSystem::stream_type)i) == strategy) {
204612bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi                //FIXME see fixme on name change
20474660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                mpClientInterface->setStreamOutput((AudioSystem::stream_type)i,
20484660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                                                   dstOutputs[0] /* ignored */);
2049f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2050f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2051f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2052f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2053f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2054f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::checkOutputForAllStrategies()
2055f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2056c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
2057f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForStrategy(STRATEGY_PHONE);
2058f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForStrategy(STRATEGY_SONIFICATION);
205912bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
2060f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForStrategy(STRATEGY_MEDIA);
2061f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForStrategy(STRATEGY_DTMF);
2062f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2063f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2064b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurentaudio_io_handle_t AudioPolicyManagerBase::getA2dpOutput()
2065b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent{
20665ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (!mHasA2dp) {
2067b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        return 0;
2068b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    }
2069b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
2070b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    for (size_t i = 0; i < mOutputs.size(); i++) {
2071b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        AudioOutputDescriptor *outputDesc = mOutputs.valueAt(i);
2072b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        if (!outputDesc->isDuplicated() && outputDesc->device() & AUDIO_DEVICE_OUT_ALL_A2DP) {
2073b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            return mOutputs.keyAt(i);
2074b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        }
2075b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    }
2076b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
2077b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    return 0;
2078b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent}
2079b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
2080f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::checkA2dpSuspend()
2081f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
20825ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (!mHasA2dp) {
2083b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        return;
2084b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    }
2085b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    audio_io_handle_t a2dpOutput = getA2dpOutput();
2086b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (a2dpOutput == 0) {
2087b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        return;
2088b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    }
2089b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
2090f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // suspend A2DP output if:
2091f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      (NOT already suspended) &&
2092f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      ((SCO device is connected &&
2093f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //       (forced usage for communication || for record is SCO))) ||
2094f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      (phone state is ringing || in call)
2095f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //
2096f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // restore A2DP output if:
2097f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      (Already suspended) &&
2098f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      ((SCO device is NOT connected ||
2099f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //       (forced usage NOT for communication && NOT for record is SCO))) &&
2100f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      (phone state is NOT ringing && NOT in call)
2101f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //
2102f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mA2dpSuspended) {
2103f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (((mScoDeviceAddress == "") ||
2104f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin             ((mForceUse[AudioSystem::FOR_COMMUNICATION] != AudioSystem::FORCE_BT_SCO) &&
2105f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin              (mForceUse[AudioSystem::FOR_RECORD] != AudioSystem::FORCE_BT_SCO))) &&
2106f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin             ((mPhoneState != AudioSystem::MODE_IN_CALL) &&
2107f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin              (mPhoneState != AudioSystem::MODE_RINGTONE))) {
2108f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2109b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            mpClientInterface->restoreOutput(a2dpOutput);
2110f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mA2dpSuspended = false;
2111f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2112f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
2113f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (((mScoDeviceAddress != "") &&
2114f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin             ((mForceUse[AudioSystem::FOR_COMMUNICATION] == AudioSystem::FORCE_BT_SCO) ||
2115f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin              (mForceUse[AudioSystem::FOR_RECORD] == AudioSystem::FORCE_BT_SCO))) ||
2116f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin             ((mPhoneState == AudioSystem::MODE_IN_CALL) ||
2117f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin              (mPhoneState == AudioSystem::MODE_RINGTONE))) {
2118f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2119b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            mpClientInterface->suspendOutput(a2dpOutput);
2120f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mA2dpSuspended = true;
2121f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2122f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2123f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2124f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2125f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurentaudio_devices_t AudioPolicyManagerBase::getNewDevice(audio_io_handle_t output, bool fromCache)
2126f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2127ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    audio_devices_t device = AUDIO_DEVICE_NONE;
2128f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2129f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
2130f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // check the following by order of priority to request a routing change if necessary:
2131c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    // 1: the strategy enforced audible is active on the output:
2132c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    //      use device for strategy enforced audible
2133c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    // 2: we are in call or the strategy phone is active on the output:
2134f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      use device for strategy phone
2135c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    // 3: the strategy sonification is active on the output:
2136f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      use device for strategy sonification
213712bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    // 4: the strategy "respectful" sonification is active on the output:
213812bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    //      use device for strategy "respectful" sonification
213912bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    // 5: the strategy media is active on the output:
2140f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      use device for strategy media
214112bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    // 6: the strategy DTMF is active on the output:
2142f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      use device for strategy DTMF
214380f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    if (outputDesc->isStrategyActive(STRATEGY_ENFORCED_AUDIBLE)) {
2144c16ac09f510437e8340be691720177a490ae78f0Eric Laurent        device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
2145c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    } else if (isInCall() ||
214680f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                    outputDesc->isStrategyActive(STRATEGY_PHONE)) {
2147f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
214880f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    } else if (outputDesc->isStrategyActive(STRATEGY_SONIFICATION)) {
2149f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
215080f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    } else if (outputDesc->isStrategyActive(STRATEGY_SONIFICATION_RESPECTFUL)) {
21515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
215280f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    } else if (outputDesc->isStrategyActive(STRATEGY_MEDIA)) {
2153f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
215480f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    } else if (outputDesc->isStrategyActive(STRATEGY_DTMF)) {
2155f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
2156f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2157f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
21586a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("getNewDevice() selected device %x", device);
2159f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return device;
2160f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2161f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2162f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinuint32_t AudioPolicyManagerBase::getStrategyForStream(AudioSystem::stream_type stream) {
2163f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return (uint32_t)getStrategy(stream);
2164f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2165f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2166f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurentaudio_devices_t AudioPolicyManagerBase::getDevicesForStream(AudioSystem::stream_type stream) {
2167f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent    audio_devices_t devices;
2168f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // By checking the range of stream before calling getStrategy, we avoid
21695efbd421e0029d6fc44b1cc65c0e5e0d85e5161fSteve Block    // getStrategy's behavior for invalid streams.  getStrategy would do a ALOGE
2170f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // and then return STRATEGY_MEDIA, but we want to return the empty set.
2171f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (stream < (AudioSystem::stream_type) 0 || stream >= AudioSystem::NUM_STREAM_TYPES) {
2172ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        devices = AUDIO_DEVICE_NONE;
2173f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
2174f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioPolicyManagerBase::routing_strategy strategy = getStrategy(stream);
21755ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        devices = getDeviceForStrategy(strategy, true /*fromCache*/);
2176f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2177f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return devices;
2178f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2179f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2180f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima ZavinAudioPolicyManagerBase::routing_strategy AudioPolicyManagerBase::getStrategy(
2181f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioSystem::stream_type stream) {
2182f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // stream to strategy mapping
2183f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    switch (stream) {
2184f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::VOICE_CALL:
2185f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::BLUETOOTH_SCO:
2186f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return STRATEGY_PHONE;
2187f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::RING:
2188f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::ALARM:
2189f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return STRATEGY_SONIFICATION;
219012bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    case AudioSystem::NOTIFICATION:
219112bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        return STRATEGY_SONIFICATION_RESPECTFUL;
2192f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::DTMF:
2193f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return STRATEGY_DTMF;
2194f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    default:
21955efbd421e0029d6fc44b1cc65c0e5e0d85e5161fSteve Block        ALOGE("unknown stream type");
2196f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::SYSTEM:
2197f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // NOTE: SYSTEM stream uses MEDIA strategy because muting music and switching outputs
2198f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // while key clicks are played produces a poor result
2199f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::TTS:
2200f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::MUSIC:
2201f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return STRATEGY_MEDIA;
2202c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    case AudioSystem::ENFORCED_AUDIBLE:
2203c16ac09f510437e8340be691720177a490ae78f0Eric Laurent        return STRATEGY_ENFORCED_AUDIBLE;
2204f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2205f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2206f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
220712bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivivoid AudioPolicyManagerBase::handleNotificationRoutingForStream(AudioSystem::stream_type stream) {
220812bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    switch(stream) {
220912bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    case AudioSystem::MUSIC:
221012bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
2211c952527e6f89d5427881462823514be9d79f13e6Eric Laurent        updateDevicesAndOutputs();
221212bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        break;
221312bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    default:
221412bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        break;
221512bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    }
221612bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi}
221712bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi
22185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentaudio_devices_t AudioPolicyManagerBase::getDeviceForStrategy(routing_strategy strategy,
22195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                             bool fromCache)
2220f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2221ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    uint32_t device = AUDIO_DEVICE_NONE;
2222f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2223f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (fromCache) {
22243cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
22255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent              strategy, mDeviceForStrategy[strategy]);
2226f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return mDeviceForStrategy[strategy];
2227f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2228f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2229f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    switch (strategy) {
223012bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi
223112bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    case STRATEGY_SONIFICATION_RESPECTFUL:
223212bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        if (isInCall()) {
22335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
2234dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi        } else if (isStreamActiveRemotely(AudioSystem::MUSIC,
2235dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi                SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
2236dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi            // while media is playing on a remote device, use the the sonification behavior.
2237dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi            // Note that we test this usecase before testing if media is playing because
2238dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi            //   the isStreamActive() method only informs about the activity of a stream, not
2239dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi            //   if it's for local playback. Note also that we use the same delay between both tests
2240dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi            device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
224112bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        } else if (isStreamActive(AudioSystem::MUSIC, SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
224212bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi            // while media is playing (or has recently played), use the same device
22435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/);
224412bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        } else {
224512bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi            // when media is not playing anymore, fall back on the sonification behavior
22465ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
224712bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        }
224812bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi
224912bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        break;
225012bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi
2251f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case STRATEGY_DTMF:
2252f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (!isInCall()) {
2253f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // when off call, DTMF strategy follows the same rules as MEDIA strategy
22545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/);
2255f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            break;
2256f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2257f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // when in call, DTMF and PHONE strategies follow the same rules
2258f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // FALL THROUGH
2259f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2260f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case STRATEGY_PHONE:
2261f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // for phone strategy, we first consider the forced use and then the available devices by order
2262f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // of priority
2263f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        switch (mForceUse[AudioSystem::FOR_COMMUNICATION]) {
2264f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        case AudioSystem::FORCE_BT_SCO:
2265f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (!isInCall() || strategy != STRATEGY_DTMF) {
2266c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT;
2267f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (device) break;
2268f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2269c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET;
2270f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (device) break;
2271c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO;
2272f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (device) break;
2273f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // if SCO device is requested but no SCO device is available, fall back to default case
2274f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // FALL THROUGH
2275f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2276f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        default:    // FORCE_NONE
2277f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to A2DP
22781afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent            if (mHasA2dp && !isInCall() &&
22791afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent                    (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
2280ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent                    (getA2dpOutput() != 0) && !mA2dpSuspended) {
2281c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
2282f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (device) break;
2283c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
2284f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (device) break;
2285f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2286c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
22871afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent            if (device) break;
2288c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADSET;
22891afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent            if (device) break;
2290b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent            if (mPhoneState != AudioSystem::MODE_IN_CALL) {
2291b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
2292b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2293b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
2294b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2295b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
2296b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2297b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
2298b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2299b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
2300b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2301b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent            }
2302c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_EARPIECE;
23035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            if (device) break;
23045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = mDefaultOutputDevice;
2305ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            if (device == AUDIO_DEVICE_NONE) {
23065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE");
2307f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2308f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            break;
2309f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2310f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        case AudioSystem::FORCE_SPEAKER:
2311f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to
2312f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // A2DP speaker when forcing to speaker output
23131afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent            if (mHasA2dp && !isInCall() &&
23141afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent                    (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
2315ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent                    (getA2dpOutput() != 0) && !mA2dpSuspended) {
2316c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
2317f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (device) break;
2318f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2319b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent            if (mPhoneState != AudioSystem::MODE_IN_CALL) {
2320b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
2321b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2322b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
2323b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2324b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
2325b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2326b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
2327b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2328b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
2329b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2330b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent            }
2331c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER;
23325ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            if (device) break;
23335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = mDefaultOutputDevice;
2334ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            if (device == AUDIO_DEVICE_NONE) {
23355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE, FORCE_SPEAKER");
2336f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2337f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            break;
2338f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2339f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    break;
2340f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2341f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case STRATEGY_SONIFICATION:
2342f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2343f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // If incall, just select the STRATEGY_PHONE device: The rest of the behavior is handled by
2344f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // handleIncallSonification().
2345f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (isInCall()) {
23465ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = getDeviceForStrategy(STRATEGY_PHONE, false /*fromCache*/);
2347f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            break;
2348f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2349c16ac09f510437e8340be691720177a490ae78f0Eric Laurent        // FALL THROUGH
2350c16ac09f510437e8340be691720177a490ae78f0Eric Laurent
2351c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    case STRATEGY_ENFORCED_AUDIBLE:
2352c16ac09f510437e8340be691720177a490ae78f0Eric Laurent        // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION
2353ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        // except:
2354ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        //   - when in call where it doesn't default to STRATEGY_PHONE behavior
2355ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        //   - in countries where not enforced in which case it follows STRATEGY_MEDIA
2356c16ac09f510437e8340be691720177a490ae78f0Eric Laurent
2357738207def5f691d605ae33d041116829a74513a9Eric Laurent        if ((strategy == STRATEGY_SONIFICATION) ||
2358738207def5f691d605ae33d041116829a74513a9Eric Laurent                (mForceUse[AudioSystem::FOR_SYSTEM] == AudioSystem::FORCE_SYSTEM_ENFORCED)) {
2359c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER;
2360ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            if (device == AUDIO_DEVICE_NONE) {
2361ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent                ALOGE("getDeviceForStrategy() speaker device not found for STRATEGY_SONIFICATION");
2362ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent            }
2363f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2364f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // The second device used for sonification is the same as the device used by media strategy
2365f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // FALL THROUGH
2366f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2367f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case STRATEGY_MEDIA: {
2368ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        uint32_t device2 = AUDIO_DEVICE_NONE;
236931363a9cb94e80330c335fede0b92b1953a09517Jean-Michel Trivi        if (strategy != STRATEGY_SONIFICATION) {
237031363a9cb94e80330c335fede0b92b1953a09517Jean-Michel Trivi            // no sonification on remote submix (e.g. WFD)
237148387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
237248387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi        }
2373ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if ((device2 == AUDIO_DEVICE_NONE) &&
237448387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi                mHasA2dp && (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
2375ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent                (getA2dpOutput() != 0) && !mA2dpSuspended) {
2376c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
2377ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            if (device2 == AUDIO_DEVICE_NONE) {
2378c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
2379f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2380ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            if (device2 == AUDIO_DEVICE_NONE) {
2381c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
2382f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2383f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2384ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (device2 == AUDIO_DEVICE_NONE) {
2385c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
23861afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent        }
2387ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (device2 == AUDIO_DEVICE_NONE) {
2388c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADSET;
23891afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent        }
2390ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (device2 == AUDIO_DEVICE_NONE) {
2391599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
2392599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent        }
2393ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (device2 == AUDIO_DEVICE_NONE) {
2394599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
2395599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent        }
2396ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (device2 == AUDIO_DEVICE_NONE) {
2397c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
2398f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
239931363a9cb94e80330c335fede0b92b1953a09517Jean-Michel Trivi        if ((device2 == AUDIO_DEVICE_NONE) && (strategy != STRATEGY_SONIFICATION)) {
240031363a9cb94e80330c335fede0b92b1953a09517Jean-Michel Trivi            // no sonification on aux digital (e.g. HDMI)
2401c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
2402f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
24035a484b753cc72d6a50c1dd3bbf68b3403c741a3aEric Laurent        if ((device2 == AUDIO_DEVICE_NONE) &&
24045a484b753cc72d6a50c1dd3bbf68b3403c741a3aEric Laurent                (mForceUse[AudioSystem::FOR_DOCK] == AudioSystem::FORCE_ANALOG_DOCK)) {
2405c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
2406f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2407ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (device2 == AUDIO_DEVICE_NONE) {
2408c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER;
2409f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2410f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2411c16ac09f510437e8340be691720177a490ae78f0Eric Laurent        // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or
2412ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise
2413f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device |= device2;
24145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (device) break;
24155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        device = mDefaultOutputDevice;
2416ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (device == AUDIO_DEVICE_NONE) {
24175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGE("getDeviceForStrategy() no device found for STRATEGY_MEDIA");
2418f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2419f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        } break;
2420f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2421f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    default:
242264cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("getDeviceForStrategy() unknown strategy: %d", strategy);
2423f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
2424f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2425f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
24263cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGVV("getDeviceForStrategy() strategy %d, device %x", strategy, device);
2427c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    return device;
2428f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2429f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2430c952527e6f89d5427881462823514be9d79f13e6Eric Laurentvoid AudioPolicyManagerBase::updateDevicesAndOutputs()
2431f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2432f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < NUM_STRATEGIES; i++) {
24335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
24345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
2435c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    mPreviousOutputs = mOutputs;
24365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
24375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
2438b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurentuint32_t AudioPolicyManagerBase::checkDeviceMuteStrategies(AudioOutputDescriptor *outputDesc,
24399029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                                                       audio_devices_t prevDevice,
24405ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                       uint32_t delayMs)
24415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
24429029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    // mute/unmute strategies using an incompatible device combination
24439029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    // if muting, wait for the audio in pcm buffer to be drained before proceeding
24449029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    // if unmuting, unmute only after the specified delay
24455ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (outputDesc->isDuplicated()) {
2446b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        return 0;
24475ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
24485ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
24495ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    uint32_t muteWaitMs = 0;
24505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    audio_devices_t device = outputDesc->device();
245180f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    bool shouldMute = outputDesc->isActive() && (AudioSystem::popCount(device) >= 2);
24529029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    // temporary mute output if device selection changes to avoid volume bursts due to
24539029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    // different per device volumes
245480f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    bool tempMute = outputDesc->isActive() && (device != prevDevice);
24555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
24565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < NUM_STRATEGIES; i++) {
24575ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
24585ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        bool mute = shouldMute && (curDevice & device) && (curDevice != device);
24595ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        bool doMute = false;
24605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
24615ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (mute && !outputDesc->mStrategyMutedByDevice[i]) {
24625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            doMute = true;
24635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            outputDesc->mStrategyMutedByDevice[i] = true;
24645ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){
24655ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            doMute = true;
24665ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            outputDesc->mStrategyMutedByDevice[i] = false;
24675ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
24689029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent        if (doMute || tempMute) {
24695ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            for (size_t j = 0; j < mOutputs.size(); j++) {
24705ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                AudioOutputDescriptor *desc = mOutputs.valueAt(j);
2471f32e38e24db196671d3ea43427125a4e212466faEric Laurent                // skip output if it does not share any device with current output
2472c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                if ((desc->supportedDevices() & outputDesc->supportedDevices())
2473ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent                        == AUDIO_DEVICE_NONE) {
24745ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                    continue;
24755ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                }
24765ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                audio_io_handle_t curOutput = mOutputs.keyAt(j);
24773cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x) on output %d",
24785ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                      mute ? "muting" : "unmuting", i, curDevice, curOutput);
24795ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                setStrategyMute((routing_strategy)i, mute, curOutput, mute ? 0 : delayMs);
248080f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                if (desc->isStrategyActive((routing_strategy)i)) {
2481f32e38e24db196671d3ea43427125a4e212466faEric Laurent                    // do tempMute only for current output
2482f32e38e24db196671d3ea43427125a4e212466faEric Laurent                    if (tempMute && (desc == outputDesc)) {
248301e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                        setStrategyMute((routing_strategy)i, true, curOutput);
248401e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                        setStrategyMute((routing_strategy)i, false, curOutput,
248501e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                            desc->latency() * 2, device);
24869029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                    }
2487f32e38e24db196671d3ea43427125a4e212466faEric Laurent                    if ((tempMute && (desc == outputDesc)) || mute) {
24889029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                        if (muteWaitMs < desc->latency()) {
24899029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                            muteWaitMs = desc->latency();
24909029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                        }
24915ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                    }
24925ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                }
24935ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
24945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
24955ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
24965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
24975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // FIXME: should not need to double latency if volume could be applied immediately by the
24985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // audioflinger mixer. We must account for the delay between now and the next time
24995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // the audioflinger thread for this output will process a buffer (which corresponds to
25005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // one buffer size, usually 1/2 or 1/4 of the latency).
25015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    muteWaitMs *= 2;
25025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // wait for the PCM output buffers to empty before proceeding with the rest of the command
25035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (muteWaitMs > delayMs) {
2504b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        muteWaitMs -= delayMs;
2505b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        usleep(muteWaitMs * 1000);
2506b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        return muteWaitMs;
2507f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2508b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent    return 0;
2509f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2510f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2511b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurentuint32_t AudioPolicyManagerBase::setOutputDevice(audio_io_handle_t output,
2512f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                                             audio_devices_t device,
2513f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                                             bool force,
2514f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                                             int delayMs)
2515f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
25165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGV("setOutputDevice() output %d device %04x delayMs %d", output, device, delayMs);
2517f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
25185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    AudioParameter param;
2519f32e38e24db196671d3ea43427125a4e212466faEric Laurent    uint32_t muteWaitMs;
2520f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2521f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (outputDesc->isDuplicated()) {
2522b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        muteWaitMs = setOutputDevice(outputDesc->mOutput1->mId, device, force, delayMs);
2523b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        muteWaitMs += setOutputDevice(outputDesc->mOutput2->mId, device, force, delayMs);
2524b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        return muteWaitMs;
2525f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2526f32e38e24db196671d3ea43427125a4e212466faEric Laurent    // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current
2527f32e38e24db196671d3ea43427125a4e212466faEric Laurent    // output profile
2528f32e38e24db196671d3ea43427125a4e212466faEric Laurent    if ((device != AUDIO_DEVICE_NONE) &&
2529f32e38e24db196671d3ea43427125a4e212466faEric Laurent            ((device & outputDesc->mProfile->mSupportedDevices) == 0)) {
2530f32e38e24db196671d3ea43427125a4e212466faEric Laurent        return 0;
2531f32e38e24db196671d3ea43427125a4e212466faEric Laurent    }
2532f32e38e24db196671d3ea43427125a4e212466faEric Laurent
2533f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // filter devices according to output selected
2534f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent    device = (audio_devices_t)(device & outputDesc->mProfile->mSupportedDevices);
2535f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
25365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    audio_devices_t prevDevice = outputDesc->mDevice;
25375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
25385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGV("setOutputDevice() prevDevice %04x", prevDevice);
25395ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
2540ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if (device != AUDIO_DEVICE_NONE) {
25415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        outputDesc->mDevice = device;
25425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
25439029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs);
25445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
2545f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // Do not change the routing if:
2546ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    //  - the requested device is AUDIO_DEVICE_NONE
2547f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //  - the requested device is the same as current device and force is not specified.
2548f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // Doing this check here allows the caller to call setOutputDevice() without conditions
2549ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if ((device == AUDIO_DEVICE_NONE || device == prevDevice) && !force) {
25505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        ALOGV("setOutputDevice() setting same device %04x or null device for output %d", device, output);
2551b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        return muteWaitMs;
2552f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2553f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
25545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGV("setOutputDevice() changing device");
2555f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // do the routing
2556f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    param.addInt(String8(AudioParameter::keyRouting), (int)device);
25575ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    mpClientInterface->setParameters(output, param.toString(), delayMs);
25585ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
2559f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // update stream volumes according to new device
2560f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    applyStreamVolumes(output, device, delayMs);
2561b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent
2562b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent    return muteWaitMs;
25635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
2564f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
25655ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric LaurentAudioPolicyManagerBase::IOProfile *AudioPolicyManagerBase::getInputProfile(audio_devices_t device,
25665ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                   uint32_t samplingRate,
25675ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                   uint32_t format,
25685ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                   uint32_t channelMask)
25695ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
25705ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // Choose an input profile based on the requested capture parameters: select the first available
25715ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // profile supporting all requested parameters.
25725ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
257370c236c9290732782d5267935af1475b8d5ae602Eric Laurent    for (size_t i = 0; i < mHwModules.size(); i++)
25745ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    {
257570c236c9290732782d5267935af1475b8d5ae602Eric Laurent        if (mHwModules[i]->mHandle == 0) {
25765ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            continue;
25775ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
257870c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
25795ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        {
258070c236c9290732782d5267935af1475b8d5ae602Eric Laurent            IOProfile *profile = mHwModules[i]->mInputProfiles[j];
258170c236c9290732782d5267935af1475b8d5ae602Eric Laurent            if (profile->isCompatibleProfile(device, samplingRate, format,
25820977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent                                             channelMask,(audio_output_flags_t)0)) {
258370c236c9290732782d5267935af1475b8d5ae602Eric Laurent                return profile;
25845ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
25855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
2586f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
258770c236c9290732782d5267935af1475b8d5ae602Eric Laurent    return NULL;
2588f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2589f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2590f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurentaudio_devices_t AudioPolicyManagerBase::getDeviceForInputSource(int inputSource)
2591f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2592ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    uint32_t device = AUDIO_DEVICE_NONE;
2593f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
25949641bd36dbf0c4b1eeb84499b35c1cdb7fa5a530Eric Laurent    switch (inputSource) {
25959641bd36dbf0c4b1eeb84499b35c1cdb7fa5a530Eric Laurent    case AUDIO_SOURCE_VOICE_UPLINK:
25969641bd36dbf0c4b1eeb84499b35c1cdb7fa5a530Eric Laurent      if (mAvailableInputDevices & AUDIO_DEVICE_IN_VOICE_CALL) {
25979641bd36dbf0c4b1eeb84499b35c1cdb7fa5a530Eric Laurent          device = AUDIO_DEVICE_IN_VOICE_CALL;
25989641bd36dbf0c4b1eeb84499b35c1cdb7fa5a530Eric Laurent          break;
25999641bd36dbf0c4b1eeb84499b35c1cdb7fa5a530Eric Laurent      }
26009641bd36dbf0c4b1eeb84499b35c1cdb7fa5a530Eric Laurent      // FALL THROUGH
26019641bd36dbf0c4b1eeb84499b35c1cdb7fa5a530Eric Laurent
2602f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_DEFAULT:
2603f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_MIC:
2604f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_RECOGNITION:
2605fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent    case AUDIO_SOURCE_HOTWORD:
2606f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_COMMUNICATION:
2607f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (mForceUse[AudioSystem::FOR_RECORD] == AudioSystem::FORCE_BT_SCO &&
2608c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            mAvailableInputDevices & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
2609c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
2610c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_WIRED_HEADSET) {
2611c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = AUDIO_DEVICE_IN_WIRED_HEADSET;
2612c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_BUILTIN_MIC) {
2613c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = AUDIO_DEVICE_IN_BUILTIN_MIC;
2614f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2615f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
2616f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_CAMCORDER:
2617c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        if (mAvailableInputDevices & AUDIO_DEVICE_IN_BACK_MIC) {
2618c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = AUDIO_DEVICE_IN_BACK_MIC;
2619c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_BUILTIN_MIC) {
2620c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = AUDIO_DEVICE_IN_BUILTIN_MIC;
2621f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2622f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
2623f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_DOWNLINK:
2624f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_CALL:
2625c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        if (mAvailableInputDevices & AUDIO_DEVICE_IN_VOICE_CALL) {
2626c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = AUDIO_DEVICE_IN_VOICE_CALL;
26275ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
2628f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
262948387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi    case AUDIO_SOURCE_REMOTE_SUBMIX:
263048387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi        if (mAvailableInputDevices & AUDIO_DEVICE_IN_REMOTE_SUBMIX) {
263148387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
263248387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi        }
263348387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi        break;
2634f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    default:
263564cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("getDeviceForInputSource() invalid input source %d", inputSource);
2636f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
2637f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
26386a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("getDeviceForInputSource()input source %d, device %08x", inputSource, device);
2639c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    return device;
2640f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2641f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
26426d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivibool AudioPolicyManagerBase::isVirtualInputDevice(audio_devices_t device)
26436d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi{
26446d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi    if ((device & AUDIO_DEVICE_BIT_IN) != 0) {
26456d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi        device &= ~AUDIO_DEVICE_BIT_IN;
26466d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi        if ((popcount(device) == 1) && ((device & ~APM_AUDIO_IN_DEVICE_VIRTUAL_ALL) == 0))
26476d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi            return true;
26486d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi    }
26496d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi    return false;
26506d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi}
26516d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi
26526d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Triviaudio_io_handle_t AudioPolicyManagerBase::getActiveInput(bool ignoreVirtualInputs)
2653f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2654f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mInputs.size(); i++) {
26556d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi        const AudioInputDescriptor * input_descriptor = mInputs.valueAt(i);
26566d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi        if ((input_descriptor->mRefCount > 0)
26576d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi                && (!ignoreVirtualInputs || !isVirtualInputDevice(input_descriptor->mDevice))) {
2658f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return mInputs.keyAt(i);
2659f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2660f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2661f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return 0;
2662f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2663f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2664e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2665c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentaudio_devices_t AudioPolicyManagerBase::getDeviceForVolume(audio_devices_t device)
2666e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent{
2667ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if (device == AUDIO_DEVICE_NONE) {
2668e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        // this happens when forcing a route update and no track is active on an output.
2669e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        // In this case the returned category is not important.
2670c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        device =  AUDIO_DEVICE_OUT_SPEAKER;
2671c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    } else if (AudioSystem::popCount(device) > 1) {
2672e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        // Multiple device selection is either:
2673e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        //  - speaker + one other device: give priority to speaker in this case.
2674e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        //  - one A2DP device + another device: happens with duplicated output. In this case
2675e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        // retain the device on the A2DP output as the other must not correspond to an active
2676e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        // selection if not the speaker.
2677c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        if (device & AUDIO_DEVICE_OUT_SPEAKER) {
2678c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            device = AUDIO_DEVICE_OUT_SPEAKER;
2679c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        } else {
2680c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            device = (audio_devices_t)(device & AUDIO_DEVICE_OUT_ALL_A2DP);
2681c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        }
2682e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    }
2683e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
268464cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block    ALOGW_IF(AudioSystem::popCount(device) != 1,
2685c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            "getDeviceForVolume() invalid device combination: %08x",
2686e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            device);
2687e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2688c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    return device;
2689c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent}
2690c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
2691f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric LaurentAudioPolicyManagerBase::device_category AudioPolicyManagerBase::getDeviceCategory(audio_devices_t device)
2692c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent{
2693f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent    switch(getDeviceForVolume(device)) {
2694e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_EARPIECE:
2695e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            return DEVICE_CATEGORY_EARPIECE;
2696e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_WIRED_HEADSET:
2697e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_WIRED_HEADPHONE:
2698e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_BLUETOOTH_SCO:
2699e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET:
2700e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP:
2701e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES:
2702e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            return DEVICE_CATEGORY_HEADSET;
2703e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_SPEAKER:
2704e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT:
2705e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER:
2706c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        case AUDIO_DEVICE_OUT_AUX_DIGITAL:
2707599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent        case AUDIO_DEVICE_OUT_USB_ACCESSORY:
2708599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent        case AUDIO_DEVICE_OUT_USB_DEVICE:
270948387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi        case AUDIO_DEVICE_OUT_REMOTE_SUBMIX:
2710e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        default:
2711e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            return DEVICE_CATEGORY_SPEAKER;
2712e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    }
2713e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent}
2714e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2715f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurentfloat AudioPolicyManagerBase::volIndexToAmpl(audio_devices_t device, const StreamDescriptor& streamDesc,
2716e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        int indexInUi)
2717e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent{
2718e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    device_category deviceCategory = getDeviceCategory(device);
2719e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    const VolumeCurvePoint *curve = streamDesc.mVolumeCurve[deviceCategory];
2720e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2721f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // the volume index in the UI is relative to the min and max volume indices for this stream type
2722e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    int nbSteps = 1 + curve[VOLMAX].mIndex -
2723e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            curve[VOLMIN].mIndex;
2724f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    int volIdx = (nbSteps * (indexInUi - streamDesc.mIndexMin)) /
2725f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            (streamDesc.mIndexMax - streamDesc.mIndexMin);
2726f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2727f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // find what part of the curve this index volume belongs to, or if it's out of bounds
2728f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    int segment = 0;
2729e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    if (volIdx < curve[VOLMIN].mIndex) {         // out of bounds
2730f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return 0.0f;
2731e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    } else if (volIdx < curve[VOLKNEE1].mIndex) {
2732f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        segment = 0;
2733e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    } else if (volIdx < curve[VOLKNEE2].mIndex) {
2734f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        segment = 1;
2735e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    } else if (volIdx <= curve[VOLMAX].mIndex) {
2736f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        segment = 2;
2737f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {                                                               // out of bounds
2738f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return 1.0f;
2739f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2740f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2741f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // linear interpolation in the attenuation table in dB
2742e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    float decibels = curve[segment].mDBAttenuation +
2743e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            ((float)(volIdx - curve[segment].mIndex)) *
2744e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent                ( (curve[segment+1].mDBAttenuation -
2745e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent                        curve[segment].mDBAttenuation) /
2746e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent                    ((float)(curve[segment+1].mIndex -
2747e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent                            curve[segment].mIndex)) );
2748f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2749f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    float amplification = exp( decibels * 0.115129f); // exp( dB * ln(10) / 20 )
2750f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
27513cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGVV("VOLUME vol index=[%d %d %d], dB=[%.1f %.1f %.1f] ampl=%.5f",
2752e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            curve[segment].mIndex, volIdx,
2753e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            curve[segment+1].mIndex,
2754e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            curve[segment].mDBAttenuation,
2755cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent            decibels,
2756e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            curve[segment+1].mDBAttenuation,
2757f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            amplification);
2758f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2759f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return amplification;
2760f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2761f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2762cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
2763e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    AudioPolicyManagerBase::sDefaultVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
2764e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    {1, -49.5f}, {33, -33.5f}, {66, -17.0f}, {100, 0.0f}
2765e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent};
2766e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2767e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
2768e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    AudioPolicyManagerBase::sDefaultMediaVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
2769e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    {1, -58.0f}, {20, -40.0f}, {60, -17.0f}, {100, 0.0f}
2770cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent};
2771cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent
2772e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
2773e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    AudioPolicyManagerBase::sSpeakerMediaVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
2774e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    {1, -56.0f}, {20, -34.0f}, {60, -11.0f}, {100, 0.0f}
2775e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent};
2776e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2777e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
2778e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    AudioPolicyManagerBase::sSpeakerSonificationVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
2779e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    {1, -29.7f}, {33, -20.1f}, {66, -10.2f}, {100, 0.0f}
2780e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent};
2781e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2782ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent// AUDIO_STREAM_SYSTEM, AUDIO_STREAM_ENFORCED_AUDIBLE and AUDIO_STREAM_DTMF volume tracks
2783ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent// AUDIO_STREAM_RING on phones and AUDIO_STREAM_MUSIC on tablets (See AudioService.java).
2784123897874418f9f0e48bb89386d8c470e6975f28Jean-Michel Trivi// The range is constrained between -24dB and -6dB over speaker and -30dB and -18dB over headset.
2785ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
2786ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    AudioPolicyManagerBase::sDefaultSystemVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
2787123897874418f9f0e48bb89386d8c470e6975f28Jean-Michel Trivi    {1, -24.0f}, {33, -18.0f}, {66, -12.0f}, {100, -6.0f}
2788ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent};
2789ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent
2790ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
2791ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    AudioPolicyManagerBase::sHeadsetSystemVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
27927465678e0d5711ebcd78ae47b3a76821534a23eaEric Laurent    {1, -30.0f}, {33, -26.0f}, {66, -22.0f}, {100, -18.0f}
2793ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent};
2794e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2795e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
27960d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent    AudioPolicyManagerBase::sDefaultVoiceVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
27970d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent    {0, -42.0f}, {33, -28.0f}, {66, -14.0f}, {100, 0.0f}
27980d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent};
27990d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent
28000d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
28010d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent    AudioPolicyManagerBase::sSpeakerVoiceVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
28020d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent    {0, -24.0f}, {33, -16.0f}, {66, -8.0f}, {100, 0.0f}
28030d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent};
28040d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent
28050d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
2806ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent            *AudioPolicyManagerBase::sVolumeProfiles[AUDIO_STREAM_CNT]
2807e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent                                                   [AudioPolicyManagerBase::DEVICE_CATEGORY_CNT] = {
2808ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_VOICE_CALL
28090d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent        sDefaultVoiceVolumeCurve, // DEVICE_CATEGORY_HEADSET
28100d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent        sSpeakerVoiceVolumeCurve, // DEVICE_CATEGORY_SPEAKER
28110d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent        sDefaultVoiceVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2812e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    },
2813ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_SYSTEM
2814ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET
2815ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2816ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultSystemVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2817ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    },
2818ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_RING
2819e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
2820e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2821e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sDefaultVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2822e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    },
2823ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_MUSIC
2824ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultMediaVolumeCurve, // DEVICE_CATEGORY_HEADSET
2825ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sSpeakerMediaVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2826ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultMediaVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2827ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    },
2828ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_ALARM
282912bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
283012bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER
283112bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        sDefaultVolumeCurve  // DEVICE_CATEGORY_EARPIECE
283212bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    },
2833ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_NOTIFICATION
2834e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
2835ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2836e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sDefaultVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2837c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    },
2838ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_BLUETOOTH_SCO
28390d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent        sDefaultVoiceVolumeCurve, // DEVICE_CATEGORY_HEADSET
28400d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent        sSpeakerVoiceVolumeCurve, // DEVICE_CATEGORY_SPEAKER
28410d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent        sDefaultVoiceVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2842c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    },
2843ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_ENFORCED_AUDIBLE
2844ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET
2845ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2846ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultSystemVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2847ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    },
2848ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    {  // AUDIO_STREAM_DTMF
2849ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET
2850ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2851ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultSystemVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2852ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    },
2853ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_TTS
2854ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultMediaVolumeCurve, // DEVICE_CATEGORY_HEADSET
2855ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sSpeakerMediaVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2856ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultMediaVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2857ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    },
2858e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent};
2859e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2860e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurentvoid AudioPolicyManagerBase::initializeVolumeCurves()
2861e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent{
2862ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    for (int i = 0; i < AUDIO_STREAM_CNT; i++) {
2863e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) {
2864cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent            mStreams[i].mVolumeCurve[j] =
2865ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent                    sVolumeProfiles[i][j];
2866cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent        }
2867cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent    }
2868f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2869f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2870c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentfloat AudioPolicyManagerBase::computeVolume(int stream,
2871c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                            int index,
2872c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                            audio_io_handle_t output,
2873f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                                            audio_devices_t device)
2874f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2875f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    float volume = 1.0;
2876f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
2877f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    StreamDescriptor &streamDesc = mStreams[stream];
2878f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2879ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if (device == AUDIO_DEVICE_NONE) {
2880f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device = outputDesc->device();
2881f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2882f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2883f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if volume is not 0 (not muted), force media volume to max on digital output
2884f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (stream == AudioSystem::MUSIC &&
2885f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        index != mStreams[stream].mIndexMin &&
2886f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent        (device == AUDIO_DEVICE_OUT_AUX_DIGITAL ||
2887599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent         device == AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET ||
2888599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent         device == AUDIO_DEVICE_OUT_USB_ACCESSORY ||
2889599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent         device == AUDIO_DEVICE_OUT_USB_DEVICE)) {
2890f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return 1.0;
2891f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2892f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2893f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    volume = volIndexToAmpl(device, streamDesc, index);
2894f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2895f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if a headset is connected, apply the following rules to ring tones and notifications
2896f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // to avoid sound level bursts in user's ears:
2897f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // - always attenuate ring tones and notifications volume by 6dB
2898f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // - if music is playing, always limit the volume to current music volume,
2899f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // with a minimum threshold at -36dB so that notification is always perceived.
290012bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    const routing_strategy stream_strategy = getStrategy((AudioSystem::stream_type)stream);
2901c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
2902c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
2903c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            AUDIO_DEVICE_OUT_WIRED_HEADSET |
2904c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) &&
290512bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        ((stream_strategy == STRATEGY_SONIFICATION)
290612bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi                || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL)
2907738207def5f691d605ae33d041116829a74513a9Eric Laurent                || (stream == AudioSystem::SYSTEM)
2908738207def5f691d605ae33d041116829a74513a9Eric Laurent                || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) &&
2909738207def5f691d605ae33d041116829a74513a9Eric Laurent                    (mForceUse[AudioSystem::FOR_SYSTEM] == AudioSystem::FORCE_NONE))) &&
2910f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        streamDesc.mCanBeMuted) {
2911f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        volume *= SONIFICATION_HEADSET_VOLUME_FACTOR;
2912f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // when the phone is ringing we must consider that music could have been paused just before
2913f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // by the music application and behave as if music was active if the last music track was
2914f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // just stopped
2915ac3cf10ef6de12e69540a1244ac7255f93fa7502Eric Laurent        if (isStreamActive(AudioSystem::MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) ||
2916ac3cf10ef6de12e69540a1244ac7255f93fa7502Eric Laurent                mLimitRingtoneVolume) {
291717a73c3394547692457299dc512b5c2312ea0344Eric Laurent            audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/);
2918c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            float musicVol = computeVolume(AudioSystem::MUSIC,
291917a73c3394547692457299dc512b5c2312ea0344Eric Laurent                               mStreams[AudioSystem::MUSIC].getVolumeIndex(musicDevice),
2920c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                               output,
292117a73c3394547692457299dc512b5c2312ea0344Eric Laurent                               musicDevice);
2922c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            float minVol = (musicVol > SONIFICATION_HEADSET_VOLUME_MIN) ?
2923c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                musicVol : SONIFICATION_HEADSET_VOLUME_MIN;
2924f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (volume > minVol) {
2925f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                volume = minVol;
29266a70518b93928d1c91457ff805e375c82d76b0e5Steve Block                ALOGV("computeVolume limiting volume to %f musicVol %f", minVol, musicVol);
2927f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2928f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2929f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2930f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2931f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return volume;
2932f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2933f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2934c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentstatus_t AudioPolicyManagerBase::checkAndSetVolume(int stream,
2935c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                   int index,
2936c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                   audio_io_handle_t output,
2937f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                                                   audio_devices_t device,
2938c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                   int delayMs,
2939c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                   bool force)
2940f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2941f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2942f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // do not change actual stream volume if the stream is muted
2943f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mOutputs.valueFor(output)->mMuteCount[stream] != 0) {
29443cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        ALOGVV("checkAndSetVolume() stream %d muted count %d",
29455ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent              stream, mOutputs.valueFor(output)->mMuteCount[stream]);
2946f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return NO_ERROR;
2947f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2948f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2949f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // do not change in call volume if bluetooth is connected and vice versa
2950f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if ((stream == AudioSystem::VOICE_CALL && mForceUse[AudioSystem::FOR_COMMUNICATION] == AudioSystem::FORCE_BT_SCO) ||
2951f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        (stream == AudioSystem::BLUETOOTH_SCO && mForceUse[AudioSystem::FOR_COMMUNICATION] != AudioSystem::FORCE_BT_SCO)) {
29526a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
2953f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin             stream, mForceUse[AudioSystem::FOR_COMMUNICATION]);
2954f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return INVALID_OPERATION;
2955f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2956f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2957f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    float volume = computeVolume(stream, index, output, device);
2958f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // We actually change the volume if:
2959f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // - the float value returned by computeVolume() changed
2960f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // - the force flag is set
2961f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (volume != mOutputs.valueFor(output)->mCurVolume[stream] ||
2962f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            force) {
2963f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mOutputs.valueFor(output)->mCurVolume[stream] = volume;
29643cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        ALOGVV("checkAndSetVolume() for output %d stream %d, volume %f, delay %d", output, stream, volume, delayMs);
29650d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent        // Force VOICE_CALL to track BLUETOOTH_SCO stream volume when bluetooth audio is
29660d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent        // enabled
29670d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent        if (stream == AudioSystem::BLUETOOTH_SCO) {
29680d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent            mpClientInterface->setStreamVolume(AudioSystem::VOICE_CALL, volume, output, delayMs);
2969f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2970f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mpClientInterface->setStreamVolume((AudioSystem::stream_type)stream, volume, output, delayMs);
2971f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2972f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2973f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (stream == AudioSystem::VOICE_CALL ||
2974f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        stream == AudioSystem::BLUETOOTH_SCO) {
2975f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        float voiceVolume;
2976f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // Force voice volume to max for bluetooth SCO as volume is managed by the headset
2977f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (stream == AudioSystem::VOICE_CALL) {
2978f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            voiceVolume = (float)index/(float)mStreams[stream].mIndexMax;
2979f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        } else {
2980f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            voiceVolume = 1.0;
2981f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2982f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2983b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        if (voiceVolume != mLastVoiceVolume && output == mPrimaryOutput) {
2984f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
2985f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mLastVoiceVolume = voiceVolume;
2986f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2987f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2988f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2989f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
2990f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2991f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2992c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentvoid AudioPolicyManagerBase::applyStreamVolumes(audio_io_handle_t output,
2993f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                                                audio_devices_t device,
2994c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                int delayMs,
2995c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                bool force)
2996f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
29973cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGVV("applyStreamVolumes() for output %d and device %x", output, device);
2998f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2999f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
3000c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        checkAndSetVolume(stream,
3001f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                          mStreams[stream].getVolumeIndex(device),
3002c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                          output,
3003c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                          device,
3004c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                          delayMs,
3005c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                          force);
3006f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3007f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3008f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
300901e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurentvoid AudioPolicyManagerBase::setStrategyMute(routing_strategy strategy,
301001e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                             bool on,
301101e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                             audio_io_handle_t output,
301201e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                             int delayMs,
301301e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                             audio_devices_t device)
3014f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
30153cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGVV("setStrategyMute() strategy %d, mute %d, output %d", strategy, on, output);
3016f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
3017f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (getStrategy((AudioSystem::stream_type)stream) == strategy) {
301801e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent            setStreamMute(stream, on, output, delayMs, device);
3019f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
3020f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3021f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3022f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
302301e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurentvoid AudioPolicyManagerBase::setStreamMute(int stream,
302401e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                           bool on,
302501e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                           audio_io_handle_t output,
302601e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                           int delayMs,
302701e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                           audio_devices_t device)
3028f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3029f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    StreamDescriptor &streamDesc = mStreams[stream];
3030f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
3031ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if (device == AUDIO_DEVICE_NONE) {
303201e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent        device = outputDesc->device();
303301e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent    }
3034f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
30353cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGVV("setStreamMute() stream %d, mute %d, output %d, mMuteCount %d device %04x",
303601e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent          stream, on, output, outputDesc->mMuteCount[stream], device);
3037f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3038f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (on) {
3039f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (outputDesc->mMuteCount[stream] == 0) {
3040738207def5f691d605ae33d041116829a74513a9Eric Laurent            if (streamDesc.mCanBeMuted &&
3041738207def5f691d605ae33d041116829a74513a9Eric Laurent                    ((stream != AudioSystem::ENFORCED_AUDIBLE) ||
3042738207def5f691d605ae33d041116829a74513a9Eric Laurent                     (mForceUse[AudioSystem::FOR_SYSTEM] == AudioSystem::FORCE_NONE))) {
3043c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                checkAndSetVolume(stream, 0, output, device, delayMs);
3044f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
3045f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
3046f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
3047f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mMuteCount[stream]++;
3048f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
3049f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (outputDesc->mMuteCount[stream] == 0) {
30503cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            ALOGV("setStreamMute() unmuting non muted stream!");
3051f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return;
3052f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
3053f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (--outputDesc->mMuteCount[stream] == 0) {
3054c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            checkAndSetVolume(stream,
3055c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                              streamDesc.getVolumeIndex(device),
3056c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                              output,
3057c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                              device,
3058c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                              delayMs);
3059f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
3060f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3061f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3062f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3063f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::handleIncallSonification(int stream, bool starting, bool stateChange)
3064f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3065f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if the stream pertains to sonification strategy and we are in call we must
3066f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // mute the stream if it is low visibility. If it is high visibility, we must play a tone
3067f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // in the device used for phone strategy and play the tone if the selected device does not
3068f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // interfere with the device used for phone strategy
3069f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
3070f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // many times as there are active tracks on the output
307112bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    const routing_strategy stream_strategy = getStrategy((AudioSystem::stream_type)stream);
307212bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    if ((stream_strategy == STRATEGY_SONIFICATION) ||
307312bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi            ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
3074b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        AudioOutputDescriptor *outputDesc = mOutputs.valueFor(mPrimaryOutput);
30756a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
3076f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                stream, starting, outputDesc->mDevice, stateChange);
3077f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (outputDesc->mRefCount[stream]) {
3078f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            int muteCount = 1;
3079f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (stateChange) {
3080f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                muteCount = outputDesc->mRefCount[stream];
3081f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
3082f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (AudioSystem::isLowVisibility((AudioSystem::stream_type)stream)) {
30836a70518b93928d1c91457ff805e375c82d76b0e5Steve Block                ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
3084f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                for (int i = 0; i < muteCount; i++) {
3085b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                    setStreamMute(stream, starting, mPrimaryOutput);
3086f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
3087f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            } else {
30886a70518b93928d1c91457ff805e375c82d76b0e5Steve Block                ALOGV("handleIncallSonification() high visibility");
30895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                if (outputDesc->device() &
30905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                        getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
30916a70518b93928d1c91457ff805e375c82d76b0e5Steve Block                    ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
3092f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    for (int i = 0; i < muteCount; i++) {
3093b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                        setStreamMute(stream, starting, mPrimaryOutput);
3094f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    }
3095f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
3096f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (starting) {
3097f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    mpClientInterface->startTone(ToneGenerator::TONE_SUP_CALL_WAITING, AudioSystem::VOICE_CALL);
3098f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else {
3099f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    mpClientInterface->stopTone();
3100f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
3101f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
3102f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
3103f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3104f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3105f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3106f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinbool AudioPolicyManagerBase::isInCall()
3107f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3108f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return isStateInCall(mPhoneState);
3109f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3110f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3111f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinbool AudioPolicyManagerBase::isStateInCall(int state) {
3112f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return ((state == AudioSystem::MODE_IN_CALL) ||
3113f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            (state == AudioSystem::MODE_IN_COMMUNICATION));
3114f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3115f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3116f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinuint32_t AudioPolicyManagerBase::getMaxEffectsCpuLoad()
3117f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3118f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return MAX_EFFECTS_CPU_LOAD;
3119f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3120f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3121f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinuint32_t AudioPolicyManagerBase::getMaxEffectsMemory()
3122f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3123f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return MAX_EFFECTS_MEMORY;
3124f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3125f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3126f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// --- AudioOutputDescriptor class implementation
3127f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3128b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric LaurentAudioPolicyManagerBase::AudioOutputDescriptor::AudioOutputDescriptor(
31295ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        const IOProfile *profile)
313070c236c9290732782d5267935af1475b8d5ae602Eric Laurent    : mId(0), mSamplingRate(0), mFormat((audio_format_t)0),
313170c236c9290732782d5267935af1475b8d5ae602Eric Laurent      mChannelMask((audio_channel_mask_t)0), mLatency(0),
3132ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    mFlags((audio_output_flags_t)0), mDevice(AUDIO_DEVICE_NONE),
31335a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent    mOutput1(0), mOutput2(0), mProfile(profile), mDirectOpenCount(0)
3134f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3135f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // clear usage count for all stream types
3136f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) {
3137f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mRefCount[i] = 0;
3138f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mCurVolume[i] = -1.0;
3139f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mMuteCount[i] = 0;
3140f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mStopTime[i] = 0;
3141f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
314285ad78f27ca032e90af0f2100659d12c16322c76Marco Nelissen    for (int i = 0; i < NUM_STRATEGIES; i++) {
314385ad78f27ca032e90af0f2100659d12c16322c76Marco Nelissen        mStrategyMutedByDevice[i] = false;
314485ad78f27ca032e90af0f2100659d12c16322c76Marco Nelissen    }
31453cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    if (profile != NULL) {
31463cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        mSamplingRate = profile->mSamplingRates[0];
31473cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        mFormat = profile->mFormats[0];
31483cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        mChannelMask = profile->mChannelMasks[0];
31493cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        mFlags = profile->mFlags;
31503cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    }
3151f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3152f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3153dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Triviaudio_devices_t AudioPolicyManagerBase::AudioOutputDescriptor::device() const
3154f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3155f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (isDuplicated()) {
3156f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent        return (audio_devices_t)(mOutput1->mDevice | mOutput2->mDevice);
3157f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
3158f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent        return mDevice;
3159f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3160f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3161f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
31625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentuint32_t AudioPolicyManagerBase::AudioOutputDescriptor::latency()
31635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
31645ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (isDuplicated()) {
31655ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return (mOutput1->mLatency > mOutput2->mLatency) ? mOutput1->mLatency : mOutput2->mLatency;
31665ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    } else {
31675ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return mLatency;
31685ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
31695ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
31705ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
31715ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentbool AudioPolicyManagerBase::AudioOutputDescriptor::sharesHwModuleWith(
31725ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        const AudioOutputDescriptor *outputDesc)
31735ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
31745ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (isDuplicated()) {
31755ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return mOutput1->sharesHwModuleWith(outputDesc) || mOutput2->sharesHwModuleWith(outputDesc);
31765ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    } else if (outputDesc->isDuplicated()){
31775ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return sharesHwModuleWith(outputDesc->mOutput1) || sharesHwModuleWith(outputDesc->mOutput2);
31785ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    } else {
317970c236c9290732782d5267935af1475b8d5ae602Eric Laurent        return (mProfile->mModule == outputDesc->mProfile->mModule);
31805ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
31815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
31825ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
3183f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::AudioOutputDescriptor::changeRefCount(AudioSystem::stream_type stream, int delta)
3184f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3185f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // forward usage count change to attached outputs
3186f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (isDuplicated()) {
3187f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mOutput1->changeRefCount(stream, delta);
3188f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mOutput2->changeRefCount(stream, delta);
3189f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3190f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if ((delta + (int)mRefCount[stream]) < 0) {
319164cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("changeRefCount() invalid delta %d for stream %d, refCount %d", delta, stream, mRefCount[stream]);
3192f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mRefCount[stream] = 0;
3193f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
3194f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3195f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mRefCount[stream] += delta;
31966a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("changeRefCount() stream %d, count %d", stream, mRefCount[stream]);
3197f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3198f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3199f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurentaudio_devices_t AudioPolicyManagerBase::AudioOutputDescriptor::supportedDevices()
3200b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent{
3201b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (isDuplicated()) {
3202f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent        return (audio_devices_t)(mOutput1->supportedDevices() | mOutput2->supportedDevices());
3203b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    } else {
3204b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        return mProfile->mSupportedDevices ;
3205b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    }
3206b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent}
3207b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
320842fa8215a763822c0d03fc936e4cac1eb864c9ccEric Laurentbool AudioPolicyManagerBase::AudioOutputDescriptor::isActive(uint32_t inPastMs) const
320942fa8215a763822c0d03fc936e4cac1eb864c9ccEric Laurent{
321080f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    return isStrategyActive(NUM_STRATEGIES, inPastMs);
321180f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent}
321280f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent
321380f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurentbool AudioPolicyManagerBase::AudioOutputDescriptor::isStrategyActive(routing_strategy strategy,
321480f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                                                                       uint32_t inPastMs,
321580f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                                                                       nsecs_t sysTime) const
321680f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent{
321780f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    if ((sysTime == 0) && (inPastMs != 0)) {
321880f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent        sysTime = systemTime();
321980f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    }
322042fa8215a763822c0d03fc936e4cac1eb864c9ccEric Laurent    for (int i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) {
322180f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent        if (((getStrategy((AudioSystem::stream_type)i) == strategy) ||
322280f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                (NUM_STRATEGIES == strategy)) &&
322380f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                isStreamActive((AudioSystem::stream_type)i, inPastMs, sysTime)) {
322442fa8215a763822c0d03fc936e4cac1eb864c9ccEric Laurent            return true;
322542fa8215a763822c0d03fc936e4cac1eb864c9ccEric Laurent        }
322642fa8215a763822c0d03fc936e4cac1eb864c9ccEric Laurent    }
322742fa8215a763822c0d03fc936e4cac1eb864c9ccEric Laurent    return false;
322842fa8215a763822c0d03fc936e4cac1eb864c9ccEric Laurent}
322942fa8215a763822c0d03fc936e4cac1eb864c9ccEric Laurent
323080f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurentbool AudioPolicyManagerBase::AudioOutputDescriptor::isStreamActive(AudioSystem::stream_type stream,
323180f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                                                                       uint32_t inPastMs,
323280f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                                                                       nsecs_t sysTime) const
323380f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent{
323480f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    if (mRefCount[stream] != 0) {
323580f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent        return true;
323680f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    }
323780f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    if (inPastMs == 0) {
323880f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent        return false;
323980f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    }
324080f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    if (sysTime == 0) {
324180f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent        sysTime = systemTime();
324280f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    }
324380f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    if (ns2ms(sysTime - mStopTime[stream]) < inPastMs) {
324480f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent        return true;
324580f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    }
324680f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    return false;
324780f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent}
324880f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent
324980f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent
3250f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::AudioOutputDescriptor::dump(int fd)
3251f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3252f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    const size_t SIZE = 256;
3253f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    char buffer[SIZE];
3254f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    String8 result;
3255f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3256f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Sampling rate: %d\n", mSamplingRate);
3257f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3258a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    snprintf(buffer, SIZE, " Format: %08x\n", mFormat);
3259f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
326070c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, " Channels: %08x\n", mChannelMask);
3261f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3262f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Latency: %d\n", mLatency);
3263f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3264f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Flags %08x\n", mFlags);
3265f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3266f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Devices %08x\n", device());
3267f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3268f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Stream volume refCount muteCount\n");
3269f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3270f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) {
3271f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        snprintf(buffer, SIZE, " %02d     %.03f     %02d       %02d\n", i, mCurVolume[i], mRefCount[i], mMuteCount[i]);
3272f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        result.append(buffer);
3273f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3274f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, result.string(), result.size());
3275f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3276f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
3277f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3278f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3279f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// --- AudioInputDescriptor class implementation
3280f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
32815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric LaurentAudioPolicyManagerBase::AudioInputDescriptor::AudioInputDescriptor(const IOProfile *profile)
328270c236c9290732782d5267935af1475b8d5ae602Eric Laurent    : mSamplingRate(0), mFormat((audio_format_t)0), mChannelMask((audio_channel_mask_t)0),
3283ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent      mDevice(AUDIO_DEVICE_NONE), mRefCount(0),
32845ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent      mInputSource(0), mProfile(profile)
3285f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3286f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3287f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3288f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::AudioInputDescriptor::dump(int fd)
3289f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3290f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    const size_t SIZE = 256;
3291f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    char buffer[SIZE];
3292f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    String8 result;
3293f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3294f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Sampling rate: %d\n", mSamplingRate);
3295f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3296f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Format: %d\n", mFormat);
3297f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
329870c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, " Channels: %08x\n", mChannelMask);
3299f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3300f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Devices %08x\n", mDevice);
3301f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3302f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Ref Count %d\n", mRefCount);
3303f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3304f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, result.string(), result.size());
3305f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3306f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
3307f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3308f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3309f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// --- StreamDescriptor class implementation
3310f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3311c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric LaurentAudioPolicyManagerBase::StreamDescriptor::StreamDescriptor()
3312c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    :   mIndexMin(0), mIndexMax(1), mCanBeMuted(true)
3313c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent{
3314c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    mIndexCur.add(AUDIO_DEVICE_OUT_DEFAULT, 0);
3315c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent}
3316c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
3317c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentint AudioPolicyManagerBase::StreamDescriptor::getVolumeIndex(audio_devices_t device)
3318c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent{
3319c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    device = AudioPolicyManagerBase::getDeviceForVolume(device);
3320c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    // there is always a valid entry for AUDIO_DEVICE_OUT_DEFAULT
3321c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    if (mIndexCur.indexOfKey(device) < 0) {
3322c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        device = AUDIO_DEVICE_OUT_DEFAULT;
3323c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    }
3324c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    return mIndexCur.valueFor(device);
3325c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent}
3326c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
3327c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentvoid AudioPolicyManagerBase::StreamDescriptor::dump(int fd)
3328f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3329c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    const size_t SIZE = 256;
3330c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    char buffer[SIZE];
3331c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    String8 result;
3332c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
3333c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    snprintf(buffer, SIZE, "%s         %02d         %02d         ",
3334c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent             mCanBeMuted ? "true " : "false", mIndexMin, mIndexMax);
3335c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    result.append(buffer);
3336c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    for (size_t i = 0; i < mIndexCur.size(); i++) {
3337c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        snprintf(buffer, SIZE, "%04x : %02d, ",
3338c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                 mIndexCur.keyAt(i),
3339c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                 mIndexCur.valueAt(i));
3340c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        result.append(buffer);
3341c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    }
3342c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    result.append("\n");
3343c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
3344c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    write(fd, result.string(), result.size());
3345f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3346f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3347f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// --- EffectDescriptor class implementation
3348f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3349f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::EffectDescriptor::dump(int fd)
3350f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3351f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    const size_t SIZE = 256;
3352f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    char buffer[SIZE];
3353f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    String8 result;
3354f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
33551c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent    snprintf(buffer, SIZE, " I/O: %d\n", mIo);
3356f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3357f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Strategy: %d\n", mStrategy);
3358f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3359f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Session: %d\n", mSession);
3360f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3361f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Name: %s\n",  mDesc.name);
3362f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3363582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    snprintf(buffer, SIZE, " %s\n",  mEnabled ? "Enabled" : "Disabled");
3364582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    result.append(buffer);
3365f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, result.string(), result.size());
3366f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3367f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
3368f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3369f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
33705ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent// --- IOProfile class implementation
33715ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
337270c236c9290732782d5267935af1475b8d5ae602Eric LaurentAudioPolicyManagerBase::HwModule::HwModule(const char *name)
337370c236c9290732782d5267935af1475b8d5ae602Eric Laurent    : mName(strndup(name, AUDIO_HARDWARE_MODULE_ID_MAX_LEN)), mHandle(0)
337470c236c9290732782d5267935af1475b8d5ae602Eric Laurent{
337570c236c9290732782d5267935af1475b8d5ae602Eric Laurent}
337670c236c9290732782d5267935af1475b8d5ae602Eric Laurent
337770c236c9290732782d5267935af1475b8d5ae602Eric LaurentAudioPolicyManagerBase::HwModule::~HwModule()
337870c236c9290732782d5267935af1475b8d5ae602Eric Laurent{
337970c236c9290732782d5267935af1475b8d5ae602Eric Laurent    for (size_t i = 0; i < mOutputProfiles.size(); i++) {
338070c236c9290732782d5267935af1475b8d5ae602Eric Laurent         delete mOutputProfiles[i];
338170c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
338270c236c9290732782d5267935af1475b8d5ae602Eric Laurent    for (size_t i = 0; i < mInputProfiles.size(); i++) {
338370c236c9290732782d5267935af1475b8d5ae602Eric Laurent         delete mInputProfiles[i];
338470c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
338570c236c9290732782d5267935af1475b8d5ae602Eric Laurent    free((void *)mName);
338670c236c9290732782d5267935af1475b8d5ae602Eric Laurent}
338770c236c9290732782d5267935af1475b8d5ae602Eric Laurent
338870c236c9290732782d5267935af1475b8d5ae602Eric Laurentvoid AudioPolicyManagerBase::HwModule::dump(int fd)
338970c236c9290732782d5267935af1475b8d5ae602Eric Laurent{
339070c236c9290732782d5267935af1475b8d5ae602Eric Laurent    const size_t SIZE = 256;
339170c236c9290732782d5267935af1475b8d5ae602Eric Laurent    char buffer[SIZE];
339270c236c9290732782d5267935af1475b8d5ae602Eric Laurent    String8 result;
339370c236c9290732782d5267935af1475b8d5ae602Eric Laurent
339470c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "  - name: %s\n", mName);
339570c236c9290732782d5267935af1475b8d5ae602Eric Laurent    result.append(buffer);
339670c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "  - handle: %d\n", mHandle);
339770c236c9290732782d5267935af1475b8d5ae602Eric Laurent    result.append(buffer);
339870c236c9290732782d5267935af1475b8d5ae602Eric Laurent    write(fd, result.string(), result.size());
339970c236c9290732782d5267935af1475b8d5ae602Eric Laurent    if (mOutputProfiles.size()) {
340070c236c9290732782d5267935af1475b8d5ae602Eric Laurent        write(fd, "  - outputs:\n", sizeof("  - outputs:\n"));
340170c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (size_t i = 0; i < mOutputProfiles.size(); i++) {
3402599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            snprintf(buffer, SIZE, "    output %d:\n", i);
3403599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            write(fd, buffer, strlen(buffer));
340470c236c9290732782d5267935af1475b8d5ae602Eric Laurent            mOutputProfiles[i]->dump(fd);
340570c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
340670c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
340770c236c9290732782d5267935af1475b8d5ae602Eric Laurent    if (mInputProfiles.size()) {
340870c236c9290732782d5267935af1475b8d5ae602Eric Laurent        write(fd, "  - inputs:\n", sizeof("  - inputs:\n"));
340970c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (size_t i = 0; i < mInputProfiles.size(); i++) {
3410599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            snprintf(buffer, SIZE, "    input %d:\n", i);
3411599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            write(fd, buffer, strlen(buffer));
341270c236c9290732782d5267935af1475b8d5ae602Eric Laurent            mInputProfiles[i]->dump(fd);
341370c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
341470c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
341570c236c9290732782d5267935af1475b8d5ae602Eric Laurent}
341670c236c9290732782d5267935af1475b8d5ae602Eric Laurent
341770c236c9290732782d5267935af1475b8d5ae602Eric LaurentAudioPolicyManagerBase::IOProfile::IOProfile(HwModule *module)
34180977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent    : mFlags((audio_output_flags_t)0), mModule(module)
34195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
34205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
34215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
34225ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric LaurentAudioPolicyManagerBase::IOProfile::~IOProfile()
34235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
342470c236c9290732782d5267935af1475b8d5ae602Eric Laurent}
342570c236c9290732782d5267935af1475b8d5ae602Eric Laurent
342670c236c9290732782d5267935af1475b8d5ae602Eric Laurent// checks if the IO profile is compatible with specified parameters. By convention a value of 0
342770c236c9290732782d5267935af1475b8d5ae602Eric Laurent// means a parameter is don't care
342870c236c9290732782d5267935af1475b8d5ae602Eric Laurentbool AudioPolicyManagerBase::IOProfile::isCompatibleProfile(audio_devices_t device,
342970c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                            uint32_t samplingRate,
343070c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                            uint32_t format,
343170c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                            uint32_t channelMask,
34320977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent                                                            audio_output_flags_t flags) const
343370c236c9290732782d5267935af1475b8d5ae602Eric Laurent{
343470c236c9290732782d5267935af1475b8d5ae602Eric Laurent    if ((mSupportedDevices & device) != device) {
343570c236c9290732782d5267935af1475b8d5ae602Eric Laurent        return false;
343670c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
343770c236c9290732782d5267935af1475b8d5ae602Eric Laurent    if ((mFlags & flags) != flags) {
343870c236c9290732782d5267935af1475b8d5ae602Eric Laurent        return false;
343970c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
344070c236c9290732782d5267935af1475b8d5ae602Eric Laurent    if (samplingRate != 0) {
344170c236c9290732782d5267935af1475b8d5ae602Eric Laurent        size_t i;
344270c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (i = 0; i < mSamplingRates.size(); i++)
344370c236c9290732782d5267935af1475b8d5ae602Eric Laurent        {
344470c236c9290732782d5267935af1475b8d5ae602Eric Laurent            if (mSamplingRates[i] == samplingRate) {
344570c236c9290732782d5267935af1475b8d5ae602Eric Laurent                break;
344670c236c9290732782d5267935af1475b8d5ae602Eric Laurent            }
344770c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
344870c236c9290732782d5267935af1475b8d5ae602Eric Laurent        if (i == mSamplingRates.size()) {
344970c236c9290732782d5267935af1475b8d5ae602Eric Laurent            return false;
345070c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
345170c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
345270c236c9290732782d5267935af1475b8d5ae602Eric Laurent    if (format != 0) {
345370c236c9290732782d5267935af1475b8d5ae602Eric Laurent        size_t i;
345470c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (i = 0; i < mFormats.size(); i++)
345570c236c9290732782d5267935af1475b8d5ae602Eric Laurent        {
345670c236c9290732782d5267935af1475b8d5ae602Eric Laurent            if (mFormats[i] == format) {
345770c236c9290732782d5267935af1475b8d5ae602Eric Laurent                break;
345870c236c9290732782d5267935af1475b8d5ae602Eric Laurent            }
345970c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
346070c236c9290732782d5267935af1475b8d5ae602Eric Laurent        if (i == mFormats.size()) {
346170c236c9290732782d5267935af1475b8d5ae602Eric Laurent            return false;
346270c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
346370c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
346470c236c9290732782d5267935af1475b8d5ae602Eric Laurent    if (channelMask != 0) {
346570c236c9290732782d5267935af1475b8d5ae602Eric Laurent        size_t i;
346670c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (i = 0; i < mChannelMasks.size(); i++)
346770c236c9290732782d5267935af1475b8d5ae602Eric Laurent        {
346870c236c9290732782d5267935af1475b8d5ae602Eric Laurent            if (mChannelMasks[i] == channelMask) {
346970c236c9290732782d5267935af1475b8d5ae602Eric Laurent                break;
347070c236c9290732782d5267935af1475b8d5ae602Eric Laurent            }
347170c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
347270c236c9290732782d5267935af1475b8d5ae602Eric Laurent        if (i == mChannelMasks.size()) {
347370c236c9290732782d5267935af1475b8d5ae602Eric Laurent            return false;
347470c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
347570c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
347670c236c9290732782d5267935af1475b8d5ae602Eric Laurent    return true;
34775ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
34785ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
34795ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::IOProfile::dump(int fd)
34805ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
34815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    const size_t SIZE = 256;
34825ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    char buffer[SIZE];
34835ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    String8 result;
34845ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
348570c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "    - sampling rates: ");
34865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    result.append(buffer);
34875ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < mSamplingRates.size(); i++) {
34885ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        snprintf(buffer, SIZE, "%d", mSamplingRates[i]);
34895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        result.append(buffer);
34905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        result.append(i == (mSamplingRates.size() - 1) ? "\n" : ", ");
34915ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
34925ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
349370c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "    - channel masks: ");
34945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    result.append(buffer);
34955ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < mChannelMasks.size(); i++) {
3496a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        snprintf(buffer, SIZE, "0x%04x", mChannelMasks[i]);
34975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        result.append(buffer);
34985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        result.append(i == (mChannelMasks.size() - 1) ? "\n" : ", ");
34995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
35005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
350170c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "    - formats: ");
35025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    result.append(buffer);
35035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < mFormats.size(); i++) {
3504a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        snprintf(buffer, SIZE, "0x%08x", mFormats[i]);
35055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        result.append(buffer);
35065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        result.append(i == (mFormats.size() - 1) ? "\n" : ", ");
35075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
35085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
3509a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    snprintf(buffer, SIZE, "    - devices: 0x%04x\n", mSupportedDevices);
35105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    result.append(buffer);
3511a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    snprintf(buffer, SIZE, "    - flags: 0x%04x\n", mFlags);
35125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    result.append(buffer);
35135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
35145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    write(fd, result.string(), result.size());
35155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
35165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
35175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent// --- audio_policy.conf file parsing
35185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
35195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentstruct StringToEnum {
35205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    const char *name;
35215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    uint32_t value;
35225ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent};
35235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
35245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent#define STRING_TO_ENUM(string) { #string, string }
35255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
35265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
35275ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentconst struct StringToEnum sDeviceNameToEnumTable[] = {
35285ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_EARPIECE),
35295ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_SPEAKER),
35305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_WIRED_HEADSET),
35315ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_WIRED_HEADPHONE),
35325ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_ALL_SCO),
35335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_ALL_A2DP),
35345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_AUX_DIGITAL),
35355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET),
3536599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET),
3537599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_USB_DEVICE),
3538599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_USB_ACCESSORY),
3539599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_ALL_USB),
354048387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi    STRING_TO_ENUM(AUDIO_DEVICE_OUT_REMOTE_SUBMIX),
35415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_BUILTIN_MIC),
35425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET),
35435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_WIRED_HEADSET),
35445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_AUX_DIGITAL),
35455ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_VOICE_CALL),
35465ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_BACK_MIC),
354748387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi    STRING_TO_ENUM(AUDIO_DEVICE_IN_REMOTE_SUBMIX),
3548ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET),
3549ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_DGTL_DOCK_HEADSET),
3550ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_USB_ACCESSORY),
35515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent};
35525ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
35535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentconst struct StringToEnum sFlagNameToEnumTable[] = {
35540977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent    STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT),
35550977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent    STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY),
3556b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent    STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST),
3557b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent    STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER),
3558b4d07b97d23cfaffe22c7859ad7c45e168a7df0eRichard Fitzgerald    STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD),
3559b4d07b97d23cfaffe22c7859ad7c45e168a7df0eRichard Fitzgerald    STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NON_BLOCKING),
35605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent};
35615ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
35625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentconst struct StringToEnum sFormatNameToEnumTable[] = {
35635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
35645ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
35655ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_FORMAT_MP3),
35665ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_FORMAT_AAC),
35675ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
35685ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent};
35695ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
35705ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentconst struct StringToEnum sOutChannelsNameToEnumTable[] = {
35715ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_OUT_MONO),
35725ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
35735ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
35745ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
35755ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent};
35765ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
35775ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentconst struct StringToEnum sInChannelsNameToEnumTable[] = {
35785ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_IN_MONO),
35795ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_IN_STEREO),
358060758e27a4be8fc9ac1180f8a4055234e1702cc9Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_IN_FRONT_BACK),
35815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent};
35825ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
35835ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
35845ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentuint32_t AudioPolicyManagerBase::stringToEnum(const struct StringToEnum *table,
35855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                              size_t size,
35865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                              const char *name)
35875ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
35885ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < size; i++) {
35895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strcmp(table[i].name, name) == 0) {
35905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("stringToEnum() found %s", table[i].name);
35915ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            return table[i].value;
35925ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
35935ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
35945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return 0;
35955ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
35965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
35970977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurentaudio_output_flags_t AudioPolicyManagerBase::parseFlagNames(char *name)
35985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
35995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    uint32_t flag = 0;
36005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
36015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // it is OK to cast name to non const here as we are not going to use it after
36025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // strtok() modifies it
36035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    char *flagName = strtok(name, "|");
36045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (flagName != NULL) {
36055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strlen(flagName) != 0) {
36065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            flag |= stringToEnum(sFlagNameToEnumTable,
36075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                               ARRAY_SIZE(sFlagNameToEnumTable),
36085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                               flagName);
36095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
36105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        flagName = strtok(NULL, "|");
36115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
3612a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    //force direct flag if offload flag is set: offloading implies a direct output stream
3613a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    // and all common behaviors are driven by checking only the direct flag
3614a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    // this should normally be set appropriately in the policy configuration file
3615a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    if ((flag & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
3616a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        flag |= AUDIO_OUTPUT_FLAG_DIRECT;
3617a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    }
3618a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald
36190977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent    return (audio_output_flags_t)flag;
36205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
36215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
36225ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentaudio_devices_t AudioPolicyManagerBase::parseDeviceNames(char *name)
36235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
36245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    uint32_t device = 0;
36255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
36265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    char *devName = strtok(name, "|");
36275ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (devName != NULL) {
36285ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strlen(devName) != 0) {
36295ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device |= stringToEnum(sDeviceNameToEnumTable,
36305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                 ARRAY_SIZE(sDeviceNameToEnumTable),
36315ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                 devName);
36325ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
36335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        devName = strtok(NULL, "|");
36345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
3635c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    return device;
36365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
36375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
36385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadSamplingRates(char *name, IOProfile *profile)
36395ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
36405ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    char *str = strtok(name, "|");
36415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
36423cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    // by convention, "0' in the first entry in mSamplingRates indicates the supported sampling
36433cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    // rates should be read from the output stream after it is opened for the first time
36443cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) {
36453cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        profile->mSamplingRates.add(0);
36463cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        return;
36473cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    }
36483cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
36495ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (str != NULL) {
36505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        uint32_t rate = atoi(str);
36515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (rate != 0) {
36525ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadSamplingRates() adding rate %d", rate);
36535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mSamplingRates.add(rate);
36545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
36555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        str = strtok(NULL, "|");
36565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
36575ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return;
36585ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
36595ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
36605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadFormats(char *name, IOProfile *profile)
36615ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
36625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    char *str = strtok(name, "|");
36635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
36643cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    // by convention, "0' in the first entry in mFormats indicates the supported formats
36653cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    // should be read from the output stream after it is opened for the first time
36663cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) {
36673cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        profile->mFormats.add((audio_format_t)0);
36683cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        return;
36693cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    }
36703cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
36715ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (str != NULL) {
36725ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_format_t format = (audio_format_t)stringToEnum(sFormatNameToEnumTable,
36735ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                             ARRAY_SIZE(sFormatNameToEnumTable),
36745ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                             str);
36755ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (format != 0) {
36765ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mFormats.add(format);
36775ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
36785ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        str = strtok(NULL, "|");
36795ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
36805ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return;
36815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
36825ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
36835ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadInChannels(char *name, IOProfile *profile)
36845ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
36855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    const char *str = strtok(name, "|");
36865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
36875ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGV("loadInChannels() %s", name);
36883cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
36893cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) {
36903cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        profile->mChannelMasks.add((audio_channel_mask_t)0);
36913cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        return;
36923cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    }
36933cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
36945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (str != NULL) {
36955ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_channel_mask_t channelMask =
36965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                (audio_channel_mask_t)stringToEnum(sInChannelsNameToEnumTable,
36975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                   ARRAY_SIZE(sInChannelsNameToEnumTable),
36985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                   str);
36995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (channelMask != 0) {
37005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadInChannels() adding channelMask %04x", channelMask);
37015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mChannelMasks.add(channelMask);
37025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
37035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        str = strtok(NULL, "|");
37045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
37055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return;
37065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
37075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
37085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadOutChannels(char *name, IOProfile *profile)
37095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
37105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    const char *str = strtok(name, "|");
37113cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
37123cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGV("loadOutChannels() %s", name);
37133cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
37143cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    // by convention, "0' in the first entry in mChannelMasks indicates the supported channel
37153cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    // masks should be read from the output stream after it is opened for the first time
37163cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) {
37173cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        profile->mChannelMasks.add((audio_channel_mask_t)0);
37183cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        return;
37193cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    }
37205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
37215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (str != NULL) {
37223cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        audio_channel_mask_t channelMask =
37233cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                (audio_channel_mask_t)stringToEnum(sOutChannelsNameToEnumTable,
37243cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                   ARRAY_SIZE(sOutChannelsNameToEnumTable),
37253cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                   str);
37265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (channelMask != 0) {
37275ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mChannelMasks.add(channelMask);
37285ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
37295ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        str = strtok(NULL, "|");
37305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
37315ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return;
37325ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
37335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
373470c236c9290732782d5267935af1475b8d5ae602Eric Laurentstatus_t AudioPolicyManagerBase::loadInput(cnode *root, HwModule *module)
37355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
37365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    cnode *node = root->first_child;
37375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
37385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    IOProfile *profile = new IOProfile(module);
37395ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
37405ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (node) {
37415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
37425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            loadSamplingRates((char *)node->value, profile);
37435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, FORMATS_TAG) == 0) {
37445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            loadFormats((char *)node->value, profile);
37455ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, CHANNELS_TAG) == 0) {
37465ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            loadInChannels((char *)node->value, profile);
37475ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, DEVICES_TAG) == 0) {
37485ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mSupportedDevices = parseDeviceNames((char *)node->value);
37495ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
37505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        node = node->next;
37515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
3752ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    ALOGW_IF(profile->mSupportedDevices == AUDIO_DEVICE_NONE,
37535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadInput() invalid supported devices");
37545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGW_IF(profile->mChannelMasks.size() == 0,
37555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadInput() invalid supported channel masks");
37565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGW_IF(profile->mSamplingRates.size() == 0,
37575ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadInput() invalid supported sampling rates");
37585ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGW_IF(profile->mFormats.size() == 0,
37595ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadInput() invalid supported formats");
3760ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if ((profile->mSupportedDevices != AUDIO_DEVICE_NONE) &&
37615ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            (profile->mChannelMasks.size() != 0) &&
37625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            (profile->mSamplingRates.size() != 0) &&
37635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            (profile->mFormats.size() != 0)) {
37645ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
37655ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        ALOGV("loadInput() adding input mSupportedDevices %04x", profile->mSupportedDevices);
37665ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
376770c236c9290732782d5267935af1475b8d5ae602Eric Laurent        module->mInputProfiles.add(profile);
37685ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return NO_ERROR;
37695ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    } else {
37705ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        delete profile;
37715ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return BAD_VALUE;
37725ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
37735ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
37745ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
377570c236c9290732782d5267935af1475b8d5ae602Eric Laurentstatus_t AudioPolicyManagerBase::loadOutput(cnode *root, HwModule *module)
37765ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
37775ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    cnode *node = root->first_child;
37785ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
37795ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    IOProfile *profile = new IOProfile(module);
37805ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
37815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (node) {
37825ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
37835ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            loadSamplingRates((char *)node->value, profile);
37845ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, FORMATS_TAG) == 0) {
37855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            loadFormats((char *)node->value, profile);
37865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, CHANNELS_TAG) == 0) {
37875ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            loadOutChannels((char *)node->value, profile);
37885ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, DEVICES_TAG) == 0) {
37895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mSupportedDevices = parseDeviceNames((char *)node->value);
37905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, FLAGS_TAG) == 0) {
37915ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mFlags = parseFlagNames((char *)node->value);
37925ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
37935ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        node = node->next;
37945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
3795ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    ALOGW_IF(profile->mSupportedDevices == AUDIO_DEVICE_NONE,
37965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadOutput() invalid supported devices");
37975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGW_IF(profile->mChannelMasks.size() == 0,
37985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadOutput() invalid supported channel masks");
37995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGW_IF(profile->mSamplingRates.size() == 0,
38005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadOutput() invalid supported sampling rates");
38015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGW_IF(profile->mFormats.size() == 0,
38025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadOutput() invalid supported formats");
3803ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if ((profile->mSupportedDevices != AUDIO_DEVICE_NONE) &&
38045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            (profile->mChannelMasks.size() != 0) &&
38055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            (profile->mSamplingRates.size() != 0) &&
38065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            (profile->mFormats.size() != 0)) {
38075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
38085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        ALOGV("loadOutput() adding output mSupportedDevices %04x, mFlags %04x",
38095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent              profile->mSupportedDevices, profile->mFlags);
38105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
381170c236c9290732782d5267935af1475b8d5ae602Eric Laurent        module->mOutputProfiles.add(profile);
38125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return NO_ERROR;
38135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    } else {
38145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        delete profile;
38155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return BAD_VALUE;
38165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
38175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
38185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
38195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadHwModule(cnode *root)
38205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
38215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    cnode *node = config_find(root, OUTPUTS_TAG);
38225ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    status_t status = NAME_NOT_FOUND;
382370c236c9290732782d5267935af1475b8d5ae602Eric Laurent
382470c236c9290732782d5267935af1475b8d5ae602Eric Laurent    HwModule *module = new HwModule(root->name);
382570c236c9290732782d5267935af1475b8d5ae602Eric Laurent
38265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (node != NULL) {
38275ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strcmp(root->name, AUDIO_HARDWARE_MODULE_ID_A2DP) == 0) {
38285ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            mHasA2dp = true;
3829599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent        } else if (strcmp(root->name, AUDIO_HARDWARE_MODULE_ID_USB) == 0) {
3830599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            mHasUsb = true;
383148387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi        } else if (strcmp(root->name, AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX) == 0) {
383248387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            mHasRemoteSubmix = true;
38335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
3834599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent
38355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        node = node->first_child;
38365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        while (node) {
38375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadHwModule() loading output %s", node->name);
383870c236c9290732782d5267935af1475b8d5ae602Eric Laurent            status_t tmpStatus = loadOutput(node, module);
38395ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            if (status == NAME_NOT_FOUND || status == NO_ERROR) {
38405ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                status = tmpStatus;
38415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
38425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            node = node->next;
38435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
38445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
38455ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    node = config_find(root, INPUTS_TAG);
38465ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (node != NULL) {
38475ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        node = node->first_child;
38485ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        while (node) {
38495ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadHwModule() loading input %s", node->name);
385070c236c9290732782d5267935af1475b8d5ae602Eric Laurent            status_t tmpStatus = loadInput(node, module);
38515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            if (status == NAME_NOT_FOUND || status == NO_ERROR) {
38525ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                status = tmpStatus;
38535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
38545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            node = node->next;
38555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
38565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
38575ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (status == NO_ERROR) {
385870c236c9290732782d5267935af1475b8d5ae602Eric Laurent        mHwModules.add(module);
385970c236c9290732782d5267935af1475b8d5ae602Eric Laurent    } else {
386070c236c9290732782d5267935af1475b8d5ae602Eric Laurent        delete module;
38615ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
38625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
38635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
38645ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadHwModules(cnode *root)
38655ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
38665ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    cnode *node = config_find(root, AUDIO_HW_MODULE_TAG);
38675ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (node == NULL) {
38685ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return;
38695ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
38705ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
38715ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    node = node->first_child;
38725ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (node) {
38735ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        ALOGV("loadHwModules() loading module %s", node->name);
38745ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        loadHwModule(node);
38755ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        node = node->next;
38765ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
38775ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
38785ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
38795ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadGlobalConfig(cnode *root)
38805ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
38815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    cnode *node = config_find(root, GLOBAL_CONFIG_TAG);
38825ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (node == NULL) {
38835ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return;
38845ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
38855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    node = node->first_child;
38865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (node) {
38875ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strcmp(ATTACHED_OUTPUT_DEVICES_TAG, node->name) == 0) {
38885ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            mAttachedOutputDevices = parseDeviceNames((char *)node->value);
3889ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            ALOGW_IF(mAttachedOutputDevices == AUDIO_DEVICE_NONE,
3890c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                    "loadGlobalConfig() no attached output devices");
38915ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadGlobalConfig() mAttachedOutputDevices %04x", mAttachedOutputDevices);
38925ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(DEFAULT_OUTPUT_DEVICE_TAG, node->name) == 0) {
38935ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            mDefaultOutputDevice = (audio_devices_t)stringToEnum(sDeviceNameToEnumTable,
38945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                              ARRAY_SIZE(sDeviceNameToEnumTable),
38955ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                              (char *)node->value);
3896ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            ALOGW_IF(mDefaultOutputDevice == AUDIO_DEVICE_NONE,
3897c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                    "loadGlobalConfig() default device not specified");
38985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadGlobalConfig() mDefaultOutputDevice %04x", mDefaultOutputDevice);
38995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(ATTACHED_INPUT_DEVICES_TAG, node->name) == 0) {
3900ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            mAvailableInputDevices = parseDeviceNames((char *)node->value) & ~AUDIO_DEVICE_BIT_IN;
39015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadGlobalConfig() mAvailableInputDevices %04x", mAvailableInputDevices);
39025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
39035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        node = node->next;
39045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
39055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
39065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
39075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentstatus_t AudioPolicyManagerBase::loadAudioPolicyConfig(const char *path)
39085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
39095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    cnode *root;
39105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    char *data;
39115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
39125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    data = (char *)load_file(path, NULL);
39135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (data == NULL) {
39145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return -ENODEV;
39155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
39165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    root = config_node("", "");
39175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    config_load(root, data);
39185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
39195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    loadGlobalConfig(root);
39205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    loadHwModules(root);
39215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
39225ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    config_free(root);
39235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    free(root);
39245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    free(data);
39255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
39265ec145df7708564d385fd3fb764085321cf4c253Dima Zavin    ALOGI("loadAudioPolicyConfig() loaded %s\n", path);
39275ec145df7708564d385fd3fb764085321cf4c253Dima Zavin
39285ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return NO_ERROR;
39295ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
3930f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3931739022f26a7127ba76a98dda65411496086114a7Dima Zavinvoid AudioPolicyManagerBase::defaultAudioPolicyConfig(void)
3932739022f26a7127ba76a98dda65411496086114a7Dima Zavin{
3933739022f26a7127ba76a98dda65411496086114a7Dima Zavin    HwModule *module;
3934739022f26a7127ba76a98dda65411496086114a7Dima Zavin    IOProfile *profile;
3935739022f26a7127ba76a98dda65411496086114a7Dima Zavin
3936739022f26a7127ba76a98dda65411496086114a7Dima Zavin    mDefaultOutputDevice = AUDIO_DEVICE_OUT_SPEAKER;
3937739022f26a7127ba76a98dda65411496086114a7Dima Zavin    mAttachedOutputDevices = AUDIO_DEVICE_OUT_SPEAKER;
3938ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    mAvailableInputDevices = AUDIO_DEVICE_IN_BUILTIN_MIC & ~AUDIO_DEVICE_BIT_IN;
3939739022f26a7127ba76a98dda65411496086114a7Dima Zavin
3940739022f26a7127ba76a98dda65411496086114a7Dima Zavin    module = new HwModule("primary");
3941739022f26a7127ba76a98dda65411496086114a7Dima Zavin
3942739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile = new IOProfile(module);
3943739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mSamplingRates.add(44100);
3944739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
3945739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mChannelMasks.add(AUDIO_CHANNEL_OUT_STEREO);
3946739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mSupportedDevices = AUDIO_DEVICE_OUT_SPEAKER;
3947739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mFlags = AUDIO_OUTPUT_FLAG_PRIMARY;
3948739022f26a7127ba76a98dda65411496086114a7Dima Zavin    module->mOutputProfiles.add(profile);
3949739022f26a7127ba76a98dda65411496086114a7Dima Zavin
3950739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile = new IOProfile(module);
3951739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mSamplingRates.add(8000);
3952739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
3953739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mChannelMasks.add(AUDIO_CHANNEL_IN_MONO);
3954739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mSupportedDevices = AUDIO_DEVICE_IN_BUILTIN_MIC;
3955739022f26a7127ba76a98dda65411496086114a7Dima Zavin    module->mInputProfiles.add(profile);
3956739022f26a7127ba76a98dda65411496086114a7Dima Zavin
3957739022f26a7127ba76a98dda65411496086114a7Dima Zavin    mHwModules.add(module);
3958739022f26a7127ba76a98dda65411496086114a7Dima Zavin}
3959f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3960f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}; // namespace android
3961