AudioPolicyManagerBase.cpp revision 806fab660adf8a5c3d36e9411d214a4942cd67e9
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
70c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        if (!mHasA2dp && audio_is_a2dp_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
979f4165f36d90dddc32246d0eddbf289383c3b2bbEric Laurent            if (mHasA2dp && audio_is_a2dp_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
1159f4165f36d90dddc32246d0eddbf289383c3b2bbEric Laurent            if (mHasA2dp && audio_is_a2dp_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
140c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            if (mHasA2dp && audio_is_a2dp_device(device)) {
1413cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                // handle A2DP device disconnection
142b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                mA2dpDeviceAddress = "";
143b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                mA2dpSuspended = false;
144c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            } else if (audio_is_bluetooth_sco_device(device)) {
1453cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                // handle SCO device disconnection
146b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                mScoDeviceAddress = "";
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);
207fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            }
208fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
209fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            if (checkInputsForDevice(device, state, inputs, paramStr) != NO_ERROR) {
210fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                return INVALID_OPERATION;
211fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            }
212ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            mAvailableInputDevices = mAvailableInputDevices | (device & ~AUDIO_DEVICE_BIT_IN);
213f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
214f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            break;
215f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
216f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // handle input device disconnection
217f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        case AudioSystem::DEVICE_STATE_UNAVAILABLE: {
218f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (!(mAvailableInputDevices & device)) {
21964cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block                ALOGW("setDeviceConnectionState() device not connected: %d", device);
220f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                return INVALID_OPERATION;
221f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
222fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            checkInputsForDevice(device, state, inputs, paramStr);
2235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            mAvailableInputDevices = (audio_devices_t) (mAvailableInputDevices & ~device);
224fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        } break;
225f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
226f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        default:
2275efbd421e0029d6fc44b1cc65c0e5e0d85e5161fSteve Block            ALOGE("setDeviceConnectionState() invalid state: %x", state);
228f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return BAD_VALUE;
229f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
230f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
231fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        closeAllInputs();
232f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
233f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return NO_ERROR;
234fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    } // end if is input device
235f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
23664cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block    ALOGW("setDeviceConnectionState() invalid device: %x", device);
237f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return BAD_VALUE;
238f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
239f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
240c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel TriviAudioSystem::device_connection_state AudioPolicyManagerBase::getDeviceConnectionState(audio_devices_t device,
241f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                  const char *device_address)
242f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
243f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioSystem::device_connection_state state = AudioSystem::DEVICE_STATE_UNAVAILABLE;
244f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    String8 address = String8(device_address);
245c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    if (audio_is_output_device(device)) {
246f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (device & mAvailableOutputDevices) {
247c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            if (audio_is_a2dp_device(device) &&
2485ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                (!mHasA2dp || (address != "" && mA2dpDeviceAddress != address))) {
249f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                return state;
250f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
251c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            if (audio_is_bluetooth_sco_device(device) &&
252f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                address != "" && mScoDeviceAddress != address) {
253f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                return state;
254f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
255fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            if (audio_is_usb_out_device(device) &&
256fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                (!mHasUsb || (address != "" && mUsbOutCardAndDevice != address))) {
25748387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi                ALOGE("getDeviceConnectionState() invalid device: %x", device);
25848387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi                return state;
25948387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            }
26048387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            if (audio_is_remote_submix_device((audio_devices_t)device) && !mHasRemoteSubmix) {
261599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent                return state;
262599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            }
263f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            state = AudioSystem::DEVICE_STATE_AVAILABLE;
264f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
265c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    } else if (audio_is_input_device(device)) {
266f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (device & mAvailableInputDevices) {
267f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            state = AudioSystem::DEVICE_STATE_AVAILABLE;
268f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
269f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
270f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
271f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return state;
272f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
273f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
274f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::setPhoneState(int state)
275f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2766a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("setPhoneState() state %d", state);
277ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    audio_devices_t newDevice = AUDIO_DEVICE_NONE;
278f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (state < 0 || state >= AudioSystem::NUM_MODES) {
27964cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("setPhoneState() invalid state %d", state);
280f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
281f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
282f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
283f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (state == mPhoneState ) {
28464cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("setPhoneState() setting same state %d", state);
285f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
286f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
287f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
288f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if leaving call state, handle special case of active streams
289f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // pertaining to sonification strategy see handleIncallSonification()
290f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (isInCall()) {
2916a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("setPhoneState() in call state management: new state is %d", state);
292f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
293f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            handleIncallSonification(stream, false, true);
294f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
295f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
296f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
297f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // store previous phone state for management of sonification strategy below
298f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    int oldState = mPhoneState;
299f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mPhoneState = state;
300f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    bool force = false;
301f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
302f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // are we entering or starting a call
303f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (!isStateInCall(oldState) && isStateInCall(state)) {
3046a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("  Entering call in setPhoneState()");
305f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // force routing command to audio hardware when starting a call
306f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // even if no device change is needed
307f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        force = true;
30887e28f7b352fb77f46e16ebcdd85cbf01396a203Eric Laurent        for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) {
30987e28f7b352fb77f46e16ebcdd85cbf01396a203Eric Laurent            mStreams[AUDIO_STREAM_DTMF].mVolumeCurve[j] =
31087e28f7b352fb77f46e16ebcdd85cbf01396a203Eric Laurent                    sVolumeProfiles[AUDIO_STREAM_VOICE_CALL][j];
31187e28f7b352fb77f46e16ebcdd85cbf01396a203Eric Laurent        }
312f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else if (isStateInCall(oldState) && !isStateInCall(state)) {
3136a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("  Exiting call in setPhoneState()");
314f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // force routing command to audio hardware when exiting a call
315f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // even if no device change is needed
316f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        force = true;
31787e28f7b352fb77f46e16ebcdd85cbf01396a203Eric Laurent        for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) {
31887e28f7b352fb77f46e16ebcdd85cbf01396a203Eric Laurent            mStreams[AUDIO_STREAM_DTMF].mVolumeCurve[j] =
31987e28f7b352fb77f46e16ebcdd85cbf01396a203Eric Laurent                    sVolumeProfiles[AUDIO_STREAM_DTMF][j];
32087e28f7b352fb77f46e16ebcdd85cbf01396a203Eric Laurent        }
321f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else if (isStateInCall(state) && (state != oldState)) {
3226a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("  Switching between telephony and VoIP in setPhoneState()");
323f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // force routing command to audio hardware when switching between telephony and VoIP
324f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // even if no device change is needed
325f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        force = true;
326f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
327f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
328f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // check for device and output changes triggered by new phone state
3295ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    newDevice = getNewDevice(mPrimaryOutput, false /*fromCache*/);
330f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkA2dpSuspend();
331f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForAllStrategies();
332c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    updateDevicesAndOutputs();
333f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
334b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    AudioOutputDescriptor *hwOutputDesc = mOutputs.valueFor(mPrimaryOutput);
335f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
336f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // force routing command to audio hardware when ending call
337f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // even if no device change is needed
338ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if (isStateInCall(oldState) && newDevice == AUDIO_DEVICE_NONE) {
339f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        newDevice = hwOutputDesc->device();
340f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
341f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
342f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    int delayMs = 0;
343772686310a3bc44d93591349e174a60360a3d30dSathishKumar Mani    if (isStateInCall(state)) {
34480f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent        nsecs_t sysTime = systemTime();
345772686310a3bc44d93591349e174a60360a3d30dSathishKumar Mani        for (size_t i = 0; i < mOutputs.size(); i++) {
346772686310a3bc44d93591349e174a60360a3d30dSathishKumar Mani            AudioOutputDescriptor *desc = mOutputs.valueAt(i);
34780f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent            // mute media and sonification strategies and delay device switch by the largest
34880f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent            // latency of any output where either strategy is active.
34980f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent            // This avoid sending the ring tone or music tail into the earpiece or headset.
35080f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent            if ((desc->isStrategyActive(STRATEGY_MEDIA,
35180f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                                     SONIFICATION_HEADSET_MUSIC_DELAY,
35280f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                                     sysTime) ||
35380f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                    desc->isStrategyActive(STRATEGY_SONIFICATION,
35480f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                                         SONIFICATION_HEADSET_MUSIC_DELAY,
35580f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                                         sysTime)) &&
35680f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                    (delayMs < (int)desc->mLatency*2)) {
35780f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                delayMs = desc->mLatency*2;
358772686310a3bc44d93591349e174a60360a3d30dSathishKumar Mani            }
35980f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent            setStrategyMute(STRATEGY_MEDIA, true, mOutputs.keyAt(i));
36080f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent            setStrategyMute(STRATEGY_MEDIA, false, mOutputs.keyAt(i), MUTE_TIME_MS,
36180f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
36280f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent            setStrategyMute(STRATEGY_SONIFICATION, true, mOutputs.keyAt(i));
36380f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent            setStrategyMute(STRATEGY_SONIFICATION, false, mOutputs.keyAt(i), MUTE_TIME_MS,
36480f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
365772686310a3bc44d93591349e174a60360a3d30dSathishKumar Mani        }
366772686310a3bc44d93591349e174a60360a3d30dSathishKumar Mani    }
367772686310a3bc44d93591349e174a60360a3d30dSathishKumar Mani
368f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // change routing is necessary
369b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    setOutputDevice(mPrimaryOutput, newDevice, force, delayMs);
370f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
371f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if entering in call state, handle special case of active streams
372f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // pertaining to sonification strategy see handleIncallSonification()
373f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (isStateInCall(state)) {
3746a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("setPhoneState() in call state management: new state is %d", state);
375f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
376f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            handleIncallSonification(stream, true, true);
377f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
378f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
379f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
380f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
381f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (state == AudioSystem::MODE_RINGTONE &&
382f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        isStreamActive(AudioSystem::MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
383f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mLimitRingtoneVolume = true;
384f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
385f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mLimitRingtoneVolume = false;
386f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
387f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
388f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
389f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::setForceUse(AudioSystem::force_use usage, AudioSystem::forced_config config)
390f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3916a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mPhoneState);
392f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
393f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    bool forceVolumeReeval = false;
394f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    switch(usage) {
395f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::FOR_COMMUNICATION:
396f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (config != AudioSystem::FORCE_SPEAKER && config != AudioSystem::FORCE_BT_SCO &&
397f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_NONE) {
39864cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("setForceUse() invalid config %d for FOR_COMMUNICATION", config);
399f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return;
400f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
401f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        forceVolumeReeval = true;
402f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mForceUse[usage] = config;
403f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
404f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::FOR_MEDIA:
405f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (config != AudioSystem::FORCE_HEADPHONES && config != AudioSystem::FORCE_BT_A2DP &&
406f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_WIRED_ACCESSORY &&
407f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_ANALOG_DOCK &&
4081afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent            config != AudioSystem::FORCE_DIGITAL_DOCK && config != AudioSystem::FORCE_NONE &&
40931363a9cb94e80330c335fede0b92b1953a09517Jean-Michel Trivi            config != AudioSystem::FORCE_NO_BT_A2DP) {
41064cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("setForceUse() invalid config %d for FOR_MEDIA", config);
411f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return;
412f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
413f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mForceUse[usage] = config;
414f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
415f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::FOR_RECORD:
416f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (config != AudioSystem::FORCE_BT_SCO && config != AudioSystem::FORCE_WIRED_ACCESSORY &&
417f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_NONE) {
41864cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("setForceUse() invalid config %d for FOR_RECORD", config);
419f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return;
420f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
421f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mForceUse[usage] = config;
422f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
423f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::FOR_DOCK:
424f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (config != AudioSystem::FORCE_NONE && config != AudioSystem::FORCE_BT_CAR_DOCK &&
425f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_BT_DESK_DOCK &&
426f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_WIRED_ACCESSORY &&
427f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_ANALOG_DOCK &&
428f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_DIGITAL_DOCK) {
42964cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("setForceUse() invalid config %d for FOR_DOCK", config);
430f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
431f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        forceVolumeReeval = true;
432f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mForceUse[usage] = config;
433f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
434738207def5f691d605ae33d041116829a74513a9Eric Laurent    case AudioSystem::FOR_SYSTEM:
435738207def5f691d605ae33d041116829a74513a9Eric Laurent        if (config != AudioSystem::FORCE_NONE &&
436738207def5f691d605ae33d041116829a74513a9Eric Laurent            config != AudioSystem::FORCE_SYSTEM_ENFORCED) {
437738207def5f691d605ae33d041116829a74513a9Eric Laurent            ALOGW("setForceUse() invalid config %d for FOR_SYSTEM", config);
438738207def5f691d605ae33d041116829a74513a9Eric Laurent        }
439738207def5f691d605ae33d041116829a74513a9Eric Laurent        forceVolumeReeval = true;
440738207def5f691d605ae33d041116829a74513a9Eric Laurent        mForceUse[usage] = config;
441738207def5f691d605ae33d041116829a74513a9Eric Laurent        break;
442f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    default:
44364cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("setForceUse() invalid usage %d", usage);
444f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
445f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
446f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
4475ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // check for device and output changes triggered by new force usage
448f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkA2dpSuspend();
449f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForAllStrategies();
450c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    updateDevicesAndOutputs();
4515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < mOutputs.size(); i++) {
4525ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_io_handle_t output = mOutputs.keyAt(i);
4535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_devices_t newDevice = getNewDevice(output, true /*fromCache*/);
454ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        setOutputDevice(output, newDevice, (newDevice != AUDIO_DEVICE_NONE));
455ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
4565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            applyStreamVolumes(output, newDevice, 0, true);
4575ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
458f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
459f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
460f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    audio_io_handle_t activeInput = getActiveInput();
461f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (activeInput != 0) {
462f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioInputDescriptor *inputDesc = mInputs.valueFor(activeInput);
4635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_devices_t newDevice = getDeviceForInputSource(inputDesc->mInputSource);
464ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if ((newDevice != AUDIO_DEVICE_NONE) && (newDevice != inputDesc->mDevice)) {
4656a70518b93928d1c91457ff805e375c82d76b0e5Steve Block            ALOGV("setForceUse() changing device from %x to %x for input %d",
466f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    inputDesc->mDevice, newDevice, activeInput);
467f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            inputDesc->mDevice = newDevice;
468f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            AudioParameter param = AudioParameter();
469f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            param.addInt(String8(AudioParameter::keyRouting), (int)newDevice);
470f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mpClientInterface->setParameters(activeInput, param.toString());
471f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
472f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
473f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
474f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
475f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
476f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima ZavinAudioSystem::forced_config AudioPolicyManagerBase::getForceUse(AudioSystem::force_use usage)
477f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
478f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return mForceUse[usage];
479f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
480f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
481f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::setSystemProperty(const char* property, const char* value)
482f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
4836a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("setSystemProperty() property %s, value %s", property, value);
484f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
485f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
486b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent// Find a direct output profile compatible with the parameters passed, even if the input flags do
487b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent// not explicitly request a direct output
4883cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric LaurentAudioPolicyManagerBase::IOProfile *AudioPolicyManagerBase::getProfileForDirectOutput(
4893cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                               audio_devices_t device,
49070c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                               uint32_t samplingRate,
4915082dbeb19e37883230510129c94336063a4f91cGlenn Kasten                                                               audio_format_t format,
4922c3d2379ee6eadb32b63bf6de538cfd6487e8e20Glenn Kasten                                                               audio_channel_mask_t channelMask,
4930977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent                                                               audio_output_flags_t flags)
49470c236c9290732782d5267935af1475b8d5ae602Eric Laurent{
49570c236c9290732782d5267935af1475b8d5ae602Eric Laurent    for (size_t i = 0; i < mHwModules.size(); i++) {
49670c236c9290732782d5267935af1475b8d5ae602Eric Laurent        if (mHwModules[i]->mHandle == 0) {
49770c236c9290732782d5267935af1475b8d5ae602Eric Laurent            continue;
49870c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
49970c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) {
500a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald            IOProfile *profile = mHwModules[i]->mOutputProfiles[j];
501a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald            if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
502a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                if (profile->isCompatibleProfile(device, samplingRate, format,
503a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                                           channelMask,
504a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                                           AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
505a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                    if (mAvailableOutputDevices & profile->mSupportedDevices) {
506a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                        return mHwModules[i]->mOutputProfiles[j];
507a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                    }
508a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                }
509b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent            } else {
510a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                if (profile->isCompatibleProfile(device, samplingRate, format,
5113cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                           channelMask,
5123cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                           AUDIO_OUTPUT_FLAG_DIRECT)) {
513a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                    if (mAvailableOutputDevices & profile->mSupportedDevices) {
514a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                        return mHwModules[i]->mOutputProfiles[j];
515a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                    }
516a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                }
517a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald            }
51870c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
51970c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
52070c236c9290732782d5267935af1475b8d5ae602Eric Laurent    return 0;
52170c236c9290732782d5267935af1475b8d5ae602Eric Laurent}
52270c236c9290732782d5267935af1475b8d5ae602Eric Laurent
523f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinaudio_io_handle_t AudioPolicyManagerBase::getOutput(AudioSystem::stream_type stream,
524f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    uint32_t samplingRate,
5255082dbeb19e37883230510129c94336063a4f91cGlenn Kasten                                    audio_format_t format,
5262c3d2379ee6eadb32b63bf6de538cfd6487e8e20Glenn Kasten                                    audio_channel_mask_t channelMask,
527b4d07b97d23cfaffe22c7859ad7c45e168a7df0eRichard Fitzgerald                                    AudioSystem::output_flags flags,
528b4d07b97d23cfaffe22c7859ad7c45e168a7df0eRichard Fitzgerald                                    const audio_offload_info_t *offloadInfo)
529f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
530f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    audio_io_handle_t output = 0;
531f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    uint32_t latency = 0;
532f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    routing_strategy strategy = getStrategy((AudioSystem::stream_type)stream);
5335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
534a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    ALOGV("getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x",
535a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald          device, stream, samplingRate, format, channelMask, flags);
536f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
537f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
538f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mCurOutput != 0) {
53970c236c9290732782d5267935af1475b8d5ae602Eric Laurent        ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
540f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
541f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
542f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (mTestOutputs[mCurOutput] == 0) {
5436a70518b93928d1c91457ff805e375c82d76b0e5Steve Block            ALOGV("getOutput() opening test output");
5443cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(NULL);
545f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            outputDesc->mDevice = mTestDevice;
546f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            outputDesc->mSamplingRate = mTestSamplingRate;
547f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            outputDesc->mFormat = mTestFormat;
54870c236c9290732782d5267935af1475b8d5ae602Eric Laurent            outputDesc->mChannelMask = mTestChannels;
549f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            outputDesc->mLatency = mTestLatencyMs;
5500977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent            outputDesc->mFlags = (audio_output_flags_t)(mDirectOutput ? AudioSystem::OUTPUT_FLAG_DIRECT : 0);
551f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            outputDesc->mRefCount[stream] = 0;
55270c236c9290732782d5267935af1475b8d5ae602Eric Laurent            mTestOutputs[mCurOutput] = mpClientInterface->openOutput(0, &outputDesc->mDevice,
553f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            &outputDesc->mSamplingRate,
554f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            &outputDesc->mFormat,
55570c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                            &outputDesc->mChannelMask,
556f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            &outputDesc->mLatency,
557b4d07b97d23cfaffe22c7859ad7c45e168a7df0eRichard Fitzgerald                                            outputDesc->mFlags,
558b4d07b97d23cfaffe22c7859ad7c45e168a7df0eRichard Fitzgerald                                            offloadInfo);
559f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (mTestOutputs[mCurOutput]) {
560f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                AudioParameter outputCmd = AudioParameter();
561f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                outputCmd.addInt(String8("set_id"),mCurOutput);
562f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
563f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                addOutput(mTestOutputs[mCurOutput], outputDesc);
564f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
565f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
566f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return mTestOutputs[mCurOutput];
567f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
568f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
569f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
570f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // open a direct output if required by specified parameters
571a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    //force direct flag if offload flag is set: offloading implies a direct output stream
572a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    // and all common behaviors are driven by checking only the direct flag
573a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    // this should normally be set appropriately in the policy configuration file
574a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
575a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        flags = (AudioSystem::output_flags)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
576a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    }
577a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald
578000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    // Do not allow offloading if one non offloadable effect is enabled. This prevents from
579000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    // creating an offloaded track and tearing it down immediately after start when audioflinger
580000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    // detects there is an active non offloadable effect.
581000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    // FIXME: We should check the audio session here but we do not have it in this context.
582000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    // This may prevent offloading in rare situations where effects are left active by apps
583000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    // in the background.
584000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    IOProfile *profile = NULL;
585000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
586000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent            !isNonOffloadableEffectEnabled()) {
587000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent        profile = getProfileForDirectOutput(device,
588000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent                                           samplingRate,
589000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent                                           format,
590000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent                                           channelMask,
591000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent                                           (audio_output_flags_t)flags);
592000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    }
593000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent
5943cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    if (profile != NULL) {
5955a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        AudioOutputDescriptor *outputDesc = NULL;
596f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
5975a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        for (size_t i = 0; i < mOutputs.size(); i++) {
5985a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent            AudioOutputDescriptor *desc = mOutputs.valueAt(i);
5995a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent            if (!desc->isDuplicated() && (profile == desc->mProfile)) {
6005a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent                outputDesc = desc;
6015a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent                // reuse direct output if currently open and configured with same parameters
6025a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent                if ((samplingRate == outputDesc->mSamplingRate) &&
6035a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent                        (format == outputDesc->mFormat) &&
6045a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent                        (channelMask == outputDesc->mChannelMask)) {
6055a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent                    outputDesc->mDirectOpenCount++;
606a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                    ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
6075a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent                    return mOutputs.keyAt(i);
6085a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent                }
6095a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent            }
6105a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        }
6115a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        // close direct output if currently open and configured with different parameters
6125a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        if (outputDesc != NULL) {
6135a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent            closeOutput(outputDesc->mId);
6145a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        }
6155a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        outputDesc = new AudioOutputDescriptor(profile);
616f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mDevice = device;
617f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mSamplingRate = samplingRate;
6182d749b0bda4ae60ef1a940eeba86121b2e94d07dGlenn Kasten        outputDesc->mFormat = format;
6192c3d2379ee6eadb32b63bf6de538cfd6487e8e20Glenn Kasten        outputDesc->mChannelMask = channelMask;
620f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mLatency = 0;
621a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        outputDesc->mFlags =(audio_output_flags_t) (outputDesc->mFlags | flags);
622f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mRefCount[stream] = 0;
623f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mStopTime[stream] = 0;
6245a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        outputDesc->mDirectOpenCount = 1;
6253cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        output = mpClientInterface->openOutput(profile->mModule->mHandle,
62670c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                        &outputDesc->mDevice,
627f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                        &outputDesc->mSamplingRate,
628f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                        &outputDesc->mFormat,
62970c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                        &outputDesc->mChannelMask,
630f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                        &outputDesc->mLatency,
631a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                                        outputDesc->mFlags,
632a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                                        offloadInfo);
633f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
6349f1f9b509c930830f6f32e9ef6c2c8a03d6fa96eJean-Michel Trivi        // only accept an output with the requested parameters
635f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (output == 0 ||
636f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            (samplingRate != 0 && samplingRate != outputDesc->mSamplingRate) ||
6372d749b0bda4ae60ef1a940eeba86121b2e94d07dGlenn Kasten            (format != AUDIO_FORMAT_DEFAULT && format != outputDesc->mFormat) ||
63870c236c9290732782d5267935af1475b8d5ae602Eric Laurent            (channelMask != 0 && channelMask != outputDesc->mChannelMask)) {
6393cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
6403cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    "format %d %d, channelMask %04x %04x", output, samplingRate,
6413cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
6423cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    outputDesc->mChannelMask);
643f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (output != 0) {
644f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mpClientInterface->closeOutput(output);
645f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
646f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            delete outputDesc;
647f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return 0;
648f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
64945c763947b657b347211dc9388754e05d30d0467Eric Laurent        audio_io_handle_t srcOutput = getOutputForEffect();
650f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        addOutput(output, outputDesc);
65145c763947b657b347211dc9388754e05d30d0467Eric Laurent        audio_io_handle_t dstOutput = getOutputForEffect();
65245c763947b657b347211dc9388754e05d30d0467Eric Laurent        if (dstOutput == output) {
65345c763947b657b347211dc9388754e05d30d0467Eric Laurent            mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
65445c763947b657b347211dc9388754e05d30d0467Eric Laurent        }
6555a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        mPreviousOutputs = mOutputs;
6565a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        ALOGV("getOutput() returns new direct output %d", output);
657f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return output;
658f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
659f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
6609f1f9b509c930830f6f32e9ef6c2c8a03d6fa96eJean-Michel Trivi    // ignoring channel mask due to downmix capability in mixer
6619f1f9b509c930830f6f32e9ef6c2c8a03d6fa96eJean-Michel Trivi
662f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // open a non direct output
663f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
664a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    // for non direct outputs, only PCM is supported
6652d749b0bda4ae60ef1a940eeba86121b2e94d07dGlenn Kasten    if (audio_is_linear_pcm(format)) {
666a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        // get which output is suitable for the specified stream. The actual
667a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        // routing change will happen when startOutput() will be called
668a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
669f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
670a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        output = selectOutput(outputs, flags);
671a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    }
672a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
67370c236c9290732782d5267935af1475b8d5ae602Eric Laurent            "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
674f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
6755ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGV("getOutput() returns output %d", output);
6765ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
677f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return output;
678f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
679f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
6805ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentaudio_io_handle_t AudioPolicyManagerBase::selectOutput(const SortedVector<audio_io_handle_t>& outputs,
6815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                       AudioSystem::output_flags flags)
6825ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
6835ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // select one output among several that provide a path to a particular device or set of
6845ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // devices (the list was previously build by getOutputsForDevice()).
6855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // The priority is as follows:
6865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // 1: the output with the highest number of requested policy flags
6875ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // 2: the primary output
6885ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // 3: the first output in the list
6895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
6905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (outputs.size() == 0) {
6915ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return 0;
6925ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
6935ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (outputs.size() == 1) {
6945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return outputs[0];
6955ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
6965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
6975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    int maxCommonFlags = 0;
6985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    audio_io_handle_t outputFlags = 0;
6995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    audio_io_handle_t outputPrimary = 0;
7005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
7015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < outputs.size(); i++) {
7025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        AudioOutputDescriptor *outputDesc = mOutputs.valueFor(outputs[i]);
7035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (!outputDesc->isDuplicated()) {
7045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            int commonFlags = (int)AudioSystem::popCount(outputDesc->mProfile->mFlags & flags);
7055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            if (commonFlags > maxCommonFlags) {
7065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                outputFlags = outputs[i];
7075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                maxCommonFlags = commonFlags;
7085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags);
7095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
7100977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent            if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
7115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                outputPrimary = outputs[i];
7125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
7135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
7145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
7155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
7165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (outputFlags != 0) {
7175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return outputFlags;
7185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
7195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (outputPrimary != 0) {
7205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return outputPrimary;
7215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
7225ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
7235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return outputs[0];
7245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
7255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
726f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::startOutput(audio_io_handle_t output,
727f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                             AudioSystem::stream_type stream,
728f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                             int session)
729f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
7306a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("startOutput() output %d, stream %d, session %d", output, stream, session);
731f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mOutputs.indexOfKey(output);
732f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
7336742b021061741c309d6e45c074b45a560f11086Glenn Kasten        ALOGW("startOutput() unknown output %d", output);
734f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
735f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
736f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
737f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index);
738f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
7395ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // increment usage count for this stream on the requested output:
740f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // NOTE that the usage count is the same for duplicated output and hardware output which is
7415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
742f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    outputDesc->changeRefCount(stream, 1);
743f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
7445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (outputDesc->mRefCount[stream] == 1) {
7455ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_devices_t newDevice = getNewDevice(output, false /*fromCache*/);
746b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        routing_strategy strategy = getStrategy(stream);
747b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
748b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                            (strategy == STRATEGY_SONIFICATION_RESPECTFUL);
749b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        uint32_t waitMs = 0;
7505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        bool force = false;
7515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        for (size_t i = 0; i < mOutputs.size(); i++) {
7525ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            AudioOutputDescriptor *desc = mOutputs.valueAt(i);
753b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent            if (desc != outputDesc) {
754b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                // force a device change if any other output is managed by the same hw
755b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                // module and has a current device selection that differs from selected device.
756b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                // In this case, the audio HAL must receive the new device selection so that it can
757b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                // change the device currently selected by the other active output.
758b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                if (outputDesc->sharesHwModuleWith(desc) &&
7595ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                    desc->device() != newDevice) {
760b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                    force = true;
761b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                }
762b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                // wait for audio on other active outputs to be presented when starting
763b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                // a notification so that audio focus effect can propagate.
76442fa8215a763822c0d03fc936e4cac1eb864c9ccEric Laurent                uint32_t latency = desc->latency();
76542fa8215a763822c0d03fc936e4cac1eb864c9ccEric Laurent                if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
76642fa8215a763822c0d03fc936e4cac1eb864c9ccEric Laurent                    waitMs = latency;
767b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                }
7685ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
7695ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
770b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        uint32_t muteWaitMs = setOutputDevice(output, newDevice, force);
771f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
7725ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // handle special case for sonification while in call
7735ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (isInCall()) {
7745ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            handleIncallSonification(stream, true, false);
7755ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
776c16ac09f510437e8340be691720177a490ae78f0Eric Laurent
7775ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // apply volume rules for current stream and device if necessary
7785ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        checkAndSetVolume(stream,
779c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                          mStreams[stream].getVolumeIndex(newDevice),
7805ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                          output,
7815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                          newDevice);
78212bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi
7835ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // update the outputs if starting an output with a stream that can affect notification
7845ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // routing
7855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        handleNotificationRoutingForStream(stream);
786b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        if (waitMs > muteWaitMs) {
787b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent            usleep((waitMs - muteWaitMs) * 2 * 1000);
788b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        }
7895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
790f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
791f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
792f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
7935ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
794f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::stopOutput(audio_io_handle_t output,
795f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            AudioSystem::stream_type stream,
796f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            int session)
797f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
7986a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
799f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mOutputs.indexOfKey(output);
800f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
8016742b021061741c309d6e45c074b45a560f11086Glenn Kasten        ALOGW("stopOutput() unknown output %d", output);
802f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
803f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
804f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
805f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index);
806f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
807f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // handle special case for sonification while in call
808f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (isInCall()) {
809f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        handleIncallSonification(stream, false, false);
810f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
811f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
812f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (outputDesc->mRefCount[stream] > 0) {
813f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // decrement usage count of this stream on the output
814f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->changeRefCount(stream, -1);
815f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // store time at which the stream was stopped - see isStreamActive()
8165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (outputDesc->mRefCount[stream] == 0) {
8175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            outputDesc->mStopTime[stream] = systemTime();
8185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            audio_devices_t newDevice = getNewDevice(output, false /*fromCache*/);
8195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // delay the device switch by twice the latency because stopOutput() is executed when
8205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // the track stop() command is received and at that time the audio track buffer can
8215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // still contain data that needs to be drained. The latency only covers the audio HAL
8225ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // and kernel buffers. Also the latency does not always include additional delay in the
8235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // audio path (audio DSP, CODEC ...)
8245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            setOutputDevice(output, newDevice, false, outputDesc->mLatency*2);
8255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
8265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // force restoring the device selection on other active outputs if it differs from the
8275ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // one being selected for this output
8285ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            for (size_t i = 0; i < mOutputs.size(); i++) {
8295ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                audio_io_handle_t curOutput = mOutputs.keyAt(i);
8305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                AudioOutputDescriptor *desc = mOutputs.valueAt(i);
8315ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                if (curOutput != output &&
83280f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                        desc->isActive() &&
8335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                        outputDesc->sharesHwModuleWith(desc) &&
834a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                        (newDevice != desc->device())) {
8355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                    setOutputDevice(curOutput,
8365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                    getNewDevice(curOutput, false /*fromCache*/),
8375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                    true,
8385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                    outputDesc->mLatency*2);
8395ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                }
8405ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
8415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // update the outputs if stopping one with a stream that can affect notification routing
8425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            handleNotificationRoutingForStream(stream);
843f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
844f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return NO_ERROR;
845f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
84664cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("stopOutput() refcount is already 0 for output %d", output);
847f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return INVALID_OPERATION;
848f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
849f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
850f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
851f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::releaseOutput(audio_io_handle_t output)
852f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
8536a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("releaseOutput() %d", output);
854f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mOutputs.indexOfKey(output);
855f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
85664cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("releaseOutput() releasing unknown output %d", output);
857f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
858f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
859f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
860f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
861f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    int testIndex = testOutputIndex(output);
862f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (testIndex != 0) {
863f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index);
86480f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent        if (outputDesc->isActive()) {
865f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mpClientInterface->closeOutput(output);
866f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            delete mOutputs.valueAt(index);
867f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mOutputs.removeItem(output);
868f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mTestOutputs[testIndex] = 0;
869f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
870f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
871f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
872f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
873f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
8745a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent    AudioOutputDescriptor *desc = mOutputs.valueAt(index);
8755a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent    if (desc->mFlags & AudioSystem::OUTPUT_FLAG_DIRECT) {
8765a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        if (desc->mDirectOpenCount <= 0) {
8775a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent            ALOGW("releaseOutput() invalid open count %d for output %d",
8785a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent                                                              desc->mDirectOpenCount, output);
8795a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent            return;
8805a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        }
8815a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        if (--desc->mDirectOpenCount == 0) {
8825a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent            closeOutput(output);
88345c763947b657b347211dc9388754e05d30d0467Eric Laurent            // If effects where present on the output, audioflinger moved them to the primary
88445c763947b657b347211dc9388754e05d30d0467Eric Laurent            // output by default: move them back to the appropriate output.
88545c763947b657b347211dc9388754e05d30d0467Eric Laurent            audio_io_handle_t dstOutput = getOutputForEffect();
88645c763947b657b347211dc9388754e05d30d0467Eric Laurent            if (dstOutput != mPrimaryOutput) {
88745c763947b657b347211dc9388754e05d30d0467Eric Laurent                mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, mPrimaryOutput, dstOutput);
88845c763947b657b347211dc9388754e05d30d0467Eric Laurent            }
8895a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        }
890f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
891f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
892f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
89345c763947b657b347211dc9388754e05d30d0467Eric Laurent
894f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinaudio_io_handle_t AudioPolicyManagerBase::getInput(int inputSource,
895f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    uint32_t samplingRate,
8965082dbeb19e37883230510129c94336063a4f91cGlenn Kasten                                    audio_format_t format,
8972c3d2379ee6eadb32b63bf6de538cfd6487e8e20Glenn Kasten                                    audio_channel_mask_t channelMask,
898f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    AudioSystem::audio_in_acoustics acoustics)
899f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
900f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    audio_io_handle_t input = 0;
901f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent    audio_devices_t device = getDeviceForInputSource(inputSource);
902f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
90370c236c9290732782d5267935af1475b8d5ae602Eric Laurent    ALOGV("getInput() inputSource %d, samplingRate %d, format %d, channelMask %x, acoustics %x",
90470c236c9290732782d5267935af1475b8d5ae602Eric Laurent          inputSource, samplingRate, format, channelMask, acoustics);
905f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
906ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if (device == AUDIO_DEVICE_NONE) {
9075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        ALOGW("getInput() could not find device for inputSource %d", inputSource);
908f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return 0;
909f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
910f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
911f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // adapt channel selection to input source
912f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    switch(inputSource) {
913f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_UPLINK:
9142c3d2379ee6eadb32b63bf6de538cfd6487e8e20Glenn Kasten        channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK;
915f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
916f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_DOWNLINK:
9172c3d2379ee6eadb32b63bf6de538cfd6487e8e20Glenn Kasten        channelMask = AUDIO_CHANNEL_IN_VOICE_DNLINK;
918f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
919f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_CALL:
9202c3d2379ee6eadb32b63bf6de538cfd6487e8e20Glenn Kasten        channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK | AUDIO_CHANNEL_IN_VOICE_DNLINK;
921f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
922f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    default:
923f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
924f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
925f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
9265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    IOProfile *profile = getInputProfile(device,
9275ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                         samplingRate,
9285ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                         format,
92970c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                         channelMask);
9305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (profile == NULL) {
931fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        ALOGW("getInput() could not find profile for device 0x%X, samplingRate %d, format %d, "
932fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                "channelMask 0x%X",
93370c236c9290732782d5267935af1475b8d5ae602Eric Laurent                device, samplingRate, format, channelMask);
93470c236c9290732782d5267935af1475b8d5ae602Eric Laurent        return 0;
93570c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
93670c236c9290732782d5267935af1475b8d5ae602Eric Laurent
93770c236c9290732782d5267935af1475b8d5ae602Eric Laurent    if (profile->mModule->mHandle == 0) {
9383cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        ALOGE("getInput(): HW module %s not opened", profile->mModule->mName);
9395ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return 0;
9405ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
9415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
9425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    AudioInputDescriptor *inputDesc = new AudioInputDescriptor(profile);
943f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
944f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    inputDesc->mInputSource = inputSource;
945f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    inputDesc->mDevice = device;
946f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    inputDesc->mSamplingRate = samplingRate;
9472d749b0bda4ae60ef1a940eeba86121b2e94d07dGlenn Kasten    inputDesc->mFormat = format;
9482c3d2379ee6eadb32b63bf6de538cfd6487e8e20Glenn Kasten    inputDesc->mChannelMask = channelMask;
949f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    inputDesc->mRefCount = 0;
950fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
95170c236c9290732782d5267935af1475b8d5ae602Eric Laurent    input = mpClientInterface->openInput(profile->mModule->mHandle,
95270c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                    &inputDesc->mDevice,
953f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    &inputDesc->mSamplingRate,
954f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    &inputDesc->mFormat,
95570c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                    &inputDesc->mChannelMask);
956f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
957f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // only accept input with the exact requested set of parameters
958f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (input == 0 ||
959f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        (samplingRate != inputDesc->mSamplingRate) ||
960f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        (format != inputDesc->mFormat) ||
96170c236c9290732782d5267935af1475b8d5ae602Eric Laurent        (channelMask != inputDesc->mChannelMask)) {
962fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        ALOGI("getInput() failed opening input: samplingRate %d, format %d, channelMask 0x%X",
96370c236c9290732782d5267935af1475b8d5ae602Eric Laurent                samplingRate, format, channelMask);
964f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (input != 0) {
965f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mpClientInterface->closeInput(input);
966f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
967f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        delete inputDesc;
968f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return 0;
969f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
970fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    addInput(input, inputDesc);
971fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
972f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return input;
973f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
974f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
975f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::startInput(audio_io_handle_t input)
976f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
9776a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("startInput() input %d", input);
978f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mInputs.indexOfKey(input);
979f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
9806742b021061741c309d6e45c074b45a560f11086Glenn Kasten        ALOGW("startInput() unknown input %d", input);
981f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
982f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
983f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioInputDescriptor *inputDesc = mInputs.valueAt(index);
984f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
985f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
986f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mTestInput == 0)
987f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
988f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    {
989fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent        // refuse 2 active AudioRecord clients at the same time except if the active input
990fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent        // uses AUDIO_SOURCE_HOTWORD in which case it is closed.
991fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent        audio_io_handle_t activeInput = getActiveInput();
992fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent        if (!isVirtualInputDevice(inputDesc->mDevice) && activeInput != 0) {
993fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent            AudioInputDescriptor *activeDesc = mInputs.valueFor(activeInput);
994fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent            if (activeDesc->mInputSource == AUDIO_SOURCE_HOTWORD) {
995fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent                ALOGW("startInput() preempting already started low-priority input %d", activeInput);
996fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent                stopInput(activeInput);
997fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent                releaseInput(activeInput);
998fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent            } else {
9996742b021061741c309d6e45c074b45a560f11086Glenn Kasten                ALOGW("startInput() input %d failed: other input already started", input);
1000fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent                return INVALID_OPERATION;
1001fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent            }
1002f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1003f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1004f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
100566707435156d8d99d795271a7bd54943065b4c2dEric Laurent    audio_devices_t newDevice = getDeviceForInputSource(inputDesc->mInputSource);
100666707435156d8d99d795271a7bd54943065b4c2dEric Laurent    if ((newDevice != AUDIO_DEVICE_NONE) && (newDevice != inputDesc->mDevice)) {
100766707435156d8d99d795271a7bd54943065b4c2dEric Laurent        inputDesc->mDevice = newDevice;
100866707435156d8d99d795271a7bd54943065b4c2dEric Laurent    }
1009b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown
1010b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown    // automatically enable the remote submix output when input is started
1011b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown    if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1012b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown        setDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1013b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown                AudioSystem::DEVICE_STATE_AVAILABLE, AUDIO_REMOTE_SUBMIX_DEVICE_ADDRESS);
1014b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown    }
1015b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown
1016f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioParameter param = AudioParameter();
1017f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    param.addInt(String8(AudioParameter::keyRouting), (int)inputDesc->mDevice);
1018f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1019fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent    int aliasSource = (inputDesc->mInputSource == AUDIO_SOURCE_HOTWORD) ?
1020fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent                                        AUDIO_SOURCE_VOICE_RECOGNITION : inputDesc->mInputSource;
1021fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent
1022fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent    param.addInt(String8(AudioParameter::keyInputSource), aliasSource);
10236a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("AudioPolicyManager::startInput() input source = %d", inputDesc->mInputSource);
1024f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1025f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mpClientInterface->setParameters(input, param.toString());
1026f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1027f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    inputDesc->mRefCount = 1;
1028f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
1029f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1030f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1031f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::stopInput(audio_io_handle_t input)
1032f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
10336a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("stopInput() input %d", input);
1034f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mInputs.indexOfKey(input);
1035f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
10366742b021061741c309d6e45c074b45a560f11086Glenn Kasten        ALOGW("stopInput() unknown input %d", input);
1037f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
1038f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1039f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioInputDescriptor *inputDesc = mInputs.valueAt(index);
1040f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1041f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (inputDesc->mRefCount == 0) {
104264cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("stopInput() input %d already stopped", input);
1043f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return INVALID_OPERATION;
1044f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
1045b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown        // automatically disable the remote submix output when input is stopped
1046b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown        if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1047b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown            setDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1048b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown                    AudioSystem::DEVICE_STATE_UNAVAILABLE, AUDIO_REMOTE_SUBMIX_DEVICE_ADDRESS);
1049b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown        }
1050b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown
1051f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioParameter param = AudioParameter();
1052f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        param.addInt(String8(AudioParameter::keyRouting), 0);
1053f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mpClientInterface->setParameters(input, param.toString());
1054f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        inputDesc->mRefCount = 0;
1055f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return NO_ERROR;
1056f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1057f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1058f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1059f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::releaseInput(audio_io_handle_t input)
1060f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
10616a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("releaseInput() %d", input);
1062f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mInputs.indexOfKey(input);
1063f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
106464cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("releaseInput() releasing unknown input %d", input);
1065f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
1066f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1067f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mpClientInterface->closeInput(input);
1068f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    delete mInputs.valueAt(index);
1069f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mInputs.removeItem(input);
1070fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
10716a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("releaseInput() exit");
1072f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1073f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1074fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLeanvoid AudioPolicyManagerBase::closeAllInputs() {
1075fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    for(size_t input_index = 0; input_index < mInputs.size(); input_index++) {
1076fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        mpClientInterface->closeInput(mInputs.keyAt(input_index));
1077fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    }
1078fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    mInputs.clear();
1079fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean}
1080fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
1081f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::initStreamVolume(AudioSystem::stream_type stream,
1082f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            int indexMin,
1083f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            int indexMax)
1084f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
10856a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
1086f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (indexMin < 0 || indexMin >= indexMax) {
108764cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("initStreamVolume() invalid index limits for stream %d, min %d, max %d", stream , indexMin, indexMax);
1088f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
1089f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1090f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mStreams[stream].mIndexMin = indexMin;
1091f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mStreams[stream].mIndexMax = indexMax;
1092f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1093f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1094c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentstatus_t AudioPolicyManagerBase::setStreamVolumeIndex(AudioSystem::stream_type stream,
1095c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                      int index,
1096c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                      audio_devices_t device)
1097f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1098f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1099f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if ((index < mStreams[stream].mIndexMin) || (index > mStreams[stream].mIndexMax)) {
1100f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
1101f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1102c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    if (!audio_is_output_device(device)) {
1103c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        return BAD_VALUE;
1104c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    }
1105f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1106f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // Force max volume if stream cannot be muted
1107f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (!mStreams[stream].mCanBeMuted) index = mStreams[stream].mIndexMax;
1108f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1109b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    ALOGV("setStreamVolumeIndex() stream %d, device %04x, index %d",
1110c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent          stream, device, index);
1111c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
1112c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    // if device is AUDIO_DEVICE_OUT_DEFAULT set default value and
1113c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    // clear all device specific values
1114c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    if (device == AUDIO_DEVICE_OUT_DEFAULT) {
1115c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        mStreams[stream].mIndexCur.clear();
1116c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    }
1117c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    mStreams[stream].mIndexCur.add(device, index);
1118f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1119f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // compute and apply stream volume on all outputs according to connected device
1120f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    status_t status = NO_ERROR;
1121f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mOutputs.size(); i++) {
1122c5eb8b4a5d4395ce335bc7c3e6df2678fa47e2ddEric Laurent        audio_devices_t curDevice =
1123c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                getDeviceForVolume(mOutputs.valueAt(i)->device());
1124e92d623811f3fd3e7cc5e5dd8bc93c0c0a8fdf50Eric Laurent        if ((device == AUDIO_DEVICE_OUT_DEFAULT) || (device == curDevice)) {
1125c5eb8b4a5d4395ce335bc7c3e6df2678fa47e2ddEric Laurent            status_t volStatus = checkAndSetVolume(stream, index, mOutputs.keyAt(i), curDevice);
1126c5eb8b4a5d4395ce335bc7c3e6df2678fa47e2ddEric Laurent            if (volStatus != NO_ERROR) {
1127c5eb8b4a5d4395ce335bc7c3e6df2678fa47e2ddEric Laurent                status = volStatus;
1128c5eb8b4a5d4395ce335bc7c3e6df2678fa47e2ddEric Laurent            }
1129f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1130f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1131f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return status;
1132f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1133f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1134c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentstatus_t AudioPolicyManagerBase::getStreamVolumeIndex(AudioSystem::stream_type stream,
1135c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                      int *index,
1136c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                      audio_devices_t device)
1137f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1138c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    if (index == NULL) {
1139f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
1140f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1141c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    if (!audio_is_output_device(device)) {
1142c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        return BAD_VALUE;
1143c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    }
1144c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    // if device is AUDIO_DEVICE_OUT_DEFAULT, return volume for device corresponding to
1145c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    // the strategy the stream belongs to.
1146c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    if (device == AUDIO_DEVICE_OUT_DEFAULT) {
1147c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
1148c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    }
1149c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    device = getDeviceForVolume(device);
1150c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
1151c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    *index =  mStreams[stream].getVolumeIndex(device);
1152c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index);
1153f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
1154f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1155f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
115645c763947b657b347211dc9388754e05d30d0467Eric Laurentaudio_io_handle_t AudioPolicyManagerBase::selectOutputForEffects(
115745c763947b657b347211dc9388754e05d30d0467Eric Laurent                                            const SortedVector<audio_io_handle_t>& outputs)
115845c763947b657b347211dc9388754e05d30d0467Eric Laurent{
115945c763947b657b347211dc9388754e05d30d0467Eric Laurent    // select one output among several suitable for global effects.
116045c763947b657b347211dc9388754e05d30d0467Eric Laurent    // The priority is as follows:
116145c763947b657b347211dc9388754e05d30d0467Eric Laurent    // 1: An offloaded output. If the effect ends up not being offloadable,
116245c763947b657b347211dc9388754e05d30d0467Eric Laurent    //    AudioFlinger will invalidate the track and the offloaded output
116345c763947b657b347211dc9388754e05d30d0467Eric Laurent    //    will be closed causing the effect to be moved to a PCM output.
116445c763947b657b347211dc9388754e05d30d0467Eric Laurent    // 2: A deep buffer output
116545c763947b657b347211dc9388754e05d30d0467Eric Laurent    // 3: the first output in the list
116645c763947b657b347211dc9388754e05d30d0467Eric Laurent
116745c763947b657b347211dc9388754e05d30d0467Eric Laurent    if (outputs.size() == 0) {
116845c763947b657b347211dc9388754e05d30d0467Eric Laurent        return 0;
116945c763947b657b347211dc9388754e05d30d0467Eric Laurent    }
117045c763947b657b347211dc9388754e05d30d0467Eric Laurent
117145c763947b657b347211dc9388754e05d30d0467Eric Laurent    audio_io_handle_t outputOffloaded = 0;
117245c763947b657b347211dc9388754e05d30d0467Eric Laurent    audio_io_handle_t outputDeepBuffer = 0;
117345c763947b657b347211dc9388754e05d30d0467Eric Laurent
117445c763947b657b347211dc9388754e05d30d0467Eric Laurent    for (size_t i = 0; i < outputs.size(); i++) {
117545c763947b657b347211dc9388754e05d30d0467Eric Laurent        AudioOutputDescriptor *desc = mOutputs.valueFor(outputs[i]);
1176c7b6e3c857c5f5af5f244d0b877c41a9c55f92abColin Cross        ALOGV("selectOutputForEffects outputs[%zu] flags %x", i, desc->mFlags);
117745c763947b657b347211dc9388754e05d30d0467Eric Laurent        if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
117845c763947b657b347211dc9388754e05d30d0467Eric Laurent            outputOffloaded = outputs[i];
117945c763947b657b347211dc9388754e05d30d0467Eric Laurent        }
118045c763947b657b347211dc9388754e05d30d0467Eric Laurent        if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) {
118145c763947b657b347211dc9388754e05d30d0467Eric Laurent            outputDeepBuffer = outputs[i];
118245c763947b657b347211dc9388754e05d30d0467Eric Laurent        }
118345c763947b657b347211dc9388754e05d30d0467Eric Laurent    }
118445c763947b657b347211dc9388754e05d30d0467Eric Laurent
118545c763947b657b347211dc9388754e05d30d0467Eric Laurent    ALOGV("selectOutputForEffects outputOffloaded %d outputDeepBuffer %d",
118645c763947b657b347211dc9388754e05d30d0467Eric Laurent          outputOffloaded, outputDeepBuffer);
118745c763947b657b347211dc9388754e05d30d0467Eric Laurent    if (outputOffloaded != 0) {
118845c763947b657b347211dc9388754e05d30d0467Eric Laurent        return outputOffloaded;
118945c763947b657b347211dc9388754e05d30d0467Eric Laurent    }
119045c763947b657b347211dc9388754e05d30d0467Eric Laurent    if (outputDeepBuffer != 0) {
119145c763947b657b347211dc9388754e05d30d0467Eric Laurent        return outputDeepBuffer;
119245c763947b657b347211dc9388754e05d30d0467Eric Laurent    }
119345c763947b657b347211dc9388754e05d30d0467Eric Laurent
119445c763947b657b347211dc9388754e05d30d0467Eric Laurent    return outputs[0];
119545c763947b657b347211dc9388754e05d30d0467Eric Laurent}
119645c763947b657b347211dc9388754e05d30d0467Eric Laurent
1197c94dccc97cc3ed5171b45f46a0f7f8762d37156fGlenn Kastenaudio_io_handle_t AudioPolicyManagerBase::getOutputForEffect(const effect_descriptor_t *desc)
1198f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1199f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // apply simple rule where global effects are attached to the same output as MUSIC streams
12004660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen
12014660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen    routing_strategy strategy = getStrategy(AudioSystem::MUSIC);
12024660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen    audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
1203c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs);
120445c763947b657b347211dc9388754e05d30d0467Eric Laurent
120545c763947b657b347211dc9388754e05d30d0467Eric Laurent    audio_io_handle_t output = selectOutputForEffects(dstOutputs);
120645c763947b657b347211dc9388754e05d30d0467Eric Laurent    ALOGV("getOutputForEffect() got output %d for fx %s flags %x",
120745c763947b657b347211dc9388754e05d30d0467Eric Laurent          output, (desc == NULL) ? "unspecified" : desc->name,  (desc == NULL) ? 0 : desc->flags);
120845c763947b657b347211dc9388754e05d30d0467Eric Laurent
120945c763947b657b347211dc9388754e05d30d0467Eric Laurent    return output;
1210f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1211f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1212c94dccc97cc3ed5171b45f46a0f7f8762d37156fGlenn Kastenstatus_t AudioPolicyManagerBase::registerEffect(const effect_descriptor_t *desc,
12131c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent                                audio_io_handle_t io,
1214f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                uint32_t strategy,
1215f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                int session,
1216f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                int id)
1217f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
12181c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent    ssize_t index = mOutputs.indexOfKey(io);
1219f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
12201c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent        index = mInputs.indexOfKey(io);
12211c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent        if (index < 0) {
122264cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("registerEffect() unknown io %d", io);
12231c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent            return INVALID_OPERATION;
12241c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent        }
1225f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1226f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1227f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mTotalEffectsMemory + desc->memoryUsage > getMaxEffectsMemory()) {
122864cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("registerEffect() memory limit exceeded for Fx %s, Memory %d KB",
1229f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                desc->name, desc->memoryUsage);
1230f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return INVALID_OPERATION;
1231f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1232f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mTotalEffectsMemory += desc->memoryUsage;
12336a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("registerEffect() effect %s, io %d, strategy %d session %d id %d",
12341c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent            desc->name, io, strategy, session, id);
12356a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("registerEffect() memory %d, total memory %d", desc->memoryUsage, mTotalEffectsMemory);
1236f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1237f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    EffectDescriptor *pDesc = new EffectDescriptor();
1238f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    memcpy (&pDesc->mDesc, desc, sizeof(effect_descriptor_t));
12391c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent    pDesc->mIo = io;
1240f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    pDesc->mStrategy = (routing_strategy)strategy;
1241f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    pDesc->mSession = session;
1242582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    pDesc->mEnabled = false;
1243582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent
1244f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mEffects.add(id, pDesc);
1245f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1246f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
1247f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1248f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1249f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::unregisterEffect(int id)
1250f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1251f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mEffects.indexOfKey(id);
1252f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
125364cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("unregisterEffect() unknown effect ID %d", id);
1254f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return INVALID_OPERATION;
1255f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1256f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1257f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    EffectDescriptor *pDesc = mEffects.valueAt(index);
1258f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1259582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    setEffectEnabled(pDesc, false);
1260582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent
1261f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mTotalEffectsMemory < pDesc->mDesc.memoryUsage) {
126264cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("unregisterEffect() memory %d too big for total %d",
1263f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                pDesc->mDesc.memoryUsage, mTotalEffectsMemory);
1264f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        pDesc->mDesc.memoryUsage = mTotalEffectsMemory;
1265f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1266f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mTotalEffectsMemory -= pDesc->mDesc.memoryUsage;
12676a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("unregisterEffect() effect %s, ID %d, memory %d total memory %d",
1268582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent            pDesc->mDesc.name, id, pDesc->mDesc.memoryUsage, mTotalEffectsMemory);
1269f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1270f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mEffects.removeItem(id);
1271f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    delete pDesc;
1272f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1273f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
1274f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1275f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1276582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurentstatus_t AudioPolicyManagerBase::setEffectEnabled(int id, bool enabled)
1277582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent{
1278582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    ssize_t index = mEffects.indexOfKey(id);
1279582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    if (index < 0) {
128064cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("unregisterEffect() unknown effect ID %d", id);
1281582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        return INVALID_OPERATION;
1282582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    }
1283582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent
1284582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    return setEffectEnabled(mEffects.valueAt(index), enabled);
1285582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent}
1286582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent
1287582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurentstatus_t AudioPolicyManagerBase::setEffectEnabled(EffectDescriptor *pDesc, bool enabled)
1288582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent{
1289582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    if (enabled == pDesc->mEnabled) {
12906a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("setEffectEnabled(%s) effect already %s",
1291582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent             enabled?"true":"false", enabled?"enabled":"disabled");
1292582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        return INVALID_OPERATION;
1293582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    }
1294582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent
1295582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    if (enabled) {
1296582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        if (mTotalEffectsCpuLoad + pDesc->mDesc.cpuLoad > getMaxEffectsCpuLoad()) {
129764cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("setEffectEnabled(true) CPU Load limit exceeded for Fx %s, CPU %f MIPS",
1298582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent                 pDesc->mDesc.name, (float)pDesc->mDesc.cpuLoad/10);
1299582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent            return INVALID_OPERATION;
1300582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        }
1301582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        mTotalEffectsCpuLoad += pDesc->mDesc.cpuLoad;
13026a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("setEffectEnabled(true) total CPU %d", mTotalEffectsCpuLoad);
1303582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    } else {
1304582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        if (mTotalEffectsCpuLoad < pDesc->mDesc.cpuLoad) {
130564cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("setEffectEnabled(false) CPU load %d too high for total %d",
1306582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent                    pDesc->mDesc.cpuLoad, mTotalEffectsCpuLoad);
1307582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent            pDesc->mDesc.cpuLoad = mTotalEffectsCpuLoad;
1308582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        }
1309582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        mTotalEffectsCpuLoad -= pDesc->mDesc.cpuLoad;
13106a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("setEffectEnabled(false) total CPU %d", mTotalEffectsCpuLoad);
1311582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    }
1312582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    pDesc->mEnabled = enabled;
1313582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    return NO_ERROR;
1314582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent}
1315582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent
1316000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurentbool AudioPolicyManagerBase::isNonOffloadableEffectEnabled()
1317000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent{
1318000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    for (size_t i = 0; i < mEffects.size(); i++) {
1319000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent        const EffectDescriptor * const pDesc = mEffects.valueAt(i);
1320000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent        if (pDesc->mEnabled && (pDesc->mStrategy == STRATEGY_MEDIA) &&
1321000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent                ((pDesc->mDesc.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) == 0)) {
1322000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent            ALOGV("isNonOffloadableEffectEnabled() non offloadable effect %s enabled on session %d",
1323000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent                  pDesc->mDesc.name, pDesc->mSession);
1324000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent            return true;
1325000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent        }
1326000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    }
1327000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    return false;
1328000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent}
1329000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent
1330f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinbool AudioPolicyManagerBase::isStreamActive(int stream, uint32_t inPastMs) const
1331f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1332f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    nsecs_t sysTime = systemTime();
1333f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mOutputs.size(); i++) {
133480f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent        const AudioOutputDescriptor *outputDesc = mOutputs.valueAt(i);
133580f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent        if (outputDesc->isStreamActive((AudioSystem::stream_type)stream, inPastMs, sysTime)) {
1336f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return true;
1337f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1338f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1339f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return false;
1340f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1341f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1342dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivibool AudioPolicyManagerBase::isStreamActiveRemotely(int stream, uint32_t inPastMs) const
1343dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi{
1344dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi    nsecs_t sysTime = systemTime();
1345dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi    for (size_t i = 0; i < mOutputs.size(); i++) {
1346dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi        const AudioOutputDescriptor *outputDesc = mOutputs.valueAt(i);
134780f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent        if (((outputDesc->device() & APM_AUDIO_OUT_DEVICE_REMOTE_ALL) != 0) &&
134880f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                outputDesc->isStreamActive((AudioSystem::stream_type)stream, inPastMs, sysTime)) {
1349dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi            return true;
1350dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi        }
1351dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi    }
1352dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi    return false;
1353dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi}
1354dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi
1355abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivibool AudioPolicyManagerBase::isSourceActive(audio_source_t source) const
1356abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi{
1357abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi    for (size_t i = 0; i < mInputs.size(); i++) {
1358abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi        const AudioInputDescriptor * inputDescriptor = mInputs.valueAt(i);
1359fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent        if ((inputDescriptor->mInputSource == (int)source ||
1360fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent                (source == (audio_source_t)AUDIO_SOURCE_VOICE_RECOGNITION &&
1361fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent                 inputDescriptor->mInputSource == AUDIO_SOURCE_HOTWORD))
1362fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent             && (inputDescriptor->mRefCount > 0)) {
1363abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi            return true;
1364abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi        }
1365abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi    }
1366abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi    return false;
1367abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi}
1368abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi
1369abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi
1370f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::dump(int fd)
1371f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1372f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    const size_t SIZE = 256;
1373f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    char buffer[SIZE];
1374f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    String8 result;
1375f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1376f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
1377f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1378b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
137970c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, " Primary Output: %d\n", mPrimaryOutput);
1380f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1381f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " A2DP device address: %s\n", mA2dpDeviceAddress.string());
1382f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1383f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " SCO device address: %s\n", mScoDeviceAddress.string());
1384f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1385fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    snprintf(buffer, SIZE, " USB audio ALSA %s\n", mUsbOutCardAndDevice.string());
1386599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    result.append(buffer);
1387f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Output devices: %08x\n", mAvailableOutputDevices);
1388f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1389f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Input devices: %08x\n", mAvailableInputDevices);
1390f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1391f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Phone state: %d\n", mPhoneState);
1392f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1393f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Force use for communications %d\n", mForceUse[AudioSystem::FOR_COMMUNICATION]);
1394f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1395f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Force use for media %d\n", mForceUse[AudioSystem::FOR_MEDIA]);
1396f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1397f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Force use for record %d\n", mForceUse[AudioSystem::FOR_RECORD]);
1398f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1399f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Force use for dock %d\n", mForceUse[AudioSystem::FOR_DOCK]);
1400f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1401738207def5f691d605ae33d041116829a74513a9Eric Laurent    snprintf(buffer, SIZE, " Force use for system %d\n", mForceUse[AudioSystem::FOR_SYSTEM]);
1402738207def5f691d605ae33d041116829a74513a9Eric Laurent    result.append(buffer);
1403f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, result.string(), result.size());
1404f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
14055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
140670c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "\nHW Modules dump:\n");
14075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    write(fd, buffer, strlen(buffer));
140870c236c9290732782d5267935af1475b8d5ae602Eric Laurent    for (size_t i = 0; i < mHwModules.size(); i++) {
1409c7b6e3c857c5f5af5f244d0b877c41a9c55f92abColin Cross        snprintf(buffer, SIZE, "- HW Module %zu:\n", i + 1);
14105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        write(fd, buffer, strlen(buffer));
141170c236c9290732782d5267935af1475b8d5ae602Eric Laurent        mHwModules[i]->dump(fd);
14125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
14135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
1414f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "\nOutputs dump:\n");
1415f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
1416f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mOutputs.size(); i++) {
1417f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        snprintf(buffer, SIZE, "- Output %d dump:\n", mOutputs.keyAt(i));
1418f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        write(fd, buffer, strlen(buffer));
1419f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mOutputs.valueAt(i)->dump(fd);
1420f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1421f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1422f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "\nInputs dump:\n");
1423f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
1424f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mInputs.size(); i++) {
1425f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        snprintf(buffer, SIZE, "- Input %d dump:\n", mInputs.keyAt(i));
1426f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        write(fd, buffer, strlen(buffer));
1427f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mInputs.valueAt(i)->dump(fd);
1428f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1429f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1430f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "\nStreams dump:\n");
1431f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
1432c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    snprintf(buffer, SIZE,
1433c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent             " Stream  Can be muted  Index Min  Index Max  Index Cur [device : index]...\n");
1434f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
1435f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) {
1436c7b6e3c857c5f5af5f244d0b877c41a9c55f92abColin Cross        snprintf(buffer, SIZE, " %02zu      ", i);
1437f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        write(fd, buffer, strlen(buffer));
1438c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        mStreams[i].dump(fd);
1439f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1440f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1441f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "\nTotal Effects CPU: %f MIPS, Total Effects memory: %d KB\n",
1442f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            (float)mTotalEffectsCpuLoad/10, mTotalEffectsMemory);
1443f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
1444f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1445f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "Registered effects:\n");
1446f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
1447f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mEffects.size(); i++) {
1448f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        snprintf(buffer, SIZE, "- Effect %d dump:\n", mEffects.keyAt(i));
1449f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        write(fd, buffer, strlen(buffer));
1450f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mEffects.valueAt(i)->dump(fd);
1451f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1452f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1453f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1454f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
1455f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1456f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1457a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald// This function checks for the parameters which can be offloaded.
1458a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald// This can be enhanced depending on the capability of the DSP and policy
1459a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald// of the system.
1460b4d07b97d23cfaffe22c7859ad7c45e168a7df0eRichard Fitzgeraldbool AudioPolicyManagerBase::isOffloadSupported(const audio_offload_info_t& offloadInfo)
1461b4d07b97d23cfaffe22c7859ad7c45e168a7df0eRichard Fitzgerald{
1462a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
1463c7b6e3c857c5f5af5f244d0b877c41a9c55f92abColin Cross     " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
1464a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald     offloadInfo.sample_rate, offloadInfo.channel_mask,
1465a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald     offloadInfo.format,
1466a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald     offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
1467a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald     offloadInfo.has_video);
1468a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald
1469a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    // Check if offload has been disabled
1470a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    char propValue[PROPERTY_VALUE_MAX];
1471a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    if (property_get("audio.offload.disable", propValue, "0")) {
1472a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        if (atoi(propValue) != 0) {
1473a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald            ALOGV("offload disabled by audio.offload.disable=%s", propValue );
1474a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald            return false;
1475a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        }
1476a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    }
1477a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald
1478a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    // Check if stream type is music, then only allow offload as of now.
1479a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
1480a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    {
148141b150b659d4ba33a9be9f99fd9f0ee1f2787541Eric Laurent        ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
148241b150b659d4ba33a9be9f99fd9f0ee1f2787541Eric Laurent        return false;
148341b150b659d4ba33a9be9f99fd9f0ee1f2787541Eric Laurent    }
148441b150b659d4ba33a9be9f99fd9f0ee1f2787541Eric Laurent
148541b150b659d4ba33a9be9f99fd9f0ee1f2787541Eric Laurent    //TODO: enable audio offloading with video when ready
148641b150b659d4ba33a9be9f99fd9f0ee1f2787541Eric Laurent    if (offloadInfo.has_video)
148741b150b659d4ba33a9be9f99fd9f0ee1f2787541Eric Laurent    {
148841b150b659d4ba33a9be9f99fd9f0ee1f2787541Eric Laurent        ALOGV("isOffloadSupported: has_video == true, returning false");
1489a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        return false;
1490a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    }
1491a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald
1492a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    //If duration is less than minimum value defined in property, return false
1493a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
1494a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
1495a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald            ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
1496a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald            return false;
1497a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        }
1498a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
1499a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
1500a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        return false;
1501a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    }
1502a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald
1503000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    // Do not allow offloading if one non offloadable effect is enabled. This prevents from
1504000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    // creating an offloaded track and tearing it down immediately after start when audioflinger
1505000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    // detects there is an active non offloadable effect.
1506000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    // FIXME: We should check the audio session here but we do not have it in this context.
1507000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    // This may prevent offloading in rare situations where effects are left active by apps
1508000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    // in the background.
1509000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    if (isNonOffloadableEffectEnabled()) {
1510000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent        return false;
1511000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    }
1512000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent
1513a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    // See if there is a profile to support this.
1514a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    // AUDIO_DEVICE_NONE
1515a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    IOProfile *profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
1516a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                                            offloadInfo.sample_rate,
1517a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                                            offloadInfo.format,
1518a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                                            offloadInfo.channel_mask,
1519a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                                            AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
1520a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    ALOGV("isOffloadSupported() profile %sfound", profile != NULL ? "" : "NOT ");
1521a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    return (profile != NULL);
1522b4d07b97d23cfaffe22c7859ad7c45e168a7df0eRichard Fitzgerald}
1523b4d07b97d23cfaffe22c7859ad7c45e168a7df0eRichard Fitzgerald
1524f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// ----------------------------------------------------------------------------
1525f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// AudioPolicyManagerBase
1526f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// ----------------------------------------------------------------------------
1527f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1528f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima ZavinAudioPolicyManagerBase::AudioPolicyManagerBase(AudioPolicyClientInterface *clientInterface)
1529f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    :
1530f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
1531f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    Thread(false),
1532f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
153370c236c9290732782d5267935af1475b8d5ae602Eric Laurent    mPrimaryOutput((audio_io_handle_t)0),
1534ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    mAvailableOutputDevices(AUDIO_DEVICE_NONE),
1535ca0657a1ca087a6d474a75fcfedd6aac3901d587Glenn Kasten    mPhoneState(AudioSystem::MODE_NORMAL),
1536f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
1537f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mTotalEffectsCpuLoad(0), mTotalEffectsMemory(0),
153818fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi    mA2dpSuspended(false), mHasA2dp(false), mHasUsb(false), mHasRemoteSubmix(false),
153918fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi    mSpeakerDrcEnabled(false)
1540f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1541f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mpClientInterface = clientInterface;
1542f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1543f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < AudioSystem::NUM_FORCE_USE; i++) {
1544f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mForceUse[i] = AudioSystem::FORCE_NONE;
1545f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1546f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1547f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mA2dpDeviceAddress = String8("");
1548f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mScoDeviceAddress = String8("");
1549fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    mUsbOutCardAndDevice = String8("");
1550f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
15515ec145df7708564d385fd3fb764085321cf4c253Dima Zavin    if (loadAudioPolicyConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE) != NO_ERROR) {
15525ec145df7708564d385fd3fb764085321cf4c253Dima Zavin        if (loadAudioPolicyConfig(AUDIO_POLICY_CONFIG_FILE) != NO_ERROR) {
1553739022f26a7127ba76a98dda65411496086114a7Dima Zavin            ALOGE("could not load audio policy configuration file, setting defaults");
1554739022f26a7127ba76a98dda65411496086114a7Dima Zavin            defaultAudioPolicyConfig();
15555ec145df7708564d385fd3fb764085321cf4c253Dima Zavin        }
15565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
15575ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
155818fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi    // must be done after reading the policy
155918fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi    initializeVolumeCurves();
156018fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi
1561b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    // open all output streams needed to access attached devices
156270c236c9290732782d5267935af1475b8d5ae602Eric Laurent    for (size_t i = 0; i < mHwModules.size(); i++) {
156370c236c9290732782d5267935af1475b8d5ae602Eric Laurent        mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->mName);
156470c236c9290732782d5267935af1475b8d5ae602Eric Laurent        if (mHwModules[i]->mHandle == 0) {
156570c236c9290732782d5267935af1475b8d5ae602Eric Laurent            ALOGW("could not open HW module %s", mHwModules[i]->mName);
156670c236c9290732782d5267935af1475b8d5ae602Eric Laurent            continue;
156770c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
156870c236c9290732782d5267935af1475b8d5ae602Eric Laurent        // open all output streams needed to access attached devices
1569a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        // except for direct output streams that are only opened when they are actually
1570a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        // required by an app.
157170c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
157270c236c9290732782d5267935af1475b8d5ae602Eric Laurent        {
157370c236c9290732782d5267935af1475b8d5ae602Eric Laurent            const IOProfile *outProfile = mHwModules[i]->mOutputProfiles[j];
157470c236c9290732782d5267935af1475b8d5ae602Eric Laurent
1575a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald            if ((outProfile->mSupportedDevices & mAttachedOutputDevices) &&
1576a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                    ((outProfile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0)) {
157770c236c9290732782d5267935af1475b8d5ae602Eric Laurent                AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(outProfile);
157870c236c9290732782d5267935af1475b8d5ae602Eric Laurent                outputDesc->mDevice = (audio_devices_t)(mDefaultOutputDevice &
157970c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                            outProfile->mSupportedDevices);
158070c236c9290732782d5267935af1475b8d5ae602Eric Laurent                audio_io_handle_t output = mpClientInterface->openOutput(
158170c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                outProfile->mModule->mHandle,
158270c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mDevice,
158370c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mSamplingRate,
158470c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mFormat,
158570c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mChannelMask,
158670c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mLatency,
158770c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                outputDesc->mFlags);
158870c236c9290732782d5267935af1475b8d5ae602Eric Laurent                if (output == 0) {
158970c236c9290732782d5267935af1475b8d5ae602Eric Laurent                    delete outputDesc;
159070c236c9290732782d5267935af1475b8d5ae602Eric Laurent                } else {
159170c236c9290732782d5267935af1475b8d5ae602Eric Laurent                    mAvailableOutputDevices = (audio_devices_t)(mAvailableOutputDevices |
159270c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                            (outProfile->mSupportedDevices & mAttachedOutputDevices));
159370c236c9290732782d5267935af1475b8d5ae602Eric Laurent                    if (mPrimaryOutput == 0 &&
15940977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent                            outProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
159570c236c9290732782d5267935af1475b8d5ae602Eric Laurent                        mPrimaryOutput = output;
159670c236c9290732782d5267935af1475b8d5ae602Eric Laurent                    }
159770c236c9290732782d5267935af1475b8d5ae602Eric Laurent                    addOutput(output, outputDesc);
159870c236c9290732782d5267935af1475b8d5ae602Eric Laurent                    setOutputDevice(output,
159970c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                    (audio_devices_t)(mDefaultOutputDevice &
160070c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                        outProfile->mSupportedDevices),
160170c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                    true);
1602b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                }
1603b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            }
1604b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        }
1605f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1606f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
16075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGE_IF((mAttachedOutputDevices & ~mAvailableOutputDevices),
1608b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent             "Not output found for attached devices %08x",
16095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent             (mAttachedOutputDevices & ~mAvailableOutputDevices));
1610b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1611b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output");
1612b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1613c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    updateDevicesAndOutputs();
16143cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
1615f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
1616b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (mPrimaryOutput != 0) {
1617f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioParameter outputCmd = AudioParameter();
1618f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputCmd.addInt(String8("set_id"), 0);
1619b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        mpClientInterface->setParameters(mPrimaryOutput, outputCmd.toString());
1620f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1621c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        mTestDevice = AUDIO_DEVICE_OUT_SPEAKER;
1622f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mTestSamplingRate = 44100;
1623f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mTestFormat = AudioSystem::PCM_16_BIT;
1624f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mTestChannels =  AudioSystem::CHANNEL_OUT_STEREO;
1625f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mTestLatencyMs = 0;
1626f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mCurOutput = 0;
1627f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mDirectOutput = false;
1628f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
1629f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mTestOutputs[i] = 0;
1630f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1631f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1632f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        const size_t SIZE = 256;
1633f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        char buffer[SIZE];
1634f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        snprintf(buffer, SIZE, "AudioPolicyManagerTest");
1635f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        run(buffer, ANDROID_PRIORITY_AUDIO);
1636f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1637f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
1638f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1639f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1640f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima ZavinAudioPolicyManagerBase::~AudioPolicyManagerBase()
1641f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1642f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
1643f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    exit();
1644f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
1645f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin   for (size_t i = 0; i < mOutputs.size(); i++) {
1646f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mpClientInterface->closeOutput(mOutputs.keyAt(i));
1647f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        delete mOutputs.valueAt(i);
1648f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin   }
1649f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin   for (size_t i = 0; i < mInputs.size(); i++) {
1650f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mpClientInterface->closeInput(mInputs.keyAt(i));
1651f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        delete mInputs.valueAt(i);
1652f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin   }
165370c236c9290732782d5267935af1475b8d5ae602Eric Laurent   for (size_t i = 0; i < mHwModules.size(); i++) {
165470c236c9290732782d5267935af1475b8d5ae602Eric Laurent        delete mHwModules[i];
16555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent   }
1656f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1657f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1658f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::initCheck()
1659f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1660b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    return (mPrimaryOutput == 0) ? NO_INIT : NO_ERROR;
1661f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1662f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1663f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
1664f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinbool AudioPolicyManagerBase::threadLoop()
1665f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
16666a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("entering threadLoop()");
1667f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    while (!exitPending())
1668f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    {
1669f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        String8 command;
1670f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        int valueInt;
1671f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        String8 value;
1672f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1673f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        Mutex::Autolock _l(mLock);
1674f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mWaitWorkCV.waitRelative(mLock, milliseconds(50));
1675f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1676f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        command = mpClientInterface->getParameters(0, String8("test_cmd_policy"));
1677f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioParameter param = AudioParameter(command);
1678f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1679f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR &&
1680f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            valueInt != 0) {
16816a70518b93928d1c91457ff805e375c82d76b0e5Steve Block            ALOGV("Test command %s received", command.string());
1682f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            String8 target;
1683f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.get(String8("target"), target) != NO_ERROR) {
1684f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                target = "Manager";
1685f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1686f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) {
1687f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_output"));
1688f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mCurOutput = valueInt;
1689f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1690f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) {
1691f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_direct"));
1692f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (value == "false") {
1693f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    mDirectOutput = false;
1694f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else if (value == "true") {
1695f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    mDirectOutput = true;
1696f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1697f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1698f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) {
1699f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_input"));
1700f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mTestInput = valueInt;
1701f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1702f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1703f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) {
1704f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_format"));
1705f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                int format = AudioSystem::INVALID_FORMAT;
1706f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (value == "PCM 16 bits") {
1707f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    format = AudioSystem::PCM_16_BIT;
1708f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else if (value == "PCM 8 bits") {
1709f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    format = AudioSystem::PCM_8_BIT;
1710f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else if (value == "Compressed MP3") {
1711f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    format = AudioSystem::MP3;
1712f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1713f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (format != AudioSystem::INVALID_FORMAT) {
1714f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    if (target == "Manager") {
1715f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mTestFormat = format;
1716f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    } else if (mTestOutputs[mCurOutput] != 0) {
1717f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        AudioParameter outputParam = AudioParameter();
1718f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        outputParam.addInt(String8("format"), format);
1719f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
1720f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    }
1721f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1722f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1723f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) {
1724f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_channels"));
1725f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                int channels = 0;
1726f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1727f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (value == "Channels Stereo") {
1728f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    channels =  AudioSystem::CHANNEL_OUT_STEREO;
1729f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else if (value == "Channels Mono") {
1730f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    channels =  AudioSystem::CHANNEL_OUT_MONO;
1731f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1732f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (channels != 0) {
1733f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    if (target == "Manager") {
1734f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mTestChannels = channels;
1735f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    } else if (mTestOutputs[mCurOutput] != 0) {
1736f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        AudioParameter outputParam = AudioParameter();
1737f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        outputParam.addInt(String8("channels"), channels);
1738f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
1739f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    }
1740f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1741f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1742f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) {
1743f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_sampleRate"));
1744f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (valueInt >= 0 && valueInt <= 96000) {
1745f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    int samplingRate = valueInt;
1746f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    if (target == "Manager") {
1747f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mTestSamplingRate = samplingRate;
1748f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    } else if (mTestOutputs[mCurOutput] != 0) {
1749f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        AudioParameter outputParam = AudioParameter();
1750f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        outputParam.addInt(String8("sampling_rate"), samplingRate);
1751f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
1752f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    }
1753f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1754f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1755f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1756f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) {
1757f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_reopen"));
1758f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
175970c236c9290732782d5267935af1475b8d5ae602Eric Laurent                AudioOutputDescriptor *outputDesc = mOutputs.valueFor(mPrimaryOutput);
1760b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                mpClientInterface->closeOutput(mPrimaryOutput);
176170c236c9290732782d5267935af1475b8d5ae602Eric Laurent
176270c236c9290732782d5267935af1475b8d5ae602Eric Laurent                audio_module_handle_t moduleHandle = outputDesc->mModule->mHandle;
176370c236c9290732782d5267935af1475b8d5ae602Eric Laurent
1764b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                delete mOutputs.valueFor(mPrimaryOutput);
1765b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                mOutputs.removeItem(mPrimaryOutput);
1766f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1767b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(NULL);
1768c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER;
176970c236c9290732782d5267935af1475b8d5ae602Eric Laurent                mPrimaryOutput = mpClientInterface->openOutput(moduleHandle,
177070c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mDevice,
1771f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                &outputDesc->mSamplingRate,
1772f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                &outputDesc->mFormat,
177370c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mChannelMask,
1774f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                &outputDesc->mLatency,
1775f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                outputDesc->mFlags);
1776b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                if (mPrimaryOutput == 0) {
17775efbd421e0029d6fc44b1cc65c0e5e0d85e5161fSteve Block                    ALOGE("Failed to reopen hardware output stream, samplingRate: %d, format %d, channels %d",
177870c236c9290732782d5267935af1475b8d5ae602Eric Laurent                            outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask);
1779f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else {
1780f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    AudioParameter outputCmd = AudioParameter();
1781f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    outputCmd.addInt(String8("set_id"), 0);
1782b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                    mpClientInterface->setParameters(mPrimaryOutput, outputCmd.toString());
1783b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                    addOutput(mPrimaryOutput, outputDesc);
1784f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1785f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1786f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1787f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1788f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mpClientInterface->setParameters(0, String8("test_cmd_policy="));
1789f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1790f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1791f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return false;
1792f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1793f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1794f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::exit()
1795f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1796f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    {
1797f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AutoMutex _l(mLock);
1798f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        requestExit();
1799f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mWaitWorkCV.signal();
1800f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1801f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    requestExitAndWait();
1802f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1803f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1804f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinint AudioPolicyManagerBase::testOutputIndex(audio_io_handle_t output)
1805f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1806f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
1807f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (output == mTestOutputs[i]) return i;
1808f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1809f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return 0;
1810f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1811f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
1812f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1813f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// ---
1814f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1815f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::addOutput(audio_io_handle_t id, AudioOutputDescriptor *outputDesc)
1816f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1817f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    outputDesc->mId = id;
1818f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mOutputs.add(id, outputDesc);
1819f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1820f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1821fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLeanvoid AudioPolicyManagerBase::addInput(audio_io_handle_t id, AudioInputDescriptor *inputDesc)
1822fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean{
1823fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    inputDesc->mId = id;
1824fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    mInputs.add(id, inputDesc);
1825fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean}
1826f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
18273cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurentstatus_t AudioPolicyManagerBase::checkOutputsForDevice(audio_devices_t device,
18283cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                       AudioSystem::device_connection_state state,
18299f4165f36d90dddc32246d0eddbf289383c3b2bbEric Laurent                                                       SortedVector<audio_io_handle_t>& outputs,
18309f4165f36d90dddc32246d0eddbf289383c3b2bbEric Laurent                                                       const String8 paramStr)
1831f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
18323cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    AudioOutputDescriptor *desc;
1833b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1834b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (state == AudioSystem::DEVICE_STATE_AVAILABLE) {
18353cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        // first list already open outputs that can be routed to this device
1836b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        for (size_t i = 0; i < mOutputs.size(); i++) {
18373cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            desc = mOutputs.valueAt(i);
18383cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            if (!desc->isDuplicated() && (desc->mProfile->mSupportedDevices & device)) {
18393cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i));
18403cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                outputs.add(mOutputs.keyAt(i));
1841b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            }
1842b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        }
18433cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        // then look for output profiles that can be routed to this device
18443cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        SortedVector<IOProfile *> profiles;
184570c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (size_t i = 0; i < mHwModules.size(); i++)
1846b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        {
184770c236c9290732782d5267935af1475b8d5ae602Eric Laurent            if (mHwModules[i]->mHandle == 0) {
184870c236c9290732782d5267935af1475b8d5ae602Eric Laurent                continue;
184970c236c9290732782d5267935af1475b8d5ae602Eric Laurent            }
185070c236c9290732782d5267935af1475b8d5ae602Eric Laurent            for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
185170c236c9290732782d5267935af1475b8d5ae602Eric Laurent            {
185270c236c9290732782d5267935af1475b8d5ae602Eric Laurent                if (mHwModules[i]->mOutputProfiles[j]->mSupportedDevices & device) {
1853c7b6e3c857c5f5af5f244d0b877c41a9c55f92abColin Cross                    ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i);
18543cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    profiles.add(mHwModules[i]->mOutputProfiles[j]);
185570c236c9290732782d5267935af1475b8d5ae602Eric Laurent                }
185670c236c9290732782d5267935af1475b8d5ae602Eric Laurent            }
1857b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        }
185870c236c9290732782d5267935af1475b8d5ae602Eric Laurent
18593cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        if (profiles.isEmpty() && outputs.isEmpty()) {
18603cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
18613cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            return BAD_VALUE;
186270c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
186370c236c9290732782d5267935af1475b8d5ae602Eric Laurent
18643cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        // open outputs for matching profiles if needed. Direct outputs are also opened to
18653cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        // query for dynamic parameters and will be closed later by setDeviceConnectionState()
18663cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
18673cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            IOProfile *profile = profiles[profile_index];
1868b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
18693cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            // nothing to do if one output is already opened for this profile
18703cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            size_t j;
1871fd8cecbee6843b444d56a1db40af76027e2b19f1Eric Laurent            for (j = 0; j < mOutputs.size(); j++) {
18723cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                desc = mOutputs.valueAt(j);
18733cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                if (!desc->isDuplicated() && desc->mProfile == profile) {
18743cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    break;
18753cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                }
18763cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
1877fd8cecbee6843b444d56a1db40af76027e2b19f1Eric Laurent            if (j != mOutputs.size()) {
18783cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                continue;
18793cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
18803cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
18819f4165f36d90dddc32246d0eddbf289383c3b2bbEric Laurent            ALOGV("opening output for device %08x with params %s", device, paramStr.string());
18823cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            desc = new AudioOutputDescriptor(profile);
18833cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            desc->mDevice = device;
1884727fef1b1a8cb6d6609ec99a749fcd9573a3417dEric Laurent            audio_offload_info_t offloadInfo = AUDIO_INFO_INITIALIZER;
1885727fef1b1a8cb6d6609ec99a749fcd9573a3417dEric Laurent            offloadInfo.sample_rate = desc->mSamplingRate;
1886727fef1b1a8cb6d6609ec99a749fcd9573a3417dEric Laurent            offloadInfo.format = desc->mFormat;
1887727fef1b1a8cb6d6609ec99a749fcd9573a3417dEric Laurent            offloadInfo.channel_mask = desc->mChannelMask;
1888727fef1b1a8cb6d6609ec99a749fcd9573a3417dEric Laurent
18893cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            audio_io_handle_t output = mpClientInterface->openOutput(profile->mModule->mHandle,
18903cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                       &desc->mDevice,
18913cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                       &desc->mSamplingRate,
18923cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                       &desc->mFormat,
18933cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                       &desc->mChannelMask,
18943cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                       &desc->mLatency,
1895727fef1b1a8cb6d6609ec99a749fcd9573a3417dEric Laurent                                                                       desc->mFlags,
1896727fef1b1a8cb6d6609ec99a749fcd9573a3417dEric Laurent                                                                       &offloadInfo);
18973cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            if (output != 0) {
18989f4165f36d90dddc32246d0eddbf289383c3b2bbEric Laurent                if (!paramStr.isEmpty()) {
1899fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    // Here is where the out_set_parameters() for card & device gets called
19009f4165f36d90dddc32246d0eddbf289383c3b2bbEric Laurent                    mpClientInterface->setParameters(output, paramStr);
19019f4165f36d90dddc32246d0eddbf289383c3b2bbEric Laurent                }
19029f4165f36d90dddc32246d0eddbf289383c3b2bbEric Laurent
1903fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                // Here is where we step through and resolve any "dynamic" fields
1904c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                String8 reply;
1905c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                char *value;
1906c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                if (profile->mSamplingRates[0] == 0) {
1907c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                    reply = mpClientInterface->getParameters(output,
1908c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                                            String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES));
1909c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                    ALOGV("checkOutputsForDevice() direct output sup sampling rates %s",
1910c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                              reply.string());
1911c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                    value = strpbrk((char *)reply.string(), "=");
1912c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                    if (value != NULL) {
1913c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                        loadSamplingRates(value + 1, profile);
19143cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
1915c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                }
1916c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
1917c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                    reply = mpClientInterface->getParameters(output,
1918c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                                                   String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
1919c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                    ALOGV("checkOutputsForDevice() direct output sup formats %s",
1920c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                              reply.string());
1921c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                    value = strpbrk((char *)reply.string(), "=");
1922c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                    if (value != NULL) {
1923c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                        loadFormats(value + 1, profile);
19243cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
1925c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                }
1926c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                if (profile->mChannelMasks[0] == 0) {
1927c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                    reply = mpClientInterface->getParameters(output,
1928c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                                                  String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS));
1929c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                    ALOGV("checkOutputsForDevice() direct output sup channel masks %s",
1930c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                              reply.string());
1931c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                    value = strpbrk((char *)reply.string(), "=");
1932c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                    if (value != NULL) {
1933c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                        loadOutChannels(value + 1, profile);
19343cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
1935c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                }
1936c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                if (((profile->mSamplingRates[0] == 0) &&
1937c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                         (profile->mSamplingRates.size() < 2)) ||
1938c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                     ((profile->mFormats[0] == 0) &&
1939c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                         (profile->mFormats.size() < 2)) ||
1940c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                     ((profile->mChannelMasks[0] == 0) &&
1941c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                         (profile->mChannelMasks.size() < 2))) {
1942c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                    ALOGW("checkOutputsForDevice() direct output missing param");
1943c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                    mpClientInterface->closeOutput(output);
1944c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                    output = 0;
1945c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                } else if (profile->mSamplingRates[0] == 0) {
1946c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                    mpClientInterface->closeOutput(output);
1947c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                    desc->mSamplingRate = profile->mSamplingRates[1];
1948c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                    offloadInfo.sample_rate = desc->mSamplingRate;
1949c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                    output = mpClientInterface->openOutput(
1950c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                                                    profile->mModule->mHandle,
1951c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                                                    &desc->mDevice,
1952c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                                                    &desc->mSamplingRate,
1953c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                                                    &desc->mFormat,
1954c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                                                    &desc->mChannelMask,
1955c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                                                    &desc->mLatency,
1956c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                                                    desc->mFlags,
1957c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                                                    &offloadInfo);
1958c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                }
1959c26f454f8d841657542916cdd140a9896a89ad20Paul McLean
1960c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                if (output != 0) {
19613cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    addOutput(output, desc);
1962c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                    if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) {
1963c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                        audio_io_handle_t duplicatedOutput = 0;
1964c26f454f8d841657542916cdd140a9896a89ad20Paul McLean
1965c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                        // set initial stream volume for device
1966c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                        applyStreamVolumes(output, device, 0, true);
1967c26f454f8d841657542916cdd140a9896a89ad20Paul McLean
1968c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                        //TODO: configure audio effect output stage here
1969c26f454f8d841657542916cdd140a9896a89ad20Paul McLean
1970c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                        // open a duplicating output thread for the new output and the primary output
1971c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                        duplicatedOutput = mpClientInterface->openDuplicateOutput(output,
1972c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                                                                                  mPrimaryOutput);
1973c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                        if (duplicatedOutput != 0) {
1974c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                            // add duplicated output descriptor
1975c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                            AudioOutputDescriptor *dupOutputDesc = new AudioOutputDescriptor(NULL);
1976c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                            dupOutputDesc->mOutput1 = mOutputs.valueFor(mPrimaryOutput);
1977c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                            dupOutputDesc->mOutput2 = mOutputs.valueFor(output);
1978c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                            dupOutputDesc->mSamplingRate = desc->mSamplingRate;
1979c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                            dupOutputDesc->mFormat = desc->mFormat;
1980c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                            dupOutputDesc->mChannelMask = desc->mChannelMask;
1981c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                            dupOutputDesc->mLatency = desc->mLatency;
1982c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                            addOutput(duplicatedOutput, dupOutputDesc);
1983c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                            applyStreamVolumes(duplicatedOutput, device, 0, true);
1984c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                        } else {
1985c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                            ALOGW("checkOutputsForDevice() could not open dup output for %d and %d",
1986c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                                    mPrimaryOutput, output);
1987c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                            mpClientInterface->closeOutput(output);
1988c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                            mOutputs.removeItem(output);
1989c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                            output = 0;
1990c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                        }
19913cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
19923cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                }
19933cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
19943cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            if (output == 0) {
19953cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                ALOGW("checkOutputsForDevice() could not open output for device %x", device);
19963cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                delete desc;
19973cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                profiles.removeAt(profile_index);
19983cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                profile_index--;
1999b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            } else {
20003cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                outputs.add(output);
20013cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                ALOGV("checkOutputsForDevice(): adding output %d", output);
2002f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
20033cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        }
20043cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
20053cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        if (profiles.isEmpty()) {
20063cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
20073cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            return BAD_VALUE;
2008f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2009fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    } else { // Disconnect
2010b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        // check if one opened output is not needed any more after disconnecting one device
2011b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        for (size_t i = 0; i < mOutputs.size(); i++) {
20123cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            desc = mOutputs.valueAt(i);
20133cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            if (!desc->isDuplicated() &&
20143cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    !(desc->mProfile->mSupportedDevices & mAvailableOutputDevices)) {
20153cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                ALOGV("checkOutputsForDevice(): disconnecting adding output %d", mOutputs.keyAt(i));
20163cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                outputs.add(mOutputs.keyAt(i));
20173cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
20183cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        }
2019fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        // Clear any profiles associated with the disconnected device.
20203cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        for (size_t i = 0; i < mHwModules.size(); i++)
20213cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        {
20223cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            if (mHwModules[i]->mHandle == 0) {
20233cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                continue;
20243cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
20253cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
20263cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            {
20273cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                IOProfile *profile = mHwModules[i]->mOutputProfiles[j];
2028c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                if (profile->mSupportedDevices & device) {
2029c7b6e3c857c5f5af5f244d0b877c41a9c55f92abColin Cross                    ALOGV("checkOutputsForDevice(): clearing direct output profile %zu on module %zu",
20303cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                          j, i);
20313cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    if (profile->mSamplingRates[0] == 0) {
20323cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        profile->mSamplingRates.clear();
20333cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        profile->mSamplingRates.add(0);
20343cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
20352d749b0bda4ae60ef1a940eeba86121b2e94d07dGlenn Kasten                    if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
20363cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        profile->mFormats.clear();
20372d749b0bda4ae60ef1a940eeba86121b2e94d07dGlenn Kasten                        profile->mFormats.add(AUDIO_FORMAT_DEFAULT);
20383cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
20393cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    if (profile->mChannelMasks[0] == 0) {
20403cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        profile->mChannelMasks.clear();
20412c3d2379ee6eadb32b63bf6de538cfd6487e8e20Glenn Kasten                        profile->mChannelMasks.add(0);
20423cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
20433cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                }
2044b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            }
2045f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2046f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
20473cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    return NO_ERROR;
2048f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2049f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2050fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLeanstatus_t AudioPolicyManagerBase::checkInputsForDevice(audio_devices_t device,
2051fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                                                      AudioSystem::device_connection_state state,
2052fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                                                      SortedVector<audio_io_handle_t>& inputs,
2053fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                                                      const String8 paramStr)
2054fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean{
2055fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    AudioInputDescriptor *desc;
2056fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    if (state == AudioSystem::DEVICE_STATE_AVAILABLE) {
2057fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        // first list already open inputs that can be routed to this device
2058fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
2059fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            desc = mInputs.valueAt(input_index);
2060fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            if (desc->mProfile->mSupportedDevices & (device & ~AUDIO_DEVICE_BIT_IN)) {
2061fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index));
2062fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean               inputs.add(mInputs.keyAt(input_index));
2063fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            }
2064fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        }
2065fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
2066fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        // then look for input profiles that can be routed to this device
2067fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        SortedVector<IOProfile *> profiles;
2068fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        for (size_t module_index = 0; module_index < mHwModules.size(); module_index++)
2069fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        {
2070fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            if (mHwModules[module_index]->mHandle == 0) {
2071fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                continue;
2072fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            }
2073fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            for (size_t profile_index = 0;
2074fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                 profile_index < mHwModules[module_index]->mInputProfiles.size();
2075fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                 profile_index++)
2076fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            {
2077fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                if (mHwModules[module_index]->mInputProfiles[profile_index]->mSupportedDevices
2078fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                        & (device & ~AUDIO_DEVICE_BIT_IN)) {
2079fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    ALOGV("checkInputsForDevice(): adding profile %d from module %d",
2080fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                          profile_index, module_index);
2081fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    profiles.add(mHwModules[module_index]->mInputProfiles[profile_index]);
2082fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                }
2083fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            }
2084fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        }
2085fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
2086fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        if (profiles.isEmpty() && inputs.isEmpty()) {
2087fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
2088fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            return BAD_VALUE;
2089fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        }
2090fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
2091fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        // open inputs for matching profiles if needed. Direct inputs are also opened to
2092fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        // query for dynamic parameters and will be closed later by setDeviceConnectionState()
2093fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
2094fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
2095fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            IOProfile *profile = profiles[profile_index];
2096fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            // nothing to do if one input is already opened for this profile
2097fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            size_t input_index;
2098fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            for (input_index = 0; input_index < mInputs.size(); input_index++) {
2099fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                desc = mInputs.valueAt(input_index);
2100fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                if (desc->mProfile == profile) {
2101fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    break;
2102fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                }
2103fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            }
2104fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            if (input_index != mInputs.size()) {
2105fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                continue;
2106fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            }
2107fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
2108fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            ALOGV("opening input for device 0x%X with params %s", device, paramStr.string());
2109fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            desc = new AudioInputDescriptor(profile);
2110fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            desc->mDevice = device;
2111fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
2112fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            audio_io_handle_t input = mpClientInterface->openInput(profile->mModule->mHandle,
2113fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                                            &desc->mDevice,
2114fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                                            &desc->mSamplingRate,
2115fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                                            &desc->mFormat,
2116fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                                            &desc->mChannelMask);
2117fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
2118fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            if (input != 0) {
2119fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                if (!paramStr.isEmpty()) {
2120fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    mpClientInterface->setParameters(input, paramStr);
2121fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                }
2122fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
2123fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                // Here is where we step through and resolve any "dynamic" fields
2124fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                String8 reply;
2125fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                char *value;
2126fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                if (profile->mSamplingRates[0] == 0) {
2127fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    reply = mpClientInterface->getParameters(input,
2128fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                                            String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES));
2129fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    ALOGV("checkInputsForDevice() direct input sup sampling rates %s",
2130fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                              reply.string());
2131fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    value = strpbrk((char *)reply.string(), "=");
2132fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    if (value != NULL) {
2133fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                        loadSamplingRates(value + 1, profile);
2134fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    }
2135fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                }
2136fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
2137fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    reply = mpClientInterface->getParameters(input,
2138fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                                                   String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
2139fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    ALOGV("checkInputsForDevice() direct input sup formats %s", reply.string());
2140fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    value = strpbrk((char *)reply.string(), "=");
2141fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    if (value != NULL) {
2142fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                        loadFormats(value + 1, profile);
2143fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    }
2144fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                }
2145fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                if (profile->mChannelMasks[0] == 0) {
2146fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    reply = mpClientInterface->getParameters(input,
2147fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                                                  String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS));
2148fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    ALOGV("checkInputsForDevice() direct input sup channel masks %s",
2149fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                              reply.string());
2150fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    value = strpbrk((char *)reply.string(), "=");
2151fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    if (value != NULL) {
2152fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                        loadInChannels(value + 1, profile);
2153fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    }
2154fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                }
2155fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                if (((profile->mSamplingRates[0] == 0) && (profile->mSamplingRates.size() < 2)) ||
2156fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                     ((profile->mFormats[0] == 0) && (profile->mFormats.size() < 2)) ||
2157fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                     ((profile->mChannelMasks[0] == 0) && (profile->mChannelMasks.size() < 2))) {
2158fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    ALOGW("checkInputsForDevice() direct input missing param");
2159fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    mpClientInterface->closeInput(input);
2160fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    input = 0;
2161fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                }
2162fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
2163fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                if (input != 0) {
2164fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    addInput(input, desc);
2165fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                }
2166fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            } // endif input != 0
2167fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
2168fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            if (input == 0) {
2169fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                ALOGW("checkInputsForDevice() could not open input for device 0x%X", device);
2170fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                delete desc;
2171fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                profiles.removeAt(profile_index);
2172fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                profile_index--;
2173fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            } else {
2174fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                inputs.add(input);
2175fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                ALOGV("checkInputsForDevice(): adding input %d", input);
2176fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            }
2177fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        } // end scan profiles
2178fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
2179fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        if (profiles.isEmpty()) {
2180fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
2181fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            return BAD_VALUE;
2182fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        }
2183fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    } else {
2184fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        // Disconnect
2185fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        // check if one opened input is not needed any more after disconnecting one device
2186fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
2187fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            desc = mInputs.valueAt(input_index);
2188fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            if (!(desc->mProfile->mSupportedDevices & mAvailableInputDevices)) {
2189fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                ALOGV("checkInputsForDevice(): disconnecting adding input %d",
2190fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                      mInputs.keyAt(input_index));
2191fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                inputs.add(mInputs.keyAt(input_index));
2192fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            }
2193fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        }
2194fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        // Clear any profiles associated with the disconnected device.
2195fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        for (size_t module_index = 0; module_index < mHwModules.size(); module_index++)
2196fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        {
2197fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            if (mHwModules[module_index]->mHandle == 0) {
2198fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                continue;
2199fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            }
2200fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            for (size_t profile_index = 0;
2201fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                 profile_index < mHwModules[module_index]->mInputProfiles.size();
2202fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                 profile_index++)
2203fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            {
2204fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                IOProfile *profile = mHwModules[module_index]->mInputProfiles[profile_index];
2205fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                if (profile->mSupportedDevices & device) {
2206fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    ALOGV("checkInputsForDevice(): clearing direct input profile %d on module %d",
2207fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                          profile_index, module_index);
2208fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    if (profile->mSamplingRates[0] == 0) {
2209fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                        profile->mSamplingRates.clear();
2210fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                        profile->mSamplingRates.add(0);
2211fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    }
2212fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
2213fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                        profile->mFormats.clear();
2214fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                        profile->mFormats.add(AUDIO_FORMAT_DEFAULT);
2215fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    }
2216fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    if (profile->mChannelMasks[0] == 0) {
2217fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                        profile->mChannelMasks.clear();
2218fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                        profile->mChannelMasks.add(0);
2219fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    }
2220fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                }
2221fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            }
2222fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        }
2223fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    } // end disconnect
2224fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
2225fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    return NO_ERROR;
2226fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean}
2227fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
2228b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurentvoid AudioPolicyManagerBase::closeOutput(audio_io_handle_t output)
2229f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2230b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    ALOGV("closeOutput(%d)", output);
2231f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2232b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
2233b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (outputDesc == NULL) {
2234b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        ALOGW("closeOutput() unknown output %d", output);
2235b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        return;
2236f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2237f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2238b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    // look for duplicated outputs connected to the output being removed.
2239b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    for (size_t i = 0; i < mOutputs.size(); i++) {
2240b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        AudioOutputDescriptor *dupOutputDesc = mOutputs.valueAt(i);
2241b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        if (dupOutputDesc->isDuplicated() &&
2242b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                (dupOutputDesc->mOutput1 == outputDesc ||
2243b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                dupOutputDesc->mOutput2 == outputDesc)) {
2244b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            AudioOutputDescriptor *outputDesc2;
2245b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            if (dupOutputDesc->mOutput1 == outputDesc) {
2246b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                outputDesc2 = dupOutputDesc->mOutput2;
2247b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            } else {
2248b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                outputDesc2 = dupOutputDesc->mOutput1;
2249b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            }
2250b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            // As all active tracks on duplicated output will be deleted,
2251b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            // and as they were also referenced on the other output, the reference
2252b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            // count for their stream type must be adjusted accordingly on
2253b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            // the other output.
2254b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            for (int j = 0; j < (int)AudioSystem::NUM_STREAM_TYPES; j++) {
2255b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                int refCount = dupOutputDesc->mRefCount[j];
2256b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                outputDesc2->changeRefCount((AudioSystem::stream_type)j,-refCount);
2257b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            }
2258b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i);
2259b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput);
2260f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2261b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            mpClientInterface->closeOutput(duplicatedOutput);
2262b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            delete mOutputs.valueFor(duplicatedOutput);
2263b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            mOutputs.removeItem(duplicatedOutput);
2264f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2265f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2266b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
2267b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    AudioParameter param;
2268b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    param.add(String8("closing"), String8("true"));
2269b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    mpClientInterface->setParameters(output, param.toString());
2270b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
2271b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    mpClientInterface->closeOutput(output);
22725a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent    delete outputDesc;
2273b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    mOutputs.removeItem(output);
22745a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent    mPreviousOutputs = mOutputs;
2275f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2276f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2277c952527e6f89d5427881462823514be9d79f13e6Eric LaurentSortedVector<audio_io_handle_t> AudioPolicyManagerBase::getOutputsForDevice(audio_devices_t device,
2278c952527e6f89d5427881462823514be9d79f13e6Eric Laurent                        DefaultKeyedVector<audio_io_handle_t, AudioOutputDescriptor *> openOutputs)
2279f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2280b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    SortedVector<audio_io_handle_t> outputs;
2281f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
22823cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGVV("getOutputsForDevice() device %04x", device);
2283c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    for (size_t i = 0; i < openOutputs.size(); i++) {
22843cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        ALOGVV("output %d isDuplicated=%d device=%04x",
2285c952527e6f89d5427881462823514be9d79f13e6Eric Laurent                i, openOutputs.valueAt(i)->isDuplicated(), openOutputs.valueAt(i)->supportedDevices());
2286c952527e6f89d5427881462823514be9d79f13e6Eric Laurent        if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) {
2287c952527e6f89d5427881462823514be9d79f13e6Eric Laurent            ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i));
2288c952527e6f89d5427881462823514be9d79f13e6Eric Laurent            outputs.add(openOutputs.keyAt(i));
2289f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2290f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2291b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    return outputs;
2292f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2293f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2294b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurentbool AudioPolicyManagerBase::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
2295b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                                   SortedVector<audio_io_handle_t>& outputs2)
2296f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2297b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (outputs1.size() != outputs2.size()) {
2298b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        return false;
2299f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2300b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    for (size_t i = 0; i < outputs1.size(); i++) {
2301b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        if (outputs1[i] != outputs2[i]) {
2302b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            return false;
2303f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2304f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2305b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    return true;
2306b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent}
2307b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
2308b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurentvoid AudioPolicyManagerBase::checkOutputForStrategy(routing_strategy strategy)
2309b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent{
231001e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent    audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
231101e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent    audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
2312c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs);
2313c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
2314b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
2315b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (!vectorsEqual(srcOutputs,dstOutputs)) {
2316b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d",
2317b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent              strategy, srcOutputs[0], dstOutputs[0]);
23185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // mute strategy while moving tracks from one output to another
23195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        for (size_t i = 0; i < srcOutputs.size(); i++) {
2320fa3697d716b444bbea6be480801536c44bf69214Eric Laurent            AudioOutputDescriptor *desc = mOutputs.valueFor(srcOutputs[i]);
232180f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent            if (desc->isStrategyActive(strategy)) {
2322fa3697d716b444bbea6be480801536c44bf69214Eric Laurent                setStrategyMute(strategy, true, srcOutputs[i]);
2323fa3697d716b444bbea6be480801536c44bf69214Eric Laurent                setStrategyMute(strategy, false, srcOutputs[i], MUTE_TIME_MS, newDevice);
2324fa3697d716b444bbea6be480801536c44bf69214Eric Laurent            }
23255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
2326f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2327f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // Move effects associated to this strategy from previous output to new output
23284660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen        if (strategy == STRATEGY_MEDIA) {
232945c763947b657b347211dc9388754e05d30d0467Eric Laurent            audio_io_handle_t fxOutput = selectOutputForEffects(dstOutputs);
23304660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen            SortedVector<audio_io_handle_t> moved;
23314660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen            for (size_t i = 0; i < mEffects.size(); i++) {
23324660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                EffectDescriptor *desc = mEffects.valueAt(i);
23334660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                if (desc->mSession == AUDIO_SESSION_OUTPUT_MIX &&
233445c763947b657b347211dc9388754e05d30d0467Eric Laurent                        desc->mIo != fxOutput) {
23354660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                    if (moved.indexOf(desc->mIo) < 0) {
23364660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                        ALOGV("checkOutputForStrategy() moving effect %d to output %d",
233745c763947b657b347211dc9388754e05d30d0467Eric Laurent                              mEffects.keyAt(i), fxOutput);
23384660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                        mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, desc->mIo,
233945c763947b657b347211dc9388754e05d30d0467Eric Laurent                                                       fxOutput);
23404660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                        moved.add(desc->mIo);
23414660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                    }
234245c763947b657b347211dc9388754e05d30d0467Eric Laurent                    desc->mIo = fxOutput;
23434660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                }
23444660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen            }
23454660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen        }
23465ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // Move tracks associated to this strategy from previous output to new output
2347f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        for (int i = 0; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) {
2348f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (getStrategy((AudioSystem::stream_type)i) == strategy) {
2349316222fcd60a429e53aa6205278697bb2df8ef32Glenn Kasten                mpClientInterface->invalidateStream((AudioSystem::stream_type)i);
2350f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2351f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2352f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2353f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2354f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2355f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::checkOutputForAllStrategies()
2356f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2357c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
2358f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForStrategy(STRATEGY_PHONE);
2359f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForStrategy(STRATEGY_SONIFICATION);
236012bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
2361f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForStrategy(STRATEGY_MEDIA);
2362f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForStrategy(STRATEGY_DTMF);
2363f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2364f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2365b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurentaudio_io_handle_t AudioPolicyManagerBase::getA2dpOutput()
2366b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent{
23675ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (!mHasA2dp) {
2368b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        return 0;
2369b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    }
2370b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
2371b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    for (size_t i = 0; i < mOutputs.size(); i++) {
2372b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        AudioOutputDescriptor *outputDesc = mOutputs.valueAt(i);
2373b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        if (!outputDesc->isDuplicated() && outputDesc->device() & AUDIO_DEVICE_OUT_ALL_A2DP) {
2374b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            return mOutputs.keyAt(i);
2375b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        }
2376b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    }
2377b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
2378b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    return 0;
2379b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent}
2380b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
2381f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::checkA2dpSuspend()
2382f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
23835ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (!mHasA2dp) {
2384b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        return;
2385b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    }
2386b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    audio_io_handle_t a2dpOutput = getA2dpOutput();
2387b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (a2dpOutput == 0) {
2388b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        return;
2389b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    }
2390b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
2391f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // suspend A2DP output if:
2392f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      (NOT already suspended) &&
2393f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      ((SCO device is connected &&
2394f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //       (forced usage for communication || for record is SCO))) ||
2395f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      (phone state is ringing || in call)
2396f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //
2397f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // restore A2DP output if:
2398f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      (Already suspended) &&
2399f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      ((SCO device is NOT connected ||
2400f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //       (forced usage NOT for communication && NOT for record is SCO))) &&
2401f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      (phone state is NOT ringing && NOT in call)
2402f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //
2403f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mA2dpSuspended) {
2404f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (((mScoDeviceAddress == "") ||
2405f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin             ((mForceUse[AudioSystem::FOR_COMMUNICATION] != AudioSystem::FORCE_BT_SCO) &&
2406f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin              (mForceUse[AudioSystem::FOR_RECORD] != AudioSystem::FORCE_BT_SCO))) &&
2407f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin             ((mPhoneState != AudioSystem::MODE_IN_CALL) &&
2408f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin              (mPhoneState != AudioSystem::MODE_RINGTONE))) {
2409f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2410b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            mpClientInterface->restoreOutput(a2dpOutput);
2411f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mA2dpSuspended = false;
2412f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2413f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
2414f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (((mScoDeviceAddress != "") &&
2415f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin             ((mForceUse[AudioSystem::FOR_COMMUNICATION] == AudioSystem::FORCE_BT_SCO) ||
2416f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin              (mForceUse[AudioSystem::FOR_RECORD] == AudioSystem::FORCE_BT_SCO))) ||
2417f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin             ((mPhoneState == AudioSystem::MODE_IN_CALL) ||
2418f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin              (mPhoneState == AudioSystem::MODE_RINGTONE))) {
2419f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2420b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            mpClientInterface->suspendOutput(a2dpOutput);
2421f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mA2dpSuspended = true;
2422f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2423f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2424f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2425f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2426f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurentaudio_devices_t AudioPolicyManagerBase::getNewDevice(audio_io_handle_t output, bool fromCache)
2427f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2428ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    audio_devices_t device = AUDIO_DEVICE_NONE;
2429f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2430f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
2431f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // check the following by order of priority to request a routing change if necessary:
2432c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    // 1: the strategy enforced audible is active on the output:
2433c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    //      use device for strategy enforced audible
2434c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    // 2: we are in call or the strategy phone is active on the output:
2435f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      use device for strategy phone
2436c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    // 3: the strategy sonification is active on the output:
2437f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      use device for strategy sonification
243812bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    // 4: the strategy "respectful" sonification is active on the output:
243912bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    //      use device for strategy "respectful" sonification
244012bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    // 5: the strategy media is active on the output:
2441f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      use device for strategy media
244212bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    // 6: the strategy DTMF is active on the output:
2443f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      use device for strategy DTMF
244480f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    if (outputDesc->isStrategyActive(STRATEGY_ENFORCED_AUDIBLE)) {
2445c16ac09f510437e8340be691720177a490ae78f0Eric Laurent        device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
2446c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    } else if (isInCall() ||
244780f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                    outputDesc->isStrategyActive(STRATEGY_PHONE)) {
2448f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
244980f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    } else if (outputDesc->isStrategyActive(STRATEGY_SONIFICATION)) {
2450f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
245180f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    } else if (outputDesc->isStrategyActive(STRATEGY_SONIFICATION_RESPECTFUL)) {
24525ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
245380f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    } else if (outputDesc->isStrategyActive(STRATEGY_MEDIA)) {
2454f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
245580f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    } else if (outputDesc->isStrategyActive(STRATEGY_DTMF)) {
2456f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
2457f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2458f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
24596a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("getNewDevice() selected device %x", device);
2460f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return device;
2461f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2462f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2463f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinuint32_t AudioPolicyManagerBase::getStrategyForStream(AudioSystem::stream_type stream) {
2464f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return (uint32_t)getStrategy(stream);
2465f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2466f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2467f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurentaudio_devices_t AudioPolicyManagerBase::getDevicesForStream(AudioSystem::stream_type stream) {
2468f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent    audio_devices_t devices;
2469f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // By checking the range of stream before calling getStrategy, we avoid
24705efbd421e0029d6fc44b1cc65c0e5e0d85e5161fSteve Block    // getStrategy's behavior for invalid streams.  getStrategy would do a ALOGE
2471f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // and then return STRATEGY_MEDIA, but we want to return the empty set.
2472f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (stream < (AudioSystem::stream_type) 0 || stream >= AudioSystem::NUM_STREAM_TYPES) {
2473ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        devices = AUDIO_DEVICE_NONE;
2474f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
2475f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioPolicyManagerBase::routing_strategy strategy = getStrategy(stream);
24765ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        devices = getDeviceForStrategy(strategy, true /*fromCache*/);
2477f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2478f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return devices;
2479f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2480f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2481f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima ZavinAudioPolicyManagerBase::routing_strategy AudioPolicyManagerBase::getStrategy(
2482f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioSystem::stream_type stream) {
2483f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // stream to strategy mapping
2484f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    switch (stream) {
2485f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::VOICE_CALL:
2486f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::BLUETOOTH_SCO:
2487f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return STRATEGY_PHONE;
2488f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::RING:
2489f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::ALARM:
2490f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return STRATEGY_SONIFICATION;
249112bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    case AudioSystem::NOTIFICATION:
249212bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        return STRATEGY_SONIFICATION_RESPECTFUL;
2493f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::DTMF:
2494f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return STRATEGY_DTMF;
2495f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    default:
24965efbd421e0029d6fc44b1cc65c0e5e0d85e5161fSteve Block        ALOGE("unknown stream type");
2497f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::SYSTEM:
2498f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // NOTE: SYSTEM stream uses MEDIA strategy because muting music and switching outputs
2499f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // while key clicks are played produces a poor result
2500f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::TTS:
2501f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::MUSIC:
2502f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return STRATEGY_MEDIA;
2503c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    case AudioSystem::ENFORCED_AUDIBLE:
2504c16ac09f510437e8340be691720177a490ae78f0Eric Laurent        return STRATEGY_ENFORCED_AUDIBLE;
2505f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2506f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2507f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
250812bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivivoid AudioPolicyManagerBase::handleNotificationRoutingForStream(AudioSystem::stream_type stream) {
250912bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    switch(stream) {
251012bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    case AudioSystem::MUSIC:
251112bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
2512c952527e6f89d5427881462823514be9d79f13e6Eric Laurent        updateDevicesAndOutputs();
251312bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        break;
251412bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    default:
251512bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        break;
251612bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    }
251712bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi}
251812bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi
25195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentaudio_devices_t AudioPolicyManagerBase::getDeviceForStrategy(routing_strategy strategy,
25205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                             bool fromCache)
2521f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2522ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    uint32_t device = AUDIO_DEVICE_NONE;
2523f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2524f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (fromCache) {
25253cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
25265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent              strategy, mDeviceForStrategy[strategy]);
2527f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return mDeviceForStrategy[strategy];
2528f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2529f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2530f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    switch (strategy) {
253112bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi
253212bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    case STRATEGY_SONIFICATION_RESPECTFUL:
253312bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        if (isInCall()) {
25345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
2535dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi        } else if (isStreamActiveRemotely(AudioSystem::MUSIC,
2536dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi                SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
2537dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi            // while media is playing on a remote device, use the the sonification behavior.
2538dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi            // Note that we test this usecase before testing if media is playing because
2539dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi            //   the isStreamActive() method only informs about the activity of a stream, not
2540dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi            //   if it's for local playback. Note also that we use the same delay between both tests
2541dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi            device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
254212bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        } else if (isStreamActive(AudioSystem::MUSIC, SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
254312bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi            // while media is playing (or has recently played), use the same device
25445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/);
254512bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        } else {
254612bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi            // when media is not playing anymore, fall back on the sonification behavior
25475ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
254812bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        }
254912bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi
255012bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        break;
255112bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi
2552f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case STRATEGY_DTMF:
2553f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (!isInCall()) {
2554f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // when off call, DTMF strategy follows the same rules as MEDIA strategy
25555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/);
2556f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            break;
2557f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2558f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // when in call, DTMF and PHONE strategies follow the same rules
2559f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // FALL THROUGH
2560f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2561f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case STRATEGY_PHONE:
2562f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // for phone strategy, we first consider the forced use and then the available devices by order
2563f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // of priority
2564f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        switch (mForceUse[AudioSystem::FOR_COMMUNICATION]) {
2565f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        case AudioSystem::FORCE_BT_SCO:
2566f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (!isInCall() || strategy != STRATEGY_DTMF) {
2567c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT;
2568f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (device) break;
2569f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2570c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET;
2571f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (device) break;
2572c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO;
2573f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (device) break;
2574f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // if SCO device is requested but no SCO device is available, fall back to default case
2575f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // FALL THROUGH
2576f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2577f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        default:    // FORCE_NONE
2578f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to A2DP
25791afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent            if (mHasA2dp && !isInCall() &&
25801afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent                    (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
2581ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent                    (getA2dpOutput() != 0) && !mA2dpSuspended) {
2582c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
2583f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (device) break;
2584c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
2585f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (device) break;
2586f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2587c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
25881afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent            if (device) break;
2589c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADSET;
25901afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent            if (device) break;
2591b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent            if (mPhoneState != AudioSystem::MODE_IN_CALL) {
2592b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
2593b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2594b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
2595b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2596b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
2597b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2598b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
2599b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2600b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
2601b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2602b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent            }
2603c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_EARPIECE;
26045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            if (device) break;
26055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = mDefaultOutputDevice;
2606ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            if (device == AUDIO_DEVICE_NONE) {
26075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE");
2608f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2609f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            break;
2610f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2611f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        case AudioSystem::FORCE_SPEAKER:
2612f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to
2613f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // A2DP speaker when forcing to speaker output
26141afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent            if (mHasA2dp && !isInCall() &&
26151afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent                    (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
2616ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent                    (getA2dpOutput() != 0) && !mA2dpSuspended) {
2617c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
2618f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (device) break;
2619f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2620b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent            if (mPhoneState != AudioSystem::MODE_IN_CALL) {
2621b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
2622b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2623b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
2624b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2625b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
2626b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2627b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
2628b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2629b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
2630b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2631b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent            }
2632c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER;
26335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            if (device) break;
26345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = mDefaultOutputDevice;
2635ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            if (device == AUDIO_DEVICE_NONE) {
26365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE, FORCE_SPEAKER");
2637f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2638f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            break;
2639f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2640f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    break;
2641f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2642f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case STRATEGY_SONIFICATION:
2643f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2644f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // If incall, just select the STRATEGY_PHONE device: The rest of the behavior is handled by
2645f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // handleIncallSonification().
2646f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (isInCall()) {
26475ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = getDeviceForStrategy(STRATEGY_PHONE, false /*fromCache*/);
2648f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            break;
2649f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2650c16ac09f510437e8340be691720177a490ae78f0Eric Laurent        // FALL THROUGH
2651c16ac09f510437e8340be691720177a490ae78f0Eric Laurent
2652c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    case STRATEGY_ENFORCED_AUDIBLE:
2653c16ac09f510437e8340be691720177a490ae78f0Eric Laurent        // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION
2654ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        // except:
2655ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        //   - when in call where it doesn't default to STRATEGY_PHONE behavior
2656ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        //   - in countries where not enforced in which case it follows STRATEGY_MEDIA
2657c16ac09f510437e8340be691720177a490ae78f0Eric Laurent
2658738207def5f691d605ae33d041116829a74513a9Eric Laurent        if ((strategy == STRATEGY_SONIFICATION) ||
2659738207def5f691d605ae33d041116829a74513a9Eric Laurent                (mForceUse[AudioSystem::FOR_SYSTEM] == AudioSystem::FORCE_SYSTEM_ENFORCED)) {
2660c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER;
2661ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            if (device == AUDIO_DEVICE_NONE) {
2662ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent                ALOGE("getDeviceForStrategy() speaker device not found for STRATEGY_SONIFICATION");
2663ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent            }
2664f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2665f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // The second device used for sonification is the same as the device used by media strategy
2666f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // FALL THROUGH
2667f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2668f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case STRATEGY_MEDIA: {
2669ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        uint32_t device2 = AUDIO_DEVICE_NONE;
267031363a9cb94e80330c335fede0b92b1953a09517Jean-Michel Trivi        if (strategy != STRATEGY_SONIFICATION) {
267131363a9cb94e80330c335fede0b92b1953a09517Jean-Michel Trivi            // no sonification on remote submix (e.g. WFD)
267248387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
267348387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi        }
2674ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if ((device2 == AUDIO_DEVICE_NONE) &&
267548387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi                mHasA2dp && (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
2676ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent                (getA2dpOutput() != 0) && !mA2dpSuspended) {
2677c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
2678ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            if (device2 == AUDIO_DEVICE_NONE) {
2679c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
2680f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2681ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            if (device2 == AUDIO_DEVICE_NONE) {
2682c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
2683f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2684f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2685ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (device2 == AUDIO_DEVICE_NONE) {
2686c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
26871afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent        }
2688ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (device2 == AUDIO_DEVICE_NONE) {
2689c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADSET;
26901afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent        }
2691ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (device2 == AUDIO_DEVICE_NONE) {
2692599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
2693599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent        }
2694ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (device2 == AUDIO_DEVICE_NONE) {
2695599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
2696599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent        }
2697ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (device2 == AUDIO_DEVICE_NONE) {
2698c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
2699f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
270031363a9cb94e80330c335fede0b92b1953a09517Jean-Michel Trivi        if ((device2 == AUDIO_DEVICE_NONE) && (strategy != STRATEGY_SONIFICATION)) {
270131363a9cb94e80330c335fede0b92b1953a09517Jean-Michel Trivi            // no sonification on aux digital (e.g. HDMI)
2702c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
2703f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
27045a484b753cc72d6a50c1dd3bbf68b3403c741a3aEric Laurent        if ((device2 == AUDIO_DEVICE_NONE) &&
27055a484b753cc72d6a50c1dd3bbf68b3403c741a3aEric Laurent                (mForceUse[AudioSystem::FOR_DOCK] == AudioSystem::FORCE_ANALOG_DOCK)) {
2706c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
2707f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2708ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (device2 == AUDIO_DEVICE_NONE) {
2709c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER;
2710f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2711f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2712c16ac09f510437e8340be691720177a490ae78f0Eric Laurent        // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or
2713ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise
2714f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device |= device2;
27155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (device) break;
27165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        device = mDefaultOutputDevice;
2717ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (device == AUDIO_DEVICE_NONE) {
27185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGE("getDeviceForStrategy() no device found for STRATEGY_MEDIA");
2719f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2720f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        } break;
2721f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2722f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    default:
272364cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("getDeviceForStrategy() unknown strategy: %d", strategy);
2724f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
2725f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2726f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
27273cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGVV("getDeviceForStrategy() strategy %d, device %x", strategy, device);
2728c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    return device;
2729f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2730f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2731c952527e6f89d5427881462823514be9d79f13e6Eric Laurentvoid AudioPolicyManagerBase::updateDevicesAndOutputs()
2732f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2733f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < NUM_STRATEGIES; i++) {
27345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
27355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
2736c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    mPreviousOutputs = mOutputs;
27375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
27385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
2739b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurentuint32_t AudioPolicyManagerBase::checkDeviceMuteStrategies(AudioOutputDescriptor *outputDesc,
27409029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                                                       audio_devices_t prevDevice,
27415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                       uint32_t delayMs)
27425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
27439029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    // mute/unmute strategies using an incompatible device combination
27449029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    // if muting, wait for the audio in pcm buffer to be drained before proceeding
27459029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    // if unmuting, unmute only after the specified delay
27465ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (outputDesc->isDuplicated()) {
2747b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        return 0;
27485ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
27495ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
27505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    uint32_t muteWaitMs = 0;
27515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    audio_devices_t device = outputDesc->device();
275280f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    bool shouldMute = outputDesc->isActive() && (AudioSystem::popCount(device) >= 2);
27539029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    // temporary mute output if device selection changes to avoid volume bursts due to
27549029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    // different per device volumes
275580f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    bool tempMute = outputDesc->isActive() && (device != prevDevice);
27565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
27575ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < NUM_STRATEGIES; i++) {
27585ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
27595ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        bool mute = shouldMute && (curDevice & device) && (curDevice != device);
27605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        bool doMute = false;
27615ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
27625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (mute && !outputDesc->mStrategyMutedByDevice[i]) {
27635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            doMute = true;
27645ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            outputDesc->mStrategyMutedByDevice[i] = true;
27655ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){
27665ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            doMute = true;
27675ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            outputDesc->mStrategyMutedByDevice[i] = false;
27685ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
27699029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent        if (doMute || tempMute) {
27705ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            for (size_t j = 0; j < mOutputs.size(); j++) {
27715ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                AudioOutputDescriptor *desc = mOutputs.valueAt(j);
2772f32e38e24db196671d3ea43427125a4e212466faEric Laurent                // skip output if it does not share any device with current output
2773c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                if ((desc->supportedDevices() & outputDesc->supportedDevices())
2774ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent                        == AUDIO_DEVICE_NONE) {
27755ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                    continue;
27765ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                }
27775ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                audio_io_handle_t curOutput = mOutputs.keyAt(j);
27783cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x) on output %d",
27795ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                      mute ? "muting" : "unmuting", i, curDevice, curOutput);
27805ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                setStrategyMute((routing_strategy)i, mute, curOutput, mute ? 0 : delayMs);
278180f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                if (desc->isStrategyActive((routing_strategy)i)) {
2782f32e38e24db196671d3ea43427125a4e212466faEric Laurent                    // do tempMute only for current output
2783f32e38e24db196671d3ea43427125a4e212466faEric Laurent                    if (tempMute && (desc == outputDesc)) {
278401e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                        setStrategyMute((routing_strategy)i, true, curOutput);
278501e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                        setStrategyMute((routing_strategy)i, false, curOutput,
278601e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                            desc->latency() * 2, device);
27879029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                    }
2788f32e38e24db196671d3ea43427125a4e212466faEric Laurent                    if ((tempMute && (desc == outputDesc)) || mute) {
27899029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                        if (muteWaitMs < desc->latency()) {
27909029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                            muteWaitMs = desc->latency();
27919029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                        }
27925ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                    }
27935ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                }
27945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
27955ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
27965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
27975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
27985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // FIXME: should not need to double latency if volume could be applied immediately by the
27995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // audioflinger mixer. We must account for the delay between now and the next time
28005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // the audioflinger thread for this output will process a buffer (which corresponds to
28015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // one buffer size, usually 1/2 or 1/4 of the latency).
28025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    muteWaitMs *= 2;
28035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // wait for the PCM output buffers to empty before proceeding with the rest of the command
28045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (muteWaitMs > delayMs) {
2805b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        muteWaitMs -= delayMs;
2806b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        usleep(muteWaitMs * 1000);
2807b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        return muteWaitMs;
2808f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2809b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent    return 0;
2810f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2811f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2812b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurentuint32_t AudioPolicyManagerBase::setOutputDevice(audio_io_handle_t output,
2813f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                                             audio_devices_t device,
2814f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                                             bool force,
2815f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                                             int delayMs)
2816f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
28175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGV("setOutputDevice() output %d device %04x delayMs %d", output, device, delayMs);
2818f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
28195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    AudioParameter param;
2820f32e38e24db196671d3ea43427125a4e212466faEric Laurent    uint32_t muteWaitMs;
2821f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2822f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (outputDesc->isDuplicated()) {
2823b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        muteWaitMs = setOutputDevice(outputDesc->mOutput1->mId, device, force, delayMs);
2824b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        muteWaitMs += setOutputDevice(outputDesc->mOutput2->mId, device, force, delayMs);
2825b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        return muteWaitMs;
2826f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2827f32e38e24db196671d3ea43427125a4e212466faEric Laurent    // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current
2828f32e38e24db196671d3ea43427125a4e212466faEric Laurent    // output profile
2829f32e38e24db196671d3ea43427125a4e212466faEric Laurent    if ((device != AUDIO_DEVICE_NONE) &&
2830f32e38e24db196671d3ea43427125a4e212466faEric Laurent            ((device & outputDesc->mProfile->mSupportedDevices) == 0)) {
2831f32e38e24db196671d3ea43427125a4e212466faEric Laurent        return 0;
2832f32e38e24db196671d3ea43427125a4e212466faEric Laurent    }
2833f32e38e24db196671d3ea43427125a4e212466faEric Laurent
2834f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // filter devices according to output selected
2835f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent    device = (audio_devices_t)(device & outputDesc->mProfile->mSupportedDevices);
2836f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
28375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    audio_devices_t prevDevice = outputDesc->mDevice;
28385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
28395ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGV("setOutputDevice() prevDevice %04x", prevDevice);
28405ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
2841ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if (device != AUDIO_DEVICE_NONE) {
28425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        outputDesc->mDevice = device;
28435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
28449029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs);
28455ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
2846f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // Do not change the routing if:
2847ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    //  - the requested device is AUDIO_DEVICE_NONE
2848f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //  - the requested device is the same as current device and force is not specified.
2849f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // Doing this check here allows the caller to call setOutputDevice() without conditions
2850ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if ((device == AUDIO_DEVICE_NONE || device == prevDevice) && !force) {
28515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        ALOGV("setOutputDevice() setting same device %04x or null device for output %d", device, output);
2852b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        return muteWaitMs;
2853f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2854f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
28555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGV("setOutputDevice() changing device");
2856f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // do the routing
2857f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    param.addInt(String8(AudioParameter::keyRouting), (int)device);
28585ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    mpClientInterface->setParameters(output, param.toString(), delayMs);
28595ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
2860f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // update stream volumes according to new device
2861f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    applyStreamVolumes(output, device, delayMs);
2862b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent
2863b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent    return muteWaitMs;
28645ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
2865f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
28665ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric LaurentAudioPolicyManagerBase::IOProfile *AudioPolicyManagerBase::getInputProfile(audio_devices_t device,
28675ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                   uint32_t samplingRate,
28685082dbeb19e37883230510129c94336063a4f91cGlenn Kasten                                                   audio_format_t format,
28692c3d2379ee6eadb32b63bf6de538cfd6487e8e20Glenn Kasten                                                   audio_channel_mask_t channelMask)
28705ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
28715ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // Choose an input profile based on the requested capture parameters: select the first available
28725ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // profile supporting all requested parameters.
287370c236c9290732782d5267935af1475b8d5ae602Eric Laurent    for (size_t i = 0; i < mHwModules.size(); i++)
28745ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    {
287570c236c9290732782d5267935af1475b8d5ae602Eric Laurent        if (mHwModules[i]->mHandle == 0) {
28765ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            continue;
28775ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
287870c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
28795ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        {
288070c236c9290732782d5267935af1475b8d5ae602Eric Laurent            IOProfile *profile = mHwModules[i]->mInputProfiles[j];
2881fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            // profile->log();
288270c236c9290732782d5267935af1475b8d5ae602Eric Laurent            if (profile->isCompatibleProfile(device, samplingRate, format,
28832d749b0bda4ae60ef1a940eeba86121b2e94d07dGlenn Kasten                                             channelMask, AUDIO_OUTPUT_FLAG_NONE)) {
288470c236c9290732782d5267935af1475b8d5ae602Eric Laurent                return profile;
28855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
28865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
2887f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
288870c236c9290732782d5267935af1475b8d5ae602Eric Laurent    return NULL;
2889f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2890f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2891f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurentaudio_devices_t AudioPolicyManagerBase::getDeviceForInputSource(int inputSource)
2892f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2893ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    uint32_t device = AUDIO_DEVICE_NONE;
2894f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
28959641bd36dbf0c4b1eeb84499b35c1cdb7fa5a530Eric Laurent    switch (inputSource) {
28969641bd36dbf0c4b1eeb84499b35c1cdb7fa5a530Eric Laurent    case AUDIO_SOURCE_VOICE_UPLINK:
28979641bd36dbf0c4b1eeb84499b35c1cdb7fa5a530Eric Laurent      if (mAvailableInputDevices & AUDIO_DEVICE_IN_VOICE_CALL) {
28989641bd36dbf0c4b1eeb84499b35c1cdb7fa5a530Eric Laurent          device = AUDIO_DEVICE_IN_VOICE_CALL;
28999641bd36dbf0c4b1eeb84499b35c1cdb7fa5a530Eric Laurent          break;
29009641bd36dbf0c4b1eeb84499b35c1cdb7fa5a530Eric Laurent      }
29019641bd36dbf0c4b1eeb84499b35c1cdb7fa5a530Eric Laurent      // FALL THROUGH
29029641bd36dbf0c4b1eeb84499b35c1cdb7fa5a530Eric Laurent
2903f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_DEFAULT:
2904f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_MIC:
2905f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_RECOGNITION:
2906fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent    case AUDIO_SOURCE_HOTWORD:
2907f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_COMMUNICATION:
2908f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (mForceUse[AudioSystem::FOR_RECORD] == AudioSystem::FORCE_BT_SCO &&
2909c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            mAvailableInputDevices & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
2910c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
2911c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_WIRED_HEADSET) {
2912c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = AUDIO_DEVICE_IN_WIRED_HEADSET;
2913fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_USB_DEVICE) {
2914fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            device = AUDIO_DEVICE_IN_USB_DEVICE;
2915c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_BUILTIN_MIC) {
2916c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = AUDIO_DEVICE_IN_BUILTIN_MIC;
2917f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2918f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
2919f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_CAMCORDER:
2920c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        if (mAvailableInputDevices & AUDIO_DEVICE_IN_BACK_MIC) {
2921c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = AUDIO_DEVICE_IN_BACK_MIC;
2922c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_BUILTIN_MIC) {
2923c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = AUDIO_DEVICE_IN_BUILTIN_MIC;
2924f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2925f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
2926f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_DOWNLINK:
2927f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_CALL:
2928c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        if (mAvailableInputDevices & AUDIO_DEVICE_IN_VOICE_CALL) {
2929c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = AUDIO_DEVICE_IN_VOICE_CALL;
29305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
2931f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
293248387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi    case AUDIO_SOURCE_REMOTE_SUBMIX:
293348387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi        if (mAvailableInputDevices & AUDIO_DEVICE_IN_REMOTE_SUBMIX) {
293448387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
293548387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi        }
293648387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi        break;
2937f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    default:
293864cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("getDeviceForInputSource() invalid input source %d", inputSource);
2939f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
2940f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
29416a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("getDeviceForInputSource()input source %d, device %08x", inputSource, device);
2942c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    return device;
2943f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2944f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
29456d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivibool AudioPolicyManagerBase::isVirtualInputDevice(audio_devices_t device)
29466d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi{
29476d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi    if ((device & AUDIO_DEVICE_BIT_IN) != 0) {
29486d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi        device &= ~AUDIO_DEVICE_BIT_IN;
29496d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi        if ((popcount(device) == 1) && ((device & ~APM_AUDIO_IN_DEVICE_VIRTUAL_ALL) == 0))
29506d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi            return true;
29516d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi    }
29526d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi    return false;
29536d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi}
29546d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi
29556d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Triviaudio_io_handle_t AudioPolicyManagerBase::getActiveInput(bool ignoreVirtualInputs)
2956f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2957f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mInputs.size(); i++) {
29586d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi        const AudioInputDescriptor * input_descriptor = mInputs.valueAt(i);
29596d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi        if ((input_descriptor->mRefCount > 0)
29606d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi                && (!ignoreVirtualInputs || !isVirtualInputDevice(input_descriptor->mDevice))) {
2961f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return mInputs.keyAt(i);
2962f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2963f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2964f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return 0;
2965f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2966f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2967e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2968c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentaudio_devices_t AudioPolicyManagerBase::getDeviceForVolume(audio_devices_t device)
2969e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent{
2970ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if (device == AUDIO_DEVICE_NONE) {
2971e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        // this happens when forcing a route update and no track is active on an output.
2972e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        // In this case the returned category is not important.
2973c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        device =  AUDIO_DEVICE_OUT_SPEAKER;
2974c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    } else if (AudioSystem::popCount(device) > 1) {
2975e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        // Multiple device selection is either:
2976e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        //  - speaker + one other device: give priority to speaker in this case.
2977e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        //  - one A2DP device + another device: happens with duplicated output. In this case
2978e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        // retain the device on the A2DP output as the other must not correspond to an active
2979e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        // selection if not the speaker.
2980c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        if (device & AUDIO_DEVICE_OUT_SPEAKER) {
2981c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            device = AUDIO_DEVICE_OUT_SPEAKER;
2982c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        } else {
2983c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            device = (audio_devices_t)(device & AUDIO_DEVICE_OUT_ALL_A2DP);
2984c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        }
2985e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    }
2986e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
298764cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block    ALOGW_IF(AudioSystem::popCount(device) != 1,
2988c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            "getDeviceForVolume() invalid device combination: %08x",
2989e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            device);
2990e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2991c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    return device;
2992c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent}
2993c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
2994f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric LaurentAudioPolicyManagerBase::device_category AudioPolicyManagerBase::getDeviceCategory(audio_devices_t device)
2995c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent{
2996f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent    switch(getDeviceForVolume(device)) {
2997e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_EARPIECE:
2998e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            return DEVICE_CATEGORY_EARPIECE;
2999e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_WIRED_HEADSET:
3000e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_WIRED_HEADPHONE:
3001e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_BLUETOOTH_SCO:
3002e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET:
3003e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP:
3004e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES:
3005e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            return DEVICE_CATEGORY_HEADSET;
3006e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_SPEAKER:
3007e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT:
3008e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER:
3009c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        case AUDIO_DEVICE_OUT_AUX_DIGITAL:
3010599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent        case AUDIO_DEVICE_OUT_USB_ACCESSORY:
3011599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent        case AUDIO_DEVICE_OUT_USB_DEVICE:
301248387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi        case AUDIO_DEVICE_OUT_REMOTE_SUBMIX:
3013e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        default:
3014e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            return DEVICE_CATEGORY_SPEAKER;
3015e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    }
3016e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent}
3017e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
3018f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurentfloat AudioPolicyManagerBase::volIndexToAmpl(audio_devices_t device, const StreamDescriptor& streamDesc,
3019e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        int indexInUi)
3020e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent{
3021e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    device_category deviceCategory = getDeviceCategory(device);
3022e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    const VolumeCurvePoint *curve = streamDesc.mVolumeCurve[deviceCategory];
3023e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
3024f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // the volume index in the UI is relative to the min and max volume indices for this stream type
3025e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    int nbSteps = 1 + curve[VOLMAX].mIndex -
3026e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            curve[VOLMIN].mIndex;
3027f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    int volIdx = (nbSteps * (indexInUi - streamDesc.mIndexMin)) /
3028f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            (streamDesc.mIndexMax - streamDesc.mIndexMin);
3029f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3030f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // find what part of the curve this index volume belongs to, or if it's out of bounds
3031f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    int segment = 0;
3032e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    if (volIdx < curve[VOLMIN].mIndex) {         // out of bounds
3033f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return 0.0f;
3034e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    } else if (volIdx < curve[VOLKNEE1].mIndex) {
3035f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        segment = 0;
3036e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    } else if (volIdx < curve[VOLKNEE2].mIndex) {
3037f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        segment = 1;
3038e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    } else if (volIdx <= curve[VOLMAX].mIndex) {
3039f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        segment = 2;
3040f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {                                                               // out of bounds
3041f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return 1.0f;
3042f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3043f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3044f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // linear interpolation in the attenuation table in dB
3045e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    float decibels = curve[segment].mDBAttenuation +
3046e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            ((float)(volIdx - curve[segment].mIndex)) *
3047e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent                ( (curve[segment+1].mDBAttenuation -
3048e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent                        curve[segment].mDBAttenuation) /
3049e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent                    ((float)(curve[segment+1].mIndex -
3050e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent                            curve[segment].mIndex)) );
3051f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3052f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    float amplification = exp( decibels * 0.115129f); // exp( dB * ln(10) / 20 )
3053f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
30543cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGVV("VOLUME vol index=[%d %d %d], dB=[%.1f %.1f %.1f] ampl=%.5f",
3055e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            curve[segment].mIndex, volIdx,
3056e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            curve[segment+1].mIndex,
3057e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            curve[segment].mDBAttenuation,
3058cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent            decibels,
3059e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            curve[segment+1].mDBAttenuation,
3060f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            amplification);
3061f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3062f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return amplification;
3063f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3064f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3065cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
3066e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    AudioPolicyManagerBase::sDefaultVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
3067e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    {1, -49.5f}, {33, -33.5f}, {66, -17.0f}, {100, 0.0f}
3068e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent};
3069e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
3070e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
3071e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    AudioPolicyManagerBase::sDefaultMediaVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
3072e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    {1, -58.0f}, {20, -40.0f}, {60, -17.0f}, {100, 0.0f}
3073cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent};
3074cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent
3075e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
3076e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    AudioPolicyManagerBase::sSpeakerMediaVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
3077e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    {1, -56.0f}, {20, -34.0f}, {60, -11.0f}, {100, 0.0f}
3078e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent};
3079e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
3080e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
3081e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    AudioPolicyManagerBase::sSpeakerSonificationVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
3082e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    {1, -29.7f}, {33, -20.1f}, {66, -10.2f}, {100, 0.0f}
3083e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent};
3084e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
308518fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Triviconst AudioPolicyManagerBase::VolumeCurvePoint
308618fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi    AudioPolicyManagerBase::sSpeakerSonificationVolumeCurveDrc[AudioPolicyManagerBase::VOLCNT] = {
308718fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi    {1, -35.7f}, {33, -26.1f}, {66, -13.2f}, {100, 0.0f}
308818fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi};
308918fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi
3090ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent// AUDIO_STREAM_SYSTEM, AUDIO_STREAM_ENFORCED_AUDIBLE and AUDIO_STREAM_DTMF volume tracks
309187e28f7b352fb77f46e16ebcdd85cbf01396a203Eric Laurent// AUDIO_STREAM_RING on phones and AUDIO_STREAM_MUSIC on tablets.
309287e28f7b352fb77f46e16ebcdd85cbf01396a203Eric Laurent// AUDIO_STREAM_DTMF tracks AUDIO_STREAM_VOICE_CALL while in call (See AudioService.java).
3093123897874418f9f0e48bb89386d8c470e6975f28Jean-Michel Trivi// The range is constrained between -24dB and -6dB over speaker and -30dB and -18dB over headset.
309487e28f7b352fb77f46e16ebcdd85cbf01396a203Eric Laurent
3095ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
3096ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    AudioPolicyManagerBase::sDefaultSystemVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
3097123897874418f9f0e48bb89386d8c470e6975f28Jean-Michel Trivi    {1, -24.0f}, {33, -18.0f}, {66, -12.0f}, {100, -6.0f}
3098ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent};
3099ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent
3100ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
310118fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi    AudioPolicyManagerBase::sDefaultSystemVolumeCurveDrc[AudioPolicyManagerBase::VOLCNT] = {
310218fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi    {1, -34.0f}, {33, -24.0f}, {66, -15.0f}, {100, -6.0f}
310318fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi};
310418fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi
310518fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Triviconst AudioPolicyManagerBase::VolumeCurvePoint
3106ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    AudioPolicyManagerBase::sHeadsetSystemVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
31077465678e0d5711ebcd78ae47b3a76821534a23eaEric Laurent    {1, -30.0f}, {33, -26.0f}, {66, -22.0f}, {100, -18.0f}
3108ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent};
3109e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
3110e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
31110d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent    AudioPolicyManagerBase::sDefaultVoiceVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
31120d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent    {0, -42.0f}, {33, -28.0f}, {66, -14.0f}, {100, 0.0f}
31130d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent};
31140d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent
31150d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
31160d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent    AudioPolicyManagerBase::sSpeakerVoiceVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
31170d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent    {0, -24.0f}, {33, -16.0f}, {66, -8.0f}, {100, 0.0f}
31180d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent};
31190d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent
31200d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
3121ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent            *AudioPolicyManagerBase::sVolumeProfiles[AUDIO_STREAM_CNT]
3122e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent                                                   [AudioPolicyManagerBase::DEVICE_CATEGORY_CNT] = {
3123ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_VOICE_CALL
31240d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent        sDefaultVoiceVolumeCurve, // DEVICE_CATEGORY_HEADSET
31250d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent        sSpeakerVoiceVolumeCurve, // DEVICE_CATEGORY_SPEAKER
31260d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent        sDefaultVoiceVolumeCurve  // DEVICE_CATEGORY_EARPIECE
3127e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    },
3128ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_SYSTEM
3129ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET
3130ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER
3131ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultSystemVolumeCurve  // DEVICE_CATEGORY_EARPIECE
3132ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    },
3133ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_RING
3134e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
3135e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER
3136e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sDefaultVolumeCurve  // DEVICE_CATEGORY_EARPIECE
3137e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    },
3138ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_MUSIC
3139ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultMediaVolumeCurve, // DEVICE_CATEGORY_HEADSET
3140ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sSpeakerMediaVolumeCurve, // DEVICE_CATEGORY_SPEAKER
3141ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultMediaVolumeCurve  // DEVICE_CATEGORY_EARPIECE
3142ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    },
3143ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_ALARM
314412bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
314512bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER
314612bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        sDefaultVolumeCurve  // DEVICE_CATEGORY_EARPIECE
314712bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    },
3148ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_NOTIFICATION
3149e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
3150ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER
3151e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sDefaultVolumeCurve  // DEVICE_CATEGORY_EARPIECE
3152c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    },
3153ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_BLUETOOTH_SCO
31540d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent        sDefaultVoiceVolumeCurve, // DEVICE_CATEGORY_HEADSET
31550d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent        sSpeakerVoiceVolumeCurve, // DEVICE_CATEGORY_SPEAKER
31560d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent        sDefaultVoiceVolumeCurve  // DEVICE_CATEGORY_EARPIECE
3157c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    },
3158ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_ENFORCED_AUDIBLE
3159ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET
3160ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER
3161ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultSystemVolumeCurve  // DEVICE_CATEGORY_EARPIECE
3162ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    },
3163ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    {  // AUDIO_STREAM_DTMF
3164ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET
3165ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER
3166ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultSystemVolumeCurve  // DEVICE_CATEGORY_EARPIECE
3167ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    },
3168ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_TTS
3169ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultMediaVolumeCurve, // DEVICE_CATEGORY_HEADSET
3170ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sSpeakerMediaVolumeCurve, // DEVICE_CATEGORY_SPEAKER
3171ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultMediaVolumeCurve  // DEVICE_CATEGORY_EARPIECE
3172ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    },
3173e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent};
3174e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
3175e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurentvoid AudioPolicyManagerBase::initializeVolumeCurves()
3176e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent{
3177ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    for (int i = 0; i < AUDIO_STREAM_CNT; i++) {
3178e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) {
3179cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent            mStreams[i].mVolumeCurve[j] =
3180ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent                    sVolumeProfiles[i][j];
3181cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent        }
3182cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent    }
318318fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi
318418fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi    // Check availability of DRC on speaker path: if available, override some of the speaker curves
318518fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi    if (mSpeakerDrcEnabled) {
318618fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi        mStreams[AUDIO_STREAM_SYSTEM].mVolumeCurve[DEVICE_CATEGORY_SPEAKER] =
318718fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi                sDefaultSystemVolumeCurveDrc;
318818fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi        mStreams[AUDIO_STREAM_RING].mVolumeCurve[DEVICE_CATEGORY_SPEAKER] =
318918fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi                sSpeakerSonificationVolumeCurveDrc;
319018fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi        mStreams[AUDIO_STREAM_ALARM].mVolumeCurve[DEVICE_CATEGORY_SPEAKER] =
319118fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi                sSpeakerSonificationVolumeCurveDrc;
319218fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi        mStreams[AUDIO_STREAM_NOTIFICATION].mVolumeCurve[DEVICE_CATEGORY_SPEAKER] =
319318fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi                sSpeakerSonificationVolumeCurveDrc;
319418fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi    }
3195f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3196f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3197c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentfloat AudioPolicyManagerBase::computeVolume(int stream,
3198c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                            int index,
3199c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                            audio_io_handle_t output,
3200f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                                            audio_devices_t device)
3201f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3202f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    float volume = 1.0;
3203f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
3204f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    StreamDescriptor &streamDesc = mStreams[stream];
3205f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3206ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if (device == AUDIO_DEVICE_NONE) {
3207f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device = outputDesc->device();
3208f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3209f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3210f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if volume is not 0 (not muted), force media volume to max on digital output
3211f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (stream == AudioSystem::MUSIC &&
3212f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        index != mStreams[stream].mIndexMin &&
3213f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent        (device == AUDIO_DEVICE_OUT_AUX_DIGITAL ||
3214599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent         device == AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET ||
3215599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent         device == AUDIO_DEVICE_OUT_USB_ACCESSORY ||
3216599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent         device == AUDIO_DEVICE_OUT_USB_DEVICE)) {
3217f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return 1.0;
3218f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3219f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3220f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    volume = volIndexToAmpl(device, streamDesc, index);
3221f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3222f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if a headset is connected, apply the following rules to ring tones and notifications
3223f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // to avoid sound level bursts in user's ears:
3224f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // - always attenuate ring tones and notifications volume by 6dB
3225f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // - if music is playing, always limit the volume to current music volume,
3226f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // with a minimum threshold at -36dB so that notification is always perceived.
322712bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    const routing_strategy stream_strategy = getStrategy((AudioSystem::stream_type)stream);
3228c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
3229c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
3230c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            AUDIO_DEVICE_OUT_WIRED_HEADSET |
3231c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) &&
323212bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        ((stream_strategy == STRATEGY_SONIFICATION)
323312bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi                || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL)
3234738207def5f691d605ae33d041116829a74513a9Eric Laurent                || (stream == AudioSystem::SYSTEM)
3235738207def5f691d605ae33d041116829a74513a9Eric Laurent                || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) &&
3236738207def5f691d605ae33d041116829a74513a9Eric Laurent                    (mForceUse[AudioSystem::FOR_SYSTEM] == AudioSystem::FORCE_NONE))) &&
3237f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        streamDesc.mCanBeMuted) {
3238f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        volume *= SONIFICATION_HEADSET_VOLUME_FACTOR;
3239f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // when the phone is ringing we must consider that music could have been paused just before
3240f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // by the music application and behave as if music was active if the last music track was
3241f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // just stopped
3242ac3cf10ef6de12e69540a1244ac7255f93fa7502Eric Laurent        if (isStreamActive(AudioSystem::MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) ||
3243ac3cf10ef6de12e69540a1244ac7255f93fa7502Eric Laurent                mLimitRingtoneVolume) {
324417a73c3394547692457299dc512b5c2312ea0344Eric Laurent            audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/);
3245c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            float musicVol = computeVolume(AudioSystem::MUSIC,
324617a73c3394547692457299dc512b5c2312ea0344Eric Laurent                               mStreams[AudioSystem::MUSIC].getVolumeIndex(musicDevice),
3247c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                               output,
324817a73c3394547692457299dc512b5c2312ea0344Eric Laurent                               musicDevice);
3249c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            float minVol = (musicVol > SONIFICATION_HEADSET_VOLUME_MIN) ?
3250c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                musicVol : SONIFICATION_HEADSET_VOLUME_MIN;
3251f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (volume > minVol) {
3252f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                volume = minVol;
32536a70518b93928d1c91457ff805e375c82d76b0e5Steve Block                ALOGV("computeVolume limiting volume to %f musicVol %f", minVol, musicVol);
3254f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
3255f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
3256f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3257f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3258f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return volume;
3259f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3260f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3261c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentstatus_t AudioPolicyManagerBase::checkAndSetVolume(int stream,
3262c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                   int index,
3263c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                   audio_io_handle_t output,
3264f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                                                   audio_devices_t device,
3265c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                   int delayMs,
3266c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                   bool force)
3267f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3268f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3269f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // do not change actual stream volume if the stream is muted
3270f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mOutputs.valueFor(output)->mMuteCount[stream] != 0) {
32713cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        ALOGVV("checkAndSetVolume() stream %d muted count %d",
32725ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent              stream, mOutputs.valueFor(output)->mMuteCount[stream]);
3273f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return NO_ERROR;
3274f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3275f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3276f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // do not change in call volume if bluetooth is connected and vice versa
3277f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if ((stream == AudioSystem::VOICE_CALL && mForceUse[AudioSystem::FOR_COMMUNICATION] == AudioSystem::FORCE_BT_SCO) ||
3278f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        (stream == AudioSystem::BLUETOOTH_SCO && mForceUse[AudioSystem::FOR_COMMUNICATION] != AudioSystem::FORCE_BT_SCO)) {
32796a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
3280f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin             stream, mForceUse[AudioSystem::FOR_COMMUNICATION]);
3281f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return INVALID_OPERATION;
3282f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3283f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3284f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    float volume = computeVolume(stream, index, output, device);
3285f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // We actually change the volume if:
3286f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // - the float value returned by computeVolume() changed
3287f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // - the force flag is set
3288f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (volume != mOutputs.valueFor(output)->mCurVolume[stream] ||
3289f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            force) {
3290f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mOutputs.valueFor(output)->mCurVolume[stream] = volume;
32913cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        ALOGVV("checkAndSetVolume() for output %d stream %d, volume %f, delay %d", output, stream, volume, delayMs);
32920d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent        // Force VOICE_CALL to track BLUETOOTH_SCO stream volume when bluetooth audio is
32930d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent        // enabled
32940d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent        if (stream == AudioSystem::BLUETOOTH_SCO) {
32950d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent            mpClientInterface->setStreamVolume(AudioSystem::VOICE_CALL, volume, output, delayMs);
3296f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
3297f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mpClientInterface->setStreamVolume((AudioSystem::stream_type)stream, volume, output, delayMs);
3298f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3299f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3300f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (stream == AudioSystem::VOICE_CALL ||
3301f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        stream == AudioSystem::BLUETOOTH_SCO) {
3302f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        float voiceVolume;
3303f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // Force voice volume to max for bluetooth SCO as volume is managed by the headset
3304f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (stream == AudioSystem::VOICE_CALL) {
3305f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            voiceVolume = (float)index/(float)mStreams[stream].mIndexMax;
3306f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        } else {
3307f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            voiceVolume = 1.0;
3308f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
3309f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3310b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        if (voiceVolume != mLastVoiceVolume && output == mPrimaryOutput) {
3311f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
3312f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mLastVoiceVolume = voiceVolume;
3313f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
3314f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3315f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3316f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
3317f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3318f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3319c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentvoid AudioPolicyManagerBase::applyStreamVolumes(audio_io_handle_t output,
3320f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                                                audio_devices_t device,
3321c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                int delayMs,
3322c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                bool force)
3323f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
33243cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGVV("applyStreamVolumes() for output %d and device %x", output, device);
3325f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3326f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
3327c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        checkAndSetVolume(stream,
3328f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                          mStreams[stream].getVolumeIndex(device),
3329c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                          output,
3330c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                          device,
3331c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                          delayMs,
3332c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                          force);
3333f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3334f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3335f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
333601e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurentvoid AudioPolicyManagerBase::setStrategyMute(routing_strategy strategy,
333701e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                             bool on,
333801e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                             audio_io_handle_t output,
333901e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                             int delayMs,
334001e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                             audio_devices_t device)
3341f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
33423cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGVV("setStrategyMute() strategy %d, mute %d, output %d", strategy, on, output);
3343f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
3344f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (getStrategy((AudioSystem::stream_type)stream) == strategy) {
334501e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent            setStreamMute(stream, on, output, delayMs, device);
3346f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
3347f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3348f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3349f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
335001e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurentvoid AudioPolicyManagerBase::setStreamMute(int stream,
335101e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                           bool on,
335201e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                           audio_io_handle_t output,
335301e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                           int delayMs,
335401e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                           audio_devices_t device)
3355f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3356f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    StreamDescriptor &streamDesc = mStreams[stream];
3357f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
3358ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if (device == AUDIO_DEVICE_NONE) {
335901e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent        device = outputDesc->device();
336001e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent    }
3361f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
33623cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGVV("setStreamMute() stream %d, mute %d, output %d, mMuteCount %d device %04x",
336301e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent          stream, on, output, outputDesc->mMuteCount[stream], device);
3364f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3365f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (on) {
3366f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (outputDesc->mMuteCount[stream] == 0) {
3367738207def5f691d605ae33d041116829a74513a9Eric Laurent            if (streamDesc.mCanBeMuted &&
3368738207def5f691d605ae33d041116829a74513a9Eric Laurent                    ((stream != AudioSystem::ENFORCED_AUDIBLE) ||
3369738207def5f691d605ae33d041116829a74513a9Eric Laurent                     (mForceUse[AudioSystem::FOR_SYSTEM] == AudioSystem::FORCE_NONE))) {
3370c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                checkAndSetVolume(stream, 0, output, device, delayMs);
3371f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
3372f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
3373f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
3374f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mMuteCount[stream]++;
3375f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
3376f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (outputDesc->mMuteCount[stream] == 0) {
33773cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            ALOGV("setStreamMute() unmuting non muted stream!");
3378f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return;
3379f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
3380f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (--outputDesc->mMuteCount[stream] == 0) {
3381c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            checkAndSetVolume(stream,
3382c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                              streamDesc.getVolumeIndex(device),
3383c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                              output,
3384c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                              device,
3385c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                              delayMs);
3386f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
3387f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3388f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3389f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3390f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::handleIncallSonification(int stream, bool starting, bool stateChange)
3391f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3392f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if the stream pertains to sonification strategy and we are in call we must
3393f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // mute the stream if it is low visibility. If it is high visibility, we must play a tone
3394f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // in the device used for phone strategy and play the tone if the selected device does not
3395f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // interfere with the device used for phone strategy
3396f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
3397f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // many times as there are active tracks on the output
339812bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    const routing_strategy stream_strategy = getStrategy((AudioSystem::stream_type)stream);
339912bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    if ((stream_strategy == STRATEGY_SONIFICATION) ||
340012bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi            ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
3401b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        AudioOutputDescriptor *outputDesc = mOutputs.valueFor(mPrimaryOutput);
34026a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
3403f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                stream, starting, outputDesc->mDevice, stateChange);
3404f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (outputDesc->mRefCount[stream]) {
3405f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            int muteCount = 1;
3406f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (stateChange) {
3407f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                muteCount = outputDesc->mRefCount[stream];
3408f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
3409f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (AudioSystem::isLowVisibility((AudioSystem::stream_type)stream)) {
34106a70518b93928d1c91457ff805e375c82d76b0e5Steve Block                ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
3411f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                for (int i = 0; i < muteCount; i++) {
3412b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                    setStreamMute(stream, starting, mPrimaryOutput);
3413f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
3414f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            } else {
34156a70518b93928d1c91457ff805e375c82d76b0e5Steve Block                ALOGV("handleIncallSonification() high visibility");
34165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                if (outputDesc->device() &
34175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                        getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
34186a70518b93928d1c91457ff805e375c82d76b0e5Steve Block                    ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
3419f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    for (int i = 0; i < muteCount; i++) {
3420b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                        setStreamMute(stream, starting, mPrimaryOutput);
3421f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    }
3422f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
3423f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (starting) {
3424f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    mpClientInterface->startTone(ToneGenerator::TONE_SUP_CALL_WAITING, AudioSystem::VOICE_CALL);
3425f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else {
3426f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    mpClientInterface->stopTone();
3427f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
3428f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
3429f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
3430f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3431f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3432f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3433f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinbool AudioPolicyManagerBase::isInCall()
3434f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3435f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return isStateInCall(mPhoneState);
3436f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3437f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3438f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinbool AudioPolicyManagerBase::isStateInCall(int state) {
3439f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return ((state == AudioSystem::MODE_IN_CALL) ||
3440f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            (state == AudioSystem::MODE_IN_COMMUNICATION));
3441f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3442f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3443f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinuint32_t AudioPolicyManagerBase::getMaxEffectsCpuLoad()
3444f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3445f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return MAX_EFFECTS_CPU_LOAD;
3446f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3447f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3448f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinuint32_t AudioPolicyManagerBase::getMaxEffectsMemory()
3449f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3450f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return MAX_EFFECTS_MEMORY;
3451f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3452f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3453f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// --- AudioOutputDescriptor class implementation
3454f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3455b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric LaurentAudioPolicyManagerBase::AudioOutputDescriptor::AudioOutputDescriptor(
34565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        const IOProfile *profile)
34572d749b0bda4ae60ef1a940eeba86121b2e94d07dGlenn Kasten    : mId(0), mSamplingRate(0), mFormat(AUDIO_FORMAT_DEFAULT),
34582c3d2379ee6eadb32b63bf6de538cfd6487e8e20Glenn Kasten      mChannelMask(0), mLatency(0),
3459ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    mFlags((audio_output_flags_t)0), mDevice(AUDIO_DEVICE_NONE),
34605a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent    mOutput1(0), mOutput2(0), mProfile(profile), mDirectOpenCount(0)
3461f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3462f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // clear usage count for all stream types
3463f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) {
3464f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mRefCount[i] = 0;
3465f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mCurVolume[i] = -1.0;
3466f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mMuteCount[i] = 0;
3467f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mStopTime[i] = 0;
3468f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
346985ad78f27ca032e90af0f2100659d12c16322c76Marco Nelissen    for (int i = 0; i < NUM_STRATEGIES; i++) {
347085ad78f27ca032e90af0f2100659d12c16322c76Marco Nelissen        mStrategyMutedByDevice[i] = false;
347185ad78f27ca032e90af0f2100659d12c16322c76Marco Nelissen    }
34723cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    if (profile != NULL) {
34733cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        mSamplingRate = profile->mSamplingRates[0];
34743cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        mFormat = profile->mFormats[0];
34753cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        mChannelMask = profile->mChannelMasks[0];
34763cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        mFlags = profile->mFlags;
34773cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    }
3478f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3479f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3480dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Triviaudio_devices_t AudioPolicyManagerBase::AudioOutputDescriptor::device() const
3481f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3482f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (isDuplicated()) {
3483f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent        return (audio_devices_t)(mOutput1->mDevice | mOutput2->mDevice);
3484f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
3485f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent        return mDevice;
3486f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3487f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3488f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
34895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentuint32_t AudioPolicyManagerBase::AudioOutputDescriptor::latency()
34905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
34915ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (isDuplicated()) {
34925ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return (mOutput1->mLatency > mOutput2->mLatency) ? mOutput1->mLatency : mOutput2->mLatency;
34935ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    } else {
34945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return mLatency;
34955ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
34965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
34975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
34985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentbool AudioPolicyManagerBase::AudioOutputDescriptor::sharesHwModuleWith(
34995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        const AudioOutputDescriptor *outputDesc)
35005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
35015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (isDuplicated()) {
35025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return mOutput1->sharesHwModuleWith(outputDesc) || mOutput2->sharesHwModuleWith(outputDesc);
35035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    } else if (outputDesc->isDuplicated()){
35045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return sharesHwModuleWith(outputDesc->mOutput1) || sharesHwModuleWith(outputDesc->mOutput2);
35055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    } else {
350670c236c9290732782d5267935af1475b8d5ae602Eric Laurent        return (mProfile->mModule == outputDesc->mProfile->mModule);
35075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
35085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
35095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
3510f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::AudioOutputDescriptor::changeRefCount(AudioSystem::stream_type stream, int delta)
3511f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3512f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // forward usage count change to attached outputs
3513f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (isDuplicated()) {
3514f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mOutput1->changeRefCount(stream, delta);
3515f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mOutput2->changeRefCount(stream, delta);
3516f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3517f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if ((delta + (int)mRefCount[stream]) < 0) {
351864cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("changeRefCount() invalid delta %d for stream %d, refCount %d", delta, stream, mRefCount[stream]);
3519f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mRefCount[stream] = 0;
3520f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
3521f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3522f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mRefCount[stream] += delta;
35236a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("changeRefCount() stream %d, count %d", stream, mRefCount[stream]);
3524f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3525f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3526f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurentaudio_devices_t AudioPolicyManagerBase::AudioOutputDescriptor::supportedDevices()
3527b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent{
3528b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (isDuplicated()) {
3529f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent        return (audio_devices_t)(mOutput1->supportedDevices() | mOutput2->supportedDevices());
3530b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    } else {
3531b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        return mProfile->mSupportedDevices ;
3532b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    }
3533b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent}
3534b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
353542fa8215a763822c0d03fc936e4cac1eb864c9ccEric Laurentbool AudioPolicyManagerBase::AudioOutputDescriptor::isActive(uint32_t inPastMs) const
353642fa8215a763822c0d03fc936e4cac1eb864c9ccEric Laurent{
353780f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    return isStrategyActive(NUM_STRATEGIES, inPastMs);
353880f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent}
353980f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent
354080f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurentbool AudioPolicyManagerBase::AudioOutputDescriptor::isStrategyActive(routing_strategy strategy,
354180f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                                                                       uint32_t inPastMs,
354280f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                                                                       nsecs_t sysTime) const
354380f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent{
354480f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    if ((sysTime == 0) && (inPastMs != 0)) {
354580f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent        sysTime = systemTime();
354680f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    }
354742fa8215a763822c0d03fc936e4cac1eb864c9ccEric Laurent    for (int i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) {
354880f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent        if (((getStrategy((AudioSystem::stream_type)i) == strategy) ||
354980f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                (NUM_STRATEGIES == strategy)) &&
355080f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                isStreamActive((AudioSystem::stream_type)i, inPastMs, sysTime)) {
355142fa8215a763822c0d03fc936e4cac1eb864c9ccEric Laurent            return true;
355242fa8215a763822c0d03fc936e4cac1eb864c9ccEric Laurent        }
355342fa8215a763822c0d03fc936e4cac1eb864c9ccEric Laurent    }
355442fa8215a763822c0d03fc936e4cac1eb864c9ccEric Laurent    return false;
355542fa8215a763822c0d03fc936e4cac1eb864c9ccEric Laurent}
355642fa8215a763822c0d03fc936e4cac1eb864c9ccEric Laurent
355780f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurentbool AudioPolicyManagerBase::AudioOutputDescriptor::isStreamActive(AudioSystem::stream_type stream,
355880f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                                                                       uint32_t inPastMs,
355980f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                                                                       nsecs_t sysTime) const
356080f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent{
356180f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    if (mRefCount[stream] != 0) {
356280f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent        return true;
356380f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    }
356480f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    if (inPastMs == 0) {
356580f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent        return false;
356680f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    }
356780f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    if (sysTime == 0) {
356880f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent        sysTime = systemTime();
356980f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    }
357080f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    if (ns2ms(sysTime - mStopTime[stream]) < inPastMs) {
357180f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent        return true;
357280f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    }
357380f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    return false;
357480f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent}
357580f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent
357680f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent
3577f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::AudioOutputDescriptor::dump(int fd)
3578f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3579f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    const size_t SIZE = 256;
3580f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    char buffer[SIZE];
3581f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    String8 result;
3582f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3583f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Sampling rate: %d\n", mSamplingRate);
3584f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3585a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    snprintf(buffer, SIZE, " Format: %08x\n", mFormat);
3586f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
358770c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, " Channels: %08x\n", mChannelMask);
3588f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3589f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Latency: %d\n", mLatency);
3590f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3591f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Flags %08x\n", mFlags);
3592f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3593f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Devices %08x\n", device());
3594f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3595f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Stream volume refCount muteCount\n");
3596f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3597f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) {
3598f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        snprintf(buffer, SIZE, " %02d     %.03f     %02d       %02d\n", i, mCurVolume[i], mRefCount[i], mMuteCount[i]);
3599f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        result.append(buffer);
3600f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3601f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, result.string(), result.size());
3602f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3603f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
3604f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3605f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3606f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// --- AudioInputDescriptor class implementation
3607f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
36085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric LaurentAudioPolicyManagerBase::AudioInputDescriptor::AudioInputDescriptor(const IOProfile *profile)
3609fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    :  mId(0), mSamplingRate(0), mFormat(AUDIO_FORMAT_DEFAULT), mChannelMask(0),
3610ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent      mDevice(AUDIO_DEVICE_NONE), mRefCount(0),
36115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent      mInputSource(0), mProfile(profile)
3612f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3613fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    if (profile != NULL) {
3614fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean         mSamplingRate = profile->mSamplingRates[0];
3615fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean         mFormat = profile->mFormats[0];
3616fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean         mChannelMask = profile->mChannelMasks[0];
3617fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean     }
3618f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3619f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3620f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::AudioInputDescriptor::dump(int fd)
3621f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3622f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    const size_t SIZE = 256;
3623f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    char buffer[SIZE];
3624f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    String8 result;
3625f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3626f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Sampling rate: %d\n", mSamplingRate);
3627f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3628f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Format: %d\n", mFormat);
3629f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
363070c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, " Channels: %08x\n", mChannelMask);
3631f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3632f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Devices %08x\n", mDevice);
3633f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3634f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Ref Count %d\n", mRefCount);
3635f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3636f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, result.string(), result.size());
3637f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3638f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
3639f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3640f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3641f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// --- StreamDescriptor class implementation
3642f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3643c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric LaurentAudioPolicyManagerBase::StreamDescriptor::StreamDescriptor()
3644c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    :   mIndexMin(0), mIndexMax(1), mCanBeMuted(true)
3645c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent{
3646c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    mIndexCur.add(AUDIO_DEVICE_OUT_DEFAULT, 0);
3647c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent}
3648c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
3649c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentint AudioPolicyManagerBase::StreamDescriptor::getVolumeIndex(audio_devices_t device)
3650c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent{
3651c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    device = AudioPolicyManagerBase::getDeviceForVolume(device);
3652c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    // there is always a valid entry for AUDIO_DEVICE_OUT_DEFAULT
3653c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    if (mIndexCur.indexOfKey(device) < 0) {
3654c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        device = AUDIO_DEVICE_OUT_DEFAULT;
3655c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    }
3656c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    return mIndexCur.valueFor(device);
3657c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent}
3658c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
3659c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentvoid AudioPolicyManagerBase::StreamDescriptor::dump(int fd)
3660f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3661c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    const size_t SIZE = 256;
3662c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    char buffer[SIZE];
3663c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    String8 result;
3664c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
3665c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    snprintf(buffer, SIZE, "%s         %02d         %02d         ",
3666c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent             mCanBeMuted ? "true " : "false", mIndexMin, mIndexMax);
3667c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    result.append(buffer);
3668c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    for (size_t i = 0; i < mIndexCur.size(); i++) {
3669c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        snprintf(buffer, SIZE, "%04x : %02d, ",
3670c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                 mIndexCur.keyAt(i),
3671c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                 mIndexCur.valueAt(i));
3672c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        result.append(buffer);
3673c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    }
3674c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    result.append("\n");
3675c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
3676c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    write(fd, result.string(), result.size());
3677f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3678f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3679f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// --- EffectDescriptor class implementation
3680f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3681f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::EffectDescriptor::dump(int fd)
3682f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3683f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    const size_t SIZE = 256;
3684f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    char buffer[SIZE];
3685f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    String8 result;
3686f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
36871c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent    snprintf(buffer, SIZE, " I/O: %d\n", mIo);
3688f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3689f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Strategy: %d\n", mStrategy);
3690f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3691f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Session: %d\n", mSession);
3692f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3693f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Name: %s\n",  mDesc.name);
3694f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3695582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    snprintf(buffer, SIZE, " %s\n",  mEnabled ? "Enabled" : "Disabled");
3696582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    result.append(buffer);
3697f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, result.string(), result.size());
3698f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3699f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
3700f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3701f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
37025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent// --- IOProfile class implementation
37035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
370470c236c9290732782d5267935af1475b8d5ae602Eric LaurentAudioPolicyManagerBase::HwModule::HwModule(const char *name)
370570c236c9290732782d5267935af1475b8d5ae602Eric Laurent    : mName(strndup(name, AUDIO_HARDWARE_MODULE_ID_MAX_LEN)), mHandle(0)
370670c236c9290732782d5267935af1475b8d5ae602Eric Laurent{
370770c236c9290732782d5267935af1475b8d5ae602Eric Laurent}
370870c236c9290732782d5267935af1475b8d5ae602Eric Laurent
370970c236c9290732782d5267935af1475b8d5ae602Eric LaurentAudioPolicyManagerBase::HwModule::~HwModule()
371070c236c9290732782d5267935af1475b8d5ae602Eric Laurent{
371170c236c9290732782d5267935af1475b8d5ae602Eric Laurent    for (size_t i = 0; i < mOutputProfiles.size(); i++) {
371270c236c9290732782d5267935af1475b8d5ae602Eric Laurent         delete mOutputProfiles[i];
371370c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
371470c236c9290732782d5267935af1475b8d5ae602Eric Laurent    for (size_t i = 0; i < mInputProfiles.size(); i++) {
371570c236c9290732782d5267935af1475b8d5ae602Eric Laurent         delete mInputProfiles[i];
371670c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
371770c236c9290732782d5267935af1475b8d5ae602Eric Laurent    free((void *)mName);
371870c236c9290732782d5267935af1475b8d5ae602Eric Laurent}
371970c236c9290732782d5267935af1475b8d5ae602Eric Laurent
372070c236c9290732782d5267935af1475b8d5ae602Eric Laurentvoid AudioPolicyManagerBase::HwModule::dump(int fd)
372170c236c9290732782d5267935af1475b8d5ae602Eric Laurent{
372270c236c9290732782d5267935af1475b8d5ae602Eric Laurent    const size_t SIZE = 256;
372370c236c9290732782d5267935af1475b8d5ae602Eric Laurent    char buffer[SIZE];
372470c236c9290732782d5267935af1475b8d5ae602Eric Laurent    String8 result;
372570c236c9290732782d5267935af1475b8d5ae602Eric Laurent
372670c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "  - name: %s\n", mName);
372770c236c9290732782d5267935af1475b8d5ae602Eric Laurent    result.append(buffer);
372870c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "  - handle: %d\n", mHandle);
372970c236c9290732782d5267935af1475b8d5ae602Eric Laurent    result.append(buffer);
373070c236c9290732782d5267935af1475b8d5ae602Eric Laurent    write(fd, result.string(), result.size());
373170c236c9290732782d5267935af1475b8d5ae602Eric Laurent    if (mOutputProfiles.size()) {
37324387d30660db3c9a10b16b3a4a1c986c330e788bPatrick Tjin        write(fd, "  - outputs:\n", strlen("  - outputs:\n"));
373370c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (size_t i = 0; i < mOutputProfiles.size(); i++) {
3734c7b6e3c857c5f5af5f244d0b877c41a9c55f92abColin Cross            snprintf(buffer, SIZE, "    output %zu:\n", i);
3735599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            write(fd, buffer, strlen(buffer));
373670c236c9290732782d5267935af1475b8d5ae602Eric Laurent            mOutputProfiles[i]->dump(fd);
373770c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
373870c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
373970c236c9290732782d5267935af1475b8d5ae602Eric Laurent    if (mInputProfiles.size()) {
37404387d30660db3c9a10b16b3a4a1c986c330e788bPatrick Tjin        write(fd, "  - inputs:\n", strlen("  - inputs:\n"));
374170c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (size_t i = 0; i < mInputProfiles.size(); i++) {
3742c7b6e3c857c5f5af5f244d0b877c41a9c55f92abColin Cross            snprintf(buffer, SIZE, "    input %zu:\n", i);
3743599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            write(fd, buffer, strlen(buffer));
374470c236c9290732782d5267935af1475b8d5ae602Eric Laurent            mInputProfiles[i]->dump(fd);
374570c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
374670c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
374770c236c9290732782d5267935af1475b8d5ae602Eric Laurent}
374870c236c9290732782d5267935af1475b8d5ae602Eric Laurent
374970c236c9290732782d5267935af1475b8d5ae602Eric LaurentAudioPolicyManagerBase::IOProfile::IOProfile(HwModule *module)
37500977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent    : mFlags((audio_output_flags_t)0), mModule(module)
37515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
37525ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
37535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
37545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric LaurentAudioPolicyManagerBase::IOProfile::~IOProfile()
37555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
375670c236c9290732782d5267935af1475b8d5ae602Eric Laurent}
375770c236c9290732782d5267935af1475b8d5ae602Eric Laurent
3758b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent// checks if the IO profile is compatible with specified parameters.
3759b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent// Sampling rate, format and channel mask must be specified in order to
3760b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent// get a valid a match
376170c236c9290732782d5267935af1475b8d5ae602Eric Laurentbool AudioPolicyManagerBase::IOProfile::isCompatibleProfile(audio_devices_t device,
376270c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                            uint32_t samplingRate,
37635082dbeb19e37883230510129c94336063a4f91cGlenn Kasten                                                            audio_format_t format,
37642c3d2379ee6eadb32b63bf6de538cfd6487e8e20Glenn Kasten                                                            audio_channel_mask_t channelMask,
37650977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent                                                            audio_output_flags_t flags) const
376670c236c9290732782d5267935af1475b8d5ae602Eric Laurent{
37672d749b0bda4ae60ef1a940eeba86121b2e94d07dGlenn Kasten    if (samplingRate == 0 || !audio_is_valid_format(format) || channelMask == 0) {
3768b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent         return false;
3769b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     }
3770b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent
3771b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     if ((mSupportedDevices & device) != device) {
3772b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent         return false;
3773b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     }
3774b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     if ((mFlags & flags) != flags) {
3775b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent         return false;
3776b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     }
3777b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     size_t i;
3778b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     for (i = 0; i < mSamplingRates.size(); i++)
3779b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     {
3780b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent         if (mSamplingRates[i] == samplingRate) {
3781b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent             break;
3782b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent         }
3783b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     }
3784b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     if (i == mSamplingRates.size()) {
3785b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent         return false;
3786b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     }
3787b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     for (i = 0; i < mFormats.size(); i++)
3788b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     {
3789b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent         if (mFormats[i] == format) {
3790b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent             break;
3791b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent         }
3792b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     }
3793b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     if (i == mFormats.size()) {
3794b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent         return false;
3795b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     }
3796b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     for (i = 0; i < mChannelMasks.size(); i++)
3797b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     {
3798b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent         if (mChannelMasks[i] == channelMask) {
3799b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent             break;
3800b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent         }
3801b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     }
3802b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     if (i == mChannelMasks.size()) {
3803b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent         return false;
3804b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     }
3805b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     return true;
38065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
38075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
38085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::IOProfile::dump(int fd)
38095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
38105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    const size_t SIZE = 256;
38115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    char buffer[SIZE];
38125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    String8 result;
38135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
381470c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "    - sampling rates: ");
38155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    result.append(buffer);
38165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < mSamplingRates.size(); i++) {
38175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        snprintf(buffer, SIZE, "%d", mSamplingRates[i]);
38185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        result.append(buffer);
38195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        result.append(i == (mSamplingRates.size() - 1) ? "\n" : ", ");
38205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
38215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
382270c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "    - channel masks: ");
38235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    result.append(buffer);
38245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < mChannelMasks.size(); i++) {
3825a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        snprintf(buffer, SIZE, "0x%04x", mChannelMasks[i]);
38265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        result.append(buffer);
38275ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        result.append(i == (mChannelMasks.size() - 1) ? "\n" : ", ");
38285ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
38295ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
383070c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "    - formats: ");
38315ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    result.append(buffer);
38325ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < mFormats.size(); i++) {
3833a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        snprintf(buffer, SIZE, "0x%08x", mFormats[i]);
38345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        result.append(buffer);
38355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        result.append(i == (mFormats.size() - 1) ? "\n" : ", ");
38365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
38375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
3838a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    snprintf(buffer, SIZE, "    - devices: 0x%04x\n", mSupportedDevices);
38395ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    result.append(buffer);
3840a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    snprintf(buffer, SIZE, "    - flags: 0x%04x\n", mFlags);
38415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    result.append(buffer);
38425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
38435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    write(fd, result.string(), result.size());
38445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
38455ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
3846fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLeanvoid AudioPolicyManagerBase::IOProfile::log()
3847fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean{
3848fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    const size_t SIZE = 256;
3849fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    char buffer[SIZE];
3850fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    String8 result;
3851fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
3852fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    ALOGV("    - sampling rates: ");
3853fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    for (size_t i = 0; i < mSamplingRates.size(); i++) {
3854fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        ALOGV("  %d", mSamplingRates[i]);
3855fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    }
3856fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
3857fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    ALOGV("    - channel masks: ");
3858fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    for (size_t i = 0; i < mChannelMasks.size(); i++) {
3859fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        ALOGV("  0x%04x", mChannelMasks[i]);
3860fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    }
3861fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
3862fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    ALOGV("    - formats: ");
3863fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    for (size_t i = 0; i < mFormats.size(); i++) {
3864fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        ALOGV("  0x%08x", mFormats[i]);
3865fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    }
3866fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
3867fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    ALOGV("    - devices: 0x%04x\n", mSupportedDevices);
3868fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    ALOGV("    - flags: 0x%04x\n", mFlags);
3869fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean}
3870fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
38715ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent// --- audio_policy.conf file parsing
38725ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
38735ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentstruct StringToEnum {
38745ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    const char *name;
38755ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    uint32_t value;
38765ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent};
38775ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
38785ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent#define STRING_TO_ENUM(string) { #string, string }
38795ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
38805ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
38815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentconst struct StringToEnum sDeviceNameToEnumTable[] = {
38825ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_EARPIECE),
38835ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_SPEAKER),
38845ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_WIRED_HEADSET),
38855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_WIRED_HEADPHONE),
38865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_ALL_SCO),
38875ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_ALL_A2DP),
38885ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_AUX_DIGITAL),
38895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET),
3890599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET),
3891599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_USB_DEVICE),
3892599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_USB_ACCESSORY),
3893599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_ALL_USB),
389448387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi    STRING_TO_ENUM(AUDIO_DEVICE_OUT_REMOTE_SUBMIX),
38955ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_BUILTIN_MIC),
38965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET),
38975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_WIRED_HEADSET),
38985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_AUX_DIGITAL),
38995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_VOICE_CALL),
39005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_BACK_MIC),
390148387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi    STRING_TO_ENUM(AUDIO_DEVICE_IN_REMOTE_SUBMIX),
3902ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET),
3903ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_DGTL_DOCK_HEADSET),
3904ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_USB_ACCESSORY),
390526101ada7b6a34cc517b92dec1e96b17fb4a996dPeter Yoon    STRING_TO_ENUM(AUDIO_DEVICE_IN_USB_DEVICE),
39065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent};
39075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
39085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentconst struct StringToEnum sFlagNameToEnumTable[] = {
39090977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent    STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT),
39100977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent    STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY),
3911b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent    STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST),
3912b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent    STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER),
3913b4d07b97d23cfaffe22c7859ad7c45e168a7df0eRichard Fitzgerald    STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD),
3914b4d07b97d23cfaffe22c7859ad7c45e168a7df0eRichard Fitzgerald    STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NON_BLOCKING),
39155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent};
39165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
39175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentconst struct StringToEnum sFormatNameToEnumTable[] = {
39185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
39195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
39209df8ecc68bdaa90aaac86b9218d728f84586660eGlenn Kasten    STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
39219df8ecc68bdaa90aaac86b9218d728f84586660eGlenn Kasten    STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_24_BIT),
39229df8ecc68bdaa90aaac86b9218d728f84586660eGlenn Kasten    STRING_TO_ENUM(AUDIO_FORMAT_PCM_FLOAT),
39239df8ecc68bdaa90aaac86b9218d728f84586660eGlenn Kasten    STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
39245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_FORMAT_MP3),
39255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_FORMAT_AAC),
39265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
39275ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent};
39285ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
39295ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentconst struct StringToEnum sOutChannelsNameToEnumTable[] = {
39305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_OUT_MONO),
39315ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
39325ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
39335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
39345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent};
39355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
39365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentconst struct StringToEnum sInChannelsNameToEnumTable[] = {
39375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_IN_MONO),
39385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_IN_STEREO),
393960758e27a4be8fc9ac1180f8a4055234e1702cc9Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_IN_FRONT_BACK),
39405ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent};
39415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
39425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
39435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentuint32_t AudioPolicyManagerBase::stringToEnum(const struct StringToEnum *table,
39445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                              size_t size,
39455ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                              const char *name)
39465ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
39475ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < size; i++) {
39485ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strcmp(table[i].name, name) == 0) {
39495ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("stringToEnum() found %s", table[i].name);
39505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            return table[i].value;
39515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
39525ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
39535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return 0;
39545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
39555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
395618fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivibool AudioPolicyManagerBase::stringToBool(const char *value)
395718fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi{
395818fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi    return ((strcasecmp("true", value) == 0) || (strcmp("1", value) == 0));
395918fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi}
396018fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi
39610977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurentaudio_output_flags_t AudioPolicyManagerBase::parseFlagNames(char *name)
39625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
39635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    uint32_t flag = 0;
39645ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
39655ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // it is OK to cast name to non const here as we are not going to use it after
39665ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // strtok() modifies it
39675ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    char *flagName = strtok(name, "|");
39685ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (flagName != NULL) {
39695ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strlen(flagName) != 0) {
39705ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            flag |= stringToEnum(sFlagNameToEnumTable,
39715ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                               ARRAY_SIZE(sFlagNameToEnumTable),
39725ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                               flagName);
39735ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
39745ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        flagName = strtok(NULL, "|");
39755ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
3976a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    //force direct flag if offload flag is set: offloading implies a direct output stream
3977a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    // and all common behaviors are driven by checking only the direct flag
3978a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    // this should normally be set appropriately in the policy configuration file
3979a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    if ((flag & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
3980a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        flag |= AUDIO_OUTPUT_FLAG_DIRECT;
3981a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    }
3982a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald
39830977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent    return (audio_output_flags_t)flag;
39845ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
39855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
39865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentaudio_devices_t AudioPolicyManagerBase::parseDeviceNames(char *name)
39875ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
39885ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    uint32_t device = 0;
39895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
39905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    char *devName = strtok(name, "|");
39915ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (devName != NULL) {
39925ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strlen(devName) != 0) {
39935ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device |= stringToEnum(sDeviceNameToEnumTable,
39945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                 ARRAY_SIZE(sDeviceNameToEnumTable),
39955ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                 devName);
39965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
39975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        devName = strtok(NULL, "|");
39985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
3999c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    return device;
40005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
40015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
40025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadSamplingRates(char *name, IOProfile *profile)
40035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
40045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    char *str = strtok(name, "|");
40055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
40063cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    // by convention, "0' in the first entry in mSamplingRates indicates the supported sampling
40073cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    // rates should be read from the output stream after it is opened for the first time
40083cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) {
40093cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        profile->mSamplingRates.add(0);
40103cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        return;
40113cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    }
40123cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
40135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (str != NULL) {
40145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        uint32_t rate = atoi(str);
40155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (rate != 0) {
40165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadSamplingRates() adding rate %d", rate);
40175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mSamplingRates.add(rate);
40185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
40195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        str = strtok(NULL, "|");
40205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
40215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return;
40225ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
40235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
40245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadFormats(char *name, IOProfile *profile)
40255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
40265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    char *str = strtok(name, "|");
40275ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
40283cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    // by convention, "0' in the first entry in mFormats indicates the supported formats
40293cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    // should be read from the output stream after it is opened for the first time
40303cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) {
40312d749b0bda4ae60ef1a940eeba86121b2e94d07dGlenn Kasten        profile->mFormats.add(AUDIO_FORMAT_DEFAULT);
40323cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        return;
40333cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    }
40343cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
40355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (str != NULL) {
40365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_format_t format = (audio_format_t)stringToEnum(sFormatNameToEnumTable,
40375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                             ARRAY_SIZE(sFormatNameToEnumTable),
40385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                             str);
40392d749b0bda4ae60ef1a940eeba86121b2e94d07dGlenn Kasten        if (format != AUDIO_FORMAT_DEFAULT) {
40405ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mFormats.add(format);
40415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
40425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        str = strtok(NULL, "|");
40435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
40445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return;
40455ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
40465ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
40475ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadInChannels(char *name, IOProfile *profile)
40485ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
40495ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    const char *str = strtok(name, "|");
40505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
40515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGV("loadInChannels() %s", name);
40523cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
40533cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) {
40542c3d2379ee6eadb32b63bf6de538cfd6487e8e20Glenn Kasten        profile->mChannelMasks.add(0);
40553cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        return;
40563cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    }
40573cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
40585ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (str != NULL) {
40595ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_channel_mask_t channelMask =
40605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                (audio_channel_mask_t)stringToEnum(sInChannelsNameToEnumTable,
40615ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                   ARRAY_SIZE(sInChannelsNameToEnumTable),
40625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                   str);
40635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (channelMask != 0) {
40645ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadInChannels() adding channelMask %04x", channelMask);
40655ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mChannelMasks.add(channelMask);
40665ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
40675ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        str = strtok(NULL, "|");
40685ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
40695ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return;
40705ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
40715ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
40725ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadOutChannels(char *name, IOProfile *profile)
40735ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
40745ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    const char *str = strtok(name, "|");
40753cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
40763cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGV("loadOutChannels() %s", name);
40773cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
40783cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    // by convention, "0' in the first entry in mChannelMasks indicates the supported channel
40793cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    // masks should be read from the output stream after it is opened for the first time
40803cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) {
40812c3d2379ee6eadb32b63bf6de538cfd6487e8e20Glenn Kasten        profile->mChannelMasks.add(0);
40823cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        return;
40833cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    }
40845ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
40855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (str != NULL) {
40863cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        audio_channel_mask_t channelMask =
40873cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                (audio_channel_mask_t)stringToEnum(sOutChannelsNameToEnumTable,
40883cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                   ARRAY_SIZE(sOutChannelsNameToEnumTable),
40893cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                   str);
40905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (channelMask != 0) {
40915ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mChannelMasks.add(channelMask);
40925ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
40935ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        str = strtok(NULL, "|");
40945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
40955ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return;
40965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
40975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
409870c236c9290732782d5267935af1475b8d5ae602Eric Laurentstatus_t AudioPolicyManagerBase::loadInput(cnode *root, HwModule *module)
40995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
41005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    cnode *node = root->first_child;
41015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
41025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    IOProfile *profile = new IOProfile(module);
41035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
41045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (node) {
41055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
41065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            loadSamplingRates((char *)node->value, profile);
41075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, FORMATS_TAG) == 0) {
41085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            loadFormats((char *)node->value, profile);
41095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, CHANNELS_TAG) == 0) {
41105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            loadInChannels((char *)node->value, profile);
41115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, DEVICES_TAG) == 0) {
41125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mSupportedDevices = parseDeviceNames((char *)node->value);
41135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
41145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        node = node->next;
41155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
4116ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    ALOGW_IF(profile->mSupportedDevices == AUDIO_DEVICE_NONE,
41175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadInput() invalid supported devices");
41185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGW_IF(profile->mChannelMasks.size() == 0,
41195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadInput() invalid supported channel masks");
41205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGW_IF(profile->mSamplingRates.size() == 0,
41215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadInput() invalid supported sampling rates");
41225ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGW_IF(profile->mFormats.size() == 0,
41235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadInput() invalid supported formats");
4124ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if ((profile->mSupportedDevices != AUDIO_DEVICE_NONE) &&
41255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            (profile->mChannelMasks.size() != 0) &&
41265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            (profile->mSamplingRates.size() != 0) &&
41275ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            (profile->mFormats.size() != 0)) {
41285ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
4129fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        ALOGV("loadInput() adding input mSupportedDevices 0x%X", profile->mSupportedDevices);
41305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
413170c236c9290732782d5267935af1475b8d5ae602Eric Laurent        module->mInputProfiles.add(profile);
41325ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return NO_ERROR;
41335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    } else {
41345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        delete profile;
41355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return BAD_VALUE;
41365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
41375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
41385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
413970c236c9290732782d5267935af1475b8d5ae602Eric Laurentstatus_t AudioPolicyManagerBase::loadOutput(cnode *root, HwModule *module)
41405ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
41415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    cnode *node = root->first_child;
41425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
41435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    IOProfile *profile = new IOProfile(module);
41445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
41455ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (node) {
41465ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
41475ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            loadSamplingRates((char *)node->value, profile);
41485ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, FORMATS_TAG) == 0) {
41495ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            loadFormats((char *)node->value, profile);
41505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, CHANNELS_TAG) == 0) {
41515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            loadOutChannels((char *)node->value, profile);
41525ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, DEVICES_TAG) == 0) {
41535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mSupportedDevices = parseDeviceNames((char *)node->value);
41545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, FLAGS_TAG) == 0) {
41555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mFlags = parseFlagNames((char *)node->value);
41565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
41575ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        node = node->next;
41585ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
4159ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    ALOGW_IF(profile->mSupportedDevices == AUDIO_DEVICE_NONE,
41605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadOutput() invalid supported devices");
41615ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGW_IF(profile->mChannelMasks.size() == 0,
41625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadOutput() invalid supported channel masks");
41635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGW_IF(profile->mSamplingRates.size() == 0,
41645ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadOutput() invalid supported sampling rates");
41655ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGW_IF(profile->mFormats.size() == 0,
41665ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadOutput() invalid supported formats");
4167ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if ((profile->mSupportedDevices != AUDIO_DEVICE_NONE) &&
41685ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            (profile->mChannelMasks.size() != 0) &&
41695ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            (profile->mSamplingRates.size() != 0) &&
41705ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            (profile->mFormats.size() != 0)) {
41715ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
41725ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        ALOGV("loadOutput() adding output mSupportedDevices %04x, mFlags %04x",
41735ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent              profile->mSupportedDevices, profile->mFlags);
41745ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
417570c236c9290732782d5267935af1475b8d5ae602Eric Laurent        module->mOutputProfiles.add(profile);
41765ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return NO_ERROR;
41775ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    } else {
41785ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        delete profile;
41795ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return BAD_VALUE;
41805ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
41815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
41825ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
41835ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadHwModule(cnode *root)
41845ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
41855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    cnode *node = config_find(root, OUTPUTS_TAG);
41865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    status_t status = NAME_NOT_FOUND;
418770c236c9290732782d5267935af1475b8d5ae602Eric Laurent
418870c236c9290732782d5267935af1475b8d5ae602Eric Laurent    HwModule *module = new HwModule(root->name);
418970c236c9290732782d5267935af1475b8d5ae602Eric Laurent
41905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (node != NULL) {
41915ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strcmp(root->name, AUDIO_HARDWARE_MODULE_ID_A2DP) == 0) {
41925ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            mHasA2dp = true;
4193599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent        } else if (strcmp(root->name, AUDIO_HARDWARE_MODULE_ID_USB) == 0) {
4194599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            mHasUsb = true;
419548387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi        } else if (strcmp(root->name, AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX) == 0) {
419648387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            mHasRemoteSubmix = true;
41975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
4198599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent
41995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        node = node->first_child;
42005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        while (node) {
42015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadHwModule() loading output %s", node->name);
420270c236c9290732782d5267935af1475b8d5ae602Eric Laurent            status_t tmpStatus = loadOutput(node, module);
42035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            if (status == NAME_NOT_FOUND || status == NO_ERROR) {
42045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                status = tmpStatus;
42055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
42065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            node = node->next;
42075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
42085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
42095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    node = config_find(root, INPUTS_TAG);
42105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (node != NULL) {
42115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        node = node->first_child;
42125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        while (node) {
42135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadHwModule() loading input %s", node->name);
421470c236c9290732782d5267935af1475b8d5ae602Eric Laurent            status_t tmpStatus = loadInput(node, module);
42155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            if (status == NAME_NOT_FOUND || status == NO_ERROR) {
42165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                status = tmpStatus;
42175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
42185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            node = node->next;
42195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
42205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
42215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (status == NO_ERROR) {
422270c236c9290732782d5267935af1475b8d5ae602Eric Laurent        mHwModules.add(module);
422370c236c9290732782d5267935af1475b8d5ae602Eric Laurent    } else {
422470c236c9290732782d5267935af1475b8d5ae602Eric Laurent        delete module;
42255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
42265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
42275ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
42285ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadHwModules(cnode *root)
42295ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
42305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    cnode *node = config_find(root, AUDIO_HW_MODULE_TAG);
42315ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (node == NULL) {
42325ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return;
42335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
42345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
42355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    node = node->first_child;
42365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (node) {
42375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        ALOGV("loadHwModules() loading module %s", node->name);
42385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        loadHwModule(node);
42395ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        node = node->next;
42405ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
42415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
42425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
42435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadGlobalConfig(cnode *root)
42445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
42455ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    cnode *node = config_find(root, GLOBAL_CONFIG_TAG);
42465ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (node == NULL) {
42475ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return;
42485ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
42495ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    node = node->first_child;
42505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (node) {
42515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strcmp(ATTACHED_OUTPUT_DEVICES_TAG, node->name) == 0) {
42525ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            mAttachedOutputDevices = parseDeviceNames((char *)node->value);
4253ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            ALOGW_IF(mAttachedOutputDevices == AUDIO_DEVICE_NONE,
4254c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                    "loadGlobalConfig() no attached output devices");
42555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadGlobalConfig() mAttachedOutputDevices %04x", mAttachedOutputDevices);
42565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(DEFAULT_OUTPUT_DEVICE_TAG, node->name) == 0) {
42575ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            mDefaultOutputDevice = (audio_devices_t)stringToEnum(sDeviceNameToEnumTable,
42585ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                              ARRAY_SIZE(sDeviceNameToEnumTable),
42595ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                              (char *)node->value);
4260ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            ALOGW_IF(mDefaultOutputDevice == AUDIO_DEVICE_NONE,
4261c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                    "loadGlobalConfig() default device not specified");
42625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadGlobalConfig() mDefaultOutputDevice %04x", mDefaultOutputDevice);
42635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(ATTACHED_INPUT_DEVICES_TAG, node->name) == 0) {
4264ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            mAvailableInputDevices = parseDeviceNames((char *)node->value) & ~AUDIO_DEVICE_BIT_IN;
42655ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadGlobalConfig() mAvailableInputDevices %04x", mAvailableInputDevices);
426618fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi        } else if (strcmp(SPEAKER_DRC_ENABLED_TAG, node->name) == 0) {
426718fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi            mSpeakerDrcEnabled = stringToBool((char *)node->value);
426818fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi            ALOGV("loadGlobalConfig() mSpeakerDrcEnabled = %d", mSpeakerDrcEnabled);
42695ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
42705ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        node = node->next;
42715ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
42725ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
42735ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
42745ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentstatus_t AudioPolicyManagerBase::loadAudioPolicyConfig(const char *path)
42755ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
42765ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    cnode *root;
42775ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    char *data;
42785ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
42795ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    data = (char *)load_file(path, NULL);
42805ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (data == NULL) {
42815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return -ENODEV;
42825ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
42835ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    root = config_node("", "");
42845ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    config_load(root, data);
42855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
42865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    loadGlobalConfig(root);
42875ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    loadHwModules(root);
42885ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
42895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    config_free(root);
42905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    free(root);
42915ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    free(data);
42925ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
42935ec145df7708564d385fd3fb764085321cf4c253Dima Zavin    ALOGI("loadAudioPolicyConfig() loaded %s\n", path);
42945ec145df7708564d385fd3fb764085321cf4c253Dima Zavin
42955ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return NO_ERROR;
42965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
4297f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
4298739022f26a7127ba76a98dda65411496086114a7Dima Zavinvoid AudioPolicyManagerBase::defaultAudioPolicyConfig(void)
4299739022f26a7127ba76a98dda65411496086114a7Dima Zavin{
4300739022f26a7127ba76a98dda65411496086114a7Dima Zavin    HwModule *module;
4301739022f26a7127ba76a98dda65411496086114a7Dima Zavin    IOProfile *profile;
4302739022f26a7127ba76a98dda65411496086114a7Dima Zavin
4303739022f26a7127ba76a98dda65411496086114a7Dima Zavin    mDefaultOutputDevice = AUDIO_DEVICE_OUT_SPEAKER;
4304739022f26a7127ba76a98dda65411496086114a7Dima Zavin    mAttachedOutputDevices = AUDIO_DEVICE_OUT_SPEAKER;
4305ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    mAvailableInputDevices = AUDIO_DEVICE_IN_BUILTIN_MIC & ~AUDIO_DEVICE_BIT_IN;
4306739022f26a7127ba76a98dda65411496086114a7Dima Zavin
4307739022f26a7127ba76a98dda65411496086114a7Dima Zavin    module = new HwModule("primary");
4308739022f26a7127ba76a98dda65411496086114a7Dima Zavin
4309739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile = new IOProfile(module);
4310739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mSamplingRates.add(44100);
4311739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
4312739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mChannelMasks.add(AUDIO_CHANNEL_OUT_STEREO);
4313739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mSupportedDevices = AUDIO_DEVICE_OUT_SPEAKER;
4314739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mFlags = AUDIO_OUTPUT_FLAG_PRIMARY;
4315739022f26a7127ba76a98dda65411496086114a7Dima Zavin    module->mOutputProfiles.add(profile);
4316739022f26a7127ba76a98dda65411496086114a7Dima Zavin
4317739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile = new IOProfile(module);
4318739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mSamplingRates.add(8000);
4319739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
4320739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mChannelMasks.add(AUDIO_CHANNEL_IN_MONO);
4321739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mSupportedDevices = AUDIO_DEVICE_IN_BUILTIN_MIC;
4322739022f26a7127ba76a98dda65411496086114a7Dima Zavin    module->mInputProfiles.add(profile);
4323739022f26a7127ba76a98dda65411496086114a7Dima Zavin
4324739022f26a7127ba76a98dda65411496086114a7Dima Zavin    mHwModules.add(module);
4325739022f26a7127ba76a98dda65411496086114a7Dima Zavin}
4326f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
4327f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}; // namespace android
4328