AudioPolicyManagerBase.cpp revision e81531e91ecae92aff471dbff9cbeb0f95ff4a80
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
19f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#include <utils/Log.h>
20f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#include <hardware_legacy/AudioPolicyManagerBase.h>
21f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#include <media/mediarecorder.h>
22f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#include <math.h>
23f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
24e81531e91ecae92aff471dbff9cbeb0f95ff4a80Dima Zavinnamespace android_audio_legacy {
25f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
26f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// ----------------------------------------------------------------------------
27f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// AudioPolicyInterface implementation
28f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// ----------------------------------------------------------------------------
29f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
30f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
31f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::setDeviceConnectionState(AudioSystem::audio_devices device,
32f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                  AudioSystem::device_connection_state state,
33f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                  const char *device_address)
34f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
35f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
36f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    LOGV("setDeviceConnectionState() device: %x, state %d, address %s", device, state, device_address);
37f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
38f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // connect/disconnect only 1 device at a time
39f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (AudioSystem::popCount(device) != 1) return BAD_VALUE;
40f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
41f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (strlen(device_address) >= MAX_DEVICE_ADDRESS_LEN) {
42f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        LOGE("setDeviceConnectionState() invalid address: %s", device_address);
43f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
44f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
45f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
46f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // handle output devices
47f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (AudioSystem::isOutputDevice(device)) {
48f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
49f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifndef WITH_A2DP
50f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (AudioSystem::isA2dpDevice(device)) {
51f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            LOGE("setDeviceConnectionState() invalid device: %x", device);
52f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return BAD_VALUE;
53f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
54f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif
55f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
56f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        switch (state)
57f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        {
58f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // handle output device connection
59f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        case AudioSystem::DEVICE_STATE_AVAILABLE:
60f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (mAvailableOutputDevices & device) {
61f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                LOGW("setDeviceConnectionState() device already connected: %x", device);
62f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                return INVALID_OPERATION;
63f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
64f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            LOGV("setDeviceConnectionState() connecting device %x", device);
65f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
66f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // register new device as available
67f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mAvailableOutputDevices |= device;
68f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
69f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef WITH_A2DP
70f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // handle A2DP device connection
71f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (AudioSystem::isA2dpDevice(device)) {
72f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                status_t status = handleA2dpConnection(device, device_address);
73f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (status != NO_ERROR) {
74f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    mAvailableOutputDevices &= ~device;
75f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    return status;
76f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
77f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            } else
78f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif
79f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            {
80f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (AudioSystem::isBluetoothScoDevice(device)) {
81f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    LOGV("setDeviceConnectionState() BT SCO  device, address %s", device_address);
82f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    // keep track of SCO device address
83f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    mScoDeviceAddress = String8(device_address, MAX_DEVICE_ADDRESS_LEN);
84f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
85f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
86f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            break;
87f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // handle output device disconnection
88f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        case AudioSystem::DEVICE_STATE_UNAVAILABLE: {
89f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (!(mAvailableOutputDevices & device)) {
90f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                LOGW("setDeviceConnectionState() device not connected: %x", device);
91f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                return INVALID_OPERATION;
92f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
93f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
94f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
95f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            LOGV("setDeviceConnectionState() disconnecting device %x", device);
96f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // remove device from available output devices
97f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mAvailableOutputDevices &= ~device;
98f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
99f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef WITH_A2DP
100f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // handle A2DP device disconnection
101f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (AudioSystem::isA2dpDevice(device)) {
102f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                status_t status = handleA2dpDisconnection(device, device_address);
103f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (status != NO_ERROR) {
104f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    mAvailableOutputDevices |= device;
105f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    return status;
106f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
107f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            } else
108f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif
109f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            {
110f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (AudioSystem::isBluetoothScoDevice(device)) {
111f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    mScoDeviceAddress = "";
112f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
113f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
114f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            } break;
115f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
116f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        default:
117f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            LOGE("setDeviceConnectionState() invalid state: %x", state);
118f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return BAD_VALUE;
119f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
120f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
121f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // request routing change if necessary
122f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        uint32_t newDevice = getNewDevice(mHardwareOutput, false);
123f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef WITH_A2DP
124f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        checkA2dpSuspend();
125f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        checkOutputForAllStrategies();
126f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // A2DP outputs must be closed after checkOutputForAllStrategies() is executed
127f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (state == AudioSystem::DEVICE_STATE_UNAVAILABLE && AudioSystem::isA2dpDevice(device)) {
128f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            closeA2dpOutputs();
129f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
130f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif
131f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        updateDeviceForStrategy();
132f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        setOutputDevice(mHardwareOutput, newDevice);
133f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
134f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (device == AudioSystem::DEVICE_OUT_WIRED_HEADSET) {
135f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            device = AudioSystem::DEVICE_IN_WIRED_HEADSET;
136f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        } else if (device == AudioSystem::DEVICE_OUT_BLUETOOTH_SCO ||
137f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                   device == AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_HEADSET ||
138f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                   device == AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_CARKIT) {
139f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            device = AudioSystem::DEVICE_IN_BLUETOOTH_SCO_HEADSET;
140f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        } else {
141f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return NO_ERROR;
142f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
143f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
144f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // handle input devices
145f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (AudioSystem::isInputDevice(device)) {
146f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
147f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        switch (state)
148f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        {
149f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // handle input device connection
150f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        case AudioSystem::DEVICE_STATE_AVAILABLE: {
151f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (mAvailableInputDevices & device) {
152f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                LOGW("setDeviceConnectionState() device already connected: %d", device);
153f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                return INVALID_OPERATION;
154f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
155f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mAvailableInputDevices |= device;
156f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
157f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            break;
158f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
159f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // handle input device disconnection
160f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        case AudioSystem::DEVICE_STATE_UNAVAILABLE: {
161f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (!(mAvailableInputDevices & device)) {
162f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                LOGW("setDeviceConnectionState() device not connected: %d", device);
163f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                return INVALID_OPERATION;
164f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
165f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mAvailableInputDevices &= ~device;
166f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            } break;
167f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
168f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        default:
169f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            LOGE("setDeviceConnectionState() invalid state: %x", state);
170f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return BAD_VALUE;
171f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
172f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
173f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        audio_io_handle_t activeInput = getActiveInput();
174f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (activeInput != 0) {
175f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            AudioInputDescriptor *inputDesc = mInputs.valueFor(activeInput);
176f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            uint32_t newDevice = getDeviceForInputSource(inputDesc->mInputSource);
177f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (newDevice != inputDesc->mDevice) {
178f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                LOGV("setDeviceConnectionState() changing device from %x to %x for input %d",
179f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        inputDesc->mDevice, newDevice, activeInput);
180f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                inputDesc->mDevice = newDevice;
181f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                AudioParameter param = AudioParameter();
182f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.addInt(String8(AudioParameter::keyRouting), (int)newDevice);
183f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mpClientInterface->setParameters(activeInput, param.toString());
184f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
185f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
186f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
187f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return NO_ERROR;
188f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
189f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
190f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    LOGW("setDeviceConnectionState() invalid device: %x", device);
191f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return BAD_VALUE;
192f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
193f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
194f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima ZavinAudioSystem::device_connection_state AudioPolicyManagerBase::getDeviceConnectionState(AudioSystem::audio_devices device,
195f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                  const char *device_address)
196f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
197f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioSystem::device_connection_state state = AudioSystem::DEVICE_STATE_UNAVAILABLE;
198f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    String8 address = String8(device_address);
199f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (AudioSystem::isOutputDevice(device)) {
200f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (device & mAvailableOutputDevices) {
201f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef WITH_A2DP
202f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (AudioSystem::isA2dpDevice(device) &&
203f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                address != "" && mA2dpDeviceAddress != address) {
204f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                return state;
205f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
206f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif
207f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (AudioSystem::isBluetoothScoDevice(device) &&
208f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                address != "" && mScoDeviceAddress != address) {
209f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                return state;
210f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
211f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            state = AudioSystem::DEVICE_STATE_AVAILABLE;
212f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
213f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else if (AudioSystem::isInputDevice(device)) {
214f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (device & mAvailableInputDevices) {
215f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            state = AudioSystem::DEVICE_STATE_AVAILABLE;
216f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
217f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
218f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
219f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return state;
220f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
221f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
222f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::setPhoneState(int state)
223f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
224f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    LOGV("setPhoneState() state %d", state);
225f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    uint32_t newDevice = 0;
226f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (state < 0 || state >= AudioSystem::NUM_MODES) {
227f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        LOGW("setPhoneState() invalid state %d", state);
228f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
229f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
230f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
231f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (state == mPhoneState ) {
232f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        LOGW("setPhoneState() setting same state %d", state);
233f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
234f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
235f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
236f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if leaving call state, handle special case of active streams
237f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // pertaining to sonification strategy see handleIncallSonification()
238f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (isInCall()) {
239f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        LOGV("setPhoneState() in call state management: new state is %d", state);
240f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
241f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            handleIncallSonification(stream, false, true);
242f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
243f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
244f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
245f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // store previous phone state for management of sonification strategy below
246f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    int oldState = mPhoneState;
247f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mPhoneState = state;
248f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    bool force = false;
249f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
250f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // are we entering or starting a call
251f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (!isStateInCall(oldState) && isStateInCall(state)) {
252f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        LOGV("  Entering call in setPhoneState()");
253f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // force routing command to audio hardware when starting a call
254f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // even if no device change is needed
255f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        force = true;
256f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else if (isStateInCall(oldState) && !isStateInCall(state)) {
257f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        LOGV("  Exiting call in setPhoneState()");
258f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // force routing command to audio hardware when exiting a call
259f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // even if no device change is needed
260f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        force = true;
261f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else if (isStateInCall(state) && (state != oldState)) {
262f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        LOGV("  Switching between telephony and VoIP in setPhoneState()");
263f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // force routing command to audio hardware when switching between telephony and VoIP
264f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // even if no device change is needed
265f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        force = true;
266f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
267f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
268f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // check for device and output changes triggered by new phone state
269f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    newDevice = getNewDevice(mHardwareOutput, false);
270f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef WITH_A2DP
271f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkA2dpSuspend();
272f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForAllStrategies();
273f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif
274f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    updateDeviceForStrategy();
275f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
276f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *hwOutputDesc = mOutputs.valueFor(mHardwareOutput);
277f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
278f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // force routing command to audio hardware when ending call
279f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // even if no device change is needed
280f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (isStateInCall(oldState) && newDevice == 0) {
281f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        newDevice = hwOutputDesc->device();
282f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
283f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
284f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // when changing from ring tone to in call mode, mute the ringing tone
285f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // immediately and delay the route change to avoid sending the ring tone
286f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // tail into the earpiece or headset.
287f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    int delayMs = 0;
288f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (isStateInCall(state) && oldState == AudioSystem::MODE_RINGTONE) {
289f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // delay the device change command by twice the output latency to have some margin
290f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // and be sure that audio buffers not yet affected by the mute are out when
291f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // we actually apply the route change
292f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        delayMs = hwOutputDesc->mLatency*2;
293f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        setStreamMute(AudioSystem::RING, true, mHardwareOutput);
294f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
295f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
296f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // change routing is necessary
297f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    setOutputDevice(mHardwareOutput, newDevice, force, delayMs);
298f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
299f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if entering in call state, handle special case of active streams
300f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // pertaining to sonification strategy see handleIncallSonification()
301f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (isStateInCall(state)) {
302f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        LOGV("setPhoneState() in call state management: new state is %d", state);
303f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // unmute the ringing tone after a sufficient delay if it was muted before
304f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // setting output device above
305f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (oldState == AudioSystem::MODE_RINGTONE) {
306f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            setStreamMute(AudioSystem::RING, false, mHardwareOutput, MUTE_TIME_MS);
307f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
308f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
309f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            handleIncallSonification(stream, true, true);
310f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
311f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
312f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
313f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
314f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (state == AudioSystem::MODE_RINGTONE &&
315f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        isStreamActive(AudioSystem::MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
316f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mLimitRingtoneVolume = true;
317f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
318f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mLimitRingtoneVolume = false;
319f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
320f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
321f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
322f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::setRingerMode(uint32_t mode, uint32_t mask)
323f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
324f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    LOGV("setRingerMode() mode %x, mask %x", mode, mask);
325f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
326f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mRingerMode = mode;
327f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
328f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
329f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::setForceUse(AudioSystem::force_use usage, AudioSystem::forced_config config)
330f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
331f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    LOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mPhoneState);
332f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
333f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    bool forceVolumeReeval = false;
334f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    switch(usage) {
335f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::FOR_COMMUNICATION:
336f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (config != AudioSystem::FORCE_SPEAKER && config != AudioSystem::FORCE_BT_SCO &&
337f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_NONE) {
338f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            LOGW("setForceUse() invalid config %d for FOR_COMMUNICATION", config);
339f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return;
340f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
341f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        forceVolumeReeval = true;
342f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mForceUse[usage] = config;
343f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
344f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::FOR_MEDIA:
345f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (config != AudioSystem::FORCE_HEADPHONES && config != AudioSystem::FORCE_BT_A2DP &&
346f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_WIRED_ACCESSORY &&
347f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_ANALOG_DOCK &&
348f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_DIGITAL_DOCK && config != AudioSystem::FORCE_NONE) {
349f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            LOGW("setForceUse() invalid config %d for FOR_MEDIA", config);
350f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return;
351f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
352f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mForceUse[usage] = config;
353f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
354f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::FOR_RECORD:
355f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (config != AudioSystem::FORCE_BT_SCO && config != AudioSystem::FORCE_WIRED_ACCESSORY &&
356f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_NONE) {
357f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            LOGW("setForceUse() invalid config %d for FOR_RECORD", config);
358f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return;
359f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
360f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mForceUse[usage] = config;
361f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
362f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::FOR_DOCK:
363f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (config != AudioSystem::FORCE_NONE && config != AudioSystem::FORCE_BT_CAR_DOCK &&
364f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_BT_DESK_DOCK &&
365f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_WIRED_ACCESSORY &&
366f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_ANALOG_DOCK &&
367f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_DIGITAL_DOCK) {
368f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            LOGW("setForceUse() invalid config %d for FOR_DOCK", config);
369f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
370f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        forceVolumeReeval = true;
371f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mForceUse[usage] = config;
372f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
373f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    default:
374f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        LOGW("setForceUse() invalid usage %d", usage);
375f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
376f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
377f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
378f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // check for device and output changes triggered by new phone state
379f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    uint32_t newDevice = getNewDevice(mHardwareOutput, false);
380f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef WITH_A2DP
381f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkA2dpSuspend();
382f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForAllStrategies();
383f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif
384f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    updateDeviceForStrategy();
385f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    setOutputDevice(mHardwareOutput, newDevice);
386f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (forceVolumeReeval) {
387f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        applyStreamVolumes(mHardwareOutput, newDevice, 0, true);
388f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
389f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
390f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    audio_io_handle_t activeInput = getActiveInput();
391f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (activeInput != 0) {
392f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioInputDescriptor *inputDesc = mInputs.valueFor(activeInput);
393f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        newDevice = getDeviceForInputSource(inputDesc->mInputSource);
394f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (newDevice != inputDesc->mDevice) {
395f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            LOGV("setForceUse() changing device from %x to %x for input %d",
396f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    inputDesc->mDevice, newDevice, activeInput);
397f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            inputDesc->mDevice = newDevice;
398f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            AudioParameter param = AudioParameter();
399f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            param.addInt(String8(AudioParameter::keyRouting), (int)newDevice);
400f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mpClientInterface->setParameters(activeInput, param.toString());
401f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
402f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
403f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
404f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
405f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
406f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima ZavinAudioSystem::forced_config AudioPolicyManagerBase::getForceUse(AudioSystem::force_use usage)
407f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
408f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return mForceUse[usage];
409f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
410f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
411f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::setSystemProperty(const char* property, const char* value)
412f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
413f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    LOGV("setSystemProperty() property %s, value %s", property, value);
414f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (strcmp(property, "ro.camera.sound.forced") == 0) {
415f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (atoi(value)) {
416f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            LOGV("ENFORCED_AUDIBLE cannot be muted");
417f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mStreams[AudioSystem::ENFORCED_AUDIBLE].mCanBeMuted = false;
418f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        } else {
419f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            LOGV("ENFORCED_AUDIBLE can be muted");
420f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mStreams[AudioSystem::ENFORCED_AUDIBLE].mCanBeMuted = true;
421f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
422f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
423f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
424f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
425f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinaudio_io_handle_t AudioPolicyManagerBase::getOutput(AudioSystem::stream_type stream,
426f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    uint32_t samplingRate,
427f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    uint32_t format,
428f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    uint32_t channels,
429f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    AudioSystem::output_flags flags)
430f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
431f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    audio_io_handle_t output = 0;
432f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    uint32_t latency = 0;
433f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    routing_strategy strategy = getStrategy((AudioSystem::stream_type)stream);
434f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    uint32_t device = getDeviceForStrategy(strategy);
435f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    LOGV("getOutput() stream %d, samplingRate %d, format %d, channels %x, flags %x", stream, samplingRate, format, channels, flags);
436f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
437f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
438f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mCurOutput != 0) {
439f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        LOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channels %x, mDirectOutput %d",
440f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
441f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
442f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (mTestOutputs[mCurOutput] == 0) {
443f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            LOGV("getOutput() opening test output");
444f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor();
445f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            outputDesc->mDevice = mTestDevice;
446f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            outputDesc->mSamplingRate = mTestSamplingRate;
447f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            outputDesc->mFormat = mTestFormat;
448f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            outputDesc->mChannels = mTestChannels;
449f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            outputDesc->mLatency = mTestLatencyMs;
450f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            outputDesc->mFlags = (AudioSystem::output_flags)(mDirectOutput ? AudioSystem::OUTPUT_FLAG_DIRECT : 0);
451f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            outputDesc->mRefCount[stream] = 0;
452f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mTestOutputs[mCurOutput] = mpClientInterface->openOutput(&outputDesc->mDevice,
453f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            &outputDesc->mSamplingRate,
454f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            &outputDesc->mFormat,
455f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            &outputDesc->mChannels,
456f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            &outputDesc->mLatency,
457f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            outputDesc->mFlags);
458f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (mTestOutputs[mCurOutput]) {
459f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                AudioParameter outputCmd = AudioParameter();
460f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                outputCmd.addInt(String8("set_id"),mCurOutput);
461f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
462f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                addOutput(mTestOutputs[mCurOutput], outputDesc);
463f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
464f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
465f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return mTestOutputs[mCurOutput];
466f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
467f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
468f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
469f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // open a direct output if required by specified parameters
470f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (needsDirectOuput(stream, samplingRate, format, channels, flags, device)) {
471f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
472f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        LOGV("getOutput() opening direct output device %x", device);
473f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor();
474f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mDevice = device;
475f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mSamplingRate = samplingRate;
476f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mFormat = format;
477f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mChannels = channels;
478f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mLatency = 0;
479f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mFlags = (AudioSystem::output_flags)(flags | AudioSystem::OUTPUT_FLAG_DIRECT);
480f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mRefCount[stream] = 0;
481f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mStopTime[stream] = 0;
482f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        output = mpClientInterface->openOutput(&outputDesc->mDevice,
483f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                        &outputDesc->mSamplingRate,
484f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                        &outputDesc->mFormat,
485f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                        &outputDesc->mChannels,
486f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                        &outputDesc->mLatency,
487f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                        outputDesc->mFlags);
488f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
489f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // only accept an output with the requeted parameters
490f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (output == 0 ||
491f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            (samplingRate != 0 && samplingRate != outputDesc->mSamplingRate) ||
492f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            (format != 0 && format != outputDesc->mFormat) ||
493f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            (channels != 0 && channels != outputDesc->mChannels)) {
494f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            LOGV("getOutput() failed opening direct output: samplingRate %d, format %d, channels %d",
495f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    samplingRate, format, channels);
496f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (output != 0) {
497f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mpClientInterface->closeOutput(output);
498f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
499f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            delete outputDesc;
500f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return 0;
501f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
502f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        addOutput(output, outputDesc);
503f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return output;
504f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
505f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
506f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (channels != 0 && channels != AudioSystem::CHANNEL_OUT_MONO &&
507f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        channels != AudioSystem::CHANNEL_OUT_STEREO) {
508f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return 0;
509f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
510f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // open a non direct output
511f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
512f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // get which output is suitable for the specified stream. The actual routing change will happen
513f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // when startOutput() will be called
514f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    uint32_t a2dpDevice = device & AudioSystem::DEVICE_OUT_ALL_A2DP;
515f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (AudioSystem::popCount((AudioSystem::audio_devices)device) == 2) {
516f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef WITH_A2DP
517f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (a2dpUsedForSonification() && a2dpDevice != 0) {
518f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // if playing on 2 devices among which one is A2DP, use duplicated output
519f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            LOGV("getOutput() using duplicated output");
520f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            LOGW_IF((mA2dpOutput == 0), "getOutput() A2DP device in multiple %x selected but A2DP output not opened", device);
521f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            output = mDuplicatedOutput;
522f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        } else
523f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif
524f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        {
525f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // if playing on 2 devices among which none is A2DP, use hardware output
526f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            output = mHardwareOutput;
527f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
528f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        LOGV("getOutput() using output %d for 2 devices %x", output, device);
529f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
530f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef WITH_A2DP
531f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (a2dpDevice != 0) {
532f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // if playing on A2DP device, use a2dp output
533f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            LOGW_IF((mA2dpOutput == 0), "getOutput() A2DP device %x selected but A2DP output not opened", device);
534f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            output = mA2dpOutput;
535f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        } else
536f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif
537f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        {
538f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // if playing on not A2DP device, use hardware output
539f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            output = mHardwareOutput;
540f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
541f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
542f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
543f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
544e81531e91ecae92aff471dbff9cbeb0f95ff4a80Dima Zavin    LOGW_IF((output ==0), "getOutput() could not find output for stream %d, samplingRate %d, format %d, channels %x, flags %x",
545f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                stream, samplingRate, format, channels, flags);
546f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
547f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return output;
548f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
549f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
550f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::startOutput(audio_io_handle_t output,
551f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                             AudioSystem::stream_type stream,
552f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                             int session)
553f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
554f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    LOGV("startOutput() output %d, stream %d, session %d", output, stream, session);
555f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mOutputs.indexOfKey(output);
556f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
557f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        LOGW("startOutput() unknow output %d", output);
558f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
559f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
560f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
561f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index);
562f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    routing_strategy strategy = getStrategy((AudioSystem::stream_type)stream);
563f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
564f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef WITH_A2DP
565f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mA2dpOutput != 0  && !a2dpUsedForSonification() && strategy == STRATEGY_SONIFICATION) {
566f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        setStrategyMute(STRATEGY_MEDIA, true, mA2dpOutput);
567f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
568f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif
569f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
570f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // incremenent usage count for this stream on the requested output:
571f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // NOTE that the usage count is the same for duplicated output and hardware output which is
572f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // necassary for a correct control of hardware output routing by startOutput() and stopOutput()
573f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    outputDesc->changeRefCount(stream, 1);
574f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
575f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    setOutputDevice(output, getNewDevice(output));
576f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
577f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // handle special case for sonification while in call
578f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (isInCall()) {
579f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        handleIncallSonification(stream, true, false);
580f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
581f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
582f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // apply volume rules for current stream and device if necessary
583f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkAndSetVolume(stream, mStreams[stream].mIndexCur, output, outputDesc->device());
584f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
585f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
586f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
587f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
588f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::stopOutput(audio_io_handle_t output,
589f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            AudioSystem::stream_type stream,
590f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            int session)
591f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
592f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    LOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
593f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mOutputs.indexOfKey(output);
594f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
595f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        LOGW("stopOutput() unknow output %d", output);
596f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
597f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
598f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
599f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index);
600f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    routing_strategy strategy = getStrategy((AudioSystem::stream_type)stream);
601f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
602f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // handle special case for sonification while in call
603f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (isInCall()) {
604f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        handleIncallSonification(stream, false, false);
605f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
606f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
607f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (outputDesc->mRefCount[stream] > 0) {
608f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // decrement usage count of this stream on the output
609f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->changeRefCount(stream, -1);
610f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // store time at which the stream was stopped - see isStreamActive()
611f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mStopTime[stream] = systemTime();
612f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
613f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        setOutputDevice(output, getNewDevice(output), false, outputDesc->mLatency*2);
614f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
615f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef WITH_A2DP
616f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (mA2dpOutput != 0 && !a2dpUsedForSonification() &&
617f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                strategy == STRATEGY_SONIFICATION) {
618f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            setStrategyMute(STRATEGY_MEDIA,
619f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                            false,
620f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                            mA2dpOutput,
621f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                            mOutputs.valueFor(mHardwareOutput)->mLatency*2);
622f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
623f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif
624f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (output != mHardwareOutput) {
625f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            setOutputDevice(mHardwareOutput, getNewDevice(mHardwareOutput), true);
626f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
627f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return NO_ERROR;
628f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
629f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        LOGW("stopOutput() refcount is already 0 for output %d", output);
630f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return INVALID_OPERATION;
631f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
632f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
633f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
634f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::releaseOutput(audio_io_handle_t output)
635f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
636f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    LOGV("releaseOutput() %d", output);
637f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mOutputs.indexOfKey(output);
638f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
639f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        LOGW("releaseOutput() releasing unknown output %d", output);
640f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
641f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
642f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
643f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
644f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    int testIndex = testOutputIndex(output);
645f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (testIndex != 0) {
646f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index);
647f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (outputDesc->refCount() == 0) {
648f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mpClientInterface->closeOutput(output);
649f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            delete mOutputs.valueAt(index);
650f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mOutputs.removeItem(output);
651f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mTestOutputs[testIndex] = 0;
652f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
653f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
654f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
655f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
656f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
657f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mOutputs.valueAt(index)->mFlags & AudioSystem::OUTPUT_FLAG_DIRECT) {
658f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mpClientInterface->closeOutput(output);
659f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        delete mOutputs.valueAt(index);
660f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mOutputs.removeItem(output);
661f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
662f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
663f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
664f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinaudio_io_handle_t AudioPolicyManagerBase::getInput(int inputSource,
665f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    uint32_t samplingRate,
666f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    uint32_t format,
667f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    uint32_t channels,
668f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    AudioSystem::audio_in_acoustics acoustics)
669f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
670f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    audio_io_handle_t input = 0;
671f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    uint32_t device = getDeviceForInputSource(inputSource);
672f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
673f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    LOGV("getInput() inputSource %d, samplingRate %d, format %d, channels %x, acoustics %x", inputSource, samplingRate, format, channels, acoustics);
674f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
675f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (device == 0) {
676f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return 0;
677f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
678f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
679f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // adapt channel selection to input source
680f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    switch(inputSource) {
681f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_UPLINK:
682f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        channels = AudioSystem::CHANNEL_IN_VOICE_UPLINK;
683f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
684f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_DOWNLINK:
685f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        channels = AudioSystem::CHANNEL_IN_VOICE_DNLINK;
686f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
687f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_CALL:
688f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        channels = (AudioSystem::CHANNEL_IN_VOICE_UPLINK | AudioSystem::CHANNEL_IN_VOICE_DNLINK);
689f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
690f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    default:
691f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
692f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
693f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
694f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioInputDescriptor *inputDesc = new AudioInputDescriptor();
695f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
696f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    inputDesc->mInputSource = inputSource;
697f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    inputDesc->mDevice = device;
698f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    inputDesc->mSamplingRate = samplingRate;
699f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    inputDesc->mFormat = format;
700f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    inputDesc->mChannels = channels;
701f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    inputDesc->mAcoustics = acoustics;
702f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    inputDesc->mRefCount = 0;
703f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    input = mpClientInterface->openInput(&inputDesc->mDevice,
704f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    &inputDesc->mSamplingRate,
705f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    &inputDesc->mFormat,
706f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    &inputDesc->mChannels,
707f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    inputDesc->mAcoustics);
708f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
709f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // only accept input with the exact requested set of parameters
710f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (input == 0 ||
711f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        (samplingRate != inputDesc->mSamplingRate) ||
712f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        (format != inputDesc->mFormat) ||
713f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        (channels != inputDesc->mChannels)) {
714f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        LOGV("getInput() failed opening input: samplingRate %d, format %d, channels %d",
715f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                samplingRate, format, channels);
716f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (input != 0) {
717f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mpClientInterface->closeInput(input);
718f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
719f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        delete inputDesc;
720f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return 0;
721f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
722f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mInputs.add(input, inputDesc);
723f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return input;
724f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
725f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
726f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::startInput(audio_io_handle_t input)
727f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
728f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    LOGV("startInput() input %d", input);
729f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mInputs.indexOfKey(input);
730f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
731f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        LOGW("startInput() unknow input %d", input);
732f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
733f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
734f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioInputDescriptor *inputDesc = mInputs.valueAt(index);
735f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
736f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
737f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mTestInput == 0)
738f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
739f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    {
740f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // refuse 2 active AudioRecord clients at the same time
741f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (getActiveInput() != 0) {
742f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            LOGW("startInput() input %d failed: other input already started", input);
743f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return INVALID_OPERATION;
744f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
745f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
746f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
747f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioParameter param = AudioParameter();
748f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    param.addInt(String8(AudioParameter::keyRouting), (int)inputDesc->mDevice);
749f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
750f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    param.addInt(String8(AudioParameter::keyInputSource), (int)inputDesc->mInputSource);
751f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    LOGV("AudioPolicyManager::startInput() input source = %d", inputDesc->mInputSource);
752f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
753f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mpClientInterface->setParameters(input, param.toString());
754f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
755f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    inputDesc->mRefCount = 1;
756f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
757f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
758f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
759f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::stopInput(audio_io_handle_t input)
760f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
761f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    LOGV("stopInput() input %d", input);
762f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mInputs.indexOfKey(input);
763f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
764f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        LOGW("stopInput() unknow input %d", input);
765f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
766f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
767f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioInputDescriptor *inputDesc = mInputs.valueAt(index);
768f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
769f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (inputDesc->mRefCount == 0) {
770f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        LOGW("stopInput() input %d already stopped", input);
771f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return INVALID_OPERATION;
772f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
773f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioParameter param = AudioParameter();
774f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        param.addInt(String8(AudioParameter::keyRouting), 0);
775f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mpClientInterface->setParameters(input, param.toString());
776f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        inputDesc->mRefCount = 0;
777f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return NO_ERROR;
778f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
779f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
780f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
781f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::releaseInput(audio_io_handle_t input)
782f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
783f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    LOGV("releaseInput() %d", input);
784f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mInputs.indexOfKey(input);
785f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
786f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        LOGW("releaseInput() releasing unknown input %d", input);
787f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
788f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
789f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mpClientInterface->closeInput(input);
790f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    delete mInputs.valueAt(index);
791f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mInputs.removeItem(input);
792f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    LOGV("releaseInput() exit");
793f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
794f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
795f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::initStreamVolume(AudioSystem::stream_type stream,
796f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            int indexMin,
797f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            int indexMax)
798f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
799f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    LOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
800f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (indexMin < 0 || indexMin >= indexMax) {
801f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        LOGW("initStreamVolume() invalid index limits for stream %d, min %d, max %d", stream , indexMin, indexMax);
802f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
803f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
804f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mStreams[stream].mIndexMin = indexMin;
805f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mStreams[stream].mIndexMax = indexMax;
806f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
807f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
808f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::setStreamVolumeIndex(AudioSystem::stream_type stream, int index)
809f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
810f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
811f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if ((index < mStreams[stream].mIndexMin) || (index > mStreams[stream].mIndexMax)) {
812f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
813f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
814f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
815f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // Force max volume if stream cannot be muted
816f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (!mStreams[stream].mCanBeMuted) index = mStreams[stream].mIndexMax;
817f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
818f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    LOGV("setStreamVolumeIndex() stream %d, index %d", stream, index);
819f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mStreams[stream].mIndexCur = index;
820f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
821f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // compute and apply stream volume on all outputs according to connected device
822f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    status_t status = NO_ERROR;
823f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mOutputs.size(); i++) {
824f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        status_t volStatus = checkAndSetVolume(stream, index, mOutputs.keyAt(i), mOutputs.valueAt(i)->device());
825f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (volStatus != NO_ERROR) {
826f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            status = volStatus;
827f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
828f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
829f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return status;
830f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
831f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
832f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::getStreamVolumeIndex(AudioSystem::stream_type stream, int *index)
833f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
834f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index == 0) {
835f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
836f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
837f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    LOGV("getStreamVolumeIndex() stream %d", stream);
838f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    *index =  mStreams[stream].mIndexCur;
839f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
840f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
841f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
842f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinaudio_io_handle_t AudioPolicyManagerBase::getOutputForEffect(effect_descriptor_t *desc)
843f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
844f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    LOGV("getOutputForEffect()");
845f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // apply simple rule where global effects are attached to the same output as MUSIC streams
846f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return getOutput(AudioSystem::MUSIC);
847f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
848f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
849f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::registerEffect(effect_descriptor_t *desc,
850f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                audio_io_handle_t output,
851f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                uint32_t strategy,
852f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                int session,
853f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                int id)
854f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
855f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mOutputs.indexOfKey(output);
856f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
857f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        LOGW("registerEffect() unknown output %d", output);
858f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return INVALID_OPERATION;
859f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
860f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
861f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mTotalEffectsCpuLoad + desc->cpuLoad > getMaxEffectsCpuLoad()) {
862f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        LOGW("registerEffect() CPU Load limit exceeded for Fx %s, CPU %f MIPS",
863f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                desc->name, (float)desc->cpuLoad/10);
864f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return INVALID_OPERATION;
865f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
866f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mTotalEffectsMemory + desc->memoryUsage > getMaxEffectsMemory()) {
867f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        LOGW("registerEffect() memory limit exceeded for Fx %s, Memory %d KB",
868f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                desc->name, desc->memoryUsage);
869f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return INVALID_OPERATION;
870f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
871f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mTotalEffectsCpuLoad += desc->cpuLoad;
872f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mTotalEffectsMemory += desc->memoryUsage;
873f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    LOGV("registerEffect() effect %s, output %d, strategy %d session %d id %d",
874f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            desc->name, output, strategy, session, id);
875f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
876f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    LOGV("registerEffect() CPU %d, memory %d", desc->cpuLoad, desc->memoryUsage);
877f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    LOGV("  total CPU %d, total memory %d", mTotalEffectsCpuLoad, mTotalEffectsMemory);
878f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
879f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    EffectDescriptor *pDesc = new EffectDescriptor();
880f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    memcpy (&pDesc->mDesc, desc, sizeof(effect_descriptor_t));
881f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    pDesc->mOutput = output;
882f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    pDesc->mStrategy = (routing_strategy)strategy;
883f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    pDesc->mSession = session;
884f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mEffects.add(id, pDesc);
885f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
886f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
887f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
888f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
889f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::unregisterEffect(int id)
890f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
891f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mEffects.indexOfKey(id);
892f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
893f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        LOGW("unregisterEffect() unknown effect ID %d", id);
894f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return INVALID_OPERATION;
895f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
896f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
897f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    EffectDescriptor *pDesc = mEffects.valueAt(index);
898f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
899f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mTotalEffectsCpuLoad < pDesc->mDesc.cpuLoad) {
900f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        LOGW("unregisterEffect() CPU load %d too high for total %d",
901f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                pDesc->mDesc.cpuLoad, mTotalEffectsCpuLoad);
902f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        pDesc->mDesc.cpuLoad = mTotalEffectsCpuLoad;
903f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
904f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mTotalEffectsCpuLoad -= pDesc->mDesc.cpuLoad;
905f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mTotalEffectsMemory < pDesc->mDesc.memoryUsage) {
906f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        LOGW("unregisterEffect() memory %d too big for total %d",
907f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                pDesc->mDesc.memoryUsage, mTotalEffectsMemory);
908f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        pDesc->mDesc.memoryUsage = mTotalEffectsMemory;
909f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
910f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mTotalEffectsMemory -= pDesc->mDesc.memoryUsage;
911f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    LOGV("unregisterEffect() effect %s, ID %d, CPU %d, memory %d",
912f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            pDesc->mDesc.name, id, pDesc->mDesc.cpuLoad, pDesc->mDesc.memoryUsage);
913f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    LOGV("  total CPU %d, total memory %d", mTotalEffectsCpuLoad, mTotalEffectsMemory);
914f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
915f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mEffects.removeItem(id);
916f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    delete pDesc;
917f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
918f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
919f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
920f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
921f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinbool AudioPolicyManagerBase::isStreamActive(int stream, uint32_t inPastMs) const
922f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
923f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    nsecs_t sysTime = systemTime();
924f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mOutputs.size(); i++) {
925f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (mOutputs.valueAt(i)->mRefCount[stream] != 0 ||
926f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            ns2ms(sysTime - mOutputs.valueAt(i)->mStopTime[stream]) < inPastMs) {
927f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return true;
928f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
929f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
930f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return false;
931f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
932f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
933f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
934f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::dump(int fd)
935f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
936f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    const size_t SIZE = 256;
937f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    char buffer[SIZE];
938f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    String8 result;
939f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
940f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
941f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
942f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Hardware Output: %d\n", mHardwareOutput);
943f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
944f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef WITH_A2DP
945f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " A2DP Output: %d\n", mA2dpOutput);
946f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
947f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Duplicated Output: %d\n", mDuplicatedOutput);
948f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
949f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " A2DP device address: %s\n", mA2dpDeviceAddress.string());
950f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
951f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif
952f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " SCO device address: %s\n", mScoDeviceAddress.string());
953f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
954f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Output devices: %08x\n", mAvailableOutputDevices);
955f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
956f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Input devices: %08x\n", mAvailableInputDevices);
957f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
958f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Phone state: %d\n", mPhoneState);
959f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
960f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Ringer mode: %d\n", mRingerMode);
961f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
962f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Force use for communications %d\n", mForceUse[AudioSystem::FOR_COMMUNICATION]);
963f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
964f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Force use for media %d\n", mForceUse[AudioSystem::FOR_MEDIA]);
965f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
966f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Force use for record %d\n", mForceUse[AudioSystem::FOR_RECORD]);
967f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
968f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Force use for dock %d\n", mForceUse[AudioSystem::FOR_DOCK]);
969f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
970f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, result.string(), result.size());
971f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
972f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "\nOutputs dump:\n");
973f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
974f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mOutputs.size(); i++) {
975f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        snprintf(buffer, SIZE, "- Output %d dump:\n", mOutputs.keyAt(i));
976f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        write(fd, buffer, strlen(buffer));
977f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mOutputs.valueAt(i)->dump(fd);
978f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
979f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
980f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "\nInputs dump:\n");
981f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
982f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mInputs.size(); i++) {
983f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        snprintf(buffer, SIZE, "- Input %d dump:\n", mInputs.keyAt(i));
984f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        write(fd, buffer, strlen(buffer));
985f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mInputs.valueAt(i)->dump(fd);
986f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
987f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
988f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "\nStreams dump:\n");
989f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
990f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Stream  Index Min  Index Max  Index Cur  Can be muted\n");
991f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
992f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) {
993f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        snprintf(buffer, SIZE, " %02d", i);
994f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mStreams[i].dump(buffer + 3, SIZE);
995f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        write(fd, buffer, strlen(buffer));
996f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
997f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
998f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "\nTotal Effects CPU: %f MIPS, Total Effects memory: %d KB\n",
999f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            (float)mTotalEffectsCpuLoad/10, mTotalEffectsMemory);
1000f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
1001f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1002f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "Registered effects:\n");
1003f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
1004f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mEffects.size(); i++) {
1005f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        snprintf(buffer, SIZE, "- Effect %d dump:\n", mEffects.keyAt(i));
1006f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        write(fd, buffer, strlen(buffer));
1007f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mEffects.valueAt(i)->dump(fd);
1008f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1009f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1010f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1011f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
1012f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1013f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1014f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// ----------------------------------------------------------------------------
1015f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// AudioPolicyManagerBase
1016f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// ----------------------------------------------------------------------------
1017f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1018f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima ZavinAudioPolicyManagerBase::AudioPolicyManagerBase(AudioPolicyClientInterface *clientInterface)
1019f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    :
1020f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
1021f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    Thread(false),
1022f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
1023f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mPhoneState(AudioSystem::MODE_NORMAL), mRingerMode(0),
1024f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
1025f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mTotalEffectsCpuLoad(0), mTotalEffectsMemory(0),
1026f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mA2dpSuspended(false)
1027f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1028f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mpClientInterface = clientInterface;
1029f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1030f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < AudioSystem::NUM_FORCE_USE; i++) {
1031f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mForceUse[i] = AudioSystem::FORCE_NONE;
1032f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1033f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1034f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    initializeVolumeCurves();
1035f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1036f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // devices available by default are speaker, ear piece and microphone
1037f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mAvailableOutputDevices = AudioSystem::DEVICE_OUT_EARPIECE |
1038f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        AudioSystem::DEVICE_OUT_SPEAKER;
1039f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mAvailableInputDevices = AudioSystem::DEVICE_IN_BUILTIN_MIC;
1040f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1041f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef WITH_A2DP
1042f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mA2dpOutput = 0;
1043f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mDuplicatedOutput = 0;
1044f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mA2dpDeviceAddress = String8("");
1045f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif
1046f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mScoDeviceAddress = String8("");
1047f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1048f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // open hardware output
1049f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor();
1050f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    outputDesc->mDevice = (uint32_t)AudioSystem::DEVICE_OUT_SPEAKER;
1051f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mHardwareOutput = mpClientInterface->openOutput(&outputDesc->mDevice,
1052f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    &outputDesc->mSamplingRate,
1053f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    &outputDesc->mFormat,
1054f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    &outputDesc->mChannels,
1055f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    &outputDesc->mLatency,
1056f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    outputDesc->mFlags);
1057f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1058f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mHardwareOutput == 0) {
1059f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        LOGE("Failed to initialize hardware output stream, samplingRate: %d, format %d, channels %d",
1060f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannels);
1061f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
1062f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        addOutput(mHardwareOutput, outputDesc);
1063f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        setOutputDevice(mHardwareOutput, (uint32_t)AudioSystem::DEVICE_OUT_SPEAKER, true);
1064f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        //TODO: configure audio effect output stage here
1065f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1066f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1067f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    updateDeviceForStrategy();
1068f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
1069f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mHardwareOutput != 0) {
1070f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioParameter outputCmd = AudioParameter();
1071f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputCmd.addInt(String8("set_id"), 0);
1072f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mpClientInterface->setParameters(mHardwareOutput, outputCmd.toString());
1073f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1074f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mTestDevice = AudioSystem::DEVICE_OUT_SPEAKER;
1075f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mTestSamplingRate = 44100;
1076f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mTestFormat = AudioSystem::PCM_16_BIT;
1077f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mTestChannels =  AudioSystem::CHANNEL_OUT_STEREO;
1078f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mTestLatencyMs = 0;
1079f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mCurOutput = 0;
1080f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mDirectOutput = false;
1081f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
1082f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mTestOutputs[i] = 0;
1083f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1084f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1085f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        const size_t SIZE = 256;
1086f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        char buffer[SIZE];
1087f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        snprintf(buffer, SIZE, "AudioPolicyManagerTest");
1088f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        run(buffer, ANDROID_PRIORITY_AUDIO);
1089f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1090f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
1091f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1092f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1093f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima ZavinAudioPolicyManagerBase::~AudioPolicyManagerBase()
1094f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1095f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
1096f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    exit();
1097f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
1098f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin   for (size_t i = 0; i < mOutputs.size(); i++) {
1099f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mpClientInterface->closeOutput(mOutputs.keyAt(i));
1100f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        delete mOutputs.valueAt(i);
1101f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin   }
1102f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin   mOutputs.clear();
1103f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin   for (size_t i = 0; i < mInputs.size(); i++) {
1104f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mpClientInterface->closeInput(mInputs.keyAt(i));
1105f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        delete mInputs.valueAt(i);
1106f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin   }
1107f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin   mInputs.clear();
1108f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1109f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1110f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::initCheck()
1111f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1112f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return (mHardwareOutput == 0) ? NO_INIT : NO_ERROR;
1113f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1114f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1115f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
1116f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinbool AudioPolicyManagerBase::threadLoop()
1117f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1118f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    LOGV("entering threadLoop()");
1119f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    while (!exitPending())
1120f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    {
1121f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        String8 command;
1122f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        int valueInt;
1123f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        String8 value;
1124f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1125f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        Mutex::Autolock _l(mLock);
1126f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mWaitWorkCV.waitRelative(mLock, milliseconds(50));
1127f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1128f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        command = mpClientInterface->getParameters(0, String8("test_cmd_policy"));
1129f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioParameter param = AudioParameter(command);
1130f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1131f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR &&
1132f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            valueInt != 0) {
1133f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            LOGV("Test command %s received", command.string());
1134f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            String8 target;
1135f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.get(String8("target"), target) != NO_ERROR) {
1136f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                target = "Manager";
1137f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1138f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) {
1139f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_output"));
1140f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mCurOutput = valueInt;
1141f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1142f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) {
1143f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_direct"));
1144f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (value == "false") {
1145f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    mDirectOutput = false;
1146f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else if (value == "true") {
1147f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    mDirectOutput = true;
1148f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1149f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1150f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) {
1151f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_input"));
1152f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mTestInput = valueInt;
1153f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1154f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1155f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) {
1156f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_format"));
1157f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                int format = AudioSystem::INVALID_FORMAT;
1158f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (value == "PCM 16 bits") {
1159f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    format = AudioSystem::PCM_16_BIT;
1160f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else if (value == "PCM 8 bits") {
1161f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    format = AudioSystem::PCM_8_BIT;
1162f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else if (value == "Compressed MP3") {
1163f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    format = AudioSystem::MP3;
1164f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1165f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (format != AudioSystem::INVALID_FORMAT) {
1166f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    if (target == "Manager") {
1167f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mTestFormat = format;
1168f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    } else if (mTestOutputs[mCurOutput] != 0) {
1169f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        AudioParameter outputParam = AudioParameter();
1170f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        outputParam.addInt(String8("format"), format);
1171f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
1172f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    }
1173f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1174f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1175f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) {
1176f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_channels"));
1177f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                int channels = 0;
1178f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1179f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (value == "Channels Stereo") {
1180f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    channels =  AudioSystem::CHANNEL_OUT_STEREO;
1181f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else if (value == "Channels Mono") {
1182f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    channels =  AudioSystem::CHANNEL_OUT_MONO;
1183f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1184f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (channels != 0) {
1185f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    if (target == "Manager") {
1186f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mTestChannels = channels;
1187f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    } else if (mTestOutputs[mCurOutput] != 0) {
1188f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        AudioParameter outputParam = AudioParameter();
1189f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        outputParam.addInt(String8("channels"), channels);
1190f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
1191f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    }
1192f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1193f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1194f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) {
1195f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_sampleRate"));
1196f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (valueInt >= 0 && valueInt <= 96000) {
1197f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    int samplingRate = valueInt;
1198f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    if (target == "Manager") {
1199f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mTestSamplingRate = samplingRate;
1200f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    } else if (mTestOutputs[mCurOutput] != 0) {
1201f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        AudioParameter outputParam = AudioParameter();
1202f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        outputParam.addInt(String8("sampling_rate"), samplingRate);
1203f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
1204f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    }
1205f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1206f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1207f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1208f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) {
1209f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_reopen"));
1210f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1211f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mpClientInterface->closeOutput(mHardwareOutput);
1212f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                delete mOutputs.valueFor(mHardwareOutput);
1213f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mOutputs.removeItem(mHardwareOutput);
1214f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1215f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor();
1216f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                outputDesc->mDevice = (uint32_t)AudioSystem::DEVICE_OUT_SPEAKER;
1217f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mHardwareOutput = mpClientInterface->openOutput(&outputDesc->mDevice,
1218f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                &outputDesc->mSamplingRate,
1219f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                &outputDesc->mFormat,
1220f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                &outputDesc->mChannels,
1221f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                &outputDesc->mLatency,
1222f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                outputDesc->mFlags);
1223f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (mHardwareOutput == 0) {
1224f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    LOGE("Failed to reopen hardware output stream, samplingRate: %d, format %d, channels %d",
1225f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                            outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannels);
1226f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else {
1227f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    AudioParameter outputCmd = AudioParameter();
1228f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    outputCmd.addInt(String8("set_id"), 0);
1229f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    mpClientInterface->setParameters(mHardwareOutput, outputCmd.toString());
1230f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    addOutput(mHardwareOutput, outputDesc);
1231f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1232f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1233f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1234f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1235f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mpClientInterface->setParameters(0, String8("test_cmd_policy="));
1236f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1237f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1238f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return false;
1239f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1240f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1241f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::exit()
1242f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1243f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    {
1244f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AutoMutex _l(mLock);
1245f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        requestExit();
1246f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mWaitWorkCV.signal();
1247f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1248f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    requestExitAndWait();
1249f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1250f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1251f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinint AudioPolicyManagerBase::testOutputIndex(audio_io_handle_t output)
1252f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1253f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
1254f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (output == mTestOutputs[i]) return i;
1255f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1256f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return 0;
1257f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1258f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
1259f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1260f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// ---
1261f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1262f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::addOutput(audio_io_handle_t id, AudioOutputDescriptor *outputDesc)
1263f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1264f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    outputDesc->mId = id;
1265f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mOutputs.add(id, outputDesc);
1266f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1267f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1268f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1269f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef WITH_A2DP
1270f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::handleA2dpConnection(AudioSystem::audio_devices device,
1271f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                 const char *device_address)
1272f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1273f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // when an A2DP device is connected, open an A2DP and a duplicated output
1274f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    LOGV("opening A2DP output for device %s", device_address);
1275f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor();
1276f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    outputDesc->mDevice = device;
1277f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mA2dpOutput = mpClientInterface->openOutput(&outputDesc->mDevice,
1278f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            &outputDesc->mSamplingRate,
1279f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            &outputDesc->mFormat,
1280f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            &outputDesc->mChannels,
1281f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            &outputDesc->mLatency,
1282f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            outputDesc->mFlags);
1283f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mA2dpOutput) {
1284f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // add A2DP output descriptor
1285f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        addOutput(mA2dpOutput, outputDesc);
1286f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1287f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        //TODO: configure audio effect output stage here
1288f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1289f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // set initial stream volume for A2DP device
1290f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        applyStreamVolumes(mA2dpOutput, device);
1291f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (a2dpUsedForSonification()) {
1292f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mDuplicatedOutput = mpClientInterface->openDuplicateOutput(mA2dpOutput, mHardwareOutput);
1293f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1294f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (mDuplicatedOutput != 0 ||
1295f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            !a2dpUsedForSonification()) {
1296f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // If both A2DP and duplicated outputs are open, send device address to A2DP hardware
1297f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // interface
1298f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            AudioParameter param;
1299f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            param.add(String8("a2dp_sink_address"), String8(device_address));
1300f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mpClientInterface->setParameters(mA2dpOutput, param.toString());
1301f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mA2dpDeviceAddress = String8(device_address, MAX_DEVICE_ADDRESS_LEN);
1302f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1303f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (a2dpUsedForSonification()) {
1304f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                // add duplicated output descriptor
1305f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                AudioOutputDescriptor *dupOutputDesc = new AudioOutputDescriptor();
1306f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                dupOutputDesc->mOutput1 = mOutputs.valueFor(mHardwareOutput);
1307f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                dupOutputDesc->mOutput2 = mOutputs.valueFor(mA2dpOutput);
1308f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                dupOutputDesc->mSamplingRate = outputDesc->mSamplingRate;
1309f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                dupOutputDesc->mFormat = outputDesc->mFormat;
1310f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                dupOutputDesc->mChannels = outputDesc->mChannels;
1311f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                dupOutputDesc->mLatency = outputDesc->mLatency;
1312f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                addOutput(mDuplicatedOutput, dupOutputDesc);
1313f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                applyStreamVolumes(mDuplicatedOutput, device);
1314f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1315f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        } else {
1316f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            LOGW("getOutput() could not open duplicated output for %d and %d",
1317f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    mHardwareOutput, mA2dpOutput);
1318f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mpClientInterface->closeOutput(mA2dpOutput);
1319f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mOutputs.removeItem(mA2dpOutput);
1320f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mA2dpOutput = 0;
1321f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            delete outputDesc;
1322f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return NO_INIT;
1323f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1324f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
1325f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        LOGW("setDeviceConnectionState() could not open A2DP output for device %x", device);
1326f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        delete outputDesc;
1327f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return NO_INIT;
1328f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1329f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *hwOutputDesc = mOutputs.valueFor(mHardwareOutput);
1330f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1331f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (!a2dpUsedForSonification()) {
1332f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // mute music on A2DP output if a notification or ringtone is playing
1333f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        uint32_t refCount = hwOutputDesc->strategyRefCount(STRATEGY_SONIFICATION);
1334f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        for (uint32_t i = 0; i < refCount; i++) {
1335f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            setStrategyMute(STRATEGY_MEDIA, true, mA2dpOutput);
1336f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1337f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1338f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1339f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mA2dpSuspended = false;
1340f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1341f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
1342f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1343f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1344f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::handleA2dpDisconnection(AudioSystem::audio_devices device,
1345f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                    const char *device_address)
1346f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1347f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mA2dpOutput == 0) {
1348f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        LOGW("setDeviceConnectionState() disconnecting A2DP and no A2DP output!");
1349f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return INVALID_OPERATION;
1350f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1351f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1352f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mA2dpDeviceAddress != device_address) {
1353f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        LOGW("setDeviceConnectionState() disconnecting unknow A2DP sink address %s", device_address);
1354f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return INVALID_OPERATION;
1355f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1356f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1357f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // mute media strategy to avoid outputting sound on hardware output while music stream
1358f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // is switched from A2DP output and before music is paused by music application
1359f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    setStrategyMute(STRATEGY_MEDIA, true, mHardwareOutput);
1360f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    setStrategyMute(STRATEGY_MEDIA, false, mHardwareOutput, MUTE_TIME_MS);
1361f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1362f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (!a2dpUsedForSonification()) {
1363f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // unmute music on A2DP output if a notification or ringtone is playing
1364f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        uint32_t refCount = mOutputs.valueFor(mHardwareOutput)->strategyRefCount(STRATEGY_SONIFICATION);
1365f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        for (uint32_t i = 0; i < refCount; i++) {
1366f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            setStrategyMute(STRATEGY_MEDIA, false, mA2dpOutput);
1367f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1368f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1369f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mA2dpDeviceAddress = "";
1370f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mA2dpSuspended = false;
1371f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
1372f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1373f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1374f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::closeA2dpOutputs()
1375f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1376f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1377f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    LOGV("setDeviceConnectionState() closing A2DP and duplicated output!");
1378f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1379f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mDuplicatedOutput != 0) {
1380f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioOutputDescriptor *dupOutputDesc = mOutputs.valueFor(mDuplicatedOutput);
1381f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioOutputDescriptor *hwOutputDesc = mOutputs.valueFor(mHardwareOutput);
1382f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // As all active tracks on duplicated output will be deleted,
1383f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // and as they were also referenced on hardware output, the reference
1384f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // count for their stream type must be adjusted accordingly on
1385f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // hardware output.
1386f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        for (int i = 0; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) {
1387f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            int refCount = dupOutputDesc->mRefCount[i];
1388f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            hwOutputDesc->changeRefCount((AudioSystem::stream_type)i,-refCount);
1389f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1390f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1391f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mpClientInterface->closeOutput(mDuplicatedOutput);
1392f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        delete mOutputs.valueFor(mDuplicatedOutput);
1393f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mOutputs.removeItem(mDuplicatedOutput);
1394f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mDuplicatedOutput = 0;
1395f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1396f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mA2dpOutput != 0) {
1397f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioParameter param;
1398f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        param.add(String8("closing"), String8("true"));
1399f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mpClientInterface->setParameters(mA2dpOutput, param.toString());
1400f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1401f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mpClientInterface->closeOutput(mA2dpOutput);
1402f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        delete mOutputs.valueFor(mA2dpOutput);
1403f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mOutputs.removeItem(mA2dpOutput);
1404f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mA2dpOutput = 0;
1405f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1406f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1407f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1408f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::checkOutputForStrategy(routing_strategy strategy)
1409f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1410f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    uint32_t prevDevice = getDeviceForStrategy(strategy);
1411f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    uint32_t curDevice = getDeviceForStrategy(strategy, false);
1412f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    bool a2dpWasUsed = AudioSystem::isA2dpDevice((AudioSystem::audio_devices)(prevDevice & ~AudioSystem::DEVICE_OUT_SPEAKER));
1413f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    bool a2dpIsUsed = AudioSystem::isA2dpDevice((AudioSystem::audio_devices)(curDevice & ~AudioSystem::DEVICE_OUT_SPEAKER));
1414f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    audio_io_handle_t srcOutput = 0;
1415f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    audio_io_handle_t dstOutput = 0;
1416f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1417f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (a2dpWasUsed && !a2dpIsUsed) {
1418f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        bool dupUsed = a2dpUsedForSonification() && a2dpWasUsed && (AudioSystem::popCount(prevDevice) == 2);
1419f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        dstOutput = mHardwareOutput;
1420f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (dupUsed) {
1421f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            LOGV("checkOutputForStrategy() moving strategy %d from duplicated", strategy);
1422f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            srcOutput = mDuplicatedOutput;
1423f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        } else {
1424f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            LOGV("checkOutputForStrategy() moving strategy %d from a2dp", strategy);
1425f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            srcOutput = mA2dpOutput;
1426f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1427f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1428f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (a2dpIsUsed && !a2dpWasUsed) {
1429f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        bool dupUsed = a2dpUsedForSonification() && a2dpIsUsed && (AudioSystem::popCount(curDevice) == 2);
1430f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        srcOutput = mHardwareOutput;
1431f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (dupUsed) {
1432f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            LOGV("checkOutputForStrategy() moving strategy %d to duplicated", strategy);
1433f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            dstOutput = mDuplicatedOutput;
1434f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        } else {
1435f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            LOGV("checkOutputForStrategy() moving strategy %d to a2dp", strategy);
1436f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            dstOutput = mA2dpOutput;
1437f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1438f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1439f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1440f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (srcOutput != 0 && dstOutput != 0) {
1441f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // Move effects associated to this strategy from previous output to new output
1442f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        for (size_t i = 0; i < mEffects.size(); i++) {
1443f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            EffectDescriptor *desc = mEffects.valueAt(i);
1444f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (desc->mSession != AudioSystem::SESSION_OUTPUT_STAGE &&
1445f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    desc->mStrategy == strategy &&
1446f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    desc->mOutput == srcOutput) {
1447f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                LOGV("checkOutputForStrategy() moving effect %d to output %d", mEffects.keyAt(i), dstOutput);
1448f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mpClientInterface->moveEffects(desc->mSession, srcOutput, dstOutput);
1449f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                desc->mOutput = dstOutput;
1450f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1451f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1452f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // Move tracks associated to this strategy from previous output to new output
1453f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        for (int i = 0; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) {
1454f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (getStrategy((AudioSystem::stream_type)i) == strategy) {
1455f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mpClientInterface->setStreamOutput((AudioSystem::stream_type)i, dstOutput);
1456f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1457f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1458f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1459f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1460f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1461f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::checkOutputForAllStrategies()
1462f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1463f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForStrategy(STRATEGY_PHONE);
1464f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForStrategy(STRATEGY_SONIFICATION);
1465f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForStrategy(STRATEGY_MEDIA);
1466f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForStrategy(STRATEGY_DTMF);
1467f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1468f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1469f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::checkA2dpSuspend()
1470f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1471f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // suspend A2DP output if:
1472f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      (NOT already suspended) &&
1473f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      ((SCO device is connected &&
1474f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //       (forced usage for communication || for record is SCO))) ||
1475f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      (phone state is ringing || in call)
1476f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //
1477f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // restore A2DP output if:
1478f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      (Already suspended) &&
1479f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      ((SCO device is NOT connected ||
1480f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //       (forced usage NOT for communication && NOT for record is SCO))) &&
1481f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      (phone state is NOT ringing && NOT in call)
1482f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //
1483f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mA2dpOutput == 0) {
1484f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
1485f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1486f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1487f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mA2dpSuspended) {
1488f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (((mScoDeviceAddress == "") ||
1489f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin             ((mForceUse[AudioSystem::FOR_COMMUNICATION] != AudioSystem::FORCE_BT_SCO) &&
1490f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin              (mForceUse[AudioSystem::FOR_RECORD] != AudioSystem::FORCE_BT_SCO))) &&
1491f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin             ((mPhoneState != AudioSystem::MODE_IN_CALL) &&
1492f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin              (mPhoneState != AudioSystem::MODE_RINGTONE))) {
1493f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1494f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mpClientInterface->restoreOutput(mA2dpOutput);
1495f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mA2dpSuspended = false;
1496f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1497f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
1498f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (((mScoDeviceAddress != "") &&
1499f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin             ((mForceUse[AudioSystem::FOR_COMMUNICATION] == AudioSystem::FORCE_BT_SCO) ||
1500f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin              (mForceUse[AudioSystem::FOR_RECORD] == AudioSystem::FORCE_BT_SCO))) ||
1501f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin             ((mPhoneState == AudioSystem::MODE_IN_CALL) ||
1502f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin              (mPhoneState == AudioSystem::MODE_RINGTONE))) {
1503f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1504f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mpClientInterface->suspendOutput(mA2dpOutput);
1505f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mA2dpSuspended = true;
1506f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1507f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1508f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1509f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1510f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1511f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif
1512f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1513f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinuint32_t AudioPolicyManagerBase::getNewDevice(audio_io_handle_t output, bool fromCache)
1514f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1515f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    uint32_t device = 0;
1516f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1517f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
1518f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // check the following by order of priority to request a routing change if necessary:
1519f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // 1: we are in call or the strategy phone is active on the hardware output:
1520f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      use device for strategy phone
1521f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // 2: the strategy sonification is active on the hardware output:
1522f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      use device for strategy sonification
1523f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // 3: the strategy media is active on the hardware output:
1524f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      use device for strategy media
1525f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // 4: the strategy DTMF is active on the hardware output:
1526f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      use device for strategy DTMF
1527f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (isInCall() ||
1528f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->isUsedByStrategy(STRATEGY_PHONE)) {
1529f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
1530f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else if (outputDesc->isUsedByStrategy(STRATEGY_SONIFICATION)) {
1531f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
1532f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else if (outputDesc->isUsedByStrategy(STRATEGY_MEDIA)) {
1533f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
1534f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else if (outputDesc->isUsedByStrategy(STRATEGY_DTMF)) {
1535f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
1536f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1537f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1538f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    LOGV("getNewDevice() selected device %x", device);
1539f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return device;
1540f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1541f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1542f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinuint32_t AudioPolicyManagerBase::getStrategyForStream(AudioSystem::stream_type stream) {
1543f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return (uint32_t)getStrategy(stream);
1544f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1545f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1546f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinuint32_t AudioPolicyManagerBase::getDevicesForStream(AudioSystem::stream_type stream) {
1547f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    uint32_t devices;
1548f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // By checking the range of stream before calling getStrategy, we avoid
1549f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // getStrategy's behavior for invalid streams.  getStrategy would do a LOGE
1550f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // and then return STRATEGY_MEDIA, but we want to return the empty set.
1551f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (stream < (AudioSystem::stream_type) 0 || stream >= AudioSystem::NUM_STREAM_TYPES) {
1552f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        devices = 0;
1553f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
1554f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioPolicyManagerBase::routing_strategy strategy = getStrategy(stream);
1555f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        devices = getDeviceForStrategy(strategy, true);
1556f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1557f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return devices;
1558f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1559f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1560f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima ZavinAudioPolicyManagerBase::routing_strategy AudioPolicyManagerBase::getStrategy(
1561f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioSystem::stream_type stream) {
1562f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // stream to strategy mapping
1563f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    switch (stream) {
1564f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::VOICE_CALL:
1565f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::BLUETOOTH_SCO:
1566f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return STRATEGY_PHONE;
1567f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::RING:
1568f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::NOTIFICATION:
1569f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::ALARM:
1570f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::ENFORCED_AUDIBLE:
1571f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return STRATEGY_SONIFICATION;
1572f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::DTMF:
1573f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return STRATEGY_DTMF;
1574f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    default:
1575f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        LOGE("unknown stream type");
1576f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::SYSTEM:
1577f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // NOTE: SYSTEM stream uses MEDIA strategy because muting music and switching outputs
1578f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // while key clicks are played produces a poor result
1579f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::TTS:
1580f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::MUSIC:
1581f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return STRATEGY_MEDIA;
1582f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1583f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1584f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1585f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinuint32_t AudioPolicyManagerBase::getDeviceForStrategy(routing_strategy strategy, bool fromCache)
1586f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1587f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    uint32_t device = 0;
1588f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1589f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (fromCache) {
1590f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        LOGV("getDeviceForStrategy() from cache strategy %d, device %x", strategy, mDeviceForStrategy[strategy]);
1591f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return mDeviceForStrategy[strategy];
1592f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1593f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1594f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    switch (strategy) {
1595f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case STRATEGY_DTMF:
1596f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (!isInCall()) {
1597f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // when off call, DTMF strategy follows the same rules as MEDIA strategy
1598f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            device = getDeviceForStrategy(STRATEGY_MEDIA, false);
1599f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            break;
1600f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1601f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // when in call, DTMF and PHONE strategies follow the same rules
1602f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // FALL THROUGH
1603f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1604f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case STRATEGY_PHONE:
1605f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // for phone strategy, we first consider the forced use and then the available devices by order
1606f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // of priority
1607f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        switch (mForceUse[AudioSystem::FOR_COMMUNICATION]) {
1608f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        case AudioSystem::FORCE_BT_SCO:
1609f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (!isInCall() || strategy != STRATEGY_DTMF) {
1610f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_CARKIT;
1611f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (device) break;
1612f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1613f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_SCO_HEADSET;
1614f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (device) break;
1615f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_SCO;
1616f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (device) break;
1617f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // if SCO device is requested but no SCO device is available, fall back to default case
1618f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // FALL THROUGH
1619f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1620f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        default:    // FORCE_NONE
1621f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_WIRED_HEADPHONE;
1622f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (device) break;
1623f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_WIRED_HEADSET;
1624f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (device) break;
1625f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef WITH_A2DP
1626f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to A2DP
1627f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (!isInCall() && !mA2dpSuspended) {
1628f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP;
1629f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (device) break;
1630f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
1631f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (device) break;
1632f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1633f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif
1634f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_AUX_DIGITAL;
1635f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (device) break;
1636f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_DGTL_DOCK_HEADSET;
1637f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (device) break;
1638f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_ANLG_DOCK_HEADSET;
1639f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (device) break;
1640f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_EARPIECE;
1641f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (device == 0) {
1642f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                LOGE("getDeviceForStrategy() earpiece device not found");
1643f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1644f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            break;
1645f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1646f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        case AudioSystem::FORCE_SPEAKER:
1647f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef WITH_A2DP
1648f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to
1649f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // A2DP speaker when forcing to speaker output
1650f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (!isInCall() && !mA2dpSuspended) {
1651f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
1652f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (device) break;
1653f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1654f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif
1655f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_AUX_DIGITAL;
1656f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (device) break;
1657f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_DGTL_DOCK_HEADSET;
1658f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (device) break;
1659f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_ANLG_DOCK_HEADSET;
1660f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (device) break;
1661f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_SPEAKER;
1662f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (device == 0) {
1663f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                LOGE("getDeviceForStrategy() speaker device not found");
1664f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1665f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            break;
1666f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1667f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    break;
1668f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1669f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case STRATEGY_SONIFICATION:
1670f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1671f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // If incall, just select the STRATEGY_PHONE device: The rest of the behavior is handled by
1672f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // handleIncallSonification().
1673f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (isInCall()) {
1674f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            device = getDeviceForStrategy(STRATEGY_PHONE, false);
1675f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            break;
1676f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1677f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_SPEAKER;
1678f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (device == 0) {
1679f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            LOGE("getDeviceForStrategy() speaker device not found");
1680f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1681f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // The second device used for sonification is the same as the device used by media strategy
1682f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // FALL THROUGH
1683f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1684f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case STRATEGY_MEDIA: {
1685f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        uint32_t device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_WIRED_HEADPHONE;
1686f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (device2 == 0) {
1687f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_WIRED_HEADSET;
1688f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1689f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef WITH_A2DP
1690f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if ((mA2dpOutput != 0) && !mA2dpSuspended &&
1691f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                (strategy != STRATEGY_SONIFICATION || a2dpUsedForSonification())) {
1692f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (device2 == 0) {
1693f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP;
1694f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1695f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (device2 == 0) {
1696f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
1697f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1698f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (device2 == 0) {
1699f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
1700f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1701f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1702f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif
1703f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (device2 == 0) {
1704f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_AUX_DIGITAL;
1705f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1706f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (device2 == 0) {
1707f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_DGTL_DOCK_HEADSET;
1708f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1709f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (device2 == 0) {
1710f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_ANLG_DOCK_HEADSET;
1711f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1712f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (device2 == 0) {
1713f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            device2 = mAvailableOutputDevices & AudioSystem::DEVICE_OUT_SPEAKER;
1714f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1715f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1716f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION, 0 otherwise
1717f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device |= device2;
1718f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (device == 0) {
1719f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            LOGE("getDeviceForStrategy() speaker device not found");
1720f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1721f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        } break;
1722f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1723f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    default:
1724f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        LOGW("getDeviceForStrategy() unknown strategy: %d", strategy);
1725f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
1726f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1727f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1728f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    LOGV("getDeviceForStrategy() strategy %d, device %x", strategy, device);
1729f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return device;
1730f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1731f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1732f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::updateDeviceForStrategy()
1733f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1734f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < NUM_STRATEGIES; i++) {
1735f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false);
1736f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1737f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1738f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1739f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::setOutputDevice(audio_io_handle_t output, uint32_t device, bool force, int delayMs)
1740f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1741f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    LOGV("setOutputDevice() output %d device %x delayMs %d", output, device, delayMs);
1742f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
1743f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1744f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1745f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (outputDesc->isDuplicated()) {
1746f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        setOutputDevice(outputDesc->mOutput1->mId, device, force, delayMs);
1747f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        setOutputDevice(outputDesc->mOutput2->mId, device, force, delayMs);
1748f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
1749f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1750f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef WITH_A2DP
1751f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // filter devices according to output selected
1752f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (output == mA2dpOutput) {
1753f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device &= AudioSystem::DEVICE_OUT_ALL_A2DP;
1754f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
1755f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device &= ~AudioSystem::DEVICE_OUT_ALL_A2DP;
1756f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1757f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif
1758f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1759f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    uint32_t prevDevice = (uint32_t)outputDesc->device();
1760f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // Do not change the routing if:
1761f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //  - the requestede device is 0
1762f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //  - the requested device is the same as current device and force is not specified.
1763f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // Doing this check here allows the caller to call setOutputDevice() without conditions
1764f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if ((device == 0 || device == prevDevice) && !force) {
1765f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        LOGV("setOutputDevice() setting same device %x or null device for output %d", device, output);
1766f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
1767f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1768f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1769f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    outputDesc->mDevice = device;
1770f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // mute media streams if both speaker and headset are selected
1771f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (output == mHardwareOutput && AudioSystem::popCount(device) == 2) {
1772f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        setStrategyMute(STRATEGY_MEDIA, true, output);
1773f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // wait for the PCM output buffers to empty before proceeding with the rest of the command
1774f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        usleep(outputDesc->mLatency*2*1000);
1775f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1776f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1777f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // do the routing
1778f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioParameter param = AudioParameter();
1779f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    param.addInt(String8(AudioParameter::keyRouting), (int)device);
1780f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mpClientInterface->setParameters(mHardwareOutput, param.toString(), delayMs);
1781f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // update stream volumes according to new device
1782f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    applyStreamVolumes(output, device, delayMs);
1783f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1784f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if changing from a combined headset + speaker route, unmute media streams
1785f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (output == mHardwareOutput && AudioSystem::popCount(prevDevice) == 2) {
1786f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        setStrategyMute(STRATEGY_MEDIA, false, output, delayMs);
1787f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1788f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1789f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1790f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinuint32_t AudioPolicyManagerBase::getDeviceForInputSource(int inputSource)
1791f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1792f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    uint32_t device;
1793f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1794f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    switch(inputSource) {
1795f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_DEFAULT:
1796f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_MIC:
1797f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_RECOGNITION:
1798f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_COMMUNICATION:
1799f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (mForceUse[AudioSystem::FOR_RECORD] == AudioSystem::FORCE_BT_SCO &&
1800f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mAvailableInputDevices & AudioSystem::DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
1801f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            device = AudioSystem::DEVICE_IN_BLUETOOTH_SCO_HEADSET;
1802f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        } else if (mAvailableInputDevices & AudioSystem::DEVICE_IN_WIRED_HEADSET) {
1803f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            device = AudioSystem::DEVICE_IN_WIRED_HEADSET;
1804f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        } else {
1805f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            device = AudioSystem::DEVICE_IN_BUILTIN_MIC;
1806f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1807f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
1808f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_CAMCORDER:
1809f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (hasBackMicrophone()) {
1810f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            device = AudioSystem::DEVICE_IN_BACK_MIC;
1811f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        } else {
1812f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            device = AudioSystem::DEVICE_IN_BUILTIN_MIC;
1813f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1814f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
1815f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_UPLINK:
1816f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_DOWNLINK:
1817f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_CALL:
1818f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device = AudioSystem::DEVICE_IN_VOICE_CALL;
1819f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
1820f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    default:
1821f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        LOGW("getInput() invalid input source %d", inputSource);
1822f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device = 0;
1823f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
1824f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1825f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    LOGV("getDeviceForInputSource()input source %d, device %08x", inputSource, device);
1826f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return device;
1827f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1828f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1829f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinaudio_io_handle_t AudioPolicyManagerBase::getActiveInput()
1830f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1831f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mInputs.size(); i++) {
1832f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (mInputs.valueAt(i)->mRefCount > 0) {
1833f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return mInputs.keyAt(i);
1834f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1835f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1836f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return 0;
1837f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1838f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1839f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinfloat AudioPolicyManagerBase::volIndexToAmpl(uint32_t device, const StreamDescriptor& streamDesc,
1840f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        int indexInUi) {
1841f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // the volume index in the UI is relative to the min and max volume indices for this stream type
1842f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    int nbSteps = 1 + streamDesc.mVolIndex[StreamDescriptor::VOLMAX] -
1843f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            streamDesc.mVolIndex[StreamDescriptor::VOLMIN];
1844f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    int volIdx = (nbSteps * (indexInUi - streamDesc.mIndexMin)) /
1845f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            (streamDesc.mIndexMax - streamDesc.mIndexMin);
1846f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1847f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // find what part of the curve this index volume belongs to, or if it's out of bounds
1848f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    int segment = 0;
1849f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (volIdx < streamDesc.mVolIndex[StreamDescriptor::VOLMIN]) {         // out of bounds
1850f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return 0.0f;
1851f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else if (volIdx < streamDesc.mVolIndex[StreamDescriptor::VOLKNEE1]) {
1852f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        segment = 0;
1853f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else if (volIdx < streamDesc.mVolIndex[StreamDescriptor::VOLKNEE2]) {
1854f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        segment = 1;
1855f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else if (volIdx <= streamDesc.mVolIndex[StreamDescriptor::VOLMAX]) {
1856f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        segment = 2;
1857f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {                                                               // out of bounds
1858f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return 1.0f;
1859f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1860f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1861f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // linear interpolation in the attenuation table in dB
1862f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    float decibels = streamDesc.mVolDbAtt[segment] +
1863f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            ((float)(volIdx - streamDesc.mVolIndex[segment])) *
1864f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                ( (streamDesc.mVolDbAtt[segment+1] - streamDesc.mVolDbAtt[segment]) /
1865f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    ((float)(streamDesc.mVolIndex[segment+1] - streamDesc.mVolIndex[segment])) );
1866f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1867f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    float amplification = exp( decibels * 0.115129f); // exp( dB * ln(10) / 20 )
1868f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1869f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    LOGV("VOLUME vol index=[%d %d %d], dB=[%.1f %.1f %.1f] ampl=%.5f",
1870f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            streamDesc.mVolIndex[segment], volIdx, streamDesc.mVolIndex[segment+1],
1871f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            streamDesc.mVolDbAtt[segment], decibels, streamDesc.mVolDbAtt[segment+1],
1872f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            amplification);
1873f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1874f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return amplification;
1875f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1876f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1877f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::initializeVolumeCurves() {
1878f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // initialize the volume curves to a (-49.5 - 0 dB) attenuation in 0.5dB steps
1879f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i=0 ; i< AudioSystem::NUM_STREAM_TYPES ; i++) {
1880f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mStreams[i].mVolIndex[StreamDescriptor::VOLMIN] = 1;
1881f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mStreams[i].mVolDbAtt[StreamDescriptor::VOLMIN] = -49.5f;
1882f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mStreams[i].mVolIndex[StreamDescriptor::VOLKNEE1] = 33;
1883f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mStreams[i].mVolDbAtt[StreamDescriptor::VOLKNEE1] = -33.5f;
1884f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mStreams[i].mVolIndex[StreamDescriptor::VOLKNEE2] = 66;
1885f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mStreams[i].mVolDbAtt[StreamDescriptor::VOLKNEE2] = -17.0f;
1886f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // here we use 100 steps to avoid rounding errors
1887f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // when computing the volume in volIndexToAmpl()
1888f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mStreams[i].mVolIndex[StreamDescriptor::VOLMAX] = 100;
1889f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mStreams[i].mVolDbAtt[StreamDescriptor::VOLMAX] = 0.0f;
1890f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1891f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1892f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // Modification for music: more attenuation for lower volumes, finer steps at high volumes
1893f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mStreams[AudioSystem::MUSIC].mVolIndex[StreamDescriptor::VOLMIN] = 1;
1894f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mStreams[AudioSystem::MUSIC].mVolDbAtt[StreamDescriptor::VOLMIN] = -58.0f;
1895f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mStreams[AudioSystem::MUSIC].mVolIndex[StreamDescriptor::VOLKNEE1] = 20;
1896f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mStreams[AudioSystem::MUSIC].mVolDbAtt[StreamDescriptor::VOLKNEE1] = -40.0f;
1897f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mStreams[AudioSystem::MUSIC].mVolIndex[StreamDescriptor::VOLKNEE2] = 60;
1898f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mStreams[AudioSystem::MUSIC].mVolDbAtt[StreamDescriptor::VOLKNEE2] = -17.0f;
1899f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mStreams[AudioSystem::MUSIC].mVolIndex[StreamDescriptor::VOLMAX] = 100;
1900f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mStreams[AudioSystem::MUSIC].mVolDbAtt[StreamDescriptor::VOLMAX] = 0.0f;
1901f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1902f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1903f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinfloat AudioPolicyManagerBase::computeVolume(int stream, int index, audio_io_handle_t output, uint32_t device)
1904f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1905f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    float volume = 1.0;
1906f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
1907f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    StreamDescriptor &streamDesc = mStreams[stream];
1908f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1909f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (device == 0) {
1910f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device = outputDesc->device();
1911f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1912f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1913f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if volume is not 0 (not muted), force media volume to max on digital output
1914f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (stream == AudioSystem::MUSIC &&
1915f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        index != mStreams[stream].mIndexMin &&
1916f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device == AudioSystem::DEVICE_OUT_AUX_DIGITAL) {
1917f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return 1.0;
1918f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1919f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1920f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    volume = volIndexToAmpl(device, streamDesc, index);
1921f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1922f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if a headset is connected, apply the following rules to ring tones and notifications
1923f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // to avoid sound level bursts in user's ears:
1924f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // - always attenuate ring tones and notifications volume by 6dB
1925f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // - if music is playing, always limit the volume to current music volume,
1926f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // with a minimum threshold at -36dB so that notification is always perceived.
1927f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if ((device &
1928f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        (AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP |
1929f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioSystem::DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
1930f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioSystem::DEVICE_OUT_WIRED_HEADSET |
1931f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioSystem::DEVICE_OUT_WIRED_HEADPHONE)) &&
1932f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        ((getStrategy((AudioSystem::stream_type)stream) == STRATEGY_SONIFICATION) ||
1933f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin         (stream == AudioSystem::SYSTEM)) &&
1934f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        streamDesc.mCanBeMuted) {
1935f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        volume *= SONIFICATION_HEADSET_VOLUME_FACTOR;
1936f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // when the phone is ringing we must consider that music could have been paused just before
1937f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // by the music application and behave as if music was active if the last music track was
1938f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // just stopped
1939f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (outputDesc->mRefCount[AudioSystem::MUSIC] || mLimitRingtoneVolume) {
1940f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            float musicVol = computeVolume(AudioSystem::MUSIC, mStreams[AudioSystem::MUSIC].mIndexCur, output, device);
1941f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            float minVol = (musicVol > SONIFICATION_HEADSET_VOLUME_MIN) ? musicVol : SONIFICATION_HEADSET_VOLUME_MIN;
1942f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (volume > minVol) {
1943f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                volume = minVol;
1944f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                LOGV("computeVolume limiting volume to %f musicVol %f", minVol, musicVol);
1945f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1946f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1947f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1948f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1949f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return volume;
1950f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1951f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1952f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::checkAndSetVolume(int stream, int index, audio_io_handle_t output, uint32_t device, int delayMs, bool force)
1953f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1954f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1955f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // do not change actual stream volume if the stream is muted
1956f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mOutputs.valueFor(output)->mMuteCount[stream] != 0) {
1957f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        LOGV("checkAndSetVolume() stream %d muted count %d", stream, mOutputs.valueFor(output)->mMuteCount[stream]);
1958f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return NO_ERROR;
1959f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1960f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1961f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // do not change in call volume if bluetooth is connected and vice versa
1962f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if ((stream == AudioSystem::VOICE_CALL && mForceUse[AudioSystem::FOR_COMMUNICATION] == AudioSystem::FORCE_BT_SCO) ||
1963f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        (stream == AudioSystem::BLUETOOTH_SCO && mForceUse[AudioSystem::FOR_COMMUNICATION] != AudioSystem::FORCE_BT_SCO)) {
1964f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        LOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
1965f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin             stream, mForceUse[AudioSystem::FOR_COMMUNICATION]);
1966f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return INVALID_OPERATION;
1967f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1968f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1969f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    float volume = computeVolume(stream, index, output, device);
1970f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // We actually change the volume if:
1971f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // - the float value returned by computeVolume() changed
1972f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // - the force flag is set
1973f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (volume != mOutputs.valueFor(output)->mCurVolume[stream] ||
1974f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            force) {
1975f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mOutputs.valueFor(output)->mCurVolume[stream] = volume;
1976f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        LOGV("setStreamVolume() for output %d stream %d, volume %f, delay %d", output, stream, volume, delayMs);
1977f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (stream == AudioSystem::VOICE_CALL ||
1978f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            stream == AudioSystem::DTMF ||
1979f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            stream == AudioSystem::BLUETOOTH_SCO) {
1980f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // offset value to reflect actual hardware volume that never reaches 0
1981f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // 1% corresponds roughly to first step in VOICE_CALL stream volume setting (see AudioService.java)
1982f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            volume = 0.01 + 0.99 * volume;
1983f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // Force VOICE_CALL to track BLUETOOTH_SCO stream volume when bluetooth audio is
1984f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // enabled
1985f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (stream == AudioSystem::BLUETOOTH_SCO) {
1986f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mpClientInterface->setStreamVolume(AudioSystem::VOICE_CALL, volume, output, delayMs);
1987f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1988f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1989f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1990f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mpClientInterface->setStreamVolume((AudioSystem::stream_type)stream, volume, output, delayMs);
1991f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1992f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1993f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (stream == AudioSystem::VOICE_CALL ||
1994f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        stream == AudioSystem::BLUETOOTH_SCO) {
1995f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        float voiceVolume;
1996f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // Force voice volume to max for bluetooth SCO as volume is managed by the headset
1997f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (stream == AudioSystem::VOICE_CALL) {
1998f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            voiceVolume = (float)index/(float)mStreams[stream].mIndexMax;
1999f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        } else {
2000f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            voiceVolume = 1.0;
2001f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2002f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2003f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (voiceVolume != mLastVoiceVolume && output == mHardwareOutput) {
2004f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
2005f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mLastVoiceVolume = voiceVolume;
2006f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2007f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2008f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2009f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
2010f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2011f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2012f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::applyStreamVolumes(audio_io_handle_t output, uint32_t device, int delayMs, bool force)
2013f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2014f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    LOGV("applyStreamVolumes() for output %d and device %x", output, device);
2015f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2016f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
2017f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        checkAndSetVolume(stream, mStreams[stream].mIndexCur, output, device, delayMs, force);
2018f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2019f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2020f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2021f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::setStrategyMute(routing_strategy strategy, bool on, audio_io_handle_t output, int delayMs)
2022f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2023f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    LOGV("setStrategyMute() strategy %d, mute %d, output %d", strategy, on, output);
2024f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
2025f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (getStrategy((AudioSystem::stream_type)stream) == strategy) {
2026f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            setStreamMute(stream, on, output, delayMs);
2027f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2028f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2029f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2030f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2031f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::setStreamMute(int stream, bool on, audio_io_handle_t output, int delayMs)
2032f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2033f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    StreamDescriptor &streamDesc = mStreams[stream];
2034f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
2035f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2036f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    LOGV("setStreamMute() stream %d, mute %d, output %d, mMuteCount %d", stream, on, output, outputDesc->mMuteCount[stream]);
2037f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2038f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (on) {
2039f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (outputDesc->mMuteCount[stream] == 0) {
2040f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (streamDesc.mCanBeMuted) {
2041f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                checkAndSetVolume(stream, 0, output, outputDesc->device(), delayMs);
2042f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2043f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2044f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
2045f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mMuteCount[stream]++;
2046f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
2047f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (outputDesc->mMuteCount[stream] == 0) {
2048f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            LOGW("setStreamMute() unmuting non muted stream!");
2049f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return;
2050f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2051f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (--outputDesc->mMuteCount[stream] == 0) {
2052f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            checkAndSetVolume(stream, streamDesc.mIndexCur, output, outputDesc->device(), delayMs);
2053f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2054f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2055f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2056f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2057f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::handleIncallSonification(int stream, bool starting, bool stateChange)
2058f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2059f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if the stream pertains to sonification strategy and we are in call we must
2060f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // mute the stream if it is low visibility. If it is high visibility, we must play a tone
2061f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // in the device used for phone strategy and play the tone if the selected device does not
2062f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // interfere with the device used for phone strategy
2063f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
2064f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // many times as there are active tracks on the output
2065f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2066f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (getStrategy((AudioSystem::stream_type)stream) == STRATEGY_SONIFICATION) {
2067f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioOutputDescriptor *outputDesc = mOutputs.valueFor(mHardwareOutput);
2068f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        LOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
2069f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                stream, starting, outputDesc->mDevice, stateChange);
2070f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (outputDesc->mRefCount[stream]) {
2071f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            int muteCount = 1;
2072f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (stateChange) {
2073f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                muteCount = outputDesc->mRefCount[stream];
2074f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2075f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (AudioSystem::isLowVisibility((AudioSystem::stream_type)stream)) {
2076f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                LOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
2077f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                for (int i = 0; i < muteCount; i++) {
2078f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    setStreamMute(stream, starting, mHardwareOutput);
2079f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
2080f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            } else {
2081f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                LOGV("handleIncallSonification() high visibility");
2082f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (outputDesc->device() & getDeviceForStrategy(STRATEGY_PHONE)) {
2083f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    LOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
2084f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    for (int i = 0; i < muteCount; i++) {
2085f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        setStreamMute(stream, starting, mHardwareOutput);
2086f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    }
2087f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
2088f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (starting) {
2089f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    mpClientInterface->startTone(ToneGenerator::TONE_SUP_CALL_WAITING, AudioSystem::VOICE_CALL);
2090f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else {
2091f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    mpClientInterface->stopTone();
2092f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
2093f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2094f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2095f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2096f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2097f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2098f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinbool AudioPolicyManagerBase::isInCall()
2099f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2100f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return isStateInCall(mPhoneState);
2101f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2102f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2103f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinbool AudioPolicyManagerBase::isStateInCall(int state) {
2104f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return ((state == AudioSystem::MODE_IN_CALL) ||
2105f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            (state == AudioSystem::MODE_IN_COMMUNICATION));
2106f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2107f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2108f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinbool AudioPolicyManagerBase::needsDirectOuput(AudioSystem::stream_type stream,
2109f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    uint32_t samplingRate,
2110f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    uint32_t format,
2111f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    uint32_t channels,
2112f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    AudioSystem::output_flags flags,
2113f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    uint32_t device)
2114f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2115f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin   return ((flags & AudioSystem::OUTPUT_FLAG_DIRECT) ||
2116e81531e91ecae92aff471dbff9cbeb0f95ff4a80Dima Zavin          (format !=0 && !AudioSystem::isLinearPCM(format)));
2117f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2118f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2119f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinuint32_t AudioPolicyManagerBase::getMaxEffectsCpuLoad()
2120f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2121f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return MAX_EFFECTS_CPU_LOAD;
2122f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2123f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2124f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinuint32_t AudioPolicyManagerBase::getMaxEffectsMemory()
2125f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2126f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return MAX_EFFECTS_MEMORY;
2127f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2128f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2129f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// --- AudioOutputDescriptor class implementation
2130f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2131f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima ZavinAudioPolicyManagerBase::AudioOutputDescriptor::AudioOutputDescriptor()
2132f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    : mId(0), mSamplingRate(0), mFormat(0), mChannels(0), mLatency(0),
2133f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mFlags((AudioSystem::output_flags)0), mDevice(0), mOutput1(0), mOutput2(0)
2134f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2135f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // clear usage count for all stream types
2136f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) {
2137f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mRefCount[i] = 0;
2138f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mCurVolume[i] = -1.0;
2139f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mMuteCount[i] = 0;
2140f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mStopTime[i] = 0;
2141f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2142f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2143f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2144f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinuint32_t AudioPolicyManagerBase::AudioOutputDescriptor::device()
2145f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2146f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    uint32_t device = 0;
2147f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (isDuplicated()) {
2148f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device = mOutput1->mDevice | mOutput2->mDevice;
2149f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
2150f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device = mDevice;
2151f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2152f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return device;
2153f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2154f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2155f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::AudioOutputDescriptor::changeRefCount(AudioSystem::stream_type stream, int delta)
2156f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2157f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // forward usage count change to attached outputs
2158f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (isDuplicated()) {
2159f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mOutput1->changeRefCount(stream, delta);
2160f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mOutput2->changeRefCount(stream, delta);
2161f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2162f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if ((delta + (int)mRefCount[stream]) < 0) {
2163f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        LOGW("changeRefCount() invalid delta %d for stream %d, refCount %d", delta, stream, mRefCount[stream]);
2164f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mRefCount[stream] = 0;
2165f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
2166f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2167f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mRefCount[stream] += delta;
2168e81531e91ecae92aff471dbff9cbeb0f95ff4a80Dima Zavin    LOGV("changeRefCount() stream %d, count %d", stream, mRefCount[stream]);
2169f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2170f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2171f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinuint32_t AudioPolicyManagerBase::AudioOutputDescriptor::refCount()
2172f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2173f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    uint32_t refcount = 0;
2174f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) {
2175f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        refcount += mRefCount[i];
2176f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2177f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return refcount;
2178f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2179f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2180f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinuint32_t AudioPolicyManagerBase::AudioOutputDescriptor::strategyRefCount(routing_strategy strategy)
2181f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2182f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    uint32_t refCount = 0;
2183f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) {
2184f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (getStrategy((AudioSystem::stream_type)i) == strategy) {
2185f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            refCount += mRefCount[i];
2186f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2187f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2188f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return refCount;
2189f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2190f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2191f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::AudioOutputDescriptor::dump(int fd)
2192f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2193f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    const size_t SIZE = 256;
2194f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    char buffer[SIZE];
2195f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    String8 result;
2196f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2197f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Sampling rate: %d\n", mSamplingRate);
2198f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
2199f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Format: %d\n", mFormat);
2200f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
2201f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Channels: %08x\n", mChannels);
2202f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
2203f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Latency: %d\n", mLatency);
2204f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
2205f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Flags %08x\n", mFlags);
2206f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
2207f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Devices %08x\n", device());
2208f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
2209f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Stream volume refCount muteCount\n");
2210f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
2211f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) {
2212f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        snprintf(buffer, SIZE, " %02d     %.03f     %02d       %02d\n", i, mCurVolume[i], mRefCount[i], mMuteCount[i]);
2213f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        result.append(buffer);
2214f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2215f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, result.string(), result.size());
2216f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2217f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
2218f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2219f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2220f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// --- AudioInputDescriptor class implementation
2221f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2222f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima ZavinAudioPolicyManagerBase::AudioInputDescriptor::AudioInputDescriptor()
2223f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    : mSamplingRate(0), mFormat(0), mChannels(0),
2224e81531e91ecae92aff471dbff9cbeb0f95ff4a80Dima Zavin     mAcoustics((AudioSystem::audio_in_acoustics)0), mDevice(0), mRefCount(0)
2225f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2226f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2227f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2228f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::AudioInputDescriptor::dump(int fd)
2229f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2230f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    const size_t SIZE = 256;
2231f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    char buffer[SIZE];
2232f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    String8 result;
2233f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2234f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Sampling rate: %d\n", mSamplingRate);
2235f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
2236f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Format: %d\n", mFormat);
2237f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
2238f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Channels: %08x\n", mChannels);
2239f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
2240f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Acoustics %08x\n", mAcoustics);
2241f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
2242f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Devices %08x\n", mDevice);
2243f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
2244f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Ref Count %d\n", mRefCount);
2245f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
2246f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, result.string(), result.size());
2247f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2248f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
2249f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2250f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2251f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// --- StreamDescriptor class implementation
2252f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2253f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::StreamDescriptor::dump(char* buffer, size_t size)
2254f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2255f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, size, "      %02d         %02d         %02d         %d\n",
2256f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mIndexMin,
2257f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mIndexMax,
2258f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mIndexCur,
2259f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mCanBeMuted);
2260f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2261f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2262f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// --- EffectDescriptor class implementation
2263f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2264f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::EffectDescriptor::dump(int fd)
2265f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2266f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    const size_t SIZE = 256;
2267f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    char buffer[SIZE];
2268f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    String8 result;
2269f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2270f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Output: %d\n", mOutput);
2271f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
2272f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Strategy: %d\n", mStrategy);
2273f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
2274f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Session: %d\n", mSession);
2275f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
2276f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Name: %s\n",  mDesc.name);
2277f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
2278f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, result.string(), result.size());
2279f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2280f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
2281f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2282f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2283f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2284f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2285f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}; // namespace android
2286