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;
31587e28f7b352fb77f46e16ebcdd85cbf01396a203Eric Laurent        for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) {
31687e28f7b352fb77f46e16ebcdd85cbf01396a203Eric Laurent            mStreams[AUDIO_STREAM_DTMF].mVolumeCurve[j] =
31787e28f7b352fb77f46e16ebcdd85cbf01396a203Eric Laurent                    sVolumeProfiles[AUDIO_STREAM_VOICE_CALL][j];
31887e28f7b352fb77f46e16ebcdd85cbf01396a203Eric Laurent        }
319f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else if (isStateInCall(oldState) && !isStateInCall(state)) {
3206a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("  Exiting call in setPhoneState()");
321f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // force routing command to audio hardware when exiting a call
322f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // even if no device change is needed
323f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        force = true;
32487e28f7b352fb77f46e16ebcdd85cbf01396a203Eric Laurent        for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) {
32587e28f7b352fb77f46e16ebcdd85cbf01396a203Eric Laurent            mStreams[AUDIO_STREAM_DTMF].mVolumeCurve[j] =
32687e28f7b352fb77f46e16ebcdd85cbf01396a203Eric Laurent                    sVolumeProfiles[AUDIO_STREAM_DTMF][j];
32787e28f7b352fb77f46e16ebcdd85cbf01396a203Eric Laurent        }
328f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else if (isStateInCall(state) && (state != oldState)) {
3296a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("  Switching between telephony and VoIP in setPhoneState()");
330f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // force routing command to audio hardware when switching between telephony and VoIP
331f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // even if no device change is needed
332f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        force = true;
333f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
334f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
335f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // check for device and output changes triggered by new phone state
3365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    newDevice = getNewDevice(mPrimaryOutput, false /*fromCache*/);
337f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkA2dpSuspend();
338f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForAllStrategies();
339c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    updateDevicesAndOutputs();
340f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
341b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    AudioOutputDescriptor *hwOutputDesc = mOutputs.valueFor(mPrimaryOutput);
342f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
343f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // force routing command to audio hardware when ending call
344f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // even if no device change is needed
345ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if (isStateInCall(oldState) && newDevice == AUDIO_DEVICE_NONE) {
346f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        newDevice = hwOutputDesc->device();
347f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
348f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
349f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    int delayMs = 0;
350772686310a3bc44d93591349e174a60360a3d30dSathishKumar Mani    if (isStateInCall(state)) {
35180f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent        nsecs_t sysTime = systemTime();
352772686310a3bc44d93591349e174a60360a3d30dSathishKumar Mani        for (size_t i = 0; i < mOutputs.size(); i++) {
353772686310a3bc44d93591349e174a60360a3d30dSathishKumar Mani            AudioOutputDescriptor *desc = mOutputs.valueAt(i);
35480f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent            // mute media and sonification strategies and delay device switch by the largest
35580f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent            // latency of any output where either strategy is active.
35680f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent            // This avoid sending the ring tone or music tail into the earpiece or headset.
35780f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent            if ((desc->isStrategyActive(STRATEGY_MEDIA,
35880f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                                     SONIFICATION_HEADSET_MUSIC_DELAY,
35980f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                                     sysTime) ||
36080f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                    desc->isStrategyActive(STRATEGY_SONIFICATION,
36180f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                                         SONIFICATION_HEADSET_MUSIC_DELAY,
36280f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                                         sysTime)) &&
36380f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                    (delayMs < (int)desc->mLatency*2)) {
36480f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                delayMs = desc->mLatency*2;
365772686310a3bc44d93591349e174a60360a3d30dSathishKumar Mani            }
36680f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent            setStrategyMute(STRATEGY_MEDIA, true, mOutputs.keyAt(i));
36780f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent            setStrategyMute(STRATEGY_MEDIA, false, mOutputs.keyAt(i), MUTE_TIME_MS,
36880f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
36980f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent            setStrategyMute(STRATEGY_SONIFICATION, true, mOutputs.keyAt(i));
37080f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent            setStrategyMute(STRATEGY_SONIFICATION, false, mOutputs.keyAt(i), MUTE_TIME_MS,
37180f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
372772686310a3bc44d93591349e174a60360a3d30dSathishKumar Mani        }
373772686310a3bc44d93591349e174a60360a3d30dSathishKumar Mani    }
374772686310a3bc44d93591349e174a60360a3d30dSathishKumar Mani
375f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // change routing is necessary
376b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    setOutputDevice(mPrimaryOutput, newDevice, force, delayMs);
377f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
378f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if entering in call state, handle special case of active streams
379f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // pertaining to sonification strategy see handleIncallSonification()
380f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (isStateInCall(state)) {
3816a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("setPhoneState() in call state management: new state is %d", state);
382f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
383f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            handleIncallSonification(stream, true, true);
384f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
385f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
386f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
387f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
388f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (state == AudioSystem::MODE_RINGTONE &&
389f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        isStreamActive(AudioSystem::MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
390f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mLimitRingtoneVolume = true;
391f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
392f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mLimitRingtoneVolume = false;
393f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
394f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
395f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
396f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::setForceUse(AudioSystem::force_use usage, AudioSystem::forced_config config)
397f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3986a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mPhoneState);
399f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
400f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    bool forceVolumeReeval = false;
401f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    switch(usage) {
402f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::FOR_COMMUNICATION:
403f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (config != AudioSystem::FORCE_SPEAKER && config != AudioSystem::FORCE_BT_SCO &&
404f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_NONE) {
40564cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("setForceUse() invalid config %d for FOR_COMMUNICATION", config);
406f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return;
407f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
408f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        forceVolumeReeval = true;
409f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mForceUse[usage] = config;
410f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
411f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::FOR_MEDIA:
412f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (config != AudioSystem::FORCE_HEADPHONES && config != AudioSystem::FORCE_BT_A2DP &&
413f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_WIRED_ACCESSORY &&
414f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_ANALOG_DOCK &&
4151afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent            config != AudioSystem::FORCE_DIGITAL_DOCK && config != AudioSystem::FORCE_NONE &&
41631363a9cb94e80330c335fede0b92b1953a09517Jean-Michel Trivi            config != AudioSystem::FORCE_NO_BT_A2DP) {
41764cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("setForceUse() invalid config %d for FOR_MEDIA", config);
418f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return;
419f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
420f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mForceUse[usage] = config;
421f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
422f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::FOR_RECORD:
423f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (config != AudioSystem::FORCE_BT_SCO && config != AudioSystem::FORCE_WIRED_ACCESSORY &&
424f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_NONE) {
42564cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("setForceUse() invalid config %d for FOR_RECORD", config);
426f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return;
427f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
428f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mForceUse[usage] = config;
429f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
430f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::FOR_DOCK:
431f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (config != AudioSystem::FORCE_NONE && config != AudioSystem::FORCE_BT_CAR_DOCK &&
432f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_BT_DESK_DOCK &&
433f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_WIRED_ACCESSORY &&
434f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_ANALOG_DOCK &&
435f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_DIGITAL_DOCK) {
43664cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("setForceUse() invalid config %d for FOR_DOCK", config);
437f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
438f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        forceVolumeReeval = true;
439f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mForceUse[usage] = config;
440f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
441738207def5f691d605ae33d041116829a74513a9Eric Laurent    case AudioSystem::FOR_SYSTEM:
442738207def5f691d605ae33d041116829a74513a9Eric Laurent        if (config != AudioSystem::FORCE_NONE &&
443738207def5f691d605ae33d041116829a74513a9Eric Laurent            config != AudioSystem::FORCE_SYSTEM_ENFORCED) {
444738207def5f691d605ae33d041116829a74513a9Eric Laurent            ALOGW("setForceUse() invalid config %d for FOR_SYSTEM", config);
445738207def5f691d605ae33d041116829a74513a9Eric Laurent        }
446738207def5f691d605ae33d041116829a74513a9Eric Laurent        forceVolumeReeval = true;
447738207def5f691d605ae33d041116829a74513a9Eric Laurent        mForceUse[usage] = config;
448738207def5f691d605ae33d041116829a74513a9Eric Laurent        break;
449f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    default:
45064cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("setForceUse() invalid usage %d", usage);
451f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
452f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
453f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
4545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // check for device and output changes triggered by new force usage
455f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkA2dpSuspend();
456f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForAllStrategies();
457c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    updateDevicesAndOutputs();
4585ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < mOutputs.size(); i++) {
4595ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_io_handle_t output = mOutputs.keyAt(i);
4605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_devices_t newDevice = getNewDevice(output, true /*fromCache*/);
461ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        setOutputDevice(output, newDevice, (newDevice != AUDIO_DEVICE_NONE));
462ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
4635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            applyStreamVolumes(output, newDevice, 0, true);
4645ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
465f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
466f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
467f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    audio_io_handle_t activeInput = getActiveInput();
468f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (activeInput != 0) {
469f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioInputDescriptor *inputDesc = mInputs.valueFor(activeInput);
4705ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_devices_t newDevice = getDeviceForInputSource(inputDesc->mInputSource);
471ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if ((newDevice != AUDIO_DEVICE_NONE) && (newDevice != inputDesc->mDevice)) {
4726a70518b93928d1c91457ff805e375c82d76b0e5Steve Block            ALOGV("setForceUse() changing device from %x to %x for input %d",
473f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    inputDesc->mDevice, newDevice, activeInput);
474f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            inputDesc->mDevice = newDevice;
475f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            AudioParameter param = AudioParameter();
476f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            param.addInt(String8(AudioParameter::keyRouting), (int)newDevice);
477f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mpClientInterface->setParameters(activeInput, param.toString());
478f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
479f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
480f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
481f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
482f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
483f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima ZavinAudioSystem::forced_config AudioPolicyManagerBase::getForceUse(AudioSystem::force_use usage)
484f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
485f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return mForceUse[usage];
486f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
487f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
488f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::setSystemProperty(const char* property, const char* value)
489f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
4906a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("setSystemProperty() property %s, value %s", property, value);
491f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
492f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
493b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent// Find a direct output profile compatible with the parameters passed, even if the input flags do
494b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent// not explicitly request a direct output
4953cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric LaurentAudioPolicyManagerBase::IOProfile *AudioPolicyManagerBase::getProfileForDirectOutput(
4963cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                               audio_devices_t device,
49770c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                               uint32_t samplingRate,
49870c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                               uint32_t format,
49970c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                               uint32_t channelMask,
5000977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent                                                               audio_output_flags_t flags)
50170c236c9290732782d5267935af1475b8d5ae602Eric Laurent{
50270c236c9290732782d5267935af1475b8d5ae602Eric Laurent    for (size_t i = 0; i < mHwModules.size(); i++) {
50370c236c9290732782d5267935af1475b8d5ae602Eric Laurent        if (mHwModules[i]->mHandle == 0) {
50470c236c9290732782d5267935af1475b8d5ae602Eric Laurent            continue;
50570c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
50670c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) {
507a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald            IOProfile *profile = mHwModules[i]->mOutputProfiles[j];
508a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald            if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
509a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                if (profile->isCompatibleProfile(device, samplingRate, format,
510a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                                           channelMask,
511a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                                           AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
512a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                    if (mAvailableOutputDevices & profile->mSupportedDevices) {
513a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                        return mHwModules[i]->mOutputProfiles[j];
514a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                    }
515a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                }
516b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent            } else {
517a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                if (profile->isCompatibleProfile(device, samplingRate, format,
5183cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                           channelMask,
5193cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                           AUDIO_OUTPUT_FLAG_DIRECT)) {
520a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                    if (mAvailableOutputDevices & profile->mSupportedDevices) {
521a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                        return mHwModules[i]->mOutputProfiles[j];
522a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                    }
523a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                }
524a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald            }
52570c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
52670c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
52770c236c9290732782d5267935af1475b8d5ae602Eric Laurent    return 0;
52870c236c9290732782d5267935af1475b8d5ae602Eric Laurent}
52970c236c9290732782d5267935af1475b8d5ae602Eric Laurent
530f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinaudio_io_handle_t AudioPolicyManagerBase::getOutput(AudioSystem::stream_type stream,
531f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    uint32_t samplingRate,
532f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    uint32_t format,
53370c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                    uint32_t channelMask,
534b4d07b97d23cfaffe22c7859ad7c45e168a7df0eRichard Fitzgerald                                    AudioSystem::output_flags flags,
535b4d07b97d23cfaffe22c7859ad7c45e168a7df0eRichard Fitzgerald                                    const audio_offload_info_t *offloadInfo)
536f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
537f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    audio_io_handle_t output = 0;
538f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    uint32_t latency = 0;
539f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    routing_strategy strategy = getStrategy((AudioSystem::stream_type)stream);
5405ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
541a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    ALOGV("getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x",
542a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald          device, stream, samplingRate, format, channelMask, flags);
543f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
544f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
545f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mCurOutput != 0) {
54670c236c9290732782d5267935af1475b8d5ae602Eric Laurent        ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
547f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
548f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
549f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (mTestOutputs[mCurOutput] == 0) {
5506a70518b93928d1c91457ff805e375c82d76b0e5Steve Block            ALOGV("getOutput() opening test output");
5513cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(NULL);
552f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            outputDesc->mDevice = mTestDevice;
553f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            outputDesc->mSamplingRate = mTestSamplingRate;
554f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            outputDesc->mFormat = mTestFormat;
55570c236c9290732782d5267935af1475b8d5ae602Eric Laurent            outputDesc->mChannelMask = mTestChannels;
556f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            outputDesc->mLatency = mTestLatencyMs;
5570977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent            outputDesc->mFlags = (audio_output_flags_t)(mDirectOutput ? AudioSystem::OUTPUT_FLAG_DIRECT : 0);
558f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            outputDesc->mRefCount[stream] = 0;
55970c236c9290732782d5267935af1475b8d5ae602Eric Laurent            mTestOutputs[mCurOutput] = mpClientInterface->openOutput(0, &outputDesc->mDevice,
560f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            &outputDesc->mSamplingRate,
561f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            &outputDesc->mFormat,
56270c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                            &outputDesc->mChannelMask,
563f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            &outputDesc->mLatency,
564b4d07b97d23cfaffe22c7859ad7c45e168a7df0eRichard Fitzgerald                                            outputDesc->mFlags,
565b4d07b97d23cfaffe22c7859ad7c45e168a7df0eRichard Fitzgerald                                            offloadInfo);
566f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (mTestOutputs[mCurOutput]) {
567f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                AudioParameter outputCmd = AudioParameter();
568f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                outputCmd.addInt(String8("set_id"),mCurOutput);
569f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
570f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                addOutput(mTestOutputs[mCurOutput], outputDesc);
571f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
572f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
573f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return mTestOutputs[mCurOutput];
574f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
575f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
576f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
577f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // open a direct output if required by specified parameters
578a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    //force direct flag if offload flag is set: offloading implies a direct output stream
579a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    // and all common behaviors are driven by checking only the direct flag
580a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    // this should normally be set appropriately in the policy configuration file
581a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
582a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        flags = (AudioSystem::output_flags)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
583a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    }
584a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald
585000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    // Do not allow offloading if one non offloadable effect is enabled. This prevents from
586000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    // creating an offloaded track and tearing it down immediately after start when audioflinger
587000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    // detects there is an active non offloadable effect.
588000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    // FIXME: We should check the audio session here but we do not have it in this context.
589000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    // This may prevent offloading in rare situations where effects are left active by apps
590000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    // in the background.
591000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    IOProfile *profile = NULL;
592000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
593000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent            !isNonOffloadableEffectEnabled()) {
594000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent        profile = getProfileForDirectOutput(device,
595000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent                                           samplingRate,
596000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent                                           format,
597000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent                                           channelMask,
598000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent                                           (audio_output_flags_t)flags);
599000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    }
600000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent
6013cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    if (profile != NULL) {
6025a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        AudioOutputDescriptor *outputDesc = NULL;
603f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
6045a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        for (size_t i = 0; i < mOutputs.size(); i++) {
6055a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent            AudioOutputDescriptor *desc = mOutputs.valueAt(i);
6065a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent            if (!desc->isDuplicated() && (profile == desc->mProfile)) {
6075a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent                outputDesc = desc;
6085a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent                // reuse direct output if currently open and configured with same parameters
6095a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent                if ((samplingRate == outputDesc->mSamplingRate) &&
6105a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent                        (format == outputDesc->mFormat) &&
6115a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent                        (channelMask == outputDesc->mChannelMask)) {
6125a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent                    outputDesc->mDirectOpenCount++;
613a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                    ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
6145a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent                    return mOutputs.keyAt(i);
6155a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent                }
6165a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent            }
6175a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        }
6185a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        // close direct output if currently open and configured with different parameters
6195a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        if (outputDesc != NULL) {
6205a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent            closeOutput(outputDesc->mId);
6215a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        }
6225a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        outputDesc = new AudioOutputDescriptor(profile);
623f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mDevice = device;
624f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mSamplingRate = samplingRate;
62570c236c9290732782d5267935af1475b8d5ae602Eric Laurent        outputDesc->mFormat = (audio_format_t)format;
62670c236c9290732782d5267935af1475b8d5ae602Eric Laurent        outputDesc->mChannelMask = (audio_channel_mask_t)channelMask;
627f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mLatency = 0;
628a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        outputDesc->mFlags =(audio_output_flags_t) (outputDesc->mFlags | flags);
629f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mRefCount[stream] = 0;
630f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mStopTime[stream] = 0;
6315a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        outputDesc->mDirectOpenCount = 1;
6323cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        output = mpClientInterface->openOutput(profile->mModule->mHandle,
63370c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                        &outputDesc->mDevice,
634f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                        &outputDesc->mSamplingRate,
635f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                        &outputDesc->mFormat,
63670c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                        &outputDesc->mChannelMask,
637f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                        &outputDesc->mLatency,
638a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                                        outputDesc->mFlags,
639a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                                        offloadInfo);
640f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
6419f1f9b509c930830f6f32e9ef6c2c8a03d6fa96eJean-Michel Trivi        // only accept an output with the requested parameters
642f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (output == 0 ||
643f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            (samplingRate != 0 && samplingRate != outputDesc->mSamplingRate) ||
644f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            (format != 0 && format != outputDesc->mFormat) ||
64570c236c9290732782d5267935af1475b8d5ae602Eric Laurent            (channelMask != 0 && channelMask != outputDesc->mChannelMask)) {
6463cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
6473cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    "format %d %d, channelMask %04x %04x", output, samplingRate,
6483cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
6493cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    outputDesc->mChannelMask);
650f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (output != 0) {
651f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mpClientInterface->closeOutput(output);
652f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
653f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            delete outputDesc;
654f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return 0;
655f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
65645c763947b657b347211dc9388754e05d30d0467Eric Laurent        audio_io_handle_t srcOutput = getOutputForEffect();
657f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        addOutput(output, outputDesc);
65845c763947b657b347211dc9388754e05d30d0467Eric Laurent        audio_io_handle_t dstOutput = getOutputForEffect();
65945c763947b657b347211dc9388754e05d30d0467Eric Laurent        if (dstOutput == output) {
66045c763947b657b347211dc9388754e05d30d0467Eric Laurent            mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
66145c763947b657b347211dc9388754e05d30d0467Eric Laurent        }
6625a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        mPreviousOutputs = mOutputs;
6635a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        ALOGV("getOutput() returns new direct output %d", output);
664f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return output;
665f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
666f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
6679f1f9b509c930830f6f32e9ef6c2c8a03d6fa96eJean-Michel Trivi    // ignoring channel mask due to downmix capability in mixer
6689f1f9b509c930830f6f32e9ef6c2c8a03d6fa96eJean-Michel Trivi
669f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // open a non direct output
670f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
671a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    // for non direct outputs, only PCM is supported
672a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    if (audio_is_linear_pcm((audio_format_t)format)) {
673a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        // get which output is suitable for the specified stream. The actual
674a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        // routing change will happen when startOutput() will be called
675a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
676f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
677a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        output = selectOutput(outputs, flags);
678a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    }
679a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
68070c236c9290732782d5267935af1475b8d5ae602Eric Laurent            "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
681f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
6825ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGV("getOutput() returns output %d", output);
6835ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
684f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return output;
685f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
686f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
6875ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentaudio_io_handle_t AudioPolicyManagerBase::selectOutput(const SortedVector<audio_io_handle_t>& outputs,
6885ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                       AudioSystem::output_flags flags)
6895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
6905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // select one output among several that provide a path to a particular device or set of
6915ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // devices (the list was previously build by getOutputsForDevice()).
6925ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // The priority is as follows:
6935ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // 1: the output with the highest number of requested policy flags
6945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // 2: the primary output
6955ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // 3: the first output in the list
6965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
6975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (outputs.size() == 0) {
6985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return 0;
6995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
7005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (outputs.size() == 1) {
7015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return outputs[0];
7025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
7035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
7045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    int maxCommonFlags = 0;
7055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    audio_io_handle_t outputFlags = 0;
7065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    audio_io_handle_t outputPrimary = 0;
7075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
7085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < outputs.size(); i++) {
7095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        AudioOutputDescriptor *outputDesc = mOutputs.valueFor(outputs[i]);
7105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (!outputDesc->isDuplicated()) {
7115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            int commonFlags = (int)AudioSystem::popCount(outputDesc->mProfile->mFlags & flags);
7125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            if (commonFlags > maxCommonFlags) {
7135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                outputFlags = outputs[i];
7145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                maxCommonFlags = commonFlags;
7155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags);
7165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
7170977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent            if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
7185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                outputPrimary = outputs[i];
7195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
7205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
7215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
7225ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
7235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (outputFlags != 0) {
7245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return outputFlags;
7255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
7265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (outputPrimary != 0) {
7275ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return outputPrimary;
7285ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
7295ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
7305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return outputs[0];
7315ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
7325ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
733f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::startOutput(audio_io_handle_t output,
734f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                             AudioSystem::stream_type stream,
735f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                             int session)
736f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
7376a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("startOutput() output %d, stream %d, session %d", output, stream, session);
738f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mOutputs.indexOfKey(output);
739f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
74064cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("startOutput() unknow output %d", output);
741f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
742f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
743f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
744f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index);
745f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
7465ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // increment usage count for this stream on the requested output:
747f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // NOTE that the usage count is the same for duplicated output and hardware output which is
7485ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
749f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    outputDesc->changeRefCount(stream, 1);
750f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
7515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (outputDesc->mRefCount[stream] == 1) {
7525ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_devices_t newDevice = getNewDevice(output, false /*fromCache*/);
753b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        routing_strategy strategy = getStrategy(stream);
754b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
755b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                            (strategy == STRATEGY_SONIFICATION_RESPECTFUL);
756b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        uint32_t waitMs = 0;
7575ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        bool force = false;
7585ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        for (size_t i = 0; i < mOutputs.size(); i++) {
7595ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            AudioOutputDescriptor *desc = mOutputs.valueAt(i);
760b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent            if (desc != outputDesc) {
761b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                // force a device change if any other output is managed by the same hw
762b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                // module and has a current device selection that differs from selected device.
763b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                // In this case, the audio HAL must receive the new device selection so that it can
764b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                // change the device currently selected by the other active output.
765b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                if (outputDesc->sharesHwModuleWith(desc) &&
7665ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                    desc->device() != newDevice) {
767b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                    force = true;
768b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                }
769b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                // wait for audio on other active outputs to be presented when starting
770b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                // a notification so that audio focus effect can propagate.
77142fa8215a763822c0d03fc936e4cac1eb864c9ccEric Laurent                uint32_t latency = desc->latency();
77242fa8215a763822c0d03fc936e4cac1eb864c9ccEric Laurent                if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
77342fa8215a763822c0d03fc936e4cac1eb864c9ccEric Laurent                    waitMs = latency;
774b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                }
7755ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
7765ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
777b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        uint32_t muteWaitMs = setOutputDevice(output, newDevice, force);
778f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
7795ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // handle special case for sonification while in call
7805ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (isInCall()) {
7815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            handleIncallSonification(stream, true, false);
7825ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
783c16ac09f510437e8340be691720177a490ae78f0Eric Laurent
7845ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // apply volume rules for current stream and device if necessary
7855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        checkAndSetVolume(stream,
786c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                          mStreams[stream].getVolumeIndex(newDevice),
7875ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                          output,
7885ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                          newDevice);
78912bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi
7905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // update the outputs if starting an output with a stream that can affect notification
7915ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // routing
7925ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        handleNotificationRoutingForStream(stream);
793b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        if (waitMs > muteWaitMs) {
794b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent            usleep((waitMs - muteWaitMs) * 2 * 1000);
795b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        }
7965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
797f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
798f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
799f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
8005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
801f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::stopOutput(audio_io_handle_t output,
802f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            AudioSystem::stream_type stream,
803f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            int session)
804f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
8056a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
806f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mOutputs.indexOfKey(output);
807f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
80864cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("stopOutput() unknow output %d", output);
809f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
810f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
811f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
812f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index);
813f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
814f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // handle special case for sonification while in call
815f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (isInCall()) {
816f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        handleIncallSonification(stream, false, false);
817f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
818f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
819f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (outputDesc->mRefCount[stream] > 0) {
820f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // decrement usage count of this stream on the output
821f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->changeRefCount(stream, -1);
822f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // store time at which the stream was stopped - see isStreamActive()
8235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (outputDesc->mRefCount[stream] == 0) {
8245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            outputDesc->mStopTime[stream] = systemTime();
8255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            audio_devices_t newDevice = getNewDevice(output, false /*fromCache*/);
8265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // delay the device switch by twice the latency because stopOutput() is executed when
8275ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // the track stop() command is received and at that time the audio track buffer can
8285ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // still contain data that needs to be drained. The latency only covers the audio HAL
8295ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // and kernel buffers. Also the latency does not always include additional delay in the
8305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // audio path (audio DSP, CODEC ...)
8315ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            setOutputDevice(output, newDevice, false, outputDesc->mLatency*2);
8325ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
8335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // force restoring the device selection on other active outputs if it differs from the
8345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // one being selected for this output
8355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            for (size_t i = 0; i < mOutputs.size(); i++) {
8365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                audio_io_handle_t curOutput = mOutputs.keyAt(i);
8375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                AudioOutputDescriptor *desc = mOutputs.valueAt(i);
8385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                if (curOutput != output &&
83980f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                        desc->isActive() &&
8405ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                        outputDesc->sharesHwModuleWith(desc) &&
841a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                        (newDevice != desc->device())) {
8425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                    setOutputDevice(curOutput,
8435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                    getNewDevice(curOutput, false /*fromCache*/),
8445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                    true,
8455ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                    outputDesc->mLatency*2);
8465ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                }
8475ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
8485ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // update the outputs if stopping one with a stream that can affect notification routing
8495ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            handleNotificationRoutingForStream(stream);
850f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
851f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return NO_ERROR;
852f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
85364cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("stopOutput() refcount is already 0 for output %d", output);
854f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return INVALID_OPERATION;
855f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
856f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
857f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
858f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::releaseOutput(audio_io_handle_t output)
859f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
8606a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("releaseOutput() %d", output);
861f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mOutputs.indexOfKey(output);
862f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
86364cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("releaseOutput() releasing unknown output %d", output);
864f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
865f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
866f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
867f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
868f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    int testIndex = testOutputIndex(output);
869f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (testIndex != 0) {
870f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index);
87180f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent        if (outputDesc->isActive()) {
872f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mpClientInterface->closeOutput(output);
873f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            delete mOutputs.valueAt(index);
874f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mOutputs.removeItem(output);
875f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mTestOutputs[testIndex] = 0;
876f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
877f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
878f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
879f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
880f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
8815a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent    AudioOutputDescriptor *desc = mOutputs.valueAt(index);
8825a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent    if (desc->mFlags & AudioSystem::OUTPUT_FLAG_DIRECT) {
8835a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        if (desc->mDirectOpenCount <= 0) {
8845a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent            ALOGW("releaseOutput() invalid open count %d for output %d",
8855a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent                                                              desc->mDirectOpenCount, output);
8865a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent            return;
8875a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        }
8885a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        if (--desc->mDirectOpenCount == 0) {
8895a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent            closeOutput(output);
89045c763947b657b347211dc9388754e05d30d0467Eric Laurent            // If effects where present on the output, audioflinger moved them to the primary
89145c763947b657b347211dc9388754e05d30d0467Eric Laurent            // output by default: move them back to the appropriate output.
89245c763947b657b347211dc9388754e05d30d0467Eric Laurent            audio_io_handle_t dstOutput = getOutputForEffect();
89345c763947b657b347211dc9388754e05d30d0467Eric Laurent            if (dstOutput != mPrimaryOutput) {
89445c763947b657b347211dc9388754e05d30d0467Eric Laurent                mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, mPrimaryOutput, dstOutput);
89545c763947b657b347211dc9388754e05d30d0467Eric Laurent            }
8965a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        }
897f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
898f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
899f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
90045c763947b657b347211dc9388754e05d30d0467Eric Laurent
901f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinaudio_io_handle_t AudioPolicyManagerBase::getInput(int inputSource,
902f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    uint32_t samplingRate,
903f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    uint32_t format,
90470c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                    uint32_t channelMask,
905f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    AudioSystem::audio_in_acoustics acoustics)
906f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
907f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    audio_io_handle_t input = 0;
908f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent    audio_devices_t device = getDeviceForInputSource(inputSource);
909f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
91070c236c9290732782d5267935af1475b8d5ae602Eric Laurent    ALOGV("getInput() inputSource %d, samplingRate %d, format %d, channelMask %x, acoustics %x",
91170c236c9290732782d5267935af1475b8d5ae602Eric Laurent          inputSource, samplingRate, format, channelMask, acoustics);
912f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
913ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if (device == AUDIO_DEVICE_NONE) {
9145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        ALOGW("getInput() could not find device for inputSource %d", inputSource);
915f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return 0;
916f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
917f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
918f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // adapt channel selection to input source
919f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    switch(inputSource) {
920f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_UPLINK:
92170c236c9290732782d5267935af1475b8d5ae602Eric Laurent        channelMask = AudioSystem::CHANNEL_IN_VOICE_UPLINK;
922f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
923f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_DOWNLINK:
92470c236c9290732782d5267935af1475b8d5ae602Eric Laurent        channelMask = AudioSystem::CHANNEL_IN_VOICE_DNLINK;
925f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
926f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_CALL:
92770c236c9290732782d5267935af1475b8d5ae602Eric Laurent        channelMask = (AudioSystem::CHANNEL_IN_VOICE_UPLINK | AudioSystem::CHANNEL_IN_VOICE_DNLINK);
928f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
929f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    default:
930f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
931f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
932f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
9335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    IOProfile *profile = getInputProfile(device,
9345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                         samplingRate,
9355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                         format,
93670c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                         channelMask);
9375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (profile == NULL) {
9385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        ALOGW("getInput() could not find profile for device %04x, samplingRate %d, format %d,"
93970c236c9290732782d5267935af1475b8d5ae602Eric Laurent                "channelMask %04x",
94070c236c9290732782d5267935af1475b8d5ae602Eric Laurent                device, samplingRate, format, channelMask);
94170c236c9290732782d5267935af1475b8d5ae602Eric Laurent        return 0;
94270c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
94370c236c9290732782d5267935af1475b8d5ae602Eric Laurent
94470c236c9290732782d5267935af1475b8d5ae602Eric Laurent    if (profile->mModule->mHandle == 0) {
9453cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        ALOGE("getInput(): HW module %s not opened", profile->mModule->mName);
9465ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return 0;
9475ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
9485ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
9495ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    AudioInputDescriptor *inputDesc = new AudioInputDescriptor(profile);
950f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
951f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    inputDesc->mInputSource = inputSource;
952f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    inputDesc->mDevice = device;
953f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    inputDesc->mSamplingRate = samplingRate;
95470c236c9290732782d5267935af1475b8d5ae602Eric Laurent    inputDesc->mFormat = (audio_format_t)format;
95570c236c9290732782d5267935af1475b8d5ae602Eric Laurent    inputDesc->mChannelMask = (audio_channel_mask_t)channelMask;
956f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    inputDesc->mRefCount = 0;
95770c236c9290732782d5267935af1475b8d5ae602Eric Laurent    input = mpClientInterface->openInput(profile->mModule->mHandle,
95870c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                    &inputDesc->mDevice,
959f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    &inputDesc->mSamplingRate,
960f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    &inputDesc->mFormat,
96170c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                    &inputDesc->mChannelMask);
962f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
963f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // only accept input with the exact requested set of parameters
964f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (input == 0 ||
965f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        (samplingRate != inputDesc->mSamplingRate) ||
966f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        (format != inputDesc->mFormat) ||
96770c236c9290732782d5267935af1475b8d5ae602Eric Laurent        (channelMask != inputDesc->mChannelMask)) {
96870c236c9290732782d5267935af1475b8d5ae602Eric Laurent        ALOGV("getInput() failed opening input: samplingRate %d, format %d, channelMask %d",
96970c236c9290732782d5267935af1475b8d5ae602Eric Laurent                samplingRate, format, channelMask);
970f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (input != 0) {
971f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mpClientInterface->closeInput(input);
972f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
973f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        delete inputDesc;
974f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return 0;
975f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
976f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mInputs.add(input, inputDesc);
977f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return input;
978f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
979f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
980f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::startInput(audio_io_handle_t input)
981f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
9826a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("startInput() input %d", input);
983f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mInputs.indexOfKey(input);
984f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
98564cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("startInput() unknow input %d", input);
986f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
987f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
988f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioInputDescriptor *inputDesc = mInputs.valueAt(index);
989f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
990f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
991f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mTestInput == 0)
992f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
993f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    {
994fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent        // refuse 2 active AudioRecord clients at the same time except if the active input
995fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent        // uses AUDIO_SOURCE_HOTWORD in which case it is closed.
996fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent        audio_io_handle_t activeInput = getActiveInput();
997fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent        if (!isVirtualInputDevice(inputDesc->mDevice) && activeInput != 0) {
998fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent            AudioInputDescriptor *activeDesc = mInputs.valueFor(activeInput);
999fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent            if (activeDesc->mInputSource == AUDIO_SOURCE_HOTWORD) {
1000fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent                ALOGW("startInput() preempting already started low-priority input %d", activeInput);
1001fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent                stopInput(activeInput);
1002fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent                releaseInput(activeInput);
1003fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent            } else {
1004fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent                ALOGW("startInput() input %d failed: other input already started..", input);
1005fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent                return INVALID_OPERATION;
1006fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent            }
1007f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1008f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1009f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
101066707435156d8d99d795271a7bd54943065b4c2dEric Laurent    audio_devices_t newDevice = getDeviceForInputSource(inputDesc->mInputSource);
101166707435156d8d99d795271a7bd54943065b4c2dEric Laurent    if ((newDevice != AUDIO_DEVICE_NONE) && (newDevice != inputDesc->mDevice)) {
101266707435156d8d99d795271a7bd54943065b4c2dEric Laurent        inputDesc->mDevice = newDevice;
101366707435156d8d99d795271a7bd54943065b4c2dEric Laurent    }
1014b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown
1015b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown    // automatically enable the remote submix output when input is started
1016b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown    if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1017b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown        setDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1018b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown                AudioSystem::DEVICE_STATE_AVAILABLE, AUDIO_REMOTE_SUBMIX_DEVICE_ADDRESS);
1019b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown    }
1020b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown
1021f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioParameter param = AudioParameter();
1022f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    param.addInt(String8(AudioParameter::keyRouting), (int)inputDesc->mDevice);
1023f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1024fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent    int aliasSource = (inputDesc->mInputSource == AUDIO_SOURCE_HOTWORD) ?
1025fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent                                        AUDIO_SOURCE_VOICE_RECOGNITION : inputDesc->mInputSource;
1026fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent
1027fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent    param.addInt(String8(AudioParameter::keyInputSource), aliasSource);
10286a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("AudioPolicyManager::startInput() input source = %d", inputDesc->mInputSource);
1029f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1030f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mpClientInterface->setParameters(input, param.toString());
1031f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1032f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    inputDesc->mRefCount = 1;
1033f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
1034f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1035f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1036f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::stopInput(audio_io_handle_t input)
1037f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
10386a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("stopInput() input %d", input);
1039f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mInputs.indexOfKey(input);
1040f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
104164cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("stopInput() unknow input %d", input);
1042f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
1043f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1044f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioInputDescriptor *inputDesc = mInputs.valueAt(index);
1045f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1046f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (inputDesc->mRefCount == 0) {
104764cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("stopInput() input %d already stopped", input);
1048f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return INVALID_OPERATION;
1049f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
1050b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown        // automatically disable the remote submix output when input is stopped
1051b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown        if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1052b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown            setDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1053b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown                    AudioSystem::DEVICE_STATE_UNAVAILABLE, AUDIO_REMOTE_SUBMIX_DEVICE_ADDRESS);
1054b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown        }
1055b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown
1056f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioParameter param = AudioParameter();
1057f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        param.addInt(String8(AudioParameter::keyRouting), 0);
1058f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mpClientInterface->setParameters(input, param.toString());
1059f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        inputDesc->mRefCount = 0;
1060f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return NO_ERROR;
1061f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1062f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1063f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1064f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::releaseInput(audio_io_handle_t input)
1065f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
10666a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("releaseInput() %d", input);
1067f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mInputs.indexOfKey(input);
1068f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
106964cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("releaseInput() releasing unknown input %d", input);
1070f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
1071f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1072f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mpClientInterface->closeInput(input);
1073f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    delete mInputs.valueAt(index);
1074f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mInputs.removeItem(input);
10756a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("releaseInput() exit");
1076f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1077f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1078f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::initStreamVolume(AudioSystem::stream_type stream,
1079f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            int indexMin,
1080f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            int indexMax)
1081f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
10826a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
1083f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (indexMin < 0 || indexMin >= indexMax) {
108464cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("initStreamVolume() invalid index limits for stream %d, min %d, max %d", stream , indexMin, indexMax);
1085f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
1086f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1087f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mStreams[stream].mIndexMin = indexMin;
1088f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mStreams[stream].mIndexMax = indexMax;
1089f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1090f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1091c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentstatus_t AudioPolicyManagerBase::setStreamVolumeIndex(AudioSystem::stream_type stream,
1092c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                      int index,
1093c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                      audio_devices_t device)
1094f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1095f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1096f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if ((index < mStreams[stream].mIndexMin) || (index > mStreams[stream].mIndexMax)) {
1097f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
1098f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1099c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    if (!audio_is_output_device(device)) {
1100c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        return BAD_VALUE;
1101c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    }
1102f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1103f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // Force max volume if stream cannot be muted
1104f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (!mStreams[stream].mCanBeMuted) index = mStreams[stream].mIndexMax;
1105f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1106b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    ALOGV("setStreamVolumeIndex() stream %d, device %04x, index %d",
1107c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent          stream, device, index);
1108c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
1109c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    // if device is AUDIO_DEVICE_OUT_DEFAULT set default value and
1110c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    // clear all device specific values
1111c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    if (device == AUDIO_DEVICE_OUT_DEFAULT) {
1112c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        mStreams[stream].mIndexCur.clear();
1113c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    }
1114c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    mStreams[stream].mIndexCur.add(device, index);
1115f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1116f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // compute and apply stream volume on all outputs according to connected device
1117f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    status_t status = NO_ERROR;
1118f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mOutputs.size(); i++) {
1119c5eb8b4a5d4395ce335bc7c3e6df2678fa47e2ddEric Laurent        audio_devices_t curDevice =
1120c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                getDeviceForVolume(mOutputs.valueAt(i)->device());
1121e92d623811f3fd3e7cc5e5dd8bc93c0c0a8fdf50Eric Laurent        if ((device == AUDIO_DEVICE_OUT_DEFAULT) || (device == curDevice)) {
1122c5eb8b4a5d4395ce335bc7c3e6df2678fa47e2ddEric Laurent            status_t volStatus = checkAndSetVolume(stream, index, mOutputs.keyAt(i), curDevice);
1123c5eb8b4a5d4395ce335bc7c3e6df2678fa47e2ddEric Laurent            if (volStatus != NO_ERROR) {
1124c5eb8b4a5d4395ce335bc7c3e6df2678fa47e2ddEric Laurent                status = volStatus;
1125c5eb8b4a5d4395ce335bc7c3e6df2678fa47e2ddEric Laurent            }
1126f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1127f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1128f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return status;
1129f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1130f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1131c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentstatus_t AudioPolicyManagerBase::getStreamVolumeIndex(AudioSystem::stream_type stream,
1132c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                      int *index,
1133c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                      audio_devices_t device)
1134f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1135c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    if (index == NULL) {
1136f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
1137f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1138c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    if (!audio_is_output_device(device)) {
1139c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        return BAD_VALUE;
1140c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    }
1141c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    // if device is AUDIO_DEVICE_OUT_DEFAULT, return volume for device corresponding to
1142c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    // the strategy the stream belongs to.
1143c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    if (device == AUDIO_DEVICE_OUT_DEFAULT) {
1144c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
1145c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    }
1146c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    device = getDeviceForVolume(device);
1147c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
1148c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    *index =  mStreams[stream].getVolumeIndex(device);
1149c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index);
1150f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
1151f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1152f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
115345c763947b657b347211dc9388754e05d30d0467Eric Laurentaudio_io_handle_t AudioPolicyManagerBase::selectOutputForEffects(
115445c763947b657b347211dc9388754e05d30d0467Eric Laurent                                            const SortedVector<audio_io_handle_t>& outputs)
115545c763947b657b347211dc9388754e05d30d0467Eric Laurent{
115645c763947b657b347211dc9388754e05d30d0467Eric Laurent    // select one output among several suitable for global effects.
115745c763947b657b347211dc9388754e05d30d0467Eric Laurent    // The priority is as follows:
115845c763947b657b347211dc9388754e05d30d0467Eric Laurent    // 1: An offloaded output. If the effect ends up not being offloadable,
115945c763947b657b347211dc9388754e05d30d0467Eric Laurent    //    AudioFlinger will invalidate the track and the offloaded output
116045c763947b657b347211dc9388754e05d30d0467Eric Laurent    //    will be closed causing the effect to be moved to a PCM output.
116145c763947b657b347211dc9388754e05d30d0467Eric Laurent    // 2: A deep buffer output
116245c763947b657b347211dc9388754e05d30d0467Eric Laurent    // 3: the first output in the list
116345c763947b657b347211dc9388754e05d30d0467Eric Laurent
116445c763947b657b347211dc9388754e05d30d0467Eric Laurent    if (outputs.size() == 0) {
116545c763947b657b347211dc9388754e05d30d0467Eric Laurent        return 0;
116645c763947b657b347211dc9388754e05d30d0467Eric Laurent    }
116745c763947b657b347211dc9388754e05d30d0467Eric Laurent
116845c763947b657b347211dc9388754e05d30d0467Eric Laurent    audio_io_handle_t outputOffloaded = 0;
116945c763947b657b347211dc9388754e05d30d0467Eric Laurent    audio_io_handle_t outputDeepBuffer = 0;
117045c763947b657b347211dc9388754e05d30d0467Eric Laurent
117145c763947b657b347211dc9388754e05d30d0467Eric Laurent    for (size_t i = 0; i < outputs.size(); i++) {
117245c763947b657b347211dc9388754e05d30d0467Eric Laurent        AudioOutputDescriptor *desc = mOutputs.valueFor(outputs[i]);
117345c763947b657b347211dc9388754e05d30d0467Eric Laurent        ALOGV("selectOutputForEffects outputs[%d] flags %x", i, desc->mFlags);
117445c763947b657b347211dc9388754e05d30d0467Eric Laurent        if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
117545c763947b657b347211dc9388754e05d30d0467Eric Laurent            outputOffloaded = outputs[i];
117645c763947b657b347211dc9388754e05d30d0467Eric Laurent        }
117745c763947b657b347211dc9388754e05d30d0467Eric Laurent        if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) {
117845c763947b657b347211dc9388754e05d30d0467Eric Laurent            outputDeepBuffer = outputs[i];
117945c763947b657b347211dc9388754e05d30d0467Eric Laurent        }
118045c763947b657b347211dc9388754e05d30d0467Eric Laurent    }
118145c763947b657b347211dc9388754e05d30d0467Eric Laurent
118245c763947b657b347211dc9388754e05d30d0467Eric Laurent    ALOGV("selectOutputForEffects outputOffloaded %d outputDeepBuffer %d",
118345c763947b657b347211dc9388754e05d30d0467Eric Laurent          outputOffloaded, outputDeepBuffer);
118445c763947b657b347211dc9388754e05d30d0467Eric Laurent    if (outputOffloaded != 0) {
118545c763947b657b347211dc9388754e05d30d0467Eric Laurent        return outputOffloaded;
118645c763947b657b347211dc9388754e05d30d0467Eric Laurent    }
118745c763947b657b347211dc9388754e05d30d0467Eric Laurent    if (outputDeepBuffer != 0) {
118845c763947b657b347211dc9388754e05d30d0467Eric Laurent        return outputDeepBuffer;
118945c763947b657b347211dc9388754e05d30d0467Eric Laurent    }
119045c763947b657b347211dc9388754e05d30d0467Eric Laurent
119145c763947b657b347211dc9388754e05d30d0467Eric Laurent    return outputs[0];
119245c763947b657b347211dc9388754e05d30d0467Eric Laurent}
119345c763947b657b347211dc9388754e05d30d0467Eric Laurent
1194c94dccc97cc3ed5171b45f46a0f7f8762d37156fGlenn Kastenaudio_io_handle_t AudioPolicyManagerBase::getOutputForEffect(const effect_descriptor_t *desc)
1195f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1196f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // apply simple rule where global effects are attached to the same output as MUSIC streams
11974660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen
11984660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen    routing_strategy strategy = getStrategy(AudioSystem::MUSIC);
11994660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen    audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
1200c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs);
120145c763947b657b347211dc9388754e05d30d0467Eric Laurent
120245c763947b657b347211dc9388754e05d30d0467Eric Laurent    audio_io_handle_t output = selectOutputForEffects(dstOutputs);
120345c763947b657b347211dc9388754e05d30d0467Eric Laurent    ALOGV("getOutputForEffect() got output %d for fx %s flags %x",
120445c763947b657b347211dc9388754e05d30d0467Eric Laurent          output, (desc == NULL) ? "unspecified" : desc->name,  (desc == NULL) ? 0 : desc->flags);
120545c763947b657b347211dc9388754e05d30d0467Eric Laurent
120645c763947b657b347211dc9388754e05d30d0467Eric Laurent    return output;
1207f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1208f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1209c94dccc97cc3ed5171b45f46a0f7f8762d37156fGlenn Kastenstatus_t AudioPolicyManagerBase::registerEffect(const effect_descriptor_t *desc,
12101c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent                                audio_io_handle_t io,
1211f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                uint32_t strategy,
1212f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                int session,
1213f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                int id)
1214f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
12151c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent    ssize_t index = mOutputs.indexOfKey(io);
1216f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
12171c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent        index = mInputs.indexOfKey(io);
12181c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent        if (index < 0) {
121964cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("registerEffect() unknown io %d", io);
12201c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent            return INVALID_OPERATION;
12211c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent        }
1222f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1223f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1224f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mTotalEffectsMemory + desc->memoryUsage > getMaxEffectsMemory()) {
122564cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("registerEffect() memory limit exceeded for Fx %s, Memory %d KB",
1226f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                desc->name, desc->memoryUsage);
1227f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return INVALID_OPERATION;
1228f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1229f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mTotalEffectsMemory += desc->memoryUsage;
12306a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("registerEffect() effect %s, io %d, strategy %d session %d id %d",
12311c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent            desc->name, io, strategy, session, id);
12326a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("registerEffect() memory %d, total memory %d", desc->memoryUsage, mTotalEffectsMemory);
1233f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1234f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    EffectDescriptor *pDesc = new EffectDescriptor();
1235f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    memcpy (&pDesc->mDesc, desc, sizeof(effect_descriptor_t));
12361c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent    pDesc->mIo = io;
1237f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    pDesc->mStrategy = (routing_strategy)strategy;
1238f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    pDesc->mSession = session;
1239582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    pDesc->mEnabled = false;
1240582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent
1241f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mEffects.add(id, pDesc);
1242f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1243f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
1244f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1245f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1246f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::unregisterEffect(int id)
1247f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1248f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mEffects.indexOfKey(id);
1249f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
125064cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("unregisterEffect() unknown effect ID %d", id);
1251f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return INVALID_OPERATION;
1252f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1253f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1254f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    EffectDescriptor *pDesc = mEffects.valueAt(index);
1255f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1256582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    setEffectEnabled(pDesc, false);
1257582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent
1258f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mTotalEffectsMemory < pDesc->mDesc.memoryUsage) {
125964cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("unregisterEffect() memory %d too big for total %d",
1260f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                pDesc->mDesc.memoryUsage, mTotalEffectsMemory);
1261f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        pDesc->mDesc.memoryUsage = mTotalEffectsMemory;
1262f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1263f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mTotalEffectsMemory -= pDesc->mDesc.memoryUsage;
12646a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("unregisterEffect() effect %s, ID %d, memory %d total memory %d",
1265582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent            pDesc->mDesc.name, id, pDesc->mDesc.memoryUsage, mTotalEffectsMemory);
1266f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1267f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mEffects.removeItem(id);
1268f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    delete pDesc;
1269f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1270f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
1271f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1272f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1273582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurentstatus_t AudioPolicyManagerBase::setEffectEnabled(int id, bool enabled)
1274582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent{
1275582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    ssize_t index = mEffects.indexOfKey(id);
1276582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    if (index < 0) {
127764cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("unregisterEffect() unknown effect ID %d", id);
1278582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        return INVALID_OPERATION;
1279582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    }
1280582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent
1281582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    return setEffectEnabled(mEffects.valueAt(index), enabled);
1282582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent}
1283582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent
1284582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurentstatus_t AudioPolicyManagerBase::setEffectEnabled(EffectDescriptor *pDesc, bool enabled)
1285582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent{
1286582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    if (enabled == pDesc->mEnabled) {
12876a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("setEffectEnabled(%s) effect already %s",
1288582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent             enabled?"true":"false", enabled?"enabled":"disabled");
1289582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        return INVALID_OPERATION;
1290582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    }
1291582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent
1292582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    if (enabled) {
1293582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        if (mTotalEffectsCpuLoad + pDesc->mDesc.cpuLoad > getMaxEffectsCpuLoad()) {
129464cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("setEffectEnabled(true) CPU Load limit exceeded for Fx %s, CPU %f MIPS",
1295582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent                 pDesc->mDesc.name, (float)pDesc->mDesc.cpuLoad/10);
1296582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent            return INVALID_OPERATION;
1297582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        }
1298582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        mTotalEffectsCpuLoad += pDesc->mDesc.cpuLoad;
12996a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("setEffectEnabled(true) total CPU %d", mTotalEffectsCpuLoad);
1300582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    } else {
1301582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        if (mTotalEffectsCpuLoad < pDesc->mDesc.cpuLoad) {
130264cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("setEffectEnabled(false) CPU load %d too high for total %d",
1303582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent                    pDesc->mDesc.cpuLoad, mTotalEffectsCpuLoad);
1304582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent            pDesc->mDesc.cpuLoad = mTotalEffectsCpuLoad;
1305582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        }
1306582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        mTotalEffectsCpuLoad -= pDesc->mDesc.cpuLoad;
13076a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("setEffectEnabled(false) total CPU %d", mTotalEffectsCpuLoad);
1308582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    }
1309582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    pDesc->mEnabled = enabled;
1310582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    return NO_ERROR;
1311582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent}
1312582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent
1313000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurentbool AudioPolicyManagerBase::isNonOffloadableEffectEnabled()
1314000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent{
1315000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    for (size_t i = 0; i < mEffects.size(); i++) {
1316000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent        const EffectDescriptor * const pDesc = mEffects.valueAt(i);
1317000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent        if (pDesc->mEnabled && (pDesc->mStrategy == STRATEGY_MEDIA) &&
1318000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent                ((pDesc->mDesc.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) == 0)) {
1319000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent            ALOGV("isNonOffloadableEffectEnabled() non offloadable effect %s enabled on session %d",
1320000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent                  pDesc->mDesc.name, pDesc->mSession);
1321000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent            return true;
1322000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent        }
1323000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    }
1324000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    return false;
1325000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent}
1326000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent
1327f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinbool AudioPolicyManagerBase::isStreamActive(int stream, uint32_t inPastMs) const
1328f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1329f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    nsecs_t sysTime = systemTime();
1330f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mOutputs.size(); i++) {
133180f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent        const AudioOutputDescriptor *outputDesc = mOutputs.valueAt(i);
133280f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent        if (outputDesc->isStreamActive((AudioSystem::stream_type)stream, inPastMs, sysTime)) {
1333f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return true;
1334f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1335f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1336f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return false;
1337f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1338f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1339dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivibool AudioPolicyManagerBase::isStreamActiveRemotely(int stream, uint32_t inPastMs) const
1340dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi{
1341dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi    nsecs_t sysTime = systemTime();
1342dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi    for (size_t i = 0; i < mOutputs.size(); i++) {
1343dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi        const AudioOutputDescriptor *outputDesc = mOutputs.valueAt(i);
134480f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent        if (((outputDesc->device() & APM_AUDIO_OUT_DEVICE_REMOTE_ALL) != 0) &&
134580f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                outputDesc->isStreamActive((AudioSystem::stream_type)stream, inPastMs, sysTime)) {
1346dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi            return true;
1347dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi        }
1348dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi    }
1349dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi    return false;
1350dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi}
1351dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi
1352abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivibool AudioPolicyManagerBase::isSourceActive(audio_source_t source) const
1353abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi{
1354abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi    for (size_t i = 0; i < mInputs.size(); i++) {
1355abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi        const AudioInputDescriptor * inputDescriptor = mInputs.valueAt(i);
1356fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent        if ((inputDescriptor->mInputSource == (int)source ||
1357fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent                (source == (audio_source_t)AUDIO_SOURCE_VOICE_RECOGNITION &&
1358fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent                 inputDescriptor->mInputSource == AUDIO_SOURCE_HOTWORD))
1359fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent             && (inputDescriptor->mRefCount > 0)) {
1360abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi            return true;
1361abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi        }
1362abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi    }
1363abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi    return false;
1364abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi}
1365abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi
1366abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi
1367f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::dump(int fd)
1368f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1369f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    const size_t SIZE = 256;
1370f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    char buffer[SIZE];
1371f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    String8 result;
1372f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1373f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
1374f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1375b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
137670c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, " Primary Output: %d\n", mPrimaryOutput);
1377f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1378f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " A2DP device address: %s\n", mA2dpDeviceAddress.string());
1379f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1380f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " SCO device address: %s\n", mScoDeviceAddress.string());
1381f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1382599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    snprintf(buffer, SIZE, " USB audio ALSA %s\n", mUsbCardAndDevice.string());
1383599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    result.append(buffer);
1384f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Output devices: %08x\n", mAvailableOutputDevices);
1385f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1386f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Input devices: %08x\n", mAvailableInputDevices);
1387f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1388f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Phone state: %d\n", mPhoneState);
1389f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1390f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Force use for communications %d\n", mForceUse[AudioSystem::FOR_COMMUNICATION]);
1391f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1392f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Force use for media %d\n", mForceUse[AudioSystem::FOR_MEDIA]);
1393f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1394f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Force use for record %d\n", mForceUse[AudioSystem::FOR_RECORD]);
1395f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1396f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Force use for dock %d\n", mForceUse[AudioSystem::FOR_DOCK]);
1397f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1398738207def5f691d605ae33d041116829a74513a9Eric Laurent    snprintf(buffer, SIZE, " Force use for system %d\n", mForceUse[AudioSystem::FOR_SYSTEM]);
1399738207def5f691d605ae33d041116829a74513a9Eric Laurent    result.append(buffer);
1400f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, result.string(), result.size());
1401f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
14025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
140370c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "\nHW Modules dump:\n");
14045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    write(fd, buffer, strlen(buffer));
140570c236c9290732782d5267935af1475b8d5ae602Eric Laurent    for (size_t i = 0; i < mHwModules.size(); i++) {
140670c236c9290732782d5267935af1475b8d5ae602Eric Laurent        snprintf(buffer, SIZE, "- HW Module %d:\n", i + 1);
14075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        write(fd, buffer, strlen(buffer));
140870c236c9290732782d5267935af1475b8d5ae602Eric Laurent        mHwModules[i]->dump(fd);
14095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
14105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
1411f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "\nOutputs dump:\n");
1412f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
1413f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mOutputs.size(); i++) {
1414f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        snprintf(buffer, SIZE, "- Output %d dump:\n", mOutputs.keyAt(i));
1415f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        write(fd, buffer, strlen(buffer));
1416f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mOutputs.valueAt(i)->dump(fd);
1417f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1418f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1419f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "\nInputs dump:\n");
1420f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
1421f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mInputs.size(); i++) {
1422f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        snprintf(buffer, SIZE, "- Input %d dump:\n", mInputs.keyAt(i));
1423f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        write(fd, buffer, strlen(buffer));
1424f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mInputs.valueAt(i)->dump(fd);
1425f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1426f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1427f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "\nStreams dump:\n");
1428f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
1429c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    snprintf(buffer, SIZE,
1430c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent             " Stream  Can be muted  Index Min  Index Max  Index Cur [device : index]...\n");
1431f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
1432f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) {
1433c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        snprintf(buffer, SIZE, " %02d      ", i);
1434f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        write(fd, buffer, strlen(buffer));
1435c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        mStreams[i].dump(fd);
1436f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1437f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1438f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "\nTotal Effects CPU: %f MIPS, Total Effects memory: %d KB\n",
1439f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            (float)mTotalEffectsCpuLoad/10, mTotalEffectsMemory);
1440f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
1441f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1442f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "Registered effects:\n");
1443f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
1444f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mEffects.size(); i++) {
1445f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        snprintf(buffer, SIZE, "- Effect %d dump:\n", mEffects.keyAt(i));
1446f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        write(fd, buffer, strlen(buffer));
1447f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mEffects.valueAt(i)->dump(fd);
1448f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1449f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1450f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1451f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
1452f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1453f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1454a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald// This function checks for the parameters which can be offloaded.
1455a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald// This can be enhanced depending on the capability of the DSP and policy
1456a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald// of the system.
1457b4d07b97d23cfaffe22c7859ad7c45e168a7df0eRichard Fitzgeraldbool AudioPolicyManagerBase::isOffloadSupported(const audio_offload_info_t& offloadInfo)
1458b4d07b97d23cfaffe22c7859ad7c45e168a7df0eRichard Fitzgerald{
1459a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
1460a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald     " BitRate=%u, duration=%lld us, has_video=%d",
1461a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald     offloadInfo.sample_rate, offloadInfo.channel_mask,
1462a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald     offloadInfo.format,
1463a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald     offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
1464a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald     offloadInfo.has_video);
1465a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald
1466a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    // Check if offload has been disabled
1467a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    char propValue[PROPERTY_VALUE_MAX];
1468a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    if (property_get("audio.offload.disable", propValue, "0")) {
1469a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        if (atoi(propValue) != 0) {
1470a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald            ALOGV("offload disabled by audio.offload.disable=%s", propValue );
1471a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald            return false;
1472a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        }
1473a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    }
1474a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald
1475a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    // Check if stream type is music, then only allow offload as of now.
1476a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
1477a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    {
147841b150b659d4ba33a9be9f99fd9f0ee1f2787541Eric Laurent        ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
147941b150b659d4ba33a9be9f99fd9f0ee1f2787541Eric Laurent        return false;
148041b150b659d4ba33a9be9f99fd9f0ee1f2787541Eric Laurent    }
148141b150b659d4ba33a9be9f99fd9f0ee1f2787541Eric Laurent
148241b150b659d4ba33a9be9f99fd9f0ee1f2787541Eric Laurent    //TODO: enable audio offloading with video when ready
148341b150b659d4ba33a9be9f99fd9f0ee1f2787541Eric Laurent    if (offloadInfo.has_video)
148441b150b659d4ba33a9be9f99fd9f0ee1f2787541Eric Laurent    {
148541b150b659d4ba33a9be9f99fd9f0ee1f2787541Eric Laurent        ALOGV("isOffloadSupported: has_video == true, returning false");
1486a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        return false;
1487a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    }
1488a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald
1489a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    //If duration is less than minimum value defined in property, return false
1490a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
1491a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
1492a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald            ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
1493a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald            return false;
1494a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        }
1495a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
1496a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
1497a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        return false;
1498a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    }
1499a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald
1500000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    // Do not allow offloading if one non offloadable effect is enabled. This prevents from
1501000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    // creating an offloaded track and tearing it down immediately after start when audioflinger
1502000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    // detects there is an active non offloadable effect.
1503000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    // FIXME: We should check the audio session here but we do not have it in this context.
1504000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    // This may prevent offloading in rare situations where effects are left active by apps
1505000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    // in the background.
1506000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    if (isNonOffloadableEffectEnabled()) {
1507000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent        return false;
1508000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    }
1509000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent
1510a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    // See if there is a profile to support this.
1511a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    // AUDIO_DEVICE_NONE
1512a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    IOProfile *profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
1513a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                                            offloadInfo.sample_rate,
1514a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                                            offloadInfo.format,
1515a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                                            offloadInfo.channel_mask,
1516a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                                            AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
1517a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    ALOGV("isOffloadSupported() profile %sfound", profile != NULL ? "" : "NOT ");
1518a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    return (profile != NULL);
1519b4d07b97d23cfaffe22c7859ad7c45e168a7df0eRichard Fitzgerald}
1520b4d07b97d23cfaffe22c7859ad7c45e168a7df0eRichard Fitzgerald
1521f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// ----------------------------------------------------------------------------
1522f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// AudioPolicyManagerBase
1523f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// ----------------------------------------------------------------------------
1524f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1525f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima ZavinAudioPolicyManagerBase::AudioPolicyManagerBase(AudioPolicyClientInterface *clientInterface)
1526f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    :
1527f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
1528f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    Thread(false),
1529f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
153070c236c9290732782d5267935af1475b8d5ae602Eric Laurent    mPrimaryOutput((audio_io_handle_t)0),
1531ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    mAvailableOutputDevices(AUDIO_DEVICE_NONE),
1532ca0657a1ca087a6d474a75fcfedd6aac3901d587Glenn Kasten    mPhoneState(AudioSystem::MODE_NORMAL),
1533f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
1534f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mTotalEffectsCpuLoad(0), mTotalEffectsMemory(0),
153518fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi    mA2dpSuspended(false), mHasA2dp(false), mHasUsb(false), mHasRemoteSubmix(false),
153618fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi    mSpeakerDrcEnabled(false)
1537f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1538f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mpClientInterface = clientInterface;
1539f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1540f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < AudioSystem::NUM_FORCE_USE; i++) {
1541f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mForceUse[i] = AudioSystem::FORCE_NONE;
1542f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1543f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1544f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mA2dpDeviceAddress = String8("");
1545f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mScoDeviceAddress = String8("");
1546599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    mUsbCardAndDevice = String8("");
1547f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
15485ec145df7708564d385fd3fb764085321cf4c253Dima Zavin    if (loadAudioPolicyConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE) != NO_ERROR) {
15495ec145df7708564d385fd3fb764085321cf4c253Dima Zavin        if (loadAudioPolicyConfig(AUDIO_POLICY_CONFIG_FILE) != NO_ERROR) {
1550739022f26a7127ba76a98dda65411496086114a7Dima Zavin            ALOGE("could not load audio policy configuration file, setting defaults");
1551739022f26a7127ba76a98dda65411496086114a7Dima Zavin            defaultAudioPolicyConfig();
15525ec145df7708564d385fd3fb764085321cf4c253Dima Zavin        }
15535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
15545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
155518fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi    // must be done after reading the policy
155618fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi    initializeVolumeCurves();
155718fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi
1558b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    // open all output streams needed to access attached devices
155970c236c9290732782d5267935af1475b8d5ae602Eric Laurent    for (size_t i = 0; i < mHwModules.size(); i++) {
156070c236c9290732782d5267935af1475b8d5ae602Eric Laurent        mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->mName);
156170c236c9290732782d5267935af1475b8d5ae602Eric Laurent        if (mHwModules[i]->mHandle == 0) {
156270c236c9290732782d5267935af1475b8d5ae602Eric Laurent            ALOGW("could not open HW module %s", mHwModules[i]->mName);
156370c236c9290732782d5267935af1475b8d5ae602Eric Laurent            continue;
156470c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
156570c236c9290732782d5267935af1475b8d5ae602Eric Laurent        // open all output streams needed to access attached devices
1566a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        // except for direct output streams that are only opened when they are actually
1567a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        // required by an app.
156870c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
156970c236c9290732782d5267935af1475b8d5ae602Eric Laurent        {
157070c236c9290732782d5267935af1475b8d5ae602Eric Laurent            const IOProfile *outProfile = mHwModules[i]->mOutputProfiles[j];
157170c236c9290732782d5267935af1475b8d5ae602Eric Laurent
1572a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald            if ((outProfile->mSupportedDevices & mAttachedOutputDevices) &&
1573a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                    ((outProfile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0)) {
157470c236c9290732782d5267935af1475b8d5ae602Eric Laurent                AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(outProfile);
157570c236c9290732782d5267935af1475b8d5ae602Eric Laurent                outputDesc->mDevice = (audio_devices_t)(mDefaultOutputDevice &
157670c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                            outProfile->mSupportedDevices);
157770c236c9290732782d5267935af1475b8d5ae602Eric Laurent                audio_io_handle_t output = mpClientInterface->openOutput(
157870c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                outProfile->mModule->mHandle,
157970c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mDevice,
158070c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mSamplingRate,
158170c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mFormat,
158270c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mChannelMask,
158370c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mLatency,
158470c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                outputDesc->mFlags);
158570c236c9290732782d5267935af1475b8d5ae602Eric Laurent                if (output == 0) {
158670c236c9290732782d5267935af1475b8d5ae602Eric Laurent                    delete outputDesc;
158770c236c9290732782d5267935af1475b8d5ae602Eric Laurent                } else {
158870c236c9290732782d5267935af1475b8d5ae602Eric Laurent                    mAvailableOutputDevices = (audio_devices_t)(mAvailableOutputDevices |
158970c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                            (outProfile->mSupportedDevices & mAttachedOutputDevices));
159070c236c9290732782d5267935af1475b8d5ae602Eric Laurent                    if (mPrimaryOutput == 0 &&
15910977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent                            outProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
159270c236c9290732782d5267935af1475b8d5ae602Eric Laurent                        mPrimaryOutput = output;
159370c236c9290732782d5267935af1475b8d5ae602Eric Laurent                    }
159470c236c9290732782d5267935af1475b8d5ae602Eric Laurent                    addOutput(output, outputDesc);
159570c236c9290732782d5267935af1475b8d5ae602Eric Laurent                    setOutputDevice(output,
159670c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                    (audio_devices_t)(mDefaultOutputDevice &
159770c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                        outProfile->mSupportedDevices),
159870c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                    true);
1599b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                }
1600b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            }
1601b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        }
1602f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1603f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
16045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGE_IF((mAttachedOutputDevices & ~mAvailableOutputDevices),
1605b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent             "Not output found for attached devices %08x",
16065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent             (mAttachedOutputDevices & ~mAvailableOutputDevices));
1607b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1608b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output");
1609b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1610c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    updateDevicesAndOutputs();
16113cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
1612f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
1613b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (mPrimaryOutput != 0) {
1614f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioParameter outputCmd = AudioParameter();
1615f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputCmd.addInt(String8("set_id"), 0);
1616b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        mpClientInterface->setParameters(mPrimaryOutput, outputCmd.toString());
1617f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1618c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        mTestDevice = AUDIO_DEVICE_OUT_SPEAKER;
1619f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mTestSamplingRate = 44100;
1620f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mTestFormat = AudioSystem::PCM_16_BIT;
1621f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mTestChannels =  AudioSystem::CHANNEL_OUT_STEREO;
1622f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mTestLatencyMs = 0;
1623f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mCurOutput = 0;
1624f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mDirectOutput = false;
1625f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
1626f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mTestOutputs[i] = 0;
1627f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1628f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1629f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        const size_t SIZE = 256;
1630f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        char buffer[SIZE];
1631f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        snprintf(buffer, SIZE, "AudioPolicyManagerTest");
1632f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        run(buffer, ANDROID_PRIORITY_AUDIO);
1633f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1634f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
1635f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1636f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1637f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima ZavinAudioPolicyManagerBase::~AudioPolicyManagerBase()
1638f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1639f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
1640f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    exit();
1641f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
1642f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin   for (size_t i = 0; i < mOutputs.size(); i++) {
1643f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mpClientInterface->closeOutput(mOutputs.keyAt(i));
1644f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        delete mOutputs.valueAt(i);
1645f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin   }
1646f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin   for (size_t i = 0; i < mInputs.size(); i++) {
1647f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mpClientInterface->closeInput(mInputs.keyAt(i));
1648f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        delete mInputs.valueAt(i);
1649f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin   }
165070c236c9290732782d5267935af1475b8d5ae602Eric Laurent   for (size_t i = 0; i < mHwModules.size(); i++) {
165170c236c9290732782d5267935af1475b8d5ae602Eric Laurent        delete mHwModules[i];
16525ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent   }
1653f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1654f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1655f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::initCheck()
1656f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1657b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    return (mPrimaryOutput == 0) ? NO_INIT : NO_ERROR;
1658f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1659f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1660f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
1661f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinbool AudioPolicyManagerBase::threadLoop()
1662f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
16636a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("entering threadLoop()");
1664f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    while (!exitPending())
1665f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    {
1666f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        String8 command;
1667f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        int valueInt;
1668f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        String8 value;
1669f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1670f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        Mutex::Autolock _l(mLock);
1671f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mWaitWorkCV.waitRelative(mLock, milliseconds(50));
1672f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1673f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        command = mpClientInterface->getParameters(0, String8("test_cmd_policy"));
1674f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioParameter param = AudioParameter(command);
1675f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1676f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR &&
1677f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            valueInt != 0) {
16786a70518b93928d1c91457ff805e375c82d76b0e5Steve Block            ALOGV("Test command %s received", command.string());
1679f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            String8 target;
1680f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.get(String8("target"), target) != NO_ERROR) {
1681f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                target = "Manager";
1682f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1683f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) {
1684f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_output"));
1685f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mCurOutput = valueInt;
1686f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1687f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) {
1688f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_direct"));
1689f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (value == "false") {
1690f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    mDirectOutput = false;
1691f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else if (value == "true") {
1692f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    mDirectOutput = true;
1693f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1694f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1695f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) {
1696f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_input"));
1697f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mTestInput = valueInt;
1698f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1699f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1700f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) {
1701f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_format"));
1702f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                int format = AudioSystem::INVALID_FORMAT;
1703f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (value == "PCM 16 bits") {
1704f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    format = AudioSystem::PCM_16_BIT;
1705f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else if (value == "PCM 8 bits") {
1706f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    format = AudioSystem::PCM_8_BIT;
1707f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else if (value == "Compressed MP3") {
1708f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    format = AudioSystem::MP3;
1709f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1710f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (format != AudioSystem::INVALID_FORMAT) {
1711f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    if (target == "Manager") {
1712f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mTestFormat = format;
1713f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    } else if (mTestOutputs[mCurOutput] != 0) {
1714f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        AudioParameter outputParam = AudioParameter();
1715f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        outputParam.addInt(String8("format"), format);
1716f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
1717f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    }
1718f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1719f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1720f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) {
1721f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_channels"));
1722f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                int channels = 0;
1723f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1724f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (value == "Channels Stereo") {
1725f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    channels =  AudioSystem::CHANNEL_OUT_STEREO;
1726f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else if (value == "Channels Mono") {
1727f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    channels =  AudioSystem::CHANNEL_OUT_MONO;
1728f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1729f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (channels != 0) {
1730f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    if (target == "Manager") {
1731f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mTestChannels = channels;
1732f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    } else if (mTestOutputs[mCurOutput] != 0) {
1733f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        AudioParameter outputParam = AudioParameter();
1734f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        outputParam.addInt(String8("channels"), channels);
1735f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
1736f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    }
1737f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1738f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1739f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) {
1740f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_sampleRate"));
1741f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (valueInt >= 0 && valueInt <= 96000) {
1742f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    int samplingRate = valueInt;
1743f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    if (target == "Manager") {
1744f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mTestSamplingRate = samplingRate;
1745f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    } else if (mTestOutputs[mCurOutput] != 0) {
1746f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        AudioParameter outputParam = AudioParameter();
1747f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        outputParam.addInt(String8("sampling_rate"), samplingRate);
1748f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
1749f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    }
1750f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1751f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1752f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1753f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) {
1754f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_reopen"));
1755f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
175670c236c9290732782d5267935af1475b8d5ae602Eric Laurent                AudioOutputDescriptor *outputDesc = mOutputs.valueFor(mPrimaryOutput);
1757b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                mpClientInterface->closeOutput(mPrimaryOutput);
175870c236c9290732782d5267935af1475b8d5ae602Eric Laurent
175970c236c9290732782d5267935af1475b8d5ae602Eric Laurent                audio_module_handle_t moduleHandle = outputDesc->mModule->mHandle;
176070c236c9290732782d5267935af1475b8d5ae602Eric Laurent
1761b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                delete mOutputs.valueFor(mPrimaryOutput);
1762b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                mOutputs.removeItem(mPrimaryOutput);
1763f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1764b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(NULL);
1765c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER;
176670c236c9290732782d5267935af1475b8d5ae602Eric Laurent                mPrimaryOutput = mpClientInterface->openOutput(moduleHandle,
176770c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mDevice,
1768f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                &outputDesc->mSamplingRate,
1769f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                &outputDesc->mFormat,
177070c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mChannelMask,
1771f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                &outputDesc->mLatency,
1772f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                outputDesc->mFlags);
1773b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                if (mPrimaryOutput == 0) {
17745efbd421e0029d6fc44b1cc65c0e5e0d85e5161fSteve Block                    ALOGE("Failed to reopen hardware output stream, samplingRate: %d, format %d, channels %d",
177570c236c9290732782d5267935af1475b8d5ae602Eric Laurent                            outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask);
1776f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else {
1777f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    AudioParameter outputCmd = AudioParameter();
1778f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    outputCmd.addInt(String8("set_id"), 0);
1779b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                    mpClientInterface->setParameters(mPrimaryOutput, outputCmd.toString());
1780b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                    addOutput(mPrimaryOutput, outputDesc);
1781f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1782f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1783f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1784f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1785f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mpClientInterface->setParameters(0, String8("test_cmd_policy="));
1786f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1787f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1788f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return false;
1789f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1790f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1791f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::exit()
1792f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1793f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    {
1794f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AutoMutex _l(mLock);
1795f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        requestExit();
1796f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mWaitWorkCV.signal();
1797f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1798f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    requestExitAndWait();
1799f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1800f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1801f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinint AudioPolicyManagerBase::testOutputIndex(audio_io_handle_t output)
1802f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1803f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
1804f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (output == mTestOutputs[i]) return i;
1805f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1806f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return 0;
1807f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1808f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
1809f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1810f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// ---
1811f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1812f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::addOutput(audio_io_handle_t id, AudioOutputDescriptor *outputDesc)
1813f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1814f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    outputDesc->mId = id;
1815f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mOutputs.add(id, outputDesc);
1816f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1817f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1818f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
18193cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurentstatus_t AudioPolicyManagerBase::checkOutputsForDevice(audio_devices_t device,
18203cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                       AudioSystem::device_connection_state state,
18213cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                       SortedVector<audio_io_handle_t>& outputs)
1822f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
18233cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    AudioOutputDescriptor *desc;
1824b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1825b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (state == AudioSystem::DEVICE_STATE_AVAILABLE) {
18263cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        // first list already open outputs that can be routed to this device
1827b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        for (size_t i = 0; i < mOutputs.size(); i++) {
18283cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            desc = mOutputs.valueAt(i);
18293cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            if (!desc->isDuplicated() && (desc->mProfile->mSupportedDevices & device)) {
18303cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i));
18313cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                outputs.add(mOutputs.keyAt(i));
1832b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            }
1833b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        }
18343cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        // then look for output profiles that can be routed to this device
18353cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        SortedVector<IOProfile *> profiles;
183670c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (size_t i = 0; i < mHwModules.size(); i++)
1837b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        {
183870c236c9290732782d5267935af1475b8d5ae602Eric Laurent            if (mHwModules[i]->mHandle == 0) {
183970c236c9290732782d5267935af1475b8d5ae602Eric Laurent                continue;
184070c236c9290732782d5267935af1475b8d5ae602Eric Laurent            }
184170c236c9290732782d5267935af1475b8d5ae602Eric Laurent            for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
184270c236c9290732782d5267935af1475b8d5ae602Eric Laurent            {
184370c236c9290732782d5267935af1475b8d5ae602Eric Laurent                if (mHwModules[i]->mOutputProfiles[j]->mSupportedDevices & device) {
18443cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    ALOGV("checkOutputsForDevice(): adding profile %d from module %d", j, i);
18453cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    profiles.add(mHwModules[i]->mOutputProfiles[j]);
184670c236c9290732782d5267935af1475b8d5ae602Eric Laurent                }
184770c236c9290732782d5267935af1475b8d5ae602Eric Laurent            }
1848b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        }
184970c236c9290732782d5267935af1475b8d5ae602Eric Laurent
18503cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        if (profiles.isEmpty() && outputs.isEmpty()) {
18513cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
18523cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            return BAD_VALUE;
185370c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
185470c236c9290732782d5267935af1475b8d5ae602Eric Laurent
18553cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        // open outputs for matching profiles if needed. Direct outputs are also opened to
18563cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        // query for dynamic parameters and will be closed later by setDeviceConnectionState()
18573cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
18583cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            IOProfile *profile = profiles[profile_index];
1859b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
18603cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            // nothing to do if one output is already opened for this profile
18613cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            size_t j;
1862fd8cecbee6843b444d56a1db40af76027e2b19f1Eric Laurent            for (j = 0; j < mOutputs.size(); j++) {
18633cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                desc = mOutputs.valueAt(j);
18643cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                if (!desc->isDuplicated() && desc->mProfile == profile) {
18653cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    break;
18663cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                }
18673cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
1868fd8cecbee6843b444d56a1db40af76027e2b19f1Eric Laurent            if (j != mOutputs.size()) {
18693cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                continue;
18703cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
18713cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
18723cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            ALOGV("opening output for device %08x", device);
18733cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            desc = new AudioOutputDescriptor(profile);
18743cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            desc->mDevice = device;
1875727fef1b1a8cb6d6609ec99a749fcd9573a3417dEric Laurent            audio_offload_info_t offloadInfo = AUDIO_INFO_INITIALIZER;
1876727fef1b1a8cb6d6609ec99a749fcd9573a3417dEric Laurent            offloadInfo.sample_rate = desc->mSamplingRate;
1877727fef1b1a8cb6d6609ec99a749fcd9573a3417dEric Laurent            offloadInfo.format = desc->mFormat;
1878727fef1b1a8cb6d6609ec99a749fcd9573a3417dEric Laurent            offloadInfo.channel_mask = desc->mChannelMask;
1879727fef1b1a8cb6d6609ec99a749fcd9573a3417dEric Laurent
18803cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            audio_io_handle_t output = mpClientInterface->openOutput(profile->mModule->mHandle,
18813cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                       &desc->mDevice,
18823cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                       &desc->mSamplingRate,
18833cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                       &desc->mFormat,
18843cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                       &desc->mChannelMask,
18853cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                       &desc->mLatency,
1886727fef1b1a8cb6d6609ec99a749fcd9573a3417dEric Laurent                                                                       desc->mFlags,
1887727fef1b1a8cb6d6609ec99a749fcd9573a3417dEric Laurent                                                                       &offloadInfo);
18883cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            if (output != 0) {
18893cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
18903cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    String8 reply;
18913cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    char *value;
18923cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    if (profile->mSamplingRates[0] == 0) {
18933cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        reply = mpClientInterface->getParameters(output,
18943cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES));
18953cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        ALOGV("checkOutputsForDevice() direct output sup sampling rates %s",
18963cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                  reply.string());
18973cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        value = strpbrk((char *)reply.string(), "=");
18983cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        if (value != NULL) {
18995debe92591363de3d73bc32b62f23df707b1aecfJohn Grossman                            loadSamplingRates(value + 1, profile);
19003cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        }
19013cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
19023cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    if (profile->mFormats[0] == 0) {
19033cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        reply = mpClientInterface->getParameters(output,
19043cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                       String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
19053cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        ALOGV("checkOutputsForDevice() direct output sup formats %s",
19063cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                  reply.string());
19073cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        value = strpbrk((char *)reply.string(), "=");
19083cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        if (value != NULL) {
19095debe92591363de3d73bc32b62f23df707b1aecfJohn Grossman                            loadFormats(value + 1, profile);
19103cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        }
19113cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
19123cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    if (profile->mChannelMasks[0] == 0) {
19133cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        reply = mpClientInterface->getParameters(output,
19143cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                      String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS));
19153cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        ALOGV("checkOutputsForDevice() direct output sup channel masks %s",
19163cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                  reply.string());
19173cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        value = strpbrk((char *)reply.string(), "=");
19183cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        if (value != NULL) {
19193cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                            loadOutChannels(value + 1, profile);
19203cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        }
19213cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
19223cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    if (((profile->mSamplingRates[0] == 0) &&
19233cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                             (profile->mSamplingRates.size() < 2)) ||
19243cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                         ((profile->mFormats[0] == 0) &&
19253cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                             (profile->mFormats.size() < 2)) ||
19263cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                         ((profile->mFormats[0] == 0) &&
19273cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                             (profile->mChannelMasks.size() < 2))) {
19283cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        ALOGW("checkOutputsForDevice() direct output missing param");
192910705c404c48ec9ac4ce47912e7731f9249cce1dJason Simmons                        mpClientInterface->closeOutput(output);
19303cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        output = 0;
19313cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    } else {
19323cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        addOutput(output, desc);
19333cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
19343cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                } else {
19353cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    audio_io_handle_t duplicatedOutput = 0;
19363cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    // add output descriptor
19373cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    addOutput(output, desc);
19383cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    // set initial stream volume for device
19394366b4a6735e5da342b56773073f0b41197c777fEric Laurent                    applyStreamVolumes(output, device, 0, true);
19403cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
19413cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    //TODO: configure audio effect output stage here
19423cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
19433cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    // open a duplicating output thread for the new output and the primary output
19443cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    duplicatedOutput = mpClientInterface->openDuplicateOutput(output,
19453cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                              mPrimaryOutput);
19463cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    if (duplicatedOutput != 0) {
19473cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        // add duplicated output descriptor
19483cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        AudioOutputDescriptor *dupOutputDesc = new AudioOutputDescriptor(NULL);
19493cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        dupOutputDesc->mOutput1 = mOutputs.valueFor(mPrimaryOutput);
19503cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        dupOutputDesc->mOutput2 = mOutputs.valueFor(output);
19513cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        dupOutputDesc->mSamplingRate = desc->mSamplingRate;
19523cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        dupOutputDesc->mFormat = desc->mFormat;
19533cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        dupOutputDesc->mChannelMask = desc->mChannelMask;
19543cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        dupOutputDesc->mLatency = desc->mLatency;
19553cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        addOutput(duplicatedOutput, dupOutputDesc);
19564366b4a6735e5da342b56773073f0b41197c777fEric Laurent                        applyStreamVolumes(duplicatedOutput, device, 0, true);
19573cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    } else {
19583cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        ALOGW("checkOutputsForDevice() could not open dup output for %d and %d",
19593cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                mPrimaryOutput, output);
19603cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        mpClientInterface->closeOutput(output);
19613cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        mOutputs.removeItem(output);
19623cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        output = 0;
19633cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
19643cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                }
19653cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
19663cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            if (output == 0) {
19673cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                ALOGW("checkOutputsForDevice() could not open output for device %x", device);
19683cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                delete desc;
19693cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                profiles.removeAt(profile_index);
19703cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                profile_index--;
1971b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            } else {
19723cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                outputs.add(output);
19733cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                ALOGV("checkOutputsForDevice(): adding output %d", output);
1974f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
19753cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        }
19763cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
19773cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        if (profiles.isEmpty()) {
19783cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
19793cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            return BAD_VALUE;
1980f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1981f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
1982b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        // check if one opened output is not needed any more after disconnecting one device
1983b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        for (size_t i = 0; i < mOutputs.size(); i++) {
19843cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            desc = mOutputs.valueAt(i);
19853cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            if (!desc->isDuplicated() &&
19863cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    !(desc->mProfile->mSupportedDevices & mAvailableOutputDevices)) {
19873cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                ALOGV("checkOutputsForDevice(): disconnecting adding output %d", mOutputs.keyAt(i));
19883cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                outputs.add(mOutputs.keyAt(i));
19893cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
19903cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        }
19913cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        for (size_t i = 0; i < mHwModules.size(); i++)
19923cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        {
19933cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            if (mHwModules[i]->mHandle == 0) {
19943cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                continue;
19953cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
19963cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
19973cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            {
19983cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                IOProfile *profile = mHwModules[i]->mOutputProfiles[j];
19993cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                if ((profile->mSupportedDevices & device) &&
20003cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        (profile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
20013cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    ALOGV("checkOutputsForDevice(): clearing direct output profile %d on module %d",
20023cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                          j, i);
20033cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    if (profile->mSamplingRates[0] == 0) {
20043cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        profile->mSamplingRates.clear();
20053cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        profile->mSamplingRates.add(0);
20063cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
20073cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    if (profile->mFormats[0] == 0) {
20083cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        profile->mFormats.clear();
20093cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        profile->mFormats.add((audio_format_t)0);
20103cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
20113cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    if (profile->mChannelMasks[0] == 0) {
20123cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        profile->mChannelMasks.clear();
20133cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        profile->mChannelMasks.add((audio_channel_mask_t)0);
20143cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
20153cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                }
2016b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            }
2017f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2018f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
20193cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    return NO_ERROR;
2020f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2021f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2022b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurentvoid AudioPolicyManagerBase::closeOutput(audio_io_handle_t output)
2023f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2024b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    ALOGV("closeOutput(%d)", output);
2025f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2026b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
2027b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (outputDesc == NULL) {
2028b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        ALOGW("closeOutput() unknown output %d", output);
2029b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        return;
2030f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2031f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2032b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    // look for duplicated outputs connected to the output being removed.
2033b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    for (size_t i = 0; i < mOutputs.size(); i++) {
2034b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        AudioOutputDescriptor *dupOutputDesc = mOutputs.valueAt(i);
2035b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        if (dupOutputDesc->isDuplicated() &&
2036b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                (dupOutputDesc->mOutput1 == outputDesc ||
2037b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                dupOutputDesc->mOutput2 == outputDesc)) {
2038b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            AudioOutputDescriptor *outputDesc2;
2039b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            if (dupOutputDesc->mOutput1 == outputDesc) {
2040b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                outputDesc2 = dupOutputDesc->mOutput2;
2041b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            } else {
2042b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                outputDesc2 = dupOutputDesc->mOutput1;
2043b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            }
2044b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            // As all active tracks on duplicated output will be deleted,
2045b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            // and as they were also referenced on the other output, the reference
2046b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            // count for their stream type must be adjusted accordingly on
2047b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            // the other output.
2048b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            for (int j = 0; j < (int)AudioSystem::NUM_STREAM_TYPES; j++) {
2049b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                int refCount = dupOutputDesc->mRefCount[j];
2050b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                outputDesc2->changeRefCount((AudioSystem::stream_type)j,-refCount);
2051b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            }
2052b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i);
2053b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput);
2054f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2055b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            mpClientInterface->closeOutput(duplicatedOutput);
2056b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            delete mOutputs.valueFor(duplicatedOutput);
2057b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            mOutputs.removeItem(duplicatedOutput);
2058f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2059f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2060b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
2061b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    AudioParameter param;
2062b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    param.add(String8("closing"), String8("true"));
2063b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    mpClientInterface->setParameters(output, param.toString());
2064b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
2065b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    mpClientInterface->closeOutput(output);
20665a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent    delete outputDesc;
2067b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    mOutputs.removeItem(output);
20685a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent    mPreviousOutputs = mOutputs;
2069f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2070f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2071c952527e6f89d5427881462823514be9d79f13e6Eric LaurentSortedVector<audio_io_handle_t> AudioPolicyManagerBase::getOutputsForDevice(audio_devices_t device,
2072c952527e6f89d5427881462823514be9d79f13e6Eric Laurent                        DefaultKeyedVector<audio_io_handle_t, AudioOutputDescriptor *> openOutputs)
2073f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2074b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    SortedVector<audio_io_handle_t> outputs;
2075f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
20763cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGVV("getOutputsForDevice() device %04x", device);
2077c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    for (size_t i = 0; i < openOutputs.size(); i++) {
20783cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        ALOGVV("output %d isDuplicated=%d device=%04x",
2079c952527e6f89d5427881462823514be9d79f13e6Eric Laurent                i, openOutputs.valueAt(i)->isDuplicated(), openOutputs.valueAt(i)->supportedDevices());
2080c952527e6f89d5427881462823514be9d79f13e6Eric Laurent        if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) {
2081c952527e6f89d5427881462823514be9d79f13e6Eric Laurent            ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i));
2082c952527e6f89d5427881462823514be9d79f13e6Eric Laurent            outputs.add(openOutputs.keyAt(i));
2083f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2084f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2085b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    return outputs;
2086f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2087f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2088b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurentbool AudioPolicyManagerBase::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
2089b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                                   SortedVector<audio_io_handle_t>& outputs2)
2090f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2091b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (outputs1.size() != outputs2.size()) {
2092b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        return false;
2093f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2094b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    for (size_t i = 0; i < outputs1.size(); i++) {
2095b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        if (outputs1[i] != outputs2[i]) {
2096b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            return false;
2097f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2098f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2099b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    return true;
2100b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent}
2101b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
2102b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurentvoid AudioPolicyManagerBase::checkOutputForStrategy(routing_strategy strategy)
2103b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent{
210401e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent    audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
210501e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent    audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
2106c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs);
2107c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
2108b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
2109b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (!vectorsEqual(srcOutputs,dstOutputs)) {
2110b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d",
2111b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent              strategy, srcOutputs[0], dstOutputs[0]);
21125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // mute strategy while moving tracks from one output to another
21135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        for (size_t i = 0; i < srcOutputs.size(); i++) {
2114fa3697d716b444bbea6be480801536c44bf69214Eric Laurent            AudioOutputDescriptor *desc = mOutputs.valueFor(srcOutputs[i]);
211580f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent            if (desc->isStrategyActive(strategy)) {
2116fa3697d716b444bbea6be480801536c44bf69214Eric Laurent                setStrategyMute(strategy, true, srcOutputs[i]);
2117fa3697d716b444bbea6be480801536c44bf69214Eric Laurent                setStrategyMute(strategy, false, srcOutputs[i], MUTE_TIME_MS, newDevice);
2118fa3697d716b444bbea6be480801536c44bf69214Eric Laurent            }
21195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
2120f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2121f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // Move effects associated to this strategy from previous output to new output
21224660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen        if (strategy == STRATEGY_MEDIA) {
212345c763947b657b347211dc9388754e05d30d0467Eric Laurent            audio_io_handle_t fxOutput = selectOutputForEffects(dstOutputs);
21244660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen            SortedVector<audio_io_handle_t> moved;
21254660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen            for (size_t i = 0; i < mEffects.size(); i++) {
21264660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                EffectDescriptor *desc = mEffects.valueAt(i);
21274660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                if (desc->mSession == AUDIO_SESSION_OUTPUT_MIX &&
212845c763947b657b347211dc9388754e05d30d0467Eric Laurent                        desc->mIo != fxOutput) {
21294660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                    if (moved.indexOf(desc->mIo) < 0) {
21304660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                        ALOGV("checkOutputForStrategy() moving effect %d to output %d",
213145c763947b657b347211dc9388754e05d30d0467Eric Laurent                              mEffects.keyAt(i), fxOutput);
21324660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                        mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, desc->mIo,
213345c763947b657b347211dc9388754e05d30d0467Eric Laurent                                                       fxOutput);
21344660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                        moved.add(desc->mIo);
21354660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                    }
213645c763947b657b347211dc9388754e05d30d0467Eric Laurent                    desc->mIo = fxOutput;
21374660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                }
21384660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen            }
21394660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen        }
21405ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // Move tracks associated to this strategy from previous output to new output
2141f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        for (int i = 0; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) {
2142f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (getStrategy((AudioSystem::stream_type)i) == strategy) {
214312bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi                //FIXME see fixme on name change
21444660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                mpClientInterface->setStreamOutput((AudioSystem::stream_type)i,
21454660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                                                   dstOutputs[0] /* ignored */);
2146f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2147f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2148f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2149f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2150f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2151f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::checkOutputForAllStrategies()
2152f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2153c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
2154f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForStrategy(STRATEGY_PHONE);
2155f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForStrategy(STRATEGY_SONIFICATION);
215612bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
2157f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForStrategy(STRATEGY_MEDIA);
2158f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForStrategy(STRATEGY_DTMF);
2159f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2160f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2161b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurentaudio_io_handle_t AudioPolicyManagerBase::getA2dpOutput()
2162b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent{
21635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (!mHasA2dp) {
2164b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        return 0;
2165b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    }
2166b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
2167b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    for (size_t i = 0; i < mOutputs.size(); i++) {
2168b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        AudioOutputDescriptor *outputDesc = mOutputs.valueAt(i);
2169b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        if (!outputDesc->isDuplicated() && outputDesc->device() & AUDIO_DEVICE_OUT_ALL_A2DP) {
2170b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            return mOutputs.keyAt(i);
2171b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        }
2172b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    }
2173b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
2174b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    return 0;
2175b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent}
2176b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
2177f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::checkA2dpSuspend()
2178f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
21795ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (!mHasA2dp) {
2180b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        return;
2181b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    }
2182b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    audio_io_handle_t a2dpOutput = getA2dpOutput();
2183b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (a2dpOutput == 0) {
2184b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        return;
2185b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    }
2186b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
2187f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // suspend A2DP output if:
2188f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      (NOT already suspended) &&
2189f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      ((SCO device is connected &&
2190f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //       (forced usage for communication || for record is SCO))) ||
2191f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      (phone state is ringing || in call)
2192f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //
2193f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // restore A2DP output if:
2194f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      (Already suspended) &&
2195f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      ((SCO device is NOT connected ||
2196f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //       (forced usage NOT for communication && NOT for record is SCO))) &&
2197f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      (phone state is NOT ringing && NOT in call)
2198f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //
2199f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mA2dpSuspended) {
2200f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (((mScoDeviceAddress == "") ||
2201f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin             ((mForceUse[AudioSystem::FOR_COMMUNICATION] != AudioSystem::FORCE_BT_SCO) &&
2202f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin              (mForceUse[AudioSystem::FOR_RECORD] != AudioSystem::FORCE_BT_SCO))) &&
2203f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin             ((mPhoneState != AudioSystem::MODE_IN_CALL) &&
2204f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin              (mPhoneState != AudioSystem::MODE_RINGTONE))) {
2205f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2206b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            mpClientInterface->restoreOutput(a2dpOutput);
2207f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mA2dpSuspended = false;
2208f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2209f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
2210f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (((mScoDeviceAddress != "") &&
2211f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin             ((mForceUse[AudioSystem::FOR_COMMUNICATION] == AudioSystem::FORCE_BT_SCO) ||
2212f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin              (mForceUse[AudioSystem::FOR_RECORD] == AudioSystem::FORCE_BT_SCO))) ||
2213f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin             ((mPhoneState == AudioSystem::MODE_IN_CALL) ||
2214f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin              (mPhoneState == AudioSystem::MODE_RINGTONE))) {
2215f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2216b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            mpClientInterface->suspendOutput(a2dpOutput);
2217f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mA2dpSuspended = true;
2218f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2219f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2220f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2221f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2222f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurentaudio_devices_t AudioPolicyManagerBase::getNewDevice(audio_io_handle_t output, bool fromCache)
2223f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2224ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    audio_devices_t device = AUDIO_DEVICE_NONE;
2225f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2226f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
2227f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // check the following by order of priority to request a routing change if necessary:
2228c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    // 1: the strategy enforced audible is active on the output:
2229c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    //      use device for strategy enforced audible
2230c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    // 2: we are in call or the strategy phone is active on the output:
2231f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      use device for strategy phone
2232c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    // 3: the strategy sonification is active on the output:
2233f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      use device for strategy sonification
223412bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    // 4: the strategy "respectful" sonification is active on the output:
223512bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    //      use device for strategy "respectful" sonification
223612bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    // 5: the strategy media is active on the output:
2237f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      use device for strategy media
223812bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    // 6: the strategy DTMF is active on the output:
2239f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      use device for strategy DTMF
224080f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    if (outputDesc->isStrategyActive(STRATEGY_ENFORCED_AUDIBLE)) {
2241c16ac09f510437e8340be691720177a490ae78f0Eric Laurent        device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
2242c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    } else if (isInCall() ||
224380f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                    outputDesc->isStrategyActive(STRATEGY_PHONE)) {
2244f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
224580f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    } else if (outputDesc->isStrategyActive(STRATEGY_SONIFICATION)) {
2246f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
224780f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    } else if (outputDesc->isStrategyActive(STRATEGY_SONIFICATION_RESPECTFUL)) {
22485ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
224980f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    } else if (outputDesc->isStrategyActive(STRATEGY_MEDIA)) {
2250f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
225180f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    } else if (outputDesc->isStrategyActive(STRATEGY_DTMF)) {
2252f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
2253f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2254f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
22556a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("getNewDevice() selected device %x", device);
2256f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return device;
2257f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2258f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2259f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinuint32_t AudioPolicyManagerBase::getStrategyForStream(AudioSystem::stream_type stream) {
2260f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return (uint32_t)getStrategy(stream);
2261f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2262f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2263f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurentaudio_devices_t AudioPolicyManagerBase::getDevicesForStream(AudioSystem::stream_type stream) {
2264f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent    audio_devices_t devices;
2265f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // By checking the range of stream before calling getStrategy, we avoid
22665efbd421e0029d6fc44b1cc65c0e5e0d85e5161fSteve Block    // getStrategy's behavior for invalid streams.  getStrategy would do a ALOGE
2267f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // and then return STRATEGY_MEDIA, but we want to return the empty set.
2268f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (stream < (AudioSystem::stream_type) 0 || stream >= AudioSystem::NUM_STREAM_TYPES) {
2269ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        devices = AUDIO_DEVICE_NONE;
2270f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
2271f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioPolicyManagerBase::routing_strategy strategy = getStrategy(stream);
22725ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        devices = getDeviceForStrategy(strategy, true /*fromCache*/);
2273f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2274f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return devices;
2275f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2276f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2277f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima ZavinAudioPolicyManagerBase::routing_strategy AudioPolicyManagerBase::getStrategy(
2278f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioSystem::stream_type stream) {
2279f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // stream to strategy mapping
2280f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    switch (stream) {
2281f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::VOICE_CALL:
2282f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::BLUETOOTH_SCO:
2283f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return STRATEGY_PHONE;
2284f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::RING:
2285f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::ALARM:
2286f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return STRATEGY_SONIFICATION;
228712bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    case AudioSystem::NOTIFICATION:
228812bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        return STRATEGY_SONIFICATION_RESPECTFUL;
2289f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::DTMF:
2290f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return STRATEGY_DTMF;
2291f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    default:
22925efbd421e0029d6fc44b1cc65c0e5e0d85e5161fSteve Block        ALOGE("unknown stream type");
2293f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::SYSTEM:
2294f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // NOTE: SYSTEM stream uses MEDIA strategy because muting music and switching outputs
2295f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // while key clicks are played produces a poor result
2296f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::TTS:
2297f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::MUSIC:
2298f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return STRATEGY_MEDIA;
2299c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    case AudioSystem::ENFORCED_AUDIBLE:
2300c16ac09f510437e8340be691720177a490ae78f0Eric Laurent        return STRATEGY_ENFORCED_AUDIBLE;
2301f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2302f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2303f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
230412bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivivoid AudioPolicyManagerBase::handleNotificationRoutingForStream(AudioSystem::stream_type stream) {
230512bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    switch(stream) {
230612bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    case AudioSystem::MUSIC:
230712bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
2308c952527e6f89d5427881462823514be9d79f13e6Eric Laurent        updateDevicesAndOutputs();
230912bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        break;
231012bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    default:
231112bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        break;
231212bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    }
231312bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi}
231412bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi
23155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentaudio_devices_t AudioPolicyManagerBase::getDeviceForStrategy(routing_strategy strategy,
23165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                             bool fromCache)
2317f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2318ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    uint32_t device = AUDIO_DEVICE_NONE;
2319f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2320f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (fromCache) {
23213cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
23225ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent              strategy, mDeviceForStrategy[strategy]);
2323f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return mDeviceForStrategy[strategy];
2324f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2325f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2326f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    switch (strategy) {
232712bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi
232812bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    case STRATEGY_SONIFICATION_RESPECTFUL:
232912bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        if (isInCall()) {
23305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
2331dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi        } else if (isStreamActiveRemotely(AudioSystem::MUSIC,
2332dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi                SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
2333dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi            // while media is playing on a remote device, use the the sonification behavior.
2334dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi            // Note that we test this usecase before testing if media is playing because
2335dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi            //   the isStreamActive() method only informs about the activity of a stream, not
2336dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi            //   if it's for local playback. Note also that we use the same delay between both tests
2337dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi            device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
233812bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        } else if (isStreamActive(AudioSystem::MUSIC, SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
233912bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi            // while media is playing (or has recently played), use the same device
23405ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/);
234112bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        } else {
234212bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi            // when media is not playing anymore, fall back on the sonification behavior
23435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
234412bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        }
234512bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi
234612bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        break;
234712bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi
2348f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case STRATEGY_DTMF:
2349f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (!isInCall()) {
2350f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // when off call, DTMF strategy follows the same rules as MEDIA strategy
23515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/);
2352f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            break;
2353f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2354f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // when in call, DTMF and PHONE strategies follow the same rules
2355f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // FALL THROUGH
2356f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2357f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case STRATEGY_PHONE:
2358f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // for phone strategy, we first consider the forced use and then the available devices by order
2359f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // of priority
2360f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        switch (mForceUse[AudioSystem::FOR_COMMUNICATION]) {
2361f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        case AudioSystem::FORCE_BT_SCO:
2362f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (!isInCall() || strategy != STRATEGY_DTMF) {
2363c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT;
2364f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (device) break;
2365f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2366c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET;
2367f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (device) break;
2368c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO;
2369f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (device) break;
2370f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // if SCO device is requested but no SCO device is available, fall back to default case
2371f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // FALL THROUGH
2372f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2373f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        default:    // FORCE_NONE
2374f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to A2DP
23751afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent            if (mHasA2dp && !isInCall() &&
23761afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent                    (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
2377ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent                    (getA2dpOutput() != 0) && !mA2dpSuspended) {
2378c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
2379f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (device) break;
2380c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
2381f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (device) break;
2382f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2383c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
23841afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent            if (device) break;
2385c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADSET;
23861afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent            if (device) break;
2387b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent            if (mPhoneState != AudioSystem::MODE_IN_CALL) {
2388b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
2389b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2390b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
2391b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2392b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
2393b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2394b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
2395b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2396b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
2397b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2398b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent            }
2399c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_EARPIECE;
24005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            if (device) break;
24015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = mDefaultOutputDevice;
2402ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            if (device == AUDIO_DEVICE_NONE) {
24035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE");
2404f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2405f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            break;
2406f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2407f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        case AudioSystem::FORCE_SPEAKER:
2408f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to
2409f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // A2DP speaker when forcing to speaker output
24101afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent            if (mHasA2dp && !isInCall() &&
24111afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent                    (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
2412ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent                    (getA2dpOutput() != 0) && !mA2dpSuspended) {
2413c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
2414f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (device) break;
2415f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2416b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent            if (mPhoneState != AudioSystem::MODE_IN_CALL) {
2417b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
2418b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2419b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
2420b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2421b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
2422b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2423b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
2424b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2425b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
2426b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2427b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent            }
2428c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER;
24295ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            if (device) break;
24305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = mDefaultOutputDevice;
2431ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            if (device == AUDIO_DEVICE_NONE) {
24325ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE, FORCE_SPEAKER");
2433f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2434f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            break;
2435f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2436f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    break;
2437f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2438f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case STRATEGY_SONIFICATION:
2439f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2440f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // If incall, just select the STRATEGY_PHONE device: The rest of the behavior is handled by
2441f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // handleIncallSonification().
2442f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (isInCall()) {
24435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = getDeviceForStrategy(STRATEGY_PHONE, false /*fromCache*/);
2444f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            break;
2445f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2446c16ac09f510437e8340be691720177a490ae78f0Eric Laurent        // FALL THROUGH
2447c16ac09f510437e8340be691720177a490ae78f0Eric Laurent
2448c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    case STRATEGY_ENFORCED_AUDIBLE:
2449c16ac09f510437e8340be691720177a490ae78f0Eric Laurent        // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION
2450ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        // except:
2451ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        //   - when in call where it doesn't default to STRATEGY_PHONE behavior
2452ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        //   - in countries where not enforced in which case it follows STRATEGY_MEDIA
2453c16ac09f510437e8340be691720177a490ae78f0Eric Laurent
2454738207def5f691d605ae33d041116829a74513a9Eric Laurent        if ((strategy == STRATEGY_SONIFICATION) ||
2455738207def5f691d605ae33d041116829a74513a9Eric Laurent                (mForceUse[AudioSystem::FOR_SYSTEM] == AudioSystem::FORCE_SYSTEM_ENFORCED)) {
2456c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER;
2457ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            if (device == AUDIO_DEVICE_NONE) {
2458ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent                ALOGE("getDeviceForStrategy() speaker device not found for STRATEGY_SONIFICATION");
2459ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent            }
2460f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2461f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // The second device used for sonification is the same as the device used by media strategy
2462f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // FALL THROUGH
2463f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2464f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case STRATEGY_MEDIA: {
2465ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        uint32_t device2 = AUDIO_DEVICE_NONE;
246631363a9cb94e80330c335fede0b92b1953a09517Jean-Michel Trivi        if (strategy != STRATEGY_SONIFICATION) {
246731363a9cb94e80330c335fede0b92b1953a09517Jean-Michel Trivi            // no sonification on remote submix (e.g. WFD)
246848387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
246948387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi        }
2470ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if ((device2 == AUDIO_DEVICE_NONE) &&
247148387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi                mHasA2dp && (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
2472ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent                (getA2dpOutput() != 0) && !mA2dpSuspended) {
2473c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
2474ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            if (device2 == AUDIO_DEVICE_NONE) {
2475c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
2476f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2477ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            if (device2 == AUDIO_DEVICE_NONE) {
2478c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
2479f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2480f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2481ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (device2 == AUDIO_DEVICE_NONE) {
2482c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
24831afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent        }
2484ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (device2 == AUDIO_DEVICE_NONE) {
2485c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADSET;
24861afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent        }
2487ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (device2 == AUDIO_DEVICE_NONE) {
2488599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
2489599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent        }
2490ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (device2 == AUDIO_DEVICE_NONE) {
2491599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
2492599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent        }
2493ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (device2 == AUDIO_DEVICE_NONE) {
2494c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
2495f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
249631363a9cb94e80330c335fede0b92b1953a09517Jean-Michel Trivi        if ((device2 == AUDIO_DEVICE_NONE) && (strategy != STRATEGY_SONIFICATION)) {
249731363a9cb94e80330c335fede0b92b1953a09517Jean-Michel Trivi            // no sonification on aux digital (e.g. HDMI)
2498c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
2499f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
25005a484b753cc72d6a50c1dd3bbf68b3403c741a3aEric Laurent        if ((device2 == AUDIO_DEVICE_NONE) &&
25015a484b753cc72d6a50c1dd3bbf68b3403c741a3aEric Laurent                (mForceUse[AudioSystem::FOR_DOCK] == AudioSystem::FORCE_ANALOG_DOCK)) {
2502c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
2503f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2504ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (device2 == AUDIO_DEVICE_NONE) {
2505c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER;
2506f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2507f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2508c16ac09f510437e8340be691720177a490ae78f0Eric Laurent        // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or
2509ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise
2510f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device |= device2;
25115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (device) break;
25125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        device = mDefaultOutputDevice;
2513ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (device == AUDIO_DEVICE_NONE) {
25145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGE("getDeviceForStrategy() no device found for STRATEGY_MEDIA");
2515f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2516f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        } break;
2517f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2518f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    default:
251964cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("getDeviceForStrategy() unknown strategy: %d", strategy);
2520f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
2521f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2522f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
25233cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGVV("getDeviceForStrategy() strategy %d, device %x", strategy, device);
2524c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    return device;
2525f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2526f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2527c952527e6f89d5427881462823514be9d79f13e6Eric Laurentvoid AudioPolicyManagerBase::updateDevicesAndOutputs()
2528f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2529f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < NUM_STRATEGIES; i++) {
25305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
25315ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
2532c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    mPreviousOutputs = mOutputs;
25335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
25345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
2535b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurentuint32_t AudioPolicyManagerBase::checkDeviceMuteStrategies(AudioOutputDescriptor *outputDesc,
25369029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                                                       audio_devices_t prevDevice,
25375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                       uint32_t delayMs)
25385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
25399029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    // mute/unmute strategies using an incompatible device combination
25409029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    // if muting, wait for the audio in pcm buffer to be drained before proceeding
25419029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    // if unmuting, unmute only after the specified delay
25425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (outputDesc->isDuplicated()) {
2543b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        return 0;
25445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
25455ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
25465ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    uint32_t muteWaitMs = 0;
25475ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    audio_devices_t device = outputDesc->device();
254880f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    bool shouldMute = outputDesc->isActive() && (AudioSystem::popCount(device) >= 2);
25499029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    // temporary mute output if device selection changes to avoid volume bursts due to
25509029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    // different per device volumes
255180f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    bool tempMute = outputDesc->isActive() && (device != prevDevice);
25525ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
25535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < NUM_STRATEGIES; i++) {
25545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
25555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        bool mute = shouldMute && (curDevice & device) && (curDevice != device);
25565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        bool doMute = false;
25575ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
25585ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (mute && !outputDesc->mStrategyMutedByDevice[i]) {
25595ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            doMute = true;
25605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            outputDesc->mStrategyMutedByDevice[i] = true;
25615ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){
25625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            doMute = true;
25635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            outputDesc->mStrategyMutedByDevice[i] = false;
25645ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
25659029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent        if (doMute || tempMute) {
25665ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            for (size_t j = 0; j < mOutputs.size(); j++) {
25675ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                AudioOutputDescriptor *desc = mOutputs.valueAt(j);
2568f32e38e24db196671d3ea43427125a4e212466faEric Laurent                // skip output if it does not share any device with current output
2569c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                if ((desc->supportedDevices() & outputDesc->supportedDevices())
2570ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent                        == AUDIO_DEVICE_NONE) {
25715ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                    continue;
25725ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                }
25735ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                audio_io_handle_t curOutput = mOutputs.keyAt(j);
25743cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x) on output %d",
25755ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                      mute ? "muting" : "unmuting", i, curDevice, curOutput);
25765ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                setStrategyMute((routing_strategy)i, mute, curOutput, mute ? 0 : delayMs);
257780f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                if (desc->isStrategyActive((routing_strategy)i)) {
2578f32e38e24db196671d3ea43427125a4e212466faEric Laurent                    // do tempMute only for current output
2579f32e38e24db196671d3ea43427125a4e212466faEric Laurent                    if (tempMute && (desc == outputDesc)) {
258001e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                        setStrategyMute((routing_strategy)i, true, curOutput);
258101e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                        setStrategyMute((routing_strategy)i, false, curOutput,
258201e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                            desc->latency() * 2, device);
25839029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                    }
2584f32e38e24db196671d3ea43427125a4e212466faEric Laurent                    if ((tempMute && (desc == outputDesc)) || mute) {
25859029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                        if (muteWaitMs < desc->latency()) {
25869029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                            muteWaitMs = desc->latency();
25879029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                        }
25885ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                    }
25895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                }
25905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
25915ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
25925ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
25935ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
25945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // FIXME: should not need to double latency if volume could be applied immediately by the
25955ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // audioflinger mixer. We must account for the delay between now and the next time
25965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // the audioflinger thread for this output will process a buffer (which corresponds to
25975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // one buffer size, usually 1/2 or 1/4 of the latency).
25985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    muteWaitMs *= 2;
25995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // wait for the PCM output buffers to empty before proceeding with the rest of the command
26005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (muteWaitMs > delayMs) {
2601b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        muteWaitMs -= delayMs;
2602b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        usleep(muteWaitMs * 1000);
2603b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        return muteWaitMs;
2604f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2605b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent    return 0;
2606f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2607f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2608b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurentuint32_t AudioPolicyManagerBase::setOutputDevice(audio_io_handle_t output,
2609f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                                             audio_devices_t device,
2610f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                                             bool force,
2611f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                                             int delayMs)
2612f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
26135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGV("setOutputDevice() output %d device %04x delayMs %d", output, device, delayMs);
2614f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
26155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    AudioParameter param;
2616f32e38e24db196671d3ea43427125a4e212466faEric Laurent    uint32_t muteWaitMs;
2617f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2618f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (outputDesc->isDuplicated()) {
2619b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        muteWaitMs = setOutputDevice(outputDesc->mOutput1->mId, device, force, delayMs);
2620b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        muteWaitMs += setOutputDevice(outputDesc->mOutput2->mId, device, force, delayMs);
2621b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        return muteWaitMs;
2622f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2623f32e38e24db196671d3ea43427125a4e212466faEric Laurent    // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current
2624f32e38e24db196671d3ea43427125a4e212466faEric Laurent    // output profile
2625f32e38e24db196671d3ea43427125a4e212466faEric Laurent    if ((device != AUDIO_DEVICE_NONE) &&
2626f32e38e24db196671d3ea43427125a4e212466faEric Laurent            ((device & outputDesc->mProfile->mSupportedDevices) == 0)) {
2627f32e38e24db196671d3ea43427125a4e212466faEric Laurent        return 0;
2628f32e38e24db196671d3ea43427125a4e212466faEric Laurent    }
2629f32e38e24db196671d3ea43427125a4e212466faEric Laurent
2630f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // filter devices according to output selected
2631f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent    device = (audio_devices_t)(device & outputDesc->mProfile->mSupportedDevices);
2632f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
26335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    audio_devices_t prevDevice = outputDesc->mDevice;
26345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
26355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGV("setOutputDevice() prevDevice %04x", prevDevice);
26365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
2637ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if (device != AUDIO_DEVICE_NONE) {
26385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        outputDesc->mDevice = device;
26395ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
26409029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs);
26415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
2642f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // Do not change the routing if:
2643ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    //  - the requested device is AUDIO_DEVICE_NONE
2644f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //  - the requested device is the same as current device and force is not specified.
2645f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // Doing this check here allows the caller to call setOutputDevice() without conditions
2646ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if ((device == AUDIO_DEVICE_NONE || device == prevDevice) && !force) {
26475ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        ALOGV("setOutputDevice() setting same device %04x or null device for output %d", device, output);
2648b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        return muteWaitMs;
2649f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2650f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
26515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGV("setOutputDevice() changing device");
2652f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // do the routing
2653f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    param.addInt(String8(AudioParameter::keyRouting), (int)device);
26545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    mpClientInterface->setParameters(output, param.toString(), delayMs);
26555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
2656f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // update stream volumes according to new device
2657f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    applyStreamVolumes(output, device, delayMs);
2658b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent
2659b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent    return muteWaitMs;
26605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
2661f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
26625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric LaurentAudioPolicyManagerBase::IOProfile *AudioPolicyManagerBase::getInputProfile(audio_devices_t device,
26635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                   uint32_t samplingRate,
26645ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                   uint32_t format,
26655ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                   uint32_t channelMask)
26665ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
26675ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // Choose an input profile based on the requested capture parameters: select the first available
26685ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // profile supporting all requested parameters.
26695ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
267070c236c9290732782d5267935af1475b8d5ae602Eric Laurent    for (size_t i = 0; i < mHwModules.size(); i++)
26715ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    {
267270c236c9290732782d5267935af1475b8d5ae602Eric Laurent        if (mHwModules[i]->mHandle == 0) {
26735ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            continue;
26745ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
267570c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
26765ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        {
267770c236c9290732782d5267935af1475b8d5ae602Eric Laurent            IOProfile *profile = mHwModules[i]->mInputProfiles[j];
267870c236c9290732782d5267935af1475b8d5ae602Eric Laurent            if (profile->isCompatibleProfile(device, samplingRate, format,
26790977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent                                             channelMask,(audio_output_flags_t)0)) {
268070c236c9290732782d5267935af1475b8d5ae602Eric Laurent                return profile;
26815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
26825ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
2683f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
268470c236c9290732782d5267935af1475b8d5ae602Eric Laurent    return NULL;
2685f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2686f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2687f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurentaudio_devices_t AudioPolicyManagerBase::getDeviceForInputSource(int inputSource)
2688f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2689ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    uint32_t device = AUDIO_DEVICE_NONE;
2690f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
26919641bd36dbf0c4b1eeb84499b35c1cdb7fa5a530Eric Laurent    switch (inputSource) {
26929641bd36dbf0c4b1eeb84499b35c1cdb7fa5a530Eric Laurent    case AUDIO_SOURCE_VOICE_UPLINK:
26939641bd36dbf0c4b1eeb84499b35c1cdb7fa5a530Eric Laurent      if (mAvailableInputDevices & AUDIO_DEVICE_IN_VOICE_CALL) {
26949641bd36dbf0c4b1eeb84499b35c1cdb7fa5a530Eric Laurent          device = AUDIO_DEVICE_IN_VOICE_CALL;
26959641bd36dbf0c4b1eeb84499b35c1cdb7fa5a530Eric Laurent          break;
26969641bd36dbf0c4b1eeb84499b35c1cdb7fa5a530Eric Laurent      }
26979641bd36dbf0c4b1eeb84499b35c1cdb7fa5a530Eric Laurent      // FALL THROUGH
26989641bd36dbf0c4b1eeb84499b35c1cdb7fa5a530Eric Laurent
2699f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_DEFAULT:
2700f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_MIC:
2701f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_RECOGNITION:
2702fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent    case AUDIO_SOURCE_HOTWORD:
2703f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_COMMUNICATION:
2704f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (mForceUse[AudioSystem::FOR_RECORD] == AudioSystem::FORCE_BT_SCO &&
2705c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            mAvailableInputDevices & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
2706c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
2707c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_WIRED_HEADSET) {
2708c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = AUDIO_DEVICE_IN_WIRED_HEADSET;
2709c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_BUILTIN_MIC) {
2710c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = AUDIO_DEVICE_IN_BUILTIN_MIC;
2711f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2712f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
2713f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_CAMCORDER:
2714c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        if (mAvailableInputDevices & AUDIO_DEVICE_IN_BACK_MIC) {
2715c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = AUDIO_DEVICE_IN_BACK_MIC;
2716c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_BUILTIN_MIC) {
2717c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = AUDIO_DEVICE_IN_BUILTIN_MIC;
2718f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2719f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
2720f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_DOWNLINK:
2721f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_CALL:
2722c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        if (mAvailableInputDevices & AUDIO_DEVICE_IN_VOICE_CALL) {
2723c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = AUDIO_DEVICE_IN_VOICE_CALL;
27245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
2725f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
272648387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi    case AUDIO_SOURCE_REMOTE_SUBMIX:
272748387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi        if (mAvailableInputDevices & AUDIO_DEVICE_IN_REMOTE_SUBMIX) {
272848387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
272948387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi        }
273048387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi        break;
2731f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    default:
273264cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("getDeviceForInputSource() invalid input source %d", inputSource);
2733f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
2734f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
27356a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("getDeviceForInputSource()input source %d, device %08x", inputSource, device);
2736c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    return device;
2737f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2738f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
27396d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivibool AudioPolicyManagerBase::isVirtualInputDevice(audio_devices_t device)
27406d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi{
27416d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi    if ((device & AUDIO_DEVICE_BIT_IN) != 0) {
27426d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi        device &= ~AUDIO_DEVICE_BIT_IN;
27436d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi        if ((popcount(device) == 1) && ((device & ~APM_AUDIO_IN_DEVICE_VIRTUAL_ALL) == 0))
27446d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi            return true;
27456d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi    }
27466d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi    return false;
27476d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi}
27486d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi
27496d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Triviaudio_io_handle_t AudioPolicyManagerBase::getActiveInput(bool ignoreVirtualInputs)
2750f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2751f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mInputs.size(); i++) {
27526d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi        const AudioInputDescriptor * input_descriptor = mInputs.valueAt(i);
27536d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi        if ((input_descriptor->mRefCount > 0)
27546d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi                && (!ignoreVirtualInputs || !isVirtualInputDevice(input_descriptor->mDevice))) {
2755f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return mInputs.keyAt(i);
2756f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2757f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2758f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return 0;
2759f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2760f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2761e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2762c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentaudio_devices_t AudioPolicyManagerBase::getDeviceForVolume(audio_devices_t device)
2763e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent{
2764ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if (device == AUDIO_DEVICE_NONE) {
2765e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        // this happens when forcing a route update and no track is active on an output.
2766e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        // In this case the returned category is not important.
2767c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        device =  AUDIO_DEVICE_OUT_SPEAKER;
2768c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    } else if (AudioSystem::popCount(device) > 1) {
2769e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        // Multiple device selection is either:
2770e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        //  - speaker + one other device: give priority to speaker in this case.
2771e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        //  - one A2DP device + another device: happens with duplicated output. In this case
2772e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        // retain the device on the A2DP output as the other must not correspond to an active
2773e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        // selection if not the speaker.
2774c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        if (device & AUDIO_DEVICE_OUT_SPEAKER) {
2775c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            device = AUDIO_DEVICE_OUT_SPEAKER;
2776c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        } else {
2777c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            device = (audio_devices_t)(device & AUDIO_DEVICE_OUT_ALL_A2DP);
2778c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        }
2779e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    }
2780e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
278164cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block    ALOGW_IF(AudioSystem::popCount(device) != 1,
2782c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            "getDeviceForVolume() invalid device combination: %08x",
2783e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            device);
2784e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2785c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    return device;
2786c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent}
2787c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
2788f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric LaurentAudioPolicyManagerBase::device_category AudioPolicyManagerBase::getDeviceCategory(audio_devices_t device)
2789c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent{
2790f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent    switch(getDeviceForVolume(device)) {
2791e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_EARPIECE:
2792e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            return DEVICE_CATEGORY_EARPIECE;
2793e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_WIRED_HEADSET:
2794e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_WIRED_HEADPHONE:
2795e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_BLUETOOTH_SCO:
2796e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET:
2797e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP:
2798e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES:
2799e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            return DEVICE_CATEGORY_HEADSET;
2800e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_SPEAKER:
2801e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT:
2802e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER:
2803c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        case AUDIO_DEVICE_OUT_AUX_DIGITAL:
2804599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent        case AUDIO_DEVICE_OUT_USB_ACCESSORY:
2805599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent        case AUDIO_DEVICE_OUT_USB_DEVICE:
280648387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi        case AUDIO_DEVICE_OUT_REMOTE_SUBMIX:
2807e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        default:
2808e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            return DEVICE_CATEGORY_SPEAKER;
2809e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    }
2810e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent}
2811e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2812f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurentfloat AudioPolicyManagerBase::volIndexToAmpl(audio_devices_t device, const StreamDescriptor& streamDesc,
2813e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        int indexInUi)
2814e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent{
2815e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    device_category deviceCategory = getDeviceCategory(device);
2816e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    const VolumeCurvePoint *curve = streamDesc.mVolumeCurve[deviceCategory];
2817e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2818f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // the volume index in the UI is relative to the min and max volume indices for this stream type
2819e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    int nbSteps = 1 + curve[VOLMAX].mIndex -
2820e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            curve[VOLMIN].mIndex;
2821f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    int volIdx = (nbSteps * (indexInUi - streamDesc.mIndexMin)) /
2822f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            (streamDesc.mIndexMax - streamDesc.mIndexMin);
2823f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2824f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // find what part of the curve this index volume belongs to, or if it's out of bounds
2825f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    int segment = 0;
2826e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    if (volIdx < curve[VOLMIN].mIndex) {         // out of bounds
2827f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return 0.0f;
2828e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    } else if (volIdx < curve[VOLKNEE1].mIndex) {
2829f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        segment = 0;
2830e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    } else if (volIdx < curve[VOLKNEE2].mIndex) {
2831f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        segment = 1;
2832e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    } else if (volIdx <= curve[VOLMAX].mIndex) {
2833f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        segment = 2;
2834f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {                                                               // out of bounds
2835f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return 1.0f;
2836f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2837f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2838f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // linear interpolation in the attenuation table in dB
2839e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    float decibels = curve[segment].mDBAttenuation +
2840e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            ((float)(volIdx - curve[segment].mIndex)) *
2841e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent                ( (curve[segment+1].mDBAttenuation -
2842e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent                        curve[segment].mDBAttenuation) /
2843e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent                    ((float)(curve[segment+1].mIndex -
2844e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent                            curve[segment].mIndex)) );
2845f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2846f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    float amplification = exp( decibels * 0.115129f); // exp( dB * ln(10) / 20 )
2847f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
28483cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGVV("VOLUME vol index=[%d %d %d], dB=[%.1f %.1f %.1f] ampl=%.5f",
2849e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            curve[segment].mIndex, volIdx,
2850e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            curve[segment+1].mIndex,
2851e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            curve[segment].mDBAttenuation,
2852cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent            decibels,
2853e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            curve[segment+1].mDBAttenuation,
2854f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            amplification);
2855f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2856f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return amplification;
2857f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2858f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2859cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
2860e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    AudioPolicyManagerBase::sDefaultVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
2861e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    {1, -49.5f}, {33, -33.5f}, {66, -17.0f}, {100, 0.0f}
2862e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent};
2863e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2864e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
2865e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    AudioPolicyManagerBase::sDefaultMediaVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
2866e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    {1, -58.0f}, {20, -40.0f}, {60, -17.0f}, {100, 0.0f}
2867cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent};
2868cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent
2869e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
2870e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    AudioPolicyManagerBase::sSpeakerMediaVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
2871e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    {1, -56.0f}, {20, -34.0f}, {60, -11.0f}, {100, 0.0f}
2872e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent};
2873e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2874e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
2875e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    AudioPolicyManagerBase::sSpeakerSonificationVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
2876e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    {1, -29.7f}, {33, -20.1f}, {66, -10.2f}, {100, 0.0f}
2877e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent};
2878e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
287918fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Triviconst AudioPolicyManagerBase::VolumeCurvePoint
288018fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi    AudioPolicyManagerBase::sSpeakerSonificationVolumeCurveDrc[AudioPolicyManagerBase::VOLCNT] = {
288118fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi    {1, -35.7f}, {33, -26.1f}, {66, -13.2f}, {100, 0.0f}
288218fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi};
288318fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi
2884ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent// AUDIO_STREAM_SYSTEM, AUDIO_STREAM_ENFORCED_AUDIBLE and AUDIO_STREAM_DTMF volume tracks
288587e28f7b352fb77f46e16ebcdd85cbf01396a203Eric Laurent// AUDIO_STREAM_RING on phones and AUDIO_STREAM_MUSIC on tablets.
288687e28f7b352fb77f46e16ebcdd85cbf01396a203Eric Laurent// AUDIO_STREAM_DTMF tracks AUDIO_STREAM_VOICE_CALL while in call (See AudioService.java).
2887123897874418f9f0e48bb89386d8c470e6975f28Jean-Michel Trivi// The range is constrained between -24dB and -6dB over speaker and -30dB and -18dB over headset.
288887e28f7b352fb77f46e16ebcdd85cbf01396a203Eric Laurent
2889ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
2890ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    AudioPolicyManagerBase::sDefaultSystemVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
2891123897874418f9f0e48bb89386d8c470e6975f28Jean-Michel Trivi    {1, -24.0f}, {33, -18.0f}, {66, -12.0f}, {100, -6.0f}
2892ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent};
2893ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent
2894ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
289518fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi    AudioPolicyManagerBase::sDefaultSystemVolumeCurveDrc[AudioPolicyManagerBase::VOLCNT] = {
289618fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi    {1, -34.0f}, {33, -24.0f}, {66, -15.0f}, {100, -6.0f}
289718fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi};
289818fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi
289918fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Triviconst AudioPolicyManagerBase::VolumeCurvePoint
2900ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    AudioPolicyManagerBase::sHeadsetSystemVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
29017465678e0d5711ebcd78ae47b3a76821534a23eaEric Laurent    {1, -30.0f}, {33, -26.0f}, {66, -22.0f}, {100, -18.0f}
2902ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent};
2903e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2904e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
29050d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent    AudioPolicyManagerBase::sDefaultVoiceVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
29060d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent    {0, -42.0f}, {33, -28.0f}, {66, -14.0f}, {100, 0.0f}
29070d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent};
29080d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent
29090d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
29100d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent    AudioPolicyManagerBase::sSpeakerVoiceVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
29110d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent    {0, -24.0f}, {33, -16.0f}, {66, -8.0f}, {100, 0.0f}
29120d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent};
29130d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent
29140d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
2915ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent            *AudioPolicyManagerBase::sVolumeProfiles[AUDIO_STREAM_CNT]
2916e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent                                                   [AudioPolicyManagerBase::DEVICE_CATEGORY_CNT] = {
2917ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_VOICE_CALL
29180d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent        sDefaultVoiceVolumeCurve, // DEVICE_CATEGORY_HEADSET
29190d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent        sSpeakerVoiceVolumeCurve, // DEVICE_CATEGORY_SPEAKER
29200d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent        sDefaultVoiceVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2921e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    },
2922ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_SYSTEM
2923ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET
2924ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2925ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultSystemVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2926ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    },
2927ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_RING
2928e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
2929e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2930e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sDefaultVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2931e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    },
2932ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_MUSIC
2933ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultMediaVolumeCurve, // DEVICE_CATEGORY_HEADSET
2934ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sSpeakerMediaVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2935ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultMediaVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2936ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    },
2937ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_ALARM
293812bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
293912bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER
294012bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        sDefaultVolumeCurve  // DEVICE_CATEGORY_EARPIECE
294112bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    },
2942ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_NOTIFICATION
2943e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
2944ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2945e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sDefaultVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2946c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    },
2947ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_BLUETOOTH_SCO
29480d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent        sDefaultVoiceVolumeCurve, // DEVICE_CATEGORY_HEADSET
29490d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent        sSpeakerVoiceVolumeCurve, // DEVICE_CATEGORY_SPEAKER
29500d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent        sDefaultVoiceVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2951c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    },
2952ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_ENFORCED_AUDIBLE
2953ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET
2954ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2955ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultSystemVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2956ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    },
2957ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    {  // AUDIO_STREAM_DTMF
2958ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET
2959ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2960ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultSystemVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2961ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    },
2962ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_TTS
2963ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultMediaVolumeCurve, // DEVICE_CATEGORY_HEADSET
2964ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sSpeakerMediaVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2965ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultMediaVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2966ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    },
2967e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent};
2968e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2969e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurentvoid AudioPolicyManagerBase::initializeVolumeCurves()
2970e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent{
2971ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    for (int i = 0; i < AUDIO_STREAM_CNT; i++) {
2972e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) {
2973cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent            mStreams[i].mVolumeCurve[j] =
2974ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent                    sVolumeProfiles[i][j];
2975cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent        }
2976cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent    }
297718fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi
297818fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi    // Check availability of DRC on speaker path: if available, override some of the speaker curves
297918fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi    if (mSpeakerDrcEnabled) {
298018fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi        mStreams[AUDIO_STREAM_SYSTEM].mVolumeCurve[DEVICE_CATEGORY_SPEAKER] =
298118fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi                sDefaultSystemVolumeCurveDrc;
298218fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi        mStreams[AUDIO_STREAM_RING].mVolumeCurve[DEVICE_CATEGORY_SPEAKER] =
298318fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi                sSpeakerSonificationVolumeCurveDrc;
298418fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi        mStreams[AUDIO_STREAM_ALARM].mVolumeCurve[DEVICE_CATEGORY_SPEAKER] =
298518fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi                sSpeakerSonificationVolumeCurveDrc;
298618fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi        mStreams[AUDIO_STREAM_NOTIFICATION].mVolumeCurve[DEVICE_CATEGORY_SPEAKER] =
298718fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi                sSpeakerSonificationVolumeCurveDrc;
298818fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi    }
2989f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2990f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2991c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentfloat AudioPolicyManagerBase::computeVolume(int stream,
2992c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                            int index,
2993c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                            audio_io_handle_t output,
2994f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                                            audio_devices_t device)
2995f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2996f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    float volume = 1.0;
2997f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
2998f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    StreamDescriptor &streamDesc = mStreams[stream];
2999f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3000ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if (device == AUDIO_DEVICE_NONE) {
3001f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device = outputDesc->device();
3002f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3003f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3004f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if volume is not 0 (not muted), force media volume to max on digital output
3005f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (stream == AudioSystem::MUSIC &&
3006f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        index != mStreams[stream].mIndexMin &&
3007f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent        (device == AUDIO_DEVICE_OUT_AUX_DIGITAL ||
3008599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent         device == AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET ||
3009599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent         device == AUDIO_DEVICE_OUT_USB_ACCESSORY ||
3010599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent         device == AUDIO_DEVICE_OUT_USB_DEVICE)) {
3011f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return 1.0;
3012f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3013f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3014f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    volume = volIndexToAmpl(device, streamDesc, index);
3015f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3016f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if a headset is connected, apply the following rules to ring tones and notifications
3017f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // to avoid sound level bursts in user's ears:
3018f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // - always attenuate ring tones and notifications volume by 6dB
3019f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // - if music is playing, always limit the volume to current music volume,
3020f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // with a minimum threshold at -36dB so that notification is always perceived.
302112bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    const routing_strategy stream_strategy = getStrategy((AudioSystem::stream_type)stream);
3022c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
3023c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
3024c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            AUDIO_DEVICE_OUT_WIRED_HEADSET |
3025c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) &&
302612bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        ((stream_strategy == STRATEGY_SONIFICATION)
302712bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi                || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL)
3028738207def5f691d605ae33d041116829a74513a9Eric Laurent                || (stream == AudioSystem::SYSTEM)
3029738207def5f691d605ae33d041116829a74513a9Eric Laurent                || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) &&
3030738207def5f691d605ae33d041116829a74513a9Eric Laurent                    (mForceUse[AudioSystem::FOR_SYSTEM] == AudioSystem::FORCE_NONE))) &&
3031f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        streamDesc.mCanBeMuted) {
3032f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        volume *= SONIFICATION_HEADSET_VOLUME_FACTOR;
3033f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // when the phone is ringing we must consider that music could have been paused just before
3034f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // by the music application and behave as if music was active if the last music track was
3035f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // just stopped
3036ac3cf10ef6de12e69540a1244ac7255f93fa7502Eric Laurent        if (isStreamActive(AudioSystem::MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) ||
3037ac3cf10ef6de12e69540a1244ac7255f93fa7502Eric Laurent                mLimitRingtoneVolume) {
303817a73c3394547692457299dc512b5c2312ea0344Eric Laurent            audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/);
3039c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            float musicVol = computeVolume(AudioSystem::MUSIC,
304017a73c3394547692457299dc512b5c2312ea0344Eric Laurent                               mStreams[AudioSystem::MUSIC].getVolumeIndex(musicDevice),
3041c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                               output,
304217a73c3394547692457299dc512b5c2312ea0344Eric Laurent                               musicDevice);
3043c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            float minVol = (musicVol > SONIFICATION_HEADSET_VOLUME_MIN) ?
3044c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                musicVol : SONIFICATION_HEADSET_VOLUME_MIN;
3045f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (volume > minVol) {
3046f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                volume = minVol;
30476a70518b93928d1c91457ff805e375c82d76b0e5Steve Block                ALOGV("computeVolume limiting volume to %f musicVol %f", minVol, musicVol);
3048f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
3049f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
3050f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3051f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3052f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return volume;
3053f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3054f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3055c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentstatus_t AudioPolicyManagerBase::checkAndSetVolume(int stream,
3056c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                   int index,
3057c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                   audio_io_handle_t output,
3058f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                                                   audio_devices_t device,
3059c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                   int delayMs,
3060c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                   bool force)
3061f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3062f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3063f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // do not change actual stream volume if the stream is muted
3064f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mOutputs.valueFor(output)->mMuteCount[stream] != 0) {
30653cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        ALOGVV("checkAndSetVolume() stream %d muted count %d",
30665ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent              stream, mOutputs.valueFor(output)->mMuteCount[stream]);
3067f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return NO_ERROR;
3068f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3069f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3070f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // do not change in call volume if bluetooth is connected and vice versa
3071f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if ((stream == AudioSystem::VOICE_CALL && mForceUse[AudioSystem::FOR_COMMUNICATION] == AudioSystem::FORCE_BT_SCO) ||
3072f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        (stream == AudioSystem::BLUETOOTH_SCO && mForceUse[AudioSystem::FOR_COMMUNICATION] != AudioSystem::FORCE_BT_SCO)) {
30736a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
3074f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin             stream, mForceUse[AudioSystem::FOR_COMMUNICATION]);
3075f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return INVALID_OPERATION;
3076f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3077f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3078f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    float volume = computeVolume(stream, index, output, device);
3079f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // We actually change the volume if:
3080f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // - the float value returned by computeVolume() changed
3081f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // - the force flag is set
3082f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (volume != mOutputs.valueFor(output)->mCurVolume[stream] ||
3083f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            force) {
3084f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mOutputs.valueFor(output)->mCurVolume[stream] = volume;
30853cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        ALOGVV("checkAndSetVolume() for output %d stream %d, volume %f, delay %d", output, stream, volume, delayMs);
30860d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent        // Force VOICE_CALL to track BLUETOOTH_SCO stream volume when bluetooth audio is
30870d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent        // enabled
30880d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent        if (stream == AudioSystem::BLUETOOTH_SCO) {
30890d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent            mpClientInterface->setStreamVolume(AudioSystem::VOICE_CALL, volume, output, delayMs);
3090f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
3091f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mpClientInterface->setStreamVolume((AudioSystem::stream_type)stream, volume, output, delayMs);
3092f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3093f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3094f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (stream == AudioSystem::VOICE_CALL ||
3095f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        stream == AudioSystem::BLUETOOTH_SCO) {
3096f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        float voiceVolume;
3097f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // Force voice volume to max for bluetooth SCO as volume is managed by the headset
3098f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (stream == AudioSystem::VOICE_CALL) {
3099f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            voiceVolume = (float)index/(float)mStreams[stream].mIndexMax;
3100f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        } else {
3101f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            voiceVolume = 1.0;
3102f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
3103f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3104b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        if (voiceVolume != mLastVoiceVolume && output == mPrimaryOutput) {
3105f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
3106f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mLastVoiceVolume = voiceVolume;
3107f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
3108f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3109f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3110f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
3111f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3112f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3113c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentvoid AudioPolicyManagerBase::applyStreamVolumes(audio_io_handle_t output,
3114f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                                                audio_devices_t device,
3115c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                int delayMs,
3116c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                bool force)
3117f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
31183cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGVV("applyStreamVolumes() for output %d and device %x", output, device);
3119f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3120f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
3121c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        checkAndSetVolume(stream,
3122f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                          mStreams[stream].getVolumeIndex(device),
3123c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                          output,
3124c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                          device,
3125c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                          delayMs,
3126c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                          force);
3127f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3128f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3129f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
313001e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurentvoid AudioPolicyManagerBase::setStrategyMute(routing_strategy strategy,
313101e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                             bool on,
313201e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                             audio_io_handle_t output,
313301e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                             int delayMs,
313401e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                             audio_devices_t device)
3135f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
31363cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGVV("setStrategyMute() strategy %d, mute %d, output %d", strategy, on, output);
3137f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
3138f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (getStrategy((AudioSystem::stream_type)stream) == strategy) {
313901e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent            setStreamMute(stream, on, output, delayMs, device);
3140f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
3141f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3142f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3143f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
314401e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurentvoid AudioPolicyManagerBase::setStreamMute(int stream,
314501e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                           bool on,
314601e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                           audio_io_handle_t output,
314701e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                           int delayMs,
314801e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                           audio_devices_t device)
3149f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3150f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    StreamDescriptor &streamDesc = mStreams[stream];
3151f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
3152ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if (device == AUDIO_DEVICE_NONE) {
315301e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent        device = outputDesc->device();
315401e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent    }
3155f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
31563cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGVV("setStreamMute() stream %d, mute %d, output %d, mMuteCount %d device %04x",
315701e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent          stream, on, output, outputDesc->mMuteCount[stream], device);
3158f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3159f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (on) {
3160f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (outputDesc->mMuteCount[stream] == 0) {
3161738207def5f691d605ae33d041116829a74513a9Eric Laurent            if (streamDesc.mCanBeMuted &&
3162738207def5f691d605ae33d041116829a74513a9Eric Laurent                    ((stream != AudioSystem::ENFORCED_AUDIBLE) ||
3163738207def5f691d605ae33d041116829a74513a9Eric Laurent                     (mForceUse[AudioSystem::FOR_SYSTEM] == AudioSystem::FORCE_NONE))) {
3164c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                checkAndSetVolume(stream, 0, output, device, delayMs);
3165f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
3166f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
3167f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
3168f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mMuteCount[stream]++;
3169f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
3170f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (outputDesc->mMuteCount[stream] == 0) {
31713cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            ALOGV("setStreamMute() unmuting non muted stream!");
3172f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return;
3173f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
3174f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (--outputDesc->mMuteCount[stream] == 0) {
3175c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            checkAndSetVolume(stream,
3176c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                              streamDesc.getVolumeIndex(device),
3177c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                              output,
3178c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                              device,
3179c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                              delayMs);
3180f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
3181f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3182f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3183f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3184f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::handleIncallSonification(int stream, bool starting, bool stateChange)
3185f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3186f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if the stream pertains to sonification strategy and we are in call we must
3187f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // mute the stream if it is low visibility. If it is high visibility, we must play a tone
3188f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // in the device used for phone strategy and play the tone if the selected device does not
3189f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // interfere with the device used for phone strategy
3190f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
3191f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // many times as there are active tracks on the output
319212bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    const routing_strategy stream_strategy = getStrategy((AudioSystem::stream_type)stream);
319312bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    if ((stream_strategy == STRATEGY_SONIFICATION) ||
319412bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi            ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
3195b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        AudioOutputDescriptor *outputDesc = mOutputs.valueFor(mPrimaryOutput);
31966a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
3197f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                stream, starting, outputDesc->mDevice, stateChange);
3198f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (outputDesc->mRefCount[stream]) {
3199f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            int muteCount = 1;
3200f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (stateChange) {
3201f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                muteCount = outputDesc->mRefCount[stream];
3202f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
3203f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (AudioSystem::isLowVisibility((AudioSystem::stream_type)stream)) {
32046a70518b93928d1c91457ff805e375c82d76b0e5Steve Block                ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
3205f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                for (int i = 0; i < muteCount; i++) {
3206b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                    setStreamMute(stream, starting, mPrimaryOutput);
3207f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
3208f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            } else {
32096a70518b93928d1c91457ff805e375c82d76b0e5Steve Block                ALOGV("handleIncallSonification() high visibility");
32105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                if (outputDesc->device() &
32115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                        getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
32126a70518b93928d1c91457ff805e375c82d76b0e5Steve Block                    ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
3213f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    for (int i = 0; i < muteCount; i++) {
3214b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                        setStreamMute(stream, starting, mPrimaryOutput);
3215f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    }
3216f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
3217f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (starting) {
3218f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    mpClientInterface->startTone(ToneGenerator::TONE_SUP_CALL_WAITING, AudioSystem::VOICE_CALL);
3219f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else {
3220f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    mpClientInterface->stopTone();
3221f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
3222f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
3223f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
3224f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3225f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3226f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3227f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinbool AudioPolicyManagerBase::isInCall()
3228f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3229f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return isStateInCall(mPhoneState);
3230f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3231f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3232f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinbool AudioPolicyManagerBase::isStateInCall(int state) {
3233f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return ((state == AudioSystem::MODE_IN_CALL) ||
3234f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            (state == AudioSystem::MODE_IN_COMMUNICATION));
3235f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3236f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3237f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinuint32_t AudioPolicyManagerBase::getMaxEffectsCpuLoad()
3238f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3239f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return MAX_EFFECTS_CPU_LOAD;
3240f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3241f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3242f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinuint32_t AudioPolicyManagerBase::getMaxEffectsMemory()
3243f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3244f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return MAX_EFFECTS_MEMORY;
3245f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3246f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3247f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// --- AudioOutputDescriptor class implementation
3248f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3249b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric LaurentAudioPolicyManagerBase::AudioOutputDescriptor::AudioOutputDescriptor(
32505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        const IOProfile *profile)
325170c236c9290732782d5267935af1475b8d5ae602Eric Laurent    : mId(0), mSamplingRate(0), mFormat((audio_format_t)0),
325270c236c9290732782d5267935af1475b8d5ae602Eric Laurent      mChannelMask((audio_channel_mask_t)0), mLatency(0),
3253ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    mFlags((audio_output_flags_t)0), mDevice(AUDIO_DEVICE_NONE),
32545a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent    mOutput1(0), mOutput2(0), mProfile(profile), mDirectOpenCount(0)
3255f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3256f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // clear usage count for all stream types
3257f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) {
3258f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mRefCount[i] = 0;
3259f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mCurVolume[i] = -1.0;
3260f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mMuteCount[i] = 0;
3261f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mStopTime[i] = 0;
3262f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
326385ad78f27ca032e90af0f2100659d12c16322c76Marco Nelissen    for (int i = 0; i < NUM_STRATEGIES; i++) {
326485ad78f27ca032e90af0f2100659d12c16322c76Marco Nelissen        mStrategyMutedByDevice[i] = false;
326585ad78f27ca032e90af0f2100659d12c16322c76Marco Nelissen    }
32663cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    if (profile != NULL) {
32673cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        mSamplingRate = profile->mSamplingRates[0];
32683cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        mFormat = profile->mFormats[0];
32693cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        mChannelMask = profile->mChannelMasks[0];
32703cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        mFlags = profile->mFlags;
32713cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    }
3272f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3273f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3274dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Triviaudio_devices_t AudioPolicyManagerBase::AudioOutputDescriptor::device() const
3275f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3276f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (isDuplicated()) {
3277f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent        return (audio_devices_t)(mOutput1->mDevice | mOutput2->mDevice);
3278f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
3279f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent        return mDevice;
3280f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3281f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3282f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
32835ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentuint32_t AudioPolicyManagerBase::AudioOutputDescriptor::latency()
32845ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
32855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (isDuplicated()) {
32865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return (mOutput1->mLatency > mOutput2->mLatency) ? mOutput1->mLatency : mOutput2->mLatency;
32875ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    } else {
32885ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return mLatency;
32895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
32905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
32915ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
32925ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentbool AudioPolicyManagerBase::AudioOutputDescriptor::sharesHwModuleWith(
32935ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        const AudioOutputDescriptor *outputDesc)
32945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
32955ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (isDuplicated()) {
32965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return mOutput1->sharesHwModuleWith(outputDesc) || mOutput2->sharesHwModuleWith(outputDesc);
32975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    } else if (outputDesc->isDuplicated()){
32985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return sharesHwModuleWith(outputDesc->mOutput1) || sharesHwModuleWith(outputDesc->mOutput2);
32995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    } else {
330070c236c9290732782d5267935af1475b8d5ae602Eric Laurent        return (mProfile->mModule == outputDesc->mProfile->mModule);
33015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
33025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
33035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
3304f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::AudioOutputDescriptor::changeRefCount(AudioSystem::stream_type stream, int delta)
3305f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3306f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // forward usage count change to attached outputs
3307f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (isDuplicated()) {
3308f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mOutput1->changeRefCount(stream, delta);
3309f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mOutput2->changeRefCount(stream, delta);
3310f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3311f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if ((delta + (int)mRefCount[stream]) < 0) {
331264cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("changeRefCount() invalid delta %d for stream %d, refCount %d", delta, stream, mRefCount[stream]);
3313f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mRefCount[stream] = 0;
3314f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
3315f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3316f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mRefCount[stream] += delta;
33176a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("changeRefCount() stream %d, count %d", stream, mRefCount[stream]);
3318f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3319f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3320f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurentaudio_devices_t AudioPolicyManagerBase::AudioOutputDescriptor::supportedDevices()
3321b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent{
3322b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (isDuplicated()) {
3323f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent        return (audio_devices_t)(mOutput1->supportedDevices() | mOutput2->supportedDevices());
3324b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    } else {
3325b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        return mProfile->mSupportedDevices ;
3326b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    }
3327b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent}
3328b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
332942fa8215a763822c0d03fc936e4cac1eb864c9ccEric Laurentbool AudioPolicyManagerBase::AudioOutputDescriptor::isActive(uint32_t inPastMs) const
333042fa8215a763822c0d03fc936e4cac1eb864c9ccEric Laurent{
333180f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    return isStrategyActive(NUM_STRATEGIES, inPastMs);
333280f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent}
333380f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent
333480f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurentbool AudioPolicyManagerBase::AudioOutputDescriptor::isStrategyActive(routing_strategy strategy,
333580f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                                                                       uint32_t inPastMs,
333680f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                                                                       nsecs_t sysTime) const
333780f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent{
333880f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    if ((sysTime == 0) && (inPastMs != 0)) {
333980f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent        sysTime = systemTime();
334080f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    }
334142fa8215a763822c0d03fc936e4cac1eb864c9ccEric Laurent    for (int i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) {
334280f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent        if (((getStrategy((AudioSystem::stream_type)i) == strategy) ||
334380f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                (NUM_STRATEGIES == strategy)) &&
334480f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                isStreamActive((AudioSystem::stream_type)i, inPastMs, sysTime)) {
334542fa8215a763822c0d03fc936e4cac1eb864c9ccEric Laurent            return true;
334642fa8215a763822c0d03fc936e4cac1eb864c9ccEric Laurent        }
334742fa8215a763822c0d03fc936e4cac1eb864c9ccEric Laurent    }
334842fa8215a763822c0d03fc936e4cac1eb864c9ccEric Laurent    return false;
334942fa8215a763822c0d03fc936e4cac1eb864c9ccEric Laurent}
335042fa8215a763822c0d03fc936e4cac1eb864c9ccEric Laurent
335180f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurentbool AudioPolicyManagerBase::AudioOutputDescriptor::isStreamActive(AudioSystem::stream_type stream,
335280f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                                                                       uint32_t inPastMs,
335380f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                                                                       nsecs_t sysTime) const
335480f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent{
335580f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    if (mRefCount[stream] != 0) {
335680f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent        return true;
335780f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    }
335880f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    if (inPastMs == 0) {
335980f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent        return false;
336080f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    }
336180f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    if (sysTime == 0) {
336280f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent        sysTime = systemTime();
336380f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    }
336480f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    if (ns2ms(sysTime - mStopTime[stream]) < inPastMs) {
336580f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent        return true;
336680f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    }
336780f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    return false;
336880f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent}
336980f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent
337080f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent
3371f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::AudioOutputDescriptor::dump(int fd)
3372f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3373f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    const size_t SIZE = 256;
3374f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    char buffer[SIZE];
3375f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    String8 result;
3376f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3377f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Sampling rate: %d\n", mSamplingRate);
3378f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3379a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    snprintf(buffer, SIZE, " Format: %08x\n", mFormat);
3380f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
338170c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, " Channels: %08x\n", mChannelMask);
3382f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3383f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Latency: %d\n", mLatency);
3384f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3385f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Flags %08x\n", mFlags);
3386f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3387f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Devices %08x\n", device());
3388f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3389f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Stream volume refCount muteCount\n");
3390f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3391f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) {
3392f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        snprintf(buffer, SIZE, " %02d     %.03f     %02d       %02d\n", i, mCurVolume[i], mRefCount[i], mMuteCount[i]);
3393f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        result.append(buffer);
3394f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3395f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, result.string(), result.size());
3396f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3397f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
3398f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3399f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3400f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// --- AudioInputDescriptor class implementation
3401f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
34025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric LaurentAudioPolicyManagerBase::AudioInputDescriptor::AudioInputDescriptor(const IOProfile *profile)
340370c236c9290732782d5267935af1475b8d5ae602Eric Laurent    : mSamplingRate(0), mFormat((audio_format_t)0), mChannelMask((audio_channel_mask_t)0),
3404ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent      mDevice(AUDIO_DEVICE_NONE), mRefCount(0),
34055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent      mInputSource(0), mProfile(profile)
3406f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3407f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3408f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3409f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::AudioInputDescriptor::dump(int fd)
3410f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3411f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    const size_t SIZE = 256;
3412f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    char buffer[SIZE];
3413f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    String8 result;
3414f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3415f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Sampling rate: %d\n", mSamplingRate);
3416f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3417f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Format: %d\n", mFormat);
3418f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
341970c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, " Channels: %08x\n", mChannelMask);
3420f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3421f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Devices %08x\n", mDevice);
3422f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3423f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Ref Count %d\n", mRefCount);
3424f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3425f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, result.string(), result.size());
3426f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3427f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
3428f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3429f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3430f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// --- StreamDescriptor class implementation
3431f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3432c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric LaurentAudioPolicyManagerBase::StreamDescriptor::StreamDescriptor()
3433c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    :   mIndexMin(0), mIndexMax(1), mCanBeMuted(true)
3434c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent{
3435c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    mIndexCur.add(AUDIO_DEVICE_OUT_DEFAULT, 0);
3436c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent}
3437c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
3438c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentint AudioPolicyManagerBase::StreamDescriptor::getVolumeIndex(audio_devices_t device)
3439c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent{
3440c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    device = AudioPolicyManagerBase::getDeviceForVolume(device);
3441c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    // there is always a valid entry for AUDIO_DEVICE_OUT_DEFAULT
3442c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    if (mIndexCur.indexOfKey(device) < 0) {
3443c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        device = AUDIO_DEVICE_OUT_DEFAULT;
3444c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    }
3445c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    return mIndexCur.valueFor(device);
3446c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent}
3447c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
3448c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentvoid AudioPolicyManagerBase::StreamDescriptor::dump(int fd)
3449f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3450c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    const size_t SIZE = 256;
3451c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    char buffer[SIZE];
3452c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    String8 result;
3453c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
3454c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    snprintf(buffer, SIZE, "%s         %02d         %02d         ",
3455c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent             mCanBeMuted ? "true " : "false", mIndexMin, mIndexMax);
3456c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    result.append(buffer);
3457c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    for (size_t i = 0; i < mIndexCur.size(); i++) {
3458c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        snprintf(buffer, SIZE, "%04x : %02d, ",
3459c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                 mIndexCur.keyAt(i),
3460c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                 mIndexCur.valueAt(i));
3461c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        result.append(buffer);
3462c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    }
3463c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    result.append("\n");
3464c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
3465c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    write(fd, result.string(), result.size());
3466f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3467f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3468f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// --- EffectDescriptor class implementation
3469f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3470f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::EffectDescriptor::dump(int fd)
3471f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3472f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    const size_t SIZE = 256;
3473f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    char buffer[SIZE];
3474f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    String8 result;
3475f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
34761c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent    snprintf(buffer, SIZE, " I/O: %d\n", mIo);
3477f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3478f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Strategy: %d\n", mStrategy);
3479f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3480f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Session: %d\n", mSession);
3481f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3482f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Name: %s\n",  mDesc.name);
3483f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3484582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    snprintf(buffer, SIZE, " %s\n",  mEnabled ? "Enabled" : "Disabled");
3485582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    result.append(buffer);
3486f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, result.string(), result.size());
3487f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3488f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
3489f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3490f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
34915ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent// --- IOProfile class implementation
34925ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
349370c236c9290732782d5267935af1475b8d5ae602Eric LaurentAudioPolicyManagerBase::HwModule::HwModule(const char *name)
349470c236c9290732782d5267935af1475b8d5ae602Eric Laurent    : mName(strndup(name, AUDIO_HARDWARE_MODULE_ID_MAX_LEN)), mHandle(0)
349570c236c9290732782d5267935af1475b8d5ae602Eric Laurent{
349670c236c9290732782d5267935af1475b8d5ae602Eric Laurent}
349770c236c9290732782d5267935af1475b8d5ae602Eric Laurent
349870c236c9290732782d5267935af1475b8d5ae602Eric LaurentAudioPolicyManagerBase::HwModule::~HwModule()
349970c236c9290732782d5267935af1475b8d5ae602Eric Laurent{
350070c236c9290732782d5267935af1475b8d5ae602Eric Laurent    for (size_t i = 0; i < mOutputProfiles.size(); i++) {
350170c236c9290732782d5267935af1475b8d5ae602Eric Laurent         delete mOutputProfiles[i];
350270c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
350370c236c9290732782d5267935af1475b8d5ae602Eric Laurent    for (size_t i = 0; i < mInputProfiles.size(); i++) {
350470c236c9290732782d5267935af1475b8d5ae602Eric Laurent         delete mInputProfiles[i];
350570c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
350670c236c9290732782d5267935af1475b8d5ae602Eric Laurent    free((void *)mName);
350770c236c9290732782d5267935af1475b8d5ae602Eric Laurent}
350870c236c9290732782d5267935af1475b8d5ae602Eric Laurent
350970c236c9290732782d5267935af1475b8d5ae602Eric Laurentvoid AudioPolicyManagerBase::HwModule::dump(int fd)
351070c236c9290732782d5267935af1475b8d5ae602Eric Laurent{
351170c236c9290732782d5267935af1475b8d5ae602Eric Laurent    const size_t SIZE = 256;
351270c236c9290732782d5267935af1475b8d5ae602Eric Laurent    char buffer[SIZE];
351370c236c9290732782d5267935af1475b8d5ae602Eric Laurent    String8 result;
351470c236c9290732782d5267935af1475b8d5ae602Eric Laurent
351570c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "  - name: %s\n", mName);
351670c236c9290732782d5267935af1475b8d5ae602Eric Laurent    result.append(buffer);
351770c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "  - handle: %d\n", mHandle);
351870c236c9290732782d5267935af1475b8d5ae602Eric Laurent    result.append(buffer);
351970c236c9290732782d5267935af1475b8d5ae602Eric Laurent    write(fd, result.string(), result.size());
352070c236c9290732782d5267935af1475b8d5ae602Eric Laurent    if (mOutputProfiles.size()) {
352170c236c9290732782d5267935af1475b8d5ae602Eric Laurent        write(fd, "  - outputs:\n", sizeof("  - outputs:\n"));
352270c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (size_t i = 0; i < mOutputProfiles.size(); i++) {
3523599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            snprintf(buffer, SIZE, "    output %d:\n", i);
3524599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            write(fd, buffer, strlen(buffer));
352570c236c9290732782d5267935af1475b8d5ae602Eric Laurent            mOutputProfiles[i]->dump(fd);
352670c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
352770c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
352870c236c9290732782d5267935af1475b8d5ae602Eric Laurent    if (mInputProfiles.size()) {
352970c236c9290732782d5267935af1475b8d5ae602Eric Laurent        write(fd, "  - inputs:\n", sizeof("  - inputs:\n"));
353070c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (size_t i = 0; i < mInputProfiles.size(); i++) {
3531599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            snprintf(buffer, SIZE, "    input %d:\n", i);
3532599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            write(fd, buffer, strlen(buffer));
353370c236c9290732782d5267935af1475b8d5ae602Eric Laurent            mInputProfiles[i]->dump(fd);
353470c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
353570c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
353670c236c9290732782d5267935af1475b8d5ae602Eric Laurent}
353770c236c9290732782d5267935af1475b8d5ae602Eric Laurent
353870c236c9290732782d5267935af1475b8d5ae602Eric LaurentAudioPolicyManagerBase::IOProfile::IOProfile(HwModule *module)
35390977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent    : mFlags((audio_output_flags_t)0), mModule(module)
35405ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
35415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
35425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
35435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric LaurentAudioPolicyManagerBase::IOProfile::~IOProfile()
35445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
354570c236c9290732782d5267935af1475b8d5ae602Eric Laurent}
354670c236c9290732782d5267935af1475b8d5ae602Eric Laurent
3547b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent// checks if the IO profile is compatible with specified parameters.
3548b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent// Sampling rate, format and channel mask must be specified in order to
3549b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent// get a valid a match
355070c236c9290732782d5267935af1475b8d5ae602Eric Laurentbool AudioPolicyManagerBase::IOProfile::isCompatibleProfile(audio_devices_t device,
355170c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                            uint32_t samplingRate,
355270c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                            uint32_t format,
355370c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                            uint32_t channelMask,
35540977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent                                                            audio_output_flags_t flags) const
355570c236c9290732782d5267935af1475b8d5ae602Eric Laurent{
3556b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent    if (samplingRate == 0 || format == 0 || channelMask == 0) {
3557b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent         return false;
3558b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     }
3559b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent
3560b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     if ((mSupportedDevices & device) != device) {
3561b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent         return false;
3562b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     }
3563b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     if ((mFlags & flags) != flags) {
3564b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent         return false;
3565b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     }
3566b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     size_t i;
3567b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     for (i = 0; i < mSamplingRates.size(); i++)
3568b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     {
3569b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent         if (mSamplingRates[i] == samplingRate) {
3570b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent             break;
3571b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent         }
3572b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     }
3573b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     if (i == mSamplingRates.size()) {
3574b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent         return false;
3575b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     }
3576b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     for (i = 0; i < mFormats.size(); i++)
3577b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     {
3578b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent         if (mFormats[i] == format) {
3579b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent             break;
3580b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent         }
3581b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     }
3582b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     if (i == mFormats.size()) {
3583b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent         return false;
3584b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     }
3585b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     for (i = 0; i < mChannelMasks.size(); i++)
3586b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     {
3587b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent         if (mChannelMasks[i] == channelMask) {
3588b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent             break;
3589b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent         }
3590b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     }
3591b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     if (i == mChannelMasks.size()) {
3592b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent         return false;
3593b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     }
3594b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     return true;
35955ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
35965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
35975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::IOProfile::dump(int fd)
35985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
35995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    const size_t SIZE = 256;
36005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    char buffer[SIZE];
36015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    String8 result;
36025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
360370c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "    - sampling rates: ");
36045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    result.append(buffer);
36055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < mSamplingRates.size(); i++) {
36065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        snprintf(buffer, SIZE, "%d", mSamplingRates[i]);
36075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        result.append(buffer);
36085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        result.append(i == (mSamplingRates.size() - 1) ? "\n" : ", ");
36095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
36105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
361170c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "    - channel masks: ");
36125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    result.append(buffer);
36135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < mChannelMasks.size(); i++) {
3614a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        snprintf(buffer, SIZE, "0x%04x", mChannelMasks[i]);
36155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        result.append(buffer);
36165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        result.append(i == (mChannelMasks.size() - 1) ? "\n" : ", ");
36175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
36185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
361970c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "    - formats: ");
36205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    result.append(buffer);
36215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < mFormats.size(); i++) {
3622a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        snprintf(buffer, SIZE, "0x%08x", mFormats[i]);
36235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        result.append(buffer);
36245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        result.append(i == (mFormats.size() - 1) ? "\n" : ", ");
36255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
36265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
3627a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    snprintf(buffer, SIZE, "    - devices: 0x%04x\n", mSupportedDevices);
36285ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    result.append(buffer);
3629a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    snprintf(buffer, SIZE, "    - flags: 0x%04x\n", mFlags);
36305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    result.append(buffer);
36315ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
36325ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    write(fd, result.string(), result.size());
36335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
36345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
36355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent// --- audio_policy.conf file parsing
36365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
36375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentstruct StringToEnum {
36385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    const char *name;
36395ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    uint32_t value;
36405ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent};
36415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
36425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent#define STRING_TO_ENUM(string) { #string, string }
36435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
36445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
36455ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentconst struct StringToEnum sDeviceNameToEnumTable[] = {
36465ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_EARPIECE),
36475ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_SPEAKER),
36485ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_WIRED_HEADSET),
36495ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_WIRED_HEADPHONE),
36505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_ALL_SCO),
36515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_ALL_A2DP),
36525ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_AUX_DIGITAL),
36535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET),
3654599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET),
3655599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_USB_DEVICE),
3656599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_USB_ACCESSORY),
3657599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_ALL_USB),
365848387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi    STRING_TO_ENUM(AUDIO_DEVICE_OUT_REMOTE_SUBMIX),
36595ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_BUILTIN_MIC),
36605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET),
36615ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_WIRED_HEADSET),
36625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_AUX_DIGITAL),
36635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_VOICE_CALL),
36645ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_BACK_MIC),
366548387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi    STRING_TO_ENUM(AUDIO_DEVICE_IN_REMOTE_SUBMIX),
3666ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET),
3667ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_DGTL_DOCK_HEADSET),
3668ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_USB_ACCESSORY),
36695ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent};
36705ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
36715ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentconst struct StringToEnum sFlagNameToEnumTable[] = {
36720977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent    STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT),
36730977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent    STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY),
3674b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent    STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST),
3675b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent    STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER),
3676b4d07b97d23cfaffe22c7859ad7c45e168a7df0eRichard Fitzgerald    STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD),
3677b4d07b97d23cfaffe22c7859ad7c45e168a7df0eRichard Fitzgerald    STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NON_BLOCKING),
36785ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent};
36795ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
36805ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentconst struct StringToEnum sFormatNameToEnumTable[] = {
36815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
36825ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
36835ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_FORMAT_MP3),
36845ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_FORMAT_AAC),
36855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
36865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent};
36875ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
36885ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentconst struct StringToEnum sOutChannelsNameToEnumTable[] = {
36895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_OUT_MONO),
36905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
36915ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
36925ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
36935ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent};
36945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
36955ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentconst struct StringToEnum sInChannelsNameToEnumTable[] = {
36965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_IN_MONO),
36975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_IN_STEREO),
369860758e27a4be8fc9ac1180f8a4055234e1702cc9Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_IN_FRONT_BACK),
36995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent};
37005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
37015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
37025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentuint32_t AudioPolicyManagerBase::stringToEnum(const struct StringToEnum *table,
37035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                              size_t size,
37045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                              const char *name)
37055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
37065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < size; i++) {
37075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strcmp(table[i].name, name) == 0) {
37085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("stringToEnum() found %s", table[i].name);
37095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            return table[i].value;
37105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
37115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
37125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return 0;
37135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
37145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
371518fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivibool AudioPolicyManagerBase::stringToBool(const char *value)
371618fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi{
371718fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi    return ((strcasecmp("true", value) == 0) || (strcmp("1", value) == 0));
371818fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi}
371918fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi
37200977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurentaudio_output_flags_t AudioPolicyManagerBase::parseFlagNames(char *name)
37215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
37225ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    uint32_t flag = 0;
37235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
37245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // it is OK to cast name to non const here as we are not going to use it after
37255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // strtok() modifies it
37265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    char *flagName = strtok(name, "|");
37275ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (flagName != NULL) {
37285ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strlen(flagName) != 0) {
37295ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            flag |= stringToEnum(sFlagNameToEnumTable,
37305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                               ARRAY_SIZE(sFlagNameToEnumTable),
37315ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                               flagName);
37325ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
37335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        flagName = strtok(NULL, "|");
37345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
3735a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    //force direct flag if offload flag is set: offloading implies a direct output stream
3736a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    // and all common behaviors are driven by checking only the direct flag
3737a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    // this should normally be set appropriately in the policy configuration file
3738a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    if ((flag & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
3739a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        flag |= AUDIO_OUTPUT_FLAG_DIRECT;
3740a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    }
3741a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald
37420977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent    return (audio_output_flags_t)flag;
37435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
37445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
37455ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentaudio_devices_t AudioPolicyManagerBase::parseDeviceNames(char *name)
37465ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
37475ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    uint32_t device = 0;
37485ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
37495ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    char *devName = strtok(name, "|");
37505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (devName != NULL) {
37515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strlen(devName) != 0) {
37525ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device |= stringToEnum(sDeviceNameToEnumTable,
37535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                 ARRAY_SIZE(sDeviceNameToEnumTable),
37545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                 devName);
37555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
37565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        devName = strtok(NULL, "|");
37575ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
3758c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    return device;
37595ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
37605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
37615ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadSamplingRates(char *name, IOProfile *profile)
37625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
37635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    char *str = strtok(name, "|");
37645ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
37653cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    // by convention, "0' in the first entry in mSamplingRates indicates the supported sampling
37663cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    // rates should be read from the output stream after it is opened for the first time
37673cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) {
37683cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        profile->mSamplingRates.add(0);
37693cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        return;
37703cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    }
37713cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
37725ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (str != NULL) {
37735ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        uint32_t rate = atoi(str);
37745ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (rate != 0) {
37755ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadSamplingRates() adding rate %d", rate);
37765ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mSamplingRates.add(rate);
37775ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
37785ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        str = strtok(NULL, "|");
37795ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
37805ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return;
37815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
37825ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
37835ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadFormats(char *name, IOProfile *profile)
37845ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
37855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    char *str = strtok(name, "|");
37865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
37873cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    // by convention, "0' in the first entry in mFormats indicates the supported formats
37883cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    // should be read from the output stream after it is opened for the first time
37893cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) {
37903cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        profile->mFormats.add((audio_format_t)0);
37913cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        return;
37923cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    }
37933cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
37945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (str != NULL) {
37955ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_format_t format = (audio_format_t)stringToEnum(sFormatNameToEnumTable,
37965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                             ARRAY_SIZE(sFormatNameToEnumTable),
37975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                             str);
37985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (format != 0) {
37995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mFormats.add(format);
38005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
38015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        str = strtok(NULL, "|");
38025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
38035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return;
38045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
38055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
38065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadInChannels(char *name, IOProfile *profile)
38075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
38085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    const char *str = strtok(name, "|");
38095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
38105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGV("loadInChannels() %s", name);
38113cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
38123cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) {
38133cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        profile->mChannelMasks.add((audio_channel_mask_t)0);
38143cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        return;
38153cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    }
38163cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
38175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (str != NULL) {
38185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_channel_mask_t channelMask =
38195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                (audio_channel_mask_t)stringToEnum(sInChannelsNameToEnumTable,
38205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                   ARRAY_SIZE(sInChannelsNameToEnumTable),
38215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                   str);
38225ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (channelMask != 0) {
38235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadInChannels() adding channelMask %04x", channelMask);
38245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mChannelMasks.add(channelMask);
38255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
38265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        str = strtok(NULL, "|");
38275ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
38285ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return;
38295ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
38305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
38315ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadOutChannels(char *name, IOProfile *profile)
38325ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
38335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    const char *str = strtok(name, "|");
38343cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
38353cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGV("loadOutChannels() %s", name);
38363cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
38373cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    // by convention, "0' in the first entry in mChannelMasks indicates the supported channel
38383cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    // masks should be read from the output stream after it is opened for the first time
38393cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) {
38403cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        profile->mChannelMasks.add((audio_channel_mask_t)0);
38413cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        return;
38423cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    }
38435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
38445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (str != NULL) {
38453cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        audio_channel_mask_t channelMask =
38463cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                (audio_channel_mask_t)stringToEnum(sOutChannelsNameToEnumTable,
38473cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                   ARRAY_SIZE(sOutChannelsNameToEnumTable),
38483cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                   str);
38495ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (channelMask != 0) {
38505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mChannelMasks.add(channelMask);
38515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
38525ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        str = strtok(NULL, "|");
38535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
38545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return;
38555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
38565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
385770c236c9290732782d5267935af1475b8d5ae602Eric Laurentstatus_t AudioPolicyManagerBase::loadInput(cnode *root, HwModule *module)
38585ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
38595ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    cnode *node = root->first_child;
38605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
38615ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    IOProfile *profile = new IOProfile(module);
38625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
38635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (node) {
38645ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
38655ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            loadSamplingRates((char *)node->value, profile);
38665ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, FORMATS_TAG) == 0) {
38675ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            loadFormats((char *)node->value, profile);
38685ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, CHANNELS_TAG) == 0) {
38695ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            loadInChannels((char *)node->value, profile);
38705ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, DEVICES_TAG) == 0) {
38715ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mSupportedDevices = parseDeviceNames((char *)node->value);
38725ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
38735ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        node = node->next;
38745ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
3875ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    ALOGW_IF(profile->mSupportedDevices == AUDIO_DEVICE_NONE,
38765ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadInput() invalid supported devices");
38775ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGW_IF(profile->mChannelMasks.size() == 0,
38785ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadInput() invalid supported channel masks");
38795ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGW_IF(profile->mSamplingRates.size() == 0,
38805ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadInput() invalid supported sampling rates");
38815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGW_IF(profile->mFormats.size() == 0,
38825ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadInput() invalid supported formats");
3883ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if ((profile->mSupportedDevices != AUDIO_DEVICE_NONE) &&
38845ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            (profile->mChannelMasks.size() != 0) &&
38855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            (profile->mSamplingRates.size() != 0) &&
38865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            (profile->mFormats.size() != 0)) {
38875ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
38885ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        ALOGV("loadInput() adding input mSupportedDevices %04x", profile->mSupportedDevices);
38895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
389070c236c9290732782d5267935af1475b8d5ae602Eric Laurent        module->mInputProfiles.add(profile);
38915ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return NO_ERROR;
38925ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    } else {
38935ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        delete profile;
38945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return BAD_VALUE;
38955ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
38965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
38975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
389870c236c9290732782d5267935af1475b8d5ae602Eric Laurentstatus_t AudioPolicyManagerBase::loadOutput(cnode *root, HwModule *module)
38995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
39005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    cnode *node = root->first_child;
39015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
39025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    IOProfile *profile = new IOProfile(module);
39035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
39045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (node) {
39055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
39065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            loadSamplingRates((char *)node->value, profile);
39075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, FORMATS_TAG) == 0) {
39085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            loadFormats((char *)node->value, profile);
39095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, CHANNELS_TAG) == 0) {
39105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            loadOutChannels((char *)node->value, profile);
39115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, DEVICES_TAG) == 0) {
39125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mSupportedDevices = parseDeviceNames((char *)node->value);
39135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, FLAGS_TAG) == 0) {
39145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mFlags = parseFlagNames((char *)node->value);
39155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
39165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        node = node->next;
39175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
3918ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    ALOGW_IF(profile->mSupportedDevices == AUDIO_DEVICE_NONE,
39195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadOutput() invalid supported devices");
39205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGW_IF(profile->mChannelMasks.size() == 0,
39215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadOutput() invalid supported channel masks");
39225ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGW_IF(profile->mSamplingRates.size() == 0,
39235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadOutput() invalid supported sampling rates");
39245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGW_IF(profile->mFormats.size() == 0,
39255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadOutput() invalid supported formats");
3926ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if ((profile->mSupportedDevices != AUDIO_DEVICE_NONE) &&
39275ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            (profile->mChannelMasks.size() != 0) &&
39285ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            (profile->mSamplingRates.size() != 0) &&
39295ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            (profile->mFormats.size() != 0)) {
39305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
39315ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        ALOGV("loadOutput() adding output mSupportedDevices %04x, mFlags %04x",
39325ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent              profile->mSupportedDevices, profile->mFlags);
39335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
393470c236c9290732782d5267935af1475b8d5ae602Eric Laurent        module->mOutputProfiles.add(profile);
39355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return NO_ERROR;
39365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    } else {
39375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        delete profile;
39385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return BAD_VALUE;
39395ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
39405ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
39415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
39425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadHwModule(cnode *root)
39435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
39445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    cnode *node = config_find(root, OUTPUTS_TAG);
39455ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    status_t status = NAME_NOT_FOUND;
394670c236c9290732782d5267935af1475b8d5ae602Eric Laurent
394770c236c9290732782d5267935af1475b8d5ae602Eric Laurent    HwModule *module = new HwModule(root->name);
394870c236c9290732782d5267935af1475b8d5ae602Eric Laurent
39495ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (node != NULL) {
39505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strcmp(root->name, AUDIO_HARDWARE_MODULE_ID_A2DP) == 0) {
39515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            mHasA2dp = true;
3952599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent        } else if (strcmp(root->name, AUDIO_HARDWARE_MODULE_ID_USB) == 0) {
3953599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            mHasUsb = true;
395448387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi        } else if (strcmp(root->name, AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX) == 0) {
395548387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            mHasRemoteSubmix = true;
39565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
3957599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent
39585ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        node = node->first_child;
39595ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        while (node) {
39605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadHwModule() loading output %s", node->name);
396170c236c9290732782d5267935af1475b8d5ae602Eric Laurent            status_t tmpStatus = loadOutput(node, module);
39625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            if (status == NAME_NOT_FOUND || status == NO_ERROR) {
39635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                status = tmpStatus;
39645ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
39655ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            node = node->next;
39665ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
39675ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
39685ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    node = config_find(root, INPUTS_TAG);
39695ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (node != NULL) {
39705ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        node = node->first_child;
39715ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        while (node) {
39725ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadHwModule() loading input %s", node->name);
397370c236c9290732782d5267935af1475b8d5ae602Eric Laurent            status_t tmpStatus = loadInput(node, module);
39745ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            if (status == NAME_NOT_FOUND || status == NO_ERROR) {
39755ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                status = tmpStatus;
39765ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
39775ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            node = node->next;
39785ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
39795ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
39805ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (status == NO_ERROR) {
398170c236c9290732782d5267935af1475b8d5ae602Eric Laurent        mHwModules.add(module);
398270c236c9290732782d5267935af1475b8d5ae602Eric Laurent    } else {
398370c236c9290732782d5267935af1475b8d5ae602Eric Laurent        delete module;
39845ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
39855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
39865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
39875ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadHwModules(cnode *root)
39885ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
39895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    cnode *node = config_find(root, AUDIO_HW_MODULE_TAG);
39905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (node == NULL) {
39915ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return;
39925ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
39935ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
39945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    node = node->first_child;
39955ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (node) {
39965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        ALOGV("loadHwModules() loading module %s", node->name);
39975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        loadHwModule(node);
39985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        node = node->next;
39995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
40005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
40015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
40025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadGlobalConfig(cnode *root)
40035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
40045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    cnode *node = config_find(root, GLOBAL_CONFIG_TAG);
40055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (node == NULL) {
40065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return;
40075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
40085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    node = node->first_child;
40095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (node) {
40105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strcmp(ATTACHED_OUTPUT_DEVICES_TAG, node->name) == 0) {
40115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            mAttachedOutputDevices = parseDeviceNames((char *)node->value);
4012ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            ALOGW_IF(mAttachedOutputDevices == AUDIO_DEVICE_NONE,
4013c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                    "loadGlobalConfig() no attached output devices");
40145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadGlobalConfig() mAttachedOutputDevices %04x", mAttachedOutputDevices);
40155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(DEFAULT_OUTPUT_DEVICE_TAG, node->name) == 0) {
40165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            mDefaultOutputDevice = (audio_devices_t)stringToEnum(sDeviceNameToEnumTable,
40175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                              ARRAY_SIZE(sDeviceNameToEnumTable),
40185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                              (char *)node->value);
4019ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            ALOGW_IF(mDefaultOutputDevice == AUDIO_DEVICE_NONE,
4020c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                    "loadGlobalConfig() default device not specified");
40215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadGlobalConfig() mDefaultOutputDevice %04x", mDefaultOutputDevice);
40225ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(ATTACHED_INPUT_DEVICES_TAG, node->name) == 0) {
4023ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            mAvailableInputDevices = parseDeviceNames((char *)node->value) & ~AUDIO_DEVICE_BIT_IN;
40245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadGlobalConfig() mAvailableInputDevices %04x", mAvailableInputDevices);
402518fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi        } else if (strcmp(SPEAKER_DRC_ENABLED_TAG, node->name) == 0) {
402618fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi            mSpeakerDrcEnabled = stringToBool((char *)node->value);
402718fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi            ALOGV("loadGlobalConfig() mSpeakerDrcEnabled = %d", mSpeakerDrcEnabled);
40285ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
40295ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        node = node->next;
40305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
40315ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
40325ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
40335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentstatus_t AudioPolicyManagerBase::loadAudioPolicyConfig(const char *path)
40345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
40355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    cnode *root;
40365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    char *data;
40375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
40385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    data = (char *)load_file(path, NULL);
40395ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (data == NULL) {
40405ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return -ENODEV;
40415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
40425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    root = config_node("", "");
40435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    config_load(root, data);
40445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
40455ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    loadGlobalConfig(root);
40465ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    loadHwModules(root);
40475ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
40485ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    config_free(root);
40495ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    free(root);
40505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    free(data);
40515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
40525ec145df7708564d385fd3fb764085321cf4c253Dima Zavin    ALOGI("loadAudioPolicyConfig() loaded %s\n", path);
40535ec145df7708564d385fd3fb764085321cf4c253Dima Zavin
40545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return NO_ERROR;
40555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
4056f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
4057739022f26a7127ba76a98dda65411496086114a7Dima Zavinvoid AudioPolicyManagerBase::defaultAudioPolicyConfig(void)
4058739022f26a7127ba76a98dda65411496086114a7Dima Zavin{
4059739022f26a7127ba76a98dda65411496086114a7Dima Zavin    HwModule *module;
4060739022f26a7127ba76a98dda65411496086114a7Dima Zavin    IOProfile *profile;
4061739022f26a7127ba76a98dda65411496086114a7Dima Zavin
4062739022f26a7127ba76a98dda65411496086114a7Dima Zavin    mDefaultOutputDevice = AUDIO_DEVICE_OUT_SPEAKER;
4063739022f26a7127ba76a98dda65411496086114a7Dima Zavin    mAttachedOutputDevices = AUDIO_DEVICE_OUT_SPEAKER;
4064ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    mAvailableInputDevices = AUDIO_DEVICE_IN_BUILTIN_MIC & ~AUDIO_DEVICE_BIT_IN;
4065739022f26a7127ba76a98dda65411496086114a7Dima Zavin
4066739022f26a7127ba76a98dda65411496086114a7Dima Zavin    module = new HwModule("primary");
4067739022f26a7127ba76a98dda65411496086114a7Dima Zavin
4068739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile = new IOProfile(module);
4069739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mSamplingRates.add(44100);
4070739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
4071739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mChannelMasks.add(AUDIO_CHANNEL_OUT_STEREO);
4072739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mSupportedDevices = AUDIO_DEVICE_OUT_SPEAKER;
4073739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mFlags = AUDIO_OUTPUT_FLAG_PRIMARY;
4074739022f26a7127ba76a98dda65411496086114a7Dima Zavin    module->mOutputProfiles.add(profile);
4075739022f26a7127ba76a98dda65411496086114a7Dima Zavin
4076739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile = new IOProfile(module);
4077739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mSamplingRates.add(8000);
4078739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
4079739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mChannelMasks.add(AUDIO_CHANNEL_IN_MONO);
4080739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mSupportedDevices = AUDIO_DEVICE_IN_BUILTIN_MIC;
4081739022f26a7127ba76a98dda65411496086114a7Dima Zavin    module->mInputProfiles.add(profile);
4082739022f26a7127ba76a98dda65411496086114a7Dima Zavin
4083739022f26a7127ba76a98dda65411496086114a7Dima Zavin    mHwModules.add(module);
4084739022f26a7127ba76a98dda65411496086114a7Dima Zavin}
4085f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
4086f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}; // namespace android
4087