AudioPolicyManagerBase.cpp revision be13d2ebd0ee66e8c09d978107c14a4221b04dff
1f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin/*
2f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin * Copyright (C) 2009 The Android Open Source Project
3f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin *
4f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin * Licensed under the Apache License, Version 2.0 (the "License");
5f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin * you may not use this file except in compliance with the License.
6f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin * You may obtain a copy of the License at
7f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin *
8f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin *      http://www.apache.org/licenses/LICENSE-2.0
9f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin *
10f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin * Unless required by applicable law or agreed to in writing, software
11f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin * distributed under the License is distributed on an "AS IS" BASIS,
12f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin * See the License for the specific language governing permissions and
14f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin * limitations under the License.
15f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin */
16f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
17f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#define LOG_TAG "AudioPolicyManagerBase"
18f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin//#define LOG_NDEBUG 0
193cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
203cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent//#define VERY_VERBOSE_LOGGING
213cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent#ifdef VERY_VERBOSE_LOGGING
223cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent#define ALOGVV ALOGV
233cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent#else
243cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent#define ALOGVV(a...) do { } while(0)
253cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent#endif
263cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
276d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi// A device mask for all audio input devices that are considered "virtual" when evaluating
286d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi// active inputs in getActiveInput()
296d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi#define APM_AUDIO_IN_DEVICE_VIRTUAL_ALL  AUDIO_DEVICE_IN_REMOTE_SUBMIX
306d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi
31f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#include <utils/Log.h>
32f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#include <hardware_legacy/AudioPolicyManagerBase.h>
331c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent#include <hardware/audio_effect.h>
3408b014d9e509c9163db6b33a63852e73db4d07ccEric Laurent#include <hardware/audio.h>
35f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#include <math.h>
365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent#include <hardware_legacy/audio_policy_conf.h>
37f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
38e81531e91ecae92aff471dbff9cbeb0f95ff4a80Dima Zavinnamespace android_audio_legacy {
39f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
40f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// ----------------------------------------------------------------------------
41f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// AudioPolicyInterface implementation
42f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// ----------------------------------------------------------------------------
43f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
44f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
45c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivistatus_t AudioPolicyManagerBase::setDeviceConnectionState(audio_devices_t device,
46f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                  AudioSystem::device_connection_state state,
47f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                  const char *device_address)
48f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
493cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    SortedVector <audio_io_handle_t> outputs;
50f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
516a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("setDeviceConnectionState() device: %x, state %d, address %s", device, state, device_address);
52f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
53f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // connect/disconnect only 1 device at a time
54ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
55f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
56f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (strlen(device_address) >= MAX_DEVICE_ADDRESS_LEN) {
575efbd421e0029d6fc44b1cc65c0e5e0d85e5161fSteve Block        ALOGE("setDeviceConnectionState() invalid address: %s", device_address);
58f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
59f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
60f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
61f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // handle output devices
62c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    if (audio_is_output_device(device)) {
63f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
64c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        if (!mHasA2dp && audio_is_a2dp_device(device)) {
6548387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            ALOGE("setDeviceConnectionState() invalid A2DP device: %x", device);
66f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return BAD_VALUE;
67f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
68c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        if (!mHasUsb && audio_is_usb_device(device)) {
6948387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            ALOGE("setDeviceConnectionState() invalid USB audio device: %x", device);
7048387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            return BAD_VALUE;
7148387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi        }
7248387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi        if (!mHasRemoteSubmix && audio_is_remote_submix_device((audio_devices_t)device)) {
7348387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            ALOGE("setDeviceConnectionState() invalid remote submix audio device: %x", device);
74599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            return BAD_VALUE;
75599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent        }
76f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
77c952527e6f89d5427881462823514be9d79f13e6Eric Laurent        // save a copy of the opened output descriptors before any output is opened or closed
78c952527e6f89d5427881462823514be9d79f13e6Eric Laurent        // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies()
79c952527e6f89d5427881462823514be9d79f13e6Eric Laurent        mPreviousOutputs = mOutputs;
80f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        switch (state)
81f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        {
82f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // handle output device connection
83f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        case AudioSystem::DEVICE_STATE_AVAILABLE:
84f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (mAvailableOutputDevices & device) {
8564cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block                ALOGW("setDeviceConnectionState() device already connected: %x", device);
86f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                return INVALID_OPERATION;
87f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
886a70518b93928d1c91457ff805e375c82d76b0e5Steve Block            ALOGV("setDeviceConnectionState() connecting device %x", device);
89f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
90c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            if (checkOutputsForDevice(device, state, outputs) != NO_ERROR) {
913cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                return INVALID_OPERATION;
923cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
933cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %d outputs",
943cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                  outputs.size());
95f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // register new device as available
96f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent            mAvailableOutputDevices = (audio_devices_t)(mAvailableOutputDevices | device);
97f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
983cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            if (!outputs.isEmpty()) {
993cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                String8 paramStr;
100c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                if (mHasA2dp && audio_is_a2dp_device(device)) {
1013cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    // handle A2DP device connection
1023cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    AudioParameter param;
1033cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    param.add(String8(AUDIO_PARAMETER_A2DP_SINK_ADDRESS), String8(device_address));
1043cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    paramStr = param.toString();
1053cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    mA2dpDeviceAddress = String8(device_address, MAX_DEVICE_ADDRESS_LEN);
1063cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    mA2dpSuspended = false;
107c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                } else if (audio_is_bluetooth_sco_device(device)) {
1083cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    // handle SCO device connection
1093cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    mScoDeviceAddress = String8(device_address, MAX_DEVICE_ADDRESS_LEN);
110c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                } else if (mHasUsb && audio_is_usb_device(device)) {
1113cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    // handle USB device connection
1123cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    mUsbCardAndDevice = String8(device_address, MAX_DEVICE_ADDRESS_LEN);
1133cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    paramStr = mUsbCardAndDevice;
1143cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                }
11548387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi                // not currently handling multiple simultaneous submixes: ignoring remote submix
11648387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi                //   case and address
1173cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                if (!paramStr.isEmpty()) {
1183cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    for (size_t i = 0; i < outputs.size(); i++) {
1193cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        mpClientInterface->setParameters(outputs[i], paramStr);
1203cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
1213cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                }
122f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
123f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            break;
124f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // handle output device disconnection
125f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        case AudioSystem::DEVICE_STATE_UNAVAILABLE: {
126f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (!(mAvailableOutputDevices & device)) {
12764cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block                ALOGW("setDeviceConnectionState() device not connected: %x", device);
128f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                return INVALID_OPERATION;
129f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
130f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1316a70518b93928d1c91457ff805e375c82d76b0e5Steve Block            ALOGV("setDeviceConnectionState() disconnecting device %x", device);
132f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // remove device from available output devices
133f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent            mAvailableOutputDevices = (audio_devices_t)(mAvailableOutputDevices & ~device);
134f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
135c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            checkOutputsForDevice(device, state, outputs);
136c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            if (mHasA2dp && audio_is_a2dp_device(device)) {
1373cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                // handle A2DP device disconnection
138b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                mA2dpDeviceAddress = "";
139b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                mA2dpSuspended = false;
140c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            } else if (audio_is_bluetooth_sco_device(device)) {
1413cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                // handle SCO device disconnection
142b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                mScoDeviceAddress = "";
143c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            } else if (mHasUsb && audio_is_usb_device(device)) {
1443cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                // handle USB device disconnection
145599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent                mUsbCardAndDevice = "";
146f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
14748387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            // not currently handling multiple simultaneous submixes: ignoring remote submix
14848387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            //   case and address
149f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            } break;
150f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
151f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        default:
1525efbd421e0029d6fc44b1cc65c0e5e0d85e5161fSteve Block            ALOGE("setDeviceConnectionState() invalid state: %x", state);
153f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return BAD_VALUE;
154f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
155f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
156f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        checkA2dpSuspend();
157f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        checkOutputForAllStrategies();
158b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        // outputs must be closed after checkOutputForAllStrategies() is executed
1593cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        if (!outputs.isEmpty()) {
1603cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            for (size_t i = 0; i < outputs.size(); i++) {
1613cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                // close unused outputs after device disconnection or direct outputs that have been
1623cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                // opened by checkOutputsForDevice() to query dynamic parameters
1633cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                if ((state == AudioSystem::DEVICE_STATE_UNAVAILABLE) ||
1643cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        (mOutputs.valueFor(outputs[i])->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
1653cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    closeOutput(outputs[i]);
1663cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                }
1673cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
168f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1695ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
170c952527e6f89d5427881462823514be9d79f13e6Eric Laurent        updateDevicesAndOutputs();
1715ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        for (size_t i = 0; i < mOutputs.size(); i++) {
1725ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            setOutputDevice(mOutputs.keyAt(i), getNewDevice(mOutputs.keyAt(i), true /*fromCache*/));
1735ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
174f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
175c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        if (device == AUDIO_DEVICE_OUT_WIRED_HEADSET) {
176c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = AUDIO_DEVICE_IN_WIRED_HEADSET;
177c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        } else if (device == AUDIO_DEVICE_OUT_BLUETOOTH_SCO ||
178c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                   device == AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET ||
179c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                   device == AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT) {
180c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
181f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        } else {
182f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return NO_ERROR;
183f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
184f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
185f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // handle input devices
186c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    if (audio_is_input_device(device)) {
187f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
188f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        switch (state)
189f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        {
190f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // handle input device connection
191f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        case AudioSystem::DEVICE_STATE_AVAILABLE: {
192f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (mAvailableInputDevices & device) {
19364cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block                ALOGW("setDeviceConnectionState() device already connected: %d", device);
194f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                return INVALID_OPERATION;
195f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
196ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            mAvailableInputDevices = mAvailableInputDevices | (device & ~AUDIO_DEVICE_BIT_IN);
197f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
198f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            break;
199f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
200f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // handle input device disconnection
201f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        case AudioSystem::DEVICE_STATE_UNAVAILABLE: {
202f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (!(mAvailableInputDevices & device)) {
20364cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block                ALOGW("setDeviceConnectionState() device not connected: %d", device);
204f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                return INVALID_OPERATION;
205f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            mAvailableInputDevices = (audio_devices_t) (mAvailableInputDevices & ~device);
207f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            } break;
208f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
209f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        default:
2105efbd421e0029d6fc44b1cc65c0e5e0d85e5161fSteve Block            ALOGE("setDeviceConnectionState() invalid state: %x", state);
211f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return BAD_VALUE;
212f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
213f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
214f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        audio_io_handle_t activeInput = getActiveInput();
215f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (activeInput != 0) {
216f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            AudioInputDescriptor *inputDesc = mInputs.valueFor(activeInput);
217f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent            audio_devices_t newDevice = getDeviceForInputSource(inputDesc->mInputSource);
218ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            if ((newDevice != AUDIO_DEVICE_NONE) && (newDevice != inputDesc->mDevice)) {
2196a70518b93928d1c91457ff805e375c82d76b0e5Steve Block                ALOGV("setDeviceConnectionState() changing device from %x to %x for input %d",
220f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        inputDesc->mDevice, newDevice, activeInput);
221f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                inputDesc->mDevice = newDevice;
222f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                AudioParameter param = AudioParameter();
223f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.addInt(String8(AudioParameter::keyRouting), (int)newDevice);
224f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mpClientInterface->setParameters(activeInput, param.toString());
225f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
226f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
227f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
228f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return NO_ERROR;
229f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
230f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
23164cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block    ALOGW("setDeviceConnectionState() invalid device: %x", device);
232f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return BAD_VALUE;
233f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
234f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
235c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel TriviAudioSystem::device_connection_state AudioPolicyManagerBase::getDeviceConnectionState(audio_devices_t device,
236f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                  const char *device_address)
237f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
238f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioSystem::device_connection_state state = AudioSystem::DEVICE_STATE_UNAVAILABLE;
239f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    String8 address = String8(device_address);
240c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    if (audio_is_output_device(device)) {
241f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (device & mAvailableOutputDevices) {
242c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            if (audio_is_a2dp_device(device) &&
2435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                (!mHasA2dp || (address != "" && mA2dpDeviceAddress != address))) {
244f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                return state;
245f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
246c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            if (audio_is_bluetooth_sco_device(device) &&
247f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                address != "" && mScoDeviceAddress != address) {
248f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                return state;
249f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
250c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            if (audio_is_usb_device(device) &&
251599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent                (!mHasUsb || (address != "" && mUsbCardAndDevice != address))) {
25248387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi                ALOGE("getDeviceConnectionState() invalid device: %x", device);
25348387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi                return state;
25448387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            }
25548387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            if (audio_is_remote_submix_device((audio_devices_t)device) && !mHasRemoteSubmix) {
256599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent                return state;
257599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            }
258f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            state = AudioSystem::DEVICE_STATE_AVAILABLE;
259f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
260c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    } else if (audio_is_input_device(device)) {
261f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (device & mAvailableInputDevices) {
262f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            state = AudioSystem::DEVICE_STATE_AVAILABLE;
263f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
264f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
265f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
266f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return state;
267f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
268f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
269f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::setPhoneState(int state)
270f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2716a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("setPhoneState() state %d", state);
272ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    audio_devices_t newDevice = AUDIO_DEVICE_NONE;
273f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (state < 0 || state >= AudioSystem::NUM_MODES) {
27464cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("setPhoneState() invalid state %d", state);
275f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
276f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
277f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
278f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (state == mPhoneState ) {
27964cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("setPhoneState() setting same state %d", state);
280f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
281f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
282f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
283f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if leaving call state, handle special case of active streams
284f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // pertaining to sonification strategy see handleIncallSonification()
285f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (isInCall()) {
2866a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("setPhoneState() in call state management: new state is %d", state);
287f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
288f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            handleIncallSonification(stream, false, true);
289f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
290f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
291f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
292f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // store previous phone state for management of sonification strategy below
293f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    int oldState = mPhoneState;
294f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mPhoneState = state;
295f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    bool force = false;
296f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
297f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // are we entering or starting a call
298f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (!isStateInCall(oldState) && isStateInCall(state)) {
2996a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("  Entering call in setPhoneState()");
300f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // force routing command to audio hardware when starting a call
301f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // even if no device change is needed
302f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        force = true;
303f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else if (isStateInCall(oldState) && !isStateInCall(state)) {
3046a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("  Exiting call in setPhoneState()");
305f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // force routing command to audio hardware when exiting a call
306f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // even if no device change is needed
307f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        force = true;
308f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else if (isStateInCall(state) && (state != oldState)) {
3096a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("  Switching between telephony and VoIP in setPhoneState()");
310f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // force routing command to audio hardware when switching between telephony and VoIP
311f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // even if no device change is needed
312f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        force = true;
313f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
314f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
315f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // check for device and output changes triggered by new phone state
3165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    newDevice = getNewDevice(mPrimaryOutput, false /*fromCache*/);
317f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkA2dpSuspend();
318f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForAllStrategies();
319c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    updateDevicesAndOutputs();
320f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
321b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    AudioOutputDescriptor *hwOutputDesc = mOutputs.valueFor(mPrimaryOutput);
322f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
323f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // force routing command to audio hardware when ending call
324f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // even if no device change is needed
325ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if (isStateInCall(oldState) && newDevice == AUDIO_DEVICE_NONE) {
326f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        newDevice = hwOutputDesc->device();
327f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
328f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
329f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // when changing from ring tone to in call mode, mute the ringing tone
330f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // immediately and delay the route change to avoid sending the ring tone
331f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // tail into the earpiece or headset.
332f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    int delayMs = 0;
333f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (isStateInCall(state) && oldState == AudioSystem::MODE_RINGTONE) {
334f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // delay the device change command by twice the output latency to have some margin
335f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // and be sure that audio buffers not yet affected by the mute are out when
336f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // we actually apply the route change
337f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        delayMs = hwOutputDesc->mLatency*2;
338b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        setStreamMute(AudioSystem::RING, true, mPrimaryOutput);
339f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
340f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
341f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // change routing is necessary
342b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    setOutputDevice(mPrimaryOutput, newDevice, force, delayMs);
343f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
344f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if entering in call state, handle special case of active streams
345f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // pertaining to sonification strategy see handleIncallSonification()
346f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (isStateInCall(state)) {
3476a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("setPhoneState() in call state management: new state is %d", state);
348f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // unmute the ringing tone after a sufficient delay if it was muted before
349f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // setting output device above
350f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (oldState == AudioSystem::MODE_RINGTONE) {
351b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            setStreamMute(AudioSystem::RING, false, mPrimaryOutput, MUTE_TIME_MS);
352f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
353f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
354f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            handleIncallSonification(stream, true, true);
355f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
356f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
357f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
358f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
359f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (state == AudioSystem::MODE_RINGTONE &&
360f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        isStreamActive(AudioSystem::MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
361f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mLimitRingtoneVolume = true;
362f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
363f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mLimitRingtoneVolume = false;
364f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
365f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
366f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
367f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::setForceUse(AudioSystem::force_use usage, AudioSystem::forced_config config)
368f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3696a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mPhoneState);
370f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
371f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    bool forceVolumeReeval = false;
372f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    switch(usage) {
373f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::FOR_COMMUNICATION:
374f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (config != AudioSystem::FORCE_SPEAKER && config != AudioSystem::FORCE_BT_SCO &&
375f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_NONE) {
37664cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("setForceUse() invalid config %d for FOR_COMMUNICATION", config);
377f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return;
378f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
379f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        forceVolumeReeval = true;
380f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mForceUse[usage] = config;
381f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
382f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::FOR_MEDIA:
383f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (config != AudioSystem::FORCE_HEADPHONES && config != AudioSystem::FORCE_BT_A2DP &&
384f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_WIRED_ACCESSORY &&
385f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_ANALOG_DOCK &&
3861afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent            config != AudioSystem::FORCE_DIGITAL_DOCK && config != AudioSystem::FORCE_NONE &&
38748387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            config != AudioSystem::FORCE_NO_BT_A2DP &&
38848387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            config != AudioSystem::FORCE_REMOTE_SUBMIX) {
38964cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("setForceUse() invalid config %d for FOR_MEDIA", config);
390f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return;
391f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
392f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mForceUse[usage] = config;
393f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
394f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::FOR_RECORD:
395f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (config != AudioSystem::FORCE_BT_SCO && config != AudioSystem::FORCE_WIRED_ACCESSORY &&
396f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_NONE) {
39764cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("setForceUse() invalid config %d for FOR_RECORD", config);
398f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return;
399f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
400f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mForceUse[usage] = config;
401f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
402f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::FOR_DOCK:
403f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (config != AudioSystem::FORCE_NONE && config != AudioSystem::FORCE_BT_CAR_DOCK &&
404f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_BT_DESK_DOCK &&
405f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_WIRED_ACCESSORY &&
406f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_ANALOG_DOCK &&
407f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_DIGITAL_DOCK) {
40864cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("setForceUse() invalid config %d for FOR_DOCK", config);
409f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
410f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        forceVolumeReeval = true;
411f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mForceUse[usage] = config;
412f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
413f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    default:
41464cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("setForceUse() invalid usage %d", usage);
415f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
416f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
417f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
4185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // check for device and output changes triggered by new force usage
419f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkA2dpSuspend();
420f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForAllStrategies();
421c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    updateDevicesAndOutputs();
4225ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < mOutputs.size(); i++) {
4235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_io_handle_t output = mOutputs.keyAt(i);
4245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_devices_t newDevice = getNewDevice(output, true /*fromCache*/);
425ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        setOutputDevice(output, newDevice, (newDevice != AUDIO_DEVICE_NONE));
426ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
4275ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            applyStreamVolumes(output, newDevice, 0, true);
4285ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
429f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
430f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
431f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    audio_io_handle_t activeInput = getActiveInput();
432f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (activeInput != 0) {
433f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioInputDescriptor *inputDesc = mInputs.valueFor(activeInput);
4345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_devices_t newDevice = getDeviceForInputSource(inputDesc->mInputSource);
435ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if ((newDevice != AUDIO_DEVICE_NONE) && (newDevice != inputDesc->mDevice)) {
4366a70518b93928d1c91457ff805e375c82d76b0e5Steve Block            ALOGV("setForceUse() changing device from %x to %x for input %d",
437f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    inputDesc->mDevice, newDevice, activeInput);
438f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            inputDesc->mDevice = newDevice;
439f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            AudioParameter param = AudioParameter();
440f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            param.addInt(String8(AudioParameter::keyRouting), (int)newDevice);
441f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mpClientInterface->setParameters(activeInput, param.toString());
442f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
443f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
444f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
445f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
446f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
447f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima ZavinAudioSystem::forced_config AudioPolicyManagerBase::getForceUse(AudioSystem::force_use usage)
448f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
449f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return mForceUse[usage];
450f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
451f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
452f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::setSystemProperty(const char* property, const char* value)
453f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
4546a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("setSystemProperty() property %s, value %s", property, value);
455f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (strcmp(property, "ro.camera.sound.forced") == 0) {
456f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (atoi(value)) {
4576a70518b93928d1c91457ff805e375c82d76b0e5Steve Block            ALOGV("ENFORCED_AUDIBLE cannot be muted");
458f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mStreams[AudioSystem::ENFORCED_AUDIBLE].mCanBeMuted = false;
459f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        } else {
4606a70518b93928d1c91457ff805e375c82d76b0e5Steve Block            ALOGV("ENFORCED_AUDIBLE can be muted");
461f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mStreams[AudioSystem::ENFORCED_AUDIBLE].mCanBeMuted = true;
462f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
463f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
464f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
465f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
4663cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric LaurentAudioPolicyManagerBase::IOProfile *AudioPolicyManagerBase::getProfileForDirectOutput(
4673cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                               audio_devices_t device,
46870c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                               uint32_t samplingRate,
46970c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                               uint32_t format,
47070c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                               uint32_t channelMask,
4710977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent                                                               audio_output_flags_t flags)
47270c236c9290732782d5267935af1475b8d5ae602Eric Laurent{
47370c236c9290732782d5267935af1475b8d5ae602Eric Laurent    for (size_t i = 0; i < mHwModules.size(); i++) {
47470c236c9290732782d5267935af1475b8d5ae602Eric Laurent        if (mHwModules[i]->mHandle == 0) {
47570c236c9290732782d5267935af1475b8d5ae602Eric Laurent            continue;
47670c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
47770c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) {
4783cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent           IOProfile *profile = mHwModules[i]->mOutputProfiles[j];
4793cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent           if (profile->isCompatibleProfile(device, samplingRate, format,
4803cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                           channelMask,
4813cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                           AUDIO_OUTPUT_FLAG_DIRECT)) {
4823cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent               if (mAvailableOutputDevices & profile->mSupportedDevices) {
4833cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                   return mHwModules[i]->mOutputProfiles[j];
4843cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent               }
48570c236c9290732782d5267935af1475b8d5ae602Eric Laurent           }
48670c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
48770c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
48870c236c9290732782d5267935af1475b8d5ae602Eric Laurent    return 0;
48970c236c9290732782d5267935af1475b8d5ae602Eric Laurent}
49070c236c9290732782d5267935af1475b8d5ae602Eric Laurent
491f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinaudio_io_handle_t AudioPolicyManagerBase::getOutput(AudioSystem::stream_type stream,
492f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    uint32_t samplingRate,
493f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    uint32_t format,
49470c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                    uint32_t channelMask,
495f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    AudioSystem::output_flags flags)
496f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
497f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    audio_io_handle_t output = 0;
498f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    uint32_t latency = 0;
499f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    routing_strategy strategy = getStrategy((AudioSystem::stream_type)stream);
5005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
50170c236c9290732782d5267935af1475b8d5ae602Eric Laurent    ALOGV("getOutput() stream %d, samplingRate %d, format %d, channelMask %x, flags %x",
50270c236c9290732782d5267935af1475b8d5ae602Eric Laurent          stream, samplingRate, format, channelMask, flags);
503f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
504f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
505f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mCurOutput != 0) {
50670c236c9290732782d5267935af1475b8d5ae602Eric Laurent        ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
507f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
508f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
509f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (mTestOutputs[mCurOutput] == 0) {
5106a70518b93928d1c91457ff805e375c82d76b0e5Steve Block            ALOGV("getOutput() opening test output");
5113cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(NULL);
512f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            outputDesc->mDevice = mTestDevice;
513f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            outputDesc->mSamplingRate = mTestSamplingRate;
514f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            outputDesc->mFormat = mTestFormat;
51570c236c9290732782d5267935af1475b8d5ae602Eric Laurent            outputDesc->mChannelMask = mTestChannels;
516f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            outputDesc->mLatency = mTestLatencyMs;
5170977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent            outputDesc->mFlags = (audio_output_flags_t)(mDirectOutput ? AudioSystem::OUTPUT_FLAG_DIRECT : 0);
518f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            outputDesc->mRefCount[stream] = 0;
51970c236c9290732782d5267935af1475b8d5ae602Eric Laurent            mTestOutputs[mCurOutput] = mpClientInterface->openOutput(0, &outputDesc->mDevice,
520f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            &outputDesc->mSamplingRate,
521f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            &outputDesc->mFormat,
52270c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                            &outputDesc->mChannelMask,
523f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            &outputDesc->mLatency,
524f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            outputDesc->mFlags);
525f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (mTestOutputs[mCurOutput]) {
526f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                AudioParameter outputCmd = AudioParameter();
527f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                outputCmd.addInt(String8("set_id"),mCurOutput);
528f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
529f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                addOutput(mTestOutputs[mCurOutput], outputDesc);
530f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
531f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
532f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return mTestOutputs[mCurOutput];
533f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
534f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
535f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
536f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // open a direct output if required by specified parameters
5373cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    IOProfile *profile = getProfileForDirectOutput(device,
5383cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                   samplingRate,
5393cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                   format,
5403cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                   channelMask,
5413cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                   (audio_output_flags_t)flags);
5423cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    if (profile != NULL) {
543f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
5446a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("getOutput() opening direct output device %x", device);
54570c236c9290732782d5267935af1475b8d5ae602Eric Laurent
5463cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(profile);
547f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mDevice = device;
548f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mSamplingRate = samplingRate;
54970c236c9290732782d5267935af1475b8d5ae602Eric Laurent        outputDesc->mFormat = (audio_format_t)format;
55070c236c9290732782d5267935af1475b8d5ae602Eric Laurent        outputDesc->mChannelMask = (audio_channel_mask_t)channelMask;
551f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mLatency = 0;
5523cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        outputDesc->mFlags = (audio_output_flags_t)(flags | AUDIO_OUTPUT_FLAG_DIRECT);;
553f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mRefCount[stream] = 0;
554f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mStopTime[stream] = 0;
5553cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        output = mpClientInterface->openOutput(profile->mModule->mHandle,
55670c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                        &outputDesc->mDevice,
557f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                        &outputDesc->mSamplingRate,
558f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                        &outputDesc->mFormat,
55970c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                        &outputDesc->mChannelMask,
560f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                        &outputDesc->mLatency,
561f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                        outputDesc->mFlags);
562f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
5639f1f9b509c930830f6f32e9ef6c2c8a03d6fa96eJean-Michel Trivi        // only accept an output with the requested parameters
564f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (output == 0 ||
565f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            (samplingRate != 0 && samplingRate != outputDesc->mSamplingRate) ||
566f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            (format != 0 && format != outputDesc->mFormat) ||
56770c236c9290732782d5267935af1475b8d5ae602Eric Laurent            (channelMask != 0 && channelMask != outputDesc->mChannelMask)) {
5683cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
5693cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    "format %d %d, channelMask %04x %04x", output, samplingRate,
5703cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
5713cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    outputDesc->mChannelMask);
572f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (output != 0) {
573f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mpClientInterface->closeOutput(output);
574f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
575f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            delete outputDesc;
576f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return 0;
577f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
578f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        addOutput(output, outputDesc);
5793cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        ALOGV("getOutput() returns direct output %d", output);
580f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return output;
581f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
582f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
5839f1f9b509c930830f6f32e9ef6c2c8a03d6fa96eJean-Michel Trivi    // ignoring channel mask due to downmix capability in mixer
5849f1f9b509c930830f6f32e9ef6c2c8a03d6fa96eJean-Michel Trivi
585f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // open a non direct output
586f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
587f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // get which output is suitable for the specified stream. The actual routing change will happen
588f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // when startOutput() will be called
589c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
590f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
5915ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    output = selectOutput(outputs, flags);
592f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
59370c236c9290732782d5267935af1475b8d5ae602Eric Laurent    ALOGW_IF((output ==0), "getOutput() could not find output for stream %d, samplingRate %d,"
59470c236c9290732782d5267935af1475b8d5ae602Eric Laurent            "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
595f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
5965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGV("getOutput() returns output %d", output);
5975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
598f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return output;
599f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
600f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
6015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentaudio_io_handle_t AudioPolicyManagerBase::selectOutput(const SortedVector<audio_io_handle_t>& outputs,
6025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                       AudioSystem::output_flags flags)
6035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
6045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // select one output among several that provide a path to a particular device or set of
6055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // devices (the list was previously build by getOutputsForDevice()).
6065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // The priority is as follows:
6075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // 1: the output with the highest number of requested policy flags
6085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // 2: the primary output
6095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // 3: the first output in the list
6105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
6115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (outputs.size() == 0) {
6125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return 0;
6135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
6145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (outputs.size() == 1) {
6155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return outputs[0];
6165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
6175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
6185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    int maxCommonFlags = 0;
6195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    audio_io_handle_t outputFlags = 0;
6205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    audio_io_handle_t outputPrimary = 0;
6215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
6225ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < outputs.size(); i++) {
6235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        AudioOutputDescriptor *outputDesc = mOutputs.valueFor(outputs[i]);
6245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (!outputDesc->isDuplicated()) {
6255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            int commonFlags = (int)AudioSystem::popCount(outputDesc->mProfile->mFlags & flags);
6265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            if (commonFlags > maxCommonFlags) {
6275ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                outputFlags = outputs[i];
6285ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                maxCommonFlags = commonFlags;
6295ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags);
6305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
6310977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent            if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
6325ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                outputPrimary = outputs[i];
6335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
6345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
6355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
6365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
6375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (outputFlags != 0) {
6385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return outputFlags;
6395ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
6405ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (outputPrimary != 0) {
6415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return outputPrimary;
6425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
6435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
6445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return outputs[0];
6455ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
6465ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
647f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::startOutput(audio_io_handle_t output,
648f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                             AudioSystem::stream_type stream,
649f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                             int session)
650f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
6516a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("startOutput() output %d, stream %d, session %d", output, stream, session);
652f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mOutputs.indexOfKey(output);
653f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
65464cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("startOutput() unknow output %d", output);
655f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
656f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
657f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
658f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index);
659f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
6605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // increment usage count for this stream on the requested output:
661f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // NOTE that the usage count is the same for duplicated output and hardware output which is
6625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
663f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    outputDesc->changeRefCount(stream, 1);
664f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
6655ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (outputDesc->mRefCount[stream] == 1) {
6665ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_devices_t newDevice = getNewDevice(output, false /*fromCache*/);
667b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        routing_strategy strategy = getStrategy(stream);
668b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
669b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                            (strategy == STRATEGY_SONIFICATION_RESPECTFUL);
670b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        uint32_t waitMs = 0;
6715ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        bool force = false;
6725ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        for (size_t i = 0; i < mOutputs.size(); i++) {
6735ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            AudioOutputDescriptor *desc = mOutputs.valueAt(i);
674b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent            if (desc != outputDesc) {
675b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                // force a device change if any other output is managed by the same hw
676b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                // module and has a current device selection that differs from selected device.
677b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                // In this case, the audio HAL must receive the new device selection so that it can
678b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                // change the device currently selected by the other active output.
679b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                if (outputDesc->sharesHwModuleWith(desc) &&
6805ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                    desc->device() != newDevice) {
681b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                    force = true;
682b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                }
683b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                // wait for audio on other active outputs to be presented when starting
684b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                // a notification so that audio focus effect can propagate.
685b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                if (shouldWait && (desc->refCount() != 0) && (waitMs < desc->latency())) {
686b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                    waitMs = desc->latency();
687b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                }
6885ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
6895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
690b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        uint32_t muteWaitMs = setOutputDevice(output, newDevice, force);
691f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
6925ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // handle special case for sonification while in call
6935ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (isInCall()) {
6945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            handleIncallSonification(stream, true, false);
6955ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
696c16ac09f510437e8340be691720177a490ae78f0Eric Laurent
6975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // apply volume rules for current stream and device if necessary
6985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        checkAndSetVolume(stream,
699c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                          mStreams[stream].getVolumeIndex(newDevice),
7005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                          output,
7015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                          newDevice);
70212bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi
7035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // update the outputs if starting an output with a stream that can affect notification
7045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // routing
7055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        handleNotificationRoutingForStream(stream);
706b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        if (waitMs > muteWaitMs) {
707b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent            usleep((waitMs - muteWaitMs) * 2 * 1000);
708b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        }
7095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
710f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
711f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
712f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
7135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
714f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::stopOutput(audio_io_handle_t output,
715f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            AudioSystem::stream_type stream,
716f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            int session)
717f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
7186a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
719f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mOutputs.indexOfKey(output);
720f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
72164cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("stopOutput() unknow output %d", output);
722f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
723f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
724f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
725f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index);
726f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
727f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // handle special case for sonification while in call
728f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (isInCall()) {
729f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        handleIncallSonification(stream, false, false);
730f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
731f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
732f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (outputDesc->mRefCount[stream] > 0) {
733f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // decrement usage count of this stream on the output
734f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->changeRefCount(stream, -1);
735f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // store time at which the stream was stopped - see isStreamActive()
7365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (outputDesc->mRefCount[stream] == 0) {
7375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            outputDesc->mStopTime[stream] = systemTime();
7385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            audio_devices_t newDevice = getNewDevice(output, false /*fromCache*/);
7395ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // delay the device switch by twice the latency because stopOutput() is executed when
7405ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // the track stop() command is received and at that time the audio track buffer can
7415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // still contain data that needs to be drained. The latency only covers the audio HAL
7425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // and kernel buffers. Also the latency does not always include additional delay in the
7435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // audio path (audio DSP, CODEC ...)
7445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            setOutputDevice(output, newDevice, false, outputDesc->mLatency*2);
7455ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
7465ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // force restoring the device selection on other active outputs if it differs from the
7475ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // one being selected for this output
7485ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            for (size_t i = 0; i < mOutputs.size(); i++) {
7495ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                audio_io_handle_t curOutput = mOutputs.keyAt(i);
7505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                AudioOutputDescriptor *desc = mOutputs.valueAt(i);
7515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                if (curOutput != output &&
7525ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                        desc->refCount() != 0 &&
7535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                        outputDesc->sharesHwModuleWith(desc) &&
7545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                        newDevice != desc->device()) {
7555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                    setOutputDevice(curOutput,
7565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                    getNewDevice(curOutput, false /*fromCache*/),
7575ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                    true,
7585ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                    outputDesc->mLatency*2);
7595ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                }
7605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
7615ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // update the outputs if stopping one with a stream that can affect notification routing
7625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            handleNotificationRoutingForStream(stream);
763f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
764f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return NO_ERROR;
765f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
76664cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("stopOutput() refcount is already 0 for output %d", output);
767f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return INVALID_OPERATION;
768f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
769f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
770f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
771f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::releaseOutput(audio_io_handle_t output)
772f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
7736a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("releaseOutput() %d", output);
774f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mOutputs.indexOfKey(output);
775f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
77664cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("releaseOutput() releasing unknown output %d", output);
777f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
778f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
779f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
780f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
781f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    int testIndex = testOutputIndex(output);
782f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (testIndex != 0) {
783f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index);
784f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (outputDesc->refCount() == 0) {
785f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mpClientInterface->closeOutput(output);
786f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            delete mOutputs.valueAt(index);
787f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mOutputs.removeItem(output);
788f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mTestOutputs[testIndex] = 0;
789f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
790f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
791f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
792f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
793f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
794f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mOutputs.valueAt(index)->mFlags & AudioSystem::OUTPUT_FLAG_DIRECT) {
795f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mpClientInterface->closeOutput(output);
796f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        delete mOutputs.valueAt(index);
797f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mOutputs.removeItem(output);
798be13d2ebd0ee66e8c09d978107c14a4221b04dffEric Laurent        mPreviousOutputs = mOutputs;
799f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
80012bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi
801f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
802f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
803f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinaudio_io_handle_t AudioPolicyManagerBase::getInput(int inputSource,
804f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    uint32_t samplingRate,
805f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    uint32_t format,
80670c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                    uint32_t channelMask,
807f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    AudioSystem::audio_in_acoustics acoustics)
808f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
809f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    audio_io_handle_t input = 0;
810f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent    audio_devices_t device = getDeviceForInputSource(inputSource);
811f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
81270c236c9290732782d5267935af1475b8d5ae602Eric Laurent    ALOGV("getInput() inputSource %d, samplingRate %d, format %d, channelMask %x, acoustics %x",
81370c236c9290732782d5267935af1475b8d5ae602Eric Laurent          inputSource, samplingRate, format, channelMask, acoustics);
814f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
815ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if (device == AUDIO_DEVICE_NONE) {
8165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        ALOGW("getInput() could not find device for inputSource %d", inputSource);
817f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return 0;
818f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
819f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
820f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // adapt channel selection to input source
821f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    switch(inputSource) {
822f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_UPLINK:
82370c236c9290732782d5267935af1475b8d5ae602Eric Laurent        channelMask = AudioSystem::CHANNEL_IN_VOICE_UPLINK;
824f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
825f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_DOWNLINK:
82670c236c9290732782d5267935af1475b8d5ae602Eric Laurent        channelMask = AudioSystem::CHANNEL_IN_VOICE_DNLINK;
827f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
828f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_CALL:
82970c236c9290732782d5267935af1475b8d5ae602Eric Laurent        channelMask = (AudioSystem::CHANNEL_IN_VOICE_UPLINK | AudioSystem::CHANNEL_IN_VOICE_DNLINK);
830f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
831f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    default:
832f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
833f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
834f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
8355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    IOProfile *profile = getInputProfile(device,
8365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                         samplingRate,
8375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                         format,
83870c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                         channelMask);
8395ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (profile == NULL) {
8405ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        ALOGW("getInput() could not find profile for device %04x, samplingRate %d, format %d,"
84170c236c9290732782d5267935af1475b8d5ae602Eric Laurent                "channelMask %04x",
84270c236c9290732782d5267935af1475b8d5ae602Eric Laurent                device, samplingRate, format, channelMask);
84370c236c9290732782d5267935af1475b8d5ae602Eric Laurent        return 0;
84470c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
84570c236c9290732782d5267935af1475b8d5ae602Eric Laurent
84670c236c9290732782d5267935af1475b8d5ae602Eric Laurent    if (profile->mModule->mHandle == 0) {
8473cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        ALOGE("getInput(): HW module %s not opened", profile->mModule->mName);
8485ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return 0;
8495ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
8505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
8515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    AudioInputDescriptor *inputDesc = new AudioInputDescriptor(profile);
852f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
853f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    inputDesc->mInputSource = inputSource;
854f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    inputDesc->mDevice = device;
855f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    inputDesc->mSamplingRate = samplingRate;
85670c236c9290732782d5267935af1475b8d5ae602Eric Laurent    inputDesc->mFormat = (audio_format_t)format;
85770c236c9290732782d5267935af1475b8d5ae602Eric Laurent    inputDesc->mChannelMask = (audio_channel_mask_t)channelMask;
858f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    inputDesc->mRefCount = 0;
85970c236c9290732782d5267935af1475b8d5ae602Eric Laurent    input = mpClientInterface->openInput(profile->mModule->mHandle,
86070c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                    &inputDesc->mDevice,
861f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    &inputDesc->mSamplingRate,
862f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    &inputDesc->mFormat,
86370c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                    &inputDesc->mChannelMask);
864f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
865f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // only accept input with the exact requested set of parameters
866f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (input == 0 ||
867f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        (samplingRate != inputDesc->mSamplingRate) ||
868f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        (format != inputDesc->mFormat) ||
86970c236c9290732782d5267935af1475b8d5ae602Eric Laurent        (channelMask != inputDesc->mChannelMask)) {
87070c236c9290732782d5267935af1475b8d5ae602Eric Laurent        ALOGV("getInput() failed opening input: samplingRate %d, format %d, channelMask %d",
87170c236c9290732782d5267935af1475b8d5ae602Eric Laurent                samplingRate, format, channelMask);
872f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (input != 0) {
873f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mpClientInterface->closeInput(input);
874f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
875f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        delete inputDesc;
876f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return 0;
877f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
878f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mInputs.add(input, inputDesc);
879f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return input;
880f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
881f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
882f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::startInput(audio_io_handle_t input)
883f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
8846a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("startInput() input %d", input);
885f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mInputs.indexOfKey(input);
886f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
88764cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("startInput() unknow input %d", input);
888f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
889f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
890f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioInputDescriptor *inputDesc = mInputs.valueAt(index);
891f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
892f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
893f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mTestInput == 0)
894f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
895f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    {
896f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // refuse 2 active AudioRecord clients at the same time
897f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (getActiveInput() != 0) {
89864cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("startInput() input %d failed: other input already started", input);
899f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return INVALID_OPERATION;
900f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
901f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
902f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
903f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioParameter param = AudioParameter();
904f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    param.addInt(String8(AudioParameter::keyRouting), (int)inputDesc->mDevice);
905f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
906f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    param.addInt(String8(AudioParameter::keyInputSource), (int)inputDesc->mInputSource);
9076a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("AudioPolicyManager::startInput() input source = %d", inputDesc->mInputSource);
908f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
909f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mpClientInterface->setParameters(input, param.toString());
910f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
911f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    inputDesc->mRefCount = 1;
912f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
913f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
914f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
915f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::stopInput(audio_io_handle_t input)
916f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
9176a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("stopInput() input %d", input);
918f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mInputs.indexOfKey(input);
919f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
92064cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("stopInput() unknow input %d", input);
921f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
922f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
923f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioInputDescriptor *inputDesc = mInputs.valueAt(index);
924f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
925f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (inputDesc->mRefCount == 0) {
92664cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("stopInput() input %d already stopped", input);
927f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return INVALID_OPERATION;
928f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
929f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioParameter param = AudioParameter();
930f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        param.addInt(String8(AudioParameter::keyRouting), 0);
931f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mpClientInterface->setParameters(input, param.toString());
932f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        inputDesc->mRefCount = 0;
933f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return NO_ERROR;
934f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
935f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
936f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
937f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::releaseInput(audio_io_handle_t input)
938f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
9396a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("releaseInput() %d", input);
940f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mInputs.indexOfKey(input);
941f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
94264cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("releaseInput() releasing unknown input %d", input);
943f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
944f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
945f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mpClientInterface->closeInput(input);
946f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    delete mInputs.valueAt(index);
947f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mInputs.removeItem(input);
9486a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("releaseInput() exit");
949f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
950f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
951f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::initStreamVolume(AudioSystem::stream_type stream,
952f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            int indexMin,
953f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            int indexMax)
954f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
9556a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
956f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (indexMin < 0 || indexMin >= indexMax) {
95764cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("initStreamVolume() invalid index limits for stream %d, min %d, max %d", stream , indexMin, indexMax);
958f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
959f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
960f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mStreams[stream].mIndexMin = indexMin;
961f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mStreams[stream].mIndexMax = indexMax;
962f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
963f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
964c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentstatus_t AudioPolicyManagerBase::setStreamVolumeIndex(AudioSystem::stream_type stream,
965c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                      int index,
966c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                      audio_devices_t device)
967f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
968f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
969f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if ((index < mStreams[stream].mIndexMin) || (index > mStreams[stream].mIndexMax)) {
970f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
971f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
972c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    if (!audio_is_output_device(device)) {
973c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        return BAD_VALUE;
974c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    }
975f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
976f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // Force max volume if stream cannot be muted
977f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (!mStreams[stream].mCanBeMuted) index = mStreams[stream].mIndexMax;
978f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
979b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    ALOGV("setStreamVolumeIndex() stream %d, device %04x, index %d",
980c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent          stream, device, index);
981c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
982c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    // if device is AUDIO_DEVICE_OUT_DEFAULT set default value and
983c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    // clear all device specific values
984c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    if (device == AUDIO_DEVICE_OUT_DEFAULT) {
985c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        mStreams[stream].mIndexCur.clear();
986c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    }
987c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    mStreams[stream].mIndexCur.add(device, index);
988f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
989f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // compute and apply stream volume on all outputs according to connected device
990f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    status_t status = NO_ERROR;
991f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mOutputs.size(); i++) {
992c5eb8b4a5d4395ce335bc7c3e6df2678fa47e2ddEric Laurent        audio_devices_t curDevice =
993c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                getDeviceForVolume(mOutputs.valueAt(i)->device());
994c5eb8b4a5d4395ce335bc7c3e6df2678fa47e2ddEric Laurent        if (device == curDevice) {
995c5eb8b4a5d4395ce335bc7c3e6df2678fa47e2ddEric Laurent            status_t volStatus = checkAndSetVolume(stream, index, mOutputs.keyAt(i), curDevice);
996c5eb8b4a5d4395ce335bc7c3e6df2678fa47e2ddEric Laurent            if (volStatus != NO_ERROR) {
997c5eb8b4a5d4395ce335bc7c3e6df2678fa47e2ddEric Laurent                status = volStatus;
998c5eb8b4a5d4395ce335bc7c3e6df2678fa47e2ddEric Laurent            }
999f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1000f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1001f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return status;
1002f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1003f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1004c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentstatus_t AudioPolicyManagerBase::getStreamVolumeIndex(AudioSystem::stream_type stream,
1005c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                      int *index,
1006c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                      audio_devices_t device)
1007f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1008c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    if (index == NULL) {
1009f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
1010f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1011c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    if (!audio_is_output_device(device)) {
1012c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        return BAD_VALUE;
1013c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    }
1014c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    // if device is AUDIO_DEVICE_OUT_DEFAULT, return volume for device corresponding to
1015c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    // the strategy the stream belongs to.
1016c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    if (device == AUDIO_DEVICE_OUT_DEFAULT) {
1017c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
1018c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    }
1019c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    device = getDeviceForVolume(device);
1020c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
1021c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    *index =  mStreams[stream].getVolumeIndex(device);
1022c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index);
1023f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
1024f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1025f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1026c94dccc97cc3ed5171b45f46a0f7f8762d37156fGlenn Kastenaudio_io_handle_t AudioPolicyManagerBase::getOutputForEffect(const effect_descriptor_t *desc)
1027f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
10286a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("getOutputForEffect()");
1029f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // apply simple rule where global effects are attached to the same output as MUSIC streams
10304660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen
10314660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen    routing_strategy strategy = getStrategy(AudioSystem::MUSIC);
10324660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen    audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
1033c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs);
10344660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen    int outIdx = 0;
10354660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen    for (size_t i = 0; i < dstOutputs.size(); i++) {
10364660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen        AudioOutputDescriptor *desc = mOutputs.valueFor(dstOutputs[i]);
10374660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen        if (desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
10384660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen            outIdx = i;
10394660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen        }
10404660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen    }
10414660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen    return dstOutputs[outIdx];
1042f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1043f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1044c94dccc97cc3ed5171b45f46a0f7f8762d37156fGlenn Kastenstatus_t AudioPolicyManagerBase::registerEffect(const effect_descriptor_t *desc,
10451c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent                                audio_io_handle_t io,
1046f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                uint32_t strategy,
1047f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                int session,
1048f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                int id)
1049f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
10501c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent    ssize_t index = mOutputs.indexOfKey(io);
1051f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
10521c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent        index = mInputs.indexOfKey(io);
10531c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent        if (index < 0) {
105464cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("registerEffect() unknown io %d", io);
10551c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent            return INVALID_OPERATION;
10561c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent        }
1057f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1058f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1059f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mTotalEffectsMemory + desc->memoryUsage > getMaxEffectsMemory()) {
106064cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("registerEffect() memory limit exceeded for Fx %s, Memory %d KB",
1061f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                desc->name, desc->memoryUsage);
1062f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return INVALID_OPERATION;
1063f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1064f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mTotalEffectsMemory += desc->memoryUsage;
10656a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("registerEffect() effect %s, io %d, strategy %d session %d id %d",
10661c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent            desc->name, io, strategy, session, id);
10676a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("registerEffect() memory %d, total memory %d", desc->memoryUsage, mTotalEffectsMemory);
1068f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1069f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    EffectDescriptor *pDesc = new EffectDescriptor();
1070f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    memcpy (&pDesc->mDesc, desc, sizeof(effect_descriptor_t));
10711c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent    pDesc->mIo = io;
1072f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    pDesc->mStrategy = (routing_strategy)strategy;
1073f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    pDesc->mSession = session;
1074582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    pDesc->mEnabled = false;
1075582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent
1076f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mEffects.add(id, pDesc);
1077f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1078f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
1079f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1080f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1081f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::unregisterEffect(int id)
1082f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1083f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mEffects.indexOfKey(id);
1084f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
108564cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("unregisterEffect() unknown effect ID %d", id);
1086f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return INVALID_OPERATION;
1087f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1088f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1089f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    EffectDescriptor *pDesc = mEffects.valueAt(index);
1090f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1091582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    setEffectEnabled(pDesc, false);
1092582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent
1093f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mTotalEffectsMemory < pDesc->mDesc.memoryUsage) {
109464cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("unregisterEffect() memory %d too big for total %d",
1095f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                pDesc->mDesc.memoryUsage, mTotalEffectsMemory);
1096f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        pDesc->mDesc.memoryUsage = mTotalEffectsMemory;
1097f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1098f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mTotalEffectsMemory -= pDesc->mDesc.memoryUsage;
10996a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("unregisterEffect() effect %s, ID %d, memory %d total memory %d",
1100582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent            pDesc->mDesc.name, id, pDesc->mDesc.memoryUsage, mTotalEffectsMemory);
1101f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1102f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mEffects.removeItem(id);
1103f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    delete pDesc;
1104f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1105f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
1106f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1107f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1108582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurentstatus_t AudioPolicyManagerBase::setEffectEnabled(int id, bool enabled)
1109582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent{
1110582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    ssize_t index = mEffects.indexOfKey(id);
1111582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    if (index < 0) {
111264cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("unregisterEffect() unknown effect ID %d", id);
1113582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        return INVALID_OPERATION;
1114582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    }
1115582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent
1116582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    return setEffectEnabled(mEffects.valueAt(index), enabled);
1117582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent}
1118582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent
1119582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurentstatus_t AudioPolicyManagerBase::setEffectEnabled(EffectDescriptor *pDesc, bool enabled)
1120582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent{
1121582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    if (enabled == pDesc->mEnabled) {
11226a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("setEffectEnabled(%s) effect already %s",
1123582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent             enabled?"true":"false", enabled?"enabled":"disabled");
1124582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        return INVALID_OPERATION;
1125582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    }
1126582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent
1127582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    if (enabled) {
1128582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        if (mTotalEffectsCpuLoad + pDesc->mDesc.cpuLoad > getMaxEffectsCpuLoad()) {
112964cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("setEffectEnabled(true) CPU Load limit exceeded for Fx %s, CPU %f MIPS",
1130582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent                 pDesc->mDesc.name, (float)pDesc->mDesc.cpuLoad/10);
1131582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent            return INVALID_OPERATION;
1132582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        }
1133582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        mTotalEffectsCpuLoad += pDesc->mDesc.cpuLoad;
11346a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("setEffectEnabled(true) total CPU %d", mTotalEffectsCpuLoad);
1135582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    } else {
1136582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        if (mTotalEffectsCpuLoad < pDesc->mDesc.cpuLoad) {
113764cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("setEffectEnabled(false) CPU load %d too high for total %d",
1138582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent                    pDesc->mDesc.cpuLoad, mTotalEffectsCpuLoad);
1139582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent            pDesc->mDesc.cpuLoad = mTotalEffectsCpuLoad;
1140582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        }
1141582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        mTotalEffectsCpuLoad -= pDesc->mDesc.cpuLoad;
11426a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("setEffectEnabled(false) total CPU %d", mTotalEffectsCpuLoad);
1143582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    }
1144582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    pDesc->mEnabled = enabled;
1145582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    return NO_ERROR;
1146582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent}
1147582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent
1148f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinbool AudioPolicyManagerBase::isStreamActive(int stream, uint32_t inPastMs) const
1149f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1150f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    nsecs_t sysTime = systemTime();
1151f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mOutputs.size(); i++) {
1152f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (mOutputs.valueAt(i)->mRefCount[stream] != 0 ||
1153f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            ns2ms(sysTime - mOutputs.valueAt(i)->mStopTime[stream]) < inPastMs) {
1154f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return true;
1155f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1156f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1157f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return false;
1158f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1159f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1160f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1161f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::dump(int fd)
1162f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1163f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    const size_t SIZE = 256;
1164f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    char buffer[SIZE];
1165f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    String8 result;
1166f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1167f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
1168f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1169b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
117070c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, " Primary Output: %d\n", mPrimaryOutput);
1171f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1172f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " A2DP device address: %s\n", mA2dpDeviceAddress.string());
1173f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1174f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " SCO device address: %s\n", mScoDeviceAddress.string());
1175f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1176599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    snprintf(buffer, SIZE, " USB audio ALSA %s\n", mUsbCardAndDevice.string());
1177599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    result.append(buffer);
1178f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Output devices: %08x\n", mAvailableOutputDevices);
1179f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1180f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Input devices: %08x\n", mAvailableInputDevices);
1181f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1182f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Phone state: %d\n", mPhoneState);
1183f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1184f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Force use for communications %d\n", mForceUse[AudioSystem::FOR_COMMUNICATION]);
1185f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1186f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Force use for media %d\n", mForceUse[AudioSystem::FOR_MEDIA]);
1187f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1188f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Force use for record %d\n", mForceUse[AudioSystem::FOR_RECORD]);
1189f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1190f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Force use for dock %d\n", mForceUse[AudioSystem::FOR_DOCK]);
1191f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1192f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, result.string(), result.size());
1193f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
11945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
119570c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "\nHW Modules dump:\n");
11965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    write(fd, buffer, strlen(buffer));
119770c236c9290732782d5267935af1475b8d5ae602Eric Laurent    for (size_t i = 0; i < mHwModules.size(); i++) {
119870c236c9290732782d5267935af1475b8d5ae602Eric Laurent        snprintf(buffer, SIZE, "- HW Module %d:\n", i + 1);
11995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        write(fd, buffer, strlen(buffer));
120070c236c9290732782d5267935af1475b8d5ae602Eric Laurent        mHwModules[i]->dump(fd);
12015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
12025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
1203f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "\nOutputs dump:\n");
1204f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
1205f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mOutputs.size(); i++) {
1206f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        snprintf(buffer, SIZE, "- Output %d dump:\n", mOutputs.keyAt(i));
1207f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        write(fd, buffer, strlen(buffer));
1208f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mOutputs.valueAt(i)->dump(fd);
1209f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1210f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1211f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "\nInputs dump:\n");
1212f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
1213f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mInputs.size(); i++) {
1214f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        snprintf(buffer, SIZE, "- Input %d dump:\n", mInputs.keyAt(i));
1215f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        write(fd, buffer, strlen(buffer));
1216f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mInputs.valueAt(i)->dump(fd);
1217f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1218f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1219f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "\nStreams dump:\n");
1220f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
1221c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    snprintf(buffer, SIZE,
1222c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent             " Stream  Can be muted  Index Min  Index Max  Index Cur [device : index]...\n");
1223f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
1224f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) {
1225c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        snprintf(buffer, SIZE, " %02d      ", i);
1226f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        write(fd, buffer, strlen(buffer));
1227c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        mStreams[i].dump(fd);
1228f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1229f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1230f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "\nTotal Effects CPU: %f MIPS, Total Effects memory: %d KB\n",
1231f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            (float)mTotalEffectsCpuLoad/10, mTotalEffectsMemory);
1232f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
1233f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1234f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "Registered effects:\n");
1235f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
1236f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mEffects.size(); i++) {
1237f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        snprintf(buffer, SIZE, "- Effect %d dump:\n", mEffects.keyAt(i));
1238f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        write(fd, buffer, strlen(buffer));
1239f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mEffects.valueAt(i)->dump(fd);
1240f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1241f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1242f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1243f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
1244f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1245f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1246f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// ----------------------------------------------------------------------------
1247f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// AudioPolicyManagerBase
1248f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// ----------------------------------------------------------------------------
1249f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1250f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima ZavinAudioPolicyManagerBase::AudioPolicyManagerBase(AudioPolicyClientInterface *clientInterface)
1251f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    :
1252f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
1253f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    Thread(false),
1254f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
125570c236c9290732782d5267935af1475b8d5ae602Eric Laurent    mPrimaryOutput((audio_io_handle_t)0),
1256ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    mAvailableOutputDevices(AUDIO_DEVICE_NONE),
1257ca0657a1ca087a6d474a75fcfedd6aac3901d587Glenn Kasten    mPhoneState(AudioSystem::MODE_NORMAL),
1258f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
1259f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mTotalEffectsCpuLoad(0), mTotalEffectsMemory(0),
126048387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi    mA2dpSuspended(false), mHasA2dp(false), mHasUsb(false), mHasRemoteSubmix(false)
1261f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1262f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mpClientInterface = clientInterface;
1263f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1264f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < AudioSystem::NUM_FORCE_USE; i++) {
1265f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mForceUse[i] = AudioSystem::FORCE_NONE;
1266f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1267f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1268f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    initializeVolumeCurves();
1269f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1270f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mA2dpDeviceAddress = String8("");
1271f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mScoDeviceAddress = String8("");
1272599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    mUsbCardAndDevice = String8("");
1273f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
12745ec145df7708564d385fd3fb764085321cf4c253Dima Zavin    if (loadAudioPolicyConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE) != NO_ERROR) {
12755ec145df7708564d385fd3fb764085321cf4c253Dima Zavin        if (loadAudioPolicyConfig(AUDIO_POLICY_CONFIG_FILE) != NO_ERROR) {
1276739022f26a7127ba76a98dda65411496086114a7Dima Zavin            ALOGE("could not load audio policy configuration file, setting defaults");
1277739022f26a7127ba76a98dda65411496086114a7Dima Zavin            defaultAudioPolicyConfig();
12785ec145df7708564d385fd3fb764085321cf4c253Dima Zavin        }
12795ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
12805ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
1281b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    // open all output streams needed to access attached devices
128270c236c9290732782d5267935af1475b8d5ae602Eric Laurent    for (size_t i = 0; i < mHwModules.size(); i++) {
128370c236c9290732782d5267935af1475b8d5ae602Eric Laurent        mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->mName);
128470c236c9290732782d5267935af1475b8d5ae602Eric Laurent        if (mHwModules[i]->mHandle == 0) {
128570c236c9290732782d5267935af1475b8d5ae602Eric Laurent            ALOGW("could not open HW module %s", mHwModules[i]->mName);
128670c236c9290732782d5267935af1475b8d5ae602Eric Laurent            continue;
128770c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
128870c236c9290732782d5267935af1475b8d5ae602Eric Laurent        // open all output streams needed to access attached devices
128970c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
129070c236c9290732782d5267935af1475b8d5ae602Eric Laurent        {
129170c236c9290732782d5267935af1475b8d5ae602Eric Laurent            const IOProfile *outProfile = mHwModules[i]->mOutputProfiles[j];
129270c236c9290732782d5267935af1475b8d5ae602Eric Laurent
129370c236c9290732782d5267935af1475b8d5ae602Eric Laurent            if (outProfile->mSupportedDevices & mAttachedOutputDevices) {
129470c236c9290732782d5267935af1475b8d5ae602Eric Laurent                AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(outProfile);
129570c236c9290732782d5267935af1475b8d5ae602Eric Laurent                outputDesc->mDevice = (audio_devices_t)(mDefaultOutputDevice &
129670c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                            outProfile->mSupportedDevices);
129770c236c9290732782d5267935af1475b8d5ae602Eric Laurent                audio_io_handle_t output = mpClientInterface->openOutput(
129870c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                outProfile->mModule->mHandle,
129970c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mDevice,
130070c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mSamplingRate,
130170c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mFormat,
130270c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mChannelMask,
130370c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mLatency,
130470c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                outputDesc->mFlags);
130570c236c9290732782d5267935af1475b8d5ae602Eric Laurent                if (output == 0) {
130670c236c9290732782d5267935af1475b8d5ae602Eric Laurent                    delete outputDesc;
130770c236c9290732782d5267935af1475b8d5ae602Eric Laurent                } else {
130870c236c9290732782d5267935af1475b8d5ae602Eric Laurent                    mAvailableOutputDevices = (audio_devices_t)(mAvailableOutputDevices |
130970c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                            (outProfile->mSupportedDevices & mAttachedOutputDevices));
131070c236c9290732782d5267935af1475b8d5ae602Eric Laurent                    if (mPrimaryOutput == 0 &&
13110977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent                            outProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
131270c236c9290732782d5267935af1475b8d5ae602Eric Laurent                        mPrimaryOutput = output;
131370c236c9290732782d5267935af1475b8d5ae602Eric Laurent                    }
131470c236c9290732782d5267935af1475b8d5ae602Eric Laurent                    addOutput(output, outputDesc);
131570c236c9290732782d5267935af1475b8d5ae602Eric Laurent                    setOutputDevice(output,
131670c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                    (audio_devices_t)(mDefaultOutputDevice &
131770c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                        outProfile->mSupportedDevices),
131870c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                    true);
1319b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                }
1320b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            }
1321b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        }
1322f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1323f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
13245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGE_IF((mAttachedOutputDevices & ~mAvailableOutputDevices),
1325b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent             "Not output found for attached devices %08x",
13265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent             (mAttachedOutputDevices & ~mAvailableOutputDevices));
1327b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1328b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output");
1329b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1330c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    updateDevicesAndOutputs();
13313cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
1332f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
1333b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (mPrimaryOutput != 0) {
1334f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioParameter outputCmd = AudioParameter();
1335f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputCmd.addInt(String8("set_id"), 0);
1336b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        mpClientInterface->setParameters(mPrimaryOutput, outputCmd.toString());
1337f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1338c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        mTestDevice = AUDIO_DEVICE_OUT_SPEAKER;
1339f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mTestSamplingRate = 44100;
1340f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mTestFormat = AudioSystem::PCM_16_BIT;
1341f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mTestChannels =  AudioSystem::CHANNEL_OUT_STEREO;
1342f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mTestLatencyMs = 0;
1343f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mCurOutput = 0;
1344f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mDirectOutput = false;
1345f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
1346f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mTestOutputs[i] = 0;
1347f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1348f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1349f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        const size_t SIZE = 256;
1350f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        char buffer[SIZE];
1351f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        snprintf(buffer, SIZE, "AudioPolicyManagerTest");
1352f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        run(buffer, ANDROID_PRIORITY_AUDIO);
1353f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1354f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
1355f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1356f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1357f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima ZavinAudioPolicyManagerBase::~AudioPolicyManagerBase()
1358f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1359f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
1360f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    exit();
1361f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
1362f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin   for (size_t i = 0; i < mOutputs.size(); i++) {
1363f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mpClientInterface->closeOutput(mOutputs.keyAt(i));
1364f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        delete mOutputs.valueAt(i);
1365f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin   }
1366f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin   for (size_t i = 0; i < mInputs.size(); i++) {
1367f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mpClientInterface->closeInput(mInputs.keyAt(i));
1368f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        delete mInputs.valueAt(i);
1369f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin   }
137070c236c9290732782d5267935af1475b8d5ae602Eric Laurent   for (size_t i = 0; i < mHwModules.size(); i++) {
137170c236c9290732782d5267935af1475b8d5ae602Eric Laurent        delete mHwModules[i];
13725ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent   }
1373f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1374f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1375f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::initCheck()
1376f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1377b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    return (mPrimaryOutput == 0) ? NO_INIT : NO_ERROR;
1378f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1379f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1380f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
1381f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinbool AudioPolicyManagerBase::threadLoop()
1382f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
13836a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("entering threadLoop()");
1384f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    while (!exitPending())
1385f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    {
1386f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        String8 command;
1387f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        int valueInt;
1388f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        String8 value;
1389f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1390f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        Mutex::Autolock _l(mLock);
1391f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mWaitWorkCV.waitRelative(mLock, milliseconds(50));
1392f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1393f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        command = mpClientInterface->getParameters(0, String8("test_cmd_policy"));
1394f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioParameter param = AudioParameter(command);
1395f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1396f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR &&
1397f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            valueInt != 0) {
13986a70518b93928d1c91457ff805e375c82d76b0e5Steve Block            ALOGV("Test command %s received", command.string());
1399f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            String8 target;
1400f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.get(String8("target"), target) != NO_ERROR) {
1401f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                target = "Manager";
1402f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1403f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) {
1404f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_output"));
1405f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mCurOutput = valueInt;
1406f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1407f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) {
1408f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_direct"));
1409f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (value == "false") {
1410f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    mDirectOutput = false;
1411f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else if (value == "true") {
1412f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    mDirectOutput = true;
1413f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1414f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1415f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) {
1416f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_input"));
1417f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mTestInput = valueInt;
1418f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1419f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1420f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) {
1421f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_format"));
1422f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                int format = AudioSystem::INVALID_FORMAT;
1423f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (value == "PCM 16 bits") {
1424f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    format = AudioSystem::PCM_16_BIT;
1425f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else if (value == "PCM 8 bits") {
1426f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    format = AudioSystem::PCM_8_BIT;
1427f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else if (value == "Compressed MP3") {
1428f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    format = AudioSystem::MP3;
1429f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1430f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (format != AudioSystem::INVALID_FORMAT) {
1431f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    if (target == "Manager") {
1432f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mTestFormat = format;
1433f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    } else if (mTestOutputs[mCurOutput] != 0) {
1434f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        AudioParameter outputParam = AudioParameter();
1435f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        outputParam.addInt(String8("format"), format);
1436f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
1437f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    }
1438f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1439f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1440f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) {
1441f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_channels"));
1442f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                int channels = 0;
1443f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1444f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (value == "Channels Stereo") {
1445f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    channels =  AudioSystem::CHANNEL_OUT_STEREO;
1446f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else if (value == "Channels Mono") {
1447f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    channels =  AudioSystem::CHANNEL_OUT_MONO;
1448f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1449f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (channels != 0) {
1450f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    if (target == "Manager") {
1451f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mTestChannels = channels;
1452f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    } else if (mTestOutputs[mCurOutput] != 0) {
1453f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        AudioParameter outputParam = AudioParameter();
1454f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        outputParam.addInt(String8("channels"), channels);
1455f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
1456f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    }
1457f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1458f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1459f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) {
1460f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_sampleRate"));
1461f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (valueInt >= 0 && valueInt <= 96000) {
1462f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    int samplingRate = valueInt;
1463f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    if (target == "Manager") {
1464f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mTestSamplingRate = samplingRate;
1465f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    } else if (mTestOutputs[mCurOutput] != 0) {
1466f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        AudioParameter outputParam = AudioParameter();
1467f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        outputParam.addInt(String8("sampling_rate"), samplingRate);
1468f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
1469f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    }
1470f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1471f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1472f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1473f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) {
1474f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_reopen"));
1475f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
147670c236c9290732782d5267935af1475b8d5ae602Eric Laurent                AudioOutputDescriptor *outputDesc = mOutputs.valueFor(mPrimaryOutput);
1477b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                mpClientInterface->closeOutput(mPrimaryOutput);
147870c236c9290732782d5267935af1475b8d5ae602Eric Laurent
147970c236c9290732782d5267935af1475b8d5ae602Eric Laurent                audio_module_handle_t moduleHandle = outputDesc->mModule->mHandle;
148070c236c9290732782d5267935af1475b8d5ae602Eric Laurent
1481b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                delete mOutputs.valueFor(mPrimaryOutput);
1482b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                mOutputs.removeItem(mPrimaryOutput);
1483f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1484b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(NULL);
1485c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER;
148670c236c9290732782d5267935af1475b8d5ae602Eric Laurent                mPrimaryOutput = mpClientInterface->openOutput(moduleHandle,
148770c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mDevice,
1488f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                &outputDesc->mSamplingRate,
1489f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                &outputDesc->mFormat,
149070c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mChannelMask,
1491f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                &outputDesc->mLatency,
1492f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                outputDesc->mFlags);
1493b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                if (mPrimaryOutput == 0) {
14945efbd421e0029d6fc44b1cc65c0e5e0d85e5161fSteve Block                    ALOGE("Failed to reopen hardware output stream, samplingRate: %d, format %d, channels %d",
149570c236c9290732782d5267935af1475b8d5ae602Eric Laurent                            outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask);
1496f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else {
1497f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    AudioParameter outputCmd = AudioParameter();
1498f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    outputCmd.addInt(String8("set_id"), 0);
1499b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                    mpClientInterface->setParameters(mPrimaryOutput, outputCmd.toString());
1500b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                    addOutput(mPrimaryOutput, outputDesc);
1501f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1502f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1503f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1504f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1505f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mpClientInterface->setParameters(0, String8("test_cmd_policy="));
1506f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1507f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1508f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return false;
1509f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1510f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1511f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::exit()
1512f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1513f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    {
1514f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AutoMutex _l(mLock);
1515f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        requestExit();
1516f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mWaitWorkCV.signal();
1517f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1518f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    requestExitAndWait();
1519f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1520f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1521f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinint AudioPolicyManagerBase::testOutputIndex(audio_io_handle_t output)
1522f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1523f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
1524f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (output == mTestOutputs[i]) return i;
1525f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1526f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return 0;
1527f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1528f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
1529f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1530f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// ---
1531f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1532f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::addOutput(audio_io_handle_t id, AudioOutputDescriptor *outputDesc)
1533f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1534f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    outputDesc->mId = id;
1535f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mOutputs.add(id, outputDesc);
1536f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1537f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1538f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
15393cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurentstatus_t AudioPolicyManagerBase::checkOutputsForDevice(audio_devices_t device,
15403cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                       AudioSystem::device_connection_state state,
15413cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                       SortedVector<audio_io_handle_t>& outputs)
1542f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
15433cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    AudioOutputDescriptor *desc;
1544b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1545b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (state == AudioSystem::DEVICE_STATE_AVAILABLE) {
15463cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        // first list already open outputs that can be routed to this device
1547b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        for (size_t i = 0; i < mOutputs.size(); i++) {
15483cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            desc = mOutputs.valueAt(i);
15493cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            if (!desc->isDuplicated() && (desc->mProfile->mSupportedDevices & device)) {
15503cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i));
15513cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                outputs.add(mOutputs.keyAt(i));
1552b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            }
1553b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        }
15543cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        // then look for output profiles that can be routed to this device
15553cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        SortedVector<IOProfile *> profiles;
155670c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (size_t i = 0; i < mHwModules.size(); i++)
1557b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        {
155870c236c9290732782d5267935af1475b8d5ae602Eric Laurent            if (mHwModules[i]->mHandle == 0) {
155970c236c9290732782d5267935af1475b8d5ae602Eric Laurent                continue;
156070c236c9290732782d5267935af1475b8d5ae602Eric Laurent            }
156170c236c9290732782d5267935af1475b8d5ae602Eric Laurent            for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
156270c236c9290732782d5267935af1475b8d5ae602Eric Laurent            {
156370c236c9290732782d5267935af1475b8d5ae602Eric Laurent                if (mHwModules[i]->mOutputProfiles[j]->mSupportedDevices & device) {
15643cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    ALOGV("checkOutputsForDevice(): adding profile %d from module %d", j, i);
15653cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    profiles.add(mHwModules[i]->mOutputProfiles[j]);
156670c236c9290732782d5267935af1475b8d5ae602Eric Laurent                }
156770c236c9290732782d5267935af1475b8d5ae602Eric Laurent            }
1568b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        }
156970c236c9290732782d5267935af1475b8d5ae602Eric Laurent
15703cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        if (profiles.isEmpty() && outputs.isEmpty()) {
15713cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
15723cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            return BAD_VALUE;
157370c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
157470c236c9290732782d5267935af1475b8d5ae602Eric Laurent
15753cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        // open outputs for matching profiles if needed. Direct outputs are also opened to
15763cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        // query for dynamic parameters and will be closed later by setDeviceConnectionState()
15773cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
15783cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            IOProfile *profile = profiles[profile_index];
1579b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
15803cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            // nothing to do if one output is already opened for this profile
15813cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            size_t j;
15823cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            for (j = 0; j < outputs.size(); j++) {
15833cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                desc = mOutputs.valueAt(j);
15843cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                if (!desc->isDuplicated() && desc->mProfile == profile) {
15853cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    break;
15863cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                }
15873cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
15883cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            if (j != outputs.size()) {
15893cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                continue;
15903cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
15913cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
15923cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            ALOGV("opening output for device %08x", device);
15933cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            desc = new AudioOutputDescriptor(profile);
15943cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            desc->mDevice = device;
15953cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            audio_io_handle_t output = mpClientInterface->openOutput(profile->mModule->mHandle,
15963cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                       &desc->mDevice,
15973cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                       &desc->mSamplingRate,
15983cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                       &desc->mFormat,
15993cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                       &desc->mChannelMask,
16003cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                       &desc->mLatency,
16013cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                       desc->mFlags);
16023cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            if (output != 0) {
16033cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                if (desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) {
16043cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    String8 reply;
16053cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    char *value;
16063cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    if (profile->mSamplingRates[0] == 0) {
16073cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        reply = mpClientInterface->getParameters(output,
16083cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES));
16093cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        ALOGV("checkOutputsForDevice() direct output sup sampling rates %s",
16103cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                  reply.string());
16113cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        value = strpbrk((char *)reply.string(), "=");
16123cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        if (value != NULL) {
16133cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                            loadSamplingRates(value, profile);
16143cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        }
16153cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
16163cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    if (profile->mFormats[0] == 0) {
16173cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        reply = mpClientInterface->getParameters(output,
16183cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                       String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
16193cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        ALOGV("checkOutputsForDevice() direct output sup formats %s",
16203cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                  reply.string());
16213cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        value = strpbrk((char *)reply.string(), "=");
16223cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        if (value != NULL) {
16233cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                            loadFormats(value, profile);
16243cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        }
16253cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
16263cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    if (profile->mChannelMasks[0] == 0) {
16273cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        reply = mpClientInterface->getParameters(output,
16283cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                      String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS));
16293cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        ALOGV("checkOutputsForDevice() direct output sup channel masks %s",
16303cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                  reply.string());
16313cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        value = strpbrk((char *)reply.string(), "=");
16323cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        if (value != NULL) {
16333cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                            loadOutChannels(value + 1, profile);
16343cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        }
16353cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
16363cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    if (((profile->mSamplingRates[0] == 0) &&
16373cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                             (profile->mSamplingRates.size() < 2)) ||
16383cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                         ((profile->mFormats[0] == 0) &&
16393cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                             (profile->mFormats.size() < 2)) ||
16403cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                         ((profile->mFormats[0] == 0) &&
16413cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                             (profile->mChannelMasks.size() < 2))) {
16423cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        ALOGW("checkOutputsForDevice() direct output missing param");
16433cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        output = 0;
16443cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    } else {
16453cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        addOutput(output, desc);
16463cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
16473cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                } else {
16483cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    audio_io_handle_t duplicatedOutput = 0;
16493cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    // add output descriptor
16503cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    addOutput(output, desc);
16513cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    // set initial stream volume for device
16524366b4a6735e5da342b56773073f0b41197c777fEric Laurent                    applyStreamVolumes(output, device, 0, true);
16533cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
16543cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    //TODO: configure audio effect output stage here
16553cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
16563cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    // open a duplicating output thread for the new output and the primary output
16573cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    duplicatedOutput = mpClientInterface->openDuplicateOutput(output,
16583cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                              mPrimaryOutput);
16593cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    if (duplicatedOutput != 0) {
16603cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        // add duplicated output descriptor
16613cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        AudioOutputDescriptor *dupOutputDesc = new AudioOutputDescriptor(NULL);
16623cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        dupOutputDesc->mOutput1 = mOutputs.valueFor(mPrimaryOutput);
16633cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        dupOutputDesc->mOutput2 = mOutputs.valueFor(output);
16643cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        dupOutputDesc->mSamplingRate = desc->mSamplingRate;
16653cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        dupOutputDesc->mFormat = desc->mFormat;
16663cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        dupOutputDesc->mChannelMask = desc->mChannelMask;
16673cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        dupOutputDesc->mLatency = desc->mLatency;
16683cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        addOutput(duplicatedOutput, dupOutputDesc);
16694366b4a6735e5da342b56773073f0b41197c777fEric Laurent                        applyStreamVolumes(duplicatedOutput, device, 0, true);
16703cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    } else {
16713cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        ALOGW("checkOutputsForDevice() could not open dup output for %d and %d",
16723cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                mPrimaryOutput, output);
16733cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        mpClientInterface->closeOutput(output);
16743cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        mOutputs.removeItem(output);
16753cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        output = 0;
16763cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
16773cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                }
16783cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
16793cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            if (output == 0) {
16803cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                ALOGW("checkOutputsForDevice() could not open output for device %x", device);
16813cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                delete desc;
16823cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                profiles.removeAt(profile_index);
16833cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                profile_index--;
1684b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            } else {
16853cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                outputs.add(output);
16863cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                ALOGV("checkOutputsForDevice(): adding output %d", output);
1687f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
16883cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        }
16893cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
16903cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        if (profiles.isEmpty()) {
16913cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
16923cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            return BAD_VALUE;
1693f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1694f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
1695b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        // check if one opened output is not needed any more after disconnecting one device
1696b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        for (size_t i = 0; i < mOutputs.size(); i++) {
16973cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            desc = mOutputs.valueAt(i);
16983cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            if (!desc->isDuplicated() &&
16993cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    !(desc->mProfile->mSupportedDevices & mAvailableOutputDevices)) {
17003cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                ALOGV("checkOutputsForDevice(): disconnecting adding output %d", mOutputs.keyAt(i));
17013cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                outputs.add(mOutputs.keyAt(i));
17023cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
17033cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        }
17043cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        for (size_t i = 0; i < mHwModules.size(); i++)
17053cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        {
17063cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            if (mHwModules[i]->mHandle == 0) {
17073cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                continue;
17083cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
17093cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
17103cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            {
17113cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                IOProfile *profile = mHwModules[i]->mOutputProfiles[j];
17123cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                if ((profile->mSupportedDevices & device) &&
17133cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        (profile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT)) {
17143cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    ALOGV("checkOutputsForDevice(): clearing direct output profile %d on module %d",
17153cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                          j, i);
17163cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    if (profile->mSamplingRates[0] == 0) {
17173cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        profile->mSamplingRates.clear();
17183cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        profile->mSamplingRates.add(0);
17193cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
17203cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    if (profile->mFormats[0] == 0) {
17213cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        profile->mFormats.clear();
17223cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        profile->mFormats.add((audio_format_t)0);
17233cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
17243cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    if (profile->mChannelMasks[0] == 0) {
17253cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        profile->mChannelMasks.clear();
17263cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        profile->mChannelMasks.add((audio_channel_mask_t)0);
17273cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
17283cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                }
1729b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            }
1730f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1731f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
17323cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    return NO_ERROR;
1733f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1734f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1735b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurentvoid AudioPolicyManagerBase::closeOutput(audio_io_handle_t output)
1736f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1737b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    ALOGV("closeOutput(%d)", output);
1738f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1739b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
1740b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (outputDesc == NULL) {
1741b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        ALOGW("closeOutput() unknown output %d", output);
1742b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        return;
1743f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1744f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1745b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    // look for duplicated outputs connected to the output being removed.
1746b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    for (size_t i = 0; i < mOutputs.size(); i++) {
1747b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        AudioOutputDescriptor *dupOutputDesc = mOutputs.valueAt(i);
1748b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        if (dupOutputDesc->isDuplicated() &&
1749b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                (dupOutputDesc->mOutput1 == outputDesc ||
1750b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                dupOutputDesc->mOutput2 == outputDesc)) {
1751b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            AudioOutputDescriptor *outputDesc2;
1752b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            if (dupOutputDesc->mOutput1 == outputDesc) {
1753b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                outputDesc2 = dupOutputDesc->mOutput2;
1754b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            } else {
1755b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                outputDesc2 = dupOutputDesc->mOutput1;
1756b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            }
1757b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            // As all active tracks on duplicated output will be deleted,
1758b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            // and as they were also referenced on the other output, the reference
1759b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            // count for their stream type must be adjusted accordingly on
1760b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            // the other output.
1761b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            for (int j = 0; j < (int)AudioSystem::NUM_STREAM_TYPES; j++) {
1762b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                int refCount = dupOutputDesc->mRefCount[j];
1763b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                outputDesc2->changeRefCount((AudioSystem::stream_type)j,-refCount);
1764b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            }
1765b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i);
1766b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput);
1767f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1768b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            mpClientInterface->closeOutput(duplicatedOutput);
1769b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            delete mOutputs.valueFor(duplicatedOutput);
1770b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            mOutputs.removeItem(duplicatedOutput);
1771f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1772f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1773b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1774b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    AudioParameter param;
1775b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    param.add(String8("closing"), String8("true"));
1776b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    mpClientInterface->setParameters(output, param.toString());
1777b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1778b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    mpClientInterface->closeOutput(output);
1779b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    delete mOutputs.valueFor(output);
1780b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    mOutputs.removeItem(output);
1781f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1782f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1783c952527e6f89d5427881462823514be9d79f13e6Eric LaurentSortedVector<audio_io_handle_t> AudioPolicyManagerBase::getOutputsForDevice(audio_devices_t device,
1784c952527e6f89d5427881462823514be9d79f13e6Eric Laurent                        DefaultKeyedVector<audio_io_handle_t, AudioOutputDescriptor *> openOutputs)
1785f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1786b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    SortedVector<audio_io_handle_t> outputs;
1787f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
17883cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGVV("getOutputsForDevice() device %04x", device);
1789c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    for (size_t i = 0; i < openOutputs.size(); i++) {
17903cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        ALOGVV("output %d isDuplicated=%d device=%04x",
1791c952527e6f89d5427881462823514be9d79f13e6Eric Laurent                i, openOutputs.valueAt(i)->isDuplicated(), openOutputs.valueAt(i)->supportedDevices());
1792c952527e6f89d5427881462823514be9d79f13e6Eric Laurent        if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) {
1793c952527e6f89d5427881462823514be9d79f13e6Eric Laurent            ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i));
1794c952527e6f89d5427881462823514be9d79f13e6Eric Laurent            outputs.add(openOutputs.keyAt(i));
1795f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1796f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1797b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    return outputs;
1798f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1799f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1800b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurentbool AudioPolicyManagerBase::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
1801b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                                   SortedVector<audio_io_handle_t>& outputs2)
1802f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1803b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (outputs1.size() != outputs2.size()) {
1804b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        return false;
1805f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1806b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    for (size_t i = 0; i < outputs1.size(); i++) {
1807b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        if (outputs1[i] != outputs2[i]) {
1808b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            return false;
1809f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1810f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1811b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    return true;
1812b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent}
1813b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1814b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurentvoid AudioPolicyManagerBase::checkOutputForStrategy(routing_strategy strategy)
1815b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent{
181601e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent    audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
181701e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent    audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
1818c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs);
1819c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
1820b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1821b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (!vectorsEqual(srcOutputs,dstOutputs)) {
1822b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d",
1823b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent              strategy, srcOutputs[0], dstOutputs[0]);
18245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // mute strategy while moving tracks from one output to another
18255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        for (size_t i = 0; i < srcOutputs.size(); i++) {
1826fa3697d716b444bbea6be480801536c44bf69214Eric Laurent            AudioOutputDescriptor *desc = mOutputs.valueFor(srcOutputs[i]);
1827fa3697d716b444bbea6be480801536c44bf69214Eric Laurent            if (desc->strategyRefCount(strategy) != 0) {
1828fa3697d716b444bbea6be480801536c44bf69214Eric Laurent                setStrategyMute(strategy, true, srcOutputs[i]);
1829fa3697d716b444bbea6be480801536c44bf69214Eric Laurent                setStrategyMute(strategy, false, srcOutputs[i], MUTE_TIME_MS, newDevice);
1830fa3697d716b444bbea6be480801536c44bf69214Eric Laurent            }
18315ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
1832f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1833f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // Move effects associated to this strategy from previous output to new output
18344660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen        if (strategy == STRATEGY_MEDIA) {
18354660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen            int outIdx = 0;
18364660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen            for (size_t i = 0; i < dstOutputs.size(); i++) {
18374660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                AudioOutputDescriptor *desc = mOutputs.valueFor(dstOutputs[i]);
18384660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                if (desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) {
18394660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                    outIdx = i;
18404660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                }
18414660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen            }
18424660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen            SortedVector<audio_io_handle_t> moved;
18434660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen            for (size_t i = 0; i < mEffects.size(); i++) {
18444660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                EffectDescriptor *desc = mEffects.valueAt(i);
18454660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                if (desc->mSession == AUDIO_SESSION_OUTPUT_MIX &&
18464660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                        desc->mIo != dstOutputs[outIdx]) {
18474660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                    if (moved.indexOf(desc->mIo) < 0) {
18484660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                        ALOGV("checkOutputForStrategy() moving effect %d to output %d",
18494660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                              mEffects.keyAt(i), dstOutputs[outIdx]);
18504660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                        mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, desc->mIo,
18514660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                                                       dstOutputs[outIdx]);
18524660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                        moved.add(desc->mIo);
18534660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                    }
18544660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                    desc->mIo = dstOutputs[outIdx];
18554660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                }
18564660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen            }
18574660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen        }
18585ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // Move tracks associated to this strategy from previous output to new output
1859f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        for (int i = 0; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) {
1860f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (getStrategy((AudioSystem::stream_type)i) == strategy) {
186112bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi                //FIXME see fixme on name change
18624660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                mpClientInterface->setStreamOutput((AudioSystem::stream_type)i,
18634660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                                                   dstOutputs[0] /* ignored */);
1864f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1865f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1866f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1867f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1868f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1869f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::checkOutputForAllStrategies()
1870f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1871c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
1872f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForStrategy(STRATEGY_PHONE);
1873f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForStrategy(STRATEGY_SONIFICATION);
187412bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
1875f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForStrategy(STRATEGY_MEDIA);
1876f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForStrategy(STRATEGY_DTMF);
1877f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1878f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1879b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurentaudio_io_handle_t AudioPolicyManagerBase::getA2dpOutput()
1880b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent{
18815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (!mHasA2dp) {
1882b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        return 0;
1883b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    }
1884b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1885b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    for (size_t i = 0; i < mOutputs.size(); i++) {
1886b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        AudioOutputDescriptor *outputDesc = mOutputs.valueAt(i);
1887b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        if (!outputDesc->isDuplicated() && outputDesc->device() & AUDIO_DEVICE_OUT_ALL_A2DP) {
1888b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            return mOutputs.keyAt(i);
1889b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        }
1890b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    }
1891b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1892b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    return 0;
1893b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent}
1894b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1895f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::checkA2dpSuspend()
1896f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
18975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (!mHasA2dp) {
1898b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        return;
1899b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    }
1900b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    audio_io_handle_t a2dpOutput = getA2dpOutput();
1901b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (a2dpOutput == 0) {
1902b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        return;
1903b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    }
1904b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1905f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // suspend A2DP output if:
1906f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      (NOT already suspended) &&
1907f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      ((SCO device is connected &&
1908f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //       (forced usage for communication || for record is SCO))) ||
1909f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      (phone state is ringing || in call)
1910f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //
1911f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // restore A2DP output if:
1912f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      (Already suspended) &&
1913f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      ((SCO device is NOT connected ||
1914f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //       (forced usage NOT for communication && NOT for record is SCO))) &&
1915f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      (phone state is NOT ringing && NOT in call)
1916f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //
1917f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mA2dpSuspended) {
1918f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (((mScoDeviceAddress == "") ||
1919f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin             ((mForceUse[AudioSystem::FOR_COMMUNICATION] != AudioSystem::FORCE_BT_SCO) &&
1920f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin              (mForceUse[AudioSystem::FOR_RECORD] != AudioSystem::FORCE_BT_SCO))) &&
1921f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin             ((mPhoneState != AudioSystem::MODE_IN_CALL) &&
1922f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin              (mPhoneState != AudioSystem::MODE_RINGTONE))) {
1923f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1924b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            mpClientInterface->restoreOutput(a2dpOutput);
1925f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mA2dpSuspended = false;
1926f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1927f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
1928f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (((mScoDeviceAddress != "") &&
1929f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin             ((mForceUse[AudioSystem::FOR_COMMUNICATION] == AudioSystem::FORCE_BT_SCO) ||
1930f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin              (mForceUse[AudioSystem::FOR_RECORD] == AudioSystem::FORCE_BT_SCO))) ||
1931f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin             ((mPhoneState == AudioSystem::MODE_IN_CALL) ||
1932f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin              (mPhoneState == AudioSystem::MODE_RINGTONE))) {
1933f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1934b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            mpClientInterface->suspendOutput(a2dpOutput);
1935f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mA2dpSuspended = true;
1936f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1937f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1938f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1939f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1940f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurentaudio_devices_t AudioPolicyManagerBase::getNewDevice(audio_io_handle_t output, bool fromCache)
1941f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1942ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    audio_devices_t device = AUDIO_DEVICE_NONE;
1943f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1944f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
1945f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // check the following by order of priority to request a routing change if necessary:
1946c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    // 1: the strategy enforced audible is active on the output:
1947c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    //      use device for strategy enforced audible
1948c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    // 2: we are in call or the strategy phone is active on the output:
1949f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      use device for strategy phone
1950c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    // 3: the strategy sonification is active on the output:
1951f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      use device for strategy sonification
195212bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    // 4: the strategy "respectful" sonification is active on the output:
195312bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    //      use device for strategy "respectful" sonification
195412bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    // 5: the strategy media is active on the output:
1955f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      use device for strategy media
195612bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    // 6: the strategy DTMF is active on the output:
1957f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      use device for strategy DTMF
1958c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    if (outputDesc->isUsedByStrategy(STRATEGY_ENFORCED_AUDIBLE)) {
1959c16ac09f510437e8340be691720177a490ae78f0Eric Laurent        device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
1960c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    } else if (isInCall() ||
1961c16ac09f510437e8340be691720177a490ae78f0Eric Laurent                    outputDesc->isUsedByStrategy(STRATEGY_PHONE)) {
1962f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
1963f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else if (outputDesc->isUsedByStrategy(STRATEGY_SONIFICATION)) {
1964f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
196512bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    } else if (outputDesc->isUsedByStrategy(STRATEGY_SONIFICATION_RESPECTFUL)) {
19665ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
1967f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else if (outputDesc->isUsedByStrategy(STRATEGY_MEDIA)) {
1968f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
1969f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else if (outputDesc->isUsedByStrategy(STRATEGY_DTMF)) {
1970f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
1971f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1972f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
19736a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("getNewDevice() selected device %x", device);
1974f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return device;
1975f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1976f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1977f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinuint32_t AudioPolicyManagerBase::getStrategyForStream(AudioSystem::stream_type stream) {
1978f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return (uint32_t)getStrategy(stream);
1979f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1980f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1981f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurentaudio_devices_t AudioPolicyManagerBase::getDevicesForStream(AudioSystem::stream_type stream) {
1982f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent    audio_devices_t devices;
1983f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // By checking the range of stream before calling getStrategy, we avoid
19845efbd421e0029d6fc44b1cc65c0e5e0d85e5161fSteve Block    // getStrategy's behavior for invalid streams.  getStrategy would do a ALOGE
1985f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // and then return STRATEGY_MEDIA, but we want to return the empty set.
1986f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (stream < (AudioSystem::stream_type) 0 || stream >= AudioSystem::NUM_STREAM_TYPES) {
1987ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        devices = AUDIO_DEVICE_NONE;
1988f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
1989f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioPolicyManagerBase::routing_strategy strategy = getStrategy(stream);
19905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        devices = getDeviceForStrategy(strategy, true /*fromCache*/);
1991f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1992f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return devices;
1993f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1994f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1995f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima ZavinAudioPolicyManagerBase::routing_strategy AudioPolicyManagerBase::getStrategy(
1996f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioSystem::stream_type stream) {
1997f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // stream to strategy mapping
1998f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    switch (stream) {
1999f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::VOICE_CALL:
2000f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::BLUETOOTH_SCO:
2001f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return STRATEGY_PHONE;
2002f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::RING:
2003f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::ALARM:
2004f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return STRATEGY_SONIFICATION;
200512bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    case AudioSystem::NOTIFICATION:
200612bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        return STRATEGY_SONIFICATION_RESPECTFUL;
2007f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::DTMF:
2008f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return STRATEGY_DTMF;
2009f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    default:
20105efbd421e0029d6fc44b1cc65c0e5e0d85e5161fSteve Block        ALOGE("unknown stream type");
2011f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::SYSTEM:
2012f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // NOTE: SYSTEM stream uses MEDIA strategy because muting music and switching outputs
2013f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // while key clicks are played produces a poor result
2014f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::TTS:
2015f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::MUSIC:
2016f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return STRATEGY_MEDIA;
2017c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    case AudioSystem::ENFORCED_AUDIBLE:
2018c16ac09f510437e8340be691720177a490ae78f0Eric Laurent        return STRATEGY_ENFORCED_AUDIBLE;
2019f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2020f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2021f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
202212bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivivoid AudioPolicyManagerBase::handleNotificationRoutingForStream(AudioSystem::stream_type stream) {
202312bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    switch(stream) {
202412bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    case AudioSystem::MUSIC:
202512bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
2026c952527e6f89d5427881462823514be9d79f13e6Eric Laurent        updateDevicesAndOutputs();
202712bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        break;
202812bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    default:
202912bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        break;
203012bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    }
203112bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi}
203212bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi
20335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentaudio_devices_t AudioPolicyManagerBase::getDeviceForStrategy(routing_strategy strategy,
20345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                             bool fromCache)
2035f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2036ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    uint32_t device = AUDIO_DEVICE_NONE;
2037f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2038f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (fromCache) {
20393cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
20405ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent              strategy, mDeviceForStrategy[strategy]);
2041f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return mDeviceForStrategy[strategy];
2042f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2043f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2044f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    switch (strategy) {
204512bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi
204612bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    case STRATEGY_SONIFICATION_RESPECTFUL:
204712bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        if (isInCall()) {
20485ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
204912bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        } else if (isStreamActive(AudioSystem::MUSIC, SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
205012bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi            // while media is playing (or has recently played), use the same device
20515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/);
205212bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        } else {
205312bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi            // when media is not playing anymore, fall back on the sonification behavior
20545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
205512bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        }
205612bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi
205712bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        break;
205812bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi
2059f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case STRATEGY_DTMF:
2060f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (!isInCall()) {
2061f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // when off call, DTMF strategy follows the same rules as MEDIA strategy
20625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/);
2063f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            break;
2064f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2065f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // when in call, DTMF and PHONE strategies follow the same rules
2066f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // FALL THROUGH
2067f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2068f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case STRATEGY_PHONE:
2069f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // for phone strategy, we first consider the forced use and then the available devices by order
2070f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // of priority
2071f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        switch (mForceUse[AudioSystem::FOR_COMMUNICATION]) {
2072f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        case AudioSystem::FORCE_BT_SCO:
2073f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (!isInCall() || strategy != STRATEGY_DTMF) {
2074c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT;
2075f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (device) break;
2076f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2077c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET;
2078f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (device) break;
2079c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO;
2080f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (device) break;
2081f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // if SCO device is requested but no SCO device is available, fall back to default case
2082f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // FALL THROUGH
2083f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2084f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        default:    // FORCE_NONE
2085f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to A2DP
20861afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent            if (mHasA2dp && !isInCall() &&
20871afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent                    (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
2088ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent                    (getA2dpOutput() != 0) && !mA2dpSuspended) {
2089c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
2090f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (device) break;
2091c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
2092f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (device) break;
2093f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2094c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
20951afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent            if (device) break;
2096c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADSET;
20971afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent            if (device) break;
2098599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
2099599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            if (device) break;
2100599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
2101599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            if (device) break;
2102c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
2103f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (device) break;
2104c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
210555ac23bc116ad9ae38266a5e660fd22f5104348eEric Laurent            if (device) break;
2106c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
2107f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (device) break;
2108c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_EARPIECE;
21095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            if (device) break;
21105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = mDefaultOutputDevice;
2111ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            if (device == AUDIO_DEVICE_NONE) {
21125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE");
2113f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2114f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            break;
2115f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2116f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        case AudioSystem::FORCE_SPEAKER:
2117f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to
2118f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // A2DP speaker when forcing to speaker output
21191afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent            if (mHasA2dp && !isInCall() &&
21201afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent                    (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
2121ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent                    (getA2dpOutput() != 0) && !mA2dpSuspended) {
2122c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
2123f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (device) break;
2124f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2125599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
2126599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            if (device) break;
2127599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
2128599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            if (device) break;
2129c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
2130f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (device) break;
2131c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
213255ac23bc116ad9ae38266a5e660fd22f5104348eEric Laurent            if (device) break;
2133c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
2134f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (device) break;
2135c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER;
21365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            if (device) break;
21375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = mDefaultOutputDevice;
2138ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            if (device == AUDIO_DEVICE_NONE) {
21395ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE, FORCE_SPEAKER");
2140f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2141f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            break;
2142f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2143f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    break;
2144f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2145f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case STRATEGY_SONIFICATION:
2146f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2147f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // If incall, just select the STRATEGY_PHONE device: The rest of the behavior is handled by
2148f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // handleIncallSonification().
2149f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (isInCall()) {
21505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = getDeviceForStrategy(STRATEGY_PHONE, false /*fromCache*/);
2151f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            break;
2152f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2153c16ac09f510437e8340be691720177a490ae78f0Eric Laurent        // FALL THROUGH
2154c16ac09f510437e8340be691720177a490ae78f0Eric Laurent
2155c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    case STRATEGY_ENFORCED_AUDIBLE:
2156c16ac09f510437e8340be691720177a490ae78f0Eric Laurent        // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION
2157ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        // except:
2158ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        //   - when in call where it doesn't default to STRATEGY_PHONE behavior
2159ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        //   - in countries where not enforced in which case it follows STRATEGY_MEDIA
2160c16ac09f510437e8340be691720177a490ae78f0Eric Laurent
2161ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        if (strategy == STRATEGY_SONIFICATION ||
2162ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent                !mStreams[AUDIO_STREAM_ENFORCED_AUDIBLE].mCanBeMuted) {
2163c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER;
2164ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            if (device == AUDIO_DEVICE_NONE) {
2165ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent                ALOGE("getDeviceForStrategy() speaker device not found for STRATEGY_SONIFICATION");
2166ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent            }
2167f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2168f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // The second device used for sonification is the same as the device used by media strategy
2169f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // FALL THROUGH
2170f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2171f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case STRATEGY_MEDIA: {
2172ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        uint32_t device2 = AUDIO_DEVICE_NONE;
217348387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi        if (mHasRemoteSubmix
217448387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi                && mForceUse[AudioSystem::FOR_MEDIA] == AudioSystem::FORCE_REMOTE_SUBMIX) {
217548387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
217648387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi        }
2177ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if ((device2 == AUDIO_DEVICE_NONE) &&
217848387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi                mHasA2dp && (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
2179ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent                (getA2dpOutput() != 0) && !mA2dpSuspended) {
2180c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
2181ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            if (device2 == AUDIO_DEVICE_NONE) {
2182c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
2183f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2184ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            if (device2 == AUDIO_DEVICE_NONE) {
2185c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
2186f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2187f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2188ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (device2 == AUDIO_DEVICE_NONE) {
2189c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
21901afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent        }
2191ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (device2 == AUDIO_DEVICE_NONE) {
2192c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADSET;
21931afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent        }
2194ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (device2 == AUDIO_DEVICE_NONE) {
2195599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
2196599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent        }
2197ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (device2 == AUDIO_DEVICE_NONE) {
2198599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
2199599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent        }
2200ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (device2 == AUDIO_DEVICE_NONE) {
2201c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
2202f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2203ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (device2 == AUDIO_DEVICE_NONE) {
2204c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
2205f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2206ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (device2 == AUDIO_DEVICE_NONE) {
2207c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
2208f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2209ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (device2 == AUDIO_DEVICE_NONE) {
2210c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER;
2211f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2212f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2213c16ac09f510437e8340be691720177a490ae78f0Eric Laurent        // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or
2214ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise
2215f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device |= device2;
22165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (device) break;
22175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        device = mDefaultOutputDevice;
2218ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (device == AUDIO_DEVICE_NONE) {
22195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGE("getDeviceForStrategy() no device found for STRATEGY_MEDIA");
2220f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2221f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        } break;
2222f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2223f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    default:
222464cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("getDeviceForStrategy() unknown strategy: %d", strategy);
2225f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
2226f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2227f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
22283cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGVV("getDeviceForStrategy() strategy %d, device %x", strategy, device);
2229c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    return device;
2230f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2231f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2232c952527e6f89d5427881462823514be9d79f13e6Eric Laurentvoid AudioPolicyManagerBase::updateDevicesAndOutputs()
2233f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2234f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < NUM_STRATEGIES; i++) {
22355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
22365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
2237c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    mPreviousOutputs = mOutputs;
22385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
22395ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
2240b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurentuint32_t AudioPolicyManagerBase::checkDeviceMuteStrategies(AudioOutputDescriptor *outputDesc,
22419029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                                                       audio_devices_t prevDevice,
22425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                       uint32_t delayMs)
22435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
22449029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    // mute/unmute strategies using an incompatible device combination
22459029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    // if muting, wait for the audio in pcm buffer to be drained before proceeding
22469029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    // if unmuting, unmute only after the specified delay
22475ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (outputDesc->isDuplicated()) {
2248b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        return 0;
22495ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
22505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
22515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    uint32_t muteWaitMs = 0;
22525ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    audio_devices_t device = outputDesc->device();
22535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    bool shouldMute = (outputDesc->refCount() != 0) &&
22545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                    (AudioSystem::popCount(device) >= 2);
22559029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    // temporary mute output if device selection changes to avoid volume bursts due to
22569029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    // different per device volumes
22579029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    bool tempMute = (outputDesc->refCount() != 0) && (device != prevDevice);
22585ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
22595ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < NUM_STRATEGIES; i++) {
22605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
22615ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        bool mute = shouldMute && (curDevice & device) && (curDevice != device);
22625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        bool doMute = false;
22635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
22645ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (mute && !outputDesc->mStrategyMutedByDevice[i]) {
22655ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            doMute = true;
22665ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            outputDesc->mStrategyMutedByDevice[i] = true;
22675ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){
22685ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            doMute = true;
22695ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            outputDesc->mStrategyMutedByDevice[i] = false;
22705ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
22719029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent        if (doMute || tempMute) {
22725ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            for (size_t j = 0; j < mOutputs.size(); j++) {
22735ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                AudioOutputDescriptor *desc = mOutputs.valueAt(j);
2274c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                if ((desc->supportedDevices() & outputDesc->supportedDevices())
2275ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent                        == AUDIO_DEVICE_NONE) {
22765ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                    continue;
22775ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                }
22785ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                audio_io_handle_t curOutput = mOutputs.keyAt(j);
22793cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x) on output %d",
22805ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                      mute ? "muting" : "unmuting", i, curDevice, curOutput);
22815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                setStrategyMute((routing_strategy)i, mute, curOutput, mute ? 0 : delayMs);
22829029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                if (desc->strategyRefCount((routing_strategy)i) != 0) {
22839029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                    if (tempMute) {
228401e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                        setStrategyMute((routing_strategy)i, true, curOutput);
228501e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                        setStrategyMute((routing_strategy)i, false, curOutput,
228601e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                            desc->latency() * 2, device);
22879029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                    }
22889029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                    if (tempMute || mute) {
22899029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                        if (muteWaitMs < desc->latency()) {
22909029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                            muteWaitMs = desc->latency();
22919029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                        }
22925ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                    }
22935ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                }
22945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
22955ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
22965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
22975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
22985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // FIXME: should not need to double latency if volume could be applied immediately by the
22995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // audioflinger mixer. We must account for the delay between now and the next time
23005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // the audioflinger thread for this output will process a buffer (which corresponds to
23015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // one buffer size, usually 1/2 or 1/4 of the latency).
23025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    muteWaitMs *= 2;
23035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // wait for the PCM output buffers to empty before proceeding with the rest of the command
23045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (muteWaitMs > delayMs) {
2305b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        muteWaitMs -= delayMs;
2306b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        usleep(muteWaitMs * 1000);
2307b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        return muteWaitMs;
2308f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2309b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent    return 0;
2310f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2311f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2312b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurentuint32_t AudioPolicyManagerBase::setOutputDevice(audio_io_handle_t output,
2313f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                                             audio_devices_t device,
2314f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                                             bool force,
2315f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                                             int delayMs)
2316f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
23175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGV("setOutputDevice() output %d device %04x delayMs %d", output, device, delayMs);
2318f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
23195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    AudioParameter param;
2320b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent    uint32_t muteWaitMs = 0;
2321f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2322f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (outputDesc->isDuplicated()) {
2323b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        muteWaitMs = setOutputDevice(outputDesc->mOutput1->mId, device, force, delayMs);
2324b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        muteWaitMs += setOutputDevice(outputDesc->mOutput2->mId, device, force, delayMs);
2325b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        return muteWaitMs;
2326f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2327f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // filter devices according to output selected
2328f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent    device = (audio_devices_t)(device & outputDesc->mProfile->mSupportedDevices);
2329f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
23305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    audio_devices_t prevDevice = outputDesc->mDevice;
23315ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
23325ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGV("setOutputDevice() prevDevice %04x", prevDevice);
23335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
2334ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if (device != AUDIO_DEVICE_NONE) {
23355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        outputDesc->mDevice = device;
23365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
23379029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs);
23385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
2339f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // Do not change the routing if:
2340ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    //  - the requested device is AUDIO_DEVICE_NONE
2341f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //  - the requested device is the same as current device and force is not specified.
2342f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // Doing this check here allows the caller to call setOutputDevice() without conditions
2343ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if ((device == AUDIO_DEVICE_NONE || device == prevDevice) && !force) {
23445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        ALOGV("setOutputDevice() setting same device %04x or null device for output %d", device, output);
2345b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        return muteWaitMs;
2346f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2347f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
23485ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGV("setOutputDevice() changing device");
2349f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // do the routing
2350f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    param.addInt(String8(AudioParameter::keyRouting), (int)device);
23515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    mpClientInterface->setParameters(output, param.toString(), delayMs);
23525ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
2353f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // update stream volumes according to new device
2354f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    applyStreamVolumes(output, device, delayMs);
2355b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent
2356b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent    return muteWaitMs;
23575ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
2358f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
23595ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric LaurentAudioPolicyManagerBase::IOProfile *AudioPolicyManagerBase::getInputProfile(audio_devices_t device,
23605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                   uint32_t samplingRate,
23615ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                   uint32_t format,
23625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                   uint32_t channelMask)
23635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
23645ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // Choose an input profile based on the requested capture parameters: select the first available
23655ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // profile supporting all requested parameters.
23665ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
236770c236c9290732782d5267935af1475b8d5ae602Eric Laurent    for (size_t i = 0; i < mHwModules.size(); i++)
23685ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    {
236970c236c9290732782d5267935af1475b8d5ae602Eric Laurent        if (mHwModules[i]->mHandle == 0) {
23705ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            continue;
23715ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
237270c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
23735ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        {
237470c236c9290732782d5267935af1475b8d5ae602Eric Laurent            IOProfile *profile = mHwModules[i]->mInputProfiles[j];
237570c236c9290732782d5267935af1475b8d5ae602Eric Laurent            if (profile->isCompatibleProfile(device, samplingRate, format,
23760977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent                                             channelMask,(audio_output_flags_t)0)) {
237770c236c9290732782d5267935af1475b8d5ae602Eric Laurent                return profile;
23785ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
23795ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
2380f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
238170c236c9290732782d5267935af1475b8d5ae602Eric Laurent    return NULL;
2382f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2383f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2384f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurentaudio_devices_t AudioPolicyManagerBase::getDeviceForInputSource(int inputSource)
2385f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2386ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    uint32_t device = AUDIO_DEVICE_NONE;
2387f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2388f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    switch(inputSource) {
2389f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_DEFAULT:
2390f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_MIC:
2391f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_RECOGNITION:
2392f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_COMMUNICATION:
2393f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (mForceUse[AudioSystem::FOR_RECORD] == AudioSystem::FORCE_BT_SCO &&
2394c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            mAvailableInputDevices & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
2395c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
2396c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_WIRED_HEADSET) {
2397c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = AUDIO_DEVICE_IN_WIRED_HEADSET;
2398c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_BUILTIN_MIC) {
2399c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = AUDIO_DEVICE_IN_BUILTIN_MIC;
2400f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2401f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
2402f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_CAMCORDER:
2403c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        if (mAvailableInputDevices & AUDIO_DEVICE_IN_BACK_MIC) {
2404c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = AUDIO_DEVICE_IN_BACK_MIC;
2405c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_BUILTIN_MIC) {
2406c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = AUDIO_DEVICE_IN_BUILTIN_MIC;
2407f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2408f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
2409f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_UPLINK:
2410f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_DOWNLINK:
2411f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_CALL:
2412c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        if (mAvailableInputDevices & AUDIO_DEVICE_IN_VOICE_CALL) {
2413c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = AUDIO_DEVICE_IN_VOICE_CALL;
24145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
2415f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
241648387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi    case AUDIO_SOURCE_REMOTE_SUBMIX:
241748387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi        if (mAvailableInputDevices & AUDIO_DEVICE_IN_REMOTE_SUBMIX) {
241848387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
241948387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi        }
242048387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi        break;
2421f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    default:
242264cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("getDeviceForInputSource() invalid input source %d", inputSource);
2423f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
2424f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
24256a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("getDeviceForInputSource()input source %d, device %08x", inputSource, device);
2426c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    return device;
2427f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2428f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
24296d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivibool AudioPolicyManagerBase::isVirtualInputDevice(audio_devices_t device)
24306d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi{
24316d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi    if ((device & AUDIO_DEVICE_BIT_IN) != 0) {
24326d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi        device &= ~AUDIO_DEVICE_BIT_IN;
24336d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi        if ((popcount(device) == 1) && ((device & ~APM_AUDIO_IN_DEVICE_VIRTUAL_ALL) == 0))
24346d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi            return true;
24356d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi    }
24366d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi    return false;
24376d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi}
24386d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi
24396d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Triviaudio_io_handle_t AudioPolicyManagerBase::getActiveInput(bool ignoreVirtualInputs)
2440f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2441f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mInputs.size(); i++) {
24426d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi        const AudioInputDescriptor * input_descriptor = mInputs.valueAt(i);
24436d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi        if ((input_descriptor->mRefCount > 0)
24446d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi                && (!ignoreVirtualInputs || !isVirtualInputDevice(input_descriptor->mDevice))) {
2445f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return mInputs.keyAt(i);
2446f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2447f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2448f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return 0;
2449f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2450f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2451e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2452c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentaudio_devices_t AudioPolicyManagerBase::getDeviceForVolume(audio_devices_t device)
2453e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent{
2454ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if (device == AUDIO_DEVICE_NONE) {
2455e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        // this happens when forcing a route update and no track is active on an output.
2456e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        // In this case the returned category is not important.
2457c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        device =  AUDIO_DEVICE_OUT_SPEAKER;
2458c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    } else if (AudioSystem::popCount(device) > 1) {
2459e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        // Multiple device selection is either:
2460e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        //  - speaker + one other device: give priority to speaker in this case.
2461e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        //  - one A2DP device + another device: happens with duplicated output. In this case
2462e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        // retain the device on the A2DP output as the other must not correspond to an active
2463e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        // selection if not the speaker.
2464c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        if (device & AUDIO_DEVICE_OUT_SPEAKER) {
2465c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            device = AUDIO_DEVICE_OUT_SPEAKER;
2466c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        } else {
2467c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            device = (audio_devices_t)(device & AUDIO_DEVICE_OUT_ALL_A2DP);
2468c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        }
2469e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    }
2470e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
247164cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block    ALOGW_IF(AudioSystem::popCount(device) != 1,
2472c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            "getDeviceForVolume() invalid device combination: %08x",
2473e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            device);
2474e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2475c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    return device;
2476c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent}
2477c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
2478f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric LaurentAudioPolicyManagerBase::device_category AudioPolicyManagerBase::getDeviceCategory(audio_devices_t device)
2479c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent{
2480f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent    switch(getDeviceForVolume(device)) {
2481e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_EARPIECE:
2482e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            return DEVICE_CATEGORY_EARPIECE;
2483e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_WIRED_HEADSET:
2484e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_WIRED_HEADPHONE:
2485e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_BLUETOOTH_SCO:
2486e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET:
2487e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP:
2488e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES:
2489e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            return DEVICE_CATEGORY_HEADSET;
2490e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_SPEAKER:
2491e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT:
2492e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER:
2493c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        case AUDIO_DEVICE_OUT_AUX_DIGITAL:
2494599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent        case AUDIO_DEVICE_OUT_USB_ACCESSORY:
2495599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent        case AUDIO_DEVICE_OUT_USB_DEVICE:
249648387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi        case AUDIO_DEVICE_OUT_REMOTE_SUBMIX:
2497e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        default:
2498e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            return DEVICE_CATEGORY_SPEAKER;
2499e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    }
2500e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent}
2501e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2502f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurentfloat AudioPolicyManagerBase::volIndexToAmpl(audio_devices_t device, const StreamDescriptor& streamDesc,
2503e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        int indexInUi)
2504e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent{
2505e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    device_category deviceCategory = getDeviceCategory(device);
2506e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    const VolumeCurvePoint *curve = streamDesc.mVolumeCurve[deviceCategory];
2507e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2508f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // the volume index in the UI is relative to the min and max volume indices for this stream type
2509e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    int nbSteps = 1 + curve[VOLMAX].mIndex -
2510e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            curve[VOLMIN].mIndex;
2511f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    int volIdx = (nbSteps * (indexInUi - streamDesc.mIndexMin)) /
2512f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            (streamDesc.mIndexMax - streamDesc.mIndexMin);
2513f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2514f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // find what part of the curve this index volume belongs to, or if it's out of bounds
2515f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    int segment = 0;
2516e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    if (volIdx < curve[VOLMIN].mIndex) {         // out of bounds
2517f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return 0.0f;
2518e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    } else if (volIdx < curve[VOLKNEE1].mIndex) {
2519f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        segment = 0;
2520e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    } else if (volIdx < curve[VOLKNEE2].mIndex) {
2521f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        segment = 1;
2522e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    } else if (volIdx <= curve[VOLMAX].mIndex) {
2523f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        segment = 2;
2524f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {                                                               // out of bounds
2525f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return 1.0f;
2526f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2527f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2528f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // linear interpolation in the attenuation table in dB
2529e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    float decibels = curve[segment].mDBAttenuation +
2530e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            ((float)(volIdx - curve[segment].mIndex)) *
2531e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent                ( (curve[segment+1].mDBAttenuation -
2532e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent                        curve[segment].mDBAttenuation) /
2533e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent                    ((float)(curve[segment+1].mIndex -
2534e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent                            curve[segment].mIndex)) );
2535f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2536f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    float amplification = exp( decibels * 0.115129f); // exp( dB * ln(10) / 20 )
2537f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
25383cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGVV("VOLUME vol index=[%d %d %d], dB=[%.1f %.1f %.1f] ampl=%.5f",
2539e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            curve[segment].mIndex, volIdx,
2540e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            curve[segment+1].mIndex,
2541e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            curve[segment].mDBAttenuation,
2542cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent            decibels,
2543e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            curve[segment+1].mDBAttenuation,
2544f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            amplification);
2545f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2546f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return amplification;
2547f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2548f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2549cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
2550e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    AudioPolicyManagerBase::sDefaultVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
2551e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    {1, -49.5f}, {33, -33.5f}, {66, -17.0f}, {100, 0.0f}
2552e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent};
2553e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2554e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
2555e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    AudioPolicyManagerBase::sDefaultMediaVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
2556e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    {1, -58.0f}, {20, -40.0f}, {60, -17.0f}, {100, 0.0f}
2557cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent};
2558cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent
2559e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
2560e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    AudioPolicyManagerBase::sSpeakerMediaVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
2561e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    {1, -56.0f}, {20, -34.0f}, {60, -11.0f}, {100, 0.0f}
2562e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent};
2563e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2564e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
2565e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    AudioPolicyManagerBase::sSpeakerSonificationVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
2566e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    {1, -29.7f}, {33, -20.1f}, {66, -10.2f}, {100, 0.0f}
2567e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent};
2568e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2569ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent// AUDIO_STREAM_SYSTEM, AUDIO_STREAM_ENFORCED_AUDIBLE and AUDIO_STREAM_DTMF volume tracks
2570ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent// AUDIO_STREAM_RING on phones and AUDIO_STREAM_MUSIC on tablets (See AudioService.java).
2571123897874418f9f0e48bb89386d8c470e6975f28Jean-Michel Trivi// The range is constrained between -24dB and -6dB over speaker and -30dB and -18dB over headset.
2572ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
2573ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    AudioPolicyManagerBase::sDefaultSystemVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
2574123897874418f9f0e48bb89386d8c470e6975f28Jean-Michel Trivi    {1, -24.0f}, {33, -18.0f}, {66, -12.0f}, {100, -6.0f}
2575ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent};
2576ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent
2577ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
2578ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    AudioPolicyManagerBase::sHeadsetSystemVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
25797465678e0d5711ebcd78ae47b3a76821534a23eaEric Laurent    {1, -30.0f}, {33, -26.0f}, {66, -22.0f}, {100, -18.0f}
2580ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent};
2581e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2582e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
2583ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent            *AudioPolicyManagerBase::sVolumeProfiles[AUDIO_STREAM_CNT]
2584e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent                                                   [AudioPolicyManagerBase::DEVICE_CATEGORY_CNT] = {
2585ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_VOICE_CALL
2586e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
2587e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sDefaultVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2588e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sDefaultVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2589e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    },
2590ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_SYSTEM
2591ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET
2592ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2593ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultSystemVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2594ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    },
2595ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_RING
2596e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
2597e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2598e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sDefaultVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2599e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    },
2600ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_MUSIC
2601ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultMediaVolumeCurve, // DEVICE_CATEGORY_HEADSET
2602ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sSpeakerMediaVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2603ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultMediaVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2604ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    },
2605ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_ALARM
260612bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
260712bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER
260812bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        sDefaultVolumeCurve  // DEVICE_CATEGORY_EARPIECE
260912bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    },
2610ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_NOTIFICATION
2611e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
2612ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2613e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sDefaultVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2614c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    },
2615ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_BLUETOOTH_SCO
2616c16ac09f510437e8340be691720177a490ae78f0Eric Laurent        sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
2617ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2618c16ac09f510437e8340be691720177a490ae78f0Eric Laurent        sDefaultVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2619c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    },
2620ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_ENFORCED_AUDIBLE
2621ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET
2622ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2623ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultSystemVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2624ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    },
2625ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    {  // AUDIO_STREAM_DTMF
2626ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET
2627ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2628ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultSystemVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2629ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    },
2630ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_TTS
2631ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultMediaVolumeCurve, // DEVICE_CATEGORY_HEADSET
2632ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sSpeakerMediaVolumeCurve, // DEVICE_CATEGORY_SPEAKER
2633ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultMediaVolumeCurve  // DEVICE_CATEGORY_EARPIECE
2634ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    },
2635e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent};
2636e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2637e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurentvoid AudioPolicyManagerBase::initializeVolumeCurves()
2638e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent{
2639ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    for (int i = 0; i < AUDIO_STREAM_CNT; i++) {
2640e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) {
2641cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent            mStreams[i].mVolumeCurve[j] =
2642ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent                    sVolumeProfiles[i][j];
2643cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent        }
2644cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent    }
2645f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2646f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2647c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentfloat AudioPolicyManagerBase::computeVolume(int stream,
2648c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                            int index,
2649c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                            audio_io_handle_t output,
2650f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                                            audio_devices_t device)
2651f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2652f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    float volume = 1.0;
2653f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
2654f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    StreamDescriptor &streamDesc = mStreams[stream];
2655f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2656ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if (device == AUDIO_DEVICE_NONE) {
2657f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device = outputDesc->device();
2658f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2659f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2660f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if volume is not 0 (not muted), force media volume to max on digital output
2661f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (stream == AudioSystem::MUSIC &&
2662f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        index != mStreams[stream].mIndexMin &&
2663f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent        (device == AUDIO_DEVICE_OUT_AUX_DIGITAL ||
2664599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent         device == AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET ||
2665599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent         device == AUDIO_DEVICE_OUT_USB_ACCESSORY ||
2666599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent         device == AUDIO_DEVICE_OUT_USB_DEVICE)) {
2667f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return 1.0;
2668f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2669f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2670f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    volume = volIndexToAmpl(device, streamDesc, index);
2671f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2672f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if a headset is connected, apply the following rules to ring tones and notifications
2673f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // to avoid sound level bursts in user's ears:
2674f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // - always attenuate ring tones and notifications volume by 6dB
2675f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // - if music is playing, always limit the volume to current music volume,
2676f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // with a minimum threshold at -36dB so that notification is always perceived.
267712bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    const routing_strategy stream_strategy = getStrategy((AudioSystem::stream_type)stream);
2678c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
2679c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
2680c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            AUDIO_DEVICE_OUT_WIRED_HEADSET |
2681c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) &&
268212bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        ((stream_strategy == STRATEGY_SONIFICATION)
268312bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi                || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL)
268412bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi                || (stream == AudioSystem::SYSTEM)) &&
2685f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        streamDesc.mCanBeMuted) {
2686f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        volume *= SONIFICATION_HEADSET_VOLUME_FACTOR;
2687f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // when the phone is ringing we must consider that music could have been paused just before
2688f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // by the music application and behave as if music was active if the last music track was
2689f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // just stopped
2690ac3cf10ef6de12e69540a1244ac7255f93fa7502Eric Laurent        if (isStreamActive(AudioSystem::MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) ||
2691ac3cf10ef6de12e69540a1244ac7255f93fa7502Eric Laurent                mLimitRingtoneVolume) {
269217a73c3394547692457299dc512b5c2312ea0344Eric Laurent            audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/);
2693c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            float musicVol = computeVolume(AudioSystem::MUSIC,
269417a73c3394547692457299dc512b5c2312ea0344Eric Laurent                               mStreams[AudioSystem::MUSIC].getVolumeIndex(musicDevice),
2695c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                               output,
269617a73c3394547692457299dc512b5c2312ea0344Eric Laurent                               musicDevice);
2697c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            float minVol = (musicVol > SONIFICATION_HEADSET_VOLUME_MIN) ?
2698c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                musicVol : SONIFICATION_HEADSET_VOLUME_MIN;
2699f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (volume > minVol) {
2700f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                volume = minVol;
27016a70518b93928d1c91457ff805e375c82d76b0e5Steve Block                ALOGV("computeVolume limiting volume to %f musicVol %f", minVol, musicVol);
2702f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2703f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2704f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2705f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2706f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return volume;
2707f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2708f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2709c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentstatus_t AudioPolicyManagerBase::checkAndSetVolume(int stream,
2710c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                   int index,
2711c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                   audio_io_handle_t output,
2712f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                                                   audio_devices_t device,
2713c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                   int delayMs,
2714c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                   bool force)
2715f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2716f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2717f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // do not change actual stream volume if the stream is muted
2718f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mOutputs.valueFor(output)->mMuteCount[stream] != 0) {
27193cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        ALOGVV("checkAndSetVolume() stream %d muted count %d",
27205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent              stream, mOutputs.valueFor(output)->mMuteCount[stream]);
2721f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return NO_ERROR;
2722f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2723f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2724f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // do not change in call volume if bluetooth is connected and vice versa
2725f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if ((stream == AudioSystem::VOICE_CALL && mForceUse[AudioSystem::FOR_COMMUNICATION] == AudioSystem::FORCE_BT_SCO) ||
2726f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        (stream == AudioSystem::BLUETOOTH_SCO && mForceUse[AudioSystem::FOR_COMMUNICATION] != AudioSystem::FORCE_BT_SCO)) {
27276a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
2728f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin             stream, mForceUse[AudioSystem::FOR_COMMUNICATION]);
2729f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return INVALID_OPERATION;
2730f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2731f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2732f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    float volume = computeVolume(stream, index, output, device);
2733f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // We actually change the volume if:
2734f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // - the float value returned by computeVolume() changed
2735f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // - the force flag is set
2736f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (volume != mOutputs.valueFor(output)->mCurVolume[stream] ||
2737f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            force) {
2738f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mOutputs.valueFor(output)->mCurVolume[stream] = volume;
27393cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        ALOGVV("checkAndSetVolume() for output %d stream %d, volume %f, delay %d", output, stream, volume, delayMs);
2740f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (stream == AudioSystem::VOICE_CALL ||
2741f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            stream == AudioSystem::DTMF ||
2742f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            stream == AudioSystem::BLUETOOTH_SCO) {
2743f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // offset value to reflect actual hardware volume that never reaches 0
2744f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // 1% corresponds roughly to first step in VOICE_CALL stream volume setting (see AudioService.java)
2745f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            volume = 0.01 + 0.99 * volume;
2746f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // Force VOICE_CALL to track BLUETOOTH_SCO stream volume when bluetooth audio is
2747f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // enabled
2748f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (stream == AudioSystem::BLUETOOTH_SCO) {
2749f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mpClientInterface->setStreamVolume(AudioSystem::VOICE_CALL, volume, output, delayMs);
2750f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2751f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2752f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2753f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mpClientInterface->setStreamVolume((AudioSystem::stream_type)stream, volume, output, delayMs);
2754f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2755f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2756f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (stream == AudioSystem::VOICE_CALL ||
2757f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        stream == AudioSystem::BLUETOOTH_SCO) {
2758f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        float voiceVolume;
2759f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // Force voice volume to max for bluetooth SCO as volume is managed by the headset
2760f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (stream == AudioSystem::VOICE_CALL) {
2761f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            voiceVolume = (float)index/(float)mStreams[stream].mIndexMax;
2762f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        } else {
2763f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            voiceVolume = 1.0;
2764f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2765f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2766b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        if (voiceVolume != mLastVoiceVolume && output == mPrimaryOutput) {
2767f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
2768f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mLastVoiceVolume = voiceVolume;
2769f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2770f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2771f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2772f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
2773f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2774f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2775c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentvoid AudioPolicyManagerBase::applyStreamVolumes(audio_io_handle_t output,
2776f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                                                audio_devices_t device,
2777c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                int delayMs,
2778c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                bool force)
2779f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
27803cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGVV("applyStreamVolumes() for output %d and device %x", output, device);
2781f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2782f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
2783c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        checkAndSetVolume(stream,
2784f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                          mStreams[stream].getVolumeIndex(device),
2785c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                          output,
2786c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                          device,
2787c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                          delayMs,
2788c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                          force);
2789f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2790f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2791f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
279201e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurentvoid AudioPolicyManagerBase::setStrategyMute(routing_strategy strategy,
279301e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                             bool on,
279401e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                             audio_io_handle_t output,
279501e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                             int delayMs,
279601e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                             audio_devices_t device)
2797f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
27983cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGVV("setStrategyMute() strategy %d, mute %d, output %d", strategy, on, output);
2799f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
2800f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (getStrategy((AudioSystem::stream_type)stream) == strategy) {
280101e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent            setStreamMute(stream, on, output, delayMs, device);
2802f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2803f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2804f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2805f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
280601e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurentvoid AudioPolicyManagerBase::setStreamMute(int stream,
280701e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                           bool on,
280801e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                           audio_io_handle_t output,
280901e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                           int delayMs,
281001e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                           audio_devices_t device)
2811f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2812f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    StreamDescriptor &streamDesc = mStreams[stream];
2813f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
2814ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if (device == AUDIO_DEVICE_NONE) {
281501e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent        device = outputDesc->device();
281601e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent    }
2817f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
28183cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGVV("setStreamMute() stream %d, mute %d, output %d, mMuteCount %d device %04x",
281901e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent          stream, on, output, outputDesc->mMuteCount[stream], device);
2820f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2821f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (on) {
2822f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (outputDesc->mMuteCount[stream] == 0) {
2823f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (streamDesc.mCanBeMuted) {
2824c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                checkAndSetVolume(stream, 0, output, device, delayMs);
2825f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2826f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2827f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
2828f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mMuteCount[stream]++;
2829f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
2830f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (outputDesc->mMuteCount[stream] == 0) {
28313cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            ALOGV("setStreamMute() unmuting non muted stream!");
2832f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return;
2833f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2834f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (--outputDesc->mMuteCount[stream] == 0) {
2835c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            checkAndSetVolume(stream,
2836c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                              streamDesc.getVolumeIndex(device),
2837c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                              output,
2838c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                              device,
2839c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                              delayMs);
2840f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2841f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2842f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2843f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2844f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::handleIncallSonification(int stream, bool starting, bool stateChange)
2845f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2846f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if the stream pertains to sonification strategy and we are in call we must
2847f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // mute the stream if it is low visibility. If it is high visibility, we must play a tone
2848f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // in the device used for phone strategy and play the tone if the selected device does not
2849f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // interfere with the device used for phone strategy
2850f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
2851f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // many times as there are active tracks on the output
285212bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    const routing_strategy stream_strategy = getStrategy((AudioSystem::stream_type)stream);
285312bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    if ((stream_strategy == STRATEGY_SONIFICATION) ||
285412bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi            ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
2855b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        AudioOutputDescriptor *outputDesc = mOutputs.valueFor(mPrimaryOutput);
28566a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
2857f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                stream, starting, outputDesc->mDevice, stateChange);
2858f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (outputDesc->mRefCount[stream]) {
2859f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            int muteCount = 1;
2860f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (stateChange) {
2861f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                muteCount = outputDesc->mRefCount[stream];
2862f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2863f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (AudioSystem::isLowVisibility((AudioSystem::stream_type)stream)) {
28646a70518b93928d1c91457ff805e375c82d76b0e5Steve Block                ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
2865f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                for (int i = 0; i < muteCount; i++) {
2866b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                    setStreamMute(stream, starting, mPrimaryOutput);
2867f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
2868f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            } else {
28696a70518b93928d1c91457ff805e375c82d76b0e5Steve Block                ALOGV("handleIncallSonification() high visibility");
28705ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                if (outputDesc->device() &
28715ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                        getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
28726a70518b93928d1c91457ff805e375c82d76b0e5Steve Block                    ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
2873f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    for (int i = 0; i < muteCount; i++) {
2874b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                        setStreamMute(stream, starting, mPrimaryOutput);
2875f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    }
2876f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
2877f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (starting) {
2878f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    mpClientInterface->startTone(ToneGenerator::TONE_SUP_CALL_WAITING, AudioSystem::VOICE_CALL);
2879f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else {
2880f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    mpClientInterface->stopTone();
2881f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
2882f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2883f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2884f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2885f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2886f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2887f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinbool AudioPolicyManagerBase::isInCall()
2888f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2889f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return isStateInCall(mPhoneState);
2890f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2891f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2892f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinbool AudioPolicyManagerBase::isStateInCall(int state) {
2893f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return ((state == AudioSystem::MODE_IN_CALL) ||
2894f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            (state == AudioSystem::MODE_IN_COMMUNICATION));
2895f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2896f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
289770c236c9290732782d5267935af1475b8d5ae602Eric Laurentbool AudioPolicyManagerBase::needsDirectOuput(audio_stream_type_t stream,
289870c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                              uint32_t samplingRate,
289970c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                              audio_format_t format,
290070c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                              audio_channel_mask_t channelMask,
29010977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent                                              audio_output_flags_t flags,
290270c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                              audio_devices_t device)
2903f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2904f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin   return ((flags & AudioSystem::OUTPUT_FLAG_DIRECT) ||
2905b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent          (format != 0 && !AudioSystem::isLinearPCM(format)));
2906f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2907f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2908f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinuint32_t AudioPolicyManagerBase::getMaxEffectsCpuLoad()
2909f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2910f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return MAX_EFFECTS_CPU_LOAD;
2911f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2912f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2913f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinuint32_t AudioPolicyManagerBase::getMaxEffectsMemory()
2914f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2915f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return MAX_EFFECTS_MEMORY;
2916f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2917f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2918f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// --- AudioOutputDescriptor class implementation
2919f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2920b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric LaurentAudioPolicyManagerBase::AudioOutputDescriptor::AudioOutputDescriptor(
29215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        const IOProfile *profile)
292270c236c9290732782d5267935af1475b8d5ae602Eric Laurent    : mId(0), mSamplingRate(0), mFormat((audio_format_t)0),
292370c236c9290732782d5267935af1475b8d5ae602Eric Laurent      mChannelMask((audio_channel_mask_t)0), mLatency(0),
2924ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    mFlags((audio_output_flags_t)0), mDevice(AUDIO_DEVICE_NONE),
2925f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent    mOutput1(0), mOutput2(0), mProfile(profile)
2926f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2927f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // clear usage count for all stream types
2928f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) {
2929f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mRefCount[i] = 0;
2930f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mCurVolume[i] = -1.0;
2931f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mMuteCount[i] = 0;
2932f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mStopTime[i] = 0;
2933f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
29343cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    if (profile != NULL) {
29353cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        mSamplingRate = profile->mSamplingRates[0];
29363cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        mFormat = profile->mFormats[0];
29373cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        mChannelMask = profile->mChannelMasks[0];
29383cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        mFlags = profile->mFlags;
29393cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    }
2940f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2941f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2942f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurentaudio_devices_t AudioPolicyManagerBase::AudioOutputDescriptor::device()
2943f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2944f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (isDuplicated()) {
2945f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent        return (audio_devices_t)(mOutput1->mDevice | mOutput2->mDevice);
2946f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
2947f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent        return mDevice;
2948f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2949f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2950f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
29515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentuint32_t AudioPolicyManagerBase::AudioOutputDescriptor::latency()
29525ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
29535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (isDuplicated()) {
29545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return (mOutput1->mLatency > mOutput2->mLatency) ? mOutput1->mLatency : mOutput2->mLatency;
29555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    } else {
29565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return mLatency;
29575ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
29585ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
29595ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
29605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentbool AudioPolicyManagerBase::AudioOutputDescriptor::sharesHwModuleWith(
29615ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        const AudioOutputDescriptor *outputDesc)
29625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
29635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (isDuplicated()) {
29645ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return mOutput1->sharesHwModuleWith(outputDesc) || mOutput2->sharesHwModuleWith(outputDesc);
29655ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    } else if (outputDesc->isDuplicated()){
29665ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return sharesHwModuleWith(outputDesc->mOutput1) || sharesHwModuleWith(outputDesc->mOutput2);
29675ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    } else {
296870c236c9290732782d5267935af1475b8d5ae602Eric Laurent        return (mProfile->mModule == outputDesc->mProfile->mModule);
29695ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
29705ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
29715ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
2972f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::AudioOutputDescriptor::changeRefCount(AudioSystem::stream_type stream, int delta)
2973f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2974f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // forward usage count change to attached outputs
2975f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (isDuplicated()) {
2976f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mOutput1->changeRefCount(stream, delta);
2977f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mOutput2->changeRefCount(stream, delta);
2978f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2979f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if ((delta + (int)mRefCount[stream]) < 0) {
298064cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("changeRefCount() invalid delta %d for stream %d, refCount %d", delta, stream, mRefCount[stream]);
2981f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mRefCount[stream] = 0;
2982f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
2983f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2984f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mRefCount[stream] += delta;
29856a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("changeRefCount() stream %d, count %d", stream, mRefCount[stream]);
2986f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2987f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2988f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinuint32_t AudioPolicyManagerBase::AudioOutputDescriptor::refCount()
2989f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2990f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    uint32_t refcount = 0;
2991f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) {
2992f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        refcount += mRefCount[i];
2993f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2994f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return refcount;
2995f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2996f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2997f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinuint32_t AudioPolicyManagerBase::AudioOutputDescriptor::strategyRefCount(routing_strategy strategy)
2998f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2999f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    uint32_t refCount = 0;
3000f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) {
3001f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (getStrategy((AudioSystem::stream_type)i) == strategy) {
3002f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            refCount += mRefCount[i];
3003f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
3004f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3005f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return refCount;
3006f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3007f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3008f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurentaudio_devices_t AudioPolicyManagerBase::AudioOutputDescriptor::supportedDevices()
3009b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent{
3010b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (isDuplicated()) {
3011f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent        return (audio_devices_t)(mOutput1->supportedDevices() | mOutput2->supportedDevices());
3012b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    } else {
3013b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        return mProfile->mSupportedDevices ;
3014b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    }
3015b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent}
3016b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
3017f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::AudioOutputDescriptor::dump(int fd)
3018f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3019f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    const size_t SIZE = 256;
3020f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    char buffer[SIZE];
3021f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    String8 result;
3022f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3023f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Sampling rate: %d\n", mSamplingRate);
3024f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3025f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Format: %d\n", mFormat);
3026f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
302770c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, " Channels: %08x\n", mChannelMask);
3028f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3029f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Latency: %d\n", mLatency);
3030f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3031f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Flags %08x\n", mFlags);
3032f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3033f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Devices %08x\n", device());
3034f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3035f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Stream volume refCount muteCount\n");
3036f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3037f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) {
3038f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        snprintf(buffer, SIZE, " %02d     %.03f     %02d       %02d\n", i, mCurVolume[i], mRefCount[i], mMuteCount[i]);
3039f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        result.append(buffer);
3040f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3041f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, result.string(), result.size());
3042f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3043f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
3044f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3045f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3046f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// --- AudioInputDescriptor class implementation
3047f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
30485ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric LaurentAudioPolicyManagerBase::AudioInputDescriptor::AudioInputDescriptor(const IOProfile *profile)
304970c236c9290732782d5267935af1475b8d5ae602Eric Laurent    : mSamplingRate(0), mFormat((audio_format_t)0), mChannelMask((audio_channel_mask_t)0),
3050ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent      mDevice(AUDIO_DEVICE_NONE), mRefCount(0),
30515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent      mInputSource(0), mProfile(profile)
3052f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3053f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3054f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3055f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::AudioInputDescriptor::dump(int fd)
3056f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3057f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    const size_t SIZE = 256;
3058f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    char buffer[SIZE];
3059f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    String8 result;
3060f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3061f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Sampling rate: %d\n", mSamplingRate);
3062f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3063f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Format: %d\n", mFormat);
3064f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
306570c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, " Channels: %08x\n", mChannelMask);
3066f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3067f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Devices %08x\n", mDevice);
3068f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3069f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Ref Count %d\n", mRefCount);
3070f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3071f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, result.string(), result.size());
3072f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3073f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
3074f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3075f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3076f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// --- StreamDescriptor class implementation
3077f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3078c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric LaurentAudioPolicyManagerBase::StreamDescriptor::StreamDescriptor()
3079c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    :   mIndexMin(0), mIndexMax(1), mCanBeMuted(true)
3080c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent{
3081c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    mIndexCur.add(AUDIO_DEVICE_OUT_DEFAULT, 0);
3082c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent}
3083c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
3084c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentint AudioPolicyManagerBase::StreamDescriptor::getVolumeIndex(audio_devices_t device)
3085c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent{
3086c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    device = AudioPolicyManagerBase::getDeviceForVolume(device);
3087c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    // there is always a valid entry for AUDIO_DEVICE_OUT_DEFAULT
3088c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    if (mIndexCur.indexOfKey(device) < 0) {
3089c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        device = AUDIO_DEVICE_OUT_DEFAULT;
3090c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    }
3091c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    return mIndexCur.valueFor(device);
3092c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent}
3093c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
3094c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentvoid AudioPolicyManagerBase::StreamDescriptor::dump(int fd)
3095f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3096c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    const size_t SIZE = 256;
3097c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    char buffer[SIZE];
3098c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    String8 result;
3099c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
3100c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    snprintf(buffer, SIZE, "%s         %02d         %02d         ",
3101c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent             mCanBeMuted ? "true " : "false", mIndexMin, mIndexMax);
3102c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    result.append(buffer);
3103c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    for (size_t i = 0; i < mIndexCur.size(); i++) {
3104c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        snprintf(buffer, SIZE, "%04x : %02d, ",
3105c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                 mIndexCur.keyAt(i),
3106c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                 mIndexCur.valueAt(i));
3107c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        result.append(buffer);
3108c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    }
3109c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    result.append("\n");
3110c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
3111c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    write(fd, result.string(), result.size());
3112f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3113f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3114f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// --- EffectDescriptor class implementation
3115f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3116f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::EffectDescriptor::dump(int fd)
3117f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3118f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    const size_t SIZE = 256;
3119f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    char buffer[SIZE];
3120f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    String8 result;
3121f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
31221c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent    snprintf(buffer, SIZE, " I/O: %d\n", mIo);
3123f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3124f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Strategy: %d\n", mStrategy);
3125f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3126f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Session: %d\n", mSession);
3127f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3128f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Name: %s\n",  mDesc.name);
3129f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3130582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    snprintf(buffer, SIZE, " %s\n",  mEnabled ? "Enabled" : "Disabled");
3131582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    result.append(buffer);
3132f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, result.string(), result.size());
3133f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3134f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
3135f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3136f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
31375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent// --- IOProfile class implementation
31385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
313970c236c9290732782d5267935af1475b8d5ae602Eric LaurentAudioPolicyManagerBase::HwModule::HwModule(const char *name)
314070c236c9290732782d5267935af1475b8d5ae602Eric Laurent    : mName(strndup(name, AUDIO_HARDWARE_MODULE_ID_MAX_LEN)), mHandle(0)
314170c236c9290732782d5267935af1475b8d5ae602Eric Laurent{
314270c236c9290732782d5267935af1475b8d5ae602Eric Laurent}
314370c236c9290732782d5267935af1475b8d5ae602Eric Laurent
314470c236c9290732782d5267935af1475b8d5ae602Eric LaurentAudioPolicyManagerBase::HwModule::~HwModule()
314570c236c9290732782d5267935af1475b8d5ae602Eric Laurent{
314670c236c9290732782d5267935af1475b8d5ae602Eric Laurent    for (size_t i = 0; i < mOutputProfiles.size(); i++) {
314770c236c9290732782d5267935af1475b8d5ae602Eric Laurent         delete mOutputProfiles[i];
314870c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
314970c236c9290732782d5267935af1475b8d5ae602Eric Laurent    for (size_t i = 0; i < mInputProfiles.size(); i++) {
315070c236c9290732782d5267935af1475b8d5ae602Eric Laurent         delete mInputProfiles[i];
315170c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
315270c236c9290732782d5267935af1475b8d5ae602Eric Laurent    free((void *)mName);
315370c236c9290732782d5267935af1475b8d5ae602Eric Laurent}
315470c236c9290732782d5267935af1475b8d5ae602Eric Laurent
315570c236c9290732782d5267935af1475b8d5ae602Eric Laurentvoid AudioPolicyManagerBase::HwModule::dump(int fd)
315670c236c9290732782d5267935af1475b8d5ae602Eric Laurent{
315770c236c9290732782d5267935af1475b8d5ae602Eric Laurent    const size_t SIZE = 256;
315870c236c9290732782d5267935af1475b8d5ae602Eric Laurent    char buffer[SIZE];
315970c236c9290732782d5267935af1475b8d5ae602Eric Laurent    String8 result;
316070c236c9290732782d5267935af1475b8d5ae602Eric Laurent
316170c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "  - name: %s\n", mName);
316270c236c9290732782d5267935af1475b8d5ae602Eric Laurent    result.append(buffer);
316370c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "  - handle: %d\n", mHandle);
316470c236c9290732782d5267935af1475b8d5ae602Eric Laurent    result.append(buffer);
316570c236c9290732782d5267935af1475b8d5ae602Eric Laurent    write(fd, result.string(), result.size());
316670c236c9290732782d5267935af1475b8d5ae602Eric Laurent    if (mOutputProfiles.size()) {
316770c236c9290732782d5267935af1475b8d5ae602Eric Laurent        write(fd, "  - outputs:\n", sizeof("  - outputs:\n"));
316870c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (size_t i = 0; i < mOutputProfiles.size(); i++) {
3169599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            snprintf(buffer, SIZE, "    output %d:\n", i);
3170599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            write(fd, buffer, strlen(buffer));
317170c236c9290732782d5267935af1475b8d5ae602Eric Laurent            mOutputProfiles[i]->dump(fd);
317270c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
317370c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
317470c236c9290732782d5267935af1475b8d5ae602Eric Laurent    if (mInputProfiles.size()) {
317570c236c9290732782d5267935af1475b8d5ae602Eric Laurent        write(fd, "  - inputs:\n", sizeof("  - inputs:\n"));
317670c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (size_t i = 0; i < mInputProfiles.size(); i++) {
3177599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            snprintf(buffer, SIZE, "    input %d:\n", i);
3178599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            write(fd, buffer, strlen(buffer));
317970c236c9290732782d5267935af1475b8d5ae602Eric Laurent            mInputProfiles[i]->dump(fd);
318070c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
318170c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
318270c236c9290732782d5267935af1475b8d5ae602Eric Laurent}
318370c236c9290732782d5267935af1475b8d5ae602Eric Laurent
318470c236c9290732782d5267935af1475b8d5ae602Eric LaurentAudioPolicyManagerBase::IOProfile::IOProfile(HwModule *module)
31850977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent    : mFlags((audio_output_flags_t)0), mModule(module)
31865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
31875ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
31885ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
31895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric LaurentAudioPolicyManagerBase::IOProfile::~IOProfile()
31905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
319170c236c9290732782d5267935af1475b8d5ae602Eric Laurent}
319270c236c9290732782d5267935af1475b8d5ae602Eric Laurent
319370c236c9290732782d5267935af1475b8d5ae602Eric Laurent// checks if the IO profile is compatible with specified parameters. By convention a value of 0
319470c236c9290732782d5267935af1475b8d5ae602Eric Laurent// means a parameter is don't care
319570c236c9290732782d5267935af1475b8d5ae602Eric Laurentbool AudioPolicyManagerBase::IOProfile::isCompatibleProfile(audio_devices_t device,
319670c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                            uint32_t samplingRate,
319770c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                            uint32_t format,
319870c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                            uint32_t channelMask,
31990977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent                                                            audio_output_flags_t flags) const
320070c236c9290732782d5267935af1475b8d5ae602Eric Laurent{
320170c236c9290732782d5267935af1475b8d5ae602Eric Laurent    if ((mSupportedDevices & device) != device) {
320270c236c9290732782d5267935af1475b8d5ae602Eric Laurent        return false;
320370c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
320470c236c9290732782d5267935af1475b8d5ae602Eric Laurent    if ((mFlags & flags) != flags) {
320570c236c9290732782d5267935af1475b8d5ae602Eric Laurent        return false;
320670c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
320770c236c9290732782d5267935af1475b8d5ae602Eric Laurent    if (samplingRate != 0) {
320870c236c9290732782d5267935af1475b8d5ae602Eric Laurent        size_t i;
320970c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (i = 0; i < mSamplingRates.size(); i++)
321070c236c9290732782d5267935af1475b8d5ae602Eric Laurent        {
321170c236c9290732782d5267935af1475b8d5ae602Eric Laurent            if (mSamplingRates[i] == samplingRate) {
321270c236c9290732782d5267935af1475b8d5ae602Eric Laurent                break;
321370c236c9290732782d5267935af1475b8d5ae602Eric Laurent            }
321470c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
321570c236c9290732782d5267935af1475b8d5ae602Eric Laurent        if (i == mSamplingRates.size()) {
321670c236c9290732782d5267935af1475b8d5ae602Eric Laurent            return false;
321770c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
321870c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
321970c236c9290732782d5267935af1475b8d5ae602Eric Laurent    if (format != 0) {
322070c236c9290732782d5267935af1475b8d5ae602Eric Laurent        size_t i;
322170c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (i = 0; i < mFormats.size(); i++)
322270c236c9290732782d5267935af1475b8d5ae602Eric Laurent        {
322370c236c9290732782d5267935af1475b8d5ae602Eric Laurent            if (mFormats[i] == format) {
322470c236c9290732782d5267935af1475b8d5ae602Eric Laurent                break;
322570c236c9290732782d5267935af1475b8d5ae602Eric Laurent            }
322670c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
322770c236c9290732782d5267935af1475b8d5ae602Eric Laurent        if (i == mFormats.size()) {
322870c236c9290732782d5267935af1475b8d5ae602Eric Laurent            return false;
322970c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
323070c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
323170c236c9290732782d5267935af1475b8d5ae602Eric Laurent    if (channelMask != 0) {
323270c236c9290732782d5267935af1475b8d5ae602Eric Laurent        size_t i;
323370c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (i = 0; i < mChannelMasks.size(); i++)
323470c236c9290732782d5267935af1475b8d5ae602Eric Laurent        {
323570c236c9290732782d5267935af1475b8d5ae602Eric Laurent            if (mChannelMasks[i] == channelMask) {
323670c236c9290732782d5267935af1475b8d5ae602Eric Laurent                break;
323770c236c9290732782d5267935af1475b8d5ae602Eric Laurent            }
323870c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
323970c236c9290732782d5267935af1475b8d5ae602Eric Laurent        if (i == mChannelMasks.size()) {
324070c236c9290732782d5267935af1475b8d5ae602Eric Laurent            return false;
324170c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
324270c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
324370c236c9290732782d5267935af1475b8d5ae602Eric Laurent    return true;
32445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
32455ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
32465ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::IOProfile::dump(int fd)
32475ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
32485ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    const size_t SIZE = 256;
32495ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    char buffer[SIZE];
32505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    String8 result;
32515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
325270c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "    - sampling rates: ");
32535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    result.append(buffer);
32545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < mSamplingRates.size(); i++) {
32555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        snprintf(buffer, SIZE, "%d", mSamplingRates[i]);
32565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        result.append(buffer);
32575ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        result.append(i == (mSamplingRates.size() - 1) ? "\n" : ", ");
32585ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
32595ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
326070c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "    - channel masks: ");
32615ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    result.append(buffer);
32625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < mChannelMasks.size(); i++) {
32635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        snprintf(buffer, SIZE, "%04x", mChannelMasks[i]);
32645ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        result.append(buffer);
32655ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        result.append(i == (mChannelMasks.size() - 1) ? "\n" : ", ");
32665ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
32675ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
326870c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "    - formats: ");
32695ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    result.append(buffer);
32705ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < mFormats.size(); i++) {
32715ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        snprintf(buffer, SIZE, "%d", mFormats[i]);
32725ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        result.append(buffer);
32735ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        result.append(i == (mFormats.size() - 1) ? "\n" : ", ");
32745ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
32755ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
327670c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "    - devices: %04x\n", mSupportedDevices);
32775ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    result.append(buffer);
327870c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "    - flags: %04x\n", mFlags);
32795ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    result.append(buffer);
32805ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
32815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    write(fd, result.string(), result.size());
32825ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
32835ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
32845ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent// --- audio_policy.conf file parsing
32855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
32865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentstruct StringToEnum {
32875ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    const char *name;
32885ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    uint32_t value;
32895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent};
32905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
32915ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent#define STRING_TO_ENUM(string) { #string, string }
32925ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
32935ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
32945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentconst struct StringToEnum sDeviceNameToEnumTable[] = {
32955ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_EARPIECE),
32965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_SPEAKER),
32975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_WIRED_HEADSET),
32985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_WIRED_HEADPHONE),
32995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_ALL_SCO),
33005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_ALL_A2DP),
33015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_AUX_DIGITAL),
33025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET),
3303599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET),
3304599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_USB_DEVICE),
3305599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_USB_ACCESSORY),
3306599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_ALL_USB),
330748387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi    STRING_TO_ENUM(AUDIO_DEVICE_OUT_REMOTE_SUBMIX),
33085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_BUILTIN_MIC),
33095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET),
33105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_WIRED_HEADSET),
33115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_AUX_DIGITAL),
33125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_VOICE_CALL),
33135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_BACK_MIC),
331448387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi    STRING_TO_ENUM(AUDIO_DEVICE_IN_REMOTE_SUBMIX),
3315ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET),
3316ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_DGTL_DOCK_HEADSET),
3317ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_USB_ACCESSORY),
33185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent};
33195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
33205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentconst struct StringToEnum sFlagNameToEnumTable[] = {
33210977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent    STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT),
33220977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent    STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY),
3323b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent    STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST),
3324b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent    STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER),
33255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent};
33265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
33275ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentconst struct StringToEnum sFormatNameToEnumTable[] = {
33285ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
33295ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
33305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_FORMAT_MP3),
33315ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_FORMAT_AAC),
33325ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
33335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent};
33345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
33355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentconst struct StringToEnum sOutChannelsNameToEnumTable[] = {
33365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_OUT_MONO),
33375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
33385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
33395ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
33405ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent};
33415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
33425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentconst struct StringToEnum sInChannelsNameToEnumTable[] = {
33435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_IN_MONO),
33445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_IN_STEREO),
33455ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent};
33465ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
33475ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
33485ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentuint32_t AudioPolicyManagerBase::stringToEnum(const struct StringToEnum *table,
33495ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                              size_t size,
33505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                              const char *name)
33515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
33525ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < size; i++) {
33535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strcmp(table[i].name, name) == 0) {
33545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("stringToEnum() found %s", table[i].name);
33555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            return table[i].value;
33565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
33575ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
33585ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return 0;
33595ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
33605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
33610977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurentaudio_output_flags_t AudioPolicyManagerBase::parseFlagNames(char *name)
33625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
33635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    uint32_t flag = 0;
33645ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
33655ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // it is OK to cast name to non const here as we are not going to use it after
33665ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // strtok() modifies it
33675ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    char *flagName = strtok(name, "|");
33685ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (flagName != NULL) {
33695ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strlen(flagName) != 0) {
33705ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            flag |= stringToEnum(sFlagNameToEnumTable,
33715ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                               ARRAY_SIZE(sFlagNameToEnumTable),
33725ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                               flagName);
33735ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
33745ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        flagName = strtok(NULL, "|");
33755ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
33760977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent    return (audio_output_flags_t)flag;
33775ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
33785ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
33795ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentaudio_devices_t AudioPolicyManagerBase::parseDeviceNames(char *name)
33805ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
33815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    uint32_t device = 0;
33825ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
33835ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    char *devName = strtok(name, "|");
33845ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (devName != NULL) {
33855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strlen(devName) != 0) {
33865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device |= stringToEnum(sDeviceNameToEnumTable,
33875ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                 ARRAY_SIZE(sDeviceNameToEnumTable),
33885ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                 devName);
33895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
33905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        devName = strtok(NULL, "|");
33915ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
3392c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    return device;
33935ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
33945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
33955ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadSamplingRates(char *name, IOProfile *profile)
33965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
33975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    char *str = strtok(name, "|");
33985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
33993cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    // by convention, "0' in the first entry in mSamplingRates indicates the supported sampling
34003cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    // rates should be read from the output stream after it is opened for the first time
34013cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) {
34023cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        profile->mSamplingRates.add(0);
34033cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        return;
34043cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    }
34053cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
34065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (str != NULL) {
34075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        uint32_t rate = atoi(str);
34085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (rate != 0) {
34095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadSamplingRates() adding rate %d", rate);
34105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mSamplingRates.add(rate);
34115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
34125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        str = strtok(NULL, "|");
34135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
34145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return;
34155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
34165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
34175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadFormats(char *name, IOProfile *profile)
34185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
34195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    char *str = strtok(name, "|");
34205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
34213cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    // by convention, "0' in the first entry in mFormats indicates the supported formats
34223cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    // should be read from the output stream after it is opened for the first time
34233cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) {
34243cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        profile->mFormats.add((audio_format_t)0);
34253cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        return;
34263cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    }
34273cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
34285ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (str != NULL) {
34295ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_format_t format = (audio_format_t)stringToEnum(sFormatNameToEnumTable,
34305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                             ARRAY_SIZE(sFormatNameToEnumTable),
34315ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                             str);
34325ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (format != 0) {
34335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mFormats.add(format);
34345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
34355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        str = strtok(NULL, "|");
34365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
34375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return;
34385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
34395ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
34405ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadInChannels(char *name, IOProfile *profile)
34415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
34425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    const char *str = strtok(name, "|");
34435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
34445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGV("loadInChannels() %s", name);
34453cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
34463cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) {
34473cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        profile->mChannelMasks.add((audio_channel_mask_t)0);
34483cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        return;
34493cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    }
34503cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
34515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (str != NULL) {
34525ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_channel_mask_t channelMask =
34535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                (audio_channel_mask_t)stringToEnum(sInChannelsNameToEnumTable,
34545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                   ARRAY_SIZE(sInChannelsNameToEnumTable),
34555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                   str);
34565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (channelMask != 0) {
34575ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadInChannels() adding channelMask %04x", channelMask);
34585ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mChannelMasks.add(channelMask);
34595ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
34605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        str = strtok(NULL, "|");
34615ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
34625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return;
34635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
34645ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
34655ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadOutChannels(char *name, IOProfile *profile)
34665ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
34675ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    const char *str = strtok(name, "|");
34683cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
34693cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGV("loadOutChannels() %s", name);
34703cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
34713cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    // by convention, "0' in the first entry in mChannelMasks indicates the supported channel
34723cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    // masks should be read from the output stream after it is opened for the first time
34733cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) {
34743cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        profile->mChannelMasks.add((audio_channel_mask_t)0);
34753cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        return;
34763cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    }
34775ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
34785ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (str != NULL) {
34793cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        audio_channel_mask_t channelMask =
34803cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                (audio_channel_mask_t)stringToEnum(sOutChannelsNameToEnumTable,
34813cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                   ARRAY_SIZE(sOutChannelsNameToEnumTable),
34823cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                   str);
34835ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (channelMask != 0) {
34845ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mChannelMasks.add(channelMask);
34855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
34865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        str = strtok(NULL, "|");
34875ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
34885ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return;
34895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
34905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
349170c236c9290732782d5267935af1475b8d5ae602Eric Laurentstatus_t AudioPolicyManagerBase::loadInput(cnode *root, HwModule *module)
34925ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
34935ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    cnode *node = root->first_child;
34945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
34955ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    IOProfile *profile = new IOProfile(module);
34965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
34975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (node) {
34985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
34995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            loadSamplingRates((char *)node->value, profile);
35005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, FORMATS_TAG) == 0) {
35015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            loadFormats((char *)node->value, profile);
35025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, CHANNELS_TAG) == 0) {
35035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            loadInChannels((char *)node->value, profile);
35045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, DEVICES_TAG) == 0) {
35055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mSupportedDevices = parseDeviceNames((char *)node->value);
35065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
35075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        node = node->next;
35085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
3509ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    ALOGW_IF(profile->mSupportedDevices == AUDIO_DEVICE_NONE,
35105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadInput() invalid supported devices");
35115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGW_IF(profile->mChannelMasks.size() == 0,
35125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadInput() invalid supported channel masks");
35135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGW_IF(profile->mSamplingRates.size() == 0,
35145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadInput() invalid supported sampling rates");
35155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGW_IF(profile->mFormats.size() == 0,
35165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadInput() invalid supported formats");
3517ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if ((profile->mSupportedDevices != AUDIO_DEVICE_NONE) &&
35185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            (profile->mChannelMasks.size() != 0) &&
35195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            (profile->mSamplingRates.size() != 0) &&
35205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            (profile->mFormats.size() != 0)) {
35215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
35225ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        ALOGV("loadInput() adding input mSupportedDevices %04x", profile->mSupportedDevices);
35235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
352470c236c9290732782d5267935af1475b8d5ae602Eric Laurent        module->mInputProfiles.add(profile);
35255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return NO_ERROR;
35265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    } else {
35275ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        delete profile;
35285ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return BAD_VALUE;
35295ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
35305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
35315ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
353270c236c9290732782d5267935af1475b8d5ae602Eric Laurentstatus_t AudioPolicyManagerBase::loadOutput(cnode *root, HwModule *module)
35335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
35345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    cnode *node = root->first_child;
35355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
35365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    IOProfile *profile = new IOProfile(module);
35375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
35385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (node) {
35395ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
35405ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            loadSamplingRates((char *)node->value, profile);
35415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, FORMATS_TAG) == 0) {
35425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            loadFormats((char *)node->value, profile);
35435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, CHANNELS_TAG) == 0) {
35445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            loadOutChannels((char *)node->value, profile);
35455ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, DEVICES_TAG) == 0) {
35465ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mSupportedDevices = parseDeviceNames((char *)node->value);
35475ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, FLAGS_TAG) == 0) {
35485ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mFlags = parseFlagNames((char *)node->value);
35495ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
35505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        node = node->next;
35515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
3552ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    ALOGW_IF(profile->mSupportedDevices == AUDIO_DEVICE_NONE,
35535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadOutput() invalid supported devices");
35545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGW_IF(profile->mChannelMasks.size() == 0,
35555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadOutput() invalid supported channel masks");
35565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGW_IF(profile->mSamplingRates.size() == 0,
35575ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadOutput() invalid supported sampling rates");
35585ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGW_IF(profile->mFormats.size() == 0,
35595ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadOutput() invalid supported formats");
3560ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if ((profile->mSupportedDevices != AUDIO_DEVICE_NONE) &&
35615ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            (profile->mChannelMasks.size() != 0) &&
35625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            (profile->mSamplingRates.size() != 0) &&
35635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            (profile->mFormats.size() != 0)) {
35645ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
35655ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        ALOGV("loadOutput() adding output mSupportedDevices %04x, mFlags %04x",
35665ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent              profile->mSupportedDevices, profile->mFlags);
35675ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
356870c236c9290732782d5267935af1475b8d5ae602Eric Laurent        module->mOutputProfiles.add(profile);
35695ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return NO_ERROR;
35705ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    } else {
35715ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        delete profile;
35725ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return BAD_VALUE;
35735ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
35745ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
35755ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
35765ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadHwModule(cnode *root)
35775ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
35785ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    cnode *node = config_find(root, OUTPUTS_TAG);
35795ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    status_t status = NAME_NOT_FOUND;
358070c236c9290732782d5267935af1475b8d5ae602Eric Laurent
358170c236c9290732782d5267935af1475b8d5ae602Eric Laurent    HwModule *module = new HwModule(root->name);
358270c236c9290732782d5267935af1475b8d5ae602Eric Laurent
35835ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (node != NULL) {
35845ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strcmp(root->name, AUDIO_HARDWARE_MODULE_ID_A2DP) == 0) {
35855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            mHasA2dp = true;
3586599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent        } else if (strcmp(root->name, AUDIO_HARDWARE_MODULE_ID_USB) == 0) {
3587599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            mHasUsb = true;
358848387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi        } else if (strcmp(root->name, AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX) == 0) {
358948387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            mHasRemoteSubmix = true;
35905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
3591599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent
35925ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        node = node->first_child;
35935ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        while (node) {
35945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadHwModule() loading output %s", node->name);
359570c236c9290732782d5267935af1475b8d5ae602Eric Laurent            status_t tmpStatus = loadOutput(node, module);
35965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            if (status == NAME_NOT_FOUND || status == NO_ERROR) {
35975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                status = tmpStatus;
35985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
35995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            node = node->next;
36005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
36015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
36025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    node = config_find(root, INPUTS_TAG);
36035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (node != NULL) {
36045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        node = node->first_child;
36055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        while (node) {
36065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadHwModule() loading input %s", node->name);
360770c236c9290732782d5267935af1475b8d5ae602Eric Laurent            status_t tmpStatus = loadInput(node, module);
36085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            if (status == NAME_NOT_FOUND || status == NO_ERROR) {
36095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                status = tmpStatus;
36105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
36115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            node = node->next;
36125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
36135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
36145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (status == NO_ERROR) {
361570c236c9290732782d5267935af1475b8d5ae602Eric Laurent        mHwModules.add(module);
361670c236c9290732782d5267935af1475b8d5ae602Eric Laurent    } else {
361770c236c9290732782d5267935af1475b8d5ae602Eric Laurent        delete module;
36185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
36195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
36205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
36215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadHwModules(cnode *root)
36225ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
36235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    cnode *node = config_find(root, AUDIO_HW_MODULE_TAG);
36245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (node == NULL) {
36255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return;
36265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
36275ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
36285ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    node = node->first_child;
36295ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (node) {
36305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        ALOGV("loadHwModules() loading module %s", node->name);
36315ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        loadHwModule(node);
36325ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        node = node->next;
36335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
36345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
36355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
36365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadGlobalConfig(cnode *root)
36375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
36385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    cnode *node = config_find(root, GLOBAL_CONFIG_TAG);
36395ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (node == NULL) {
36405ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return;
36415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
36425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    node = node->first_child;
36435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (node) {
36445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strcmp(ATTACHED_OUTPUT_DEVICES_TAG, node->name) == 0) {
36455ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            mAttachedOutputDevices = parseDeviceNames((char *)node->value);
3646ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            ALOGW_IF(mAttachedOutputDevices == AUDIO_DEVICE_NONE,
3647c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                    "loadGlobalConfig() no attached output devices");
36485ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadGlobalConfig() mAttachedOutputDevices %04x", mAttachedOutputDevices);
36495ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(DEFAULT_OUTPUT_DEVICE_TAG, node->name) == 0) {
36505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            mDefaultOutputDevice = (audio_devices_t)stringToEnum(sDeviceNameToEnumTable,
36515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                              ARRAY_SIZE(sDeviceNameToEnumTable),
36525ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                              (char *)node->value);
3653ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            ALOGW_IF(mDefaultOutputDevice == AUDIO_DEVICE_NONE,
3654c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                    "loadGlobalConfig() default device not specified");
36555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadGlobalConfig() mDefaultOutputDevice %04x", mDefaultOutputDevice);
36565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(ATTACHED_INPUT_DEVICES_TAG, node->name) == 0) {
3657ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            mAvailableInputDevices = parseDeviceNames((char *)node->value) & ~AUDIO_DEVICE_BIT_IN;
36585ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadGlobalConfig() mAvailableInputDevices %04x", mAvailableInputDevices);
36595ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
36605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        node = node->next;
36615ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
36625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
36635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
36645ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentstatus_t AudioPolicyManagerBase::loadAudioPolicyConfig(const char *path)
36655ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
36665ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    cnode *root;
36675ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    char *data;
36685ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
36695ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    data = (char *)load_file(path, NULL);
36705ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (data == NULL) {
36715ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return -ENODEV;
36725ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
36735ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    root = config_node("", "");
36745ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    config_load(root, data);
36755ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
36765ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    loadGlobalConfig(root);
36775ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    loadHwModules(root);
36785ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
36795ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    config_free(root);
36805ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    free(root);
36815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    free(data);
36825ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
36835ec145df7708564d385fd3fb764085321cf4c253Dima Zavin    ALOGI("loadAudioPolicyConfig() loaded %s\n", path);
36845ec145df7708564d385fd3fb764085321cf4c253Dima Zavin
36855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return NO_ERROR;
36865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
3687f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3688739022f26a7127ba76a98dda65411496086114a7Dima Zavinvoid AudioPolicyManagerBase::defaultAudioPolicyConfig(void)
3689739022f26a7127ba76a98dda65411496086114a7Dima Zavin{
3690739022f26a7127ba76a98dda65411496086114a7Dima Zavin    HwModule *module;
3691739022f26a7127ba76a98dda65411496086114a7Dima Zavin    IOProfile *profile;
3692739022f26a7127ba76a98dda65411496086114a7Dima Zavin
3693739022f26a7127ba76a98dda65411496086114a7Dima Zavin    mDefaultOutputDevice = AUDIO_DEVICE_OUT_SPEAKER;
3694739022f26a7127ba76a98dda65411496086114a7Dima Zavin    mAttachedOutputDevices = AUDIO_DEVICE_OUT_SPEAKER;
3695ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    mAvailableInputDevices = AUDIO_DEVICE_IN_BUILTIN_MIC & ~AUDIO_DEVICE_BIT_IN;
3696739022f26a7127ba76a98dda65411496086114a7Dima Zavin
3697739022f26a7127ba76a98dda65411496086114a7Dima Zavin    module = new HwModule("primary");
3698739022f26a7127ba76a98dda65411496086114a7Dima Zavin
3699739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile = new IOProfile(module);
3700739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mSamplingRates.add(44100);
3701739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
3702739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mChannelMasks.add(AUDIO_CHANNEL_OUT_STEREO);
3703739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mSupportedDevices = AUDIO_DEVICE_OUT_SPEAKER;
3704739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mFlags = AUDIO_OUTPUT_FLAG_PRIMARY;
3705739022f26a7127ba76a98dda65411496086114a7Dima Zavin    module->mOutputProfiles.add(profile);
3706739022f26a7127ba76a98dda65411496086114a7Dima Zavin
3707739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile = new IOProfile(module);
3708739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mSamplingRates.add(8000);
3709739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
3710739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mChannelMasks.add(AUDIO_CHANNEL_IN_MONO);
3711739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mSupportedDevices = AUDIO_DEVICE_IN_BUILTIN_MIC;
3712739022f26a7127ba76a98dda65411496086114a7Dima Zavin    module->mInputProfiles.add(profile);
3713739022f26a7127ba76a98dda65411496086114a7Dima Zavin
3714739022f26a7127ba76a98dda65411496086114a7Dima Zavin    mHwModules.add(module);
3715739022f26a7127ba76a98dda65411496086114a7Dima Zavin}
3716f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3717f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}; // namespace android
3718