AudioPolicyManagerBase.cpp revision 03d92f4a6ec954cfa81765f00c1784e2c2ff2f49
1f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin/*
2f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin * Copyright (C) 2009 The Android Open Source Project
3f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin *
4f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin * Licensed under the Apache License, Version 2.0 (the "License");
5f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin * you may not use this file except in compliance with the License.
6f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin * You may obtain a copy of the License at
7f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin *
8f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin *      http://www.apache.org/licenses/LICENSE-2.0
9f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin *
10f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin * Unless required by applicable law or agreed to in writing, software
11f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin * distributed under the License is distributed on an "AS IS" BASIS,
12f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin * See the License for the specific language governing permissions and
14f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin * limitations under the License.
15f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin */
16f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
17f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#define LOG_TAG "AudioPolicyManagerBase"
184e57a1d4979c2644dd8999dc5759b10ed0af56e9Eric Laurent//#define LOG_NDEBUG 0
193cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
203cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent//#define VERY_VERBOSE_LOGGING
213cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent#ifdef VERY_VERBOSE_LOGGING
223cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent#define ALOGVV ALOGV
233cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent#else
243cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent#define ALOGVV(a...) do { } while(0)
253cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent#endif
263cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
276d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi// A device mask for all audio input devices that are considered "virtual" when evaluating
286d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi// active inputs in getActiveInput()
296d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi#define APM_AUDIO_IN_DEVICE_VIRTUAL_ALL  AUDIO_DEVICE_IN_REMOTE_SUBMIX
30dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi// A device mask for all audio output devices that are considered "remote" when evaluating
31dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi// active output devices in isStreamActiveRemotely()
32dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi#define APM_AUDIO_OUT_DEVICE_REMOTE_ALL  AUDIO_DEVICE_OUT_REMOTE_SUBMIX
336d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi
34c7b6e3c857c5f5af5f244d0b877c41a9c55f92abColin Cross#include <inttypes.h>
35c7b6e3c857c5f5af5f244d0b877c41a9c55f92abColin Cross#include <math.h>
36c7b6e3c857c5f5af5f244d0b877c41a9c55f92abColin Cross
37c7b6e3c857c5f5af5f244d0b877c41a9c55f92abColin Cross#include <cutils/properties.h>
38f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#include <utils/Log.h>
39c7b6e3c857c5f5af5f244d0b877c41a9c55f92abColin Cross
4008b014d9e509c9163db6b33a63852e73db4d07ccEric Laurent#include <hardware/audio.h>
41c7b6e3c857c5f5af5f244d0b877c41a9c55f92abColin Cross#include <hardware/audio_effect.h>
425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent#include <hardware_legacy/audio_policy_conf.h>
43c7b6e3c857c5f5af5f244d0b877c41a9c55f92abColin Cross#include <hardware_legacy/AudioPolicyManagerBase.h>
44f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
45e81531e91ecae92aff471dbff9cbeb0f95ff4a80Dima Zavinnamespace android_audio_legacy {
46f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
47f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// ----------------------------------------------------------------------------
48f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// AudioPolicyInterface implementation
49f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// ----------------------------------------------------------------------------
50f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
51f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
52c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivistatus_t AudioPolicyManagerBase::setDeviceConnectionState(audio_devices_t device,
53f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                  AudioSystem::device_connection_state state,
54f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                  const char *device_address)
55f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
56c26f454f8d841657542916cdd140a9896a89ad20Paul McLean    ALOGV("setDeviceConnectionState() device: 0x%X, state %d, address %s", device, state, device_address);
57f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
58f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // connect/disconnect only 1 device at a time
59ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
60f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
61f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (strlen(device_address) >= MAX_DEVICE_ADDRESS_LEN) {
625efbd421e0029d6fc44b1cc65c0e5e0d85e5161fSteve Block        ALOGE("setDeviceConnectionState() invalid address: %s", device_address);
63f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
64f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
65f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
66f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // handle output devices
67c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    if (audio_is_output_device(device)) {
68fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        SortedVector <audio_io_handle_t> outputs;
69f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
7033bf1b0fe363bd4892349d160f54d860567fab12Mike Lockwood        if (!mHasA2dp && audio_is_a2dp_out_device(device)) {
7148387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            ALOGE("setDeviceConnectionState() invalid A2DP device: %x", device);
72f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return BAD_VALUE;
73f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
74fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        if (!mHasUsb && audio_is_usb_out_device(device)) {
7548387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            ALOGE("setDeviceConnectionState() invalid USB audio device: %x", device);
7648387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            return BAD_VALUE;
7748387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi        }
7848387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi        if (!mHasRemoteSubmix && audio_is_remote_submix_device((audio_devices_t)device)) {
7948387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            ALOGE("setDeviceConnectionState() invalid remote submix audio device: %x", device);
80599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            return BAD_VALUE;
81599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent        }
82f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
83c952527e6f89d5427881462823514be9d79f13e6Eric Laurent        // save a copy of the opened output descriptors before any output is opened or closed
84c952527e6f89d5427881462823514be9d79f13e6Eric Laurent        // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies()
85c952527e6f89d5427881462823514be9d79f13e6Eric Laurent        mPreviousOutputs = mOutputs;
869f4165f36d90dddc32246d0eddbf289383c3b2bbEric Laurent        String8 paramStr;
87f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        switch (state)
88f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        {
89f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // handle output device connection
90f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        case AudioSystem::DEVICE_STATE_AVAILABLE:
91f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (mAvailableOutputDevices & device) {
9264cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block                ALOGW("setDeviceConnectionState() device already connected: %x", device);
93f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                return INVALID_OPERATION;
94f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
956a70518b93928d1c91457ff805e375c82d76b0e5Steve Block            ALOGV("setDeviceConnectionState() connecting device %x", device);
96f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
9733bf1b0fe363bd4892349d160f54d860567fab12Mike Lockwood            if (mHasA2dp && audio_is_a2dp_out_device(device)) {
989f4165f36d90dddc32246d0eddbf289383c3b2bbEric Laurent                // handle A2DP device connection
999f4165f36d90dddc32246d0eddbf289383c3b2bbEric Laurent                AudioParameter param;
1009f4165f36d90dddc32246d0eddbf289383c3b2bbEric Laurent                param.add(String8(AUDIO_PARAMETER_A2DP_SINK_ADDRESS), String8(device_address));
1019f4165f36d90dddc32246d0eddbf289383c3b2bbEric Laurent                paramStr = param.toString();
102fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            } else if (mHasUsb && audio_is_usb_out_device(device)) {
1039f4165f36d90dddc32246d0eddbf289383c3b2bbEric Laurent                // handle USB device connection
1049f4165f36d90dddc32246d0eddbf289383c3b2bbEric Laurent                paramStr = String8(device_address, MAX_DEVICE_ADDRESS_LEN);
1059f4165f36d90dddc32246d0eddbf289383c3b2bbEric Laurent            }
1069f4165f36d90dddc32246d0eddbf289383c3b2bbEric Laurent
1079f4165f36d90dddc32246d0eddbf289383c3b2bbEric Laurent            if (checkOutputsForDevice(device, state, outputs, paramStr) != NO_ERROR) {
1083cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                return INVALID_OPERATION;
1093cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
110c7b6e3c857c5f5af5f244d0b877c41a9c55f92abColin Cross            ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs",
1113cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                  outputs.size());
112f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // register new device as available
113f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent            mAvailableOutputDevices = (audio_devices_t)(mAvailableOutputDevices | device);
114f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
11533bf1b0fe363bd4892349d160f54d860567fab12Mike Lockwood            if (mHasA2dp && audio_is_a2dp_out_device(device)) {
1169f4165f36d90dddc32246d0eddbf289383c3b2bbEric Laurent                // handle A2DP device connection
1179f4165f36d90dddc32246d0eddbf289383c3b2bbEric Laurent                mA2dpDeviceAddress = String8(device_address, MAX_DEVICE_ADDRESS_LEN);
1189f4165f36d90dddc32246d0eddbf289383c3b2bbEric Laurent                mA2dpSuspended = false;
1199f4165f36d90dddc32246d0eddbf289383c3b2bbEric Laurent            } else if (audio_is_bluetooth_sco_device(device)) {
1209f4165f36d90dddc32246d0eddbf289383c3b2bbEric Laurent                // handle SCO device connection
1219f4165f36d90dddc32246d0eddbf289383c3b2bbEric Laurent                mScoDeviceAddress = String8(device_address, MAX_DEVICE_ADDRESS_LEN);
122fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            } else if (mHasUsb && audio_is_usb_out_device(device)) {
1239f4165f36d90dddc32246d0eddbf289383c3b2bbEric Laurent                // handle USB device connection
124fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                mUsbOutCardAndDevice = String8(device_address, MAX_DEVICE_ADDRESS_LEN);
125f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1269f4165f36d90dddc32246d0eddbf289383c3b2bbEric Laurent
127f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            break;
128f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // handle output device disconnection
129f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        case AudioSystem::DEVICE_STATE_UNAVAILABLE: {
130f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (!(mAvailableOutputDevices & device)) {
13164cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block                ALOGW("setDeviceConnectionState() device not connected: %x", device);
132f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                return INVALID_OPERATION;
133f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
134f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1356a70518b93928d1c91457ff805e375c82d76b0e5Steve Block            ALOGV("setDeviceConnectionState() disconnecting device %x", device);
136f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // remove device from available output devices
137f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent            mAvailableOutputDevices = (audio_devices_t)(mAvailableOutputDevices & ~device);
1389f4165f36d90dddc32246d0eddbf289383c3b2bbEric Laurent            checkOutputsForDevice(device, state, outputs, paramStr);
139fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
14033bf1b0fe363bd4892349d160f54d860567fab12Mike Lockwood            if (mHasA2dp && audio_is_a2dp_out_device(device)) {
1413cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                // handle A2DP device disconnection
142b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                mA2dpDeviceAddress = "";
143b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                mA2dpSuspended = false;
144c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            } else if (audio_is_bluetooth_sco_device(device)) {
1453cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                // handle SCO device disconnection
146b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                mScoDeviceAddress = "";
147fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            } else if (mHasUsb && audio_is_usb_out_device(device)) {
1483cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                // handle USB device disconnection
149fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                mUsbOutCardAndDevice = "";
150f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
15148387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            // not currently handling multiple simultaneous submixes: ignoring remote submix
15248387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            //   case and address
153f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            } break;
154f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
155f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        default:
1565efbd421e0029d6fc44b1cc65c0e5e0d85e5161fSteve Block            ALOGE("setDeviceConnectionState() invalid state: %x", state);
157f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return BAD_VALUE;
158f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
159f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
160f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        checkA2dpSuspend();
161f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        checkOutputForAllStrategies();
162b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        // outputs must be closed after checkOutputForAllStrategies() is executed
1633cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        if (!outputs.isEmpty()) {
1643cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            for (size_t i = 0; i < outputs.size(); i++) {
165a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                AudioOutputDescriptor *desc = mOutputs.valueFor(outputs[i]);
1663cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                // close unused outputs after device disconnection or direct outputs that have been
1673cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                // opened by checkOutputsForDevice() to query dynamic parameters
1683cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                if ((state == AudioSystem::DEVICE_STATE_UNAVAILABLE) ||
169a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                        (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) &&
170a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                         (desc->mDirectOpenCount == 0))) {
1713cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    closeOutput(outputs[i]);
1723cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                }
1733cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
174f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1755ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
176c952527e6f89d5427881462823514be9d79f13e6Eric Laurent        updateDevicesAndOutputs();
1775ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        for (size_t i = 0; i < mOutputs.size(); i++) {
1782c72e9faa5ad5e324b85b78e383dd85c8bdc04a9Eric Laurent            // do not force device change on duplicated output because if device is 0, it will
1792c72e9faa5ad5e324b85b78e383dd85c8bdc04a9Eric Laurent            // also force a device 0 for the two outputs it is duplicated to which may override
1802c72e9faa5ad5e324b85b78e383dd85c8bdc04a9Eric Laurent            // a valid device selection on those outputs.
18176e97d3950f2654adbb0a415218b6d048200c395Eric Laurent            setOutputDevice(mOutputs.keyAt(i),
18276e97d3950f2654adbb0a415218b6d048200c395Eric Laurent                            getNewDevice(mOutputs.keyAt(i), true /*fromCache*/),
1832c72e9faa5ad5e324b85b78e383dd85c8bdc04a9Eric Laurent                            !mOutputs.valueAt(i)->isDuplicated(),
18476e97d3950f2654adbb0a415218b6d048200c395Eric Laurent                            0);
1855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
186f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
187806fab660adf8a5c3d36e9411d214a4942cd67e9Eric Laurent        return NO_ERROR;
188fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    }  // end if is output device
189fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
190f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // handle input devices
191c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    if (audio_is_input_device(device)) {
192fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        SortedVector <audio_io_handle_t> inputs;
193f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
194fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        String8 paramStr;
195f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        switch (state)
196f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        {
197f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // handle input device connection
198f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        case AudioSystem::DEVICE_STATE_AVAILABLE: {
199f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (mAvailableInputDevices & device) {
20064cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block                ALOGW("setDeviceConnectionState() device already connected: %d", device);
201f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                return INVALID_OPERATION;
202f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
203fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
204fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            if (mHasUsb && audio_is_usb_in_device(device)) {
205fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                // handle USB device connection
206fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                paramStr = String8(device_address, MAX_DEVICE_ADDRESS_LEN);
20733bf1b0fe363bd4892349d160f54d860567fab12Mike Lockwood            } else if (mHasA2dp && audio_is_a2dp_in_device(device)) {
20833bf1b0fe363bd4892349d160f54d860567fab12Mike Lockwood                // handle A2DP device connection
20933bf1b0fe363bd4892349d160f54d860567fab12Mike Lockwood                AudioParameter param;
21033bf1b0fe363bd4892349d160f54d860567fab12Mike Lockwood                param.add(String8(AUDIO_PARAMETER_A2DP_SOURCE_ADDRESS), String8(device_address));
21133bf1b0fe363bd4892349d160f54d860567fab12Mike Lockwood                paramStr = param.toString();
212fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            }
213fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
214fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            if (checkInputsForDevice(device, state, inputs, paramStr) != NO_ERROR) {
215fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                return INVALID_OPERATION;
216fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            }
217ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            mAvailableInputDevices = mAvailableInputDevices | (device & ~AUDIO_DEVICE_BIT_IN);
218f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
219f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            break;
220f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
221f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // handle input device disconnection
222f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        case AudioSystem::DEVICE_STATE_UNAVAILABLE: {
223f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (!(mAvailableInputDevices & device)) {
22464cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block                ALOGW("setDeviceConnectionState() device not connected: %d", device);
225f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                return INVALID_OPERATION;
226f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
227fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            checkInputsForDevice(device, state, inputs, paramStr);
2285ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            mAvailableInputDevices = (audio_devices_t) (mAvailableInputDevices & ~device);
229fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        } break;
230f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
231f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        default:
2325efbd421e0029d6fc44b1cc65c0e5e0d85e5161fSteve Block            ALOGE("setDeviceConnectionState() invalid state: %x", state);
233f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return BAD_VALUE;
234f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
235f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
236fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        closeAllInputs();
237f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
238f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return NO_ERROR;
239fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    } // end if is input device
240f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
24164cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block    ALOGW("setDeviceConnectionState() invalid device: %x", device);
242f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return BAD_VALUE;
243f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
244f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
245c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel TriviAudioSystem::device_connection_state AudioPolicyManagerBase::getDeviceConnectionState(audio_devices_t device,
246f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                  const char *device_address)
247f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
248f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioSystem::device_connection_state state = AudioSystem::DEVICE_STATE_UNAVAILABLE;
249f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    String8 address = String8(device_address);
250c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    if (audio_is_output_device(device)) {
251f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (device & mAvailableOutputDevices) {
25233bf1b0fe363bd4892349d160f54d860567fab12Mike Lockwood            if (audio_is_a2dp_out_device(device) &&
2535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                (!mHasA2dp || (address != "" && mA2dpDeviceAddress != address))) {
254f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                return state;
255f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
256c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            if (audio_is_bluetooth_sco_device(device) &&
257f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                address != "" && mScoDeviceAddress != address) {
258f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                return state;
259f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
260fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            if (audio_is_usb_out_device(device) &&
261fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                (!mHasUsb || (address != "" && mUsbOutCardAndDevice != address))) {
26248387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi                ALOGE("getDeviceConnectionState() invalid device: %x", device);
26348387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi                return state;
26448387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            }
26548387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            if (audio_is_remote_submix_device((audio_devices_t)device) && !mHasRemoteSubmix) {
266599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent                return state;
267599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            }
268f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            state = AudioSystem::DEVICE_STATE_AVAILABLE;
269f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
270c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    } else if (audio_is_input_device(device)) {
271f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (device & mAvailableInputDevices) {
272f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            state = AudioSystem::DEVICE_STATE_AVAILABLE;
273f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
274f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
275f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
276f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return state;
277f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
278f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
279f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::setPhoneState(int state)
280f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2816a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("setPhoneState() state %d", state);
282ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    audio_devices_t newDevice = AUDIO_DEVICE_NONE;
283f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (state < 0 || state >= AudioSystem::NUM_MODES) {
28464cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("setPhoneState() invalid state %d", state);
285f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
286f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
287f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
288f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (state == mPhoneState ) {
28964cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("setPhoneState() setting same state %d", state);
290f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
291f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
292f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
293f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if leaving call state, handle special case of active streams
294f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // pertaining to sonification strategy see handleIncallSonification()
295f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (isInCall()) {
2966a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("setPhoneState() in call state management: new state is %d", state);
297f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
298f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            handleIncallSonification(stream, false, true);
299f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
300f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
301f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
302f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // store previous phone state for management of sonification strategy below
303f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    int oldState = mPhoneState;
304f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mPhoneState = state;
305f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    bool force = false;
306f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
307f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // are we entering or starting a call
308f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (!isStateInCall(oldState) && isStateInCall(state)) {
3096a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("  Entering call in setPhoneState()");
310f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // force routing command to audio hardware when starting a call
311f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // even if no device change is needed
312f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        force = true;
31387e28f7b352fb77f46e16ebcdd85cbf01396a203Eric Laurent        for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) {
31487e28f7b352fb77f46e16ebcdd85cbf01396a203Eric Laurent            mStreams[AUDIO_STREAM_DTMF].mVolumeCurve[j] =
31587e28f7b352fb77f46e16ebcdd85cbf01396a203Eric Laurent                    sVolumeProfiles[AUDIO_STREAM_VOICE_CALL][j];
31687e28f7b352fb77f46e16ebcdd85cbf01396a203Eric Laurent        }
317f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else if (isStateInCall(oldState) && !isStateInCall(state)) {
3186a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("  Exiting call in setPhoneState()");
319f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // force routing command to audio hardware when exiting a call
320f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // even if no device change is needed
321f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        force = true;
32287e28f7b352fb77f46e16ebcdd85cbf01396a203Eric Laurent        for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) {
32387e28f7b352fb77f46e16ebcdd85cbf01396a203Eric Laurent            mStreams[AUDIO_STREAM_DTMF].mVolumeCurve[j] =
32487e28f7b352fb77f46e16ebcdd85cbf01396a203Eric Laurent                    sVolumeProfiles[AUDIO_STREAM_DTMF][j];
32587e28f7b352fb77f46e16ebcdd85cbf01396a203Eric Laurent        }
326f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else if (isStateInCall(state) && (state != oldState)) {
3276a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("  Switching between telephony and VoIP in setPhoneState()");
328f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // force routing command to audio hardware when switching between telephony and VoIP
329f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // even if no device change is needed
330f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        force = true;
331f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
332f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
333f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // check for device and output changes triggered by new phone state
3345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    newDevice = getNewDevice(mPrimaryOutput, false /*fromCache*/);
335f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkA2dpSuspend();
336f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForAllStrategies();
337c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    updateDevicesAndOutputs();
338f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
339b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    AudioOutputDescriptor *hwOutputDesc = mOutputs.valueFor(mPrimaryOutput);
340f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
341f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // force routing command to audio hardware when ending call
342f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // even if no device change is needed
343ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if (isStateInCall(oldState) && newDevice == AUDIO_DEVICE_NONE) {
344f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        newDevice = hwOutputDesc->device();
345f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
346f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
347f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    int delayMs = 0;
348772686310a3bc44d93591349e174a60360a3d30dSathishKumar Mani    if (isStateInCall(state)) {
34980f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent        nsecs_t sysTime = systemTime();
350772686310a3bc44d93591349e174a60360a3d30dSathishKumar Mani        for (size_t i = 0; i < mOutputs.size(); i++) {
351772686310a3bc44d93591349e174a60360a3d30dSathishKumar Mani            AudioOutputDescriptor *desc = mOutputs.valueAt(i);
35280f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent            // mute media and sonification strategies and delay device switch by the largest
35380f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent            // latency of any output where either strategy is active.
35480f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent            // This avoid sending the ring tone or music tail into the earpiece or headset.
35580f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent            if ((desc->isStrategyActive(STRATEGY_MEDIA,
35680f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                                     SONIFICATION_HEADSET_MUSIC_DELAY,
35780f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                                     sysTime) ||
35880f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                    desc->isStrategyActive(STRATEGY_SONIFICATION,
35980f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                                         SONIFICATION_HEADSET_MUSIC_DELAY,
36080f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                                         sysTime)) &&
36180f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                    (delayMs < (int)desc->mLatency*2)) {
36280f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                delayMs = desc->mLatency*2;
363772686310a3bc44d93591349e174a60360a3d30dSathishKumar Mani            }
36480f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent            setStrategyMute(STRATEGY_MEDIA, true, mOutputs.keyAt(i));
36580f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent            setStrategyMute(STRATEGY_MEDIA, false, mOutputs.keyAt(i), MUTE_TIME_MS,
36680f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
36780f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent            setStrategyMute(STRATEGY_SONIFICATION, true, mOutputs.keyAt(i));
36880f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent            setStrategyMute(STRATEGY_SONIFICATION, false, mOutputs.keyAt(i), MUTE_TIME_MS,
36980f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
370772686310a3bc44d93591349e174a60360a3d30dSathishKumar Mani        }
371772686310a3bc44d93591349e174a60360a3d30dSathishKumar Mani    }
372772686310a3bc44d93591349e174a60360a3d30dSathishKumar Mani
373f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // change routing is necessary
374b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    setOutputDevice(mPrimaryOutput, newDevice, force, delayMs);
375f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
376f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if entering in call state, handle special case of active streams
377f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // pertaining to sonification strategy see handleIncallSonification()
378f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (isStateInCall(state)) {
3796a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("setPhoneState() in call state management: new state is %d", state);
380f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
381f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            handleIncallSonification(stream, true, true);
382f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
383f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
384f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
385f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
386f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (state == AudioSystem::MODE_RINGTONE &&
387f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        isStreamActive(AudioSystem::MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
388f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mLimitRingtoneVolume = true;
389f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
390f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mLimitRingtoneVolume = false;
391f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
392f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
393f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
394f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::setForceUse(AudioSystem::force_use usage, AudioSystem::forced_config config)
395f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3966a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mPhoneState);
397f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
398f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    bool forceVolumeReeval = false;
399f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    switch(usage) {
400f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::FOR_COMMUNICATION:
401f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (config != AudioSystem::FORCE_SPEAKER && config != AudioSystem::FORCE_BT_SCO &&
402f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_NONE) {
40364cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("setForceUse() invalid config %d for FOR_COMMUNICATION", config);
404f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return;
405f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
406f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        forceVolumeReeval = true;
407f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mForceUse[usage] = config;
408f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
409f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::FOR_MEDIA:
410f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (config != AudioSystem::FORCE_HEADPHONES && config != AudioSystem::FORCE_BT_A2DP &&
411f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_WIRED_ACCESSORY &&
412f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_ANALOG_DOCK &&
4131afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent            config != AudioSystem::FORCE_DIGITAL_DOCK && config != AudioSystem::FORCE_NONE &&
41431363a9cb94e80330c335fede0b92b1953a09517Jean-Michel Trivi            config != AudioSystem::FORCE_NO_BT_A2DP) {
41564cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("setForceUse() invalid config %d for FOR_MEDIA", config);
416f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return;
417f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
418f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mForceUse[usage] = config;
419f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
420f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::FOR_RECORD:
421f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (config != AudioSystem::FORCE_BT_SCO && config != AudioSystem::FORCE_WIRED_ACCESSORY &&
422f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_NONE) {
42364cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("setForceUse() invalid config %d for FOR_RECORD", config);
424f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return;
425f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
426f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mForceUse[usage] = config;
427f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
428f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::FOR_DOCK:
429f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (config != AudioSystem::FORCE_NONE && config != AudioSystem::FORCE_BT_CAR_DOCK &&
430f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_BT_DESK_DOCK &&
431f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_WIRED_ACCESSORY &&
432f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_ANALOG_DOCK &&
433f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_DIGITAL_DOCK) {
43464cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("setForceUse() invalid config %d for FOR_DOCK", config);
435f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
436f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        forceVolumeReeval = true;
437f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mForceUse[usage] = config;
438f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
439738207def5f691d605ae33d041116829a74513a9Eric Laurent    case AudioSystem::FOR_SYSTEM:
440738207def5f691d605ae33d041116829a74513a9Eric Laurent        if (config != AudioSystem::FORCE_NONE &&
441738207def5f691d605ae33d041116829a74513a9Eric Laurent            config != AudioSystem::FORCE_SYSTEM_ENFORCED) {
442738207def5f691d605ae33d041116829a74513a9Eric Laurent            ALOGW("setForceUse() invalid config %d for FOR_SYSTEM", config);
443738207def5f691d605ae33d041116829a74513a9Eric Laurent        }
444738207def5f691d605ae33d041116829a74513a9Eric Laurent        forceVolumeReeval = true;
445738207def5f691d605ae33d041116829a74513a9Eric Laurent        mForceUse[usage] = config;
446738207def5f691d605ae33d041116829a74513a9Eric Laurent        break;
447f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    default:
44864cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("setForceUse() invalid usage %d", usage);
449f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
450f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
451f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
4525ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // check for device and output changes triggered by new force usage
453f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkA2dpSuspend();
454f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForAllStrategies();
455c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    updateDevicesAndOutputs();
4565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < mOutputs.size(); i++) {
4575ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_io_handle_t output = mOutputs.keyAt(i);
4585ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_devices_t newDevice = getNewDevice(output, true /*fromCache*/);
459ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        setOutputDevice(output, newDevice, (newDevice != AUDIO_DEVICE_NONE));
460ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
4615ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            applyStreamVolumes(output, newDevice, 0, true);
4625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
463f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
464f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
465f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    audio_io_handle_t activeInput = getActiveInput();
466f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (activeInput != 0) {
467f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioInputDescriptor *inputDesc = mInputs.valueFor(activeInput);
4685ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_devices_t newDevice = getDeviceForInputSource(inputDesc->mInputSource);
469ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if ((newDevice != AUDIO_DEVICE_NONE) && (newDevice != inputDesc->mDevice)) {
4706a70518b93928d1c91457ff805e375c82d76b0e5Steve Block            ALOGV("setForceUse() changing device from %x to %x for input %d",
471f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    inputDesc->mDevice, newDevice, activeInput);
472f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            inputDesc->mDevice = newDevice;
473f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            AudioParameter param = AudioParameter();
474f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            param.addInt(String8(AudioParameter::keyRouting), (int)newDevice);
475f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mpClientInterface->setParameters(activeInput, param.toString());
476f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
477f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
478f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
479f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
480f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
481f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima ZavinAudioSystem::forced_config AudioPolicyManagerBase::getForceUse(AudioSystem::force_use usage)
482f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
483f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return mForceUse[usage];
484f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
485f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
486f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::setSystemProperty(const char* property, const char* value)
487f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
4886a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("setSystemProperty() property %s, value %s", property, value);
489f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
490f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
491b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent// Find a direct output profile compatible with the parameters passed, even if the input flags do
492b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent// not explicitly request a direct output
4933cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric LaurentAudioPolicyManagerBase::IOProfile *AudioPolicyManagerBase::getProfileForDirectOutput(
4943cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                               audio_devices_t device,
49570c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                               uint32_t samplingRate,
4965082dbeb19e37883230510129c94336063a4f91cGlenn Kasten                                                               audio_format_t format,
4972c3d2379ee6eadb32b63bf6de538cfd6487e8e20Glenn Kasten                                                               audio_channel_mask_t channelMask,
4980977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent                                                               audio_output_flags_t flags)
49970c236c9290732782d5267935af1475b8d5ae602Eric Laurent{
50070c236c9290732782d5267935af1475b8d5ae602Eric Laurent    for (size_t i = 0; i < mHwModules.size(); i++) {
50170c236c9290732782d5267935af1475b8d5ae602Eric Laurent        if (mHwModules[i]->mHandle == 0) {
50270c236c9290732782d5267935af1475b8d5ae602Eric Laurent            continue;
50370c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
50470c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) {
505a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald            IOProfile *profile = mHwModules[i]->mOutputProfiles[j];
506a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald            if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
507a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                if (profile->isCompatibleProfile(device, samplingRate, format,
508a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                                           channelMask,
509a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                                           AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
510a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                    if (mAvailableOutputDevices & profile->mSupportedDevices) {
511a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                        return mHwModules[i]->mOutputProfiles[j];
512a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                    }
513a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                }
514b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent            } else {
515a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                if (profile->isCompatibleProfile(device, samplingRate, format,
5163cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                           channelMask,
5173cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                           AUDIO_OUTPUT_FLAG_DIRECT)) {
518a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                    if (mAvailableOutputDevices & profile->mSupportedDevices) {
519a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                        return mHwModules[i]->mOutputProfiles[j];
520a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                    }
521a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                }
522a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald            }
52370c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
52470c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
52570c236c9290732782d5267935af1475b8d5ae602Eric Laurent    return 0;
52670c236c9290732782d5267935af1475b8d5ae602Eric Laurent}
52770c236c9290732782d5267935af1475b8d5ae602Eric Laurent
528f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinaudio_io_handle_t AudioPolicyManagerBase::getOutput(AudioSystem::stream_type stream,
529f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    uint32_t samplingRate,
5305082dbeb19e37883230510129c94336063a4f91cGlenn Kasten                                    audio_format_t format,
5312c3d2379ee6eadb32b63bf6de538cfd6487e8e20Glenn Kasten                                    audio_channel_mask_t channelMask,
532b4d07b97d23cfaffe22c7859ad7c45e168a7df0eRichard Fitzgerald                                    AudioSystem::output_flags flags,
533b4d07b97d23cfaffe22c7859ad7c45e168a7df0eRichard Fitzgerald                                    const audio_offload_info_t *offloadInfo)
534f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
535f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    audio_io_handle_t output = 0;
536f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    uint32_t latency = 0;
537f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    routing_strategy strategy = getStrategy((AudioSystem::stream_type)stream);
5385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
539a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    ALOGV("getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x",
540a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald          device, stream, samplingRate, format, channelMask, flags);
541f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
542f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
543f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mCurOutput != 0) {
54470c236c9290732782d5267935af1475b8d5ae602Eric Laurent        ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
545f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
546f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
547f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (mTestOutputs[mCurOutput] == 0) {
5486a70518b93928d1c91457ff805e375c82d76b0e5Steve Block            ALOGV("getOutput() opening test output");
5493cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(NULL);
550f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            outputDesc->mDevice = mTestDevice;
551f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            outputDesc->mSamplingRate = mTestSamplingRate;
552f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            outputDesc->mFormat = mTestFormat;
55370c236c9290732782d5267935af1475b8d5ae602Eric Laurent            outputDesc->mChannelMask = mTestChannels;
554f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            outputDesc->mLatency = mTestLatencyMs;
5550977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent            outputDesc->mFlags = (audio_output_flags_t)(mDirectOutput ? AudioSystem::OUTPUT_FLAG_DIRECT : 0);
556f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            outputDesc->mRefCount[stream] = 0;
55770c236c9290732782d5267935af1475b8d5ae602Eric Laurent            mTestOutputs[mCurOutput] = mpClientInterface->openOutput(0, &outputDesc->mDevice,
558f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            &outputDesc->mSamplingRate,
559f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            &outputDesc->mFormat,
56070c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                            &outputDesc->mChannelMask,
561f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            &outputDesc->mLatency,
562b4d07b97d23cfaffe22c7859ad7c45e168a7df0eRichard Fitzgerald                                            outputDesc->mFlags,
563b4d07b97d23cfaffe22c7859ad7c45e168a7df0eRichard Fitzgerald                                            offloadInfo);
564f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (mTestOutputs[mCurOutput]) {
565f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                AudioParameter outputCmd = AudioParameter();
566f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                outputCmd.addInt(String8("set_id"),mCurOutput);
567f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
568f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                addOutput(mTestOutputs[mCurOutput], outputDesc);
569f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
570f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
571f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return mTestOutputs[mCurOutput];
572f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
573f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
574f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
575f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // open a direct output if required by specified parameters
576a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    //force direct flag if offload flag is set: offloading implies a direct output stream
577a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    // and all common behaviors are driven by checking only the direct flag
578a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    // this should normally be set appropriately in the policy configuration file
579a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
580a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        flags = (AudioSystem::output_flags)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
581a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    }
582a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald
583000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    // Do not allow offloading if one non offloadable effect is enabled. This prevents from
584000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    // creating an offloaded track and tearing it down immediately after start when audioflinger
585000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    // detects there is an active non offloadable effect.
586000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    // FIXME: We should check the audio session here but we do not have it in this context.
587000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    // This may prevent offloading in rare situations where effects are left active by apps
588000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    // in the background.
589000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    IOProfile *profile = NULL;
590000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
591000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent            !isNonOffloadableEffectEnabled()) {
592000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent        profile = getProfileForDirectOutput(device,
593000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent                                           samplingRate,
594000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent                                           format,
595000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent                                           channelMask,
596000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent                                           (audio_output_flags_t)flags);
597000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    }
598000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent
5993cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    if (profile != NULL) {
6005a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        AudioOutputDescriptor *outputDesc = NULL;
601f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
6025a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        for (size_t i = 0; i < mOutputs.size(); i++) {
6035a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent            AudioOutputDescriptor *desc = mOutputs.valueAt(i);
6045a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent            if (!desc->isDuplicated() && (profile == desc->mProfile)) {
6055a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent                outputDesc = desc;
6065a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent                // reuse direct output if currently open and configured with same parameters
6075a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent                if ((samplingRate == outputDesc->mSamplingRate) &&
6085a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent                        (format == outputDesc->mFormat) &&
6095a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent                        (channelMask == outputDesc->mChannelMask)) {
6105a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent                    outputDesc->mDirectOpenCount++;
611a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                    ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
6125a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent                    return mOutputs.keyAt(i);
6135a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent                }
6145a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent            }
6155a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        }
6165a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        // close direct output if currently open and configured with different parameters
6175a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        if (outputDesc != NULL) {
6185a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent            closeOutput(outputDesc->mId);
6195a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        }
6205a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        outputDesc = new AudioOutputDescriptor(profile);
621f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mDevice = device;
622f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mSamplingRate = samplingRate;
6232d749b0bda4ae60ef1a940eeba86121b2e94d07dGlenn Kasten        outputDesc->mFormat = format;
6242c3d2379ee6eadb32b63bf6de538cfd6487e8e20Glenn Kasten        outputDesc->mChannelMask = channelMask;
625f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mLatency = 0;
626a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        outputDesc->mFlags =(audio_output_flags_t) (outputDesc->mFlags | flags);
627f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mRefCount[stream] = 0;
628f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mStopTime[stream] = 0;
6295a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        outputDesc->mDirectOpenCount = 1;
6303cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        output = mpClientInterface->openOutput(profile->mModule->mHandle,
63170c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                        &outputDesc->mDevice,
632f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                        &outputDesc->mSamplingRate,
633f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                        &outputDesc->mFormat,
63470c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                        &outputDesc->mChannelMask,
635f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                        &outputDesc->mLatency,
636a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                                        outputDesc->mFlags,
637a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                                        offloadInfo);
638f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
6399f1f9b509c930830f6f32e9ef6c2c8a03d6fa96eJean-Michel Trivi        // only accept an output with the requested parameters
640f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (output == 0 ||
641f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            (samplingRate != 0 && samplingRate != outputDesc->mSamplingRate) ||
6422d749b0bda4ae60ef1a940eeba86121b2e94d07dGlenn Kasten            (format != AUDIO_FORMAT_DEFAULT && format != outputDesc->mFormat) ||
64370c236c9290732782d5267935af1475b8d5ae602Eric Laurent            (channelMask != 0 && channelMask != outputDesc->mChannelMask)) {
6443cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
6453cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    "format %d %d, channelMask %04x %04x", output, samplingRate,
6463cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
6473cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    outputDesc->mChannelMask);
648f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (output != 0) {
649f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mpClientInterface->closeOutput(output);
650f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
651f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            delete outputDesc;
652f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return 0;
653f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
65445c763947b657b347211dc9388754e05d30d0467Eric Laurent        audio_io_handle_t srcOutput = getOutputForEffect();
655f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        addOutput(output, outputDesc);
65645c763947b657b347211dc9388754e05d30d0467Eric Laurent        audio_io_handle_t dstOutput = getOutputForEffect();
65745c763947b657b347211dc9388754e05d30d0467Eric Laurent        if (dstOutput == output) {
65845c763947b657b347211dc9388754e05d30d0467Eric Laurent            mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
65945c763947b657b347211dc9388754e05d30d0467Eric Laurent        }
6605a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        mPreviousOutputs = mOutputs;
6615a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        ALOGV("getOutput() returns new direct output %d", output);
662f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return output;
663f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
664f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
6659f1f9b509c930830f6f32e9ef6c2c8a03d6fa96eJean-Michel Trivi    // ignoring channel mask due to downmix capability in mixer
6669f1f9b509c930830f6f32e9ef6c2c8a03d6fa96eJean-Michel Trivi
667f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // open a non direct output
668f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
669a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    // for non direct outputs, only PCM is supported
6702d749b0bda4ae60ef1a940eeba86121b2e94d07dGlenn Kasten    if (audio_is_linear_pcm(format)) {
671a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        // get which output is suitable for the specified stream. The actual
672a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        // routing change will happen when startOutput() will be called
673a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
674f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
675a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        output = selectOutput(outputs, flags);
676a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    }
677a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
67870c236c9290732782d5267935af1475b8d5ae602Eric Laurent            "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
679f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
6805ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGV("getOutput() returns output %d", output);
6815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
682f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return output;
683f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
684f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
6855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentaudio_io_handle_t AudioPolicyManagerBase::selectOutput(const SortedVector<audio_io_handle_t>& outputs,
6865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                       AudioSystem::output_flags flags)
6875ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
6885ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // select one output among several that provide a path to a particular device or set of
6895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // devices (the list was previously build by getOutputsForDevice()).
6905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // The priority is as follows:
6915ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // 1: the output with the highest number of requested policy flags
6925ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // 2: the primary output
6935ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // 3: the first output in the list
6945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
6955ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (outputs.size() == 0) {
6965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return 0;
6975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
6985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (outputs.size() == 1) {
6995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return outputs[0];
7005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
7015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
7025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    int maxCommonFlags = 0;
7035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    audio_io_handle_t outputFlags = 0;
7045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    audio_io_handle_t outputPrimary = 0;
7055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
7065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < outputs.size(); i++) {
7075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        AudioOutputDescriptor *outputDesc = mOutputs.valueFor(outputs[i]);
7085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (!outputDesc->isDuplicated()) {
7095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            int commonFlags = (int)AudioSystem::popCount(outputDesc->mProfile->mFlags & flags);
7105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            if (commonFlags > maxCommonFlags) {
7115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                outputFlags = outputs[i];
7125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                maxCommonFlags = commonFlags;
7135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags);
7145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
7150977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent            if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
7165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                outputPrimary = outputs[i];
7175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
7185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
7195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
7205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
7215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (outputFlags != 0) {
7225ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return outputFlags;
7235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
7245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (outputPrimary != 0) {
7255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return outputPrimary;
7265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
7275ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
7285ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return outputs[0];
7295ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
7305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
731f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::startOutput(audio_io_handle_t output,
732f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                             AudioSystem::stream_type stream,
733f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                             int session)
734f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
7356a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("startOutput() output %d, stream %d, session %d", output, stream, session);
736f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mOutputs.indexOfKey(output);
737f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
7386742b021061741c309d6e45c074b45a560f11086Glenn Kasten        ALOGW("startOutput() unknown output %d", output);
739f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
740f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
741f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
742f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index);
743f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
7445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // increment usage count for this stream on the requested output:
745f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // NOTE that the usage count is the same for duplicated output and hardware output which is
7465ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
747f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    outputDesc->changeRefCount(stream, 1);
748f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
7495ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (outputDesc->mRefCount[stream] == 1) {
7505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_devices_t newDevice = getNewDevice(output, false /*fromCache*/);
751b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        routing_strategy strategy = getStrategy(stream);
752b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
753b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                            (strategy == STRATEGY_SONIFICATION_RESPECTFUL);
754b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        uint32_t waitMs = 0;
7555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        bool force = false;
7565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        for (size_t i = 0; i < mOutputs.size(); i++) {
7575ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            AudioOutputDescriptor *desc = mOutputs.valueAt(i);
758b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent            if (desc != outputDesc) {
759b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                // force a device change if any other output is managed by the same hw
760b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                // module and has a current device selection that differs from selected device.
761b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                // In this case, the audio HAL must receive the new device selection so that it can
762b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                // change the device currently selected by the other active output.
763b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                if (outputDesc->sharesHwModuleWith(desc) &&
7645ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                    desc->device() != newDevice) {
765b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                    force = true;
766b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                }
767b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                // wait for audio on other active outputs to be presented when starting
768b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                // a notification so that audio focus effect can propagate.
76942fa8215a763822c0d03fc936e4cac1eb864c9ccEric Laurent                uint32_t latency = desc->latency();
77042fa8215a763822c0d03fc936e4cac1eb864c9ccEric Laurent                if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
77142fa8215a763822c0d03fc936e4cac1eb864c9ccEric Laurent                    waitMs = latency;
772b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                }
7735ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
7745ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
775b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        uint32_t muteWaitMs = setOutputDevice(output, newDevice, force);
776f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
7775ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // handle special case for sonification while in call
7785ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (isInCall()) {
7795ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            handleIncallSonification(stream, true, false);
7805ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
781c16ac09f510437e8340be691720177a490ae78f0Eric Laurent
7825ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // apply volume rules for current stream and device if necessary
7835ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        checkAndSetVolume(stream,
784c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                          mStreams[stream].getVolumeIndex(newDevice),
7855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                          output,
7865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                          newDevice);
78712bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi
7885ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // update the outputs if starting an output with a stream that can affect notification
7895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // routing
7905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        handleNotificationRoutingForStream(stream);
791b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        if (waitMs > muteWaitMs) {
792b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent            usleep((waitMs - muteWaitMs) * 2 * 1000);
793b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        }
7945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
795f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
796f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
797f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
7985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
799f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::stopOutput(audio_io_handle_t output,
800f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            AudioSystem::stream_type stream,
801f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            int session)
802f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
8036a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
804f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mOutputs.indexOfKey(output);
805f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
8066742b021061741c309d6e45c074b45a560f11086Glenn Kasten        ALOGW("stopOutput() unknown output %d", output);
807f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
808f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
809f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
810f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index);
811f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
812f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // handle special case for sonification while in call
813f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (isInCall()) {
814f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        handleIncallSonification(stream, false, false);
815f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
816f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
817f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (outputDesc->mRefCount[stream] > 0) {
818f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // decrement usage count of this stream on the output
819f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->changeRefCount(stream, -1);
820f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // store time at which the stream was stopped - see isStreamActive()
8215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (outputDesc->mRefCount[stream] == 0) {
8225ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            outputDesc->mStopTime[stream] = systemTime();
8235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            audio_devices_t newDevice = getNewDevice(output, false /*fromCache*/);
8245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // delay the device switch by twice the latency because stopOutput() is executed when
8255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // the track stop() command is received and at that time the audio track buffer can
8265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // still contain data that needs to be drained. The latency only covers the audio HAL
8275ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // and kernel buffers. Also the latency does not always include additional delay in the
8285ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // audio path (audio DSP, CODEC ...)
8295ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            setOutputDevice(output, newDevice, false, outputDesc->mLatency*2);
8305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
8315ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // force restoring the device selection on other active outputs if it differs from the
8325ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // one being selected for this output
8335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            for (size_t i = 0; i < mOutputs.size(); i++) {
8345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                audio_io_handle_t curOutput = mOutputs.keyAt(i);
8355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                AudioOutputDescriptor *desc = mOutputs.valueAt(i);
8365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                if (curOutput != output &&
83780f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                        desc->isActive() &&
8385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                        outputDesc->sharesHwModuleWith(desc) &&
839a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                        (newDevice != desc->device())) {
8405ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                    setOutputDevice(curOutput,
8415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                    getNewDevice(curOutput, false /*fromCache*/),
8425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                    true,
8435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                    outputDesc->mLatency*2);
8445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                }
8455ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
8465ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // update the outputs if stopping one with a stream that can affect notification routing
8475ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            handleNotificationRoutingForStream(stream);
848f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
849f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return NO_ERROR;
850f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
85164cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("stopOutput() refcount is already 0 for output %d", output);
852f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return INVALID_OPERATION;
853f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
854f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
855f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
856f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::releaseOutput(audio_io_handle_t output)
857f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
8586a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("releaseOutput() %d", output);
859f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mOutputs.indexOfKey(output);
860f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
86164cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("releaseOutput() releasing unknown output %d", output);
862f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
863f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
864f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
865f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
866f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    int testIndex = testOutputIndex(output);
867f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (testIndex != 0) {
868f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index);
86980f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent        if (outputDesc->isActive()) {
870f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mpClientInterface->closeOutput(output);
871f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            delete mOutputs.valueAt(index);
872f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mOutputs.removeItem(output);
873f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mTestOutputs[testIndex] = 0;
874f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
875f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
876f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
877f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
878f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
8795a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent    AudioOutputDescriptor *desc = mOutputs.valueAt(index);
8805a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent    if (desc->mFlags & AudioSystem::OUTPUT_FLAG_DIRECT) {
8815a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        if (desc->mDirectOpenCount <= 0) {
8825a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent            ALOGW("releaseOutput() invalid open count %d for output %d",
8835a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent                                                              desc->mDirectOpenCount, output);
8845a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent            return;
8855a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        }
8865a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        if (--desc->mDirectOpenCount == 0) {
8875a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent            closeOutput(output);
88845c763947b657b347211dc9388754e05d30d0467Eric Laurent            // If effects where present on the output, audioflinger moved them to the primary
88945c763947b657b347211dc9388754e05d30d0467Eric Laurent            // output by default: move them back to the appropriate output.
89045c763947b657b347211dc9388754e05d30d0467Eric Laurent            audio_io_handle_t dstOutput = getOutputForEffect();
89145c763947b657b347211dc9388754e05d30d0467Eric Laurent            if (dstOutput != mPrimaryOutput) {
89245c763947b657b347211dc9388754e05d30d0467Eric Laurent                mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, mPrimaryOutput, dstOutput);
89345c763947b657b347211dc9388754e05d30d0467Eric Laurent            }
8945a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        }
895f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
896f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
897f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
89845c763947b657b347211dc9388754e05d30d0467Eric Laurent
899f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinaudio_io_handle_t AudioPolicyManagerBase::getInput(int inputSource,
900f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    uint32_t samplingRate,
9015082dbeb19e37883230510129c94336063a4f91cGlenn Kasten                                    audio_format_t format,
9022c3d2379ee6eadb32b63bf6de538cfd6487e8e20Glenn Kasten                                    audio_channel_mask_t channelMask,
903f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    AudioSystem::audio_in_acoustics acoustics)
904f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
905f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    audio_io_handle_t input = 0;
906f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent    audio_devices_t device = getDeviceForInputSource(inputSource);
907f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
90870c236c9290732782d5267935af1475b8d5ae602Eric Laurent    ALOGV("getInput() inputSource %d, samplingRate %d, format %d, channelMask %x, acoustics %x",
90970c236c9290732782d5267935af1475b8d5ae602Eric Laurent          inputSource, samplingRate, format, channelMask, acoustics);
910f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
911ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if (device == AUDIO_DEVICE_NONE) {
9125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        ALOGW("getInput() could not find device for inputSource %d", inputSource);
913f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return 0;
914f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
915f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
916f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // adapt channel selection to input source
917f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    switch(inputSource) {
918f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_UPLINK:
9192c3d2379ee6eadb32b63bf6de538cfd6487e8e20Glenn Kasten        channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK;
920f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
921f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_DOWNLINK:
9222c3d2379ee6eadb32b63bf6de538cfd6487e8e20Glenn Kasten        channelMask = AUDIO_CHANNEL_IN_VOICE_DNLINK;
923f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
924f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_CALL:
9252c3d2379ee6eadb32b63bf6de538cfd6487e8e20Glenn Kasten        channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK | AUDIO_CHANNEL_IN_VOICE_DNLINK;
926f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
927f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    default:
928f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
929f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
930f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
9315ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    IOProfile *profile = getInputProfile(device,
9325ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                         samplingRate,
9335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                         format,
93470c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                         channelMask);
9355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (profile == NULL) {
936fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        ALOGW("getInput() could not find profile for device 0x%X, samplingRate %d, format %d, "
937fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                "channelMask 0x%X",
93870c236c9290732782d5267935af1475b8d5ae602Eric Laurent                device, samplingRate, format, channelMask);
93970c236c9290732782d5267935af1475b8d5ae602Eric Laurent        return 0;
94070c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
94170c236c9290732782d5267935af1475b8d5ae602Eric Laurent
94270c236c9290732782d5267935af1475b8d5ae602Eric Laurent    if (profile->mModule->mHandle == 0) {
9433cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        ALOGE("getInput(): HW module %s not opened", profile->mModule->mName);
9445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return 0;
9455ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
9465ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
9475ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    AudioInputDescriptor *inputDesc = new AudioInputDescriptor(profile);
948f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
949f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    inputDesc->mInputSource = inputSource;
950f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    inputDesc->mDevice = device;
951f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    inputDesc->mSamplingRate = samplingRate;
9522d749b0bda4ae60ef1a940eeba86121b2e94d07dGlenn Kasten    inputDesc->mFormat = format;
9532c3d2379ee6eadb32b63bf6de538cfd6487e8e20Glenn Kasten    inputDesc->mChannelMask = channelMask;
954f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    inputDesc->mRefCount = 0;
955fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
95670c236c9290732782d5267935af1475b8d5ae602Eric Laurent    input = mpClientInterface->openInput(profile->mModule->mHandle,
95770c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                    &inputDesc->mDevice,
958f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    &inputDesc->mSamplingRate,
959f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    &inputDesc->mFormat,
96070c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                    &inputDesc->mChannelMask);
961f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
962f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // only accept input with the exact requested set of parameters
963f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (input == 0 ||
964f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        (samplingRate != inputDesc->mSamplingRate) ||
965f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        (format != inputDesc->mFormat) ||
96670c236c9290732782d5267935af1475b8d5ae602Eric Laurent        (channelMask != inputDesc->mChannelMask)) {
967fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        ALOGI("getInput() failed opening input: samplingRate %d, format %d, channelMask 0x%X",
96870c236c9290732782d5267935af1475b8d5ae602Eric Laurent                samplingRate, format, channelMask);
969f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (input != 0) {
970f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mpClientInterface->closeInput(input);
971f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
972f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        delete inputDesc;
973f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return 0;
974f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
975fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    addInput(input, inputDesc);
976fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
977f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return input;
978f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
979f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
980f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::startInput(audio_io_handle_t input)
981f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
9826a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("startInput() input %d", input);
983f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mInputs.indexOfKey(input);
984f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
9856742b021061741c309d6e45c074b45a560f11086Glenn Kasten        ALOGW("startInput() unknown input %d", input);
986f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
987f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
988f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioInputDescriptor *inputDesc = mInputs.valueAt(index);
989f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
990f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
991f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mTestInput == 0)
992f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
993f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    {
994fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent        // refuse 2 active AudioRecord clients at the same time except if the active input
995fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent        // uses AUDIO_SOURCE_HOTWORD in which case it is closed.
996fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent        audio_io_handle_t activeInput = getActiveInput();
997fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent        if (!isVirtualInputDevice(inputDesc->mDevice) && activeInput != 0) {
998fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent            AudioInputDescriptor *activeDesc = mInputs.valueFor(activeInput);
999fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent            if (activeDesc->mInputSource == AUDIO_SOURCE_HOTWORD) {
1000fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent                ALOGW("startInput() preempting already started low-priority input %d", activeInput);
1001fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent                stopInput(activeInput);
1002fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent                releaseInput(activeInput);
1003fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent            } else {
10046742b021061741c309d6e45c074b45a560f11086Glenn Kasten                ALOGW("startInput() input %d failed: other input already started", input);
1005fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent                return INVALID_OPERATION;
1006fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent            }
1007f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1008f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1009f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
101066707435156d8d99d795271a7bd54943065b4c2dEric Laurent    audio_devices_t newDevice = getDeviceForInputSource(inputDesc->mInputSource);
101166707435156d8d99d795271a7bd54943065b4c2dEric Laurent    if ((newDevice != AUDIO_DEVICE_NONE) && (newDevice != inputDesc->mDevice)) {
101266707435156d8d99d795271a7bd54943065b4c2dEric Laurent        inputDesc->mDevice = newDevice;
101366707435156d8d99d795271a7bd54943065b4c2dEric Laurent    }
1014b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown
1015b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown    // automatically enable the remote submix output when input is started
1016b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown    if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1017b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown        setDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1018b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown                AudioSystem::DEVICE_STATE_AVAILABLE, AUDIO_REMOTE_SUBMIX_DEVICE_ADDRESS);
1019b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown    }
1020b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown
1021f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioParameter param = AudioParameter();
1022f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    param.addInt(String8(AudioParameter::keyRouting), (int)inputDesc->mDevice);
1023f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1024fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent    int aliasSource = (inputDesc->mInputSource == AUDIO_SOURCE_HOTWORD) ?
1025fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent                                        AUDIO_SOURCE_VOICE_RECOGNITION : inputDesc->mInputSource;
1026fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent
1027fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent    param.addInt(String8(AudioParameter::keyInputSource), aliasSource);
10286a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("AudioPolicyManager::startInput() input source = %d", inputDesc->mInputSource);
1029f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1030f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mpClientInterface->setParameters(input, param.toString());
1031f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1032f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    inputDesc->mRefCount = 1;
1033f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
1034f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1035f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1036f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::stopInput(audio_io_handle_t input)
1037f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
10386a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("stopInput() input %d", input);
1039f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mInputs.indexOfKey(input);
1040f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
10416742b021061741c309d6e45c074b45a560f11086Glenn Kasten        ALOGW("stopInput() unknown input %d", input);
1042f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
1043f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1044f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioInputDescriptor *inputDesc = mInputs.valueAt(index);
1045f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1046f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (inputDesc->mRefCount == 0) {
104764cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("stopInput() input %d already stopped", input);
1048f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return INVALID_OPERATION;
1049f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
1050b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown        // automatically disable the remote submix output when input is stopped
1051b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown        if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1052b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown            setDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1053b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown                    AudioSystem::DEVICE_STATE_UNAVAILABLE, AUDIO_REMOTE_SUBMIX_DEVICE_ADDRESS);
1054b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown        }
1055b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown
1056f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioParameter param = AudioParameter();
1057f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        param.addInt(String8(AudioParameter::keyRouting), 0);
1058f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mpClientInterface->setParameters(input, param.toString());
1059f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        inputDesc->mRefCount = 0;
1060f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return NO_ERROR;
1061f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1062f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1063f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1064f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::releaseInput(audio_io_handle_t input)
1065f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
10666a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("releaseInput() %d", input);
1067f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mInputs.indexOfKey(input);
1068f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
106964cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("releaseInput() releasing unknown input %d", input);
1070f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
1071f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1072f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mpClientInterface->closeInput(input);
1073f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    delete mInputs.valueAt(index);
1074f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mInputs.removeItem(input);
1075fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
10766a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("releaseInput() exit");
1077f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1078f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1079fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLeanvoid AudioPolicyManagerBase::closeAllInputs() {
1080fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    for(size_t input_index = 0; input_index < mInputs.size(); input_index++) {
1081fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        mpClientInterface->closeInput(mInputs.keyAt(input_index));
1082fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    }
1083fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    mInputs.clear();
1084fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean}
1085fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
1086f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::initStreamVolume(AudioSystem::stream_type stream,
1087f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            int indexMin,
1088f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            int indexMax)
1089f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
10906a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
1091f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (indexMin < 0 || indexMin >= indexMax) {
109264cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("initStreamVolume() invalid index limits for stream %d, min %d, max %d", stream , indexMin, indexMax);
1093f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
1094f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1095f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mStreams[stream].mIndexMin = indexMin;
1096f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mStreams[stream].mIndexMax = indexMax;
1097f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1098f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1099c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentstatus_t AudioPolicyManagerBase::setStreamVolumeIndex(AudioSystem::stream_type stream,
1100c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                      int index,
1101c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                      audio_devices_t device)
1102f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1103f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1104f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if ((index < mStreams[stream].mIndexMin) || (index > mStreams[stream].mIndexMax)) {
1105f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
1106f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1107c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    if (!audio_is_output_device(device)) {
1108c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        return BAD_VALUE;
1109c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    }
1110f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1111f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // Force max volume if stream cannot be muted
1112f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (!mStreams[stream].mCanBeMuted) index = mStreams[stream].mIndexMax;
1113f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1114b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    ALOGV("setStreamVolumeIndex() stream %d, device %04x, index %d",
1115c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent          stream, device, index);
1116c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
1117c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    // if device is AUDIO_DEVICE_OUT_DEFAULT set default value and
1118c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    // clear all device specific values
1119c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    if (device == AUDIO_DEVICE_OUT_DEFAULT) {
1120c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        mStreams[stream].mIndexCur.clear();
1121c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    }
1122c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    mStreams[stream].mIndexCur.add(device, index);
1123f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1124f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // compute and apply stream volume on all outputs according to connected device
1125f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    status_t status = NO_ERROR;
1126f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mOutputs.size(); i++) {
1127c5eb8b4a5d4395ce335bc7c3e6df2678fa47e2ddEric Laurent        audio_devices_t curDevice =
1128c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                getDeviceForVolume(mOutputs.valueAt(i)->device());
1129e92d623811f3fd3e7cc5e5dd8bc93c0c0a8fdf50Eric Laurent        if ((device == AUDIO_DEVICE_OUT_DEFAULT) || (device == curDevice)) {
1130c5eb8b4a5d4395ce335bc7c3e6df2678fa47e2ddEric Laurent            status_t volStatus = checkAndSetVolume(stream, index, mOutputs.keyAt(i), curDevice);
1131c5eb8b4a5d4395ce335bc7c3e6df2678fa47e2ddEric Laurent            if (volStatus != NO_ERROR) {
1132c5eb8b4a5d4395ce335bc7c3e6df2678fa47e2ddEric Laurent                status = volStatus;
1133c5eb8b4a5d4395ce335bc7c3e6df2678fa47e2ddEric Laurent            }
1134f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1135f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1136f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return status;
1137f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1138f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1139c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentstatus_t AudioPolicyManagerBase::getStreamVolumeIndex(AudioSystem::stream_type stream,
1140c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                      int *index,
1141c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                      audio_devices_t device)
1142f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1143c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    if (index == NULL) {
1144f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
1145f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1146c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    if (!audio_is_output_device(device)) {
1147c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        return BAD_VALUE;
1148c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    }
1149c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    // if device is AUDIO_DEVICE_OUT_DEFAULT, return volume for device corresponding to
1150c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    // the strategy the stream belongs to.
1151c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    if (device == AUDIO_DEVICE_OUT_DEFAULT) {
1152c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
1153c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    }
1154c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    device = getDeviceForVolume(device);
1155c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
1156c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    *index =  mStreams[stream].getVolumeIndex(device);
1157c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index);
1158f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
1159f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1160f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
116145c763947b657b347211dc9388754e05d30d0467Eric Laurentaudio_io_handle_t AudioPolicyManagerBase::selectOutputForEffects(
116245c763947b657b347211dc9388754e05d30d0467Eric Laurent                                            const SortedVector<audio_io_handle_t>& outputs)
116345c763947b657b347211dc9388754e05d30d0467Eric Laurent{
116445c763947b657b347211dc9388754e05d30d0467Eric Laurent    // select one output among several suitable for global effects.
116545c763947b657b347211dc9388754e05d30d0467Eric Laurent    // The priority is as follows:
116645c763947b657b347211dc9388754e05d30d0467Eric Laurent    // 1: An offloaded output. If the effect ends up not being offloadable,
116745c763947b657b347211dc9388754e05d30d0467Eric Laurent    //    AudioFlinger will invalidate the track and the offloaded output
116845c763947b657b347211dc9388754e05d30d0467Eric Laurent    //    will be closed causing the effect to be moved to a PCM output.
116945c763947b657b347211dc9388754e05d30d0467Eric Laurent    // 2: A deep buffer output
117045c763947b657b347211dc9388754e05d30d0467Eric Laurent    // 3: the first output in the list
117145c763947b657b347211dc9388754e05d30d0467Eric Laurent
117245c763947b657b347211dc9388754e05d30d0467Eric Laurent    if (outputs.size() == 0) {
117345c763947b657b347211dc9388754e05d30d0467Eric Laurent        return 0;
117445c763947b657b347211dc9388754e05d30d0467Eric Laurent    }
117545c763947b657b347211dc9388754e05d30d0467Eric Laurent
117645c763947b657b347211dc9388754e05d30d0467Eric Laurent    audio_io_handle_t outputOffloaded = 0;
117745c763947b657b347211dc9388754e05d30d0467Eric Laurent    audio_io_handle_t outputDeepBuffer = 0;
117845c763947b657b347211dc9388754e05d30d0467Eric Laurent
117945c763947b657b347211dc9388754e05d30d0467Eric Laurent    for (size_t i = 0; i < outputs.size(); i++) {
118045c763947b657b347211dc9388754e05d30d0467Eric Laurent        AudioOutputDescriptor *desc = mOutputs.valueFor(outputs[i]);
1181c7b6e3c857c5f5af5f244d0b877c41a9c55f92abColin Cross        ALOGV("selectOutputForEffects outputs[%zu] flags %x", i, desc->mFlags);
118245c763947b657b347211dc9388754e05d30d0467Eric Laurent        if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
118345c763947b657b347211dc9388754e05d30d0467Eric Laurent            outputOffloaded = outputs[i];
118445c763947b657b347211dc9388754e05d30d0467Eric Laurent        }
118545c763947b657b347211dc9388754e05d30d0467Eric Laurent        if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) {
118645c763947b657b347211dc9388754e05d30d0467Eric Laurent            outputDeepBuffer = outputs[i];
118745c763947b657b347211dc9388754e05d30d0467Eric Laurent        }
118845c763947b657b347211dc9388754e05d30d0467Eric Laurent    }
118945c763947b657b347211dc9388754e05d30d0467Eric Laurent
119045c763947b657b347211dc9388754e05d30d0467Eric Laurent    ALOGV("selectOutputForEffects outputOffloaded %d outputDeepBuffer %d",
119145c763947b657b347211dc9388754e05d30d0467Eric Laurent          outputOffloaded, outputDeepBuffer);
119245c763947b657b347211dc9388754e05d30d0467Eric Laurent    if (outputOffloaded != 0) {
119345c763947b657b347211dc9388754e05d30d0467Eric Laurent        return outputOffloaded;
119445c763947b657b347211dc9388754e05d30d0467Eric Laurent    }
119545c763947b657b347211dc9388754e05d30d0467Eric Laurent    if (outputDeepBuffer != 0) {
119645c763947b657b347211dc9388754e05d30d0467Eric Laurent        return outputDeepBuffer;
119745c763947b657b347211dc9388754e05d30d0467Eric Laurent    }
119845c763947b657b347211dc9388754e05d30d0467Eric Laurent
119945c763947b657b347211dc9388754e05d30d0467Eric Laurent    return outputs[0];
120045c763947b657b347211dc9388754e05d30d0467Eric Laurent}
120145c763947b657b347211dc9388754e05d30d0467Eric Laurent
1202c94dccc97cc3ed5171b45f46a0f7f8762d37156fGlenn Kastenaudio_io_handle_t AudioPolicyManagerBase::getOutputForEffect(const effect_descriptor_t *desc)
1203f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1204f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // apply simple rule where global effects are attached to the same output as MUSIC streams
12054660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen
12064660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen    routing_strategy strategy = getStrategy(AudioSystem::MUSIC);
12074660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen    audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
1208c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs);
120945c763947b657b347211dc9388754e05d30d0467Eric Laurent
121045c763947b657b347211dc9388754e05d30d0467Eric Laurent    audio_io_handle_t output = selectOutputForEffects(dstOutputs);
121145c763947b657b347211dc9388754e05d30d0467Eric Laurent    ALOGV("getOutputForEffect() got output %d for fx %s flags %x",
121245c763947b657b347211dc9388754e05d30d0467Eric Laurent          output, (desc == NULL) ? "unspecified" : desc->name,  (desc == NULL) ? 0 : desc->flags);
121345c763947b657b347211dc9388754e05d30d0467Eric Laurent
121445c763947b657b347211dc9388754e05d30d0467Eric Laurent    return output;
1215f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1216f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1217c94dccc97cc3ed5171b45f46a0f7f8762d37156fGlenn Kastenstatus_t AudioPolicyManagerBase::registerEffect(const effect_descriptor_t *desc,
12181c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent                                audio_io_handle_t io,
1219f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                uint32_t strategy,
1220f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                int session,
1221f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                int id)
1222f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
12231c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent    ssize_t index = mOutputs.indexOfKey(io);
1224f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
12251c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent        index = mInputs.indexOfKey(io);
12261c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent        if (index < 0) {
122764cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("registerEffect() unknown io %d", io);
12281c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent            return INVALID_OPERATION;
12291c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent        }
1230f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1231f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1232f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mTotalEffectsMemory + desc->memoryUsage > getMaxEffectsMemory()) {
123364cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("registerEffect() memory limit exceeded for Fx %s, Memory %d KB",
1234f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                desc->name, desc->memoryUsage);
1235f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return INVALID_OPERATION;
1236f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1237f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mTotalEffectsMemory += desc->memoryUsage;
12386a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("registerEffect() effect %s, io %d, strategy %d session %d id %d",
12391c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent            desc->name, io, strategy, session, id);
12406a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("registerEffect() memory %d, total memory %d", desc->memoryUsage, mTotalEffectsMemory);
1241f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1242f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    EffectDescriptor *pDesc = new EffectDescriptor();
1243f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    memcpy (&pDesc->mDesc, desc, sizeof(effect_descriptor_t));
12441c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent    pDesc->mIo = io;
1245f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    pDesc->mStrategy = (routing_strategy)strategy;
1246f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    pDesc->mSession = session;
1247582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    pDesc->mEnabled = false;
1248582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent
1249f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mEffects.add(id, pDesc);
1250f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1251f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
1252f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1253f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1254f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::unregisterEffect(int id)
1255f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1256f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mEffects.indexOfKey(id);
1257f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
125864cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("unregisterEffect() unknown effect ID %d", id);
1259f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return INVALID_OPERATION;
1260f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1261f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1262f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    EffectDescriptor *pDesc = mEffects.valueAt(index);
1263f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1264582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    setEffectEnabled(pDesc, false);
1265582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent
1266f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mTotalEffectsMemory < pDesc->mDesc.memoryUsage) {
126764cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("unregisterEffect() memory %d too big for total %d",
1268f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                pDesc->mDesc.memoryUsage, mTotalEffectsMemory);
1269f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        pDesc->mDesc.memoryUsage = mTotalEffectsMemory;
1270f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1271f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mTotalEffectsMemory -= pDesc->mDesc.memoryUsage;
12726a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("unregisterEffect() effect %s, ID %d, memory %d total memory %d",
1273582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent            pDesc->mDesc.name, id, pDesc->mDesc.memoryUsage, mTotalEffectsMemory);
1274f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1275f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mEffects.removeItem(id);
1276f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    delete pDesc;
1277f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1278f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
1279f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1280f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1281582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurentstatus_t AudioPolicyManagerBase::setEffectEnabled(int id, bool enabled)
1282582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent{
1283582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    ssize_t index = mEffects.indexOfKey(id);
1284582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    if (index < 0) {
128564cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("unregisterEffect() unknown effect ID %d", id);
1286582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        return INVALID_OPERATION;
1287582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    }
1288582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent
1289582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    return setEffectEnabled(mEffects.valueAt(index), enabled);
1290582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent}
1291582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent
1292582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurentstatus_t AudioPolicyManagerBase::setEffectEnabled(EffectDescriptor *pDesc, bool enabled)
1293582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent{
1294582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    if (enabled == pDesc->mEnabled) {
12956a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("setEffectEnabled(%s) effect already %s",
1296582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent             enabled?"true":"false", enabled?"enabled":"disabled");
1297582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        return INVALID_OPERATION;
1298582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    }
1299582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent
1300582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    if (enabled) {
1301582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        if (mTotalEffectsCpuLoad + pDesc->mDesc.cpuLoad > getMaxEffectsCpuLoad()) {
130264cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("setEffectEnabled(true) CPU Load limit exceeded for Fx %s, CPU %f MIPS",
1303582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent                 pDesc->mDesc.name, (float)pDesc->mDesc.cpuLoad/10);
1304582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent            return INVALID_OPERATION;
1305582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        }
1306582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        mTotalEffectsCpuLoad += pDesc->mDesc.cpuLoad;
13076a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("setEffectEnabled(true) total CPU %d", mTotalEffectsCpuLoad);
1308582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    } else {
1309582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        if (mTotalEffectsCpuLoad < pDesc->mDesc.cpuLoad) {
131064cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("setEffectEnabled(false) CPU load %d too high for total %d",
1311582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent                    pDesc->mDesc.cpuLoad, mTotalEffectsCpuLoad);
1312582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent            pDesc->mDesc.cpuLoad = mTotalEffectsCpuLoad;
1313582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        }
1314582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        mTotalEffectsCpuLoad -= pDesc->mDesc.cpuLoad;
13156a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("setEffectEnabled(false) total CPU %d", mTotalEffectsCpuLoad);
1316582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    }
1317582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    pDesc->mEnabled = enabled;
1318582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    return NO_ERROR;
1319582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent}
1320582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent
1321000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurentbool AudioPolicyManagerBase::isNonOffloadableEffectEnabled()
1322000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent{
1323000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    for (size_t i = 0; i < mEffects.size(); i++) {
1324000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent        const EffectDescriptor * const pDesc = mEffects.valueAt(i);
1325000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent        if (pDesc->mEnabled && (pDesc->mStrategy == STRATEGY_MEDIA) &&
1326000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent                ((pDesc->mDesc.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) == 0)) {
1327000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent            ALOGV("isNonOffloadableEffectEnabled() non offloadable effect %s enabled on session %d",
1328000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent                  pDesc->mDesc.name, pDesc->mSession);
1329000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent            return true;
1330000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent        }
1331000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    }
1332000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    return false;
1333000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent}
1334000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent
1335f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinbool AudioPolicyManagerBase::isStreamActive(int stream, uint32_t inPastMs) const
1336f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1337f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    nsecs_t sysTime = systemTime();
1338f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mOutputs.size(); i++) {
133980f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent        const AudioOutputDescriptor *outputDesc = mOutputs.valueAt(i);
134080f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent        if (outputDesc->isStreamActive((AudioSystem::stream_type)stream, inPastMs, sysTime)) {
1341f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return true;
1342f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1343f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1344f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return false;
1345f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1346f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1347dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivibool AudioPolicyManagerBase::isStreamActiveRemotely(int stream, uint32_t inPastMs) const
1348dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi{
1349dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi    nsecs_t sysTime = systemTime();
1350dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi    for (size_t i = 0; i < mOutputs.size(); i++) {
1351dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi        const AudioOutputDescriptor *outputDesc = mOutputs.valueAt(i);
135280f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent        if (((outputDesc->device() & APM_AUDIO_OUT_DEVICE_REMOTE_ALL) != 0) &&
135380f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                outputDesc->isStreamActive((AudioSystem::stream_type)stream, inPastMs, sysTime)) {
1354dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi            return true;
1355dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi        }
1356dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi    }
1357dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi    return false;
1358dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi}
1359dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi
1360abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivibool AudioPolicyManagerBase::isSourceActive(audio_source_t source) const
1361abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi{
1362abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi    for (size_t i = 0; i < mInputs.size(); i++) {
1363abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi        const AudioInputDescriptor * inputDescriptor = mInputs.valueAt(i);
1364fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent        if ((inputDescriptor->mInputSource == (int)source ||
1365fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent                (source == (audio_source_t)AUDIO_SOURCE_VOICE_RECOGNITION &&
1366fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent                 inputDescriptor->mInputSource == AUDIO_SOURCE_HOTWORD))
1367fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent             && (inputDescriptor->mRefCount > 0)) {
1368abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi            return true;
1369abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi        }
1370abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi    }
1371abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi    return false;
1372abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi}
1373abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi
1374abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi
1375f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::dump(int fd)
1376f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1377f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    const size_t SIZE = 256;
1378f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    char buffer[SIZE];
1379f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    String8 result;
1380f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1381f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
1382f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1383b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
138470c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, " Primary Output: %d\n", mPrimaryOutput);
1385f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1386f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " A2DP device address: %s\n", mA2dpDeviceAddress.string());
1387f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1388f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " SCO device address: %s\n", mScoDeviceAddress.string());
1389f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1390fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    snprintf(buffer, SIZE, " USB audio ALSA %s\n", mUsbOutCardAndDevice.string());
1391599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    result.append(buffer);
1392f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Output devices: %08x\n", mAvailableOutputDevices);
1393f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1394f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Input devices: %08x\n", mAvailableInputDevices);
1395f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1396f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Phone state: %d\n", mPhoneState);
1397f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1398f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Force use for communications %d\n", mForceUse[AudioSystem::FOR_COMMUNICATION]);
1399f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1400f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Force use for media %d\n", mForceUse[AudioSystem::FOR_MEDIA]);
1401f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1402f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Force use for record %d\n", mForceUse[AudioSystem::FOR_RECORD]);
1403f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1404f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Force use for dock %d\n", mForceUse[AudioSystem::FOR_DOCK]);
1405f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1406738207def5f691d605ae33d041116829a74513a9Eric Laurent    snprintf(buffer, SIZE, " Force use for system %d\n", mForceUse[AudioSystem::FOR_SYSTEM]);
1407738207def5f691d605ae33d041116829a74513a9Eric Laurent    result.append(buffer);
1408f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, result.string(), result.size());
1409f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
14105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
141170c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "\nHW Modules dump:\n");
14125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    write(fd, buffer, strlen(buffer));
141370c236c9290732782d5267935af1475b8d5ae602Eric Laurent    for (size_t i = 0; i < mHwModules.size(); i++) {
1414c7b6e3c857c5f5af5f244d0b877c41a9c55f92abColin Cross        snprintf(buffer, SIZE, "- HW Module %zu:\n", i + 1);
14155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        write(fd, buffer, strlen(buffer));
141670c236c9290732782d5267935af1475b8d5ae602Eric Laurent        mHwModules[i]->dump(fd);
14175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
14185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
1419f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "\nOutputs dump:\n");
1420f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
1421f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mOutputs.size(); i++) {
1422f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        snprintf(buffer, SIZE, "- Output %d dump:\n", mOutputs.keyAt(i));
1423f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        write(fd, buffer, strlen(buffer));
1424f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mOutputs.valueAt(i)->dump(fd);
1425f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1426f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1427f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "\nInputs dump:\n");
1428f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
1429f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mInputs.size(); i++) {
1430f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        snprintf(buffer, SIZE, "- Input %d dump:\n", mInputs.keyAt(i));
1431f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        write(fd, buffer, strlen(buffer));
1432f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mInputs.valueAt(i)->dump(fd);
1433f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1434f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1435f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "\nStreams dump:\n");
1436f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
1437c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    snprintf(buffer, SIZE,
1438c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent             " Stream  Can be muted  Index Min  Index Max  Index Cur [device : index]...\n");
1439f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
1440f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) {
1441c7b6e3c857c5f5af5f244d0b877c41a9c55f92abColin Cross        snprintf(buffer, SIZE, " %02zu      ", i);
1442f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        write(fd, buffer, strlen(buffer));
1443c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        mStreams[i].dump(fd);
1444f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1445f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1446f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "\nTotal Effects CPU: %f MIPS, Total Effects memory: %d KB\n",
1447f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            (float)mTotalEffectsCpuLoad/10, mTotalEffectsMemory);
1448f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
1449f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1450f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "Registered effects:\n");
1451f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
1452f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mEffects.size(); i++) {
1453f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        snprintf(buffer, SIZE, "- Effect %d dump:\n", mEffects.keyAt(i));
1454f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        write(fd, buffer, strlen(buffer));
1455f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mEffects.valueAt(i)->dump(fd);
1456f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1457f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1458f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1459f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
1460f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1461f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1462a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald// This function checks for the parameters which can be offloaded.
1463a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald// This can be enhanced depending on the capability of the DSP and policy
1464a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald// of the system.
1465b4d07b97d23cfaffe22c7859ad7c45e168a7df0eRichard Fitzgeraldbool AudioPolicyManagerBase::isOffloadSupported(const audio_offload_info_t& offloadInfo)
1466b4d07b97d23cfaffe22c7859ad7c45e168a7df0eRichard Fitzgerald{
1467a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
1468c7b6e3c857c5f5af5f244d0b877c41a9c55f92abColin Cross     " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
1469a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald     offloadInfo.sample_rate, offloadInfo.channel_mask,
1470a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald     offloadInfo.format,
1471a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald     offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
1472a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald     offloadInfo.has_video);
1473a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald
1474a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    // Check if offload has been disabled
1475a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    char propValue[PROPERTY_VALUE_MAX];
1476a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    if (property_get("audio.offload.disable", propValue, "0")) {
1477a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        if (atoi(propValue) != 0) {
1478a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald            ALOGV("offload disabled by audio.offload.disable=%s", propValue );
1479a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald            return false;
1480a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        }
1481a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    }
1482a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald
1483a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    // Check if stream type is music, then only allow offload as of now.
1484a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
1485a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    {
148641b150b659d4ba33a9be9f99fd9f0ee1f2787541Eric Laurent        ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
148741b150b659d4ba33a9be9f99fd9f0ee1f2787541Eric Laurent        return false;
148841b150b659d4ba33a9be9f99fd9f0ee1f2787541Eric Laurent    }
148941b150b659d4ba33a9be9f99fd9f0ee1f2787541Eric Laurent
149041b150b659d4ba33a9be9f99fd9f0ee1f2787541Eric Laurent    //TODO: enable audio offloading with video when ready
149141b150b659d4ba33a9be9f99fd9f0ee1f2787541Eric Laurent    if (offloadInfo.has_video)
149241b150b659d4ba33a9be9f99fd9f0ee1f2787541Eric Laurent    {
149341b150b659d4ba33a9be9f99fd9f0ee1f2787541Eric Laurent        ALOGV("isOffloadSupported: has_video == true, returning false");
1494a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        return false;
1495a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    }
1496a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald
1497a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    //If duration is less than minimum value defined in property, return false
1498a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
1499a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
1500a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald            ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
1501a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald            return false;
1502a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        }
1503a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
1504a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
1505a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        return false;
1506a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    }
1507a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald
1508000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    // Do not allow offloading if one non offloadable effect is enabled. This prevents from
1509000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    // creating an offloaded track and tearing it down immediately after start when audioflinger
1510000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    // detects there is an active non offloadable effect.
1511000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    // FIXME: We should check the audio session here but we do not have it in this context.
1512000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    // This may prevent offloading in rare situations where effects are left active by apps
1513000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    // in the background.
1514000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    if (isNonOffloadableEffectEnabled()) {
1515000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent        return false;
1516000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    }
1517000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent
1518a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    // See if there is a profile to support this.
1519a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    // AUDIO_DEVICE_NONE
1520a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    IOProfile *profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
1521a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                                            offloadInfo.sample_rate,
1522a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                                            offloadInfo.format,
1523a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                                            offloadInfo.channel_mask,
1524a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                                            AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
1525a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    ALOGV("isOffloadSupported() profile %sfound", profile != NULL ? "" : "NOT ");
1526a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    return (profile != NULL);
1527b4d07b97d23cfaffe22c7859ad7c45e168a7df0eRichard Fitzgerald}
1528b4d07b97d23cfaffe22c7859ad7c45e168a7df0eRichard Fitzgerald
1529f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// ----------------------------------------------------------------------------
1530f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// AudioPolicyManagerBase
1531f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// ----------------------------------------------------------------------------
1532f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1533f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima ZavinAudioPolicyManagerBase::AudioPolicyManagerBase(AudioPolicyClientInterface *clientInterface)
1534f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    :
1535f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
1536f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    Thread(false),
1537f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
153870c236c9290732782d5267935af1475b8d5ae602Eric Laurent    mPrimaryOutput((audio_io_handle_t)0),
1539ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    mAvailableOutputDevices(AUDIO_DEVICE_NONE),
1540ca0657a1ca087a6d474a75fcfedd6aac3901d587Glenn Kasten    mPhoneState(AudioSystem::MODE_NORMAL),
1541f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
1542f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mTotalEffectsCpuLoad(0), mTotalEffectsMemory(0),
154318fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi    mA2dpSuspended(false), mHasA2dp(false), mHasUsb(false), mHasRemoteSubmix(false),
154418fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi    mSpeakerDrcEnabled(false)
1545f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1546f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mpClientInterface = clientInterface;
1547f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1548f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < AudioSystem::NUM_FORCE_USE; i++) {
1549f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mForceUse[i] = AudioSystem::FORCE_NONE;
1550f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1551f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1552f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mA2dpDeviceAddress = String8("");
1553f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mScoDeviceAddress = String8("");
1554fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    mUsbOutCardAndDevice = String8("");
1555f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
15565ec145df7708564d385fd3fb764085321cf4c253Dima Zavin    if (loadAudioPolicyConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE) != NO_ERROR) {
15575ec145df7708564d385fd3fb764085321cf4c253Dima Zavin        if (loadAudioPolicyConfig(AUDIO_POLICY_CONFIG_FILE) != NO_ERROR) {
1558739022f26a7127ba76a98dda65411496086114a7Dima Zavin            ALOGE("could not load audio policy configuration file, setting defaults");
1559739022f26a7127ba76a98dda65411496086114a7Dima Zavin            defaultAudioPolicyConfig();
15605ec145df7708564d385fd3fb764085321cf4c253Dima Zavin        }
15615ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
15625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
156318fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi    // must be done after reading the policy
156418fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi    initializeVolumeCurves();
156518fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi
1566b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    // open all output streams needed to access attached devices
156770c236c9290732782d5267935af1475b8d5ae602Eric Laurent    for (size_t i = 0; i < mHwModules.size(); i++) {
156870c236c9290732782d5267935af1475b8d5ae602Eric Laurent        mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->mName);
156970c236c9290732782d5267935af1475b8d5ae602Eric Laurent        if (mHwModules[i]->mHandle == 0) {
157070c236c9290732782d5267935af1475b8d5ae602Eric Laurent            ALOGW("could not open HW module %s", mHwModules[i]->mName);
157170c236c9290732782d5267935af1475b8d5ae602Eric Laurent            continue;
157270c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
157370c236c9290732782d5267935af1475b8d5ae602Eric Laurent        // open all output streams needed to access attached devices
1574a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        // except for direct output streams that are only opened when they are actually
1575a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        // required by an app.
157670c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
157770c236c9290732782d5267935af1475b8d5ae602Eric Laurent        {
157870c236c9290732782d5267935af1475b8d5ae602Eric Laurent            const IOProfile *outProfile = mHwModules[i]->mOutputProfiles[j];
157970c236c9290732782d5267935af1475b8d5ae602Eric Laurent
1580a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald            if ((outProfile->mSupportedDevices & mAttachedOutputDevices) &&
1581a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                    ((outProfile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0)) {
158270c236c9290732782d5267935af1475b8d5ae602Eric Laurent                AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(outProfile);
158370c236c9290732782d5267935af1475b8d5ae602Eric Laurent                outputDesc->mDevice = (audio_devices_t)(mDefaultOutputDevice &
158470c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                            outProfile->mSupportedDevices);
158570c236c9290732782d5267935af1475b8d5ae602Eric Laurent                audio_io_handle_t output = mpClientInterface->openOutput(
158670c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                outProfile->mModule->mHandle,
158770c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mDevice,
158870c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mSamplingRate,
158970c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mFormat,
159070c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mChannelMask,
159170c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mLatency,
159270c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                outputDesc->mFlags);
159370c236c9290732782d5267935af1475b8d5ae602Eric Laurent                if (output == 0) {
159470c236c9290732782d5267935af1475b8d5ae602Eric Laurent                    delete outputDesc;
159570c236c9290732782d5267935af1475b8d5ae602Eric Laurent                } else {
159670c236c9290732782d5267935af1475b8d5ae602Eric Laurent                    mAvailableOutputDevices = (audio_devices_t)(mAvailableOutputDevices |
159770c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                            (outProfile->mSupportedDevices & mAttachedOutputDevices));
159870c236c9290732782d5267935af1475b8d5ae602Eric Laurent                    if (mPrimaryOutput == 0 &&
15990977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent                            outProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
160070c236c9290732782d5267935af1475b8d5ae602Eric Laurent                        mPrimaryOutput = output;
160170c236c9290732782d5267935af1475b8d5ae602Eric Laurent                    }
160270c236c9290732782d5267935af1475b8d5ae602Eric Laurent                    addOutput(output, outputDesc);
160370c236c9290732782d5267935af1475b8d5ae602Eric Laurent                    setOutputDevice(output,
160470c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                    (audio_devices_t)(mDefaultOutputDevice &
160570c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                        outProfile->mSupportedDevices),
160670c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                    true);
1607b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                }
1608b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            }
1609b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        }
1610f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1611f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
16125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGE_IF((mAttachedOutputDevices & ~mAvailableOutputDevices),
1613b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent             "Not output found for attached devices %08x",
16145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent             (mAttachedOutputDevices & ~mAvailableOutputDevices));
1615b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1616b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output");
1617b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1618c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    updateDevicesAndOutputs();
16193cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
1620f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
1621b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (mPrimaryOutput != 0) {
1622f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioParameter outputCmd = AudioParameter();
1623f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputCmd.addInt(String8("set_id"), 0);
1624b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        mpClientInterface->setParameters(mPrimaryOutput, outputCmd.toString());
1625f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1626c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        mTestDevice = AUDIO_DEVICE_OUT_SPEAKER;
1627f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mTestSamplingRate = 44100;
1628f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mTestFormat = AudioSystem::PCM_16_BIT;
1629f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mTestChannels =  AudioSystem::CHANNEL_OUT_STEREO;
1630f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mTestLatencyMs = 0;
1631f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mCurOutput = 0;
1632f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mDirectOutput = false;
1633f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
1634f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mTestOutputs[i] = 0;
1635f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1636f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1637f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        const size_t SIZE = 256;
1638f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        char buffer[SIZE];
1639f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        snprintf(buffer, SIZE, "AudioPolicyManagerTest");
1640f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        run(buffer, ANDROID_PRIORITY_AUDIO);
1641f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1642f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
1643f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1644f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1645f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima ZavinAudioPolicyManagerBase::~AudioPolicyManagerBase()
1646f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1647f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
1648f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    exit();
1649f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
1650f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin   for (size_t i = 0; i < mOutputs.size(); i++) {
1651f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mpClientInterface->closeOutput(mOutputs.keyAt(i));
1652f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        delete mOutputs.valueAt(i);
1653f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin   }
1654f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin   for (size_t i = 0; i < mInputs.size(); i++) {
1655f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mpClientInterface->closeInput(mInputs.keyAt(i));
1656f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        delete mInputs.valueAt(i);
1657f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin   }
165870c236c9290732782d5267935af1475b8d5ae602Eric Laurent   for (size_t i = 0; i < mHwModules.size(); i++) {
165970c236c9290732782d5267935af1475b8d5ae602Eric Laurent        delete mHwModules[i];
16605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent   }
1661f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1662f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1663f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::initCheck()
1664f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1665b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    return (mPrimaryOutput == 0) ? NO_INIT : NO_ERROR;
1666f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1667f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1668f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
1669f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinbool AudioPolicyManagerBase::threadLoop()
1670f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
16716a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("entering threadLoop()");
1672f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    while (!exitPending())
1673f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    {
1674f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        String8 command;
1675f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        int valueInt;
1676f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        String8 value;
1677f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1678f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        Mutex::Autolock _l(mLock);
1679f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mWaitWorkCV.waitRelative(mLock, milliseconds(50));
1680f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1681f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        command = mpClientInterface->getParameters(0, String8("test_cmd_policy"));
1682f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioParameter param = AudioParameter(command);
1683f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1684f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR &&
1685f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            valueInt != 0) {
16866a70518b93928d1c91457ff805e375c82d76b0e5Steve Block            ALOGV("Test command %s received", command.string());
1687f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            String8 target;
1688f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.get(String8("target"), target) != NO_ERROR) {
1689f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                target = "Manager";
1690f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1691f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) {
1692f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_output"));
1693f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mCurOutput = valueInt;
1694f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1695f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) {
1696f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_direct"));
1697f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (value == "false") {
1698f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    mDirectOutput = false;
1699f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else if (value == "true") {
1700f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    mDirectOutput = true;
1701f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1702f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1703f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) {
1704f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_input"));
1705f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mTestInput = valueInt;
1706f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1707f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1708f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) {
1709f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_format"));
1710f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                int format = AudioSystem::INVALID_FORMAT;
1711f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (value == "PCM 16 bits") {
1712f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    format = AudioSystem::PCM_16_BIT;
1713f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else if (value == "PCM 8 bits") {
1714f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    format = AudioSystem::PCM_8_BIT;
1715f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else if (value == "Compressed MP3") {
1716f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    format = AudioSystem::MP3;
1717f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1718f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (format != AudioSystem::INVALID_FORMAT) {
1719f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    if (target == "Manager") {
1720f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mTestFormat = format;
1721f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    } else if (mTestOutputs[mCurOutput] != 0) {
1722f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        AudioParameter outputParam = AudioParameter();
1723f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        outputParam.addInt(String8("format"), format);
1724f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
1725f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    }
1726f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1727f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1728f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) {
1729f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_channels"));
1730f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                int channels = 0;
1731f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1732f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (value == "Channels Stereo") {
1733f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    channels =  AudioSystem::CHANNEL_OUT_STEREO;
1734f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else if (value == "Channels Mono") {
1735f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    channels =  AudioSystem::CHANNEL_OUT_MONO;
1736f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1737f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (channels != 0) {
1738f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    if (target == "Manager") {
1739f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mTestChannels = channels;
1740f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    } else if (mTestOutputs[mCurOutput] != 0) {
1741f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        AudioParameter outputParam = AudioParameter();
1742f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        outputParam.addInt(String8("channels"), channels);
1743f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
1744f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    }
1745f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1746f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1747f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) {
1748f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_sampleRate"));
1749f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (valueInt >= 0 && valueInt <= 96000) {
1750f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    int samplingRate = valueInt;
1751f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    if (target == "Manager") {
1752f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mTestSamplingRate = samplingRate;
1753f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    } else if (mTestOutputs[mCurOutput] != 0) {
1754f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        AudioParameter outputParam = AudioParameter();
1755f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        outputParam.addInt(String8("sampling_rate"), samplingRate);
1756f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
1757f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    }
1758f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1759f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1760f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1761f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) {
1762f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_reopen"));
1763f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
176470c236c9290732782d5267935af1475b8d5ae602Eric Laurent                AudioOutputDescriptor *outputDesc = mOutputs.valueFor(mPrimaryOutput);
1765b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                mpClientInterface->closeOutput(mPrimaryOutput);
176670c236c9290732782d5267935af1475b8d5ae602Eric Laurent
176770c236c9290732782d5267935af1475b8d5ae602Eric Laurent                audio_module_handle_t moduleHandle = outputDesc->mModule->mHandle;
176870c236c9290732782d5267935af1475b8d5ae602Eric Laurent
1769b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                delete mOutputs.valueFor(mPrimaryOutput);
1770b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                mOutputs.removeItem(mPrimaryOutput);
1771f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1772b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(NULL);
1773c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER;
177470c236c9290732782d5267935af1475b8d5ae602Eric Laurent                mPrimaryOutput = mpClientInterface->openOutput(moduleHandle,
177570c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mDevice,
1776f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                &outputDesc->mSamplingRate,
1777f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                &outputDesc->mFormat,
177870c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mChannelMask,
1779f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                &outputDesc->mLatency,
1780f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                outputDesc->mFlags);
1781b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                if (mPrimaryOutput == 0) {
17825efbd421e0029d6fc44b1cc65c0e5e0d85e5161fSteve Block                    ALOGE("Failed to reopen hardware output stream, samplingRate: %d, format %d, channels %d",
178370c236c9290732782d5267935af1475b8d5ae602Eric Laurent                            outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask);
1784f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else {
1785f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    AudioParameter outputCmd = AudioParameter();
1786f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    outputCmd.addInt(String8("set_id"), 0);
1787b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                    mpClientInterface->setParameters(mPrimaryOutput, outputCmd.toString());
1788b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                    addOutput(mPrimaryOutput, outputDesc);
1789f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1790f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1791f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1792f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1793f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mpClientInterface->setParameters(0, String8("test_cmd_policy="));
1794f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1795f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1796f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return false;
1797f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1798f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1799f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::exit()
1800f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1801f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    {
1802f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AutoMutex _l(mLock);
1803f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        requestExit();
1804f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mWaitWorkCV.signal();
1805f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1806f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    requestExitAndWait();
1807f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1808f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1809f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinint AudioPolicyManagerBase::testOutputIndex(audio_io_handle_t output)
1810f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1811f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
1812f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (output == mTestOutputs[i]) return i;
1813f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1814f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return 0;
1815f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1816f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
1817f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1818f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// ---
1819f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1820f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::addOutput(audio_io_handle_t id, AudioOutputDescriptor *outputDesc)
1821f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1822f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    outputDesc->mId = id;
1823f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mOutputs.add(id, outputDesc);
1824f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1825f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1826fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLeanvoid AudioPolicyManagerBase::addInput(audio_io_handle_t id, AudioInputDescriptor *inputDesc)
1827fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean{
1828fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    inputDesc->mId = id;
1829fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    mInputs.add(id, inputDesc);
1830fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean}
1831f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
18323cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurentstatus_t AudioPolicyManagerBase::checkOutputsForDevice(audio_devices_t device,
18333cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                       AudioSystem::device_connection_state state,
18349f4165f36d90dddc32246d0eddbf289383c3b2bbEric Laurent                                                       SortedVector<audio_io_handle_t>& outputs,
18359f4165f36d90dddc32246d0eddbf289383c3b2bbEric Laurent                                                       const String8 paramStr)
1836f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
18373cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    AudioOutputDescriptor *desc;
1838b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1839b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (state == AudioSystem::DEVICE_STATE_AVAILABLE) {
18403cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        // first list already open outputs that can be routed to this device
1841b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        for (size_t i = 0; i < mOutputs.size(); i++) {
18423cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            desc = mOutputs.valueAt(i);
18433cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            if (!desc->isDuplicated() && (desc->mProfile->mSupportedDevices & device)) {
18443cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i));
18453cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                outputs.add(mOutputs.keyAt(i));
1846b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            }
1847b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        }
18483cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        // then look for output profiles that can be routed to this device
18493cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        SortedVector<IOProfile *> profiles;
185070c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (size_t i = 0; i < mHwModules.size(); i++)
1851b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        {
185270c236c9290732782d5267935af1475b8d5ae602Eric Laurent            if (mHwModules[i]->mHandle == 0) {
185370c236c9290732782d5267935af1475b8d5ae602Eric Laurent                continue;
185470c236c9290732782d5267935af1475b8d5ae602Eric Laurent            }
185570c236c9290732782d5267935af1475b8d5ae602Eric Laurent            for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
185670c236c9290732782d5267935af1475b8d5ae602Eric Laurent            {
185770c236c9290732782d5267935af1475b8d5ae602Eric Laurent                if (mHwModules[i]->mOutputProfiles[j]->mSupportedDevices & device) {
1858c7b6e3c857c5f5af5f244d0b877c41a9c55f92abColin Cross                    ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i);
18593cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    profiles.add(mHwModules[i]->mOutputProfiles[j]);
186070c236c9290732782d5267935af1475b8d5ae602Eric Laurent                }
186170c236c9290732782d5267935af1475b8d5ae602Eric Laurent            }
1862b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        }
186370c236c9290732782d5267935af1475b8d5ae602Eric Laurent
18643cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        if (profiles.isEmpty() && outputs.isEmpty()) {
18653cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
18663cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            return BAD_VALUE;
186770c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
186870c236c9290732782d5267935af1475b8d5ae602Eric Laurent
18693cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        // open outputs for matching profiles if needed. Direct outputs are also opened to
18703cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        // query for dynamic parameters and will be closed later by setDeviceConnectionState()
18713cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
18723cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            IOProfile *profile = profiles[profile_index];
1873b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
18743cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            // nothing to do if one output is already opened for this profile
18753cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            size_t j;
1876fd8cecbee6843b444d56a1db40af76027e2b19f1Eric Laurent            for (j = 0; j < mOutputs.size(); j++) {
18773cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                desc = mOutputs.valueAt(j);
18783cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                if (!desc->isDuplicated() && desc->mProfile == profile) {
18793cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    break;
18803cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                }
18813cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
1882fd8cecbee6843b444d56a1db40af76027e2b19f1Eric Laurent            if (j != mOutputs.size()) {
18833cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                continue;
18843cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
18853cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
18869f4165f36d90dddc32246d0eddbf289383c3b2bbEric Laurent            ALOGV("opening output for device %08x with params %s", device, paramStr.string());
18873cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            desc = new AudioOutputDescriptor(profile);
18883cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            desc->mDevice = device;
1889727fef1b1a8cb6d6609ec99a749fcd9573a3417dEric Laurent            audio_offload_info_t offloadInfo = AUDIO_INFO_INITIALIZER;
1890727fef1b1a8cb6d6609ec99a749fcd9573a3417dEric Laurent            offloadInfo.sample_rate = desc->mSamplingRate;
1891727fef1b1a8cb6d6609ec99a749fcd9573a3417dEric Laurent            offloadInfo.format = desc->mFormat;
1892727fef1b1a8cb6d6609ec99a749fcd9573a3417dEric Laurent            offloadInfo.channel_mask = desc->mChannelMask;
1893727fef1b1a8cb6d6609ec99a749fcd9573a3417dEric Laurent
18943cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            audio_io_handle_t output = mpClientInterface->openOutput(profile->mModule->mHandle,
18953cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                       &desc->mDevice,
18963cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                       &desc->mSamplingRate,
18973cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                       &desc->mFormat,
18983cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                       &desc->mChannelMask,
18993cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                       &desc->mLatency,
1900727fef1b1a8cb6d6609ec99a749fcd9573a3417dEric Laurent                                                                       desc->mFlags,
1901727fef1b1a8cb6d6609ec99a749fcd9573a3417dEric Laurent                                                                       &offloadInfo);
19023cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            if (output != 0) {
19039f4165f36d90dddc32246d0eddbf289383c3b2bbEric Laurent                if (!paramStr.isEmpty()) {
1904fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    // Here is where the out_set_parameters() for card & device gets called
19059f4165f36d90dddc32246d0eddbf289383c3b2bbEric Laurent                    mpClientInterface->setParameters(output, paramStr);
19069f4165f36d90dddc32246d0eddbf289383c3b2bbEric Laurent                }
19079f4165f36d90dddc32246d0eddbf289383c3b2bbEric Laurent
1908fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                // Here is where we step through and resolve any "dynamic" fields
1909c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                String8 reply;
1910c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                char *value;
1911c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                if (profile->mSamplingRates[0] == 0) {
1912c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                    reply = mpClientInterface->getParameters(output,
1913c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                                            String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES));
1914c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                    ALOGV("checkOutputsForDevice() direct output sup sampling rates %s",
1915c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                              reply.string());
1916c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                    value = strpbrk((char *)reply.string(), "=");
1917c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                    if (value != NULL) {
1918c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                        loadSamplingRates(value + 1, profile);
19193cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
1920c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                }
1921c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
1922c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                    reply = mpClientInterface->getParameters(output,
1923c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                                                   String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
1924c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                    ALOGV("checkOutputsForDevice() direct output sup formats %s",
1925c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                              reply.string());
1926c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                    value = strpbrk((char *)reply.string(), "=");
1927c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                    if (value != NULL) {
1928c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                        loadFormats(value + 1, profile);
19293cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
1930c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                }
1931c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                if (profile->mChannelMasks[0] == 0) {
1932c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                    reply = mpClientInterface->getParameters(output,
1933c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                                                  String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS));
1934c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                    ALOGV("checkOutputsForDevice() direct output sup channel masks %s",
1935c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                              reply.string());
1936c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                    value = strpbrk((char *)reply.string(), "=");
1937c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                    if (value != NULL) {
1938c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                        loadOutChannels(value + 1, profile);
19393cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
1940c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                }
1941c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                if (((profile->mSamplingRates[0] == 0) &&
1942c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                         (profile->mSamplingRates.size() < 2)) ||
1943c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                     ((profile->mFormats[0] == 0) &&
1944c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                         (profile->mFormats.size() < 2)) ||
1945c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                     ((profile->mChannelMasks[0] == 0) &&
1946c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                         (profile->mChannelMasks.size() < 2))) {
1947c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                    ALOGW("checkOutputsForDevice() direct output missing param");
1948c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                    mpClientInterface->closeOutput(output);
1949c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                    output = 0;
1950c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                } else if (profile->mSamplingRates[0] == 0) {
1951c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                    mpClientInterface->closeOutput(output);
1952c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                    desc->mSamplingRate = profile->mSamplingRates[1];
1953c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                    offloadInfo.sample_rate = desc->mSamplingRate;
1954c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                    output = mpClientInterface->openOutput(
1955c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                                                    profile->mModule->mHandle,
1956c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                                                    &desc->mDevice,
1957c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                                                    &desc->mSamplingRate,
1958c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                                                    &desc->mFormat,
1959c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                                                    &desc->mChannelMask,
1960c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                                                    &desc->mLatency,
1961c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                                                    desc->mFlags,
1962c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                                                    &offloadInfo);
1963c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                }
1964c26f454f8d841657542916cdd140a9896a89ad20Paul McLean
1965c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                if (output != 0) {
19663cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    addOutput(output, desc);
1967c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                    if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) {
1968c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                        audio_io_handle_t duplicatedOutput = 0;
1969c26f454f8d841657542916cdd140a9896a89ad20Paul McLean
1970c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                        // set initial stream volume for device
1971c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                        applyStreamVolumes(output, device, 0, true);
1972c26f454f8d841657542916cdd140a9896a89ad20Paul McLean
1973c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                        //TODO: configure audio effect output stage here
1974c26f454f8d841657542916cdd140a9896a89ad20Paul McLean
1975c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                        // open a duplicating output thread for the new output and the primary output
1976c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                        duplicatedOutput = mpClientInterface->openDuplicateOutput(output,
1977c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                                                                                  mPrimaryOutput);
1978c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                        if (duplicatedOutput != 0) {
1979c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                            // add duplicated output descriptor
1980c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                            AudioOutputDescriptor *dupOutputDesc = new AudioOutputDescriptor(NULL);
1981c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                            dupOutputDesc->mOutput1 = mOutputs.valueFor(mPrimaryOutput);
1982c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                            dupOutputDesc->mOutput2 = mOutputs.valueFor(output);
1983c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                            dupOutputDesc->mSamplingRate = desc->mSamplingRate;
1984c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                            dupOutputDesc->mFormat = desc->mFormat;
1985c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                            dupOutputDesc->mChannelMask = desc->mChannelMask;
1986c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                            dupOutputDesc->mLatency = desc->mLatency;
1987c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                            addOutput(duplicatedOutput, dupOutputDesc);
1988c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                            applyStreamVolumes(duplicatedOutput, device, 0, true);
1989c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                        } else {
1990c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                            ALOGW("checkOutputsForDevice() could not open dup output for %d and %d",
1991c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                                    mPrimaryOutput, output);
1992c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                            mpClientInterface->closeOutput(output);
1993c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                            mOutputs.removeItem(output);
1994c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                            output = 0;
1995c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                        }
19963cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
19973cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                }
19983cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
19993cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            if (output == 0) {
20003cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                ALOGW("checkOutputsForDevice() could not open output for device %x", device);
20013cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                delete desc;
20023cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                profiles.removeAt(profile_index);
20033cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                profile_index--;
2004b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            } else {
20053cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                outputs.add(output);
20063cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                ALOGV("checkOutputsForDevice(): adding output %d", output);
2007f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
20083cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        }
20093cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
20103cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        if (profiles.isEmpty()) {
20113cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
20123cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            return BAD_VALUE;
2013f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2014fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    } else { // Disconnect
2015b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        // check if one opened output is not needed any more after disconnecting one device
2016b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        for (size_t i = 0; i < mOutputs.size(); i++) {
20173cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            desc = mOutputs.valueAt(i);
20183cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            if (!desc->isDuplicated() &&
20193cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    !(desc->mProfile->mSupportedDevices & mAvailableOutputDevices)) {
20203cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                ALOGV("checkOutputsForDevice(): disconnecting adding output %d", mOutputs.keyAt(i));
20213cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                outputs.add(mOutputs.keyAt(i));
20223cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
20233cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        }
2024fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        // Clear any profiles associated with the disconnected device.
20253cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        for (size_t i = 0; i < mHwModules.size(); i++)
20263cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        {
20273cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            if (mHwModules[i]->mHandle == 0) {
20283cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                continue;
20293cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
20303cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
20313cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            {
20323cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                IOProfile *profile = mHwModules[i]->mOutputProfiles[j];
2033c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                if (profile->mSupportedDevices & device) {
2034c7b6e3c857c5f5af5f244d0b877c41a9c55f92abColin Cross                    ALOGV("checkOutputsForDevice(): clearing direct output profile %zu on module %zu",
20353cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                          j, i);
20363cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    if (profile->mSamplingRates[0] == 0) {
20373cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        profile->mSamplingRates.clear();
20383cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        profile->mSamplingRates.add(0);
20393cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
20402d749b0bda4ae60ef1a940eeba86121b2e94d07dGlenn Kasten                    if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
20413cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        profile->mFormats.clear();
20422d749b0bda4ae60ef1a940eeba86121b2e94d07dGlenn Kasten                        profile->mFormats.add(AUDIO_FORMAT_DEFAULT);
20433cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
20443cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    if (profile->mChannelMasks[0] == 0) {
20453cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        profile->mChannelMasks.clear();
20462c3d2379ee6eadb32b63bf6de538cfd6487e8e20Glenn Kasten                        profile->mChannelMasks.add(0);
20473cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
20483cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                }
2049b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            }
2050f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2051f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
20523cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    return NO_ERROR;
2053f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2054f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2055fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLeanstatus_t AudioPolicyManagerBase::checkInputsForDevice(audio_devices_t device,
2056fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                                                      AudioSystem::device_connection_state state,
2057fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                                                      SortedVector<audio_io_handle_t>& inputs,
2058fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                                                      const String8 paramStr)
2059fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean{
2060fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    AudioInputDescriptor *desc;
2061fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    if (state == AudioSystem::DEVICE_STATE_AVAILABLE) {
2062fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        // first list already open inputs that can be routed to this device
2063fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
2064fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            desc = mInputs.valueAt(input_index);
2065fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            if (desc->mProfile->mSupportedDevices & (device & ~AUDIO_DEVICE_BIT_IN)) {
2066fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index));
2067fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean               inputs.add(mInputs.keyAt(input_index));
2068fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            }
2069fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        }
2070fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
2071fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        // then look for input profiles that can be routed to this device
2072fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        SortedVector<IOProfile *> profiles;
2073fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        for (size_t module_index = 0; module_index < mHwModules.size(); module_index++)
2074fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        {
2075fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            if (mHwModules[module_index]->mHandle == 0) {
2076fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                continue;
2077fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            }
2078fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            for (size_t profile_index = 0;
2079fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                 profile_index < mHwModules[module_index]->mInputProfiles.size();
2080fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                 profile_index++)
2081fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            {
2082fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                if (mHwModules[module_index]->mInputProfiles[profile_index]->mSupportedDevices
2083fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                        & (device & ~AUDIO_DEVICE_BIT_IN)) {
2084fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    ALOGV("checkInputsForDevice(): adding profile %d from module %d",
2085fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                          profile_index, module_index);
2086fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    profiles.add(mHwModules[module_index]->mInputProfiles[profile_index]);
2087fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                }
2088fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            }
2089fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        }
2090fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
2091fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        if (profiles.isEmpty() && inputs.isEmpty()) {
2092fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
2093fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            return BAD_VALUE;
2094fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        }
2095fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
2096fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        // open inputs for matching profiles if needed. Direct inputs are also opened to
2097fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        // query for dynamic parameters and will be closed later by setDeviceConnectionState()
2098fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
2099fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
2100fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            IOProfile *profile = profiles[profile_index];
2101fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            // nothing to do if one input is already opened for this profile
2102fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            size_t input_index;
2103fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            for (input_index = 0; input_index < mInputs.size(); input_index++) {
2104fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                desc = mInputs.valueAt(input_index);
2105fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                if (desc->mProfile == profile) {
2106fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    break;
2107fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                }
2108fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            }
2109fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            if (input_index != mInputs.size()) {
2110fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                continue;
2111fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            }
2112fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
2113fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            ALOGV("opening input for device 0x%X with params %s", device, paramStr.string());
2114fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            desc = new AudioInputDescriptor(profile);
2115fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            desc->mDevice = device;
2116fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
2117fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            audio_io_handle_t input = mpClientInterface->openInput(profile->mModule->mHandle,
2118fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                                            &desc->mDevice,
2119fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                                            &desc->mSamplingRate,
2120fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                                            &desc->mFormat,
2121fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                                            &desc->mChannelMask);
2122fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
2123fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            if (input != 0) {
2124fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                if (!paramStr.isEmpty()) {
2125fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    mpClientInterface->setParameters(input, paramStr);
2126fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                }
2127fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
2128fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                // Here is where we step through and resolve any "dynamic" fields
2129fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                String8 reply;
2130fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                char *value;
2131fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                if (profile->mSamplingRates[0] == 0) {
2132fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    reply = mpClientInterface->getParameters(input,
2133fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                                            String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES));
2134fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    ALOGV("checkInputsForDevice() direct input sup sampling rates %s",
2135fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                              reply.string());
2136fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    value = strpbrk((char *)reply.string(), "=");
2137fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    if (value != NULL) {
2138fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                        loadSamplingRates(value + 1, profile);
2139fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    }
2140fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                }
2141fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
2142fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    reply = mpClientInterface->getParameters(input,
2143fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                                                   String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
2144fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    ALOGV("checkInputsForDevice() direct input sup formats %s", reply.string());
2145fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    value = strpbrk((char *)reply.string(), "=");
2146fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    if (value != NULL) {
2147fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                        loadFormats(value + 1, profile);
2148fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    }
2149fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                }
2150fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                if (profile->mChannelMasks[0] == 0) {
2151fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    reply = mpClientInterface->getParameters(input,
2152fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                                                  String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS));
2153fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    ALOGV("checkInputsForDevice() direct input sup channel masks %s",
2154fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                              reply.string());
2155fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    value = strpbrk((char *)reply.string(), "=");
2156fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    if (value != NULL) {
2157fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                        loadInChannels(value + 1, profile);
2158fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    }
2159fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                }
2160fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                if (((profile->mSamplingRates[0] == 0) && (profile->mSamplingRates.size() < 2)) ||
2161fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                     ((profile->mFormats[0] == 0) && (profile->mFormats.size() < 2)) ||
2162fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                     ((profile->mChannelMasks[0] == 0) && (profile->mChannelMasks.size() < 2))) {
2163fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    ALOGW("checkInputsForDevice() direct input missing param");
2164fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    mpClientInterface->closeInput(input);
2165fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    input = 0;
2166fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                }
2167fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
2168fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                if (input != 0) {
2169fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    addInput(input, desc);
2170fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                }
2171fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            } // endif input != 0
2172fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
2173fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            if (input == 0) {
2174fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                ALOGW("checkInputsForDevice() could not open input for device 0x%X", device);
2175fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                delete desc;
2176fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                profiles.removeAt(profile_index);
2177fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                profile_index--;
2178fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            } else {
2179fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                inputs.add(input);
2180fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                ALOGV("checkInputsForDevice(): adding input %d", input);
2181fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            }
2182fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        } // end scan profiles
2183fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
2184fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        if (profiles.isEmpty()) {
2185fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
2186fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            return BAD_VALUE;
2187fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        }
2188fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    } else {
2189fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        // Disconnect
2190fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        // check if one opened input is not needed any more after disconnecting one device
2191fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
2192fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            desc = mInputs.valueAt(input_index);
2193fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            if (!(desc->mProfile->mSupportedDevices & mAvailableInputDevices)) {
2194fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                ALOGV("checkInputsForDevice(): disconnecting adding input %d",
2195fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                      mInputs.keyAt(input_index));
2196fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                inputs.add(mInputs.keyAt(input_index));
2197fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            }
2198fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        }
2199fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        // Clear any profiles associated with the disconnected device.
2200fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        for (size_t module_index = 0; module_index < mHwModules.size(); module_index++)
2201fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        {
2202fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            if (mHwModules[module_index]->mHandle == 0) {
2203fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                continue;
2204fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            }
2205fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            for (size_t profile_index = 0;
2206fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                 profile_index < mHwModules[module_index]->mInputProfiles.size();
2207fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                 profile_index++)
2208fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            {
2209fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                IOProfile *profile = mHwModules[module_index]->mInputProfiles[profile_index];
2210fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                if (profile->mSupportedDevices & device) {
2211fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    ALOGV("checkInputsForDevice(): clearing direct input profile %d on module %d",
2212fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                          profile_index, module_index);
2213fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    if (profile->mSamplingRates[0] == 0) {
2214fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                        profile->mSamplingRates.clear();
2215fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                        profile->mSamplingRates.add(0);
2216fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    }
2217fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
2218fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                        profile->mFormats.clear();
2219fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                        profile->mFormats.add(AUDIO_FORMAT_DEFAULT);
2220fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    }
2221fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    if (profile->mChannelMasks[0] == 0) {
2222fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                        profile->mChannelMasks.clear();
2223fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                        profile->mChannelMasks.add(0);
2224fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    }
2225fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                }
2226fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            }
2227fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        }
2228fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    } // end disconnect
2229fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
2230fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    return NO_ERROR;
2231fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean}
2232fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
2233b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurentvoid AudioPolicyManagerBase::closeOutput(audio_io_handle_t output)
2234f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2235b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    ALOGV("closeOutput(%d)", output);
2236f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2237b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
2238b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (outputDesc == NULL) {
2239b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        ALOGW("closeOutput() unknown output %d", output);
2240b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        return;
2241f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2242f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2243b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    // look for duplicated outputs connected to the output being removed.
2244b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    for (size_t i = 0; i < mOutputs.size(); i++) {
2245b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        AudioOutputDescriptor *dupOutputDesc = mOutputs.valueAt(i);
2246b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        if (dupOutputDesc->isDuplicated() &&
2247b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                (dupOutputDesc->mOutput1 == outputDesc ||
2248b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                dupOutputDesc->mOutput2 == outputDesc)) {
2249b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            AudioOutputDescriptor *outputDesc2;
2250b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            if (dupOutputDesc->mOutput1 == outputDesc) {
2251b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                outputDesc2 = dupOutputDesc->mOutput2;
2252b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            } else {
2253b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                outputDesc2 = dupOutputDesc->mOutput1;
2254b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            }
2255b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            // As all active tracks on duplicated output will be deleted,
2256b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            // and as they were also referenced on the other output, the reference
2257b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            // count for their stream type must be adjusted accordingly on
2258b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            // the other output.
2259b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            for (int j = 0; j < (int)AudioSystem::NUM_STREAM_TYPES; j++) {
2260b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                int refCount = dupOutputDesc->mRefCount[j];
2261b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                outputDesc2->changeRefCount((AudioSystem::stream_type)j,-refCount);
2262b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            }
2263b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i);
2264b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput);
2265f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2266b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            mpClientInterface->closeOutput(duplicatedOutput);
2267b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            delete mOutputs.valueFor(duplicatedOutput);
2268b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            mOutputs.removeItem(duplicatedOutput);
2269f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2270f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2271b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
2272b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    AudioParameter param;
2273b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    param.add(String8("closing"), String8("true"));
2274b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    mpClientInterface->setParameters(output, param.toString());
2275b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
2276b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    mpClientInterface->closeOutput(output);
22775a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent    delete outputDesc;
2278b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    mOutputs.removeItem(output);
22795a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent    mPreviousOutputs = mOutputs;
2280f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2281f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2282c952527e6f89d5427881462823514be9d79f13e6Eric LaurentSortedVector<audio_io_handle_t> AudioPolicyManagerBase::getOutputsForDevice(audio_devices_t device,
2283c952527e6f89d5427881462823514be9d79f13e6Eric Laurent                        DefaultKeyedVector<audio_io_handle_t, AudioOutputDescriptor *> openOutputs)
2284f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2285b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    SortedVector<audio_io_handle_t> outputs;
2286f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
22873cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGVV("getOutputsForDevice() device %04x", device);
2288c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    for (size_t i = 0; i < openOutputs.size(); i++) {
22893cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        ALOGVV("output %d isDuplicated=%d device=%04x",
2290c952527e6f89d5427881462823514be9d79f13e6Eric Laurent                i, openOutputs.valueAt(i)->isDuplicated(), openOutputs.valueAt(i)->supportedDevices());
2291c952527e6f89d5427881462823514be9d79f13e6Eric Laurent        if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) {
2292c952527e6f89d5427881462823514be9d79f13e6Eric Laurent            ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i));
2293c952527e6f89d5427881462823514be9d79f13e6Eric Laurent            outputs.add(openOutputs.keyAt(i));
2294f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2295f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2296b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    return outputs;
2297f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2298f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2299b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurentbool AudioPolicyManagerBase::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
2300b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                                   SortedVector<audio_io_handle_t>& outputs2)
2301f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2302b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (outputs1.size() != outputs2.size()) {
2303b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        return false;
2304f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2305b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    for (size_t i = 0; i < outputs1.size(); i++) {
2306b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        if (outputs1[i] != outputs2[i]) {
2307b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            return false;
2308f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2309f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2310b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    return true;
2311b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent}
2312b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
2313b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurentvoid AudioPolicyManagerBase::checkOutputForStrategy(routing_strategy strategy)
2314b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent{
231501e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent    audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
231601e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent    audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
2317c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs);
2318c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
2319b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
2320b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (!vectorsEqual(srcOutputs,dstOutputs)) {
2321b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d",
2322b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent              strategy, srcOutputs[0], dstOutputs[0]);
23235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // mute strategy while moving tracks from one output to another
23245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        for (size_t i = 0; i < srcOutputs.size(); i++) {
2325fa3697d716b444bbea6be480801536c44bf69214Eric Laurent            AudioOutputDescriptor *desc = mOutputs.valueFor(srcOutputs[i]);
232680f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent            if (desc->isStrategyActive(strategy)) {
2327fa3697d716b444bbea6be480801536c44bf69214Eric Laurent                setStrategyMute(strategy, true, srcOutputs[i]);
2328fa3697d716b444bbea6be480801536c44bf69214Eric Laurent                setStrategyMute(strategy, false, srcOutputs[i], MUTE_TIME_MS, newDevice);
2329fa3697d716b444bbea6be480801536c44bf69214Eric Laurent            }
23305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
2331f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2332f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // Move effects associated to this strategy from previous output to new output
23334660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen        if (strategy == STRATEGY_MEDIA) {
233445c763947b657b347211dc9388754e05d30d0467Eric Laurent            audio_io_handle_t fxOutput = selectOutputForEffects(dstOutputs);
23354660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen            SortedVector<audio_io_handle_t> moved;
23364660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen            for (size_t i = 0; i < mEffects.size(); i++) {
23374660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                EffectDescriptor *desc = mEffects.valueAt(i);
23384660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                if (desc->mSession == AUDIO_SESSION_OUTPUT_MIX &&
233945c763947b657b347211dc9388754e05d30d0467Eric Laurent                        desc->mIo != fxOutput) {
23404660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                    if (moved.indexOf(desc->mIo) < 0) {
23414660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                        ALOGV("checkOutputForStrategy() moving effect %d to output %d",
234245c763947b657b347211dc9388754e05d30d0467Eric Laurent                              mEffects.keyAt(i), fxOutput);
23434660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                        mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, desc->mIo,
234445c763947b657b347211dc9388754e05d30d0467Eric Laurent                                                       fxOutput);
23454660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                        moved.add(desc->mIo);
23464660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                    }
234745c763947b657b347211dc9388754e05d30d0467Eric Laurent                    desc->mIo = fxOutput;
23484660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                }
23494660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen            }
23504660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen        }
23515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // Move tracks associated to this strategy from previous output to new output
2352f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        for (int i = 0; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) {
2353f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (getStrategy((AudioSystem::stream_type)i) == strategy) {
2354316222fcd60a429e53aa6205278697bb2df8ef32Glenn Kasten                mpClientInterface->invalidateStream((AudioSystem::stream_type)i);
2355f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2356f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2357f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2358f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2359f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2360f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::checkOutputForAllStrategies()
2361f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2362c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
2363f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForStrategy(STRATEGY_PHONE);
2364f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForStrategy(STRATEGY_SONIFICATION);
236512bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
2366f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForStrategy(STRATEGY_MEDIA);
2367f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForStrategy(STRATEGY_DTMF);
2368f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2369f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2370b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurentaudio_io_handle_t AudioPolicyManagerBase::getA2dpOutput()
2371b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent{
23725ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (!mHasA2dp) {
2373b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        return 0;
2374b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    }
2375b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
2376b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    for (size_t i = 0; i < mOutputs.size(); i++) {
2377b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        AudioOutputDescriptor *outputDesc = mOutputs.valueAt(i);
2378b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        if (!outputDesc->isDuplicated() && outputDesc->device() & AUDIO_DEVICE_OUT_ALL_A2DP) {
2379b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            return mOutputs.keyAt(i);
2380b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        }
2381b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    }
2382b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
2383b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    return 0;
2384b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent}
2385b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
2386f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::checkA2dpSuspend()
2387f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
23885ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (!mHasA2dp) {
2389b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        return;
2390b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    }
2391b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    audio_io_handle_t a2dpOutput = getA2dpOutput();
2392b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (a2dpOutput == 0) {
2393b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        return;
2394b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    }
2395b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
2396f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // suspend A2DP output if:
2397f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      (NOT already suspended) &&
2398f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      ((SCO device is connected &&
2399f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //       (forced usage for communication || for record is SCO))) ||
2400f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      (phone state is ringing || in call)
2401f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //
2402f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // restore A2DP output if:
2403f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      (Already suspended) &&
2404f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      ((SCO device is NOT connected ||
2405f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //       (forced usage NOT for communication && NOT for record is SCO))) &&
2406f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      (phone state is NOT ringing && NOT in call)
2407f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //
2408f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mA2dpSuspended) {
2409f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (((mScoDeviceAddress == "") ||
2410f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin             ((mForceUse[AudioSystem::FOR_COMMUNICATION] != AudioSystem::FORCE_BT_SCO) &&
2411f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin              (mForceUse[AudioSystem::FOR_RECORD] != AudioSystem::FORCE_BT_SCO))) &&
2412f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin             ((mPhoneState != AudioSystem::MODE_IN_CALL) &&
2413f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin              (mPhoneState != AudioSystem::MODE_RINGTONE))) {
2414f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2415b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            mpClientInterface->restoreOutput(a2dpOutput);
2416f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mA2dpSuspended = false;
2417f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2418f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
2419f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (((mScoDeviceAddress != "") &&
2420f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin             ((mForceUse[AudioSystem::FOR_COMMUNICATION] == AudioSystem::FORCE_BT_SCO) ||
2421f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin              (mForceUse[AudioSystem::FOR_RECORD] == AudioSystem::FORCE_BT_SCO))) ||
2422f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin             ((mPhoneState == AudioSystem::MODE_IN_CALL) ||
2423f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin              (mPhoneState == AudioSystem::MODE_RINGTONE))) {
2424f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2425b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            mpClientInterface->suspendOutput(a2dpOutput);
2426f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mA2dpSuspended = true;
2427f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2428f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2429f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2430f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2431f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurentaudio_devices_t AudioPolicyManagerBase::getNewDevice(audio_io_handle_t output, bool fromCache)
2432f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2433ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    audio_devices_t device = AUDIO_DEVICE_NONE;
2434f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2435f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
2436f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // check the following by order of priority to request a routing change if necessary:
2437c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    // 1: the strategy enforced audible is active on the output:
2438c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    //      use device for strategy enforced audible
2439c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    // 2: we are in call or the strategy phone is active on the output:
2440f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      use device for strategy phone
2441c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    // 3: the strategy sonification is active on the output:
2442f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      use device for strategy sonification
244312bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    // 4: the strategy "respectful" sonification is active on the output:
244412bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    //      use device for strategy "respectful" sonification
244512bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    // 5: the strategy media is active on the output:
2446f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      use device for strategy media
244712bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    // 6: the strategy DTMF is active on the output:
2448f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      use device for strategy DTMF
244980f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    if (outputDesc->isStrategyActive(STRATEGY_ENFORCED_AUDIBLE)) {
2450c16ac09f510437e8340be691720177a490ae78f0Eric Laurent        device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
2451c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    } else if (isInCall() ||
245280f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                    outputDesc->isStrategyActive(STRATEGY_PHONE)) {
2453f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
245480f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    } else if (outputDesc->isStrategyActive(STRATEGY_SONIFICATION)) {
2455f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
245680f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    } else if (outputDesc->isStrategyActive(STRATEGY_SONIFICATION_RESPECTFUL)) {
24575ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
245880f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    } else if (outputDesc->isStrategyActive(STRATEGY_MEDIA)) {
2459f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
246080f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    } else if (outputDesc->isStrategyActive(STRATEGY_DTMF)) {
2461f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
2462f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2463f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
24646a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("getNewDevice() selected device %x", device);
2465f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return device;
2466f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2467f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2468f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinuint32_t AudioPolicyManagerBase::getStrategyForStream(AudioSystem::stream_type stream) {
2469f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return (uint32_t)getStrategy(stream);
2470f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2471f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2472f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurentaudio_devices_t AudioPolicyManagerBase::getDevicesForStream(AudioSystem::stream_type stream) {
2473f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent    audio_devices_t devices;
2474f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // By checking the range of stream before calling getStrategy, we avoid
24755efbd421e0029d6fc44b1cc65c0e5e0d85e5161fSteve Block    // getStrategy's behavior for invalid streams.  getStrategy would do a ALOGE
2476f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // and then return STRATEGY_MEDIA, but we want to return the empty set.
2477f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (stream < (AudioSystem::stream_type) 0 || stream >= AudioSystem::NUM_STREAM_TYPES) {
2478ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        devices = AUDIO_DEVICE_NONE;
2479f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
2480f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioPolicyManagerBase::routing_strategy strategy = getStrategy(stream);
24815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        devices = getDeviceForStrategy(strategy, true /*fromCache*/);
2482f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2483f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return devices;
2484f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2485f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2486f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima ZavinAudioPolicyManagerBase::routing_strategy AudioPolicyManagerBase::getStrategy(
2487f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioSystem::stream_type stream) {
2488f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // stream to strategy mapping
2489f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    switch (stream) {
2490f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::VOICE_CALL:
2491f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::BLUETOOTH_SCO:
2492f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return STRATEGY_PHONE;
2493f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::RING:
2494f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::ALARM:
2495f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return STRATEGY_SONIFICATION;
249612bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    case AudioSystem::NOTIFICATION:
249712bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        return STRATEGY_SONIFICATION_RESPECTFUL;
2498f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::DTMF:
2499f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return STRATEGY_DTMF;
2500f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    default:
25015efbd421e0029d6fc44b1cc65c0e5e0d85e5161fSteve Block        ALOGE("unknown stream type");
2502f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::SYSTEM:
2503f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // NOTE: SYSTEM stream uses MEDIA strategy because muting music and switching outputs
2504f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // while key clicks are played produces a poor result
2505f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::TTS:
2506f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::MUSIC:
2507f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return STRATEGY_MEDIA;
2508c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    case AudioSystem::ENFORCED_AUDIBLE:
2509c16ac09f510437e8340be691720177a490ae78f0Eric Laurent        return STRATEGY_ENFORCED_AUDIBLE;
2510f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2511f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2512f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
251312bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivivoid AudioPolicyManagerBase::handleNotificationRoutingForStream(AudioSystem::stream_type stream) {
251412bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    switch(stream) {
251512bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    case AudioSystem::MUSIC:
251612bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
2517c952527e6f89d5427881462823514be9d79f13e6Eric Laurent        updateDevicesAndOutputs();
251812bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        break;
251912bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    default:
252012bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        break;
252112bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    }
252212bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi}
252312bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi
25245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentaudio_devices_t AudioPolicyManagerBase::getDeviceForStrategy(routing_strategy strategy,
25255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                             bool fromCache)
2526f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2527ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    uint32_t device = AUDIO_DEVICE_NONE;
2528f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2529f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (fromCache) {
25303cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
25315ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent              strategy, mDeviceForStrategy[strategy]);
2532f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return mDeviceForStrategy[strategy];
2533f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2534f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2535f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    switch (strategy) {
253612bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi
253712bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    case STRATEGY_SONIFICATION_RESPECTFUL:
253812bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        if (isInCall()) {
25395ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
2540dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi        } else if (isStreamActiveRemotely(AudioSystem::MUSIC,
2541dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi                SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
2542dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi            // while media is playing on a remote device, use the the sonification behavior.
2543dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi            // Note that we test this usecase before testing if media is playing because
2544dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi            //   the isStreamActive() method only informs about the activity of a stream, not
2545dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi            //   if it's for local playback. Note also that we use the same delay between both tests
2546dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi            device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
254712bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        } else if (isStreamActive(AudioSystem::MUSIC, SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
254812bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi            // while media is playing (or has recently played), use the same device
25495ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/);
255012bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        } else {
255112bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi            // when media is not playing anymore, fall back on the sonification behavior
25525ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
255312bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        }
255412bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi
255512bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        break;
255612bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi
2557f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case STRATEGY_DTMF:
2558f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (!isInCall()) {
2559f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // when off call, DTMF strategy follows the same rules as MEDIA strategy
25605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/);
2561f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            break;
2562f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2563f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // when in call, DTMF and PHONE strategies follow the same rules
2564f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // FALL THROUGH
2565f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2566f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case STRATEGY_PHONE:
2567f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // for phone strategy, we first consider the forced use and then the available devices by order
2568f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // of priority
2569f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        switch (mForceUse[AudioSystem::FOR_COMMUNICATION]) {
2570f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        case AudioSystem::FORCE_BT_SCO:
2571f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (!isInCall() || strategy != STRATEGY_DTMF) {
2572c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT;
2573f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (device) break;
2574f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2575c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET;
2576f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (device) break;
2577c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO;
2578f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (device) break;
2579f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // if SCO device is requested but no SCO device is available, fall back to default case
2580f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // FALL THROUGH
2581f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2582f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        default:    // FORCE_NONE
2583f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to A2DP
25841afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent            if (mHasA2dp && !isInCall() &&
25851afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent                    (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
2586ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent                    (getA2dpOutput() != 0) && !mA2dpSuspended) {
2587c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
2588f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (device) break;
2589c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
2590f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (device) break;
2591f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2592c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
25931afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent            if (device) break;
2594c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADSET;
25951afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent            if (device) break;
2596b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent            if (mPhoneState != AudioSystem::MODE_IN_CALL) {
2597b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
2598b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2599b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
2600b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2601b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
2602b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2603b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
2604b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2605b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
2606b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2607b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent            }
2608c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_EARPIECE;
26095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            if (device) break;
26105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = mDefaultOutputDevice;
2611ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            if (device == AUDIO_DEVICE_NONE) {
26125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE");
2613f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2614f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            break;
2615f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2616f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        case AudioSystem::FORCE_SPEAKER:
2617f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to
2618f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // A2DP speaker when forcing to speaker output
26191afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent            if (mHasA2dp && !isInCall() &&
26201afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent                    (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
2621ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent                    (getA2dpOutput() != 0) && !mA2dpSuspended) {
2622c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
2623f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (device) break;
2624f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2625b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent            if (mPhoneState != AudioSystem::MODE_IN_CALL) {
2626b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
2627b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2628b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
2629b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2630b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
2631b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2632b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
2633b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2634b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
2635b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2636b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent            }
2637c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER;
26385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            if (device) break;
26395ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = mDefaultOutputDevice;
2640ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            if (device == AUDIO_DEVICE_NONE) {
26415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE, FORCE_SPEAKER");
2642f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2643f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            break;
2644f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2645f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    break;
2646f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2647f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case STRATEGY_SONIFICATION:
2648f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2649f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // If incall, just select the STRATEGY_PHONE device: The rest of the behavior is handled by
2650f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // handleIncallSonification().
2651f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (isInCall()) {
26525ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = getDeviceForStrategy(STRATEGY_PHONE, false /*fromCache*/);
2653f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            break;
2654f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2655c16ac09f510437e8340be691720177a490ae78f0Eric Laurent        // FALL THROUGH
2656c16ac09f510437e8340be691720177a490ae78f0Eric Laurent
2657c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    case STRATEGY_ENFORCED_AUDIBLE:
2658c16ac09f510437e8340be691720177a490ae78f0Eric Laurent        // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION
2659ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        // except:
2660ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        //   - when in call where it doesn't default to STRATEGY_PHONE behavior
2661ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        //   - in countries where not enforced in which case it follows STRATEGY_MEDIA
2662c16ac09f510437e8340be691720177a490ae78f0Eric Laurent
2663738207def5f691d605ae33d041116829a74513a9Eric Laurent        if ((strategy == STRATEGY_SONIFICATION) ||
2664738207def5f691d605ae33d041116829a74513a9Eric Laurent                (mForceUse[AudioSystem::FOR_SYSTEM] == AudioSystem::FORCE_SYSTEM_ENFORCED)) {
2665c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER;
2666ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            if (device == AUDIO_DEVICE_NONE) {
2667ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent                ALOGE("getDeviceForStrategy() speaker device not found for STRATEGY_SONIFICATION");
2668ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent            }
2669f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2670f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // The second device used for sonification is the same as the device used by media strategy
2671f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // FALL THROUGH
2672f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2673f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case STRATEGY_MEDIA: {
2674ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        uint32_t device2 = AUDIO_DEVICE_NONE;
267531363a9cb94e80330c335fede0b92b1953a09517Jean-Michel Trivi        if (strategy != STRATEGY_SONIFICATION) {
267631363a9cb94e80330c335fede0b92b1953a09517Jean-Michel Trivi            // no sonification on remote submix (e.g. WFD)
267748387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
267848387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi        }
2679ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if ((device2 == AUDIO_DEVICE_NONE) &&
268048387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi                mHasA2dp && (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
2681ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent                (getA2dpOutput() != 0) && !mA2dpSuspended) {
2682c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
2683ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            if (device2 == AUDIO_DEVICE_NONE) {
2684c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
2685f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2686ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            if (device2 == AUDIO_DEVICE_NONE) {
2687c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
2688f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2689f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2690ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (device2 == AUDIO_DEVICE_NONE) {
2691c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
26921afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent        }
2693ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (device2 == AUDIO_DEVICE_NONE) {
2694c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADSET;
26951afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent        }
2696ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (device2 == AUDIO_DEVICE_NONE) {
2697599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
2698599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent        }
2699ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (device2 == AUDIO_DEVICE_NONE) {
2700599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
2701599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent        }
2702ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (device2 == AUDIO_DEVICE_NONE) {
2703c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
2704f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
270531363a9cb94e80330c335fede0b92b1953a09517Jean-Michel Trivi        if ((device2 == AUDIO_DEVICE_NONE) && (strategy != STRATEGY_SONIFICATION)) {
270631363a9cb94e80330c335fede0b92b1953a09517Jean-Michel Trivi            // no sonification on aux digital (e.g. HDMI)
2707c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
2708f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
27095a484b753cc72d6a50c1dd3bbf68b3403c741a3aEric Laurent        if ((device2 == AUDIO_DEVICE_NONE) &&
27105a484b753cc72d6a50c1dd3bbf68b3403c741a3aEric Laurent                (mForceUse[AudioSystem::FOR_DOCK] == AudioSystem::FORCE_ANALOG_DOCK)) {
2711c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
2712f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2713ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (device2 == AUDIO_DEVICE_NONE) {
2714c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER;
2715f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2716f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2717c16ac09f510437e8340be691720177a490ae78f0Eric Laurent        // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or
2718ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise
2719f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device |= device2;
27205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (device) break;
27215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        device = mDefaultOutputDevice;
2722ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (device == AUDIO_DEVICE_NONE) {
27235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGE("getDeviceForStrategy() no device found for STRATEGY_MEDIA");
2724f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2725f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        } break;
2726f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2727f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    default:
272864cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("getDeviceForStrategy() unknown strategy: %d", strategy);
2729f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
2730f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2731f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
27323cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGVV("getDeviceForStrategy() strategy %d, device %x", strategy, device);
2733c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    return device;
2734f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2735f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2736c952527e6f89d5427881462823514be9d79f13e6Eric Laurentvoid AudioPolicyManagerBase::updateDevicesAndOutputs()
2737f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2738f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < NUM_STRATEGIES; i++) {
27395ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
27405ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
2741c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    mPreviousOutputs = mOutputs;
27425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
27435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
2744b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurentuint32_t AudioPolicyManagerBase::checkDeviceMuteStrategies(AudioOutputDescriptor *outputDesc,
27459029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                                                       audio_devices_t prevDevice,
27465ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                       uint32_t delayMs)
27475ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
27489029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    // mute/unmute strategies using an incompatible device combination
27499029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    // if muting, wait for the audio in pcm buffer to be drained before proceeding
27509029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    // if unmuting, unmute only after the specified delay
27515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (outputDesc->isDuplicated()) {
2752b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        return 0;
27535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
27545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
27555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    uint32_t muteWaitMs = 0;
27565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    audio_devices_t device = outputDesc->device();
275780f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    bool shouldMute = outputDesc->isActive() && (AudioSystem::popCount(device) >= 2);
27589029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    // temporary mute output if device selection changes to avoid volume bursts due to
27599029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    // different per device volumes
276080f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    bool tempMute = outputDesc->isActive() && (device != prevDevice);
27615ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
27625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < NUM_STRATEGIES; i++) {
27635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
27645ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        bool mute = shouldMute && (curDevice & device) && (curDevice != device);
27655ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        bool doMute = false;
27665ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
27675ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (mute && !outputDesc->mStrategyMutedByDevice[i]) {
27685ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            doMute = true;
27695ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            outputDesc->mStrategyMutedByDevice[i] = true;
27705ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){
27715ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            doMute = true;
27725ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            outputDesc->mStrategyMutedByDevice[i] = false;
27735ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
27749029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent        if (doMute || tempMute) {
27755ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            for (size_t j = 0; j < mOutputs.size(); j++) {
27765ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                AudioOutputDescriptor *desc = mOutputs.valueAt(j);
2777f32e38e24db196671d3ea43427125a4e212466faEric Laurent                // skip output if it does not share any device with current output
2778c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                if ((desc->supportedDevices() & outputDesc->supportedDevices())
2779ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent                        == AUDIO_DEVICE_NONE) {
27805ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                    continue;
27815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                }
27825ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                audio_io_handle_t curOutput = mOutputs.keyAt(j);
27833cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x) on output %d",
27845ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                      mute ? "muting" : "unmuting", i, curDevice, curOutput);
27855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                setStrategyMute((routing_strategy)i, mute, curOutput, mute ? 0 : delayMs);
278680f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                if (desc->isStrategyActive((routing_strategy)i)) {
2787f32e38e24db196671d3ea43427125a4e212466faEric Laurent                    // do tempMute only for current output
2788f32e38e24db196671d3ea43427125a4e212466faEric Laurent                    if (tempMute && (desc == outputDesc)) {
278901e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                        setStrategyMute((routing_strategy)i, true, curOutput);
279001e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                        setStrategyMute((routing_strategy)i, false, curOutput,
279101e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                            desc->latency() * 2, device);
27929029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                    }
2793f32e38e24db196671d3ea43427125a4e212466faEric Laurent                    if ((tempMute && (desc == outputDesc)) || mute) {
27949029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                        if (muteWaitMs < desc->latency()) {
27959029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                            muteWaitMs = desc->latency();
27969029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                        }
27975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                    }
27985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                }
27995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
28005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
28015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
28025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
28035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // FIXME: should not need to double latency if volume could be applied immediately by the
28045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // audioflinger mixer. We must account for the delay between now and the next time
28055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // the audioflinger thread for this output will process a buffer (which corresponds to
28065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // one buffer size, usually 1/2 or 1/4 of the latency).
28075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    muteWaitMs *= 2;
28085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // wait for the PCM output buffers to empty before proceeding with the rest of the command
28095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (muteWaitMs > delayMs) {
2810b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        muteWaitMs -= delayMs;
2811b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        usleep(muteWaitMs * 1000);
2812b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        return muteWaitMs;
2813f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2814b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent    return 0;
2815f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2816f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2817b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurentuint32_t AudioPolicyManagerBase::setOutputDevice(audio_io_handle_t output,
2818f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                                             audio_devices_t device,
2819f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                                             bool force,
2820f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                                             int delayMs)
2821f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
28225ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGV("setOutputDevice() output %d device %04x delayMs %d", output, device, delayMs);
2823f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
28245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    AudioParameter param;
2825f32e38e24db196671d3ea43427125a4e212466faEric Laurent    uint32_t muteWaitMs;
2826f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2827f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (outputDesc->isDuplicated()) {
2828b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        muteWaitMs = setOutputDevice(outputDesc->mOutput1->mId, device, force, delayMs);
2829b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        muteWaitMs += setOutputDevice(outputDesc->mOutput2->mId, device, force, delayMs);
2830b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        return muteWaitMs;
2831f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2832f32e38e24db196671d3ea43427125a4e212466faEric Laurent    // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current
2833f32e38e24db196671d3ea43427125a4e212466faEric Laurent    // output profile
2834f32e38e24db196671d3ea43427125a4e212466faEric Laurent    if ((device != AUDIO_DEVICE_NONE) &&
2835f32e38e24db196671d3ea43427125a4e212466faEric Laurent            ((device & outputDesc->mProfile->mSupportedDevices) == 0)) {
2836f32e38e24db196671d3ea43427125a4e212466faEric Laurent        return 0;
2837f32e38e24db196671d3ea43427125a4e212466faEric Laurent    }
2838f32e38e24db196671d3ea43427125a4e212466faEric Laurent
2839f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // filter devices according to output selected
2840f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent    device = (audio_devices_t)(device & outputDesc->mProfile->mSupportedDevices);
2841f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
28425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    audio_devices_t prevDevice = outputDesc->mDevice;
28435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
28445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGV("setOutputDevice() prevDevice %04x", prevDevice);
28455ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
2846ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if (device != AUDIO_DEVICE_NONE) {
28475ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        outputDesc->mDevice = device;
28485ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
28499029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs);
28505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
2851f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // Do not change the routing if:
2852ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    //  - the requested device is AUDIO_DEVICE_NONE
2853f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //  - the requested device is the same as current device and force is not specified.
2854f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // Doing this check here allows the caller to call setOutputDevice() without conditions
2855ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if ((device == AUDIO_DEVICE_NONE || device == prevDevice) && !force) {
28565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        ALOGV("setOutputDevice() setting same device %04x or null device for output %d", device, output);
2857b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        return muteWaitMs;
2858f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2859f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
28605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGV("setOutputDevice() changing device");
2861f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // do the routing
2862f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    param.addInt(String8(AudioParameter::keyRouting), (int)device);
28635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    mpClientInterface->setParameters(output, param.toString(), delayMs);
28645ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
2865f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // update stream volumes according to new device
2866f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    applyStreamVolumes(output, device, delayMs);
2867b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent
2868b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent    return muteWaitMs;
28695ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
2870f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
28715ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric LaurentAudioPolicyManagerBase::IOProfile *AudioPolicyManagerBase::getInputProfile(audio_devices_t device,
28725ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                   uint32_t samplingRate,
28735082dbeb19e37883230510129c94336063a4f91cGlenn Kasten                                                   audio_format_t format,
28742c3d2379ee6eadb32b63bf6de538cfd6487e8e20Glenn Kasten                                                   audio_channel_mask_t channelMask)
28755ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
28765ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // Choose an input profile based on the requested capture parameters: select the first available
28775ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // profile supporting all requested parameters.
287870c236c9290732782d5267935af1475b8d5ae602Eric Laurent    for (size_t i = 0; i < mHwModules.size(); i++)
28795ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    {
288070c236c9290732782d5267935af1475b8d5ae602Eric Laurent        if (mHwModules[i]->mHandle == 0) {
28815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            continue;
28825ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
288370c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
28845ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        {
288570c236c9290732782d5267935af1475b8d5ae602Eric Laurent            IOProfile *profile = mHwModules[i]->mInputProfiles[j];
2886fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            // profile->log();
288770c236c9290732782d5267935af1475b8d5ae602Eric Laurent            if (profile->isCompatibleProfile(device, samplingRate, format,
28882d749b0bda4ae60ef1a940eeba86121b2e94d07dGlenn Kasten                                             channelMask, AUDIO_OUTPUT_FLAG_NONE)) {
288970c236c9290732782d5267935af1475b8d5ae602Eric Laurent                return profile;
28905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
28915ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
2892f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
289370c236c9290732782d5267935af1475b8d5ae602Eric Laurent    return NULL;
2894f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2895f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2896f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurentaudio_devices_t AudioPolicyManagerBase::getDeviceForInputSource(int inputSource)
2897f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2898ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    uint32_t device = AUDIO_DEVICE_NONE;
2899f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
29009641bd36dbf0c4b1eeb84499b35c1cdb7fa5a530Eric Laurent    switch (inputSource) {
29019641bd36dbf0c4b1eeb84499b35c1cdb7fa5a530Eric Laurent    case AUDIO_SOURCE_VOICE_UPLINK:
29029641bd36dbf0c4b1eeb84499b35c1cdb7fa5a530Eric Laurent      if (mAvailableInputDevices & AUDIO_DEVICE_IN_VOICE_CALL) {
29039641bd36dbf0c4b1eeb84499b35c1cdb7fa5a530Eric Laurent          device = AUDIO_DEVICE_IN_VOICE_CALL;
29049641bd36dbf0c4b1eeb84499b35c1cdb7fa5a530Eric Laurent          break;
29059641bd36dbf0c4b1eeb84499b35c1cdb7fa5a530Eric Laurent      }
29069641bd36dbf0c4b1eeb84499b35c1cdb7fa5a530Eric Laurent      // FALL THROUGH
29079641bd36dbf0c4b1eeb84499b35c1cdb7fa5a530Eric Laurent
2908f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_DEFAULT:
2909f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_MIC:
291033bf1b0fe363bd4892349d160f54d860567fab12Mike Lockwood    if (mAvailableInputDevices & AUDIO_DEVICE_IN_BLUETOOTH_A2DP) {
291133bf1b0fe363bd4892349d160f54d860567fab12Mike Lockwood        device = AUDIO_DEVICE_IN_BLUETOOTH_A2DP;
291233bf1b0fe363bd4892349d160f54d860567fab12Mike Lockwood        break;
291333bf1b0fe363bd4892349d160f54d860567fab12Mike Lockwood    }
291433bf1b0fe363bd4892349d160f54d860567fab12Mike Lockwood    // FALL THROUGH
291533bf1b0fe363bd4892349d160f54d860567fab12Mike Lockwood
2916f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_RECOGNITION:
2917fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent    case AUDIO_SOURCE_HOTWORD:
2918f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_COMMUNICATION:
2919f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (mForceUse[AudioSystem::FOR_RECORD] == AudioSystem::FORCE_BT_SCO &&
2920c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            mAvailableInputDevices & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
2921c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
2922c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_WIRED_HEADSET) {
2923c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = AUDIO_DEVICE_IN_WIRED_HEADSET;
2924fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_USB_DEVICE) {
2925fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            device = AUDIO_DEVICE_IN_USB_DEVICE;
2926c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_BUILTIN_MIC) {
2927c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = AUDIO_DEVICE_IN_BUILTIN_MIC;
2928f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2929f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
2930f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_CAMCORDER:
2931c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        if (mAvailableInputDevices & AUDIO_DEVICE_IN_BACK_MIC) {
2932c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = AUDIO_DEVICE_IN_BACK_MIC;
2933c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_BUILTIN_MIC) {
2934c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = AUDIO_DEVICE_IN_BUILTIN_MIC;
2935f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2936f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
2937f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_DOWNLINK:
2938f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_CALL:
2939c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        if (mAvailableInputDevices & AUDIO_DEVICE_IN_VOICE_CALL) {
2940c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = AUDIO_DEVICE_IN_VOICE_CALL;
29415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
2942f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
294348387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi    case AUDIO_SOURCE_REMOTE_SUBMIX:
294448387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi        if (mAvailableInputDevices & AUDIO_DEVICE_IN_REMOTE_SUBMIX) {
294548387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
294648387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi        }
294748387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi        break;
2948f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    default:
294964cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("getDeviceForInputSource() invalid input source %d", inputSource);
2950f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
2951f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
29526a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("getDeviceForInputSource()input source %d, device %08x", inputSource, device);
2953c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    return device;
2954f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2955f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
29566d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivibool AudioPolicyManagerBase::isVirtualInputDevice(audio_devices_t device)
29576d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi{
29586d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi    if ((device & AUDIO_DEVICE_BIT_IN) != 0) {
29596d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi        device &= ~AUDIO_DEVICE_BIT_IN;
29606d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi        if ((popcount(device) == 1) && ((device & ~APM_AUDIO_IN_DEVICE_VIRTUAL_ALL) == 0))
29616d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi            return true;
29626d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi    }
29636d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi    return false;
29646d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi}
29656d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi
29666d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Triviaudio_io_handle_t AudioPolicyManagerBase::getActiveInput(bool ignoreVirtualInputs)
2967f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2968f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mInputs.size(); i++) {
29696d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi        const AudioInputDescriptor * input_descriptor = mInputs.valueAt(i);
29706d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi        if ((input_descriptor->mRefCount > 0)
29716d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi                && (!ignoreVirtualInputs || !isVirtualInputDevice(input_descriptor->mDevice))) {
2972f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return mInputs.keyAt(i);
2973f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2974f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2975f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return 0;
2976f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2977f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2978e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2979c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentaudio_devices_t AudioPolicyManagerBase::getDeviceForVolume(audio_devices_t device)
2980e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent{
2981ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if (device == AUDIO_DEVICE_NONE) {
2982e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        // this happens when forcing a route update and no track is active on an output.
2983e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        // In this case the returned category is not important.
2984c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        device =  AUDIO_DEVICE_OUT_SPEAKER;
2985c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    } else if (AudioSystem::popCount(device) > 1) {
2986e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        // Multiple device selection is either:
2987e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        //  - speaker + one other device: give priority to speaker in this case.
2988e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        //  - one A2DP device + another device: happens with duplicated output. In this case
2989e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        // retain the device on the A2DP output as the other must not correspond to an active
2990e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        // selection if not the speaker.
2991c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        if (device & AUDIO_DEVICE_OUT_SPEAKER) {
2992c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            device = AUDIO_DEVICE_OUT_SPEAKER;
2993c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        } else {
2994c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            device = (audio_devices_t)(device & AUDIO_DEVICE_OUT_ALL_A2DP);
2995c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        }
2996e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    }
2997e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
299864cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block    ALOGW_IF(AudioSystem::popCount(device) != 1,
2999c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            "getDeviceForVolume() invalid device combination: %08x",
3000e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            device);
3001e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
3002c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    return device;
3003c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent}
3004c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
3005f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric LaurentAudioPolicyManagerBase::device_category AudioPolicyManagerBase::getDeviceCategory(audio_devices_t device)
3006c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent{
3007f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent    switch(getDeviceForVolume(device)) {
3008e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_EARPIECE:
3009e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            return DEVICE_CATEGORY_EARPIECE;
3010e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_WIRED_HEADSET:
3011e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_WIRED_HEADPHONE:
3012e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_BLUETOOTH_SCO:
3013e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET:
3014e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP:
3015e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES:
3016e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            return DEVICE_CATEGORY_HEADSET;
3017e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_SPEAKER:
3018e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT:
3019e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER:
3020c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        case AUDIO_DEVICE_OUT_AUX_DIGITAL:
3021599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent        case AUDIO_DEVICE_OUT_USB_ACCESSORY:
3022599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent        case AUDIO_DEVICE_OUT_USB_DEVICE:
302348387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi        case AUDIO_DEVICE_OUT_REMOTE_SUBMIX:
3024e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        default:
3025e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            return DEVICE_CATEGORY_SPEAKER;
3026e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    }
3027e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent}
3028e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
3029f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurentfloat AudioPolicyManagerBase::volIndexToAmpl(audio_devices_t device, const StreamDescriptor& streamDesc,
3030e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        int indexInUi)
3031e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent{
3032e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    device_category deviceCategory = getDeviceCategory(device);
3033e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    const VolumeCurvePoint *curve = streamDesc.mVolumeCurve[deviceCategory];
3034e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
3035f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // the volume index in the UI is relative to the min and max volume indices for this stream type
3036e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    int nbSteps = 1 + curve[VOLMAX].mIndex -
3037e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            curve[VOLMIN].mIndex;
3038f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    int volIdx = (nbSteps * (indexInUi - streamDesc.mIndexMin)) /
3039f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            (streamDesc.mIndexMax - streamDesc.mIndexMin);
3040f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3041f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // find what part of the curve this index volume belongs to, or if it's out of bounds
3042f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    int segment = 0;
3043e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    if (volIdx < curve[VOLMIN].mIndex) {         // out of bounds
3044f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return 0.0f;
3045e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    } else if (volIdx < curve[VOLKNEE1].mIndex) {
3046f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        segment = 0;
3047e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    } else if (volIdx < curve[VOLKNEE2].mIndex) {
3048f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        segment = 1;
3049e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    } else if (volIdx <= curve[VOLMAX].mIndex) {
3050f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        segment = 2;
3051f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {                                                               // out of bounds
3052f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return 1.0f;
3053f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3054f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3055f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // linear interpolation in the attenuation table in dB
3056e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    float decibels = curve[segment].mDBAttenuation +
3057e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            ((float)(volIdx - curve[segment].mIndex)) *
3058e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent                ( (curve[segment+1].mDBAttenuation -
3059e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent                        curve[segment].mDBAttenuation) /
3060e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent                    ((float)(curve[segment+1].mIndex -
3061e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent                            curve[segment].mIndex)) );
3062f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3063f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    float amplification = exp( decibels * 0.115129f); // exp( dB * ln(10) / 20 )
3064f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
30653cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGVV("VOLUME vol index=[%d %d %d], dB=[%.1f %.1f %.1f] ampl=%.5f",
3066e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            curve[segment].mIndex, volIdx,
3067e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            curve[segment+1].mIndex,
3068e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            curve[segment].mDBAttenuation,
3069cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent            decibels,
3070e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            curve[segment+1].mDBAttenuation,
3071f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            amplification);
3072f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3073f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return amplification;
3074f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3075f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3076cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
3077e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    AudioPolicyManagerBase::sDefaultVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
3078e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    {1, -49.5f}, {33, -33.5f}, {66, -17.0f}, {100, 0.0f}
3079e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent};
3080e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
3081e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
3082e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    AudioPolicyManagerBase::sDefaultMediaVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
3083e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    {1, -58.0f}, {20, -40.0f}, {60, -17.0f}, {100, 0.0f}
3084cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent};
3085cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent
3086e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
3087e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    AudioPolicyManagerBase::sSpeakerMediaVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
3088e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    {1, -56.0f}, {20, -34.0f}, {60, -11.0f}, {100, 0.0f}
3089e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent};
3090e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
3091e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
3092e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    AudioPolicyManagerBase::sSpeakerSonificationVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
3093e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    {1, -29.7f}, {33, -20.1f}, {66, -10.2f}, {100, 0.0f}
3094e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent};
3095e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
309618fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Triviconst AudioPolicyManagerBase::VolumeCurvePoint
309718fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi    AudioPolicyManagerBase::sSpeakerSonificationVolumeCurveDrc[AudioPolicyManagerBase::VOLCNT] = {
309818fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi    {1, -35.7f}, {33, -26.1f}, {66, -13.2f}, {100, 0.0f}
309918fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi};
310018fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi
3101ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent// AUDIO_STREAM_SYSTEM, AUDIO_STREAM_ENFORCED_AUDIBLE and AUDIO_STREAM_DTMF volume tracks
310287e28f7b352fb77f46e16ebcdd85cbf01396a203Eric Laurent// AUDIO_STREAM_RING on phones and AUDIO_STREAM_MUSIC on tablets.
310387e28f7b352fb77f46e16ebcdd85cbf01396a203Eric Laurent// AUDIO_STREAM_DTMF tracks AUDIO_STREAM_VOICE_CALL while in call (See AudioService.java).
3104123897874418f9f0e48bb89386d8c470e6975f28Jean-Michel Trivi// The range is constrained between -24dB and -6dB over speaker and -30dB and -18dB over headset.
310587e28f7b352fb77f46e16ebcdd85cbf01396a203Eric Laurent
3106ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
3107ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    AudioPolicyManagerBase::sDefaultSystemVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
3108123897874418f9f0e48bb89386d8c470e6975f28Jean-Michel Trivi    {1, -24.0f}, {33, -18.0f}, {66, -12.0f}, {100, -6.0f}
3109ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent};
3110ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent
3111ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
311218fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi    AudioPolicyManagerBase::sDefaultSystemVolumeCurveDrc[AudioPolicyManagerBase::VOLCNT] = {
311318fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi    {1, -34.0f}, {33, -24.0f}, {66, -15.0f}, {100, -6.0f}
311418fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi};
311518fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi
311618fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Triviconst AudioPolicyManagerBase::VolumeCurvePoint
3117ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    AudioPolicyManagerBase::sHeadsetSystemVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
31187465678e0d5711ebcd78ae47b3a76821534a23eaEric Laurent    {1, -30.0f}, {33, -26.0f}, {66, -22.0f}, {100, -18.0f}
3119ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent};
3120e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
3121e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
31220d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent    AudioPolicyManagerBase::sDefaultVoiceVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
31230d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent    {0, -42.0f}, {33, -28.0f}, {66, -14.0f}, {100, 0.0f}
31240d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent};
31250d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent
31260d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
31270d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent    AudioPolicyManagerBase::sSpeakerVoiceVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
31280d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent    {0, -24.0f}, {33, -16.0f}, {66, -8.0f}, {100, 0.0f}
31290d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent};
31300d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent
31310d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
3132ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent            *AudioPolicyManagerBase::sVolumeProfiles[AUDIO_STREAM_CNT]
3133e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent                                                   [AudioPolicyManagerBase::DEVICE_CATEGORY_CNT] = {
3134ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_VOICE_CALL
31350d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent        sDefaultVoiceVolumeCurve, // DEVICE_CATEGORY_HEADSET
31360d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent        sSpeakerVoiceVolumeCurve, // DEVICE_CATEGORY_SPEAKER
31370d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent        sDefaultVoiceVolumeCurve  // DEVICE_CATEGORY_EARPIECE
3138e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    },
3139ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_SYSTEM
3140ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET
3141ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER
3142ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultSystemVolumeCurve  // DEVICE_CATEGORY_EARPIECE
3143ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    },
3144ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_RING
3145e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
3146e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER
3147e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sDefaultVolumeCurve  // DEVICE_CATEGORY_EARPIECE
3148e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    },
3149ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_MUSIC
3150ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultMediaVolumeCurve, // DEVICE_CATEGORY_HEADSET
3151ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sSpeakerMediaVolumeCurve, // DEVICE_CATEGORY_SPEAKER
3152ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultMediaVolumeCurve  // DEVICE_CATEGORY_EARPIECE
3153ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    },
3154ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_ALARM
315512bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
315612bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER
315712bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        sDefaultVolumeCurve  // DEVICE_CATEGORY_EARPIECE
315812bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    },
3159ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_NOTIFICATION
3160e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
3161ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER
3162e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sDefaultVolumeCurve  // DEVICE_CATEGORY_EARPIECE
3163c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    },
3164ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_BLUETOOTH_SCO
31650d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent        sDefaultVoiceVolumeCurve, // DEVICE_CATEGORY_HEADSET
31660d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent        sSpeakerVoiceVolumeCurve, // DEVICE_CATEGORY_SPEAKER
31670d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent        sDefaultVoiceVolumeCurve  // DEVICE_CATEGORY_EARPIECE
3168c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    },
3169ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_ENFORCED_AUDIBLE
3170ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET
3171ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER
3172ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultSystemVolumeCurve  // DEVICE_CATEGORY_EARPIECE
3173ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    },
3174ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    {  // AUDIO_STREAM_DTMF
3175ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET
3176ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER
3177ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultSystemVolumeCurve  // DEVICE_CATEGORY_EARPIECE
3178ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    },
3179ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_TTS
3180ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultMediaVolumeCurve, // DEVICE_CATEGORY_HEADSET
3181ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sSpeakerMediaVolumeCurve, // DEVICE_CATEGORY_SPEAKER
3182ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultMediaVolumeCurve  // DEVICE_CATEGORY_EARPIECE
3183ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    },
3184e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent};
3185e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
3186e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurentvoid AudioPolicyManagerBase::initializeVolumeCurves()
3187e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent{
3188ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    for (int i = 0; i < AUDIO_STREAM_CNT; i++) {
3189e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) {
3190cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent            mStreams[i].mVolumeCurve[j] =
3191ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent                    sVolumeProfiles[i][j];
3192cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent        }
3193cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent    }
319418fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi
319518fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi    // Check availability of DRC on speaker path: if available, override some of the speaker curves
319618fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi    if (mSpeakerDrcEnabled) {
319718fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi        mStreams[AUDIO_STREAM_SYSTEM].mVolumeCurve[DEVICE_CATEGORY_SPEAKER] =
319818fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi                sDefaultSystemVolumeCurveDrc;
319918fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi        mStreams[AUDIO_STREAM_RING].mVolumeCurve[DEVICE_CATEGORY_SPEAKER] =
320018fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi                sSpeakerSonificationVolumeCurveDrc;
320118fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi        mStreams[AUDIO_STREAM_ALARM].mVolumeCurve[DEVICE_CATEGORY_SPEAKER] =
320218fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi                sSpeakerSonificationVolumeCurveDrc;
320318fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi        mStreams[AUDIO_STREAM_NOTIFICATION].mVolumeCurve[DEVICE_CATEGORY_SPEAKER] =
320418fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi                sSpeakerSonificationVolumeCurveDrc;
320518fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi    }
3206f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3207f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3208c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentfloat AudioPolicyManagerBase::computeVolume(int stream,
3209c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                            int index,
3210c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                            audio_io_handle_t output,
3211f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                                            audio_devices_t device)
3212f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3213f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    float volume = 1.0;
3214f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
3215f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    StreamDescriptor &streamDesc = mStreams[stream];
3216f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3217ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if (device == AUDIO_DEVICE_NONE) {
3218f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device = outputDesc->device();
3219f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3220f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3221f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if volume is not 0 (not muted), force media volume to max on digital output
3222f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (stream == AudioSystem::MUSIC &&
3223f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        index != mStreams[stream].mIndexMin &&
3224f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent        (device == AUDIO_DEVICE_OUT_AUX_DIGITAL ||
322503d92f4a6ec954cfa81765f00c1784e2c2ff2f49Paul McLean         device == AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET)) {
3226f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return 1.0;
3227f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3228f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3229f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    volume = volIndexToAmpl(device, streamDesc, index);
3230f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3231f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if a headset is connected, apply the following rules to ring tones and notifications
3232f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // to avoid sound level bursts in user's ears:
3233f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // - always attenuate ring tones and notifications volume by 6dB
3234f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // - if music is playing, always limit the volume to current music volume,
3235f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // with a minimum threshold at -36dB so that notification is always perceived.
323612bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    const routing_strategy stream_strategy = getStrategy((AudioSystem::stream_type)stream);
3237c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
3238c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
3239c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            AUDIO_DEVICE_OUT_WIRED_HEADSET |
3240c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) &&
324112bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        ((stream_strategy == STRATEGY_SONIFICATION)
324212bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi                || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL)
3243738207def5f691d605ae33d041116829a74513a9Eric Laurent                || (stream == AudioSystem::SYSTEM)
3244738207def5f691d605ae33d041116829a74513a9Eric Laurent                || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) &&
3245738207def5f691d605ae33d041116829a74513a9Eric Laurent                    (mForceUse[AudioSystem::FOR_SYSTEM] == AudioSystem::FORCE_NONE))) &&
3246f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        streamDesc.mCanBeMuted) {
3247f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        volume *= SONIFICATION_HEADSET_VOLUME_FACTOR;
3248f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // when the phone is ringing we must consider that music could have been paused just before
3249f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // by the music application and behave as if music was active if the last music track was
3250f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // just stopped
3251ac3cf10ef6de12e69540a1244ac7255f93fa7502Eric Laurent        if (isStreamActive(AudioSystem::MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) ||
3252ac3cf10ef6de12e69540a1244ac7255f93fa7502Eric Laurent                mLimitRingtoneVolume) {
325317a73c3394547692457299dc512b5c2312ea0344Eric Laurent            audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/);
3254c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            float musicVol = computeVolume(AudioSystem::MUSIC,
325517a73c3394547692457299dc512b5c2312ea0344Eric Laurent                               mStreams[AudioSystem::MUSIC].getVolumeIndex(musicDevice),
3256c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                               output,
325717a73c3394547692457299dc512b5c2312ea0344Eric Laurent                               musicDevice);
3258c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            float minVol = (musicVol > SONIFICATION_HEADSET_VOLUME_MIN) ?
3259c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                musicVol : SONIFICATION_HEADSET_VOLUME_MIN;
3260f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (volume > minVol) {
3261f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                volume = minVol;
32626a70518b93928d1c91457ff805e375c82d76b0e5Steve Block                ALOGV("computeVolume limiting volume to %f musicVol %f", minVol, musicVol);
3263f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
3264f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
3265f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3266f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3267f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return volume;
3268f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3269f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3270c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentstatus_t AudioPolicyManagerBase::checkAndSetVolume(int stream,
3271c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                   int index,
3272c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                   audio_io_handle_t output,
3273f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                                                   audio_devices_t device,
3274c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                   int delayMs,
3275c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                   bool force)
3276f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3277f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3278f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // do not change actual stream volume if the stream is muted
3279f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mOutputs.valueFor(output)->mMuteCount[stream] != 0) {
32803cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        ALOGVV("checkAndSetVolume() stream %d muted count %d",
32815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent              stream, mOutputs.valueFor(output)->mMuteCount[stream]);
3282f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return NO_ERROR;
3283f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3284f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3285f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // do not change in call volume if bluetooth is connected and vice versa
3286f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if ((stream == AudioSystem::VOICE_CALL && mForceUse[AudioSystem::FOR_COMMUNICATION] == AudioSystem::FORCE_BT_SCO) ||
3287f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        (stream == AudioSystem::BLUETOOTH_SCO && mForceUse[AudioSystem::FOR_COMMUNICATION] != AudioSystem::FORCE_BT_SCO)) {
32886a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
3289f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin             stream, mForceUse[AudioSystem::FOR_COMMUNICATION]);
3290f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return INVALID_OPERATION;
3291f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3292f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3293f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    float volume = computeVolume(stream, index, output, device);
3294f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // We actually change the volume if:
3295f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // - the float value returned by computeVolume() changed
3296f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // - the force flag is set
3297f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (volume != mOutputs.valueFor(output)->mCurVolume[stream] ||
3298f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            force) {
3299f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mOutputs.valueFor(output)->mCurVolume[stream] = volume;
33003cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        ALOGVV("checkAndSetVolume() for output %d stream %d, volume %f, delay %d", output, stream, volume, delayMs);
33010d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent        // Force VOICE_CALL to track BLUETOOTH_SCO stream volume when bluetooth audio is
33020d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent        // enabled
33030d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent        if (stream == AudioSystem::BLUETOOTH_SCO) {
33040d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent            mpClientInterface->setStreamVolume(AudioSystem::VOICE_CALL, volume, output, delayMs);
3305f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
3306f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mpClientInterface->setStreamVolume((AudioSystem::stream_type)stream, volume, output, delayMs);
3307f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3308f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3309f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (stream == AudioSystem::VOICE_CALL ||
3310f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        stream == AudioSystem::BLUETOOTH_SCO) {
3311f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        float voiceVolume;
3312f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // Force voice volume to max for bluetooth SCO as volume is managed by the headset
3313f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (stream == AudioSystem::VOICE_CALL) {
3314f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            voiceVolume = (float)index/(float)mStreams[stream].mIndexMax;
3315f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        } else {
3316f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            voiceVolume = 1.0;
3317f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
3318f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3319b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        if (voiceVolume != mLastVoiceVolume && output == mPrimaryOutput) {
3320f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
3321f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mLastVoiceVolume = voiceVolume;
3322f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
3323f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3324f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3325f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
3326f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3327f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3328c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentvoid AudioPolicyManagerBase::applyStreamVolumes(audio_io_handle_t output,
3329f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                                                audio_devices_t device,
3330c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                int delayMs,
3331c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                bool force)
3332f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
33333cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGVV("applyStreamVolumes() for output %d and device %x", output, device);
3334f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3335f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
3336c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        checkAndSetVolume(stream,
3337f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                          mStreams[stream].getVolumeIndex(device),
3338c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                          output,
3339c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                          device,
3340c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                          delayMs,
3341c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                          force);
3342f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3343f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3344f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
334501e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurentvoid AudioPolicyManagerBase::setStrategyMute(routing_strategy strategy,
334601e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                             bool on,
334701e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                             audio_io_handle_t output,
334801e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                             int delayMs,
334901e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                             audio_devices_t device)
3350f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
33513cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGVV("setStrategyMute() strategy %d, mute %d, output %d", strategy, on, output);
3352f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
3353f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (getStrategy((AudioSystem::stream_type)stream) == strategy) {
335401e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent            setStreamMute(stream, on, output, delayMs, device);
3355f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
3356f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3357f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3358f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
335901e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurentvoid AudioPolicyManagerBase::setStreamMute(int stream,
336001e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                           bool on,
336101e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                           audio_io_handle_t output,
336201e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                           int delayMs,
336301e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                           audio_devices_t device)
3364f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3365f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    StreamDescriptor &streamDesc = mStreams[stream];
3366f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
3367ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if (device == AUDIO_DEVICE_NONE) {
336801e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent        device = outputDesc->device();
336901e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent    }
3370f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
33713cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGVV("setStreamMute() stream %d, mute %d, output %d, mMuteCount %d device %04x",
337201e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent          stream, on, output, outputDesc->mMuteCount[stream], device);
3373f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3374f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (on) {
3375f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (outputDesc->mMuteCount[stream] == 0) {
3376738207def5f691d605ae33d041116829a74513a9Eric Laurent            if (streamDesc.mCanBeMuted &&
3377738207def5f691d605ae33d041116829a74513a9Eric Laurent                    ((stream != AudioSystem::ENFORCED_AUDIBLE) ||
3378738207def5f691d605ae33d041116829a74513a9Eric Laurent                     (mForceUse[AudioSystem::FOR_SYSTEM] == AudioSystem::FORCE_NONE))) {
3379c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                checkAndSetVolume(stream, 0, output, device, delayMs);
3380f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
3381f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
3382f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
3383f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mMuteCount[stream]++;
3384f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
3385f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (outputDesc->mMuteCount[stream] == 0) {
33863cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            ALOGV("setStreamMute() unmuting non muted stream!");
3387f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return;
3388f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
3389f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (--outputDesc->mMuteCount[stream] == 0) {
3390c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            checkAndSetVolume(stream,
3391c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                              streamDesc.getVolumeIndex(device),
3392c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                              output,
3393c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                              device,
3394c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                              delayMs);
3395f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
3396f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3397f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3398f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3399f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::handleIncallSonification(int stream, bool starting, bool stateChange)
3400f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3401f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if the stream pertains to sonification strategy and we are in call we must
3402f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // mute the stream if it is low visibility. If it is high visibility, we must play a tone
3403f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // in the device used for phone strategy and play the tone if the selected device does not
3404f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // interfere with the device used for phone strategy
3405f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
3406f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // many times as there are active tracks on the output
340712bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    const routing_strategy stream_strategy = getStrategy((AudioSystem::stream_type)stream);
340812bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    if ((stream_strategy == STRATEGY_SONIFICATION) ||
340912bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi            ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
3410b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        AudioOutputDescriptor *outputDesc = mOutputs.valueFor(mPrimaryOutput);
34116a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
3412f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                stream, starting, outputDesc->mDevice, stateChange);
3413f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (outputDesc->mRefCount[stream]) {
3414f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            int muteCount = 1;
3415f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (stateChange) {
3416f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                muteCount = outputDesc->mRefCount[stream];
3417f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
3418f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (AudioSystem::isLowVisibility((AudioSystem::stream_type)stream)) {
34196a70518b93928d1c91457ff805e375c82d76b0e5Steve Block                ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
3420f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                for (int i = 0; i < muteCount; i++) {
3421b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                    setStreamMute(stream, starting, mPrimaryOutput);
3422f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
3423f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            } else {
34246a70518b93928d1c91457ff805e375c82d76b0e5Steve Block                ALOGV("handleIncallSonification() high visibility");
34255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                if (outputDesc->device() &
34265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                        getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
34276a70518b93928d1c91457ff805e375c82d76b0e5Steve Block                    ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
3428f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    for (int i = 0; i < muteCount; i++) {
3429b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                        setStreamMute(stream, starting, mPrimaryOutput);
3430f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    }
3431f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
3432f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (starting) {
3433f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    mpClientInterface->startTone(ToneGenerator::TONE_SUP_CALL_WAITING, AudioSystem::VOICE_CALL);
3434f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else {
3435f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    mpClientInterface->stopTone();
3436f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
3437f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
3438f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
3439f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3440f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3441f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3442f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinbool AudioPolicyManagerBase::isInCall()
3443f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3444f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return isStateInCall(mPhoneState);
3445f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3446f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3447f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinbool AudioPolicyManagerBase::isStateInCall(int state) {
3448f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return ((state == AudioSystem::MODE_IN_CALL) ||
3449f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            (state == AudioSystem::MODE_IN_COMMUNICATION));
3450f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3451f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3452f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinuint32_t AudioPolicyManagerBase::getMaxEffectsCpuLoad()
3453f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3454f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return MAX_EFFECTS_CPU_LOAD;
3455f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3456f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3457f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinuint32_t AudioPolicyManagerBase::getMaxEffectsMemory()
3458f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3459f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return MAX_EFFECTS_MEMORY;
3460f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3461f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3462f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// --- AudioOutputDescriptor class implementation
3463f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3464b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric LaurentAudioPolicyManagerBase::AudioOutputDescriptor::AudioOutputDescriptor(
34655ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        const IOProfile *profile)
34662d749b0bda4ae60ef1a940eeba86121b2e94d07dGlenn Kasten    : mId(0), mSamplingRate(0), mFormat(AUDIO_FORMAT_DEFAULT),
34672c3d2379ee6eadb32b63bf6de538cfd6487e8e20Glenn Kasten      mChannelMask(0), mLatency(0),
3468ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    mFlags((audio_output_flags_t)0), mDevice(AUDIO_DEVICE_NONE),
34695a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent    mOutput1(0), mOutput2(0), mProfile(profile), mDirectOpenCount(0)
3470f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3471f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // clear usage count for all stream types
3472f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) {
3473f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mRefCount[i] = 0;
3474f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mCurVolume[i] = -1.0;
3475f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mMuteCount[i] = 0;
3476f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mStopTime[i] = 0;
3477f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
347885ad78f27ca032e90af0f2100659d12c16322c76Marco Nelissen    for (int i = 0; i < NUM_STRATEGIES; i++) {
347985ad78f27ca032e90af0f2100659d12c16322c76Marco Nelissen        mStrategyMutedByDevice[i] = false;
348085ad78f27ca032e90af0f2100659d12c16322c76Marco Nelissen    }
34813cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    if (profile != NULL) {
34823cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        mSamplingRate = profile->mSamplingRates[0];
34833cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        mFormat = profile->mFormats[0];
34843cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        mChannelMask = profile->mChannelMasks[0];
34853cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        mFlags = profile->mFlags;
34863cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    }
3487f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3488f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3489dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Triviaudio_devices_t AudioPolicyManagerBase::AudioOutputDescriptor::device() const
3490f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3491f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (isDuplicated()) {
3492f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent        return (audio_devices_t)(mOutput1->mDevice | mOutput2->mDevice);
3493f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
3494f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent        return mDevice;
3495f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3496f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3497f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
34985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentuint32_t AudioPolicyManagerBase::AudioOutputDescriptor::latency()
34995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
35005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (isDuplicated()) {
35015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return (mOutput1->mLatency > mOutput2->mLatency) ? mOutput1->mLatency : mOutput2->mLatency;
35025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    } else {
35035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return mLatency;
35045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
35055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
35065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
35075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentbool AudioPolicyManagerBase::AudioOutputDescriptor::sharesHwModuleWith(
35085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        const AudioOutputDescriptor *outputDesc)
35095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
35105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (isDuplicated()) {
35115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return mOutput1->sharesHwModuleWith(outputDesc) || mOutput2->sharesHwModuleWith(outputDesc);
35125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    } else if (outputDesc->isDuplicated()){
35135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return sharesHwModuleWith(outputDesc->mOutput1) || sharesHwModuleWith(outputDesc->mOutput2);
35145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    } else {
351570c236c9290732782d5267935af1475b8d5ae602Eric Laurent        return (mProfile->mModule == outputDesc->mProfile->mModule);
35165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
35175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
35185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
3519f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::AudioOutputDescriptor::changeRefCount(AudioSystem::stream_type stream, int delta)
3520f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3521f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // forward usage count change to attached outputs
3522f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (isDuplicated()) {
3523f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mOutput1->changeRefCount(stream, delta);
3524f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mOutput2->changeRefCount(stream, delta);
3525f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3526f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if ((delta + (int)mRefCount[stream]) < 0) {
352764cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("changeRefCount() invalid delta %d for stream %d, refCount %d", delta, stream, mRefCount[stream]);
3528f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mRefCount[stream] = 0;
3529f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
3530f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3531f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mRefCount[stream] += delta;
35326a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("changeRefCount() stream %d, count %d", stream, mRefCount[stream]);
3533f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3534f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3535f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurentaudio_devices_t AudioPolicyManagerBase::AudioOutputDescriptor::supportedDevices()
3536b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent{
3537b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (isDuplicated()) {
3538f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent        return (audio_devices_t)(mOutput1->supportedDevices() | mOutput2->supportedDevices());
3539b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    } else {
3540b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        return mProfile->mSupportedDevices ;
3541b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    }
3542b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent}
3543b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
354442fa8215a763822c0d03fc936e4cac1eb864c9ccEric Laurentbool AudioPolicyManagerBase::AudioOutputDescriptor::isActive(uint32_t inPastMs) const
354542fa8215a763822c0d03fc936e4cac1eb864c9ccEric Laurent{
354680f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    return isStrategyActive(NUM_STRATEGIES, inPastMs);
354780f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent}
354880f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent
354980f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurentbool AudioPolicyManagerBase::AudioOutputDescriptor::isStrategyActive(routing_strategy strategy,
355080f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                                                                       uint32_t inPastMs,
355180f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                                                                       nsecs_t sysTime) const
355280f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent{
355380f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    if ((sysTime == 0) && (inPastMs != 0)) {
355480f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent        sysTime = systemTime();
355580f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    }
355642fa8215a763822c0d03fc936e4cac1eb864c9ccEric Laurent    for (int i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) {
355780f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent        if (((getStrategy((AudioSystem::stream_type)i) == strategy) ||
355880f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                (NUM_STRATEGIES == strategy)) &&
355980f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                isStreamActive((AudioSystem::stream_type)i, inPastMs, sysTime)) {
356042fa8215a763822c0d03fc936e4cac1eb864c9ccEric Laurent            return true;
356142fa8215a763822c0d03fc936e4cac1eb864c9ccEric Laurent        }
356242fa8215a763822c0d03fc936e4cac1eb864c9ccEric Laurent    }
356342fa8215a763822c0d03fc936e4cac1eb864c9ccEric Laurent    return false;
356442fa8215a763822c0d03fc936e4cac1eb864c9ccEric Laurent}
356542fa8215a763822c0d03fc936e4cac1eb864c9ccEric Laurent
356680f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurentbool AudioPolicyManagerBase::AudioOutputDescriptor::isStreamActive(AudioSystem::stream_type stream,
356780f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                                                                       uint32_t inPastMs,
356880f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                                                                       nsecs_t sysTime) const
356980f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent{
357080f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    if (mRefCount[stream] != 0) {
357180f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent        return true;
357280f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    }
357380f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    if (inPastMs == 0) {
357480f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent        return false;
357580f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    }
357680f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    if (sysTime == 0) {
357780f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent        sysTime = systemTime();
357880f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    }
357980f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    if (ns2ms(sysTime - mStopTime[stream]) < inPastMs) {
358080f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent        return true;
358180f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    }
358280f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    return false;
358380f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent}
358480f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent
358580f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent
3586f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::AudioOutputDescriptor::dump(int fd)
3587f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3588f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    const size_t SIZE = 256;
3589f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    char buffer[SIZE];
3590f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    String8 result;
3591f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3592f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Sampling rate: %d\n", mSamplingRate);
3593f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3594a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    snprintf(buffer, SIZE, " Format: %08x\n", mFormat);
3595f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
359670c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, " Channels: %08x\n", mChannelMask);
3597f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3598f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Latency: %d\n", mLatency);
3599f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3600f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Flags %08x\n", mFlags);
3601f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3602f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Devices %08x\n", device());
3603f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3604f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Stream volume refCount muteCount\n");
3605f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3606f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) {
3607f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        snprintf(buffer, SIZE, " %02d     %.03f     %02d       %02d\n", i, mCurVolume[i], mRefCount[i], mMuteCount[i]);
3608f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        result.append(buffer);
3609f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3610f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, result.string(), result.size());
3611f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3612f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
3613f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3614f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3615f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// --- AudioInputDescriptor class implementation
3616f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
36175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric LaurentAudioPolicyManagerBase::AudioInputDescriptor::AudioInputDescriptor(const IOProfile *profile)
3618fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    :  mId(0), mSamplingRate(0), mFormat(AUDIO_FORMAT_DEFAULT), mChannelMask(0),
3619ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent      mDevice(AUDIO_DEVICE_NONE), mRefCount(0),
36205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent      mInputSource(0), mProfile(profile)
3621f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3622fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    if (profile != NULL) {
3623fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean         mSamplingRate = profile->mSamplingRates[0];
3624fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean         mFormat = profile->mFormats[0];
3625fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean         mChannelMask = profile->mChannelMasks[0];
3626fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean     }
3627f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3628f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3629f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::AudioInputDescriptor::dump(int fd)
3630f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3631f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    const size_t SIZE = 256;
3632f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    char buffer[SIZE];
3633f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    String8 result;
3634f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3635f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Sampling rate: %d\n", mSamplingRate);
3636f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3637f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Format: %d\n", mFormat);
3638f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
363970c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, " Channels: %08x\n", mChannelMask);
3640f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3641f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Devices %08x\n", mDevice);
3642f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3643f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Ref Count %d\n", mRefCount);
3644f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3645f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, result.string(), result.size());
3646f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3647f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
3648f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3649f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3650f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// --- StreamDescriptor class implementation
3651f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3652c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric LaurentAudioPolicyManagerBase::StreamDescriptor::StreamDescriptor()
3653c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    :   mIndexMin(0), mIndexMax(1), mCanBeMuted(true)
3654c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent{
3655c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    mIndexCur.add(AUDIO_DEVICE_OUT_DEFAULT, 0);
3656c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent}
3657c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
3658c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentint AudioPolicyManagerBase::StreamDescriptor::getVolumeIndex(audio_devices_t device)
3659c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent{
3660c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    device = AudioPolicyManagerBase::getDeviceForVolume(device);
3661c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    // there is always a valid entry for AUDIO_DEVICE_OUT_DEFAULT
3662c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    if (mIndexCur.indexOfKey(device) < 0) {
3663c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        device = AUDIO_DEVICE_OUT_DEFAULT;
3664c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    }
3665c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    return mIndexCur.valueFor(device);
3666c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent}
3667c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
3668c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentvoid AudioPolicyManagerBase::StreamDescriptor::dump(int fd)
3669f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3670c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    const size_t SIZE = 256;
3671c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    char buffer[SIZE];
3672c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    String8 result;
3673c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
3674c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    snprintf(buffer, SIZE, "%s         %02d         %02d         ",
3675c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent             mCanBeMuted ? "true " : "false", mIndexMin, mIndexMax);
3676c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    result.append(buffer);
3677c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    for (size_t i = 0; i < mIndexCur.size(); i++) {
3678c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        snprintf(buffer, SIZE, "%04x : %02d, ",
3679c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                 mIndexCur.keyAt(i),
3680c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                 mIndexCur.valueAt(i));
3681c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        result.append(buffer);
3682c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    }
3683c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    result.append("\n");
3684c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
3685c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    write(fd, result.string(), result.size());
3686f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3687f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3688f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// --- EffectDescriptor class implementation
3689f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3690f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::EffectDescriptor::dump(int fd)
3691f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3692f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    const size_t SIZE = 256;
3693f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    char buffer[SIZE];
3694f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    String8 result;
3695f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
36961c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent    snprintf(buffer, SIZE, " I/O: %d\n", mIo);
3697f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3698f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Strategy: %d\n", mStrategy);
3699f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3700f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Session: %d\n", mSession);
3701f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3702f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Name: %s\n",  mDesc.name);
3703f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3704582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    snprintf(buffer, SIZE, " %s\n",  mEnabled ? "Enabled" : "Disabled");
3705582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    result.append(buffer);
3706f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, result.string(), result.size());
3707f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3708f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
3709f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3710f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
37115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent// --- IOProfile class implementation
37125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
371370c236c9290732782d5267935af1475b8d5ae602Eric LaurentAudioPolicyManagerBase::HwModule::HwModule(const char *name)
371470c236c9290732782d5267935af1475b8d5ae602Eric Laurent    : mName(strndup(name, AUDIO_HARDWARE_MODULE_ID_MAX_LEN)), mHandle(0)
371570c236c9290732782d5267935af1475b8d5ae602Eric Laurent{
371670c236c9290732782d5267935af1475b8d5ae602Eric Laurent}
371770c236c9290732782d5267935af1475b8d5ae602Eric Laurent
371870c236c9290732782d5267935af1475b8d5ae602Eric LaurentAudioPolicyManagerBase::HwModule::~HwModule()
371970c236c9290732782d5267935af1475b8d5ae602Eric Laurent{
372070c236c9290732782d5267935af1475b8d5ae602Eric Laurent    for (size_t i = 0; i < mOutputProfiles.size(); i++) {
372170c236c9290732782d5267935af1475b8d5ae602Eric Laurent         delete mOutputProfiles[i];
372270c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
372370c236c9290732782d5267935af1475b8d5ae602Eric Laurent    for (size_t i = 0; i < mInputProfiles.size(); i++) {
372470c236c9290732782d5267935af1475b8d5ae602Eric Laurent         delete mInputProfiles[i];
372570c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
372670c236c9290732782d5267935af1475b8d5ae602Eric Laurent    free((void *)mName);
372770c236c9290732782d5267935af1475b8d5ae602Eric Laurent}
372870c236c9290732782d5267935af1475b8d5ae602Eric Laurent
372970c236c9290732782d5267935af1475b8d5ae602Eric Laurentvoid AudioPolicyManagerBase::HwModule::dump(int fd)
373070c236c9290732782d5267935af1475b8d5ae602Eric Laurent{
373170c236c9290732782d5267935af1475b8d5ae602Eric Laurent    const size_t SIZE = 256;
373270c236c9290732782d5267935af1475b8d5ae602Eric Laurent    char buffer[SIZE];
373370c236c9290732782d5267935af1475b8d5ae602Eric Laurent    String8 result;
373470c236c9290732782d5267935af1475b8d5ae602Eric Laurent
373570c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "  - name: %s\n", mName);
373670c236c9290732782d5267935af1475b8d5ae602Eric Laurent    result.append(buffer);
373770c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "  - handle: %d\n", mHandle);
373870c236c9290732782d5267935af1475b8d5ae602Eric Laurent    result.append(buffer);
373970c236c9290732782d5267935af1475b8d5ae602Eric Laurent    write(fd, result.string(), result.size());
374070c236c9290732782d5267935af1475b8d5ae602Eric Laurent    if (mOutputProfiles.size()) {
37414387d30660db3c9a10b16b3a4a1c986c330e788bPatrick Tjin        write(fd, "  - outputs:\n", strlen("  - outputs:\n"));
374270c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (size_t i = 0; i < mOutputProfiles.size(); i++) {
3743c7b6e3c857c5f5af5f244d0b877c41a9c55f92abColin Cross            snprintf(buffer, SIZE, "    output %zu:\n", i);
3744599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            write(fd, buffer, strlen(buffer));
374570c236c9290732782d5267935af1475b8d5ae602Eric Laurent            mOutputProfiles[i]->dump(fd);
374670c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
374770c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
374870c236c9290732782d5267935af1475b8d5ae602Eric Laurent    if (mInputProfiles.size()) {
37494387d30660db3c9a10b16b3a4a1c986c330e788bPatrick Tjin        write(fd, "  - inputs:\n", strlen("  - inputs:\n"));
375070c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (size_t i = 0; i < mInputProfiles.size(); i++) {
3751c7b6e3c857c5f5af5f244d0b877c41a9c55f92abColin Cross            snprintf(buffer, SIZE, "    input %zu:\n", i);
3752599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            write(fd, buffer, strlen(buffer));
375370c236c9290732782d5267935af1475b8d5ae602Eric Laurent            mInputProfiles[i]->dump(fd);
375470c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
375570c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
375670c236c9290732782d5267935af1475b8d5ae602Eric Laurent}
375770c236c9290732782d5267935af1475b8d5ae602Eric Laurent
375870c236c9290732782d5267935af1475b8d5ae602Eric LaurentAudioPolicyManagerBase::IOProfile::IOProfile(HwModule *module)
37590977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent    : mFlags((audio_output_flags_t)0), mModule(module)
37605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
37615ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
37625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
37635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric LaurentAudioPolicyManagerBase::IOProfile::~IOProfile()
37645ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
376570c236c9290732782d5267935af1475b8d5ae602Eric Laurent}
376670c236c9290732782d5267935af1475b8d5ae602Eric Laurent
3767b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent// checks if the IO profile is compatible with specified parameters.
3768b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent// Sampling rate, format and channel mask must be specified in order to
3769b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent// get a valid a match
377070c236c9290732782d5267935af1475b8d5ae602Eric Laurentbool AudioPolicyManagerBase::IOProfile::isCompatibleProfile(audio_devices_t device,
377170c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                            uint32_t samplingRate,
37725082dbeb19e37883230510129c94336063a4f91cGlenn Kasten                                                            audio_format_t format,
37732c3d2379ee6eadb32b63bf6de538cfd6487e8e20Glenn Kasten                                                            audio_channel_mask_t channelMask,
37740977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent                                                            audio_output_flags_t flags) const
377570c236c9290732782d5267935af1475b8d5ae602Eric Laurent{
37762d749b0bda4ae60ef1a940eeba86121b2e94d07dGlenn Kasten    if (samplingRate == 0 || !audio_is_valid_format(format) || channelMask == 0) {
3777b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent         return false;
3778b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     }
3779b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent
3780b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     if ((mSupportedDevices & device) != device) {
3781b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent         return false;
3782b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     }
3783b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     if ((mFlags & flags) != flags) {
3784b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent         return false;
3785b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     }
3786b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     size_t i;
3787b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     for (i = 0; i < mSamplingRates.size(); i++)
3788b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     {
3789b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent         if (mSamplingRates[i] == samplingRate) {
3790b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent             break;
3791b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent         }
3792b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     }
3793b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     if (i == mSamplingRates.size()) {
3794b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent         return false;
3795b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     }
3796b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     for (i = 0; i < mFormats.size(); i++)
3797b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     {
3798b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent         if (mFormats[i] == format) {
3799b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent             break;
3800b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent         }
3801b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     }
3802b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     if (i == mFormats.size()) {
3803b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent         return false;
3804b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     }
3805b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     for (i = 0; i < mChannelMasks.size(); i++)
3806b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     {
3807b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent         if (mChannelMasks[i] == channelMask) {
3808b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent             break;
3809b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent         }
3810b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     }
3811b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     if (i == mChannelMasks.size()) {
3812b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent         return false;
3813b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     }
3814b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     return true;
38155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
38165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
38175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::IOProfile::dump(int fd)
38185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
38195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    const size_t SIZE = 256;
38205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    char buffer[SIZE];
38215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    String8 result;
38225ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
382370c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "    - sampling rates: ");
38245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    result.append(buffer);
38255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < mSamplingRates.size(); i++) {
38265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        snprintf(buffer, SIZE, "%d", mSamplingRates[i]);
38275ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        result.append(buffer);
38285ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        result.append(i == (mSamplingRates.size() - 1) ? "\n" : ", ");
38295ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
38305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
383170c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "    - channel masks: ");
38325ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    result.append(buffer);
38335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < mChannelMasks.size(); i++) {
3834a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        snprintf(buffer, SIZE, "0x%04x", mChannelMasks[i]);
38355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        result.append(buffer);
38365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        result.append(i == (mChannelMasks.size() - 1) ? "\n" : ", ");
38375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
38385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
383970c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "    - formats: ");
38405ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    result.append(buffer);
38415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < mFormats.size(); i++) {
3842a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        snprintf(buffer, SIZE, "0x%08x", mFormats[i]);
38435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        result.append(buffer);
38445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        result.append(i == (mFormats.size() - 1) ? "\n" : ", ");
38455ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
38465ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
3847a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    snprintf(buffer, SIZE, "    - devices: 0x%04x\n", mSupportedDevices);
38485ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    result.append(buffer);
3849a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    snprintf(buffer, SIZE, "    - flags: 0x%04x\n", mFlags);
38505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    result.append(buffer);
38515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
38525ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    write(fd, result.string(), result.size());
38535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
38545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
3855fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLeanvoid AudioPolicyManagerBase::IOProfile::log()
3856fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean{
3857fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    const size_t SIZE = 256;
3858fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    char buffer[SIZE];
3859fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    String8 result;
3860fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
3861fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    ALOGV("    - sampling rates: ");
3862fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    for (size_t i = 0; i < mSamplingRates.size(); i++) {
3863fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        ALOGV("  %d", mSamplingRates[i]);
3864fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    }
3865fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
3866fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    ALOGV("    - channel masks: ");
3867fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    for (size_t i = 0; i < mChannelMasks.size(); i++) {
3868fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        ALOGV("  0x%04x", mChannelMasks[i]);
3869fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    }
3870fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
3871fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    ALOGV("    - formats: ");
3872fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    for (size_t i = 0; i < mFormats.size(); i++) {
3873fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        ALOGV("  0x%08x", mFormats[i]);
3874fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    }
3875fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
3876fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    ALOGV("    - devices: 0x%04x\n", mSupportedDevices);
3877fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    ALOGV("    - flags: 0x%04x\n", mFlags);
3878fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean}
3879fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
38805ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent// --- audio_policy.conf file parsing
38815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
38825ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentstruct StringToEnum {
38835ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    const char *name;
38845ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    uint32_t value;
38855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent};
38865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
38875ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent#define STRING_TO_ENUM(string) { #string, string }
38885ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
38895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
38905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentconst struct StringToEnum sDeviceNameToEnumTable[] = {
38915ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_EARPIECE),
38925ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_SPEAKER),
38935ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_WIRED_HEADSET),
38945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_WIRED_HEADPHONE),
38955ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_ALL_SCO),
38965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_ALL_A2DP),
38975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_AUX_DIGITAL),
38985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET),
3899599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET),
3900599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_USB_DEVICE),
3901599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_USB_ACCESSORY),
3902599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_ALL_USB),
390348387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi    STRING_TO_ENUM(AUDIO_DEVICE_OUT_REMOTE_SUBMIX),
39045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_BUILTIN_MIC),
39055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET),
39065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_WIRED_HEADSET),
39075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_AUX_DIGITAL),
39085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_VOICE_CALL),
39095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_BACK_MIC),
391048387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi    STRING_TO_ENUM(AUDIO_DEVICE_IN_REMOTE_SUBMIX),
3911ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET),
3912ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_DGTL_DOCK_HEADSET),
3913ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_USB_ACCESSORY),
391426101ada7b6a34cc517b92dec1e96b17fb4a996dPeter Yoon    STRING_TO_ENUM(AUDIO_DEVICE_IN_USB_DEVICE),
391533bf1b0fe363bd4892349d160f54d860567fab12Mike Lockwood    STRING_TO_ENUM(AUDIO_DEVICE_IN_BLUETOOTH_A2DP),
39165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent};
39175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
39185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentconst struct StringToEnum sFlagNameToEnumTable[] = {
39190977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent    STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT),
39200977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent    STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY),
3921b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent    STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST),
3922b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent    STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER),
3923b4d07b97d23cfaffe22c7859ad7c45e168a7df0eRichard Fitzgerald    STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD),
3924b4d07b97d23cfaffe22c7859ad7c45e168a7df0eRichard Fitzgerald    STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NON_BLOCKING),
39255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent};
39265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
39275ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentconst struct StringToEnum sFormatNameToEnumTable[] = {
39285ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
39295ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
39309df8ecc68bdaa90aaac86b9218d728f84586660eGlenn Kasten    STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
39319df8ecc68bdaa90aaac86b9218d728f84586660eGlenn Kasten    STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_24_BIT),
39329df8ecc68bdaa90aaac86b9218d728f84586660eGlenn Kasten    STRING_TO_ENUM(AUDIO_FORMAT_PCM_FLOAT),
39339df8ecc68bdaa90aaac86b9218d728f84586660eGlenn Kasten    STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
39345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_FORMAT_MP3),
39355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_FORMAT_AAC),
39365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
3937d97aa170de2e0d37aa4cf5305a72939ea7639d88Eric Laurent    STRING_TO_ENUM(AUDIO_FORMAT_HE_AAC_V1),
3938d97aa170de2e0d37aa4cf5305a72939ea7639d88Eric Laurent    STRING_TO_ENUM(AUDIO_FORMAT_HE_AAC_V2),
3939d97aa170de2e0d37aa4cf5305a72939ea7639d88Eric Laurent    STRING_TO_ENUM(AUDIO_FORMAT_OPUS),
3940d97aa170de2e0d37aa4cf5305a72939ea7639d88Eric Laurent    STRING_TO_ENUM(AUDIO_FORMAT_AC3),
3941d97aa170de2e0d37aa4cf5305a72939ea7639d88Eric Laurent    STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
39425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent};
39435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
39445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentconst struct StringToEnum sOutChannelsNameToEnumTable[] = {
39455ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_OUT_MONO),
39465ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
39475ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
39485ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
39495ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent};
39505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
39515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentconst struct StringToEnum sInChannelsNameToEnumTable[] = {
39525ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_IN_MONO),
39535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_IN_STEREO),
395460758e27a4be8fc9ac1180f8a4055234e1702cc9Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_IN_FRONT_BACK),
39555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent};
39565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
39575ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
39585ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentuint32_t AudioPolicyManagerBase::stringToEnum(const struct StringToEnum *table,
39595ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                              size_t size,
39605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                              const char *name)
39615ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
39625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < size; i++) {
39635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strcmp(table[i].name, name) == 0) {
39645ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("stringToEnum() found %s", table[i].name);
39655ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            return table[i].value;
39665ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
39675ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
39685ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return 0;
39695ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
39705ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
397118fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivibool AudioPolicyManagerBase::stringToBool(const char *value)
397218fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi{
397318fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi    return ((strcasecmp("true", value) == 0) || (strcmp("1", value) == 0));
397418fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi}
397518fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi
39760977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurentaudio_output_flags_t AudioPolicyManagerBase::parseFlagNames(char *name)
39775ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
39785ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    uint32_t flag = 0;
39795ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
39805ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // it is OK to cast name to non const here as we are not going to use it after
39815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // strtok() modifies it
39825ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    char *flagName = strtok(name, "|");
39835ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (flagName != NULL) {
39845ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strlen(flagName) != 0) {
39855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            flag |= stringToEnum(sFlagNameToEnumTable,
39865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                               ARRAY_SIZE(sFlagNameToEnumTable),
39875ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                               flagName);
39885ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
39895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        flagName = strtok(NULL, "|");
39905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
3991a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    //force direct flag if offload flag is set: offloading implies a direct output stream
3992a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    // and all common behaviors are driven by checking only the direct flag
3993a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    // this should normally be set appropriately in the policy configuration file
3994a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    if ((flag & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
3995a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        flag |= AUDIO_OUTPUT_FLAG_DIRECT;
3996a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    }
3997a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald
39980977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent    return (audio_output_flags_t)flag;
39995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
40005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
40015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentaudio_devices_t AudioPolicyManagerBase::parseDeviceNames(char *name)
40025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
40035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    uint32_t device = 0;
40045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
40055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    char *devName = strtok(name, "|");
40065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (devName != NULL) {
40075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strlen(devName) != 0) {
40085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device |= stringToEnum(sDeviceNameToEnumTable,
40095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                 ARRAY_SIZE(sDeviceNameToEnumTable),
40105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                 devName);
40115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
40125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        devName = strtok(NULL, "|");
40135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
4014c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    return device;
40155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
40165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
40175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadSamplingRates(char *name, IOProfile *profile)
40185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
40195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    char *str = strtok(name, "|");
40205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
40213cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    // by convention, "0' in the first entry in mSamplingRates indicates the supported sampling
40223cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    // rates should be read from the output stream after it is opened for the first time
40233cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) {
40243cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        profile->mSamplingRates.add(0);
40253cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        return;
40263cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    }
40273cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
40285ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (str != NULL) {
40295ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        uint32_t rate = atoi(str);
40305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (rate != 0) {
40315ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadSamplingRates() adding rate %d", rate);
40325ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mSamplingRates.add(rate);
40335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
40345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        str = strtok(NULL, "|");
40355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
40365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return;
40375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
40385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
40395ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadFormats(char *name, IOProfile *profile)
40405ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
40415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    char *str = strtok(name, "|");
40425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
40433cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    // by convention, "0' in the first entry in mFormats indicates the supported formats
40443cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    // should be read from the output stream after it is opened for the first time
40453cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) {
40462d749b0bda4ae60ef1a940eeba86121b2e94d07dGlenn Kasten        profile->mFormats.add(AUDIO_FORMAT_DEFAULT);
40473cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        return;
40483cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    }
40493cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
40505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (str != NULL) {
40515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_format_t format = (audio_format_t)stringToEnum(sFormatNameToEnumTable,
40525ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                             ARRAY_SIZE(sFormatNameToEnumTable),
40535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                             str);
40542d749b0bda4ae60ef1a940eeba86121b2e94d07dGlenn Kasten        if (format != AUDIO_FORMAT_DEFAULT) {
40555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mFormats.add(format);
40565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
40575ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        str = strtok(NULL, "|");
40585ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
40595ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return;
40605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
40615ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
40625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadInChannels(char *name, IOProfile *profile)
40635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
40645ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    const char *str = strtok(name, "|");
40655ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
40665ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGV("loadInChannels() %s", name);
40673cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
40683cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) {
40692c3d2379ee6eadb32b63bf6de538cfd6487e8e20Glenn Kasten        profile->mChannelMasks.add(0);
40703cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        return;
40713cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    }
40723cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
40735ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (str != NULL) {
40745ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_channel_mask_t channelMask =
40755ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                (audio_channel_mask_t)stringToEnum(sInChannelsNameToEnumTable,
40765ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                   ARRAY_SIZE(sInChannelsNameToEnumTable),
40775ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                   str);
40785ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (channelMask != 0) {
40795ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadInChannels() adding channelMask %04x", channelMask);
40805ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mChannelMasks.add(channelMask);
40815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
40825ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        str = strtok(NULL, "|");
40835ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
40845ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return;
40855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
40865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
40875ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadOutChannels(char *name, IOProfile *profile)
40885ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
40895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    const char *str = strtok(name, "|");
40903cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
40913cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGV("loadOutChannels() %s", name);
40923cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
40933cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    // by convention, "0' in the first entry in mChannelMasks indicates the supported channel
40943cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    // masks should be read from the output stream after it is opened for the first time
40953cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) {
40962c3d2379ee6eadb32b63bf6de538cfd6487e8e20Glenn Kasten        profile->mChannelMasks.add(0);
40973cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        return;
40983cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    }
40995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
41005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (str != NULL) {
41013cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        audio_channel_mask_t channelMask =
41023cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                (audio_channel_mask_t)stringToEnum(sOutChannelsNameToEnumTable,
41033cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                   ARRAY_SIZE(sOutChannelsNameToEnumTable),
41043cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                   str);
41055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (channelMask != 0) {
41065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mChannelMasks.add(channelMask);
41075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
41085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        str = strtok(NULL, "|");
41095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
41105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return;
41115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
41125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
411370c236c9290732782d5267935af1475b8d5ae602Eric Laurentstatus_t AudioPolicyManagerBase::loadInput(cnode *root, HwModule *module)
41145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
41155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    cnode *node = root->first_child;
41165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
41175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    IOProfile *profile = new IOProfile(module);
41185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
41195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (node) {
41205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
41215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            loadSamplingRates((char *)node->value, profile);
41225ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, FORMATS_TAG) == 0) {
41235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            loadFormats((char *)node->value, profile);
41245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, CHANNELS_TAG) == 0) {
41255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            loadInChannels((char *)node->value, profile);
41265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, DEVICES_TAG) == 0) {
41275ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mSupportedDevices = parseDeviceNames((char *)node->value);
41285ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
41295ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        node = node->next;
41305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
4131ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    ALOGW_IF(profile->mSupportedDevices == AUDIO_DEVICE_NONE,
41325ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadInput() invalid supported devices");
41335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGW_IF(profile->mChannelMasks.size() == 0,
41345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadInput() invalid supported channel masks");
41355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGW_IF(profile->mSamplingRates.size() == 0,
41365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadInput() invalid supported sampling rates");
41375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGW_IF(profile->mFormats.size() == 0,
41385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadInput() invalid supported formats");
4139ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if ((profile->mSupportedDevices != AUDIO_DEVICE_NONE) &&
41405ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            (profile->mChannelMasks.size() != 0) &&
41415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            (profile->mSamplingRates.size() != 0) &&
41425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            (profile->mFormats.size() != 0)) {
41435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
4144fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        ALOGV("loadInput() adding input mSupportedDevices 0x%X", profile->mSupportedDevices);
41455ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
414670c236c9290732782d5267935af1475b8d5ae602Eric Laurent        module->mInputProfiles.add(profile);
41475ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return NO_ERROR;
41485ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    } else {
41495ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        delete profile;
41505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return BAD_VALUE;
41515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
41525ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
41535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
415470c236c9290732782d5267935af1475b8d5ae602Eric Laurentstatus_t AudioPolicyManagerBase::loadOutput(cnode *root, HwModule *module)
41555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
41565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    cnode *node = root->first_child;
41575ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
41585ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    IOProfile *profile = new IOProfile(module);
41595ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
41605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (node) {
41615ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
41625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            loadSamplingRates((char *)node->value, profile);
41635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, FORMATS_TAG) == 0) {
41645ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            loadFormats((char *)node->value, profile);
41655ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, CHANNELS_TAG) == 0) {
41665ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            loadOutChannels((char *)node->value, profile);
41675ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, DEVICES_TAG) == 0) {
41685ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mSupportedDevices = parseDeviceNames((char *)node->value);
41695ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, FLAGS_TAG) == 0) {
41705ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mFlags = parseFlagNames((char *)node->value);
41715ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
41725ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        node = node->next;
41735ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
4174ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    ALOGW_IF(profile->mSupportedDevices == AUDIO_DEVICE_NONE,
41755ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadOutput() invalid supported devices");
41765ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGW_IF(profile->mChannelMasks.size() == 0,
41775ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadOutput() invalid supported channel masks");
41785ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGW_IF(profile->mSamplingRates.size() == 0,
41795ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadOutput() invalid supported sampling rates");
41805ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGW_IF(profile->mFormats.size() == 0,
41815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadOutput() invalid supported formats");
4182ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if ((profile->mSupportedDevices != AUDIO_DEVICE_NONE) &&
41835ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            (profile->mChannelMasks.size() != 0) &&
41845ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            (profile->mSamplingRates.size() != 0) &&
41855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            (profile->mFormats.size() != 0)) {
41865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
41875ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        ALOGV("loadOutput() adding output mSupportedDevices %04x, mFlags %04x",
41885ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent              profile->mSupportedDevices, profile->mFlags);
41895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
419070c236c9290732782d5267935af1475b8d5ae602Eric Laurent        module->mOutputProfiles.add(profile);
41915ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return NO_ERROR;
41925ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    } else {
41935ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        delete profile;
41945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return BAD_VALUE;
41955ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
41965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
41975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
41985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadHwModule(cnode *root)
41995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
42005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    cnode *node = config_find(root, OUTPUTS_TAG);
42015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    status_t status = NAME_NOT_FOUND;
420270c236c9290732782d5267935af1475b8d5ae602Eric Laurent
420370c236c9290732782d5267935af1475b8d5ae602Eric Laurent    HwModule *module = new HwModule(root->name);
420470c236c9290732782d5267935af1475b8d5ae602Eric Laurent
42055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (node != NULL) {
42065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strcmp(root->name, AUDIO_HARDWARE_MODULE_ID_A2DP) == 0) {
42075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            mHasA2dp = true;
4208599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent        } else if (strcmp(root->name, AUDIO_HARDWARE_MODULE_ID_USB) == 0) {
4209599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            mHasUsb = true;
421048387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi        } else if (strcmp(root->name, AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX) == 0) {
421148387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            mHasRemoteSubmix = true;
42125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
4213599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent
42145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        node = node->first_child;
42155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        while (node) {
42165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadHwModule() loading output %s", node->name);
421770c236c9290732782d5267935af1475b8d5ae602Eric Laurent            status_t tmpStatus = loadOutput(node, module);
42185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            if (status == NAME_NOT_FOUND || status == NO_ERROR) {
42195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                status = tmpStatus;
42205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
42215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            node = node->next;
42225ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
42235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
42245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    node = config_find(root, INPUTS_TAG);
42255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (node != NULL) {
42265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        node = node->first_child;
42275ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        while (node) {
42285ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadHwModule() loading input %s", node->name);
422970c236c9290732782d5267935af1475b8d5ae602Eric Laurent            status_t tmpStatus = loadInput(node, module);
42305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            if (status == NAME_NOT_FOUND || status == NO_ERROR) {
42315ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                status = tmpStatus;
42325ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
42335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            node = node->next;
42345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
42355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
42365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (status == NO_ERROR) {
423770c236c9290732782d5267935af1475b8d5ae602Eric Laurent        mHwModules.add(module);
423870c236c9290732782d5267935af1475b8d5ae602Eric Laurent    } else {
423970c236c9290732782d5267935af1475b8d5ae602Eric Laurent        delete module;
42405ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
42415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
42425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
42435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadHwModules(cnode *root)
42445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
42455ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    cnode *node = config_find(root, AUDIO_HW_MODULE_TAG);
42465ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (node == NULL) {
42475ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return;
42485ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
42495ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
42505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    node = node->first_child;
42515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (node) {
42525ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        ALOGV("loadHwModules() loading module %s", node->name);
42535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        loadHwModule(node);
42545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        node = node->next;
42555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
42565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
42575ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
42585ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadGlobalConfig(cnode *root)
42595ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
42605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    cnode *node = config_find(root, GLOBAL_CONFIG_TAG);
42615ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (node == NULL) {
42625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return;
42635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
42645ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    node = node->first_child;
42655ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (node) {
42665ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strcmp(ATTACHED_OUTPUT_DEVICES_TAG, node->name) == 0) {
42675ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            mAttachedOutputDevices = parseDeviceNames((char *)node->value);
4268ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            ALOGW_IF(mAttachedOutputDevices == AUDIO_DEVICE_NONE,
4269c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                    "loadGlobalConfig() no attached output devices");
42705ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadGlobalConfig() mAttachedOutputDevices %04x", mAttachedOutputDevices);
42715ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(DEFAULT_OUTPUT_DEVICE_TAG, node->name) == 0) {
42725ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            mDefaultOutputDevice = (audio_devices_t)stringToEnum(sDeviceNameToEnumTable,
42735ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                              ARRAY_SIZE(sDeviceNameToEnumTable),
42745ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                              (char *)node->value);
4275ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            ALOGW_IF(mDefaultOutputDevice == AUDIO_DEVICE_NONE,
4276c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                    "loadGlobalConfig() default device not specified");
42775ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadGlobalConfig() mDefaultOutputDevice %04x", mDefaultOutputDevice);
42785ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(ATTACHED_INPUT_DEVICES_TAG, node->name) == 0) {
4279ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            mAvailableInputDevices = parseDeviceNames((char *)node->value) & ~AUDIO_DEVICE_BIT_IN;
42805ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadGlobalConfig() mAvailableInputDevices %04x", mAvailableInputDevices);
428118fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi        } else if (strcmp(SPEAKER_DRC_ENABLED_TAG, node->name) == 0) {
428218fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi            mSpeakerDrcEnabled = stringToBool((char *)node->value);
428318fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi            ALOGV("loadGlobalConfig() mSpeakerDrcEnabled = %d", mSpeakerDrcEnabled);
42845ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
42855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        node = node->next;
42865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
42875ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
42885ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
42895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentstatus_t AudioPolicyManagerBase::loadAudioPolicyConfig(const char *path)
42905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
42915ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    cnode *root;
42925ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    char *data;
42935ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
42945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    data = (char *)load_file(path, NULL);
42955ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (data == NULL) {
42965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return -ENODEV;
42975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
42985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    root = config_node("", "");
42995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    config_load(root, data);
43005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
43015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    loadGlobalConfig(root);
43025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    loadHwModules(root);
43035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
43045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    config_free(root);
43055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    free(root);
43065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    free(data);
43075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
43085ec145df7708564d385fd3fb764085321cf4c253Dima Zavin    ALOGI("loadAudioPolicyConfig() loaded %s\n", path);
43095ec145df7708564d385fd3fb764085321cf4c253Dima Zavin
43105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return NO_ERROR;
43115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
4312f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
4313739022f26a7127ba76a98dda65411496086114a7Dima Zavinvoid AudioPolicyManagerBase::defaultAudioPolicyConfig(void)
4314739022f26a7127ba76a98dda65411496086114a7Dima Zavin{
4315739022f26a7127ba76a98dda65411496086114a7Dima Zavin    HwModule *module;
4316739022f26a7127ba76a98dda65411496086114a7Dima Zavin    IOProfile *profile;
4317739022f26a7127ba76a98dda65411496086114a7Dima Zavin
4318739022f26a7127ba76a98dda65411496086114a7Dima Zavin    mDefaultOutputDevice = AUDIO_DEVICE_OUT_SPEAKER;
4319739022f26a7127ba76a98dda65411496086114a7Dima Zavin    mAttachedOutputDevices = AUDIO_DEVICE_OUT_SPEAKER;
4320ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    mAvailableInputDevices = AUDIO_DEVICE_IN_BUILTIN_MIC & ~AUDIO_DEVICE_BIT_IN;
4321739022f26a7127ba76a98dda65411496086114a7Dima Zavin
4322739022f26a7127ba76a98dda65411496086114a7Dima Zavin    module = new HwModule("primary");
4323739022f26a7127ba76a98dda65411496086114a7Dima Zavin
4324739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile = new IOProfile(module);
4325739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mSamplingRates.add(44100);
4326739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
4327739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mChannelMasks.add(AUDIO_CHANNEL_OUT_STEREO);
4328739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mSupportedDevices = AUDIO_DEVICE_OUT_SPEAKER;
4329739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mFlags = AUDIO_OUTPUT_FLAG_PRIMARY;
4330739022f26a7127ba76a98dda65411496086114a7Dima Zavin    module->mOutputProfiles.add(profile);
4331739022f26a7127ba76a98dda65411496086114a7Dima Zavin
4332739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile = new IOProfile(module);
4333739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mSamplingRates.add(8000);
4334739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
4335739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mChannelMasks.add(AUDIO_CHANNEL_IN_MONO);
4336739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mSupportedDevices = AUDIO_DEVICE_IN_BUILTIN_MIC;
4337739022f26a7127ba76a98dda65411496086114a7Dima Zavin    module->mInputProfiles.add(profile);
4338739022f26a7127ba76a98dda65411496086114a7Dima Zavin
4339739022f26a7127ba76a98dda65411496086114a7Dima Zavin    mHwModules.add(module);
4340739022f26a7127ba76a98dda65411496086114a7Dima Zavin}
4341f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
4342f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}; // namespace android
4343