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{
562d2ea50df16fc1a04f1ebf8772c65c56e4f5ecfaGlenn Kasten    // device_address can be NULL and should be handled as an empty string in this case,
572d2ea50df16fc1a04f1ebf8772c65c56e4f5ecfaGlenn Kasten    // and it is not checked by AudioPolicyInterfaceImpl.cpp
582d2ea50df16fc1a04f1ebf8772c65c56e4f5ecfaGlenn Kasten    if (device_address == NULL) {
592d2ea50df16fc1a04f1ebf8772c65c56e4f5ecfaGlenn Kasten        device_address = "";
602d2ea50df16fc1a04f1ebf8772c65c56e4f5ecfaGlenn Kasten    }
61c26f454f8d841657542916cdd140a9896a89ad20Paul McLean    ALOGV("setDeviceConnectionState() device: 0x%X, state %d, address %s", device, state, device_address);
62f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
63f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // connect/disconnect only 1 device at a time
64ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if (!audio_is_output_device(device) && !audio_is_input_device(device)) return BAD_VALUE;
65f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
66f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (strlen(device_address) >= MAX_DEVICE_ADDRESS_LEN) {
675efbd421e0029d6fc44b1cc65c0e5e0d85e5161fSteve Block        ALOGE("setDeviceConnectionState() invalid address: %s", device_address);
68f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
69f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
70f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
71f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // handle output devices
72c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    if (audio_is_output_device(device)) {
73fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        SortedVector <audio_io_handle_t> outputs;
74f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
7533bf1b0fe363bd4892349d160f54d860567fab12Mike Lockwood        if (!mHasA2dp && audio_is_a2dp_out_device(device)) {
7648387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            ALOGE("setDeviceConnectionState() invalid A2DP device: %x", device);
77f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return BAD_VALUE;
78f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
79fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        if (!mHasUsb && audio_is_usb_out_device(device)) {
8048387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            ALOGE("setDeviceConnectionState() invalid USB audio device: %x", device);
8148387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            return BAD_VALUE;
8248387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi        }
8348387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi        if (!mHasRemoteSubmix && audio_is_remote_submix_device((audio_devices_t)device)) {
8448387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            ALOGE("setDeviceConnectionState() invalid remote submix audio device: %x", device);
85599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            return BAD_VALUE;
86599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent        }
87f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
88c952527e6f89d5427881462823514be9d79f13e6Eric Laurent        // save a copy of the opened output descriptors before any output is opened or closed
89c952527e6f89d5427881462823514be9d79f13e6Eric Laurent        // by checkOutputsForDevice(). This will be needed by checkOutputForAllStrategies()
90c952527e6f89d5427881462823514be9d79f13e6Eric Laurent        mPreviousOutputs = mOutputs;
919f4165f36d90dddc32246d0eddbf289383c3b2bbEric Laurent        String8 paramStr;
92f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        switch (state)
93f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        {
94f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // handle output device connection
95f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        case AudioSystem::DEVICE_STATE_AVAILABLE:
96f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (mAvailableOutputDevices & device) {
9764cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block                ALOGW("setDeviceConnectionState() device already connected: %x", device);
98f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                return INVALID_OPERATION;
99f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1006a70518b93928d1c91457ff805e375c82d76b0e5Steve Block            ALOGV("setDeviceConnectionState() connecting device %x", device);
101f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
10233bf1b0fe363bd4892349d160f54d860567fab12Mike Lockwood            if (mHasA2dp && audio_is_a2dp_out_device(device)) {
1039f4165f36d90dddc32246d0eddbf289383c3b2bbEric Laurent                // handle A2DP device connection
1049f4165f36d90dddc32246d0eddbf289383c3b2bbEric Laurent                AudioParameter param;
1059f4165f36d90dddc32246d0eddbf289383c3b2bbEric Laurent                param.add(String8(AUDIO_PARAMETER_A2DP_SINK_ADDRESS), String8(device_address));
1069f4165f36d90dddc32246d0eddbf289383c3b2bbEric Laurent                paramStr = param.toString();
107fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            } else if (mHasUsb && audio_is_usb_out_device(device)) {
1089f4165f36d90dddc32246d0eddbf289383c3b2bbEric Laurent                // handle USB device connection
1099f4165f36d90dddc32246d0eddbf289383c3b2bbEric Laurent                paramStr = String8(device_address, MAX_DEVICE_ADDRESS_LEN);
1109f4165f36d90dddc32246d0eddbf289383c3b2bbEric Laurent            }
1119f4165f36d90dddc32246d0eddbf289383c3b2bbEric Laurent
1129f4165f36d90dddc32246d0eddbf289383c3b2bbEric Laurent            if (checkOutputsForDevice(device, state, outputs, paramStr) != NO_ERROR) {
1133cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                return INVALID_OPERATION;
1143cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
115c7b6e3c857c5f5af5f244d0b877c41a9c55f92abColin Cross            ALOGV("setDeviceConnectionState() checkOutputsForDevice() returned %zu outputs",
1163cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                  outputs.size());
117f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // register new device as available
118f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent            mAvailableOutputDevices = (audio_devices_t)(mAvailableOutputDevices | device);
119f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
12033bf1b0fe363bd4892349d160f54d860567fab12Mike Lockwood            if (mHasA2dp && audio_is_a2dp_out_device(device)) {
1219f4165f36d90dddc32246d0eddbf289383c3b2bbEric Laurent                // handle A2DP device connection
1229f4165f36d90dddc32246d0eddbf289383c3b2bbEric Laurent                mA2dpDeviceAddress = String8(device_address, MAX_DEVICE_ADDRESS_LEN);
1239f4165f36d90dddc32246d0eddbf289383c3b2bbEric Laurent                mA2dpSuspended = false;
1249f4165f36d90dddc32246d0eddbf289383c3b2bbEric Laurent            } else if (audio_is_bluetooth_sco_device(device)) {
1259f4165f36d90dddc32246d0eddbf289383c3b2bbEric Laurent                // handle SCO device connection
1269f4165f36d90dddc32246d0eddbf289383c3b2bbEric Laurent                mScoDeviceAddress = String8(device_address, MAX_DEVICE_ADDRESS_LEN);
127fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            } else if (mHasUsb && audio_is_usb_out_device(device)) {
1289f4165f36d90dddc32246d0eddbf289383c3b2bbEric Laurent                // handle USB device connection
129fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                mUsbOutCardAndDevice = String8(device_address, MAX_DEVICE_ADDRESS_LEN);
130f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1319f4165f36d90dddc32246d0eddbf289383c3b2bbEric Laurent
132f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            break;
133f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // handle output device disconnection
134f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        case AudioSystem::DEVICE_STATE_UNAVAILABLE: {
135f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (!(mAvailableOutputDevices & device)) {
13664cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block                ALOGW("setDeviceConnectionState() device not connected: %x", device);
137f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                return INVALID_OPERATION;
138f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
139f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1406a70518b93928d1c91457ff805e375c82d76b0e5Steve Block            ALOGV("setDeviceConnectionState() disconnecting device %x", device);
141f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // remove device from available output devices
142f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent            mAvailableOutputDevices = (audio_devices_t)(mAvailableOutputDevices & ~device);
1439f4165f36d90dddc32246d0eddbf289383c3b2bbEric Laurent            checkOutputsForDevice(device, state, outputs, paramStr);
144fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
14533bf1b0fe363bd4892349d160f54d860567fab12Mike Lockwood            if (mHasA2dp && audio_is_a2dp_out_device(device)) {
1463cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                // handle A2DP device disconnection
147b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                mA2dpDeviceAddress = "";
148b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                mA2dpSuspended = false;
149c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            } else if (audio_is_bluetooth_sco_device(device)) {
1503cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                // handle SCO device disconnection
151b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                mScoDeviceAddress = "";
152fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            } else if (mHasUsb && audio_is_usb_out_device(device)) {
1533cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                // handle USB device disconnection
154fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                mUsbOutCardAndDevice = "";
155f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
15648387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            // not currently handling multiple simultaneous submixes: ignoring remote submix
15748387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            //   case and address
158f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            } break;
159f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
160f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        default:
1615efbd421e0029d6fc44b1cc65c0e5e0d85e5161fSteve Block            ALOGE("setDeviceConnectionState() invalid state: %x", state);
162f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return BAD_VALUE;
163f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
164f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
165f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        checkA2dpSuspend();
166f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        checkOutputForAllStrategies();
167b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        // outputs must be closed after checkOutputForAllStrategies() is executed
1683cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        if (!outputs.isEmpty()) {
1693cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            for (size_t i = 0; i < outputs.size(); i++) {
170a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                AudioOutputDescriptor *desc = mOutputs.valueFor(outputs[i]);
1713cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                // close unused outputs after device disconnection or direct outputs that have been
1723cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                // opened by checkOutputsForDevice() to query dynamic parameters
1733cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                if ((state == AudioSystem::DEVICE_STATE_UNAVAILABLE) ||
174a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                        (((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) != 0) &&
175a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                         (desc->mDirectOpenCount == 0))) {
1763cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    closeOutput(outputs[i]);
1773cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                }
1783cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
179f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1805ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
181c952527e6f89d5427881462823514be9d79f13e6Eric Laurent        updateDevicesAndOutputs();
1825ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        for (size_t i = 0; i < mOutputs.size(); i++) {
1832c72e9faa5ad5e324b85b78e383dd85c8bdc04a9Eric Laurent            // do not force device change on duplicated output because if device is 0, it will
1842c72e9faa5ad5e324b85b78e383dd85c8bdc04a9Eric Laurent            // also force a device 0 for the two outputs it is duplicated to which may override
1852c72e9faa5ad5e324b85b78e383dd85c8bdc04a9Eric Laurent            // a valid device selection on those outputs.
18676e97d3950f2654adbb0a415218b6d048200c395Eric Laurent            setOutputDevice(mOutputs.keyAt(i),
18776e97d3950f2654adbb0a415218b6d048200c395Eric Laurent                            getNewDevice(mOutputs.keyAt(i), true /*fromCache*/),
1882c72e9faa5ad5e324b85b78e383dd85c8bdc04a9Eric Laurent                            !mOutputs.valueAt(i)->isDuplicated(),
18976e97d3950f2654adbb0a415218b6d048200c395Eric Laurent                            0);
1905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
191f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
192806fab660adf8a5c3d36e9411d214a4942cd67e9Eric Laurent        return NO_ERROR;
193fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    }  // end if is output device
194fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
195f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // handle input devices
196c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    if (audio_is_input_device(device)) {
197fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        SortedVector <audio_io_handle_t> inputs;
198f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
199fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        String8 paramStr;
200f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        switch (state)
201f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        {
202f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // handle input device connection
203f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        case AudioSystem::DEVICE_STATE_AVAILABLE: {
204f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (mAvailableInputDevices & device) {
20564cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block                ALOGW("setDeviceConnectionState() device already connected: %d", device);
206f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                return INVALID_OPERATION;
207f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
208fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
209fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            if (mHasUsb && audio_is_usb_in_device(device)) {
210fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                // handle USB device connection
211fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                paramStr = String8(device_address, MAX_DEVICE_ADDRESS_LEN);
21233bf1b0fe363bd4892349d160f54d860567fab12Mike Lockwood            } else if (mHasA2dp && audio_is_a2dp_in_device(device)) {
21333bf1b0fe363bd4892349d160f54d860567fab12Mike Lockwood                // handle A2DP device connection
21433bf1b0fe363bd4892349d160f54d860567fab12Mike Lockwood                AudioParameter param;
21533bf1b0fe363bd4892349d160f54d860567fab12Mike Lockwood                param.add(String8(AUDIO_PARAMETER_A2DP_SOURCE_ADDRESS), String8(device_address));
21633bf1b0fe363bd4892349d160f54d860567fab12Mike Lockwood                paramStr = param.toString();
217fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            }
218fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
219fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            if (checkInputsForDevice(device, state, inputs, paramStr) != NO_ERROR) {
220fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                return INVALID_OPERATION;
221fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            }
222ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            mAvailableInputDevices = mAvailableInputDevices | (device & ~AUDIO_DEVICE_BIT_IN);
223f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
224f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            break;
225f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
226f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // handle input device disconnection
227f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        case AudioSystem::DEVICE_STATE_UNAVAILABLE: {
228f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (!(mAvailableInputDevices & device)) {
22964cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block                ALOGW("setDeviceConnectionState() device not connected: %d", device);
230f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                return INVALID_OPERATION;
231f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
232fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            checkInputsForDevice(device, state, inputs, paramStr);
2335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            mAvailableInputDevices = (audio_devices_t) (mAvailableInputDevices & ~device);
234fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        } break;
235f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
236f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        default:
2375efbd421e0029d6fc44b1cc65c0e5e0d85e5161fSteve Block            ALOGE("setDeviceConnectionState() invalid state: %x", state);
238f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return BAD_VALUE;
239f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
240f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
241fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        closeAllInputs();
242f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
243f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return NO_ERROR;
244fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    } // end if is input device
245f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
24664cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block    ALOGW("setDeviceConnectionState() invalid device: %x", device);
247f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return BAD_VALUE;
248f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
249f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
250c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel TriviAudioSystem::device_connection_state AudioPolicyManagerBase::getDeviceConnectionState(audio_devices_t device,
251f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                  const char *device_address)
252f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2532d2ea50df16fc1a04f1ebf8772c65c56e4f5ecfaGlenn Kasten    // similar to setDeviceConnectionState
2542d2ea50df16fc1a04f1ebf8772c65c56e4f5ecfaGlenn Kasten    if (device_address == NULL) {
2552d2ea50df16fc1a04f1ebf8772c65c56e4f5ecfaGlenn Kasten        device_address = "";
2562d2ea50df16fc1a04f1ebf8772c65c56e4f5ecfaGlenn Kasten    }
257f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioSystem::device_connection_state state = AudioSystem::DEVICE_STATE_UNAVAILABLE;
258f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    String8 address = String8(device_address);
259c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    if (audio_is_output_device(device)) {
260f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (device & mAvailableOutputDevices) {
26133bf1b0fe363bd4892349d160f54d860567fab12Mike Lockwood            if (audio_is_a2dp_out_device(device) &&
2625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                (!mHasA2dp || (address != "" && mA2dpDeviceAddress != address))) {
263f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                return state;
264f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
265c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            if (audio_is_bluetooth_sco_device(device) &&
266f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                address != "" && mScoDeviceAddress != address) {
267f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                return state;
268f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
269fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            if (audio_is_usb_out_device(device) &&
270fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                (!mHasUsb || (address != "" && mUsbOutCardAndDevice != address))) {
27148387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi                ALOGE("getDeviceConnectionState() invalid device: %x", device);
27248387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi                return state;
27348387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            }
27448387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            if (audio_is_remote_submix_device((audio_devices_t)device) && !mHasRemoteSubmix) {
275599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent                return state;
276599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            }
277f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            state = AudioSystem::DEVICE_STATE_AVAILABLE;
278f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
279c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    } else if (audio_is_input_device(device)) {
280f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (device & mAvailableInputDevices) {
281f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            state = AudioSystem::DEVICE_STATE_AVAILABLE;
282f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
283f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
284f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
285f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return state;
286f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
287f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
288f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::setPhoneState(int state)
289f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2906a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("setPhoneState() state %d", state);
291ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    audio_devices_t newDevice = AUDIO_DEVICE_NONE;
292f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (state < 0 || state >= AudioSystem::NUM_MODES) {
29364cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("setPhoneState() invalid state %d", state);
294f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
295f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
296f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
297f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (state == mPhoneState ) {
29864cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("setPhoneState() setting same state %d", state);
299f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
300f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
301f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
302f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if leaving call state, handle special case of active streams
303f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // pertaining to sonification strategy see handleIncallSonification()
304f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (isInCall()) {
3056a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("setPhoneState() in call state management: new state is %d", state);
306f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
307f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            handleIncallSonification(stream, false, true);
308f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
309f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
310f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
311f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // store previous phone state for management of sonification strategy below
312f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    int oldState = mPhoneState;
313f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mPhoneState = state;
314f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    bool force = false;
315f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
316f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // are we entering or starting a call
317f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (!isStateInCall(oldState) && isStateInCall(state)) {
3186a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("  Entering call in setPhoneState()");
319f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // force routing command to audio hardware when starting a call
320f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // even if no device change is needed
321f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        force = true;
32287e28f7b352fb77f46e16ebcdd85cbf01396a203Eric Laurent        for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) {
32387e28f7b352fb77f46e16ebcdd85cbf01396a203Eric Laurent            mStreams[AUDIO_STREAM_DTMF].mVolumeCurve[j] =
32487e28f7b352fb77f46e16ebcdd85cbf01396a203Eric Laurent                    sVolumeProfiles[AUDIO_STREAM_VOICE_CALL][j];
32587e28f7b352fb77f46e16ebcdd85cbf01396a203Eric Laurent        }
326f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else if (isStateInCall(oldState) && !isStateInCall(state)) {
3276a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("  Exiting call in setPhoneState()");
328f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // force routing command to audio hardware when exiting a call
329f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // even if no device change is needed
330f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        force = true;
33187e28f7b352fb77f46e16ebcdd85cbf01396a203Eric Laurent        for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) {
33287e28f7b352fb77f46e16ebcdd85cbf01396a203Eric Laurent            mStreams[AUDIO_STREAM_DTMF].mVolumeCurve[j] =
33387e28f7b352fb77f46e16ebcdd85cbf01396a203Eric Laurent                    sVolumeProfiles[AUDIO_STREAM_DTMF][j];
33487e28f7b352fb77f46e16ebcdd85cbf01396a203Eric Laurent        }
335f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else if (isStateInCall(state) && (state != oldState)) {
3366a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("  Switching between telephony and VoIP in setPhoneState()");
337f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // force routing command to audio hardware when switching between telephony and VoIP
338f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // even if no device change is needed
339f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        force = true;
340f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
341f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
342f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // check for device and output changes triggered by new phone state
3435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    newDevice = getNewDevice(mPrimaryOutput, false /*fromCache*/);
344f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkA2dpSuspend();
345f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForAllStrategies();
346c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    updateDevicesAndOutputs();
347f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
348b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    AudioOutputDescriptor *hwOutputDesc = mOutputs.valueFor(mPrimaryOutput);
349f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
350f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // force routing command to audio hardware when ending call
351f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // even if no device change is needed
352ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if (isStateInCall(oldState) && newDevice == AUDIO_DEVICE_NONE) {
353f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        newDevice = hwOutputDesc->device();
354f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
355f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
356f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    int delayMs = 0;
357772686310a3bc44d93591349e174a60360a3d30dSathishKumar Mani    if (isStateInCall(state)) {
35880f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent        nsecs_t sysTime = systemTime();
359772686310a3bc44d93591349e174a60360a3d30dSathishKumar Mani        for (size_t i = 0; i < mOutputs.size(); i++) {
360772686310a3bc44d93591349e174a60360a3d30dSathishKumar Mani            AudioOutputDescriptor *desc = mOutputs.valueAt(i);
36180f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent            // mute media and sonification strategies and delay device switch by the largest
36280f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent            // latency of any output where either strategy is active.
36380f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent            // This avoid sending the ring tone or music tail into the earpiece or headset.
36480f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent            if ((desc->isStrategyActive(STRATEGY_MEDIA,
36580f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                                     SONIFICATION_HEADSET_MUSIC_DELAY,
36680f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                                     sysTime) ||
36780f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                    desc->isStrategyActive(STRATEGY_SONIFICATION,
36880f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                                         SONIFICATION_HEADSET_MUSIC_DELAY,
36980f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                                         sysTime)) &&
37080f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                    (delayMs < (int)desc->mLatency*2)) {
37180f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                delayMs = desc->mLatency*2;
372772686310a3bc44d93591349e174a60360a3d30dSathishKumar Mani            }
37380f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent            setStrategyMute(STRATEGY_MEDIA, true, mOutputs.keyAt(i));
37480f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent            setStrategyMute(STRATEGY_MEDIA, false, mOutputs.keyAt(i), MUTE_TIME_MS,
37580f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/));
37680f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent            setStrategyMute(STRATEGY_SONIFICATION, true, mOutputs.keyAt(i));
37780f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent            setStrategyMute(STRATEGY_SONIFICATION, false, mOutputs.keyAt(i), MUTE_TIME_MS,
37880f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                getDeviceForStrategy(STRATEGY_SONIFICATION, true /*fromCache*/));
379772686310a3bc44d93591349e174a60360a3d30dSathishKumar Mani        }
380772686310a3bc44d93591349e174a60360a3d30dSathishKumar Mani    }
381772686310a3bc44d93591349e174a60360a3d30dSathishKumar Mani
382f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // change routing is necessary
383b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    setOutputDevice(mPrimaryOutput, newDevice, force, delayMs);
384f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
385f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if entering in call state, handle special case of active streams
386f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // pertaining to sonification strategy see handleIncallSonification()
387f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (isStateInCall(state)) {
3886a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("setPhoneState() in call state management: new state is %d", state);
389f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
390f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            handleIncallSonification(stream, true, true);
391f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
392f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
393f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
394f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // Flag that ringtone volume must be limited to music volume until we exit MODE_RINGTONE
395f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (state == AudioSystem::MODE_RINGTONE &&
396f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        isStreamActive(AudioSystem::MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY)) {
397f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mLimitRingtoneVolume = true;
398f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
399f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mLimitRingtoneVolume = false;
400f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
401f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
402f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
403f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::setForceUse(AudioSystem::force_use usage, AudioSystem::forced_config config)
404f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
4056a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("setForceUse() usage %d, config %d, mPhoneState %d", usage, config, mPhoneState);
406f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
407f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    bool forceVolumeReeval = false;
408f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    switch(usage) {
409f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::FOR_COMMUNICATION:
410f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (config != AudioSystem::FORCE_SPEAKER && config != AudioSystem::FORCE_BT_SCO &&
411f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_NONE) {
41264cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("setForceUse() invalid config %d for FOR_COMMUNICATION", config);
413f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return;
414f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
415f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        forceVolumeReeval = true;
416f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mForceUse[usage] = config;
417f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
418f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::FOR_MEDIA:
419f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (config != AudioSystem::FORCE_HEADPHONES && config != AudioSystem::FORCE_BT_A2DP &&
420f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_WIRED_ACCESSORY &&
421f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_ANALOG_DOCK &&
4221afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent            config != AudioSystem::FORCE_DIGITAL_DOCK && config != AudioSystem::FORCE_NONE &&
42331363a9cb94e80330c335fede0b92b1953a09517Jean-Michel Trivi            config != AudioSystem::FORCE_NO_BT_A2DP) {
42464cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("setForceUse() invalid config %d for FOR_MEDIA", config);
425f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return;
426f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
427f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mForceUse[usage] = config;
428f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
429f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::FOR_RECORD:
430f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (config != AudioSystem::FORCE_BT_SCO && config != AudioSystem::FORCE_WIRED_ACCESSORY &&
431f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_NONE) {
43264cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("setForceUse() invalid config %d for FOR_RECORD", config);
433f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return;
434f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
435f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mForceUse[usage] = config;
436f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
437f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::FOR_DOCK:
438f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (config != AudioSystem::FORCE_NONE && config != AudioSystem::FORCE_BT_CAR_DOCK &&
439f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_BT_DESK_DOCK &&
440f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_WIRED_ACCESSORY &&
441f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_ANALOG_DOCK &&
442f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            config != AudioSystem::FORCE_DIGITAL_DOCK) {
44364cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("setForceUse() invalid config %d for FOR_DOCK", config);
444f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
445f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        forceVolumeReeval = true;
446f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mForceUse[usage] = config;
447f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
448738207def5f691d605ae33d041116829a74513a9Eric Laurent    case AudioSystem::FOR_SYSTEM:
449738207def5f691d605ae33d041116829a74513a9Eric Laurent        if (config != AudioSystem::FORCE_NONE &&
450738207def5f691d605ae33d041116829a74513a9Eric Laurent            config != AudioSystem::FORCE_SYSTEM_ENFORCED) {
451738207def5f691d605ae33d041116829a74513a9Eric Laurent            ALOGW("setForceUse() invalid config %d for FOR_SYSTEM", config);
452738207def5f691d605ae33d041116829a74513a9Eric Laurent        }
453738207def5f691d605ae33d041116829a74513a9Eric Laurent        forceVolumeReeval = true;
454738207def5f691d605ae33d041116829a74513a9Eric Laurent        mForceUse[usage] = config;
455738207def5f691d605ae33d041116829a74513a9Eric Laurent        break;
456f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    default:
45764cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("setForceUse() invalid usage %d", usage);
458f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
459f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
460f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
4615ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // check for device and output changes triggered by new force usage
462f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkA2dpSuspend();
463f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForAllStrategies();
464c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    updateDevicesAndOutputs();
4655ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < mOutputs.size(); i++) {
4665ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_io_handle_t output = mOutputs.keyAt(i);
4675ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_devices_t newDevice = getNewDevice(output, true /*fromCache*/);
468ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        setOutputDevice(output, newDevice, (newDevice != AUDIO_DEVICE_NONE));
469ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (forceVolumeReeval && (newDevice != AUDIO_DEVICE_NONE)) {
4705ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            applyStreamVolumes(output, newDevice, 0, true);
4715ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
472f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
473f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
474f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    audio_io_handle_t activeInput = getActiveInput();
475f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (activeInput != 0) {
476f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioInputDescriptor *inputDesc = mInputs.valueFor(activeInput);
4775ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_devices_t newDevice = getDeviceForInputSource(inputDesc->mInputSource);
478ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if ((newDevice != AUDIO_DEVICE_NONE) && (newDevice != inputDesc->mDevice)) {
4796a70518b93928d1c91457ff805e375c82d76b0e5Steve Block            ALOGV("setForceUse() changing device from %x to %x for input %d",
480f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    inputDesc->mDevice, newDevice, activeInput);
481f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            inputDesc->mDevice = newDevice;
482f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            AudioParameter param = AudioParameter();
483f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            param.addInt(String8(AudioParameter::keyRouting), (int)newDevice);
484f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mpClientInterface->setParameters(activeInput, param.toString());
485f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
486f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
487f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
488f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
489f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
490f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima ZavinAudioSystem::forced_config AudioPolicyManagerBase::getForceUse(AudioSystem::force_use usage)
491f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
492f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return mForceUse[usage];
493f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
494f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
495f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::setSystemProperty(const char* property, const char* value)
496f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
4976a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("setSystemProperty() property %s, value %s", property, value);
498f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
499f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
500b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent// Find a direct output profile compatible with the parameters passed, even if the input flags do
501b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent// not explicitly request a direct output
5023cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric LaurentAudioPolicyManagerBase::IOProfile *AudioPolicyManagerBase::getProfileForDirectOutput(
5033cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                               audio_devices_t device,
50470c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                               uint32_t samplingRate,
5055082dbeb19e37883230510129c94336063a4f91cGlenn Kasten                                                               audio_format_t format,
5062c3d2379ee6eadb32b63bf6de538cfd6487e8e20Glenn Kasten                                                               audio_channel_mask_t channelMask,
5070977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent                                                               audio_output_flags_t flags)
50870c236c9290732782d5267935af1475b8d5ae602Eric Laurent{
50970c236c9290732782d5267935af1475b8d5ae602Eric Laurent    for (size_t i = 0; i < mHwModules.size(); i++) {
51070c236c9290732782d5267935af1475b8d5ae602Eric Laurent        if (mHwModules[i]->mHandle == 0) {
51170c236c9290732782d5267935af1475b8d5ae602Eric Laurent            continue;
51270c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
51370c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++) {
514a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald            IOProfile *profile = mHwModules[i]->mOutputProfiles[j];
515a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald            if (flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) {
516a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                if (profile->isCompatibleProfile(device, samplingRate, format,
517a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                                           channelMask,
518a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                                           AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD)) {
519a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                    if (mAvailableOutputDevices & profile->mSupportedDevices) {
520a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                        return mHwModules[i]->mOutputProfiles[j];
521a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                    }
522a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                }
523b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent            } else {
524a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                if (profile->isCompatibleProfile(device, samplingRate, format,
5253cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                           channelMask,
5263cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                           AUDIO_OUTPUT_FLAG_DIRECT)) {
527a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                    if (mAvailableOutputDevices & profile->mSupportedDevices) {
528a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                        return mHwModules[i]->mOutputProfiles[j];
529a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                    }
530a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                }
531a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald            }
53270c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
53370c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
53470c236c9290732782d5267935af1475b8d5ae602Eric Laurent    return 0;
53570c236c9290732782d5267935af1475b8d5ae602Eric Laurent}
53670c236c9290732782d5267935af1475b8d5ae602Eric Laurent
537f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinaudio_io_handle_t AudioPolicyManagerBase::getOutput(AudioSystem::stream_type stream,
538f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    uint32_t samplingRate,
5395082dbeb19e37883230510129c94336063a4f91cGlenn Kasten                                    audio_format_t format,
5402c3d2379ee6eadb32b63bf6de538cfd6487e8e20Glenn Kasten                                    audio_channel_mask_t channelMask,
541b4d07b97d23cfaffe22c7859ad7c45e168a7df0eRichard Fitzgerald                                    AudioSystem::output_flags flags,
542b4d07b97d23cfaffe22c7859ad7c45e168a7df0eRichard Fitzgerald                                    const audio_offload_info_t *offloadInfo)
543f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
544f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    audio_io_handle_t output = 0;
545f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    uint32_t latency = 0;
546f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    routing_strategy strategy = getStrategy((AudioSystem::stream_type)stream);
5475ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
548a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    ALOGV("getOutput() device %d, stream %d, samplingRate %d, format %x, channelMask %x, flags %x",
549a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald          device, stream, samplingRate, format, channelMask, flags);
550f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
551f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
552f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mCurOutput != 0) {
55370c236c9290732782d5267935af1475b8d5ae602Eric Laurent        ALOGV("getOutput() test output mCurOutput %d, samplingRate %d, format %d, channelMask %x, mDirectOutput %d",
554f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mCurOutput, mTestSamplingRate, mTestFormat, mTestChannels, mDirectOutput);
555f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
556f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (mTestOutputs[mCurOutput] == 0) {
5576a70518b93928d1c91457ff805e375c82d76b0e5Steve Block            ALOGV("getOutput() opening test output");
5583cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(NULL);
559f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            outputDesc->mDevice = mTestDevice;
560f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            outputDesc->mSamplingRate = mTestSamplingRate;
561f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            outputDesc->mFormat = mTestFormat;
56270c236c9290732782d5267935af1475b8d5ae602Eric Laurent            outputDesc->mChannelMask = mTestChannels;
563f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            outputDesc->mLatency = mTestLatencyMs;
5640977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent            outputDesc->mFlags = (audio_output_flags_t)(mDirectOutput ? AudioSystem::OUTPUT_FLAG_DIRECT : 0);
565f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            outputDesc->mRefCount[stream] = 0;
56670c236c9290732782d5267935af1475b8d5ae602Eric Laurent            mTestOutputs[mCurOutput] = mpClientInterface->openOutput(0, &outputDesc->mDevice,
567f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            &outputDesc->mSamplingRate,
568f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            &outputDesc->mFormat,
56970c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                            &outputDesc->mChannelMask,
570f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            &outputDesc->mLatency,
571b4d07b97d23cfaffe22c7859ad7c45e168a7df0eRichard Fitzgerald                                            outputDesc->mFlags,
572b4d07b97d23cfaffe22c7859ad7c45e168a7df0eRichard Fitzgerald                                            offloadInfo);
573f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (mTestOutputs[mCurOutput]) {
574f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                AudioParameter outputCmd = AudioParameter();
575f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                outputCmd.addInt(String8("set_id"),mCurOutput);
576f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mpClientInterface->setParameters(mTestOutputs[mCurOutput],outputCmd.toString());
577f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                addOutput(mTestOutputs[mCurOutput], outputDesc);
578f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
579f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
580f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return mTestOutputs[mCurOutput];
581f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
582f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
583f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
584f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // open a direct output if required by specified parameters
585a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    //force direct flag if offload flag is set: offloading implies a direct output stream
586a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    // and all common behaviors are driven by checking only the direct flag
587a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    // this should normally be set appropriately in the policy configuration file
588a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    if ((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
589a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        flags = (AudioSystem::output_flags)(flags | AUDIO_OUTPUT_FLAG_DIRECT);
590a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    }
591a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald
592000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    // Do not allow offloading if one non offloadable effect is enabled. This prevents from
593000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    // creating an offloaded track and tearing it down immediately after start when audioflinger
594000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    // detects there is an active non offloadable effect.
595000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    // FIXME: We should check the audio session here but we do not have it in this context.
596000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    // This may prevent offloading in rare situations where effects are left active by apps
597000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    // in the background.
598000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    IOProfile *profile = NULL;
599000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    if (((flags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) == 0) ||
600000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent            !isNonOffloadableEffectEnabled()) {
601000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent        profile = getProfileForDirectOutput(device,
602000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent                                           samplingRate,
603000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent                                           format,
604000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent                                           channelMask,
605000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent                                           (audio_output_flags_t)flags);
606000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    }
607000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent
6083cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    if (profile != NULL) {
6095a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        AudioOutputDescriptor *outputDesc = NULL;
610f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
6115a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        for (size_t i = 0; i < mOutputs.size(); i++) {
6125a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent            AudioOutputDescriptor *desc = mOutputs.valueAt(i);
6135a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent            if (!desc->isDuplicated() && (profile == desc->mProfile)) {
6145a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent                outputDesc = desc;
6155a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent                // reuse direct output if currently open and configured with same parameters
6165a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent                if ((samplingRate == outputDesc->mSamplingRate) &&
6175a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent                        (format == outputDesc->mFormat) &&
6185a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent                        (channelMask == outputDesc->mChannelMask)) {
6195a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent                    outputDesc->mDirectOpenCount++;
620a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                    ALOGV("getOutput() reusing direct output %d", mOutputs.keyAt(i));
6215a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent                    return mOutputs.keyAt(i);
6225a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent                }
6235a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent            }
6245a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        }
6255a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        // close direct output if currently open and configured with different parameters
6265a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        if (outputDesc != NULL) {
6275a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent            closeOutput(outputDesc->mId);
6285a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        }
6295a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        outputDesc = new AudioOutputDescriptor(profile);
630f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mDevice = device;
631f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mSamplingRate = samplingRate;
6322d749b0bda4ae60ef1a940eeba86121b2e94d07dGlenn Kasten        outputDesc->mFormat = format;
6332c3d2379ee6eadb32b63bf6de538cfd6487e8e20Glenn Kasten        outputDesc->mChannelMask = channelMask;
634f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mLatency = 0;
635a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        outputDesc->mFlags =(audio_output_flags_t) (outputDesc->mFlags | flags);
636f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mRefCount[stream] = 0;
637f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mStopTime[stream] = 0;
6385a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        outputDesc->mDirectOpenCount = 1;
6393cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        output = mpClientInterface->openOutput(profile->mModule->mHandle,
64070c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                        &outputDesc->mDevice,
641f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                        &outputDesc->mSamplingRate,
642f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                        &outputDesc->mFormat,
64370c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                        &outputDesc->mChannelMask,
644f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                        &outputDesc->mLatency,
645a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                                        outputDesc->mFlags,
646a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                                        offloadInfo);
647f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
6489f1f9b509c930830f6f32e9ef6c2c8a03d6fa96eJean-Michel Trivi        // only accept an output with the requested parameters
649f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (output == 0 ||
650f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            (samplingRate != 0 && samplingRate != outputDesc->mSamplingRate) ||
6512d749b0bda4ae60ef1a940eeba86121b2e94d07dGlenn Kasten            (format != AUDIO_FORMAT_DEFAULT && format != outputDesc->mFormat) ||
65270c236c9290732782d5267935af1475b8d5ae602Eric Laurent            (channelMask != 0 && channelMask != outputDesc->mChannelMask)) {
6533cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            ALOGV("getOutput() failed opening direct output: output %d samplingRate %d %d,"
6543cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    "format %d %d, channelMask %04x %04x", output, samplingRate,
6553cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    outputDesc->mSamplingRate, format, outputDesc->mFormat, channelMask,
6563cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    outputDesc->mChannelMask);
657f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (output != 0) {
658f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mpClientInterface->closeOutput(output);
659f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
660f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            delete outputDesc;
661f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return 0;
662f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
66345c763947b657b347211dc9388754e05d30d0467Eric Laurent        audio_io_handle_t srcOutput = getOutputForEffect();
664f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        addOutput(output, outputDesc);
66545c763947b657b347211dc9388754e05d30d0467Eric Laurent        audio_io_handle_t dstOutput = getOutputForEffect();
66645c763947b657b347211dc9388754e05d30d0467Eric Laurent        if (dstOutput == output) {
66745c763947b657b347211dc9388754e05d30d0467Eric Laurent            mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, srcOutput, dstOutput);
66845c763947b657b347211dc9388754e05d30d0467Eric Laurent        }
6695a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        mPreviousOutputs = mOutputs;
6705a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        ALOGV("getOutput() returns new direct output %d", output);
671f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return output;
672f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
673f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
6749f1f9b509c930830f6f32e9ef6c2c8a03d6fa96eJean-Michel Trivi    // ignoring channel mask due to downmix capability in mixer
6759f1f9b509c930830f6f32e9ef6c2c8a03d6fa96eJean-Michel Trivi
676f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // open a non direct output
677f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
678a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    // for non direct outputs, only PCM is supported
6792d749b0bda4ae60ef1a940eeba86121b2e94d07dGlenn Kasten    if (audio_is_linear_pcm(format)) {
680a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        // get which output is suitable for the specified stream. The actual
681a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        // routing change will happen when startOutput() will be called
682a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        SortedVector<audio_io_handle_t> outputs = getOutputsForDevice(device, mOutputs);
683f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
684a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        output = selectOutput(outputs, flags);
685a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    }
686a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    ALOGW_IF((output == 0), "getOutput() could not find output for stream %d, samplingRate %d,"
68770c236c9290732782d5267935af1475b8d5ae602Eric Laurent            "format %d, channels %x, flags %x", stream, samplingRate, format, channelMask, flags);
688f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
6895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGV("getOutput() returns output %d", output);
6905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
691f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return output;
692f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
693f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
6945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentaudio_io_handle_t AudioPolicyManagerBase::selectOutput(const SortedVector<audio_io_handle_t>& outputs,
6955ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                       AudioSystem::output_flags flags)
6965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
6975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // select one output among several that provide a path to a particular device or set of
6985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // devices (the list was previously build by getOutputsForDevice()).
6995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // The priority is as follows:
7005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // 1: the output with the highest number of requested policy flags
7015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // 2: the primary output
7025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // 3: the first output in the list
7035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
7045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (outputs.size() == 0) {
7055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return 0;
7065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
7075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (outputs.size() == 1) {
7085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return outputs[0];
7095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
7105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
7115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    int maxCommonFlags = 0;
7125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    audio_io_handle_t outputFlags = 0;
7135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    audio_io_handle_t outputPrimary = 0;
7145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
7155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < outputs.size(); i++) {
7165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        AudioOutputDescriptor *outputDesc = mOutputs.valueFor(outputs[i]);
7175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (!outputDesc->isDuplicated()) {
7185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            int commonFlags = (int)AudioSystem::popCount(outputDesc->mProfile->mFlags & flags);
7195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            if (commonFlags > maxCommonFlags) {
7205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                outputFlags = outputs[i];
7215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                maxCommonFlags = commonFlags;
7225ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                ALOGV("selectOutput() commonFlags for output %d, %04x", outputs[i], commonFlags);
7235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
7240977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent            if (outputDesc->mProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
7255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                outputPrimary = outputs[i];
7265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
7275ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
7285ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
7295ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
7305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (outputFlags != 0) {
7315ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return outputFlags;
7325ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
7335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (outputPrimary != 0) {
7345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return outputPrimary;
7355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
7365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
7375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return outputs[0];
7385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
7395ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
740f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::startOutput(audio_io_handle_t output,
741f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                             AudioSystem::stream_type stream,
742f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                             int session)
743f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
7446a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("startOutput() output %d, stream %d, session %d", output, stream, session);
745f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mOutputs.indexOfKey(output);
746f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
7476742b021061741c309d6e45c074b45a560f11086Glenn Kasten        ALOGW("startOutput() unknown output %d", output);
748f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
749f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
750f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
751f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index);
752f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
7535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // increment usage count for this stream on the requested output:
754f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // NOTE that the usage count is the same for duplicated output and hardware output which is
7555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // necessary for a correct control of hardware output routing by startOutput() and stopOutput()
756f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    outputDesc->changeRefCount(stream, 1);
757f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
7585ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (outputDesc->mRefCount[stream] == 1) {
7595ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_devices_t newDevice = getNewDevice(output, false /*fromCache*/);
760b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        routing_strategy strategy = getStrategy(stream);
761b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        bool shouldWait = (strategy == STRATEGY_SONIFICATION) ||
762b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                            (strategy == STRATEGY_SONIFICATION_RESPECTFUL);
763b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        uint32_t waitMs = 0;
7645ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        bool force = false;
7655ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        for (size_t i = 0; i < mOutputs.size(); i++) {
7665ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            AudioOutputDescriptor *desc = mOutputs.valueAt(i);
767b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent            if (desc != outputDesc) {
768b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                // force a device change if any other output is managed by the same hw
769b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                // module and has a current device selection that differs from selected device.
770b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                // In this case, the audio HAL must receive the new device selection so that it can
771b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                // change the device currently selected by the other active output.
772b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                if (outputDesc->sharesHwModuleWith(desc) &&
7735ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                    desc->device() != newDevice) {
774b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                    force = true;
775b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                }
776b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                // wait for audio on other active outputs to be presented when starting
777b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                // a notification so that audio focus effect can propagate.
77842fa8215a763822c0d03fc936e4cac1eb864c9ccEric Laurent                uint32_t latency = desc->latency();
77942fa8215a763822c0d03fc936e4cac1eb864c9ccEric Laurent                if (shouldWait && desc->isActive(latency * 2) && (waitMs < latency)) {
78042fa8215a763822c0d03fc936e4cac1eb864c9ccEric Laurent                    waitMs = latency;
781b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent                }
7825ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
7835ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
784b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        uint32_t muteWaitMs = setOutputDevice(output, newDevice, force);
785f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
7865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // handle special case for sonification while in call
7875ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (isInCall()) {
7885ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            handleIncallSonification(stream, true, false);
7895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
790c16ac09f510437e8340be691720177a490ae78f0Eric Laurent
7915ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // apply volume rules for current stream and device if necessary
7925ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        checkAndSetVolume(stream,
793c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                          mStreams[stream].getVolumeIndex(newDevice),
7945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                          output,
7955ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                          newDevice);
79612bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi
7975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // update the outputs if starting an output with a stream that can affect notification
7985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // routing
7995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        handleNotificationRoutingForStream(stream);
800b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        if (waitMs > muteWaitMs) {
801b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent            usleep((waitMs - muteWaitMs) * 2 * 1000);
802b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        }
8035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
804f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
805f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
806f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
8075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
808f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::stopOutput(audio_io_handle_t output,
809f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            AudioSystem::stream_type stream,
810f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            int session)
811f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
8126a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("stopOutput() output %d, stream %d, session %d", output, stream, session);
813f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mOutputs.indexOfKey(output);
814f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
8156742b021061741c309d6e45c074b45a560f11086Glenn Kasten        ALOGW("stopOutput() unknown output %d", output);
816f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
817f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
818f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
819f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index);
820f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
821f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // handle special case for sonification while in call
822f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (isInCall()) {
823f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        handleIncallSonification(stream, false, false);
824f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
825f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
826f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (outputDesc->mRefCount[stream] > 0) {
827f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // decrement usage count of this stream on the output
828f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->changeRefCount(stream, -1);
829f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // store time at which the stream was stopped - see isStreamActive()
8305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (outputDesc->mRefCount[stream] == 0) {
8315ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            outputDesc->mStopTime[stream] = systemTime();
8325ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            audio_devices_t newDevice = getNewDevice(output, false /*fromCache*/);
8335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // delay the device switch by twice the latency because stopOutput() is executed when
8345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // the track stop() command is received and at that time the audio track buffer can
8355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // still contain data that needs to be drained. The latency only covers the audio HAL
8365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // and kernel buffers. Also the latency does not always include additional delay in the
8375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // audio path (audio DSP, CODEC ...)
8385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            setOutputDevice(output, newDevice, false, outputDesc->mLatency*2);
8395ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
8405ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // force restoring the device selection on other active outputs if it differs from the
8415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // one being selected for this output
8425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            for (size_t i = 0; i < mOutputs.size(); i++) {
8435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                audio_io_handle_t curOutput = mOutputs.keyAt(i);
8445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                AudioOutputDescriptor *desc = mOutputs.valueAt(i);
8455ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                if (curOutput != output &&
84680f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                        desc->isActive() &&
8475ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                        outputDesc->sharesHwModuleWith(desc) &&
848a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                        (newDevice != desc->device())) {
8495ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                    setOutputDevice(curOutput,
8505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                    getNewDevice(curOutput, false /*fromCache*/),
8515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                    true,
8525ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                    outputDesc->mLatency*2);
8535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                }
8545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
8555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            // update the outputs if stopping one with a stream that can affect notification routing
8565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            handleNotificationRoutingForStream(stream);
857f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
858f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return NO_ERROR;
859f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
86064cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("stopOutput() refcount is already 0 for output %d", output);
861f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return INVALID_OPERATION;
862f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
863f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
864f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
865f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::releaseOutput(audio_io_handle_t output)
866f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
8676a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("releaseOutput() %d", output);
868f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mOutputs.indexOfKey(output);
869f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
87064cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("releaseOutput() releasing unknown output %d", output);
871f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
872f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
873f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
874f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
875f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    int testIndex = testOutputIndex(output);
876f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (testIndex != 0) {
877f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioOutputDescriptor *outputDesc = mOutputs.valueAt(index);
87880f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent        if (outputDesc->isActive()) {
879f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mpClientInterface->closeOutput(output);
880f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            delete mOutputs.valueAt(index);
881f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mOutputs.removeItem(output);
882f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mTestOutputs[testIndex] = 0;
883f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
884f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
885f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
886f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
887f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
8885a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent    AudioOutputDescriptor *desc = mOutputs.valueAt(index);
8895a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent    if (desc->mFlags & AudioSystem::OUTPUT_FLAG_DIRECT) {
8905a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        if (desc->mDirectOpenCount <= 0) {
8915a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent            ALOGW("releaseOutput() invalid open count %d for output %d",
8925a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent                                                              desc->mDirectOpenCount, output);
8935a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent            return;
8945a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        }
8955a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        if (--desc->mDirectOpenCount == 0) {
8965a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent            closeOutput(output);
89745c763947b657b347211dc9388754e05d30d0467Eric Laurent            // If effects where present on the output, audioflinger moved them to the primary
89845c763947b657b347211dc9388754e05d30d0467Eric Laurent            // output by default: move them back to the appropriate output.
89945c763947b657b347211dc9388754e05d30d0467Eric Laurent            audio_io_handle_t dstOutput = getOutputForEffect();
90045c763947b657b347211dc9388754e05d30d0467Eric Laurent            if (dstOutput != mPrimaryOutput) {
90145c763947b657b347211dc9388754e05d30d0467Eric Laurent                mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, mPrimaryOutput, dstOutput);
90245c763947b657b347211dc9388754e05d30d0467Eric Laurent            }
9035a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent        }
904f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
905f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
906f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
90745c763947b657b347211dc9388754e05d30d0467Eric Laurent
908f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinaudio_io_handle_t AudioPolicyManagerBase::getInput(int inputSource,
909f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    uint32_t samplingRate,
9105082dbeb19e37883230510129c94336063a4f91cGlenn Kasten                                    audio_format_t format,
9112c3d2379ee6eadb32b63bf6de538cfd6487e8e20Glenn Kasten                                    audio_channel_mask_t channelMask,
912f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    AudioSystem::audio_in_acoustics acoustics)
913f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
914f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    audio_io_handle_t input = 0;
915f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent    audio_devices_t device = getDeviceForInputSource(inputSource);
916f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
91770c236c9290732782d5267935af1475b8d5ae602Eric Laurent    ALOGV("getInput() inputSource %d, samplingRate %d, format %d, channelMask %x, acoustics %x",
91870c236c9290732782d5267935af1475b8d5ae602Eric Laurent          inputSource, samplingRate, format, channelMask, acoustics);
919f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
920ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if (device == AUDIO_DEVICE_NONE) {
9215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        ALOGW("getInput() could not find device for inputSource %d", inputSource);
922f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return 0;
923f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
924f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
925f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // adapt channel selection to input source
926f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    switch(inputSource) {
927f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_UPLINK:
9282c3d2379ee6eadb32b63bf6de538cfd6487e8e20Glenn Kasten        channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK;
929f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
930f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_DOWNLINK:
9312c3d2379ee6eadb32b63bf6de538cfd6487e8e20Glenn Kasten        channelMask = AUDIO_CHANNEL_IN_VOICE_DNLINK;
932f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
933f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_CALL:
9342c3d2379ee6eadb32b63bf6de538cfd6487e8e20Glenn Kasten        channelMask = AUDIO_CHANNEL_IN_VOICE_UPLINK | AUDIO_CHANNEL_IN_VOICE_DNLINK;
935f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
936f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    default:
937f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
938f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
939f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
9405ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    IOProfile *profile = getInputProfile(device,
9415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                         samplingRate,
9425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                         format,
94370c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                         channelMask);
9445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (profile == NULL) {
945fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        ALOGW("getInput() could not find profile for device 0x%X, samplingRate %d, format %d, "
946fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                "channelMask 0x%X",
94770c236c9290732782d5267935af1475b8d5ae602Eric Laurent                device, samplingRate, format, channelMask);
94870c236c9290732782d5267935af1475b8d5ae602Eric Laurent        return 0;
94970c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
95070c236c9290732782d5267935af1475b8d5ae602Eric Laurent
95170c236c9290732782d5267935af1475b8d5ae602Eric Laurent    if (profile->mModule->mHandle == 0) {
9523cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        ALOGE("getInput(): HW module %s not opened", profile->mModule->mName);
9535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return 0;
9545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
9555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
9565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    AudioInputDescriptor *inputDesc = new AudioInputDescriptor(profile);
957f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
958f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    inputDesc->mInputSource = inputSource;
959f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    inputDesc->mDevice = device;
960f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    inputDesc->mSamplingRate = samplingRate;
9612d749b0bda4ae60ef1a940eeba86121b2e94d07dGlenn Kasten    inputDesc->mFormat = format;
9622c3d2379ee6eadb32b63bf6de538cfd6487e8e20Glenn Kasten    inputDesc->mChannelMask = channelMask;
963f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    inputDesc->mRefCount = 0;
964fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
96570c236c9290732782d5267935af1475b8d5ae602Eric Laurent    input = mpClientInterface->openInput(profile->mModule->mHandle,
96670c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                    &inputDesc->mDevice,
967f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    &inputDesc->mSamplingRate,
968f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                    &inputDesc->mFormat,
96970c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                    &inputDesc->mChannelMask);
970f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
971f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // only accept input with the exact requested set of parameters
972f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (input == 0 ||
973f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        (samplingRate != inputDesc->mSamplingRate) ||
974f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        (format != inputDesc->mFormat) ||
97570c236c9290732782d5267935af1475b8d5ae602Eric Laurent        (channelMask != inputDesc->mChannelMask)) {
976fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        ALOGI("getInput() failed opening input: samplingRate %d, format %d, channelMask 0x%X",
97770c236c9290732782d5267935af1475b8d5ae602Eric Laurent                samplingRate, format, channelMask);
978f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (input != 0) {
979f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mpClientInterface->closeInput(input);
980f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
981f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        delete inputDesc;
982f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return 0;
983f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
984fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    addInput(input, inputDesc);
985fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
986f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return input;
987f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
988f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
989f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::startInput(audio_io_handle_t input)
990f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
9916a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("startInput() input %d", input);
992f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mInputs.indexOfKey(input);
993f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
9946742b021061741c309d6e45c074b45a560f11086Glenn Kasten        ALOGW("startInput() unknown input %d", input);
995f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
996f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
997f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioInputDescriptor *inputDesc = mInputs.valueAt(index);
998f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
999f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
1000f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mTestInput == 0)
1001f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
1002f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    {
1003fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent        // refuse 2 active AudioRecord clients at the same time except if the active input
1004fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent        // uses AUDIO_SOURCE_HOTWORD in which case it is closed.
1005fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent        audio_io_handle_t activeInput = getActiveInput();
1006fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent        if (!isVirtualInputDevice(inputDesc->mDevice) && activeInput != 0) {
1007fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent            AudioInputDescriptor *activeDesc = mInputs.valueFor(activeInput);
1008fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent            if (activeDesc->mInputSource == AUDIO_SOURCE_HOTWORD) {
1009fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent                ALOGW("startInput() preempting already started low-priority input %d", activeInput);
1010fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent                stopInput(activeInput);
1011fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent                releaseInput(activeInput);
1012fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent            } else {
10136742b021061741c309d6e45c074b45a560f11086Glenn Kasten                ALOGW("startInput() input %d failed: other input already started", input);
1014fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent                return INVALID_OPERATION;
1015fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent            }
1016f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1017f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1018f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
101966707435156d8d99d795271a7bd54943065b4c2dEric Laurent    audio_devices_t newDevice = getDeviceForInputSource(inputDesc->mInputSource);
102066707435156d8d99d795271a7bd54943065b4c2dEric Laurent    if ((newDevice != AUDIO_DEVICE_NONE) && (newDevice != inputDesc->mDevice)) {
102166707435156d8d99d795271a7bd54943065b4c2dEric Laurent        inputDesc->mDevice = newDevice;
102266707435156d8d99d795271a7bd54943065b4c2dEric Laurent    }
1023b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown
1024b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown    // automatically enable the remote submix output when input is started
1025b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown    if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1026b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown        setDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1027b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown                AudioSystem::DEVICE_STATE_AVAILABLE, AUDIO_REMOTE_SUBMIX_DEVICE_ADDRESS);
1028b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown    }
1029b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown
1030f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioParameter param = AudioParameter();
1031f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    param.addInt(String8(AudioParameter::keyRouting), (int)inputDesc->mDevice);
1032f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1033fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent    int aliasSource = (inputDesc->mInputSource == AUDIO_SOURCE_HOTWORD) ?
1034fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent                                        AUDIO_SOURCE_VOICE_RECOGNITION : inputDesc->mInputSource;
1035fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent
1036fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent    param.addInt(String8(AudioParameter::keyInputSource), aliasSource);
10376a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("AudioPolicyManager::startInput() input source = %d", inputDesc->mInputSource);
1038f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1039f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mpClientInterface->setParameters(input, param.toString());
1040f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1041f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    inputDesc->mRefCount = 1;
1042f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
1043f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1044f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1045f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::stopInput(audio_io_handle_t input)
1046f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
10476a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("stopInput() input %d", input);
1048f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mInputs.indexOfKey(input);
1049f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
10506742b021061741c309d6e45c074b45a560f11086Glenn Kasten        ALOGW("stopInput() unknown input %d", input);
1051f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
1052f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1053f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioInputDescriptor *inputDesc = mInputs.valueAt(index);
1054f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1055f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (inputDesc->mRefCount == 0) {
105664cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("stopInput() input %d already stopped", input);
1057f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return INVALID_OPERATION;
1058f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
1059b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown        // automatically disable the remote submix output when input is stopped
1060b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown        if (audio_is_remote_submix_device(inputDesc->mDevice)) {
1061b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown            setDeviceConnectionState(AUDIO_DEVICE_OUT_REMOTE_SUBMIX,
1062b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown                    AudioSystem::DEVICE_STATE_UNAVAILABLE, AUDIO_REMOTE_SUBMIX_DEVICE_ADDRESS);
1063b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown        }
1064b96b2839f464f93e30d256124034ec985f8a9702Jeff Brown
1065f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioParameter param = AudioParameter();
1066f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        param.addInt(String8(AudioParameter::keyRouting), 0);
1067f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mpClientInterface->setParameters(input, param.toString());
1068f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        inputDesc->mRefCount = 0;
1069f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return NO_ERROR;
1070f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1071f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1072f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1073f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::releaseInput(audio_io_handle_t input)
1074f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
10756a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("releaseInput() %d", input);
1076f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mInputs.indexOfKey(input);
1077f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
107864cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("releaseInput() releasing unknown input %d", input);
1079f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
1080f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1081f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mpClientInterface->closeInput(input);
1082f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    delete mInputs.valueAt(index);
1083f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mInputs.removeItem(input);
1084fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
10856a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("releaseInput() exit");
1086f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1087f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1088fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLeanvoid AudioPolicyManagerBase::closeAllInputs() {
1089fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    for(size_t input_index = 0; input_index < mInputs.size(); input_index++) {
1090fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        mpClientInterface->closeInput(mInputs.keyAt(input_index));
1091fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    }
1092fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    mInputs.clear();
1093fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean}
1094fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
1095f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::initStreamVolume(AudioSystem::stream_type stream,
1096f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            int indexMin,
1097f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                            int indexMax)
1098f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
10996a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("initStreamVolume() stream %d, min %d, max %d", stream , indexMin, indexMax);
1100f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (indexMin < 0 || indexMin >= indexMax) {
110164cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("initStreamVolume() invalid index limits for stream %d, min %d, max %d", stream , indexMin, indexMax);
1102f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
1103f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1104f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mStreams[stream].mIndexMin = indexMin;
1105f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mStreams[stream].mIndexMax = indexMax;
1106f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1107f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1108c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentstatus_t AudioPolicyManagerBase::setStreamVolumeIndex(AudioSystem::stream_type stream,
1109c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                      int index,
1110c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                      audio_devices_t device)
1111f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1112f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1113f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if ((index < mStreams[stream].mIndexMin) || (index > mStreams[stream].mIndexMax)) {
1114f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
1115f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1116c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    if (!audio_is_output_device(device)) {
1117c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        return BAD_VALUE;
1118c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    }
1119f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1120f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // Force max volume if stream cannot be muted
1121f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (!mStreams[stream].mCanBeMuted) index = mStreams[stream].mIndexMax;
1122f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1123b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    ALOGV("setStreamVolumeIndex() stream %d, device %04x, index %d",
1124c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent          stream, device, index);
1125c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
1126c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    // if device is AUDIO_DEVICE_OUT_DEFAULT set default value and
1127c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    // clear all device specific values
1128c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    if (device == AUDIO_DEVICE_OUT_DEFAULT) {
1129c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        mStreams[stream].mIndexCur.clear();
1130c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    }
1131c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    mStreams[stream].mIndexCur.add(device, index);
1132f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1133f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // compute and apply stream volume on all outputs according to connected device
1134f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    status_t status = NO_ERROR;
1135f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mOutputs.size(); i++) {
1136c5eb8b4a5d4395ce335bc7c3e6df2678fa47e2ddEric Laurent        audio_devices_t curDevice =
1137c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                getDeviceForVolume(mOutputs.valueAt(i)->device());
1138e92d623811f3fd3e7cc5e5dd8bc93c0c0a8fdf50Eric Laurent        if ((device == AUDIO_DEVICE_OUT_DEFAULT) || (device == curDevice)) {
1139c5eb8b4a5d4395ce335bc7c3e6df2678fa47e2ddEric Laurent            status_t volStatus = checkAndSetVolume(stream, index, mOutputs.keyAt(i), curDevice);
1140c5eb8b4a5d4395ce335bc7c3e6df2678fa47e2ddEric Laurent            if (volStatus != NO_ERROR) {
1141c5eb8b4a5d4395ce335bc7c3e6df2678fa47e2ddEric Laurent                status = volStatus;
1142c5eb8b4a5d4395ce335bc7c3e6df2678fa47e2ddEric Laurent            }
1143f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1144f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1145f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return status;
1146f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1147f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1148c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentstatus_t AudioPolicyManagerBase::getStreamVolumeIndex(AudioSystem::stream_type stream,
1149c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                      int *index,
1150c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                      audio_devices_t device)
1151f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1152c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    if (index == NULL) {
1153f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return BAD_VALUE;
1154f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1155c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    if (!audio_is_output_device(device)) {
1156c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        return BAD_VALUE;
1157c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    }
1158c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    // if device is AUDIO_DEVICE_OUT_DEFAULT, return volume for device corresponding to
1159c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    // the strategy the stream belongs to.
1160c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    if (device == AUDIO_DEVICE_OUT_DEFAULT) {
1161c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        device = getDeviceForStrategy(getStrategy(stream), true /*fromCache*/);
1162c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    }
1163c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    device = getDeviceForVolume(device);
1164c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
1165c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    *index =  mStreams[stream].getVolumeIndex(device);
1166c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    ALOGV("getStreamVolumeIndex() stream %d device %08x index %d", stream, device, *index);
1167f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
1168f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1169f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
117045c763947b657b347211dc9388754e05d30d0467Eric Laurentaudio_io_handle_t AudioPolicyManagerBase::selectOutputForEffects(
117145c763947b657b347211dc9388754e05d30d0467Eric Laurent                                            const SortedVector<audio_io_handle_t>& outputs)
117245c763947b657b347211dc9388754e05d30d0467Eric Laurent{
117345c763947b657b347211dc9388754e05d30d0467Eric Laurent    // select one output among several suitable for global effects.
117445c763947b657b347211dc9388754e05d30d0467Eric Laurent    // The priority is as follows:
117545c763947b657b347211dc9388754e05d30d0467Eric Laurent    // 1: An offloaded output. If the effect ends up not being offloadable,
117645c763947b657b347211dc9388754e05d30d0467Eric Laurent    //    AudioFlinger will invalidate the track and the offloaded output
117745c763947b657b347211dc9388754e05d30d0467Eric Laurent    //    will be closed causing the effect to be moved to a PCM output.
117845c763947b657b347211dc9388754e05d30d0467Eric Laurent    // 2: A deep buffer output
117945c763947b657b347211dc9388754e05d30d0467Eric Laurent    // 3: the first output in the list
118045c763947b657b347211dc9388754e05d30d0467Eric Laurent
118145c763947b657b347211dc9388754e05d30d0467Eric Laurent    if (outputs.size() == 0) {
118245c763947b657b347211dc9388754e05d30d0467Eric Laurent        return 0;
118345c763947b657b347211dc9388754e05d30d0467Eric Laurent    }
118445c763947b657b347211dc9388754e05d30d0467Eric Laurent
118545c763947b657b347211dc9388754e05d30d0467Eric Laurent    audio_io_handle_t outputOffloaded = 0;
118645c763947b657b347211dc9388754e05d30d0467Eric Laurent    audio_io_handle_t outputDeepBuffer = 0;
118745c763947b657b347211dc9388754e05d30d0467Eric Laurent
118845c763947b657b347211dc9388754e05d30d0467Eric Laurent    for (size_t i = 0; i < outputs.size(); i++) {
118945c763947b657b347211dc9388754e05d30d0467Eric Laurent        AudioOutputDescriptor *desc = mOutputs.valueFor(outputs[i]);
1190c7b6e3c857c5f5af5f244d0b877c41a9c55f92abColin Cross        ALOGV("selectOutputForEffects outputs[%zu] flags %x", i, desc->mFlags);
119145c763947b657b347211dc9388754e05d30d0467Eric Laurent        if ((desc->mFlags & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
119245c763947b657b347211dc9388754e05d30d0467Eric Laurent            outputOffloaded = outputs[i];
119345c763947b657b347211dc9388754e05d30d0467Eric Laurent        }
119445c763947b657b347211dc9388754e05d30d0467Eric Laurent        if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DEEP_BUFFER) != 0) {
119545c763947b657b347211dc9388754e05d30d0467Eric Laurent            outputDeepBuffer = outputs[i];
119645c763947b657b347211dc9388754e05d30d0467Eric Laurent        }
119745c763947b657b347211dc9388754e05d30d0467Eric Laurent    }
119845c763947b657b347211dc9388754e05d30d0467Eric Laurent
119945c763947b657b347211dc9388754e05d30d0467Eric Laurent    ALOGV("selectOutputForEffects outputOffloaded %d outputDeepBuffer %d",
120045c763947b657b347211dc9388754e05d30d0467Eric Laurent          outputOffloaded, outputDeepBuffer);
120145c763947b657b347211dc9388754e05d30d0467Eric Laurent    if (outputOffloaded != 0) {
120245c763947b657b347211dc9388754e05d30d0467Eric Laurent        return outputOffloaded;
120345c763947b657b347211dc9388754e05d30d0467Eric Laurent    }
120445c763947b657b347211dc9388754e05d30d0467Eric Laurent    if (outputDeepBuffer != 0) {
120545c763947b657b347211dc9388754e05d30d0467Eric Laurent        return outputDeepBuffer;
120645c763947b657b347211dc9388754e05d30d0467Eric Laurent    }
120745c763947b657b347211dc9388754e05d30d0467Eric Laurent
120845c763947b657b347211dc9388754e05d30d0467Eric Laurent    return outputs[0];
120945c763947b657b347211dc9388754e05d30d0467Eric Laurent}
121045c763947b657b347211dc9388754e05d30d0467Eric Laurent
1211c94dccc97cc3ed5171b45f46a0f7f8762d37156fGlenn Kastenaudio_io_handle_t AudioPolicyManagerBase::getOutputForEffect(const effect_descriptor_t *desc)
1212f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1213f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // apply simple rule where global effects are attached to the same output as MUSIC streams
12144660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen
12154660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen    routing_strategy strategy = getStrategy(AudioSystem::MUSIC);
12164660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen    audio_devices_t device = getDeviceForStrategy(strategy, false /*fromCache*/);
1217c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(device, mOutputs);
121845c763947b657b347211dc9388754e05d30d0467Eric Laurent
121945c763947b657b347211dc9388754e05d30d0467Eric Laurent    audio_io_handle_t output = selectOutputForEffects(dstOutputs);
122045c763947b657b347211dc9388754e05d30d0467Eric Laurent    ALOGV("getOutputForEffect() got output %d for fx %s flags %x",
122145c763947b657b347211dc9388754e05d30d0467Eric Laurent          output, (desc == NULL) ? "unspecified" : desc->name,  (desc == NULL) ? 0 : desc->flags);
122245c763947b657b347211dc9388754e05d30d0467Eric Laurent
122345c763947b657b347211dc9388754e05d30d0467Eric Laurent    return output;
1224f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1225f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1226c94dccc97cc3ed5171b45f46a0f7f8762d37156fGlenn Kastenstatus_t AudioPolicyManagerBase::registerEffect(const effect_descriptor_t *desc,
12271c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent                                audio_io_handle_t io,
1228f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                uint32_t strategy,
1229f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                int session,
1230f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                int id)
1231f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
12321c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent    ssize_t index = mOutputs.indexOfKey(io);
1233f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
12341c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent        index = mInputs.indexOfKey(io);
12351c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent        if (index < 0) {
123664cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("registerEffect() unknown io %d", io);
12371c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent            return INVALID_OPERATION;
12381c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent        }
1239f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1240f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1241f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mTotalEffectsMemory + desc->memoryUsage > getMaxEffectsMemory()) {
124264cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("registerEffect() memory limit exceeded for Fx %s, Memory %d KB",
1243f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                desc->name, desc->memoryUsage);
1244f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return INVALID_OPERATION;
1245f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1246f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mTotalEffectsMemory += desc->memoryUsage;
12476a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("registerEffect() effect %s, io %d, strategy %d session %d id %d",
12481c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent            desc->name, io, strategy, session, id);
12496a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("registerEffect() memory %d, total memory %d", desc->memoryUsage, mTotalEffectsMemory);
1250f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1251f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    EffectDescriptor *pDesc = new EffectDescriptor();
1252f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    memcpy (&pDesc->mDesc, desc, sizeof(effect_descriptor_t));
12531c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent    pDesc->mIo = io;
1254f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    pDesc->mStrategy = (routing_strategy)strategy;
1255f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    pDesc->mSession = session;
1256582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    pDesc->mEnabled = false;
1257582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent
1258f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mEffects.add(id, pDesc);
1259f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1260f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
1261f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1262f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1263f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::unregisterEffect(int id)
1264f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1265f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    ssize_t index = mEffects.indexOfKey(id);
1266f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (index < 0) {
126764cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("unregisterEffect() unknown effect ID %d", id);
1268f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return INVALID_OPERATION;
1269f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1270f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1271f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    EffectDescriptor *pDesc = mEffects.valueAt(index);
1272f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1273582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    setEffectEnabled(pDesc, false);
1274582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent
1275f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mTotalEffectsMemory < pDesc->mDesc.memoryUsage) {
127664cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("unregisterEffect() memory %d too big for total %d",
1277f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                pDesc->mDesc.memoryUsage, mTotalEffectsMemory);
1278f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        pDesc->mDesc.memoryUsage = mTotalEffectsMemory;
1279f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1280f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mTotalEffectsMemory -= pDesc->mDesc.memoryUsage;
12816a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("unregisterEffect() effect %s, ID %d, memory %d total memory %d",
1282582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent            pDesc->mDesc.name, id, pDesc->mDesc.memoryUsage, mTotalEffectsMemory);
1283f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1284f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mEffects.removeItem(id);
1285f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    delete pDesc;
1286f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1287f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
1288f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1289f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1290582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurentstatus_t AudioPolicyManagerBase::setEffectEnabled(int id, bool enabled)
1291582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent{
1292582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    ssize_t index = mEffects.indexOfKey(id);
1293582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    if (index < 0) {
129464cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("unregisterEffect() unknown effect ID %d", id);
1295582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        return INVALID_OPERATION;
1296582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    }
1297582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent
1298582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    return setEffectEnabled(mEffects.valueAt(index), enabled);
1299582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent}
1300582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent
1301582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurentstatus_t AudioPolicyManagerBase::setEffectEnabled(EffectDescriptor *pDesc, bool enabled)
1302582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent{
1303582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    if (enabled == pDesc->mEnabled) {
13046a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("setEffectEnabled(%s) effect already %s",
1305582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent             enabled?"true":"false", enabled?"enabled":"disabled");
1306582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        return INVALID_OPERATION;
1307582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    }
1308582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent
1309582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    if (enabled) {
1310582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        if (mTotalEffectsCpuLoad + pDesc->mDesc.cpuLoad > getMaxEffectsCpuLoad()) {
131164cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("setEffectEnabled(true) CPU Load limit exceeded for Fx %s, CPU %f MIPS",
1312582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent                 pDesc->mDesc.name, (float)pDesc->mDesc.cpuLoad/10);
1313582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent            return INVALID_OPERATION;
1314582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        }
1315582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        mTotalEffectsCpuLoad += pDesc->mDesc.cpuLoad;
13166a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("setEffectEnabled(true) total CPU %d", mTotalEffectsCpuLoad);
1317582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    } else {
1318582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        if (mTotalEffectsCpuLoad < pDesc->mDesc.cpuLoad) {
131964cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block            ALOGW("setEffectEnabled(false) CPU load %d too high for total %d",
1320582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent                    pDesc->mDesc.cpuLoad, mTotalEffectsCpuLoad);
1321582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent            pDesc->mDesc.cpuLoad = mTotalEffectsCpuLoad;
1322582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        }
1323582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent        mTotalEffectsCpuLoad -= pDesc->mDesc.cpuLoad;
13246a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("setEffectEnabled(false) total CPU %d", mTotalEffectsCpuLoad);
1325582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    }
1326582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    pDesc->mEnabled = enabled;
1327582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    return NO_ERROR;
1328582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent}
1329582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent
1330000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurentbool AudioPolicyManagerBase::isNonOffloadableEffectEnabled()
1331000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent{
1332000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    for (size_t i = 0; i < mEffects.size(); i++) {
1333000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent        const EffectDescriptor * const pDesc = mEffects.valueAt(i);
1334000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent        if (pDesc->mEnabled && (pDesc->mStrategy == STRATEGY_MEDIA) &&
1335000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent                ((pDesc->mDesc.flags & EFFECT_FLAG_OFFLOAD_SUPPORTED) == 0)) {
1336000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent            ALOGV("isNonOffloadableEffectEnabled() non offloadable effect %s enabled on session %d",
1337000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent                  pDesc->mDesc.name, pDesc->mSession);
1338000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent            return true;
1339000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent        }
1340000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    }
1341000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    return false;
1342000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent}
1343000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent
1344f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinbool AudioPolicyManagerBase::isStreamActive(int stream, uint32_t inPastMs) const
1345f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1346f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    nsecs_t sysTime = systemTime();
1347f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mOutputs.size(); i++) {
134880f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent        const AudioOutputDescriptor *outputDesc = mOutputs.valueAt(i);
134980f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent        if (outputDesc->isStreamActive((AudioSystem::stream_type)stream, inPastMs, sysTime)) {
1350f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return true;
1351f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1352f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1353f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return false;
1354f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1355f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1356dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivibool AudioPolicyManagerBase::isStreamActiveRemotely(int stream, uint32_t inPastMs) const
1357dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi{
1358dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi    nsecs_t sysTime = systemTime();
1359dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi    for (size_t i = 0; i < mOutputs.size(); i++) {
1360dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi        const AudioOutputDescriptor *outputDesc = mOutputs.valueAt(i);
136180f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent        if (((outputDesc->device() & APM_AUDIO_OUT_DEVICE_REMOTE_ALL) != 0) &&
136280f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                outputDesc->isStreamActive((AudioSystem::stream_type)stream, inPastMs, sysTime)) {
1363dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi            return true;
1364dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi        }
1365dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi    }
1366dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi    return false;
1367dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi}
1368dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi
1369abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivibool AudioPolicyManagerBase::isSourceActive(audio_source_t source) const
1370abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi{
1371abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi    for (size_t i = 0; i < mInputs.size(); i++) {
1372abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi        const AudioInputDescriptor * inputDescriptor = mInputs.valueAt(i);
1373fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent        if ((inputDescriptor->mInputSource == (int)source ||
1374fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent                (source == (audio_source_t)AUDIO_SOURCE_VOICE_RECOGNITION &&
1375fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent                 inputDescriptor->mInputSource == AUDIO_SOURCE_HOTWORD))
1376fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent             && (inputDescriptor->mRefCount > 0)) {
1377abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi            return true;
1378abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi        }
1379abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi    }
1380abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi    return false;
1381abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi}
1382abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi
1383abc55c698301e05a80d7f9394c75abca0b307602Jean-Michel Trivi
1384f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::dump(int fd)
1385f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1386f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    const size_t SIZE = 256;
1387f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    char buffer[SIZE];
1388f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    String8 result;
1389f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1390f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "\nAudioPolicyManager Dump: %p\n", this);
1391f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1392b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
139370c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, " Primary Output: %d\n", mPrimaryOutput);
1394f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1395f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " A2DP device address: %s\n", mA2dpDeviceAddress.string());
1396f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1397f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " SCO device address: %s\n", mScoDeviceAddress.string());
1398f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1399fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    snprintf(buffer, SIZE, " USB audio ALSA %s\n", mUsbOutCardAndDevice.string());
1400599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    result.append(buffer);
1401f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Output devices: %08x\n", mAvailableOutputDevices);
1402f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1403f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Input devices: %08x\n", mAvailableInputDevices);
1404f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1405f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Phone state: %d\n", mPhoneState);
1406f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1407f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Force use for communications %d\n", mForceUse[AudioSystem::FOR_COMMUNICATION]);
1408f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1409f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Force use for media %d\n", mForceUse[AudioSystem::FOR_MEDIA]);
1410f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1411f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Force use for record %d\n", mForceUse[AudioSystem::FOR_RECORD]);
1412f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1413f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Force use for dock %d\n", mForceUse[AudioSystem::FOR_DOCK]);
1414f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
1415738207def5f691d605ae33d041116829a74513a9Eric Laurent    snprintf(buffer, SIZE, " Force use for system %d\n", mForceUse[AudioSystem::FOR_SYSTEM]);
1416738207def5f691d605ae33d041116829a74513a9Eric Laurent    result.append(buffer);
1417f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, result.string(), result.size());
1418f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
14195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
142070c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "\nHW Modules dump:\n");
14215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    write(fd, buffer, strlen(buffer));
142270c236c9290732782d5267935af1475b8d5ae602Eric Laurent    for (size_t i = 0; i < mHwModules.size(); i++) {
1423c7b6e3c857c5f5af5f244d0b877c41a9c55f92abColin Cross        snprintf(buffer, SIZE, "- HW Module %zu:\n", i + 1);
14245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        write(fd, buffer, strlen(buffer));
142570c236c9290732782d5267935af1475b8d5ae602Eric Laurent        mHwModules[i]->dump(fd);
14265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
14275ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
1428f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "\nOutputs dump:\n");
1429f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
1430f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mOutputs.size(); i++) {
1431f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        snprintf(buffer, SIZE, "- Output %d dump:\n", mOutputs.keyAt(i));
1432f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        write(fd, buffer, strlen(buffer));
1433f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mOutputs.valueAt(i)->dump(fd);
1434f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1435f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1436f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "\nInputs dump:\n");
1437f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
1438f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mInputs.size(); i++) {
1439f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        snprintf(buffer, SIZE, "- Input %d dump:\n", mInputs.keyAt(i));
1440f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        write(fd, buffer, strlen(buffer));
1441f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mInputs.valueAt(i)->dump(fd);
1442f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1443f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1444f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "\nStreams dump:\n");
1445f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
1446c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    snprintf(buffer, SIZE,
1447c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent             " Stream  Can be muted  Index Min  Index Max  Index Cur [device : index]...\n");
1448f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
1449f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) {
1450c7b6e3c857c5f5af5f244d0b877c41a9c55f92abColin Cross        snprintf(buffer, SIZE, " %02zu      ", i);
1451f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        write(fd, buffer, strlen(buffer));
1452c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        mStreams[i].dump(fd);
1453f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1454f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1455f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "\nTotal Effects CPU: %f MIPS, Total Effects memory: %d KB\n",
1456f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            (float)mTotalEffectsCpuLoad/10, mTotalEffectsMemory);
1457f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
1458f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1459f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, "Registered effects:\n");
1460f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, buffer, strlen(buffer));
1461f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mEffects.size(); i++) {
1462f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        snprintf(buffer, SIZE, "- Effect %d dump:\n", mEffects.keyAt(i));
1463f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        write(fd, buffer, strlen(buffer));
1464f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mEffects.valueAt(i)->dump(fd);
1465f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1466f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1467f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1468f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
1469f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1470f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1471a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald// This function checks for the parameters which can be offloaded.
1472a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald// This can be enhanced depending on the capability of the DSP and policy
1473a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald// of the system.
1474b4d07b97d23cfaffe22c7859ad7c45e168a7df0eRichard Fitzgeraldbool AudioPolicyManagerBase::isOffloadSupported(const audio_offload_info_t& offloadInfo)
1475b4d07b97d23cfaffe22c7859ad7c45e168a7df0eRichard Fitzgerald{
1476a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    ALOGV("isOffloadSupported: SR=%u, CM=0x%x, Format=0x%x, StreamType=%d,"
1477c7b6e3c857c5f5af5f244d0b877c41a9c55f92abColin Cross     " BitRate=%u, duration=%" PRId64 " us, has_video=%d",
1478a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald     offloadInfo.sample_rate, offloadInfo.channel_mask,
1479a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald     offloadInfo.format,
1480a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald     offloadInfo.stream_type, offloadInfo.bit_rate, offloadInfo.duration_us,
1481a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald     offloadInfo.has_video);
1482a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald
1483a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    // Check if offload has been disabled
1484a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    char propValue[PROPERTY_VALUE_MAX];
1485a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    if (property_get("audio.offload.disable", propValue, "0")) {
1486a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        if (atoi(propValue) != 0) {
1487a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald            ALOGV("offload disabled by audio.offload.disable=%s", propValue );
1488a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald            return false;
1489a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        }
1490a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    }
1491a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald
1492a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    // Check if stream type is music, then only allow offload as of now.
1493a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    if (offloadInfo.stream_type != AUDIO_STREAM_MUSIC)
1494a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    {
149541b150b659d4ba33a9be9f99fd9f0ee1f2787541Eric Laurent        ALOGV("isOffloadSupported: stream_type != MUSIC, returning false");
149641b150b659d4ba33a9be9f99fd9f0ee1f2787541Eric Laurent        return false;
149741b150b659d4ba33a9be9f99fd9f0ee1f2787541Eric Laurent    }
149841b150b659d4ba33a9be9f99fd9f0ee1f2787541Eric Laurent
149941b150b659d4ba33a9be9f99fd9f0ee1f2787541Eric Laurent    //TODO: enable audio offloading with video when ready
150041b150b659d4ba33a9be9f99fd9f0ee1f2787541Eric Laurent    if (offloadInfo.has_video)
150141b150b659d4ba33a9be9f99fd9f0ee1f2787541Eric Laurent    {
150241b150b659d4ba33a9be9f99fd9f0ee1f2787541Eric Laurent        ALOGV("isOffloadSupported: has_video == true, returning false");
1503a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        return false;
1504a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    }
1505a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald
1506a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    //If duration is less than minimum value defined in property, return false
1507a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    if (property_get("audio.offload.min.duration.secs", propValue, NULL)) {
1508a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        if (offloadInfo.duration_us < (atoi(propValue) * 1000000 )) {
1509a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald            ALOGV("Offload denied by duration < audio.offload.min.duration.secs(=%s)", propValue);
1510a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald            return false;
1511a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        }
1512a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    } else if (offloadInfo.duration_us < OFFLOAD_DEFAULT_MIN_DURATION_SECS * 1000000) {
1513a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        ALOGV("Offload denied by duration < default min(=%u)", OFFLOAD_DEFAULT_MIN_DURATION_SECS);
1514a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        return false;
1515a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    }
1516a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald
1517000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    // Do not allow offloading if one non offloadable effect is enabled. This prevents from
1518000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    // creating an offloaded track and tearing it down immediately after start when audioflinger
1519000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    // detects there is an active non offloadable effect.
1520000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    // FIXME: We should check the audio session here but we do not have it in this context.
1521000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    // This may prevent offloading in rare situations where effects are left active by apps
1522000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    // in the background.
1523000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    if (isNonOffloadableEffectEnabled()) {
1524000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent        return false;
1525000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent    }
1526000bb51ca507645f3aa4ccfcbbb8859e8d539629Eric Laurent
1527a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    // See if there is a profile to support this.
1528a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    // AUDIO_DEVICE_NONE
1529a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    IOProfile *profile = getProfileForDirectOutput(AUDIO_DEVICE_NONE /*ignore device */,
1530a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                                            offloadInfo.sample_rate,
1531a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                                            offloadInfo.format,
1532a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                                            offloadInfo.channel_mask,
1533a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                                            AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD);
1534a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    ALOGV("isOffloadSupported() profile %sfound", profile != NULL ? "" : "NOT ");
1535a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    return (profile != NULL);
1536b4d07b97d23cfaffe22c7859ad7c45e168a7df0eRichard Fitzgerald}
1537b4d07b97d23cfaffe22c7859ad7c45e168a7df0eRichard Fitzgerald
1538f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// ----------------------------------------------------------------------------
1539f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// AudioPolicyManagerBase
1540f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// ----------------------------------------------------------------------------
1541f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1542f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima ZavinAudioPolicyManagerBase::AudioPolicyManagerBase(AudioPolicyClientInterface *clientInterface)
1543f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    :
1544f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
1545f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    Thread(false),
1546f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
154770c236c9290732782d5267935af1475b8d5ae602Eric Laurent    mPrimaryOutput((audio_io_handle_t)0),
1548ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    mAvailableOutputDevices(AUDIO_DEVICE_NONE),
1549ca0657a1ca087a6d474a75fcfedd6aac3901d587Glenn Kasten    mPhoneState(AudioSystem::MODE_NORMAL),
1550f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mLimitRingtoneVolume(false), mLastVoiceVolume(-1.0f),
1551f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mTotalEffectsCpuLoad(0), mTotalEffectsMemory(0),
155218fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi    mA2dpSuspended(false), mHasA2dp(false), mHasUsb(false), mHasRemoteSubmix(false),
155318fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi    mSpeakerDrcEnabled(false)
1554f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1555f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mpClientInterface = clientInterface;
1556f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1557f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < AudioSystem::NUM_FORCE_USE; i++) {
1558f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mForceUse[i] = AudioSystem::FORCE_NONE;
1559f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1560f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1561f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mA2dpDeviceAddress = String8("");
1562f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mScoDeviceAddress = String8("");
1563fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    mUsbOutCardAndDevice = String8("");
1564f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
15655ec145df7708564d385fd3fb764085321cf4c253Dima Zavin    if (loadAudioPolicyConfig(AUDIO_POLICY_VENDOR_CONFIG_FILE) != NO_ERROR) {
15665ec145df7708564d385fd3fb764085321cf4c253Dima Zavin        if (loadAudioPolicyConfig(AUDIO_POLICY_CONFIG_FILE) != NO_ERROR) {
1567739022f26a7127ba76a98dda65411496086114a7Dima Zavin            ALOGE("could not load audio policy configuration file, setting defaults");
1568739022f26a7127ba76a98dda65411496086114a7Dima Zavin            defaultAudioPolicyConfig();
15695ec145df7708564d385fd3fb764085321cf4c253Dima Zavin        }
15705ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
15715ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
157218fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi    // must be done after reading the policy
157318fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi    initializeVolumeCurves();
157418fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi
1575b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    // open all output streams needed to access attached devices
157670c236c9290732782d5267935af1475b8d5ae602Eric Laurent    for (size_t i = 0; i < mHwModules.size(); i++) {
157770c236c9290732782d5267935af1475b8d5ae602Eric Laurent        mHwModules[i]->mHandle = mpClientInterface->loadHwModule(mHwModules[i]->mName);
157870c236c9290732782d5267935af1475b8d5ae602Eric Laurent        if (mHwModules[i]->mHandle == 0) {
157970c236c9290732782d5267935af1475b8d5ae602Eric Laurent            ALOGW("could not open HW module %s", mHwModules[i]->mName);
158070c236c9290732782d5267935af1475b8d5ae602Eric Laurent            continue;
158170c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
158270c236c9290732782d5267935af1475b8d5ae602Eric Laurent        // open all output streams needed to access attached devices
1583a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        // except for direct output streams that are only opened when they are actually
1584a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        // required by an app.
158570c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
158670c236c9290732782d5267935af1475b8d5ae602Eric Laurent        {
158770c236c9290732782d5267935af1475b8d5ae602Eric Laurent            const IOProfile *outProfile = mHwModules[i]->mOutputProfiles[j];
158870c236c9290732782d5267935af1475b8d5ae602Eric Laurent
1589a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald            if ((outProfile->mSupportedDevices & mAttachedOutputDevices) &&
1590a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald                    ((outProfile->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0)) {
159170c236c9290732782d5267935af1475b8d5ae602Eric Laurent                AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(outProfile);
159270c236c9290732782d5267935af1475b8d5ae602Eric Laurent                outputDesc->mDevice = (audio_devices_t)(mDefaultOutputDevice &
159370c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                            outProfile->mSupportedDevices);
159470c236c9290732782d5267935af1475b8d5ae602Eric Laurent                audio_io_handle_t output = mpClientInterface->openOutput(
159570c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                outProfile->mModule->mHandle,
159670c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mDevice,
159770c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mSamplingRate,
159870c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mFormat,
159970c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mChannelMask,
160070c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mLatency,
160170c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                outputDesc->mFlags);
160270c236c9290732782d5267935af1475b8d5ae602Eric Laurent                if (output == 0) {
160370c236c9290732782d5267935af1475b8d5ae602Eric Laurent                    delete outputDesc;
160470c236c9290732782d5267935af1475b8d5ae602Eric Laurent                } else {
160570c236c9290732782d5267935af1475b8d5ae602Eric Laurent                    mAvailableOutputDevices = (audio_devices_t)(mAvailableOutputDevices |
160670c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                            (outProfile->mSupportedDevices & mAttachedOutputDevices));
160770c236c9290732782d5267935af1475b8d5ae602Eric Laurent                    if (mPrimaryOutput == 0 &&
16080977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent                            outProfile->mFlags & AUDIO_OUTPUT_FLAG_PRIMARY) {
160970c236c9290732782d5267935af1475b8d5ae602Eric Laurent                        mPrimaryOutput = output;
161070c236c9290732782d5267935af1475b8d5ae602Eric Laurent                    }
161170c236c9290732782d5267935af1475b8d5ae602Eric Laurent                    addOutput(output, outputDesc);
161270c236c9290732782d5267935af1475b8d5ae602Eric Laurent                    setOutputDevice(output,
161370c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                    (audio_devices_t)(mDefaultOutputDevice &
161470c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                        outProfile->mSupportedDevices),
161570c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                    true);
1616b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                }
1617b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            }
1618b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        }
1619f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1620f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
16215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGE_IF((mAttachedOutputDevices & ~mAvailableOutputDevices),
1622b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent             "Not output found for attached devices %08x",
16235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent             (mAttachedOutputDevices & ~mAvailableOutputDevices));
1624b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1625b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    ALOGE_IF((mPrimaryOutput == 0), "Failed to open primary output");
1626b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1627c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    updateDevicesAndOutputs();
16283cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
1629f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
1630b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (mPrimaryOutput != 0) {
1631f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioParameter outputCmd = AudioParameter();
1632f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputCmd.addInt(String8("set_id"), 0);
1633b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        mpClientInterface->setParameters(mPrimaryOutput, outputCmd.toString());
1634f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1635c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        mTestDevice = AUDIO_DEVICE_OUT_SPEAKER;
1636f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mTestSamplingRate = 44100;
1637f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mTestFormat = AudioSystem::PCM_16_BIT;
1638f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mTestChannels =  AudioSystem::CHANNEL_OUT_STEREO;
1639f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mTestLatencyMs = 0;
1640f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mCurOutput = 0;
1641f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mDirectOutput = false;
1642f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
1643f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mTestOutputs[i] = 0;
1644f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1645f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1646f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        const size_t SIZE = 256;
1647f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        char buffer[SIZE];
1648f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        snprintf(buffer, SIZE, "AudioPolicyManagerTest");
1649f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        run(buffer, ANDROID_PRIORITY_AUDIO);
1650f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1651f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
1652f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1653f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1654f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima ZavinAudioPolicyManagerBase::~AudioPolicyManagerBase()
1655f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1656f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
1657f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    exit();
1658f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
1659f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin   for (size_t i = 0; i < mOutputs.size(); i++) {
1660f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mpClientInterface->closeOutput(mOutputs.keyAt(i));
1661f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        delete mOutputs.valueAt(i);
1662f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin   }
1663f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin   for (size_t i = 0; i < mInputs.size(); i++) {
1664f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mpClientInterface->closeInput(mInputs.keyAt(i));
1665f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        delete mInputs.valueAt(i);
1666f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin   }
166770c236c9290732782d5267935af1475b8d5ae602Eric Laurent   for (size_t i = 0; i < mHwModules.size(); i++) {
166870c236c9290732782d5267935af1475b8d5ae602Eric Laurent        delete mHwModules[i];
16695ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent   }
1670f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1671f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1672f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::initCheck()
1673f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1674b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    return (mPrimaryOutput == 0) ? NO_INIT : NO_ERROR;
1675f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1676f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1677f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#ifdef AUDIO_POLICY_TEST
1678f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinbool AudioPolicyManagerBase::threadLoop()
1679f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
16806a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("entering threadLoop()");
1681f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    while (!exitPending())
1682f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    {
1683f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        String8 command;
1684f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        int valueInt;
1685f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        String8 value;
1686f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1687f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        Mutex::Autolock _l(mLock);
1688f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mWaitWorkCV.waitRelative(mLock, milliseconds(50));
1689f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1690f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        command = mpClientInterface->getParameters(0, String8("test_cmd_policy"));
1691f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioParameter param = AudioParameter(command);
1692f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1693f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (param.getInt(String8("test_cmd_policy"), valueInt) == NO_ERROR &&
1694f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            valueInt != 0) {
16956a70518b93928d1c91457ff805e375c82d76b0e5Steve Block            ALOGV("Test command %s received", command.string());
1696f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            String8 target;
1697f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.get(String8("target"), target) != NO_ERROR) {
1698f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                target = "Manager";
1699f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1700f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.getInt(String8("test_cmd_policy_output"), valueInt) == NO_ERROR) {
1701f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_output"));
1702f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mCurOutput = valueInt;
1703f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1704f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.get(String8("test_cmd_policy_direct"), value) == NO_ERROR) {
1705f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_direct"));
1706f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (value == "false") {
1707f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    mDirectOutput = false;
1708f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else if (value == "true") {
1709f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    mDirectOutput = true;
1710f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1711f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1712f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.getInt(String8("test_cmd_policy_input"), valueInt) == NO_ERROR) {
1713f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_input"));
1714f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                mTestInput = valueInt;
1715f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1716f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1717f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.get(String8("test_cmd_policy_format"), value) == NO_ERROR) {
1718f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_format"));
1719f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                int format = AudioSystem::INVALID_FORMAT;
1720f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (value == "PCM 16 bits") {
1721f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    format = AudioSystem::PCM_16_BIT;
1722f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else if (value == "PCM 8 bits") {
1723f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    format = AudioSystem::PCM_8_BIT;
1724f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else if (value == "Compressed MP3") {
1725f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    format = AudioSystem::MP3;
1726f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1727f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (format != AudioSystem::INVALID_FORMAT) {
1728f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    if (target == "Manager") {
1729f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mTestFormat = format;
1730f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    } else if (mTestOutputs[mCurOutput] != 0) {
1731f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        AudioParameter outputParam = AudioParameter();
1732f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        outputParam.addInt(String8("format"), format);
1733f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
1734f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    }
1735f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1736f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1737f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.get(String8("test_cmd_policy_channels"), value) == NO_ERROR) {
1738f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_channels"));
1739f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                int channels = 0;
1740f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1741f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (value == "Channels Stereo") {
1742f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    channels =  AudioSystem::CHANNEL_OUT_STEREO;
1743f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else if (value == "Channels Mono") {
1744f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    channels =  AudioSystem::CHANNEL_OUT_MONO;
1745f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1746f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (channels != 0) {
1747f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    if (target == "Manager") {
1748f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mTestChannels = channels;
1749f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    } else if (mTestOutputs[mCurOutput] != 0) {
1750f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        AudioParameter outputParam = AudioParameter();
1751f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        outputParam.addInt(String8("channels"), channels);
1752f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
1753f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    }
1754f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1755f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1756f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.getInt(String8("test_cmd_policy_sampleRate"), valueInt) == NO_ERROR) {
1757f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_sampleRate"));
1758f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (valueInt >= 0 && valueInt <= 96000) {
1759f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    int samplingRate = valueInt;
1760f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    if (target == "Manager") {
1761f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mTestSamplingRate = samplingRate;
1762f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    } else if (mTestOutputs[mCurOutput] != 0) {
1763f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        AudioParameter outputParam = AudioParameter();
1764f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        outputParam.addInt(String8("sampling_rate"), samplingRate);
1765f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                        mpClientInterface->setParameters(mTestOutputs[mCurOutput], outputParam.toString());
1766f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    }
1767f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1768f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1769f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1770f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (param.get(String8("test_cmd_policy_reopen"), value) == NO_ERROR) {
1771f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                param.remove(String8("test_cmd_policy_reopen"));
1772f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
177370c236c9290732782d5267935af1475b8d5ae602Eric Laurent                AudioOutputDescriptor *outputDesc = mOutputs.valueFor(mPrimaryOutput);
1774b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                mpClientInterface->closeOutput(mPrimaryOutput);
177570c236c9290732782d5267935af1475b8d5ae602Eric Laurent
177670c236c9290732782d5267935af1475b8d5ae602Eric Laurent                audio_module_handle_t moduleHandle = outputDesc->mModule->mHandle;
177770c236c9290732782d5267935af1475b8d5ae602Eric Laurent
1778b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                delete mOutputs.valueFor(mPrimaryOutput);
1779b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                mOutputs.removeItem(mPrimaryOutput);
1780f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1781b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                AudioOutputDescriptor *outputDesc = new AudioOutputDescriptor(NULL);
1782c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                outputDesc->mDevice = AUDIO_DEVICE_OUT_SPEAKER;
178370c236c9290732782d5267935af1475b8d5ae602Eric Laurent                mPrimaryOutput = mpClientInterface->openOutput(moduleHandle,
178470c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mDevice,
1785f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                &outputDesc->mSamplingRate,
1786f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                &outputDesc->mFormat,
178770c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                &outputDesc->mChannelMask,
1788f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                &outputDesc->mLatency,
1789f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                                                outputDesc->mFlags);
1790b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                if (mPrimaryOutput == 0) {
17915efbd421e0029d6fc44b1cc65c0e5e0d85e5161fSteve Block                    ALOGE("Failed to reopen hardware output stream, samplingRate: %d, format %d, channels %d",
179270c236c9290732782d5267935af1475b8d5ae602Eric Laurent                            outputDesc->mSamplingRate, outputDesc->mFormat, outputDesc->mChannelMask);
1793f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else {
1794f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    AudioParameter outputCmd = AudioParameter();
1795f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    outputCmd.addInt(String8("set_id"), 0);
1796b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                    mpClientInterface->setParameters(mPrimaryOutput, outputCmd.toString());
1797b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                    addOutput(mPrimaryOutput, outputDesc);
1798f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
1799f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
1800f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1801f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1802f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mpClientInterface->setParameters(0, String8("test_cmd_policy="));
1803f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
1804f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1805f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return false;
1806f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1807f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1808f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::exit()
1809f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1810f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    {
1811f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AutoMutex _l(mLock);
1812f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        requestExit();
1813f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mWaitWorkCV.signal();
1814f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1815f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    requestExitAndWait();
1816f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1817f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1818f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinint AudioPolicyManagerBase::testOutputIndex(audio_io_handle_t output)
1819f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1820f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < NUM_TEST_OUTPUTS; i++) {
1821f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (output == mTestOutputs[i]) return i;
1822f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
1823f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return 0;
1824f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1825f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin#endif //AUDIO_POLICY_TEST
1826f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1827f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// ---
1828f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1829f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::addOutput(audio_io_handle_t id, AudioOutputDescriptor *outputDesc)
1830f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
1831f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    outputDesc->mId = id;
1832f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mOutputs.add(id, outputDesc);
1833f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
1834f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
1835fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLeanvoid AudioPolicyManagerBase::addInput(audio_io_handle_t id, AudioInputDescriptor *inputDesc)
1836fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean{
1837fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    inputDesc->mId = id;
1838fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    mInputs.add(id, inputDesc);
1839fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean}
1840f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
18413cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurentstatus_t AudioPolicyManagerBase::checkOutputsForDevice(audio_devices_t device,
18423cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                       AudioSystem::device_connection_state state,
18439f4165f36d90dddc32246d0eddbf289383c3b2bbEric Laurent                                                       SortedVector<audio_io_handle_t>& outputs,
18449f4165f36d90dddc32246d0eddbf289383c3b2bbEric Laurent                                                       const String8 paramStr)
1845f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
18463cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    AudioOutputDescriptor *desc;
1847b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
1848b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (state == AudioSystem::DEVICE_STATE_AVAILABLE) {
18493cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        // first list already open outputs that can be routed to this device
1850b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        for (size_t i = 0; i < mOutputs.size(); i++) {
18513cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            desc = mOutputs.valueAt(i);
18523cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            if (!desc->isDuplicated() && (desc->mProfile->mSupportedDevices & device)) {
18533cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                ALOGV("checkOutputsForDevice(): adding opened output %d", mOutputs.keyAt(i));
18543cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                outputs.add(mOutputs.keyAt(i));
1855b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            }
1856b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        }
18573cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        // then look for output profiles that can be routed to this device
18583cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        SortedVector<IOProfile *> profiles;
185970c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (size_t i = 0; i < mHwModules.size(); i++)
1860b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        {
186170c236c9290732782d5267935af1475b8d5ae602Eric Laurent            if (mHwModules[i]->mHandle == 0) {
186270c236c9290732782d5267935af1475b8d5ae602Eric Laurent                continue;
186370c236c9290732782d5267935af1475b8d5ae602Eric Laurent            }
186470c236c9290732782d5267935af1475b8d5ae602Eric Laurent            for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
186570c236c9290732782d5267935af1475b8d5ae602Eric Laurent            {
186670c236c9290732782d5267935af1475b8d5ae602Eric Laurent                if (mHwModules[i]->mOutputProfiles[j]->mSupportedDevices & device) {
1867c7b6e3c857c5f5af5f244d0b877c41a9c55f92abColin Cross                    ALOGV("checkOutputsForDevice(): adding profile %zu from module %zu", j, i);
18683cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    profiles.add(mHwModules[i]->mOutputProfiles[j]);
186970c236c9290732782d5267935af1475b8d5ae602Eric Laurent                }
187070c236c9290732782d5267935af1475b8d5ae602Eric Laurent            }
1871b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        }
187270c236c9290732782d5267935af1475b8d5ae602Eric Laurent
18733cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        if (profiles.isEmpty() && outputs.isEmpty()) {
18743cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
18753cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            return BAD_VALUE;
187670c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
187770c236c9290732782d5267935af1475b8d5ae602Eric Laurent
18783cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        // open outputs for matching profiles if needed. Direct outputs are also opened to
18793cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        // query for dynamic parameters and will be closed later by setDeviceConnectionState()
18803cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
18813cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            IOProfile *profile = profiles[profile_index];
1882b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
18833cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            // nothing to do if one output is already opened for this profile
18843cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            size_t j;
1885fd8cecbee6843b444d56a1db40af76027e2b19f1Eric Laurent            for (j = 0; j < mOutputs.size(); j++) {
18863cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                desc = mOutputs.valueAt(j);
18873cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                if (!desc->isDuplicated() && desc->mProfile == profile) {
18883cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    break;
18893cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                }
18903cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
1891fd8cecbee6843b444d56a1db40af76027e2b19f1Eric Laurent            if (j != mOutputs.size()) {
18923cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                continue;
18933cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
18943cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
18959f4165f36d90dddc32246d0eddbf289383c3b2bbEric Laurent            ALOGV("opening output for device %08x with params %s", device, paramStr.string());
18963cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            desc = new AudioOutputDescriptor(profile);
18973cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            desc->mDevice = device;
1898727fef1b1a8cb6d6609ec99a749fcd9573a3417dEric Laurent            audio_offload_info_t offloadInfo = AUDIO_INFO_INITIALIZER;
1899727fef1b1a8cb6d6609ec99a749fcd9573a3417dEric Laurent            offloadInfo.sample_rate = desc->mSamplingRate;
1900727fef1b1a8cb6d6609ec99a749fcd9573a3417dEric Laurent            offloadInfo.format = desc->mFormat;
1901727fef1b1a8cb6d6609ec99a749fcd9573a3417dEric Laurent            offloadInfo.channel_mask = desc->mChannelMask;
1902727fef1b1a8cb6d6609ec99a749fcd9573a3417dEric Laurent
19033cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            audio_io_handle_t output = mpClientInterface->openOutput(profile->mModule->mHandle,
19043cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                       &desc->mDevice,
19053cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                       &desc->mSamplingRate,
19063cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                       &desc->mFormat,
19073cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                       &desc->mChannelMask,
19083cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                                       &desc->mLatency,
1909727fef1b1a8cb6d6609ec99a749fcd9573a3417dEric Laurent                                                                       desc->mFlags,
1910727fef1b1a8cb6d6609ec99a749fcd9573a3417dEric Laurent                                                                       &offloadInfo);
19113cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            if (output != 0) {
19129f4165f36d90dddc32246d0eddbf289383c3b2bbEric Laurent                if (!paramStr.isEmpty()) {
1913fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    // Here is where the out_set_parameters() for card & device gets called
19149f4165f36d90dddc32246d0eddbf289383c3b2bbEric Laurent                    mpClientInterface->setParameters(output, paramStr);
19159f4165f36d90dddc32246d0eddbf289383c3b2bbEric Laurent                }
19169f4165f36d90dddc32246d0eddbf289383c3b2bbEric Laurent
1917fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                // Here is where we step through and resolve any "dynamic" fields
1918c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                String8 reply;
1919c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                char *value;
1920c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                if (profile->mSamplingRates[0] == 0) {
1921c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                    reply = mpClientInterface->getParameters(output,
1922c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                                            String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES));
1923c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                    ALOGV("checkOutputsForDevice() direct output sup sampling rates %s",
1924c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                              reply.string());
1925c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                    value = strpbrk((char *)reply.string(), "=");
1926c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                    if (value != NULL) {
1927c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                        loadSamplingRates(value + 1, profile);
19283cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
1929c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                }
1930c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
1931c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                    reply = mpClientInterface->getParameters(output,
1932c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                                                   String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
1933c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                    ALOGV("checkOutputsForDevice() direct output sup formats %s",
1934c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                              reply.string());
1935c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                    value = strpbrk((char *)reply.string(), "=");
1936c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                    if (value != NULL) {
1937c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                        loadFormats(value + 1, profile);
19383cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
1939c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                }
1940c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                if (profile->mChannelMasks[0] == 0) {
1941c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                    reply = mpClientInterface->getParameters(output,
1942c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                                                  String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS));
1943c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                    ALOGV("checkOutputsForDevice() direct output sup channel masks %s",
1944c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                              reply.string());
1945c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                    value = strpbrk((char *)reply.string(), "=");
1946c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                    if (value != NULL) {
1947c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                        loadOutChannels(value + 1, profile);
19483cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
1949c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                }
1950c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                if (((profile->mSamplingRates[0] == 0) &&
1951c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                         (profile->mSamplingRates.size() < 2)) ||
1952c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                     ((profile->mFormats[0] == 0) &&
1953c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                         (profile->mFormats.size() < 2)) ||
1954c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                     ((profile->mChannelMasks[0] == 0) &&
1955c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                         (profile->mChannelMasks.size() < 2))) {
1956c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                    ALOGW("checkOutputsForDevice() direct output missing param");
1957c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                    mpClientInterface->closeOutput(output);
1958c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                    output = 0;
1959c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                } else if (profile->mSamplingRates[0] == 0) {
1960c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                    mpClientInterface->closeOutput(output);
1961c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                    desc->mSamplingRate = profile->mSamplingRates[1];
1962c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                    offloadInfo.sample_rate = desc->mSamplingRate;
1963c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                    output = mpClientInterface->openOutput(
1964c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                                                    profile->mModule->mHandle,
1965c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                                                    &desc->mDevice,
1966c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                                                    &desc->mSamplingRate,
1967c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                                                    &desc->mFormat,
1968c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                                                    &desc->mChannelMask,
1969c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                                                    &desc->mLatency,
1970c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                                                    desc->mFlags,
1971c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                                                    &offloadInfo);
1972c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                }
1973c26f454f8d841657542916cdd140a9896a89ad20Paul McLean
1974c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                if (output != 0) {
19753cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    addOutput(output, desc);
1976c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                    if ((desc->mFlags & AUDIO_OUTPUT_FLAG_DIRECT) == 0) {
1977c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                        audio_io_handle_t duplicatedOutput = 0;
1978c26f454f8d841657542916cdd140a9896a89ad20Paul McLean
1979c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                        // set initial stream volume for device
1980c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                        applyStreamVolumes(output, device, 0, true);
1981c26f454f8d841657542916cdd140a9896a89ad20Paul McLean
1982c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                        //TODO: configure audio effect output stage here
1983c26f454f8d841657542916cdd140a9896a89ad20Paul McLean
1984c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                        // open a duplicating output thread for the new output and the primary output
1985c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                        duplicatedOutput = mpClientInterface->openDuplicateOutput(output,
1986c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                                                                                  mPrimaryOutput);
1987c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                        if (duplicatedOutput != 0) {
1988c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                            // add duplicated output descriptor
1989c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                            AudioOutputDescriptor *dupOutputDesc = new AudioOutputDescriptor(NULL);
1990c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                            dupOutputDesc->mOutput1 = mOutputs.valueFor(mPrimaryOutput);
1991c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                            dupOutputDesc->mOutput2 = mOutputs.valueFor(output);
1992c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                            dupOutputDesc->mSamplingRate = desc->mSamplingRate;
1993c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                            dupOutputDesc->mFormat = desc->mFormat;
1994c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                            dupOutputDesc->mChannelMask = desc->mChannelMask;
1995c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                            dupOutputDesc->mLatency = desc->mLatency;
1996c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                            addOutput(duplicatedOutput, dupOutputDesc);
1997c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                            applyStreamVolumes(duplicatedOutput, device, 0, true);
1998c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                        } else {
1999c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                            ALOGW("checkOutputsForDevice() could not open dup output for %d and %d",
2000c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                                    mPrimaryOutput, output);
2001c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                            mpClientInterface->closeOutput(output);
2002c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                            mOutputs.removeItem(output);
2003c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                            output = 0;
2004c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                        }
20053cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
20063cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                }
20073cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
20083cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            if (output == 0) {
20093cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                ALOGW("checkOutputsForDevice() could not open output for device %x", device);
20103cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                delete desc;
20113cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                profiles.removeAt(profile_index);
20123cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                profile_index--;
2013b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            } else {
20143cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                outputs.add(output);
20153cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                ALOGV("checkOutputsForDevice(): adding output %d", output);
2016f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
20173cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        }
20183cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
20193cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        if (profiles.isEmpty()) {
20203cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            ALOGW("checkOutputsForDevice(): No output available for device %04x", device);
20213cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            return BAD_VALUE;
2022f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2023fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    } else { // Disconnect
2024b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        // check if one opened output is not needed any more after disconnecting one device
2025b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        for (size_t i = 0; i < mOutputs.size(); i++) {
20263cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            desc = mOutputs.valueAt(i);
20273cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            if (!desc->isDuplicated() &&
20283cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    !(desc->mProfile->mSupportedDevices & mAvailableOutputDevices)) {
20293cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                ALOGV("checkOutputsForDevice(): disconnecting adding output %d", mOutputs.keyAt(i));
20303cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                outputs.add(mOutputs.keyAt(i));
20313cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
20323cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        }
2033fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        // Clear any profiles associated with the disconnected device.
20343cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        for (size_t i = 0; i < mHwModules.size(); i++)
20353cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        {
20363cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            if (mHwModules[i]->mHandle == 0) {
20373cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                continue;
20383cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            }
20393cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            for (size_t j = 0; j < mHwModules[i]->mOutputProfiles.size(); j++)
20403cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            {
20413cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                IOProfile *profile = mHwModules[i]->mOutputProfiles[j];
2042c26f454f8d841657542916cdd140a9896a89ad20Paul McLean                if (profile->mSupportedDevices & device) {
2043c7b6e3c857c5f5af5f244d0b877c41a9c55f92abColin Cross                    ALOGV("checkOutputsForDevice(): clearing direct output profile %zu on module %zu",
20443cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                          j, i);
20453cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    if (profile->mSamplingRates[0] == 0) {
20463cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        profile->mSamplingRates.clear();
20473cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        profile->mSamplingRates.add(0);
20483cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
20492d749b0bda4ae60ef1a940eeba86121b2e94d07dGlenn Kasten                    if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
20503cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        profile->mFormats.clear();
20512d749b0bda4ae60ef1a940eeba86121b2e94d07dGlenn Kasten                        profile->mFormats.add(AUDIO_FORMAT_DEFAULT);
20523cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
20533cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    if (profile->mChannelMasks[0] == 0) {
20543cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                        profile->mChannelMasks.clear();
20552c3d2379ee6eadb32b63bf6de538cfd6487e8e20Glenn Kasten                        profile->mChannelMasks.add(0);
20563cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                    }
20573cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                }
2058b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            }
2059f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2060f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
20613cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    return NO_ERROR;
2062f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2063f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2064fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLeanstatus_t AudioPolicyManagerBase::checkInputsForDevice(audio_devices_t device,
2065fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                                                      AudioSystem::device_connection_state state,
2066fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                                                      SortedVector<audio_io_handle_t>& inputs,
2067fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                                                      const String8 paramStr)
2068fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean{
2069fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    AudioInputDescriptor *desc;
2070fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    if (state == AudioSystem::DEVICE_STATE_AVAILABLE) {
2071fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        // first list already open inputs that can be routed to this device
2072fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
2073fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            desc = mInputs.valueAt(input_index);
2074fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            if (desc->mProfile->mSupportedDevices & (device & ~AUDIO_DEVICE_BIT_IN)) {
2075fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                ALOGV("checkInputsForDevice(): adding opened input %d", mInputs.keyAt(input_index));
2076fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean               inputs.add(mInputs.keyAt(input_index));
2077fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            }
2078fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        }
2079fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
2080fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        // then look for input profiles that can be routed to this device
2081fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        SortedVector<IOProfile *> profiles;
2082fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        for (size_t module_index = 0; module_index < mHwModules.size(); module_index++)
2083fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        {
2084fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            if (mHwModules[module_index]->mHandle == 0) {
2085fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                continue;
2086fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            }
2087fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            for (size_t profile_index = 0;
2088fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                 profile_index < mHwModules[module_index]->mInputProfiles.size();
2089fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                 profile_index++)
2090fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            {
2091fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                if (mHwModules[module_index]->mInputProfiles[profile_index]->mSupportedDevices
2092fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                        & (device & ~AUDIO_DEVICE_BIT_IN)) {
2093fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    ALOGV("checkInputsForDevice(): adding profile %d from module %d",
2094fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                          profile_index, module_index);
2095fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    profiles.add(mHwModules[module_index]->mInputProfiles[profile_index]);
2096fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                }
2097fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            }
2098fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        }
2099fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
2100fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        if (profiles.isEmpty() && inputs.isEmpty()) {
2101fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
2102fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            return BAD_VALUE;
2103fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        }
2104fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
2105fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        // open inputs for matching profiles if needed. Direct inputs are also opened to
2106fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        // query for dynamic parameters and will be closed later by setDeviceConnectionState()
2107fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        for (ssize_t profile_index = 0; profile_index < (ssize_t)profiles.size(); profile_index++) {
2108fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
2109fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            IOProfile *profile = profiles[profile_index];
2110fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            // nothing to do if one input is already opened for this profile
2111fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            size_t input_index;
2112fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            for (input_index = 0; input_index < mInputs.size(); input_index++) {
2113fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                desc = mInputs.valueAt(input_index);
2114fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                if (desc->mProfile == profile) {
2115fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    break;
2116fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                }
2117fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            }
2118fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            if (input_index != mInputs.size()) {
2119fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                continue;
2120fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            }
2121fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
2122fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            ALOGV("opening input for device 0x%X with params %s", device, paramStr.string());
2123fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            desc = new AudioInputDescriptor(profile);
2124fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            desc->mDevice = device;
2125fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
2126fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            audio_io_handle_t input = mpClientInterface->openInput(profile->mModule->mHandle,
2127fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                                            &desc->mDevice,
2128fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                                            &desc->mSamplingRate,
2129fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                                            &desc->mFormat,
2130fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                                            &desc->mChannelMask);
2131fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
2132fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            if (input != 0) {
2133fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                if (!paramStr.isEmpty()) {
2134fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    mpClientInterface->setParameters(input, paramStr);
2135fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                }
2136fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
2137fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                // Here is where we step through and resolve any "dynamic" fields
2138fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                String8 reply;
2139fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                char *value;
2140fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                if (profile->mSamplingRates[0] == 0) {
2141fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    reply = mpClientInterface->getParameters(input,
2142fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                                            String8(AUDIO_PARAMETER_STREAM_SUP_SAMPLING_RATES));
2143fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    ALOGV("checkInputsForDevice() direct input sup sampling rates %s",
2144fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                              reply.string());
2145fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    value = strpbrk((char *)reply.string(), "=");
2146fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    if (value != NULL) {
2147fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                        loadSamplingRates(value + 1, profile);
2148fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    }
2149fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                }
2150fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
2151fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    reply = mpClientInterface->getParameters(input,
2152fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                                                   String8(AUDIO_PARAMETER_STREAM_SUP_FORMATS));
2153fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    ALOGV("checkInputsForDevice() direct input sup formats %s", reply.string());
2154fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    value = strpbrk((char *)reply.string(), "=");
2155fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    if (value != NULL) {
2156fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                        loadFormats(value + 1, profile);
2157fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    }
2158fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                }
2159fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                if (profile->mChannelMasks[0] == 0) {
2160fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    reply = mpClientInterface->getParameters(input,
2161fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                                                  String8(AUDIO_PARAMETER_STREAM_SUP_CHANNELS));
2162fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    ALOGV("checkInputsForDevice() direct input sup channel masks %s",
2163fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                              reply.string());
2164fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    value = strpbrk((char *)reply.string(), "=");
2165fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    if (value != NULL) {
2166fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                        loadInChannels(value + 1, profile);
2167fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    }
2168fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                }
2169fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                if (((profile->mSamplingRates[0] == 0) && (profile->mSamplingRates.size() < 2)) ||
2170fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                     ((profile->mFormats[0] == 0) && (profile->mFormats.size() < 2)) ||
2171fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                     ((profile->mChannelMasks[0] == 0) && (profile->mChannelMasks.size() < 2))) {
2172fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    ALOGW("checkInputsForDevice() direct input missing param");
2173fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    mpClientInterface->closeInput(input);
2174fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    input = 0;
2175fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                }
2176fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
2177fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                if (input != 0) {
2178fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    addInput(input, desc);
2179fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                }
2180fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            } // endif input != 0
2181fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
2182fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            if (input == 0) {
2183fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                ALOGW("checkInputsForDevice() could not open input for device 0x%X", device);
2184fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                delete desc;
2185fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                profiles.removeAt(profile_index);
2186fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                profile_index--;
2187fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            } else {
2188fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                inputs.add(input);
2189fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                ALOGV("checkInputsForDevice(): adding input %d", input);
2190fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            }
2191fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        } // end scan profiles
2192fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
2193fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        if (profiles.isEmpty()) {
2194fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            ALOGW("checkInputsForDevice(): No input available for device 0x%X", device);
2195fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            return BAD_VALUE;
2196fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        }
2197fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    } else {
2198fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        // Disconnect
2199fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        // check if one opened input is not needed any more after disconnecting one device
2200fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        for (size_t input_index = 0; input_index < mInputs.size(); input_index++) {
2201fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            desc = mInputs.valueAt(input_index);
2202fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            if (!(desc->mProfile->mSupportedDevices & mAvailableInputDevices)) {
2203fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                ALOGV("checkInputsForDevice(): disconnecting adding input %d",
2204fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                      mInputs.keyAt(input_index));
2205fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                inputs.add(mInputs.keyAt(input_index));
2206fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            }
2207fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        }
2208fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        // Clear any profiles associated with the disconnected device.
2209fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        for (size_t module_index = 0; module_index < mHwModules.size(); module_index++)
2210fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        {
2211fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            if (mHwModules[module_index]->mHandle == 0) {
2212fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                continue;
2213fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            }
2214fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            for (size_t profile_index = 0;
2215fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                 profile_index < mHwModules[module_index]->mInputProfiles.size();
2216fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                 profile_index++)
2217fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            {
2218fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                IOProfile *profile = mHwModules[module_index]->mInputProfiles[profile_index];
2219fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                if (profile->mSupportedDevices & device) {
2220fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    ALOGV("checkInputsForDevice(): clearing direct input profile %d on module %d",
2221fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                          profile_index, module_index);
2222fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    if (profile->mSamplingRates[0] == 0) {
2223fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                        profile->mSamplingRates.clear();
2224fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                        profile->mSamplingRates.add(0);
2225fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    }
2226fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    if (profile->mFormats[0] == AUDIO_FORMAT_DEFAULT) {
2227fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                        profile->mFormats.clear();
2228fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                        profile->mFormats.add(AUDIO_FORMAT_DEFAULT);
2229fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    }
2230fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    if (profile->mChannelMasks[0] == 0) {
2231fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                        profile->mChannelMasks.clear();
2232fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                        profile->mChannelMasks.add(0);
2233fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                    }
2234fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean                }
2235fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            }
2236fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        }
2237fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    } // end disconnect
2238fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
2239fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    return NO_ERROR;
2240fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean}
2241fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
2242b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurentvoid AudioPolicyManagerBase::closeOutput(audio_io_handle_t output)
2243f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2244b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    ALOGV("closeOutput(%d)", output);
2245f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2246b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
2247b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (outputDesc == NULL) {
2248b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        ALOGW("closeOutput() unknown output %d", output);
2249b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        return;
2250f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2251f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2252b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    // look for duplicated outputs connected to the output being removed.
2253b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    for (size_t i = 0; i < mOutputs.size(); i++) {
2254b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        AudioOutputDescriptor *dupOutputDesc = mOutputs.valueAt(i);
2255b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        if (dupOutputDesc->isDuplicated() &&
2256b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                (dupOutputDesc->mOutput1 == outputDesc ||
2257b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                dupOutputDesc->mOutput2 == outputDesc)) {
2258b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            AudioOutputDescriptor *outputDesc2;
2259b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            if (dupOutputDesc->mOutput1 == outputDesc) {
2260b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                outputDesc2 = dupOutputDesc->mOutput2;
2261b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            } else {
2262b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                outputDesc2 = dupOutputDesc->mOutput1;
2263b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            }
2264b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            // As all active tracks on duplicated output will be deleted,
2265b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            // and as they were also referenced on the other output, the reference
2266b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            // count for their stream type must be adjusted accordingly on
2267b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            // the other output.
2268b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            for (int j = 0; j < (int)AudioSystem::NUM_STREAM_TYPES; j++) {
2269b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                int refCount = dupOutputDesc->mRefCount[j];
2270b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                outputDesc2->changeRefCount((AudioSystem::stream_type)j,-refCount);
2271b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            }
2272b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            audio_io_handle_t duplicatedOutput = mOutputs.keyAt(i);
2273b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            ALOGV("closeOutput() closing also duplicated output %d", duplicatedOutput);
2274f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2275b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            mpClientInterface->closeOutput(duplicatedOutput);
2276b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            delete mOutputs.valueFor(duplicatedOutput);
2277b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            mOutputs.removeItem(duplicatedOutput);
2278f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2279f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2280b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
2281b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    AudioParameter param;
2282b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    param.add(String8("closing"), String8("true"));
2283b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    mpClientInterface->setParameters(output, param.toString());
2284b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
2285b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    mpClientInterface->closeOutput(output);
22865a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent    delete outputDesc;
2287b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    mOutputs.removeItem(output);
22885a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent    mPreviousOutputs = mOutputs;
2289f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2290f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2291c952527e6f89d5427881462823514be9d79f13e6Eric LaurentSortedVector<audio_io_handle_t> AudioPolicyManagerBase::getOutputsForDevice(audio_devices_t device,
2292c952527e6f89d5427881462823514be9d79f13e6Eric Laurent                        DefaultKeyedVector<audio_io_handle_t, AudioOutputDescriptor *> openOutputs)
2293f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2294b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    SortedVector<audio_io_handle_t> outputs;
2295f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
22963cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGVV("getOutputsForDevice() device %04x", device);
2297c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    for (size_t i = 0; i < openOutputs.size(); i++) {
22983cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        ALOGVV("output %d isDuplicated=%d device=%04x",
2299c952527e6f89d5427881462823514be9d79f13e6Eric Laurent                i, openOutputs.valueAt(i)->isDuplicated(), openOutputs.valueAt(i)->supportedDevices());
2300c952527e6f89d5427881462823514be9d79f13e6Eric Laurent        if ((device & openOutputs.valueAt(i)->supportedDevices()) == device) {
2301c952527e6f89d5427881462823514be9d79f13e6Eric Laurent            ALOGVV("getOutputsForDevice() found output %d", openOutputs.keyAt(i));
2302c952527e6f89d5427881462823514be9d79f13e6Eric Laurent            outputs.add(openOutputs.keyAt(i));
2303f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2304f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2305b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    return outputs;
2306f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2307f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2308b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurentbool AudioPolicyManagerBase::vectorsEqual(SortedVector<audio_io_handle_t>& outputs1,
2309b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                                   SortedVector<audio_io_handle_t>& outputs2)
2310f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2311b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (outputs1.size() != outputs2.size()) {
2312b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        return false;
2313f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2314b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    for (size_t i = 0; i < outputs1.size(); i++) {
2315b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        if (outputs1[i] != outputs2[i]) {
2316b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            return false;
2317f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2318f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2319b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    return true;
2320b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent}
2321b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
2322b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurentvoid AudioPolicyManagerBase::checkOutputForStrategy(routing_strategy strategy)
2323b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent{
232401e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent    audio_devices_t oldDevice = getDeviceForStrategy(strategy, true /*fromCache*/);
232501e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent    audio_devices_t newDevice = getDeviceForStrategy(strategy, false /*fromCache*/);
2326c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    SortedVector<audio_io_handle_t> srcOutputs = getOutputsForDevice(oldDevice, mPreviousOutputs);
2327c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    SortedVector<audio_io_handle_t> dstOutputs = getOutputsForDevice(newDevice, mOutputs);
2328b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
2329b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (!vectorsEqual(srcOutputs,dstOutputs)) {
2330b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        ALOGV("checkOutputForStrategy() strategy %d, moving from output %d to output %d",
2331b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent              strategy, srcOutputs[0], dstOutputs[0]);
23325ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // mute strategy while moving tracks from one output to another
23335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        for (size_t i = 0; i < srcOutputs.size(); i++) {
2334fa3697d716b444bbea6be480801536c44bf69214Eric Laurent            AudioOutputDescriptor *desc = mOutputs.valueFor(srcOutputs[i]);
233580f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent            if (desc->isStrategyActive(strategy)) {
2336fa3697d716b444bbea6be480801536c44bf69214Eric Laurent                setStrategyMute(strategy, true, srcOutputs[i]);
2337fa3697d716b444bbea6be480801536c44bf69214Eric Laurent                setStrategyMute(strategy, false, srcOutputs[i], MUTE_TIME_MS, newDevice);
2338fa3697d716b444bbea6be480801536c44bf69214Eric Laurent            }
23395ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
2340f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2341f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // Move effects associated to this strategy from previous output to new output
23424660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen        if (strategy == STRATEGY_MEDIA) {
234345c763947b657b347211dc9388754e05d30d0467Eric Laurent            audio_io_handle_t fxOutput = selectOutputForEffects(dstOutputs);
23444660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen            SortedVector<audio_io_handle_t> moved;
23454660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen            for (size_t i = 0; i < mEffects.size(); i++) {
23464660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                EffectDescriptor *desc = mEffects.valueAt(i);
23474660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                if (desc->mSession == AUDIO_SESSION_OUTPUT_MIX &&
234845c763947b657b347211dc9388754e05d30d0467Eric Laurent                        desc->mIo != fxOutput) {
23494660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                    if (moved.indexOf(desc->mIo) < 0) {
23504660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                        ALOGV("checkOutputForStrategy() moving effect %d to output %d",
235145c763947b657b347211dc9388754e05d30d0467Eric Laurent                              mEffects.keyAt(i), fxOutput);
23524660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                        mpClientInterface->moveEffects(AUDIO_SESSION_OUTPUT_MIX, desc->mIo,
235345c763947b657b347211dc9388754e05d30d0467Eric Laurent                                                       fxOutput);
23544660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                        moved.add(desc->mIo);
23554660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                    }
235645c763947b657b347211dc9388754e05d30d0467Eric Laurent                    desc->mIo = fxOutput;
23574660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen                }
23584660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen            }
23594660455366d2ee64cb65f0ecd6f7ddeb1c17bac6Marco Nelissen        }
23605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        // Move tracks associated to this strategy from previous output to new output
2361f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        for (int i = 0; i < (int)AudioSystem::NUM_STREAM_TYPES; i++) {
2362f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (getStrategy((AudioSystem::stream_type)i) == strategy) {
2363316222fcd60a429e53aa6205278697bb2df8ef32Glenn Kasten                mpClientInterface->invalidateStream((AudioSystem::stream_type)i);
2364f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2365f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2366f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2367f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2368f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2369f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::checkOutputForAllStrategies()
2370f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2371c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    checkOutputForStrategy(STRATEGY_ENFORCED_AUDIBLE);
2372f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForStrategy(STRATEGY_PHONE);
2373f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForStrategy(STRATEGY_SONIFICATION);
237412bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
2375f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForStrategy(STRATEGY_MEDIA);
2376f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    checkOutputForStrategy(STRATEGY_DTMF);
2377f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2378f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2379b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurentaudio_io_handle_t AudioPolicyManagerBase::getA2dpOutput()
2380b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent{
23815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (!mHasA2dp) {
2382b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        return 0;
2383b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    }
2384b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
2385b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    for (size_t i = 0; i < mOutputs.size(); i++) {
2386b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        AudioOutputDescriptor *outputDesc = mOutputs.valueAt(i);
2387b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        if (!outputDesc->isDuplicated() && outputDesc->device() & AUDIO_DEVICE_OUT_ALL_A2DP) {
2388b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            return mOutputs.keyAt(i);
2389b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        }
2390b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    }
2391b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
2392b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    return 0;
2393b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent}
2394b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
2395f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::checkA2dpSuspend()
2396f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
23975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (!mHasA2dp) {
2398b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        return;
2399b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    }
2400b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    audio_io_handle_t a2dpOutput = getA2dpOutput();
2401b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (a2dpOutput == 0) {
2402b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        return;
2403b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    }
2404b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
2405f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // suspend A2DP output if:
2406f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      (NOT already suspended) &&
2407f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      ((SCO device is connected &&
2408f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //       (forced usage for communication || for record is SCO))) ||
2409f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      (phone state is ringing || in call)
2410f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //
2411f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // restore A2DP output if:
2412f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      (Already suspended) &&
2413f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      ((SCO device is NOT connected ||
2414f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //       (forced usage NOT for communication && NOT for record is SCO))) &&
2415f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      (phone state is NOT ringing && NOT in call)
2416f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //
2417f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mA2dpSuspended) {
2418f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (((mScoDeviceAddress == "") ||
2419f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin             ((mForceUse[AudioSystem::FOR_COMMUNICATION] != AudioSystem::FORCE_BT_SCO) &&
2420f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin              (mForceUse[AudioSystem::FOR_RECORD] != AudioSystem::FORCE_BT_SCO))) &&
2421f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin             ((mPhoneState != AudioSystem::MODE_IN_CALL) &&
2422f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin              (mPhoneState != AudioSystem::MODE_RINGTONE))) {
2423f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2424b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            mpClientInterface->restoreOutput(a2dpOutput);
2425f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mA2dpSuspended = false;
2426f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2427f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
2428f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (((mScoDeviceAddress != "") &&
2429f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin             ((mForceUse[AudioSystem::FOR_COMMUNICATION] == AudioSystem::FORCE_BT_SCO) ||
2430f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin              (mForceUse[AudioSystem::FOR_RECORD] == AudioSystem::FORCE_BT_SCO))) ||
2431f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin             ((mPhoneState == AudioSystem::MODE_IN_CALL) ||
2432f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin              (mPhoneState == AudioSystem::MODE_RINGTONE))) {
2433f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2434b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent            mpClientInterface->suspendOutput(a2dpOutput);
2435f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mA2dpSuspended = true;
2436f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2437f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2438f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2439f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2440f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurentaudio_devices_t AudioPolicyManagerBase::getNewDevice(audio_io_handle_t output, bool fromCache)
2441f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2442ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    audio_devices_t device = AUDIO_DEVICE_NONE;
2443f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2444f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
2445f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // check the following by order of priority to request a routing change if necessary:
2446c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    // 1: the strategy enforced audible is active on the output:
2447c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    //      use device for strategy enforced audible
2448c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    // 2: we are in call or the strategy phone is active on the output:
2449f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      use device for strategy phone
2450c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    // 3: the strategy sonification is active on the output:
2451f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      use device for strategy sonification
245212bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    // 4: the strategy "respectful" sonification is active on the output:
245312bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    //      use device for strategy "respectful" sonification
245412bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    // 5: the strategy media is active on the output:
2455f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      use device for strategy media
245612bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    // 6: the strategy DTMF is active on the output:
2457f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //      use device for strategy DTMF
245880f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    if (outputDesc->isStrategyActive(STRATEGY_ENFORCED_AUDIBLE)) {
2459c16ac09f510437e8340be691720177a490ae78f0Eric Laurent        device = getDeviceForStrategy(STRATEGY_ENFORCED_AUDIBLE, fromCache);
2460c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    } else if (isInCall() ||
246180f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                    outputDesc->isStrategyActive(STRATEGY_PHONE)) {
2462f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device = getDeviceForStrategy(STRATEGY_PHONE, fromCache);
246380f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    } else if (outputDesc->isStrategyActive(STRATEGY_SONIFICATION)) {
2464f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device = getDeviceForStrategy(STRATEGY_SONIFICATION, fromCache);
246580f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    } else if (outputDesc->isStrategyActive(STRATEGY_SONIFICATION_RESPECTFUL)) {
24665ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        device = getDeviceForStrategy(STRATEGY_SONIFICATION_RESPECTFUL, fromCache);
246780f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    } else if (outputDesc->isStrategyActive(STRATEGY_MEDIA)) {
2468f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device = getDeviceForStrategy(STRATEGY_MEDIA, fromCache);
246980f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    } else if (outputDesc->isStrategyActive(STRATEGY_DTMF)) {
2470f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device = getDeviceForStrategy(STRATEGY_DTMF, fromCache);
2471f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2472f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
24736a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("getNewDevice() selected device %x", device);
2474f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return device;
2475f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2476f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2477f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinuint32_t AudioPolicyManagerBase::getStrategyForStream(AudioSystem::stream_type stream) {
2478f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return (uint32_t)getStrategy(stream);
2479f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2480f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2481f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurentaudio_devices_t AudioPolicyManagerBase::getDevicesForStream(AudioSystem::stream_type stream) {
2482f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent    audio_devices_t devices;
2483f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // By checking the range of stream before calling getStrategy, we avoid
24845efbd421e0029d6fc44b1cc65c0e5e0d85e5161fSteve Block    // getStrategy's behavior for invalid streams.  getStrategy would do a ALOGE
2485f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // and then return STRATEGY_MEDIA, but we want to return the empty set.
2486f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (stream < (AudioSystem::stream_type) 0 || stream >= AudioSystem::NUM_STREAM_TYPES) {
2487ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        devices = AUDIO_DEVICE_NONE;
2488f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
2489f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioPolicyManagerBase::routing_strategy strategy = getStrategy(stream);
24905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        devices = getDeviceForStrategy(strategy, true /*fromCache*/);
2491f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2492f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return devices;
2493f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2494f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2495f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima ZavinAudioPolicyManagerBase::routing_strategy AudioPolicyManagerBase::getStrategy(
2496f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        AudioSystem::stream_type stream) {
2497f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // stream to strategy mapping
2498f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    switch (stream) {
2499f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::VOICE_CALL:
2500f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::BLUETOOTH_SCO:
2501f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return STRATEGY_PHONE;
2502f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::RING:
2503f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::ALARM:
2504f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return STRATEGY_SONIFICATION;
250512bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    case AudioSystem::NOTIFICATION:
250612bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        return STRATEGY_SONIFICATION_RESPECTFUL;
2507f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::DTMF:
2508f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return STRATEGY_DTMF;
2509f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    default:
25105efbd421e0029d6fc44b1cc65c0e5e0d85e5161fSteve Block        ALOGE("unknown stream type");
2511f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::SYSTEM:
2512f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // NOTE: SYSTEM stream uses MEDIA strategy because muting music and switching outputs
2513f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // while key clicks are played produces a poor result
2514f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::TTS:
2515f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AudioSystem::MUSIC:
2516f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return STRATEGY_MEDIA;
2517c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    case AudioSystem::ENFORCED_AUDIBLE:
2518c16ac09f510437e8340be691720177a490ae78f0Eric Laurent        return STRATEGY_ENFORCED_AUDIBLE;
2519f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2520f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2521f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
252212bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivivoid AudioPolicyManagerBase::handleNotificationRoutingForStream(AudioSystem::stream_type stream) {
252312bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    switch(stream) {
252412bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    case AudioSystem::MUSIC:
252512bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        checkOutputForStrategy(STRATEGY_SONIFICATION_RESPECTFUL);
2526c952527e6f89d5427881462823514be9d79f13e6Eric Laurent        updateDevicesAndOutputs();
252712bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        break;
252812bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    default:
252912bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        break;
253012bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    }
253112bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi}
253212bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi
25335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentaudio_devices_t AudioPolicyManagerBase::getDeviceForStrategy(routing_strategy strategy,
25345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                             bool fromCache)
2535f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2536ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    uint32_t device = AUDIO_DEVICE_NONE;
2537f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2538f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (fromCache) {
25393cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        ALOGVV("getDeviceForStrategy() from cache strategy %d, device %x",
25405ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent              strategy, mDeviceForStrategy[strategy]);
2541f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return mDeviceForStrategy[strategy];
2542f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2543f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2544f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    switch (strategy) {
254512bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi
254612bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    case STRATEGY_SONIFICATION_RESPECTFUL:
254712bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        if (isInCall()) {
25485ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
2549dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi        } else if (isStreamActiveRemotely(AudioSystem::MUSIC,
2550dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi                SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
2551dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi            // while media is playing on a remote device, use the the sonification behavior.
2552dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi            // Note that we test this usecase before testing if media is playing because
2553dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi            //   the isStreamActive() method only informs about the activity of a stream, not
2554dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi            //   if it's for local playback. Note also that we use the same delay between both tests
2555dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Trivi            device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
255612bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        } else if (isStreamActive(AudioSystem::MUSIC, SONIFICATION_RESPECTFUL_AFTER_MUSIC_DELAY)) {
255712bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi            // while media is playing (or has recently played), use the same device
25585ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/);
255912bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        } else {
256012bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi            // when media is not playing anymore, fall back on the sonification behavior
25615ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = getDeviceForStrategy(STRATEGY_SONIFICATION, false /*fromCache*/);
256212bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        }
256312bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi
256412bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        break;
256512bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi
2566f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case STRATEGY_DTMF:
2567f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (!isInCall()) {
2568f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // when off call, DTMF strategy follows the same rules as MEDIA strategy
25695ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = getDeviceForStrategy(STRATEGY_MEDIA, false /*fromCache*/);
2570f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            break;
2571f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2572f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // when in call, DTMF and PHONE strategies follow the same rules
2573f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // FALL THROUGH
2574f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2575f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case STRATEGY_PHONE:
2576f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // for phone strategy, we first consider the forced use and then the available devices by order
2577f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // of priority
2578f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        switch (mForceUse[AudioSystem::FOR_COMMUNICATION]) {
2579f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        case AudioSystem::FORCE_BT_SCO:
2580f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (!isInCall() || strategy != STRATEGY_DTMF) {
2581c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT;
2582f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (device) break;
2583f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2584c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET;
2585f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (device) break;
2586c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_SCO;
2587f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (device) break;
2588f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // if SCO device is requested but no SCO device is available, fall back to default case
2589f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // FALL THROUGH
2590f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2591f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        default:    // FORCE_NONE
2592f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to A2DP
25931afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent            if (mHasA2dp && !isInCall() &&
25941afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent                    (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
2595ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent                    (getA2dpOutput() != 0) && !mA2dpSuspended) {
2596c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
2597f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (device) break;
2598c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
2599f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (device) break;
2600f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2601c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
26021afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent            if (device) break;
2603c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADSET;
26041afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent            if (device) break;
2605b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent            if (mPhoneState != AudioSystem::MODE_IN_CALL) {
2606b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
2607b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2608b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
2609b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2610b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
2611b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2612b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
2613b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2614b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
2615b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2616b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent            }
2617c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_EARPIECE;
26185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            if (device) break;
26195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = mDefaultOutputDevice;
2620ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            if (device == AUDIO_DEVICE_NONE) {
26215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE");
2622f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2623f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            break;
2624f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2625f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        case AudioSystem::FORCE_SPEAKER:
2626f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // when not in a phone call, phone strategy should route STREAM_VOICE_CALL to
2627f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            // A2DP speaker when forcing to speaker output
26281afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent            if (mHasA2dp && !isInCall() &&
26291afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent                    (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
2630ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent                    (getA2dpOutput() != 0) && !mA2dpSuspended) {
2631c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
2632f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (device) break;
2633f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2634b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent            if (mPhoneState != AudioSystem::MODE_IN_CALL) {
2635b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
2636b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2637b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
2638b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2639b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
2640b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2641b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
2642b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2643b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
2644b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent                if (device) break;
2645b52f373bd56fbbb07a625de15125d33672d5143fEric Laurent            }
2646c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER;
26475ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            if (device) break;
26485ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = mDefaultOutputDevice;
2649ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            if (device == AUDIO_DEVICE_NONE) {
26505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                ALOGE("getDeviceForStrategy() no device found for STRATEGY_PHONE, FORCE_SPEAKER");
2651f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2652f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            break;
2653f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2654f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    break;
2655f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2656f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case STRATEGY_SONIFICATION:
2657f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2658f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // If incall, just select the STRATEGY_PHONE device: The rest of the behavior is handled by
2659f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // handleIncallSonification().
2660f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (isInCall()) {
26615ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device = getDeviceForStrategy(STRATEGY_PHONE, false /*fromCache*/);
2662f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            break;
2663f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2664c16ac09f510437e8340be691720177a490ae78f0Eric Laurent        // FALL THROUGH
2665c16ac09f510437e8340be691720177a490ae78f0Eric Laurent
2666c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    case STRATEGY_ENFORCED_AUDIBLE:
2667c16ac09f510437e8340be691720177a490ae78f0Eric Laurent        // strategy STRATEGY_ENFORCED_AUDIBLE uses same routing policy as STRATEGY_SONIFICATION
2668ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        // except:
2669ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        //   - when in call where it doesn't default to STRATEGY_PHONE behavior
2670ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        //   - in countries where not enforced in which case it follows STRATEGY_MEDIA
2671c16ac09f510437e8340be691720177a490ae78f0Eric Laurent
2672738207def5f691d605ae33d041116829a74513a9Eric Laurent        if ((strategy == STRATEGY_SONIFICATION) ||
2673738207def5f691d605ae33d041116829a74513a9Eric Laurent                (mForceUse[AudioSystem::FOR_SYSTEM] == AudioSystem::FORCE_SYSTEM_ENFORCED)) {
2674c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER;
2675ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            if (device == AUDIO_DEVICE_NONE) {
2676ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent                ALOGE("getDeviceForStrategy() speaker device not found for STRATEGY_SONIFICATION");
2677ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent            }
2678f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2679f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // The second device used for sonification is the same as the device used by media strategy
2680f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // FALL THROUGH
2681f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2682f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case STRATEGY_MEDIA: {
2683ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        uint32_t device2 = AUDIO_DEVICE_NONE;
268431363a9cb94e80330c335fede0b92b1953a09517Jean-Michel Trivi        if (strategy != STRATEGY_SONIFICATION) {
268531363a9cb94e80330c335fede0b92b1953a09517Jean-Michel Trivi            // no sonification on remote submix (e.g. WFD)
268648387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_REMOTE_SUBMIX;
268748387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi        }
2688ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if ((device2 == AUDIO_DEVICE_NONE) &&
268948387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi                mHasA2dp && (mForceUse[AudioSystem::FOR_MEDIA] != AudioSystem::FORCE_NO_BT_A2DP) &&
2690ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent                (getA2dpOutput() != 0) && !mA2dpSuspended) {
2691c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP;
2692ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            if (device2 == AUDIO_DEVICE_NONE) {
2693c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES;
2694f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2695ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            if (device2 == AUDIO_DEVICE_NONE) {
2696c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER;
2697f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
2698f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2699ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (device2 == AUDIO_DEVICE_NONE) {
2700c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADPHONE;
27011afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent        }
2702ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (device2 == AUDIO_DEVICE_NONE) {
2703c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_WIRED_HEADSET;
27041afd84f62291d20414cbf58c7af01462a8fdca60Eric Laurent        }
2705ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (device2 == AUDIO_DEVICE_NONE) {
2706599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_ACCESSORY;
2707599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent        }
2708ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (device2 == AUDIO_DEVICE_NONE) {
2709599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_USB_DEVICE;
2710599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent        }
2711ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (device2 == AUDIO_DEVICE_NONE) {
2712c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET;
2713f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
271431363a9cb94e80330c335fede0b92b1953a09517Jean-Michel Trivi        if ((device2 == AUDIO_DEVICE_NONE) && (strategy != STRATEGY_SONIFICATION)) {
271531363a9cb94e80330c335fede0b92b1953a09517Jean-Michel Trivi            // no sonification on aux digital (e.g. HDMI)
2716c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_AUX_DIGITAL;
2717f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
27185a484b753cc72d6a50c1dd3bbf68b3403c741a3aEric Laurent        if ((device2 == AUDIO_DEVICE_NONE) &&
27195a484b753cc72d6a50c1dd3bbf68b3403c741a3aEric Laurent                (mForceUse[AudioSystem::FOR_DOCK] == AudioSystem::FORCE_ANALOG_DOCK)) {
2720c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET;
2721f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2722ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (device2 == AUDIO_DEVICE_NONE) {
2723c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device2 = mAvailableOutputDevices & AUDIO_DEVICE_OUT_SPEAKER;
2724f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2725f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2726c16ac09f510437e8340be691720177a490ae78f0Eric Laurent        // device is DEVICE_OUT_SPEAKER if we come from case STRATEGY_SONIFICATION or
2727ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        // STRATEGY_ENFORCED_AUDIBLE, AUDIO_DEVICE_NONE otherwise
2728f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device |= device2;
27295ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (device) break;
27305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        device = mDefaultOutputDevice;
2731ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent        if (device == AUDIO_DEVICE_NONE) {
27325ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGE("getDeviceForStrategy() no device found for STRATEGY_MEDIA");
2733f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2734f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        } break;
2735f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2736f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    default:
273764cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("getDeviceForStrategy() unknown strategy: %d", strategy);
2738f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
2739f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2740f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
27413cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGVV("getDeviceForStrategy() strategy %d, device %x", strategy, device);
2742c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    return device;
2743f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2744f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2745c952527e6f89d5427881462823514be9d79f13e6Eric Laurentvoid AudioPolicyManagerBase::updateDevicesAndOutputs()
2746f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2747f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < NUM_STRATEGIES; i++) {
27485ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        mDeviceForStrategy[i] = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
27495ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
2750c952527e6f89d5427881462823514be9d79f13e6Eric Laurent    mPreviousOutputs = mOutputs;
27515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
27525ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
2753b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurentuint32_t AudioPolicyManagerBase::checkDeviceMuteStrategies(AudioOutputDescriptor *outputDesc,
27549029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                                                       audio_devices_t prevDevice,
27555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                       uint32_t delayMs)
27565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
27579029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    // mute/unmute strategies using an incompatible device combination
27589029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    // if muting, wait for the audio in pcm buffer to be drained before proceeding
27599029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    // if unmuting, unmute only after the specified delay
27605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (outputDesc->isDuplicated()) {
2761b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        return 0;
27625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
27635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
27645ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    uint32_t muteWaitMs = 0;
27655ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    audio_devices_t device = outputDesc->device();
276680f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    bool shouldMute = outputDesc->isActive() && (AudioSystem::popCount(device) >= 2);
27679029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    // temporary mute output if device selection changes to avoid volume bursts due to
27689029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    // different per device volumes
276980f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    bool tempMute = outputDesc->isActive() && (device != prevDevice);
27705ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
27715ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < NUM_STRATEGIES; i++) {
27725ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_devices_t curDevice = getDeviceForStrategy((routing_strategy)i, false /*fromCache*/);
27735ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        bool mute = shouldMute && (curDevice & device) && (curDevice != device);
27745ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        bool doMute = false;
27755ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
27765ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (mute && !outputDesc->mStrategyMutedByDevice[i]) {
27775ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            doMute = true;
27785ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            outputDesc->mStrategyMutedByDevice[i] = true;
27795ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (!mute && outputDesc->mStrategyMutedByDevice[i]){
27805ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            doMute = true;
27815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            outputDesc->mStrategyMutedByDevice[i] = false;
27825ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
27839029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent        if (doMute || tempMute) {
27845ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            for (size_t j = 0; j < mOutputs.size(); j++) {
27855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                AudioOutputDescriptor *desc = mOutputs.valueAt(j);
2786f32e38e24db196671d3ea43427125a4e212466faEric Laurent                // skip output if it does not share any device with current output
2787c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                if ((desc->supportedDevices() & outputDesc->supportedDevices())
2788ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent                        == AUDIO_DEVICE_NONE) {
27895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                    continue;
27905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                }
27915ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                audio_io_handle_t curOutput = mOutputs.keyAt(j);
27923cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                ALOGVV("checkDeviceMuteStrategies() %s strategy %d (curDevice %04x) on output %d",
27935ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                      mute ? "muting" : "unmuting", i, curDevice, curOutput);
27945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                setStrategyMute((routing_strategy)i, mute, curOutput, mute ? 0 : delayMs);
279580f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                if (desc->isStrategyActive((routing_strategy)i)) {
2796f32e38e24db196671d3ea43427125a4e212466faEric Laurent                    // do tempMute only for current output
2797f32e38e24db196671d3ea43427125a4e212466faEric Laurent                    if (tempMute && (desc == outputDesc)) {
279801e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                        setStrategyMute((routing_strategy)i, true, curOutput);
279901e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                        setStrategyMute((routing_strategy)i, false, curOutput,
280001e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                            desc->latency() * 2, device);
28019029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                    }
2802f32e38e24db196671d3ea43427125a4e212466faEric Laurent                    if ((tempMute && (desc == outputDesc)) || mute) {
28039029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                        if (muteWaitMs < desc->latency()) {
28049029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                            muteWaitMs = desc->latency();
28059029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent                        }
28065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                    }
28075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                }
28085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
28095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
28105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
28115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
28125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // FIXME: should not need to double latency if volume could be applied immediately by the
28135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // audioflinger mixer. We must account for the delay between now and the next time
28145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // the audioflinger thread for this output will process a buffer (which corresponds to
28155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // one buffer size, usually 1/2 or 1/4 of the latency).
28165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    muteWaitMs *= 2;
28175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // wait for the PCM output buffers to empty before proceeding with the rest of the command
28185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (muteWaitMs > delayMs) {
2819b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        muteWaitMs -= delayMs;
2820b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        usleep(muteWaitMs * 1000);
2821b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        return muteWaitMs;
2822f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2823b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent    return 0;
2824f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2825f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2826b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurentuint32_t AudioPolicyManagerBase::setOutputDevice(audio_io_handle_t output,
2827f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                                             audio_devices_t device,
2828f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                                             bool force,
2829f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                                             int delayMs)
2830f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
28315ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGV("setOutputDevice() output %d device %04x delayMs %d", output, device, delayMs);
2832f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
28335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    AudioParameter param;
2834f32e38e24db196671d3ea43427125a4e212466faEric Laurent    uint32_t muteWaitMs;
2835f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2836f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (outputDesc->isDuplicated()) {
2837b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        muteWaitMs = setOutputDevice(outputDesc->mOutput1->mId, device, force, delayMs);
2838b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        muteWaitMs += setOutputDevice(outputDesc->mOutput2->mId, device, force, delayMs);
2839b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        return muteWaitMs;
2840f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2841f32e38e24db196671d3ea43427125a4e212466faEric Laurent    // no need to proceed if new device is not AUDIO_DEVICE_NONE and not supported by current
2842f32e38e24db196671d3ea43427125a4e212466faEric Laurent    // output profile
2843f32e38e24db196671d3ea43427125a4e212466faEric Laurent    if ((device != AUDIO_DEVICE_NONE) &&
2844f32e38e24db196671d3ea43427125a4e212466faEric Laurent            ((device & outputDesc->mProfile->mSupportedDevices) == 0)) {
2845f32e38e24db196671d3ea43427125a4e212466faEric Laurent        return 0;
2846f32e38e24db196671d3ea43427125a4e212466faEric Laurent    }
2847f32e38e24db196671d3ea43427125a4e212466faEric Laurent
2848f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // filter devices according to output selected
2849f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent    device = (audio_devices_t)(device & outputDesc->mProfile->mSupportedDevices);
2850f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
28515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    audio_devices_t prevDevice = outputDesc->mDevice;
28525ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
28535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGV("setOutputDevice() prevDevice %04x", prevDevice);
28545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
2855ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if (device != AUDIO_DEVICE_NONE) {
28565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        outputDesc->mDevice = device;
28575ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
28589029a4fe8abafd383e6fbb1409d1e2f749b51391Eric Laurent    muteWaitMs = checkDeviceMuteStrategies(outputDesc, prevDevice, delayMs);
28595ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
2860f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // Do not change the routing if:
2861ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    //  - the requested device is AUDIO_DEVICE_NONE
2862f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    //  - the requested device is the same as current device and force is not specified.
2863f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // Doing this check here allows the caller to call setOutputDevice() without conditions
2864ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if ((device == AUDIO_DEVICE_NONE || device == prevDevice) && !force) {
28655ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        ALOGV("setOutputDevice() setting same device %04x or null device for output %d", device, output);
2866b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent        return muteWaitMs;
2867f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2868f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
28695ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGV("setOutputDevice() changing device");
2870f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // do the routing
2871f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    param.addInt(String8(AudioParameter::keyRouting), (int)device);
28725ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    mpClientInterface->setParameters(output, param.toString(), delayMs);
28735ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
2874f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // update stream volumes according to new device
2875f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    applyStreamVolumes(output, device, delayMs);
2876b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent
2877b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent    return muteWaitMs;
28785ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
2879f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
28805ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric LaurentAudioPolicyManagerBase::IOProfile *AudioPolicyManagerBase::getInputProfile(audio_devices_t device,
28815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                   uint32_t samplingRate,
28825082dbeb19e37883230510129c94336063a4f91cGlenn Kasten                                                   audio_format_t format,
28832c3d2379ee6eadb32b63bf6de538cfd6487e8e20Glenn Kasten                                                   audio_channel_mask_t channelMask)
28845ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
28855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // Choose an input profile based on the requested capture parameters: select the first available
28865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // profile supporting all requested parameters.
288770c236c9290732782d5267935af1475b8d5ae602Eric Laurent    for (size_t i = 0; i < mHwModules.size(); i++)
28885ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    {
288970c236c9290732782d5267935af1475b8d5ae602Eric Laurent        if (mHwModules[i]->mHandle == 0) {
28905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            continue;
28915ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
289270c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (size_t j = 0; j < mHwModules[i]->mInputProfiles.size(); j++)
28935ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        {
289470c236c9290732782d5267935af1475b8d5ae602Eric Laurent            IOProfile *profile = mHwModules[i]->mInputProfiles[j];
2895fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            // profile->log();
289670c236c9290732782d5267935af1475b8d5ae602Eric Laurent            if (profile->isCompatibleProfile(device, samplingRate, format,
28972d749b0bda4ae60ef1a940eeba86121b2e94d07dGlenn Kasten                                             channelMask, AUDIO_OUTPUT_FLAG_NONE)) {
289870c236c9290732782d5267935af1475b8d5ae602Eric Laurent                return profile;
28995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
29005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
2901f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
290270c236c9290732782d5267935af1475b8d5ae602Eric Laurent    return NULL;
2903f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2904f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2905f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurentaudio_devices_t AudioPolicyManagerBase::getDeviceForInputSource(int inputSource)
2906f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2907ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    uint32_t device = AUDIO_DEVICE_NONE;
2908f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
29099641bd36dbf0c4b1eeb84499b35c1cdb7fa5a530Eric Laurent    switch (inputSource) {
29109641bd36dbf0c4b1eeb84499b35c1cdb7fa5a530Eric Laurent    case AUDIO_SOURCE_VOICE_UPLINK:
29119641bd36dbf0c4b1eeb84499b35c1cdb7fa5a530Eric Laurent      if (mAvailableInputDevices & AUDIO_DEVICE_IN_VOICE_CALL) {
29129641bd36dbf0c4b1eeb84499b35c1cdb7fa5a530Eric Laurent          device = AUDIO_DEVICE_IN_VOICE_CALL;
29139641bd36dbf0c4b1eeb84499b35c1cdb7fa5a530Eric Laurent          break;
29149641bd36dbf0c4b1eeb84499b35c1cdb7fa5a530Eric Laurent      }
29159641bd36dbf0c4b1eeb84499b35c1cdb7fa5a530Eric Laurent      // FALL THROUGH
29169641bd36dbf0c4b1eeb84499b35c1cdb7fa5a530Eric Laurent
2917f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_DEFAULT:
2918f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_MIC:
291933bf1b0fe363bd4892349d160f54d860567fab12Mike Lockwood    if (mAvailableInputDevices & AUDIO_DEVICE_IN_BLUETOOTH_A2DP) {
292033bf1b0fe363bd4892349d160f54d860567fab12Mike Lockwood        device = AUDIO_DEVICE_IN_BLUETOOTH_A2DP;
292133bf1b0fe363bd4892349d160f54d860567fab12Mike Lockwood        break;
292233bf1b0fe363bd4892349d160f54d860567fab12Mike Lockwood    }
292333bf1b0fe363bd4892349d160f54d860567fab12Mike Lockwood    // FALL THROUGH
292433bf1b0fe363bd4892349d160f54d860567fab12Mike Lockwood
2925f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_RECOGNITION:
2926fc9b2457ce4721538dca0903de63c7766e97498aEric Laurent    case AUDIO_SOURCE_HOTWORD:
2927f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_COMMUNICATION:
2928f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (mForceUse[AudioSystem::FOR_RECORD] == AudioSystem::FORCE_BT_SCO &&
2929c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            mAvailableInputDevices & AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET) {
2930c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET;
2931c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_WIRED_HEADSET) {
2932c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = AUDIO_DEVICE_IN_WIRED_HEADSET;
2933fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_USB_DEVICE) {
2934fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean            device = AUDIO_DEVICE_IN_USB_DEVICE;
2935c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_BUILTIN_MIC) {
2936c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = AUDIO_DEVICE_IN_BUILTIN_MIC;
2937f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2938f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
2939f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_CAMCORDER:
2940c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        if (mAvailableInputDevices & AUDIO_DEVICE_IN_BACK_MIC) {
2941c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = AUDIO_DEVICE_IN_BACK_MIC;
2942c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        } else if (mAvailableInputDevices & AUDIO_DEVICE_IN_BUILTIN_MIC) {
2943c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = AUDIO_DEVICE_IN_BUILTIN_MIC;
2944f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2945f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
2946f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_DOWNLINK:
2947f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    case AUDIO_SOURCE_VOICE_CALL:
2948c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi        if (mAvailableInputDevices & AUDIO_DEVICE_IN_VOICE_CALL) {
2949c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            device = AUDIO_DEVICE_IN_VOICE_CALL;
29505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
2951f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
295248387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi    case AUDIO_SOURCE_REMOTE_SUBMIX:
295348387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi        if (mAvailableInputDevices & AUDIO_DEVICE_IN_REMOTE_SUBMIX) {
295448387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            device = AUDIO_DEVICE_IN_REMOTE_SUBMIX;
295548387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi        }
295648387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi        break;
2957f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    default:
295864cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("getDeviceForInputSource() invalid input source %d", inputSource);
2959f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        break;
2960f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
29616a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("getDeviceForInputSource()input source %d, device %08x", inputSource, device);
2962c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    return device;
2963f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2964f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
29656d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivibool AudioPolicyManagerBase::isVirtualInputDevice(audio_devices_t device)
29666d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi{
29676d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi    if ((device & AUDIO_DEVICE_BIT_IN) != 0) {
29686d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi        device &= ~AUDIO_DEVICE_BIT_IN;
29696d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi        if ((popcount(device) == 1) && ((device & ~APM_AUDIO_IN_DEVICE_VIRTUAL_ALL) == 0))
29706d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi            return true;
29716d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi    }
29726d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi    return false;
29736d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi}
29746d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi
29756d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Triviaudio_io_handle_t AudioPolicyManagerBase::getActiveInput(bool ignoreVirtualInputs)
2976f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
2977f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (size_t i = 0; i < mInputs.size(); i++) {
29786d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi        const AudioInputDescriptor * input_descriptor = mInputs.valueAt(i);
29796d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi        if ((input_descriptor->mRefCount > 0)
29806d3a115c091adc1618b3d1e50a86951c3b35fa30Jean-Michel Trivi                && (!ignoreVirtualInputs || !isVirtualInputDevice(input_descriptor->mDevice))) {
2981f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return mInputs.keyAt(i);
2982f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
2983f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
2984f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return 0;
2985f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
2986f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
2987e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
2988c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentaudio_devices_t AudioPolicyManagerBase::getDeviceForVolume(audio_devices_t device)
2989e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent{
2990ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if (device == AUDIO_DEVICE_NONE) {
2991e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        // this happens when forcing a route update and no track is active on an output.
2992e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        // In this case the returned category is not important.
2993c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        device =  AUDIO_DEVICE_OUT_SPEAKER;
2994c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    } else if (AudioSystem::popCount(device) > 1) {
2995e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        // Multiple device selection is either:
2996e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        //  - speaker + one other device: give priority to speaker in this case.
2997e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        //  - one A2DP device + another device: happens with duplicated output. In this case
2998e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        // retain the device on the A2DP output as the other must not correspond to an active
2999e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        // selection if not the speaker.
3000c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        if (device & AUDIO_DEVICE_OUT_SPEAKER) {
3001c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            device = AUDIO_DEVICE_OUT_SPEAKER;
3002c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        } else {
3003c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            device = (audio_devices_t)(device & AUDIO_DEVICE_OUT_ALL_A2DP);
3004c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        }
3005e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    }
3006e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
300764cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block    ALOGW_IF(AudioSystem::popCount(device) != 1,
3008c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            "getDeviceForVolume() invalid device combination: %08x",
3009e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            device);
3010e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
3011c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    return device;
3012c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent}
3013c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
3014f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric LaurentAudioPolicyManagerBase::device_category AudioPolicyManagerBase::getDeviceCategory(audio_devices_t device)
3015c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent{
3016f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent    switch(getDeviceForVolume(device)) {
3017e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_EARPIECE:
3018e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            return DEVICE_CATEGORY_EARPIECE;
3019e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_WIRED_HEADSET:
3020e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_WIRED_HEADPHONE:
3021e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_BLUETOOTH_SCO:
3022e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_HEADSET:
3023e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP:
3024e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES:
3025e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            return DEVICE_CATEGORY_HEADSET;
3026e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_SPEAKER:
3027e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_BLUETOOTH_SCO_CARKIT:
3028e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        case AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER:
3029c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        case AUDIO_DEVICE_OUT_AUX_DIGITAL:
3030599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent        case AUDIO_DEVICE_OUT_USB_ACCESSORY:
3031599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent        case AUDIO_DEVICE_OUT_USB_DEVICE:
303248387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi        case AUDIO_DEVICE_OUT_REMOTE_SUBMIX:
3033e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        default:
3034e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            return DEVICE_CATEGORY_SPEAKER;
3035e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    }
3036e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent}
3037e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
3038f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurentfloat AudioPolicyManagerBase::volIndexToAmpl(audio_devices_t device, const StreamDescriptor& streamDesc,
3039e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        int indexInUi)
3040e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent{
3041e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    device_category deviceCategory = getDeviceCategory(device);
3042e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    const VolumeCurvePoint *curve = streamDesc.mVolumeCurve[deviceCategory];
3043e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
3044f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // the volume index in the UI is relative to the min and max volume indices for this stream type
3045e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    int nbSteps = 1 + curve[VOLMAX].mIndex -
3046e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            curve[VOLMIN].mIndex;
3047f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    int volIdx = (nbSteps * (indexInUi - streamDesc.mIndexMin)) /
3048f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            (streamDesc.mIndexMax - streamDesc.mIndexMin);
3049f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3050f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // find what part of the curve this index volume belongs to, or if it's out of bounds
3051f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    int segment = 0;
3052e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    if (volIdx < curve[VOLMIN].mIndex) {         // out of bounds
3053f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return 0.0f;
3054e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    } else if (volIdx < curve[VOLKNEE1].mIndex) {
3055f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        segment = 0;
3056e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    } else if (volIdx < curve[VOLKNEE2].mIndex) {
3057f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        segment = 1;
3058e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    } else if (volIdx <= curve[VOLMAX].mIndex) {
3059f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        segment = 2;
3060f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {                                                               // out of bounds
3061f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return 1.0f;
3062f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3063f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3064f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // linear interpolation in the attenuation table in dB
3065e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    float decibels = curve[segment].mDBAttenuation +
3066e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            ((float)(volIdx - curve[segment].mIndex)) *
3067e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent                ( (curve[segment+1].mDBAttenuation -
3068e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent                        curve[segment].mDBAttenuation) /
3069e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent                    ((float)(curve[segment+1].mIndex -
3070e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent                            curve[segment].mIndex)) );
3071f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3072f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    float amplification = exp( decibels * 0.115129f); // exp( dB * ln(10) / 20 )
3073f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
30743cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGVV("VOLUME vol index=[%d %d %d], dB=[%.1f %.1f %.1f] ampl=%.5f",
3075e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            curve[segment].mIndex, volIdx,
3076e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            curve[segment+1].mIndex,
3077e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            curve[segment].mDBAttenuation,
3078cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent            decibels,
3079e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent            curve[segment+1].mDBAttenuation,
3080f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            amplification);
3081f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3082f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return amplification;
3083f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3084f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3085cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
3086e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    AudioPolicyManagerBase::sDefaultVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
3087e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    {1, -49.5f}, {33, -33.5f}, {66, -17.0f}, {100, 0.0f}
3088e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent};
3089e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
3090e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
3091e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    AudioPolicyManagerBase::sDefaultMediaVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
3092e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    {1, -58.0f}, {20, -40.0f}, {60, -17.0f}, {100, 0.0f}
3093cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent};
3094cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent
3095e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
3096e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    AudioPolicyManagerBase::sSpeakerMediaVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
3097e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    {1, -56.0f}, {20, -34.0f}, {60, -11.0f}, {100, 0.0f}
3098e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent};
3099e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
3100e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
3101e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    AudioPolicyManagerBase::sSpeakerSonificationVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
3102e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    {1, -29.7f}, {33, -20.1f}, {66, -10.2f}, {100, 0.0f}
3103e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent};
3104e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
310518fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Triviconst AudioPolicyManagerBase::VolumeCurvePoint
310618fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi    AudioPolicyManagerBase::sSpeakerSonificationVolumeCurveDrc[AudioPolicyManagerBase::VOLCNT] = {
310718fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi    {1, -35.7f}, {33, -26.1f}, {66, -13.2f}, {100, 0.0f}
310818fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi};
310918fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi
3110ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent// AUDIO_STREAM_SYSTEM, AUDIO_STREAM_ENFORCED_AUDIBLE and AUDIO_STREAM_DTMF volume tracks
311187e28f7b352fb77f46e16ebcdd85cbf01396a203Eric Laurent// AUDIO_STREAM_RING on phones and AUDIO_STREAM_MUSIC on tablets.
311287e28f7b352fb77f46e16ebcdd85cbf01396a203Eric Laurent// AUDIO_STREAM_DTMF tracks AUDIO_STREAM_VOICE_CALL while in call (See AudioService.java).
3113123897874418f9f0e48bb89386d8c470e6975f28Jean-Michel Trivi// The range is constrained between -24dB and -6dB over speaker and -30dB and -18dB over headset.
311487e28f7b352fb77f46e16ebcdd85cbf01396a203Eric Laurent
3115ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
3116ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    AudioPolicyManagerBase::sDefaultSystemVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
3117123897874418f9f0e48bb89386d8c470e6975f28Jean-Michel Trivi    {1, -24.0f}, {33, -18.0f}, {66, -12.0f}, {100, -6.0f}
3118ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent};
3119ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent
3120ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
312118fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi    AudioPolicyManagerBase::sDefaultSystemVolumeCurveDrc[AudioPolicyManagerBase::VOLCNT] = {
312218fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi    {1, -34.0f}, {33, -24.0f}, {66, -15.0f}, {100, -6.0f}
312318fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi};
312418fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi
312518fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Triviconst AudioPolicyManagerBase::VolumeCurvePoint
3126ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    AudioPolicyManagerBase::sHeadsetSystemVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
31277465678e0d5711ebcd78ae47b3a76821534a23eaEric Laurent    {1, -30.0f}, {33, -26.0f}, {66, -22.0f}, {100, -18.0f}
3128ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent};
3129e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
3130e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
31310d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent    AudioPolicyManagerBase::sDefaultVoiceVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
31320d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent    {0, -42.0f}, {33, -28.0f}, {66, -14.0f}, {100, 0.0f}
31330d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent};
31340d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent
31350d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
31360d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent    AudioPolicyManagerBase::sSpeakerVoiceVolumeCurve[AudioPolicyManagerBase::VOLCNT] = {
31370d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent    {0, -24.0f}, {33, -16.0f}, {66, -8.0f}, {100, 0.0f}
31380d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent};
31390d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent
31400d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurentconst AudioPolicyManagerBase::VolumeCurvePoint
3141e714af927c1e87a488178315b78b0c12c0ec9db9Eric Laurent            *AudioPolicyManagerBase::sVolumeProfiles[AudioSystem::NUM_STREAM_TYPES]
3142e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent                                                   [AudioPolicyManagerBase::DEVICE_CATEGORY_CNT] = {
3143ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_VOICE_CALL
31440d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent        sDefaultVoiceVolumeCurve, // DEVICE_CATEGORY_HEADSET
31450d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent        sSpeakerVoiceVolumeCurve, // DEVICE_CATEGORY_SPEAKER
31460d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent        sDefaultVoiceVolumeCurve  // DEVICE_CATEGORY_EARPIECE
3147e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    },
3148ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_SYSTEM
3149ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET
3150ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER
3151ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultSystemVolumeCurve  // DEVICE_CATEGORY_EARPIECE
3152ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    },
3153ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_RING
3154e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
3155e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER
3156e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sDefaultVolumeCurve  // DEVICE_CATEGORY_EARPIECE
3157e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent    },
3158ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_MUSIC
3159ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultMediaVolumeCurve, // DEVICE_CATEGORY_HEADSET
3160ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sSpeakerMediaVolumeCurve, // DEVICE_CATEGORY_SPEAKER
3161ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultMediaVolumeCurve  // DEVICE_CATEGORY_EARPIECE
3162ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    },
3163ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_ALARM
316412bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
316512bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER
316612bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        sDefaultVolumeCurve  // DEVICE_CATEGORY_EARPIECE
316712bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    },
3168ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_NOTIFICATION
3169e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sDefaultVolumeCurve, // DEVICE_CATEGORY_HEADSET
3170ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sSpeakerSonificationVolumeCurve, // DEVICE_CATEGORY_SPEAKER
3171e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        sDefaultVolumeCurve  // DEVICE_CATEGORY_EARPIECE
3172c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    },
3173ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_BLUETOOTH_SCO
31740d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent        sDefaultVoiceVolumeCurve, // DEVICE_CATEGORY_HEADSET
31750d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent        sSpeakerVoiceVolumeCurve, // DEVICE_CATEGORY_SPEAKER
31760d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent        sDefaultVoiceVolumeCurve  // DEVICE_CATEGORY_EARPIECE
3177c16ac09f510437e8340be691720177a490ae78f0Eric Laurent    },
3178ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_ENFORCED_AUDIBLE
3179ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET
3180ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER
3181ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultSystemVolumeCurve  // DEVICE_CATEGORY_EARPIECE
3182ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    },
3183ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    {  // AUDIO_STREAM_DTMF
3184ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sHeadsetSystemVolumeCurve, // DEVICE_CATEGORY_HEADSET
3185ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultSystemVolumeCurve, // DEVICE_CATEGORY_SPEAKER
3186ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultSystemVolumeCurve  // DEVICE_CATEGORY_EARPIECE
3187ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    },
3188ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    { // AUDIO_STREAM_TTS
3189ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultMediaVolumeCurve, // DEVICE_CATEGORY_HEADSET
3190ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sSpeakerMediaVolumeCurve, // DEVICE_CATEGORY_SPEAKER
3191ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent        sDefaultMediaVolumeCurve  // DEVICE_CATEGORY_EARPIECE
3192ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent    },
3193e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent};
3194e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent
3195e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurentvoid AudioPolicyManagerBase::initializeVolumeCurves()
3196e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent{
3197e714af927c1e87a488178315b78b0c12c0ec9db9Eric Laurent    for (int i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) {
3198e43c5c4ca4477a6b384711552c3e990609039d93Eric Laurent        for (int j = 0; j < DEVICE_CATEGORY_CNT; j++) {
3199cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent            mStreams[i].mVolumeCurve[j] =
3200ddfe26905ea3210c86a88eeb331eef4c9f99b931Eric Laurent                    sVolumeProfiles[i][j];
3201cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent        }
3202cd057ad2774903c62f5c11bc71c4e7357aee71b8Eric Laurent    }
320318fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi
320418fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi    // Check availability of DRC on speaker path: if available, override some of the speaker curves
320518fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi    if (mSpeakerDrcEnabled) {
320618fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi        mStreams[AUDIO_STREAM_SYSTEM].mVolumeCurve[DEVICE_CATEGORY_SPEAKER] =
320718fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi                sDefaultSystemVolumeCurveDrc;
320818fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi        mStreams[AUDIO_STREAM_RING].mVolumeCurve[DEVICE_CATEGORY_SPEAKER] =
320918fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi                sSpeakerSonificationVolumeCurveDrc;
321018fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi        mStreams[AUDIO_STREAM_ALARM].mVolumeCurve[DEVICE_CATEGORY_SPEAKER] =
321118fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi                sSpeakerSonificationVolumeCurveDrc;
321218fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi        mStreams[AUDIO_STREAM_NOTIFICATION].mVolumeCurve[DEVICE_CATEGORY_SPEAKER] =
321318fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi                sSpeakerSonificationVolumeCurveDrc;
321418fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi    }
3215f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3216f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3217c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentfloat AudioPolicyManagerBase::computeVolume(int stream,
3218c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                            int index,
3219c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                            audio_io_handle_t output,
3220f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                                            audio_devices_t device)
3221f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3222f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    float volume = 1.0;
3223f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
3224f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    StreamDescriptor &streamDesc = mStreams[stream];
3225f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3226ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if (device == AUDIO_DEVICE_NONE) {
3227f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        device = outputDesc->device();
3228f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3229f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3230f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if volume is not 0 (not muted), force media volume to max on digital output
3231f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (stream == AudioSystem::MUSIC &&
3232f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        index != mStreams[stream].mIndexMin &&
3233f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent        (device == AUDIO_DEVICE_OUT_AUX_DIGITAL ||
323403d92f4a6ec954cfa81765f00c1784e2c2ff2f49Paul McLean         device == AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET)) {
3235f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return 1.0;
3236f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3237f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3238f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    volume = volIndexToAmpl(device, streamDesc, index);
3239f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3240f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if a headset is connected, apply the following rules to ring tones and notifications
3241f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // to avoid sound level bursts in user's ears:
3242f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // - always attenuate ring tones and notifications volume by 6dB
3243f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // - if music is playing, always limit the volume to current music volume,
3244f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // with a minimum threshold at -36dB so that notification is always perceived.
324512bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    const routing_strategy stream_strategy = getStrategy((AudioSystem::stream_type)stream);
3246c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    if ((device & (AUDIO_DEVICE_OUT_BLUETOOTH_A2DP |
3247c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_HEADPHONES |
3248c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            AUDIO_DEVICE_OUT_WIRED_HEADSET |
3249c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi            AUDIO_DEVICE_OUT_WIRED_HEADPHONE)) &&
325012bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi        ((stream_strategy == STRATEGY_SONIFICATION)
325112bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi                || (stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL)
3252738207def5f691d605ae33d041116829a74513a9Eric Laurent                || (stream == AudioSystem::SYSTEM)
3253738207def5f691d605ae33d041116829a74513a9Eric Laurent                || ((stream_strategy == STRATEGY_ENFORCED_AUDIBLE) &&
3254738207def5f691d605ae33d041116829a74513a9Eric Laurent                    (mForceUse[AudioSystem::FOR_SYSTEM] == AudioSystem::FORCE_NONE))) &&
3255f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        streamDesc.mCanBeMuted) {
3256f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        volume *= SONIFICATION_HEADSET_VOLUME_FACTOR;
3257f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // when the phone is ringing we must consider that music could have been paused just before
3258f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // by the music application and behave as if music was active if the last music track was
3259f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // just stopped
3260ac3cf10ef6de12e69540a1244ac7255f93fa7502Eric Laurent        if (isStreamActive(AudioSystem::MUSIC, SONIFICATION_HEADSET_MUSIC_DELAY) ||
3261ac3cf10ef6de12e69540a1244ac7255f93fa7502Eric Laurent                mLimitRingtoneVolume) {
326217a73c3394547692457299dc512b5c2312ea0344Eric Laurent            audio_devices_t musicDevice = getDeviceForStrategy(STRATEGY_MEDIA, true /*fromCache*/);
3263c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            float musicVol = computeVolume(AudioSystem::MUSIC,
326417a73c3394547692457299dc512b5c2312ea0344Eric Laurent                               mStreams[AudioSystem::MUSIC].getVolumeIndex(musicDevice),
3265c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                               output,
326617a73c3394547692457299dc512b5c2312ea0344Eric Laurent                               musicDevice);
3267c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            float minVol = (musicVol > SONIFICATION_HEADSET_VOLUME_MIN) ?
3268c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                musicVol : SONIFICATION_HEADSET_VOLUME_MIN;
3269f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (volume > minVol) {
3270f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                volume = minVol;
32716a70518b93928d1c91457ff805e375c82d76b0e5Steve Block                ALOGV("computeVolume limiting volume to %f musicVol %f", minVol, musicVol);
3272f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
3273f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
3274f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3275f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3276f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return volume;
3277f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3278f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3279c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentstatus_t AudioPolicyManagerBase::checkAndSetVolume(int stream,
3280c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                   int index,
3281c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                   audio_io_handle_t output,
3282f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                                                   audio_devices_t device,
3283c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                   int delayMs,
3284c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                   bool force)
3285f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3286f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3287f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // do not change actual stream volume if the stream is muted
3288f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (mOutputs.valueFor(output)->mMuteCount[stream] != 0) {
32893cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        ALOGVV("checkAndSetVolume() stream %d muted count %d",
32905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent              stream, mOutputs.valueFor(output)->mMuteCount[stream]);
3291f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return NO_ERROR;
3292f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3293f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3294f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // do not change in call volume if bluetooth is connected and vice versa
3295f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if ((stream == AudioSystem::VOICE_CALL && mForceUse[AudioSystem::FOR_COMMUNICATION] == AudioSystem::FORCE_BT_SCO) ||
3296f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        (stream == AudioSystem::BLUETOOTH_SCO && mForceUse[AudioSystem::FOR_COMMUNICATION] != AudioSystem::FORCE_BT_SCO)) {
32976a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("checkAndSetVolume() cannot set stream %d volume with force use = %d for comm",
3298f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin             stream, mForceUse[AudioSystem::FOR_COMMUNICATION]);
3299f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return INVALID_OPERATION;
3300f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3301f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3302f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    float volume = computeVolume(stream, index, output, device);
3303f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // We actually change the volume if:
3304f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // - the float value returned by computeVolume() changed
3305f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // - the force flag is set
3306f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (volume != mOutputs.valueFor(output)->mCurVolume[stream] ||
3307f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            force) {
3308f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mOutputs.valueFor(output)->mCurVolume[stream] = volume;
33093cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        ALOGVV("checkAndSetVolume() for output %d stream %d, volume %f, delay %d", output, stream, volume, delayMs);
33100d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent        // Force VOICE_CALL to track BLUETOOTH_SCO stream volume when bluetooth audio is
33110d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent        // enabled
33120d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent        if (stream == AudioSystem::BLUETOOTH_SCO) {
33130d6490a58bf607f1efc00638709f9c4c8f7dd270Eric Laurent            mpClientInterface->setStreamVolume(AudioSystem::VOICE_CALL, volume, output, delayMs);
3314f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
3315f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mpClientInterface->setStreamVolume((AudioSystem::stream_type)stream, volume, output, delayMs);
3316f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3317f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3318f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (stream == AudioSystem::VOICE_CALL ||
3319f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        stream == AudioSystem::BLUETOOTH_SCO) {
3320f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        float voiceVolume;
3321f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // Force voice volume to max for bluetooth SCO as volume is managed by the headset
3322f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (stream == AudioSystem::VOICE_CALL) {
3323f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            voiceVolume = (float)index/(float)mStreams[stream].mIndexMax;
3324f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        } else {
3325f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            voiceVolume = 1.0;
3326f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
3327f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3328b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        if (voiceVolume != mLastVoiceVolume && output == mPrimaryOutput) {
3329f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mpClientInterface->setVoiceVolume(voiceVolume, delayMs);
3330f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            mLastVoiceVolume = voiceVolume;
3331f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
3332f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3333f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3334f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
3335f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3336f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3337c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentvoid AudioPolicyManagerBase::applyStreamVolumes(audio_io_handle_t output,
3338f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                                                audio_devices_t device,
3339c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                int delayMs,
3340c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                                                bool force)
3341f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
33423cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGVV("applyStreamVolumes() for output %d and device %x", output, device);
3343f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3344f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
3345c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        checkAndSetVolume(stream,
3346f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent                          mStreams[stream].getVolumeIndex(device),
3347c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                          output,
3348c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                          device,
3349c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                          delayMs,
3350c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                          force);
3351f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3352f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3353f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
335401e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurentvoid AudioPolicyManagerBase::setStrategyMute(routing_strategy strategy,
335501e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                             bool on,
335601e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                             audio_io_handle_t output,
335701e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                             int delayMs,
335801e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                             audio_devices_t device)
3359f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
33603cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGVV("setStrategyMute() strategy %d, mute %d, output %d", strategy, on, output);
3361f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int stream = 0; stream < AudioSystem::NUM_STREAM_TYPES; stream++) {
3362f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (getStrategy((AudioSystem::stream_type)stream) == strategy) {
336301e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent            setStreamMute(stream, on, output, delayMs, device);
3364f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
3365f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3366f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3367f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
336801e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurentvoid AudioPolicyManagerBase::setStreamMute(int stream,
336901e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                           bool on,
337001e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                           audio_io_handle_t output,
337101e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                           int delayMs,
337201e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent                                           audio_devices_t device)
3373f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3374f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    StreamDescriptor &streamDesc = mStreams[stream];
3375f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    AudioOutputDescriptor *outputDesc = mOutputs.valueFor(output);
3376ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if (device == AUDIO_DEVICE_NONE) {
337701e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent        device = outputDesc->device();
337801e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent    }
3379f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
33803cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGVV("setStreamMute() stream %d, mute %d, output %d, mMuteCount %d device %04x",
338101e6272f0a3a7d1d53e826012377ff9269b03b06Eric Laurent          stream, on, output, outputDesc->mMuteCount[stream], device);
3382f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3383f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (on) {
3384f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (outputDesc->mMuteCount[stream] == 0) {
3385738207def5f691d605ae33d041116829a74513a9Eric Laurent            if (streamDesc.mCanBeMuted &&
3386738207def5f691d605ae33d041116829a74513a9Eric Laurent                    ((stream != AudioSystem::ENFORCED_AUDIBLE) ||
3387738207def5f691d605ae33d041116829a74513a9Eric Laurent                     (mForceUse[AudioSystem::FOR_SYSTEM] == AudioSystem::FORCE_NONE))) {
3388c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                checkAndSetVolume(stream, 0, output, device, delayMs);
3389f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
3390f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
3391f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        // increment mMuteCount after calling checkAndSetVolume() so that volume change is not ignored
3392f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        outputDesc->mMuteCount[stream]++;
3393f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
3394f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (outputDesc->mMuteCount[stream] == 0) {
33953cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent            ALOGV("setStreamMute() unmuting non muted stream!");
3396f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            return;
3397f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
3398f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (--outputDesc->mMuteCount[stream] == 0) {
3399c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent            checkAndSetVolume(stream,
3400c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                              streamDesc.getVolumeIndex(device),
3401c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                              output,
3402c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                              device,
3403c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                              delayMs);
3404f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
3405f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3406f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3407f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3408f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::handleIncallSonification(int stream, bool starting, bool stateChange)
3409f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3410f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if the stream pertains to sonification strategy and we are in call we must
3411f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // mute the stream if it is low visibility. If it is high visibility, we must play a tone
3412f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // in the device used for phone strategy and play the tone if the selected device does not
3413f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // interfere with the device used for phone strategy
3414f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // if stateChange is true, we are called from setPhoneState() and we must mute or unmute as
3415f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // many times as there are active tracks on the output
341612bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    const routing_strategy stream_strategy = getStrategy((AudioSystem::stream_type)stream);
341712bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi    if ((stream_strategy == STRATEGY_SONIFICATION) ||
341812bd6e4a5c26bd8035bf804d0cc821bd9b8cce9bJean-Michel Trivi            ((stream_strategy == STRATEGY_SONIFICATION_RESPECTFUL))) {
3419b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        AudioOutputDescriptor *outputDesc = mOutputs.valueFor(mPrimaryOutput);
34206a70518b93928d1c91457ff805e375c82d76b0e5Steve Block        ALOGV("handleIncallSonification() stream %d starting %d device %x stateChange %d",
3421f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                stream, starting, outputDesc->mDevice, stateChange);
3422f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        if (outputDesc->mRefCount[stream]) {
3423f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            int muteCount = 1;
3424f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (stateChange) {
3425f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                muteCount = outputDesc->mRefCount[stream];
3426f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
3427f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            if (AudioSystem::isLowVisibility((AudioSystem::stream_type)stream)) {
34286a70518b93928d1c91457ff805e375c82d76b0e5Steve Block                ALOGV("handleIncallSonification() low visibility, muteCount %d", muteCount);
3429f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                for (int i = 0; i < muteCount; i++) {
3430b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                    setStreamMute(stream, starting, mPrimaryOutput);
3431f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
3432f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            } else {
34336a70518b93928d1c91457ff805e375c82d76b0e5Steve Block                ALOGV("handleIncallSonification() high visibility");
34345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                if (outputDesc->device() &
34355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                        getDeviceForStrategy(STRATEGY_PHONE, true /*fromCache*/)) {
34366a70518b93928d1c91457ff805e375c82d76b0e5Steve Block                    ALOGV("handleIncallSonification() high visibility muted, muteCount %d", muteCount);
3437f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    for (int i = 0; i < muteCount; i++) {
3438b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent                        setStreamMute(stream, starting, mPrimaryOutput);
3439f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    }
3440f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
3441f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                if (starting) {
3442f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    mpClientInterface->startTone(ToneGenerator::TONE_SUP_CALL_WAITING, AudioSystem::VOICE_CALL);
3443f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                } else {
3444f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                    mpClientInterface->stopTone();
3445f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin                }
3446f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            }
3447f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        }
3448f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3449f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3450f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3451f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinbool AudioPolicyManagerBase::isInCall()
3452f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3453f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return isStateInCall(mPhoneState);
3454f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3455f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3456f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinbool AudioPolicyManagerBase::isStateInCall(int state) {
3457f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return ((state == AudioSystem::MODE_IN_CALL) ||
3458f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin            (state == AudioSystem::MODE_IN_COMMUNICATION));
3459f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3460f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3461f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinuint32_t AudioPolicyManagerBase::getMaxEffectsCpuLoad()
3462f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3463f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return MAX_EFFECTS_CPU_LOAD;
3464f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3465f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3466f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinuint32_t AudioPolicyManagerBase::getMaxEffectsMemory()
3467f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3468f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return MAX_EFFECTS_MEMORY;
3469f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3470f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3471f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// --- AudioOutputDescriptor class implementation
3472f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3473b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric LaurentAudioPolicyManagerBase::AudioOutputDescriptor::AudioOutputDescriptor(
34745ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        const IOProfile *profile)
34752d749b0bda4ae60ef1a940eeba86121b2e94d07dGlenn Kasten    : mId(0), mSamplingRate(0), mFormat(AUDIO_FORMAT_DEFAULT),
34762c3d2379ee6eadb32b63bf6de538cfd6487e8e20Glenn Kasten      mChannelMask(0), mLatency(0),
3477ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    mFlags((audio_output_flags_t)0), mDevice(AUDIO_DEVICE_NONE),
34785a950c960cd40648cc3ab8defe817f88e0a7ecf4Eric Laurent    mOutput1(0), mOutput2(0), mProfile(profile), mDirectOpenCount(0)
3479f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3480f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // clear usage count for all stream types
3481f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) {
3482f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mRefCount[i] = 0;
3483f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mCurVolume[i] = -1.0;
3484f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mMuteCount[i] = 0;
3485f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mStopTime[i] = 0;
3486f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
348785ad78f27ca032e90af0f2100659d12c16322c76Marco Nelissen    for (int i = 0; i < NUM_STRATEGIES; i++) {
348885ad78f27ca032e90af0f2100659d12c16322c76Marco Nelissen        mStrategyMutedByDevice[i] = false;
348985ad78f27ca032e90af0f2100659d12c16322c76Marco Nelissen    }
34903cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    if (profile != NULL) {
34913cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        mSamplingRate = profile->mSamplingRates[0];
34923cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        mFormat = profile->mFormats[0];
34933cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        mChannelMask = profile->mChannelMasks[0];
34943cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        mFlags = profile->mFlags;
34953cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    }
3496f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3497f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3498dc3bf1a37425697277f6ed04fc8cfe4a52fd678aJean-Michel Triviaudio_devices_t AudioPolicyManagerBase::AudioOutputDescriptor::device() const
3499f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3500f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (isDuplicated()) {
3501f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent        return (audio_devices_t)(mOutput1->mDevice | mOutput2->mDevice);
3502f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    } else {
3503f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent        return mDevice;
3504f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3505f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3506f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
35075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentuint32_t AudioPolicyManagerBase::AudioOutputDescriptor::latency()
35085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
35095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (isDuplicated()) {
35105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return (mOutput1->mLatency > mOutput2->mLatency) ? mOutput1->mLatency : mOutput2->mLatency;
35115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    } else {
35125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return mLatency;
35135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
35145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
35155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
35165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentbool AudioPolicyManagerBase::AudioOutputDescriptor::sharesHwModuleWith(
35175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        const AudioOutputDescriptor *outputDesc)
35185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
35195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (isDuplicated()) {
35205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return mOutput1->sharesHwModuleWith(outputDesc) || mOutput2->sharesHwModuleWith(outputDesc);
35215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    } else if (outputDesc->isDuplicated()){
35225ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return sharesHwModuleWith(outputDesc->mOutput1) || sharesHwModuleWith(outputDesc->mOutput2);
35235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    } else {
352470c236c9290732782d5267935af1475b8d5ae602Eric Laurent        return (mProfile->mModule == outputDesc->mProfile->mModule);
35255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
35265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
35275ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
3528f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinvoid AudioPolicyManagerBase::AudioOutputDescriptor::changeRefCount(AudioSystem::stream_type stream, int delta)
3529f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3530f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    // forward usage count change to attached outputs
3531f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if (isDuplicated()) {
3532f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mOutput1->changeRefCount(stream, delta);
3533f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mOutput2->changeRefCount(stream, delta);
3534f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3535f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    if ((delta + (int)mRefCount[stream]) < 0) {
353664cca04dcbf4e21a51131224b9d0f0c596f876d4Steve Block        ALOGW("changeRefCount() invalid delta %d for stream %d, refCount %d", delta, stream, mRefCount[stream]);
3537f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        mRefCount[stream] = 0;
3538f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        return;
3539f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3540f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    mRefCount[stream] += delta;
35416a70518b93928d1c91457ff805e375c82d76b0e5Steve Block    ALOGV("changeRefCount() stream %d, count %d", stream, mRefCount[stream]);
3542f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3543f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3544f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurentaudio_devices_t AudioPolicyManagerBase::AudioOutputDescriptor::supportedDevices()
3545b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent{
3546b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    if (isDuplicated()) {
3547f9a4e2eccfbbe451512337af0806cfc54be0eaf9Eric Laurent        return (audio_devices_t)(mOutput1->supportedDevices() | mOutput2->supportedDevices());
3548b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    } else {
3549b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent        return mProfile->mSupportedDevices ;
3550b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent    }
3551b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent}
3552b4696fc22ba822ed37bd2e3a19bc17514ccc79c8Eric Laurent
355342fa8215a763822c0d03fc936e4cac1eb864c9ccEric Laurentbool AudioPolicyManagerBase::AudioOutputDescriptor::isActive(uint32_t inPastMs) const
355442fa8215a763822c0d03fc936e4cac1eb864c9ccEric Laurent{
355580f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    return isStrategyActive(NUM_STRATEGIES, inPastMs);
355680f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent}
355780f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent
355880f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurentbool AudioPolicyManagerBase::AudioOutputDescriptor::isStrategyActive(routing_strategy strategy,
355980f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                                                                       uint32_t inPastMs,
356080f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                                                                       nsecs_t sysTime) const
356180f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent{
356280f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    if ((sysTime == 0) && (inPastMs != 0)) {
356380f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent        sysTime = systemTime();
356480f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    }
356542fa8215a763822c0d03fc936e4cac1eb864c9ccEric Laurent    for (int i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) {
356680f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent        if (((getStrategy((AudioSystem::stream_type)i) == strategy) ||
356780f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                (NUM_STRATEGIES == strategy)) &&
356880f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                isStreamActive((AudioSystem::stream_type)i, inPastMs, sysTime)) {
356942fa8215a763822c0d03fc936e4cac1eb864c9ccEric Laurent            return true;
357042fa8215a763822c0d03fc936e4cac1eb864c9ccEric Laurent        }
357142fa8215a763822c0d03fc936e4cac1eb864c9ccEric Laurent    }
357242fa8215a763822c0d03fc936e4cac1eb864c9ccEric Laurent    return false;
357342fa8215a763822c0d03fc936e4cac1eb864c9ccEric Laurent}
357442fa8215a763822c0d03fc936e4cac1eb864c9ccEric Laurent
357580f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurentbool AudioPolicyManagerBase::AudioOutputDescriptor::isStreamActive(AudioSystem::stream_type stream,
357680f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                                                                       uint32_t inPastMs,
357780f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent                                                                       nsecs_t sysTime) const
357880f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent{
357980f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    if (mRefCount[stream] != 0) {
358080f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent        return true;
358180f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    }
358280f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    if (inPastMs == 0) {
358380f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent        return false;
358480f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    }
358580f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    if (sysTime == 0) {
358680f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent        sysTime = systemTime();
358780f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    }
358880f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    if (ns2ms(sysTime - mStopTime[stream]) < inPastMs) {
358980f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent        return true;
359080f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    }
359180f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent    return false;
359280f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent}
359380f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent
359480f5b0400f86d49e70aa1a793b34f34492f005bcEric Laurent
3595f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::AudioOutputDescriptor::dump(int fd)
3596f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3597f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    const size_t SIZE = 256;
3598f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    char buffer[SIZE];
3599f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    String8 result;
3600f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3601f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Sampling rate: %d\n", mSamplingRate);
3602f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3603a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    snprintf(buffer, SIZE, " Format: %08x\n", mFormat);
3604f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
360570c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, " Channels: %08x\n", mChannelMask);
3606f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3607f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Latency: %d\n", mLatency);
3608f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3609f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Flags %08x\n", mFlags);
3610f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3611f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Devices %08x\n", device());
3612f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3613f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Stream volume refCount muteCount\n");
3614f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3615f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    for (int i = 0; i < AudioSystem::NUM_STREAM_TYPES; i++) {
3616f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        snprintf(buffer, SIZE, " %02d     %.03f     %02d       %02d\n", i, mCurVolume[i], mRefCount[i], mMuteCount[i]);
3617f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin        result.append(buffer);
3618f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    }
3619f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, result.string(), result.size());
3620f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3621f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
3622f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3623f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3624f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// --- AudioInputDescriptor class implementation
3625f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
36265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric LaurentAudioPolicyManagerBase::AudioInputDescriptor::AudioInputDescriptor(const IOProfile *profile)
3627fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    :  mId(0), mSamplingRate(0), mFormat(AUDIO_FORMAT_DEFAULT), mChannelMask(0),
3628ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent      mDevice(AUDIO_DEVICE_NONE), mRefCount(0),
36295ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent      mInputSource(0), mProfile(profile)
3630f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3631fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    if (profile != NULL) {
3632fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean         mSamplingRate = profile->mSamplingRates[0];
3633fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean         mFormat = profile->mFormats[0];
3634fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean         mChannelMask = profile->mChannelMasks[0];
3635fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean     }
3636f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3637f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3638f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::AudioInputDescriptor::dump(int fd)
3639f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3640f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    const size_t SIZE = 256;
3641f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    char buffer[SIZE];
3642f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    String8 result;
3643f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3644f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Sampling rate: %d\n", mSamplingRate);
3645f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3646f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Format: %d\n", mFormat);
3647f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
364870c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, " Channels: %08x\n", mChannelMask);
3649f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3650f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Devices %08x\n", mDevice);
3651f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3652f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Ref Count %d\n", mRefCount);
3653f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3654f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, result.string(), result.size());
3655f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3656f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
3657f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3658f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3659f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// --- StreamDescriptor class implementation
3660f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3661c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric LaurentAudioPolicyManagerBase::StreamDescriptor::StreamDescriptor()
3662c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    :   mIndexMin(0), mIndexMax(1), mCanBeMuted(true)
3663c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent{
3664c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    mIndexCur.add(AUDIO_DEVICE_OUT_DEFAULT, 0);
3665c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent}
3666c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
3667c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentint AudioPolicyManagerBase::StreamDescriptor::getVolumeIndex(audio_devices_t device)
3668c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent{
3669c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    device = AudioPolicyManagerBase::getDeviceForVolume(device);
3670c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    // there is always a valid entry for AUDIO_DEVICE_OUT_DEFAULT
3671c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    if (mIndexCur.indexOfKey(device) < 0) {
3672c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        device = AUDIO_DEVICE_OUT_DEFAULT;
3673c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    }
3674c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    return mIndexCur.valueFor(device);
3675c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent}
3676c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
3677c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurentvoid AudioPolicyManagerBase::StreamDescriptor::dump(int fd)
3678f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3679c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    const size_t SIZE = 256;
3680c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    char buffer[SIZE];
3681c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    String8 result;
3682c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
3683c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    snprintf(buffer, SIZE, "%s         %02d         %02d         ",
3684c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent             mCanBeMuted ? "true " : "false", mIndexMin, mIndexMax);
3685c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    result.append(buffer);
3686c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    for (size_t i = 0; i < mIndexCur.size(); i++) {
3687c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        snprintf(buffer, SIZE, "%04x : %02d, ",
3688c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                 mIndexCur.keyAt(i),
3689c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent                 mIndexCur.valueAt(i));
3690c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent        result.append(buffer);
3691c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    }
3692c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    result.append("\n");
3693c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent
3694c6f331b3f48455c9a9cdf00fc82894badd0a7da6Eric Laurent    write(fd, result.string(), result.size());
3695f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3696f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3697f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin// --- EffectDescriptor class implementation
3698f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3699f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavinstatus_t AudioPolicyManagerBase::EffectDescriptor::dump(int fd)
3700f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin{
3701f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    const size_t SIZE = 256;
3702f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    char buffer[SIZE];
3703f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    String8 result;
3704f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
37051c65a49da0c89f75e528354ef02de5dc93a28232Eric Laurent    snprintf(buffer, SIZE, " I/O: %d\n", mIo);
3706f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3707f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Strategy: %d\n", mStrategy);
3708f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3709f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Session: %d\n", mSession);
3710f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3711f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    snprintf(buffer, SIZE, " Name: %s\n",  mDesc.name);
3712f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    result.append(buffer);
3713582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    snprintf(buffer, SIZE, " %s\n",  mEnabled ? "Enabled" : "Disabled");
3714582a15744be109fb630db7dbd1d1bdf22ff44f12Eric Laurent    result.append(buffer);
3715f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    write(fd, result.string(), result.size());
3716f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
3717f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin    return NO_ERROR;
3718f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}
3719f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
37205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent// --- IOProfile class implementation
37215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
372270c236c9290732782d5267935af1475b8d5ae602Eric LaurentAudioPolicyManagerBase::HwModule::HwModule(const char *name)
372370c236c9290732782d5267935af1475b8d5ae602Eric Laurent    : mName(strndup(name, AUDIO_HARDWARE_MODULE_ID_MAX_LEN)), mHandle(0)
372470c236c9290732782d5267935af1475b8d5ae602Eric Laurent{
372570c236c9290732782d5267935af1475b8d5ae602Eric Laurent}
372670c236c9290732782d5267935af1475b8d5ae602Eric Laurent
372770c236c9290732782d5267935af1475b8d5ae602Eric LaurentAudioPolicyManagerBase::HwModule::~HwModule()
372870c236c9290732782d5267935af1475b8d5ae602Eric Laurent{
372970c236c9290732782d5267935af1475b8d5ae602Eric Laurent    for (size_t i = 0; i < mOutputProfiles.size(); i++) {
373070c236c9290732782d5267935af1475b8d5ae602Eric Laurent         delete mOutputProfiles[i];
373170c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
373270c236c9290732782d5267935af1475b8d5ae602Eric Laurent    for (size_t i = 0; i < mInputProfiles.size(); i++) {
373370c236c9290732782d5267935af1475b8d5ae602Eric Laurent         delete mInputProfiles[i];
373470c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
373570c236c9290732782d5267935af1475b8d5ae602Eric Laurent    free((void *)mName);
373670c236c9290732782d5267935af1475b8d5ae602Eric Laurent}
373770c236c9290732782d5267935af1475b8d5ae602Eric Laurent
373870c236c9290732782d5267935af1475b8d5ae602Eric Laurentvoid AudioPolicyManagerBase::HwModule::dump(int fd)
373970c236c9290732782d5267935af1475b8d5ae602Eric Laurent{
374070c236c9290732782d5267935af1475b8d5ae602Eric Laurent    const size_t SIZE = 256;
374170c236c9290732782d5267935af1475b8d5ae602Eric Laurent    char buffer[SIZE];
374270c236c9290732782d5267935af1475b8d5ae602Eric Laurent    String8 result;
374370c236c9290732782d5267935af1475b8d5ae602Eric Laurent
374470c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "  - name: %s\n", mName);
374570c236c9290732782d5267935af1475b8d5ae602Eric Laurent    result.append(buffer);
374670c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "  - handle: %d\n", mHandle);
374770c236c9290732782d5267935af1475b8d5ae602Eric Laurent    result.append(buffer);
374870c236c9290732782d5267935af1475b8d5ae602Eric Laurent    write(fd, result.string(), result.size());
374970c236c9290732782d5267935af1475b8d5ae602Eric Laurent    if (mOutputProfiles.size()) {
37504387d30660db3c9a10b16b3a4a1c986c330e788bPatrick Tjin        write(fd, "  - outputs:\n", strlen("  - outputs:\n"));
375170c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (size_t i = 0; i < mOutputProfiles.size(); i++) {
3752c7b6e3c857c5f5af5f244d0b877c41a9c55f92abColin Cross            snprintf(buffer, SIZE, "    output %zu:\n", i);
3753599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            write(fd, buffer, strlen(buffer));
375470c236c9290732782d5267935af1475b8d5ae602Eric Laurent            mOutputProfiles[i]->dump(fd);
375570c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
375670c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
375770c236c9290732782d5267935af1475b8d5ae602Eric Laurent    if (mInputProfiles.size()) {
37584387d30660db3c9a10b16b3a4a1c986c330e788bPatrick Tjin        write(fd, "  - inputs:\n", strlen("  - inputs:\n"));
375970c236c9290732782d5267935af1475b8d5ae602Eric Laurent        for (size_t i = 0; i < mInputProfiles.size(); i++) {
3760c7b6e3c857c5f5af5f244d0b877c41a9c55f92abColin Cross            snprintf(buffer, SIZE, "    input %zu:\n", i);
3761599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            write(fd, buffer, strlen(buffer));
376270c236c9290732782d5267935af1475b8d5ae602Eric Laurent            mInputProfiles[i]->dump(fd);
376370c236c9290732782d5267935af1475b8d5ae602Eric Laurent        }
376470c236c9290732782d5267935af1475b8d5ae602Eric Laurent    }
376570c236c9290732782d5267935af1475b8d5ae602Eric Laurent}
376670c236c9290732782d5267935af1475b8d5ae602Eric Laurent
376770c236c9290732782d5267935af1475b8d5ae602Eric LaurentAudioPolicyManagerBase::IOProfile::IOProfile(HwModule *module)
37680977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent    : mFlags((audio_output_flags_t)0), mModule(module)
37695ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
37705ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
37715ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
37725ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric LaurentAudioPolicyManagerBase::IOProfile::~IOProfile()
37735ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
377470c236c9290732782d5267935af1475b8d5ae602Eric Laurent}
377570c236c9290732782d5267935af1475b8d5ae602Eric Laurent
3776b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent// checks if the IO profile is compatible with specified parameters.
3777b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent// Sampling rate, format and channel mask must be specified in order to
3778b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent// get a valid a match
377970c236c9290732782d5267935af1475b8d5ae602Eric Laurentbool AudioPolicyManagerBase::IOProfile::isCompatibleProfile(audio_devices_t device,
378070c236c9290732782d5267935af1475b8d5ae602Eric Laurent                                                            uint32_t samplingRate,
37815082dbeb19e37883230510129c94336063a4f91cGlenn Kasten                                                            audio_format_t format,
37822c3d2379ee6eadb32b63bf6de538cfd6487e8e20Glenn Kasten                                                            audio_channel_mask_t channelMask,
37830977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent                                                            audio_output_flags_t flags) const
378470c236c9290732782d5267935af1475b8d5ae602Eric Laurent{
37852d749b0bda4ae60ef1a940eeba86121b2e94d07dGlenn Kasten    if (samplingRate == 0 || !audio_is_valid_format(format) || channelMask == 0) {
3786b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent         return false;
3787b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     }
3788b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent
3789b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     if ((mSupportedDevices & device) != device) {
3790b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent         return false;
3791b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     }
3792b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     if ((mFlags & flags) != flags) {
3793b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent         return false;
3794b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     }
3795b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     size_t i;
3796b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     for (i = 0; i < mSamplingRates.size(); i++)
3797b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     {
3798b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent         if (mSamplingRates[i] == samplingRate) {
3799b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent             break;
3800b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent         }
3801b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     }
3802b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     if (i == mSamplingRates.size()) {
3803b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent         return false;
3804b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     }
3805b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     for (i = 0; i < mFormats.size(); i++)
3806b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     {
3807b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent         if (mFormats[i] == format) {
3808b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent             break;
3809b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent         }
3810b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     }
3811b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     if (i == mFormats.size()) {
3812b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent         return false;
3813b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     }
3814b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     for (i = 0; i < mChannelMasks.size(); i++)
3815b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     {
3816b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent         if (mChannelMasks[i] == channelMask) {
3817b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent             break;
3818b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent         }
3819b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     }
3820b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     if (i == mChannelMasks.size()) {
3821b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent         return false;
3822b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     }
3823b40b96a78537d63d801af7e706764c68acf8f182Eric Laurent     return true;
38245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
38255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
38265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::IOProfile::dump(int fd)
38275ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
38285ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    const size_t SIZE = 256;
38295ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    char buffer[SIZE];
38305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    String8 result;
38315ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
383270c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "    - sampling rates: ");
38335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    result.append(buffer);
38345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < mSamplingRates.size(); i++) {
38355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        snprintf(buffer, SIZE, "%d", mSamplingRates[i]);
38365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        result.append(buffer);
38375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        result.append(i == (mSamplingRates.size() - 1) ? "\n" : ", ");
38385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
38395ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
384070c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "    - channel masks: ");
38415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    result.append(buffer);
38425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < mChannelMasks.size(); i++) {
3843a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        snprintf(buffer, SIZE, "0x%04x", mChannelMasks[i]);
38445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        result.append(buffer);
38455ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        result.append(i == (mChannelMasks.size() - 1) ? "\n" : ", ");
38465ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
38475ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
384870c236c9290732782d5267935af1475b8d5ae602Eric Laurent    snprintf(buffer, SIZE, "    - formats: ");
38495ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    result.append(buffer);
38505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < mFormats.size(); i++) {
3851a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        snprintf(buffer, SIZE, "0x%08x", mFormats[i]);
38525ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        result.append(buffer);
38535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        result.append(i == (mFormats.size() - 1) ? "\n" : ", ");
38545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
38555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
3856a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    snprintf(buffer, SIZE, "    - devices: 0x%04x\n", mSupportedDevices);
38575ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    result.append(buffer);
3858a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    snprintf(buffer, SIZE, "    - flags: 0x%04x\n", mFlags);
38595ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    result.append(buffer);
38605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
38615ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    write(fd, result.string(), result.size());
38625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
38635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
3864fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLeanvoid AudioPolicyManagerBase::IOProfile::log()
3865fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean{
3866fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    const size_t SIZE = 256;
3867fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    char buffer[SIZE];
3868fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    String8 result;
3869fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
3870fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    ALOGV("    - sampling rates: ");
3871fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    for (size_t i = 0; i < mSamplingRates.size(); i++) {
3872fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        ALOGV("  %d", mSamplingRates[i]);
3873fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    }
3874fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
3875fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    ALOGV("    - channel masks: ");
3876fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    for (size_t i = 0; i < mChannelMasks.size(); i++) {
3877fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        ALOGV("  0x%04x", mChannelMasks[i]);
3878fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    }
3879fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
3880fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    ALOGV("    - formats: ");
3881fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    for (size_t i = 0; i < mFormats.size(); i++) {
3882fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        ALOGV("  0x%08x", mFormats[i]);
3883fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    }
3884fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
3885fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    ALOGV("    - devices: 0x%04x\n", mSupportedDevices);
3886fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean    ALOGV("    - flags: 0x%04x\n", mFlags);
3887fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean}
3888fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean
38895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent// --- audio_policy.conf file parsing
38905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
38915ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentstruct StringToEnum {
38925ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    const char *name;
38935ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    uint32_t value;
38945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent};
38955ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
38965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent#define STRING_TO_ENUM(string) { #string, string }
38975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
38985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
38995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentconst struct StringToEnum sDeviceNameToEnumTable[] = {
39005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_EARPIECE),
39015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_SPEAKER),
39025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_WIRED_HEADSET),
39035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_WIRED_HEADPHONE),
39045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_ALL_SCO),
39055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_ALL_A2DP),
39065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_AUX_DIGITAL),
39075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_DGTL_DOCK_HEADSET),
3908599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_ANLG_DOCK_HEADSET),
3909599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_USB_DEVICE),
3910599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_USB_ACCESSORY),
3911599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_OUT_ALL_USB),
391248387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi    STRING_TO_ENUM(AUDIO_DEVICE_OUT_REMOTE_SUBMIX),
39135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_BUILTIN_MIC),
39145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_BLUETOOTH_SCO_HEADSET),
39155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_WIRED_HEADSET),
39165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_AUX_DIGITAL),
39175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_VOICE_CALL),
39185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_BACK_MIC),
391948387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi    STRING_TO_ENUM(AUDIO_DEVICE_IN_REMOTE_SUBMIX),
3920ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_ANLG_DOCK_HEADSET),
3921ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_DGTL_DOCK_HEADSET),
3922ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    STRING_TO_ENUM(AUDIO_DEVICE_IN_USB_ACCESSORY),
392326101ada7b6a34cc517b92dec1e96b17fb4a996dPeter Yoon    STRING_TO_ENUM(AUDIO_DEVICE_IN_USB_DEVICE),
392433bf1b0fe363bd4892349d160f54d860567fab12Mike Lockwood    STRING_TO_ENUM(AUDIO_DEVICE_IN_BLUETOOTH_A2DP),
39255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent};
39265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
39275ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentconst struct StringToEnum sFlagNameToEnumTable[] = {
39280977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent    STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DIRECT),
39290977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent    STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_PRIMARY),
3930b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent    STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_FAST),
3931b2971bf2ae4e9da3155559aa134e3aa6c2b216a4Eric Laurent    STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_DEEP_BUFFER),
3932b4d07b97d23cfaffe22c7859ad7c45e168a7df0eRichard Fitzgerald    STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD),
3933b4d07b97d23cfaffe22c7859ad7c45e168a7df0eRichard Fitzgerald    STRING_TO_ENUM(AUDIO_OUTPUT_FLAG_NON_BLOCKING),
39345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent};
39355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
39365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentconst struct StringToEnum sFormatNameToEnumTable[] = {
39375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_FORMAT_PCM_16_BIT),
39385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_BIT),
39399df8ecc68bdaa90aaac86b9218d728f84586660eGlenn Kasten    STRING_TO_ENUM(AUDIO_FORMAT_PCM_32_BIT),
39409df8ecc68bdaa90aaac86b9218d728f84586660eGlenn Kasten    STRING_TO_ENUM(AUDIO_FORMAT_PCM_8_24_BIT),
39419df8ecc68bdaa90aaac86b9218d728f84586660eGlenn Kasten    STRING_TO_ENUM(AUDIO_FORMAT_PCM_FLOAT),
39429df8ecc68bdaa90aaac86b9218d728f84586660eGlenn Kasten    STRING_TO_ENUM(AUDIO_FORMAT_PCM_24_BIT_PACKED),
39435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_FORMAT_MP3),
39445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_FORMAT_AAC),
39455ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_FORMAT_VORBIS),
3946d97aa170de2e0d37aa4cf5305a72939ea7639d88Eric Laurent    STRING_TO_ENUM(AUDIO_FORMAT_HE_AAC_V1),
3947d97aa170de2e0d37aa4cf5305a72939ea7639d88Eric Laurent    STRING_TO_ENUM(AUDIO_FORMAT_HE_AAC_V2),
3948d97aa170de2e0d37aa4cf5305a72939ea7639d88Eric Laurent    STRING_TO_ENUM(AUDIO_FORMAT_OPUS),
3949d97aa170de2e0d37aa4cf5305a72939ea7639d88Eric Laurent    STRING_TO_ENUM(AUDIO_FORMAT_AC3),
3950d97aa170de2e0d37aa4cf5305a72939ea7639d88Eric Laurent    STRING_TO_ENUM(AUDIO_FORMAT_E_AC3),
39515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent};
39525ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
39535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentconst struct StringToEnum sOutChannelsNameToEnumTable[] = {
39545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_OUT_MONO),
39555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_OUT_STEREO),
39565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_OUT_5POINT1),
39575ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_OUT_7POINT1),
39585ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent};
39595ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
39605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentconst struct StringToEnum sInChannelsNameToEnumTable[] = {
39615ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_IN_MONO),
39625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_IN_STEREO),
396360758e27a4be8fc9ac1180f8a4055234e1702cc9Eric Laurent    STRING_TO_ENUM(AUDIO_CHANNEL_IN_FRONT_BACK),
39645ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent};
39655ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
39665ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
39675ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentuint32_t AudioPolicyManagerBase::stringToEnum(const struct StringToEnum *table,
39685ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                              size_t size,
39695ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                              const char *name)
39705ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
39715ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    for (size_t i = 0; i < size; i++) {
39725ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strcmp(table[i].name, name) == 0) {
39735ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("stringToEnum() found %s", table[i].name);
39745ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            return table[i].value;
39755ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
39765ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
39775ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return 0;
39785ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
39795ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
398018fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivibool AudioPolicyManagerBase::stringToBool(const char *value)
398118fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi{
398218fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi    return ((strcasecmp("true", value) == 0) || (strcmp("1", value) == 0));
398318fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi}
398418fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi
39850977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurentaudio_output_flags_t AudioPolicyManagerBase::parseFlagNames(char *name)
39865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
39875ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    uint32_t flag = 0;
39885ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
39895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // it is OK to cast name to non const here as we are not going to use it after
39905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    // strtok() modifies it
39915ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    char *flagName = strtok(name, "|");
39925ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (flagName != NULL) {
39935ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strlen(flagName) != 0) {
39945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            flag |= stringToEnum(sFlagNameToEnumTable,
39955ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                               ARRAY_SIZE(sFlagNameToEnumTable),
39965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                               flagName);
39975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
39985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        flagName = strtok(NULL, "|");
39995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
4000a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    //force direct flag if offload flag is set: offloading implies a direct output stream
4001a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    // and all common behaviors are driven by checking only the direct flag
4002a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    // this should normally be set appropriately in the policy configuration file
4003a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    if ((flag & AUDIO_OUTPUT_FLAG_COMPRESS_OFFLOAD) != 0) {
4004a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald        flag |= AUDIO_OUTPUT_FLAG_DIRECT;
4005a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald    }
4006a527ffd3c2ce72048a8fae51cd399024af4439b9Richard Fitzgerald
40070977cf534ffb71c2abac622716510ae8ea25f3e9Eric Laurent    return (audio_output_flags_t)flag;
40085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
40095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
40105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentaudio_devices_t AudioPolicyManagerBase::parseDeviceNames(char *name)
40115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
40125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    uint32_t device = 0;
40135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
40145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    char *devName = strtok(name, "|");
40155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (devName != NULL) {
40165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strlen(devName) != 0) {
40175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            device |= stringToEnum(sDeviceNameToEnumTable,
40185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                 ARRAY_SIZE(sDeviceNameToEnumTable),
40195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                 devName);
40205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
40215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        devName = strtok(NULL, "|");
40225ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
4023c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi    return device;
40245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
40255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
40265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadSamplingRates(char *name, IOProfile *profile)
40275ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
40285ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    char *str = strtok(name, "|");
40295ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
40303cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    // by convention, "0' in the first entry in mSamplingRates indicates the supported sampling
40313cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    // rates should be read from the output stream after it is opened for the first time
40323cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) {
40333cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        profile->mSamplingRates.add(0);
40343cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        return;
40353cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    }
40363cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
40375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (str != NULL) {
40385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        uint32_t rate = atoi(str);
40395ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (rate != 0) {
40405ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadSamplingRates() adding rate %d", rate);
40415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mSamplingRates.add(rate);
40425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
40435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        str = strtok(NULL, "|");
40445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
40455ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return;
40465ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
40475ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
40485ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadFormats(char *name, IOProfile *profile)
40495ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
40505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    char *str = strtok(name, "|");
40515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
40523cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    // by convention, "0' in the first entry in mFormats indicates the supported formats
40533cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    // should be read from the output stream after it is opened for the first time
40543cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) {
40552d749b0bda4ae60ef1a940eeba86121b2e94d07dGlenn Kasten        profile->mFormats.add(AUDIO_FORMAT_DEFAULT);
40563cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        return;
40573cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    }
40583cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
40595ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (str != NULL) {
40605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_format_t format = (audio_format_t)stringToEnum(sFormatNameToEnumTable,
40615ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                             ARRAY_SIZE(sFormatNameToEnumTable),
40625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                             str);
40632d749b0bda4ae60ef1a940eeba86121b2e94d07dGlenn Kasten        if (format != AUDIO_FORMAT_DEFAULT) {
40645ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mFormats.add(format);
40655ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
40665ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        str = strtok(NULL, "|");
40675ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
40685ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return;
40695ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
40705ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
40715ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadInChannels(char *name, IOProfile *profile)
40725ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
40735ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    const char *str = strtok(name, "|");
40745ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
40755ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGV("loadInChannels() %s", name);
40763cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
40773cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) {
40782c3d2379ee6eadb32b63bf6de538cfd6487e8e20Glenn Kasten        profile->mChannelMasks.add(0);
40793cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        return;
40803cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    }
40813cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
40825ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (str != NULL) {
40835ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        audio_channel_mask_t channelMask =
40845ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                (audio_channel_mask_t)stringToEnum(sInChannelsNameToEnumTable,
40855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                   ARRAY_SIZE(sInChannelsNameToEnumTable),
40865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                                   str);
40875ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (channelMask != 0) {
40885ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadInChannels() adding channelMask %04x", channelMask);
40895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mChannelMasks.add(channelMask);
40905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
40915ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        str = strtok(NULL, "|");
40925ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
40935ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return;
40945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
40955ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
40965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadOutChannels(char *name, IOProfile *profile)
40975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
40985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    const char *str = strtok(name, "|");
40993cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
41003cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    ALOGV("loadOutChannels() %s", name);
41013cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent
41023cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    // by convention, "0' in the first entry in mChannelMasks indicates the supported channel
41033cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    // masks should be read from the output stream after it is opened for the first time
41043cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    if (str != NULL && strcmp(str, DYNAMIC_VALUE_TAG) == 0) {
41052c3d2379ee6eadb32b63bf6de538cfd6487e8e20Glenn Kasten        profile->mChannelMasks.add(0);
41063cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        return;
41073cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent    }
41085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
41095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (str != NULL) {
41103cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent        audio_channel_mask_t channelMask =
41113cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                (audio_channel_mask_t)stringToEnum(sOutChannelsNameToEnumTable,
41123cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                   ARRAY_SIZE(sOutChannelsNameToEnumTable),
41133cdfddf1b27bcb5408ca3a04fcdf206447ba07baEric Laurent                                                   str);
41145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (channelMask != 0) {
41155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mChannelMasks.add(channelMask);
41165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
41175ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        str = strtok(NULL, "|");
41185ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
41195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return;
41205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
41215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
412270c236c9290732782d5267935af1475b8d5ae602Eric Laurentstatus_t AudioPolicyManagerBase::loadInput(cnode *root, HwModule *module)
41235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
41245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    cnode *node = root->first_child;
41255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
41265ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    IOProfile *profile = new IOProfile(module);
41275ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
41285ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (node) {
41295ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
41305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            loadSamplingRates((char *)node->value, profile);
41315ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, FORMATS_TAG) == 0) {
41325ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            loadFormats((char *)node->value, profile);
41335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, CHANNELS_TAG) == 0) {
41345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            loadInChannels((char *)node->value, profile);
41355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, DEVICES_TAG) == 0) {
41365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mSupportedDevices = parseDeviceNames((char *)node->value);
41375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
41385ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        node = node->next;
41395ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
4140ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    ALOGW_IF(profile->mSupportedDevices == AUDIO_DEVICE_NONE,
41415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadInput() invalid supported devices");
41425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGW_IF(profile->mChannelMasks.size() == 0,
41435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadInput() invalid supported channel masks");
41445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGW_IF(profile->mSamplingRates.size() == 0,
41455ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadInput() invalid supported sampling rates");
41465ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGW_IF(profile->mFormats.size() == 0,
41475ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadInput() invalid supported formats");
4148ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if ((profile->mSupportedDevices != AUDIO_DEVICE_NONE) &&
41495ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            (profile->mChannelMasks.size() != 0) &&
41505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            (profile->mSamplingRates.size() != 0) &&
41515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            (profile->mFormats.size() != 0)) {
41525ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
4153fd61179b6483f1c6360968d0f9f7695ea6f380a7Paul McLean        ALOGV("loadInput() adding input mSupportedDevices 0x%X", profile->mSupportedDevices);
41545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
415570c236c9290732782d5267935af1475b8d5ae602Eric Laurent        module->mInputProfiles.add(profile);
41565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return NO_ERROR;
41575ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    } else {
41585ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        delete profile;
41595ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return BAD_VALUE;
41605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
41615ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
41625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
416370c236c9290732782d5267935af1475b8d5ae602Eric Laurentstatus_t AudioPolicyManagerBase::loadOutput(cnode *root, HwModule *module)
41645ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
41655ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    cnode *node = root->first_child;
41665ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
41675ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    IOProfile *profile = new IOProfile(module);
41685ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
41695ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (node) {
41705ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strcmp(node->name, SAMPLING_RATES_TAG) == 0) {
41715ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            loadSamplingRates((char *)node->value, profile);
41725ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, FORMATS_TAG) == 0) {
41735ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            loadFormats((char *)node->value, profile);
41745ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, CHANNELS_TAG) == 0) {
41755ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            loadOutChannels((char *)node->value, profile);
41765ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, DEVICES_TAG) == 0) {
41775ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mSupportedDevices = parseDeviceNames((char *)node->value);
41785ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(node->name, FLAGS_TAG) == 0) {
41795ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            profile->mFlags = parseFlagNames((char *)node->value);
41805ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
41815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        node = node->next;
41825ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
4183ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    ALOGW_IF(profile->mSupportedDevices == AUDIO_DEVICE_NONE,
41845ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadOutput() invalid supported devices");
41855ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGW_IF(profile->mChannelMasks.size() == 0,
41865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadOutput() invalid supported channel masks");
41875ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGW_IF(profile->mSamplingRates.size() == 0,
41885ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadOutput() invalid supported sampling rates");
41895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    ALOGW_IF(profile->mFormats.size() == 0,
41905ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            "loadOutput() invalid supported formats");
4191ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    if ((profile->mSupportedDevices != AUDIO_DEVICE_NONE) &&
41925ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            (profile->mChannelMasks.size() != 0) &&
41935ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            (profile->mSamplingRates.size() != 0) &&
41945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            (profile->mFormats.size() != 0)) {
41955ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
41965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        ALOGV("loadOutput() adding output mSupportedDevices %04x, mFlags %04x",
41975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent              profile->mSupportedDevices, profile->mFlags);
41985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
419970c236c9290732782d5267935af1475b8d5ae602Eric Laurent        module->mOutputProfiles.add(profile);
42005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return NO_ERROR;
42015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    } else {
42025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        delete profile;
42035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return BAD_VALUE;
42045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
42055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
42065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
42075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadHwModule(cnode *root)
42085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
42095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    cnode *node = config_find(root, OUTPUTS_TAG);
42105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    status_t status = NAME_NOT_FOUND;
421170c236c9290732782d5267935af1475b8d5ae602Eric Laurent
421270c236c9290732782d5267935af1475b8d5ae602Eric Laurent    HwModule *module = new HwModule(root->name);
421370c236c9290732782d5267935af1475b8d5ae602Eric Laurent
42145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (node != NULL) {
42155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strcmp(root->name, AUDIO_HARDWARE_MODULE_ID_A2DP) == 0) {
42165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            mHasA2dp = true;
4217599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent        } else if (strcmp(root->name, AUDIO_HARDWARE_MODULE_ID_USB) == 0) {
4218599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent            mHasUsb = true;
421948387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi        } else if (strcmp(root->name, AUDIO_HARDWARE_MODULE_ID_REMOTE_SUBMIX) == 0) {
422048387b28c87327c6c4d512eabe091c29236d2e70Jean-Michel Trivi            mHasRemoteSubmix = true;
42215ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
4222599a1fc11596ac669499c1caf87c6ceddd0bfee4Eric Laurent
42235ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        node = node->first_child;
42245ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        while (node) {
42255ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadHwModule() loading output %s", node->name);
422670c236c9290732782d5267935af1475b8d5ae602Eric Laurent            status_t tmpStatus = loadOutput(node, module);
42275ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            if (status == NAME_NOT_FOUND || status == NO_ERROR) {
42285ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                status = tmpStatus;
42295ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
42305ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            node = node->next;
42315ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
42325ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
42335ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    node = config_find(root, INPUTS_TAG);
42345ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (node != NULL) {
42355ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        node = node->first_child;
42365ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        while (node) {
42375ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadHwModule() loading input %s", node->name);
423870c236c9290732782d5267935af1475b8d5ae602Eric Laurent            status_t tmpStatus = loadInput(node, module);
42395ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            if (status == NAME_NOT_FOUND || status == NO_ERROR) {
42405ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                status = tmpStatus;
42415ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            }
42425ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            node = node->next;
42435ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
42445ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
42455ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (status == NO_ERROR) {
424670c236c9290732782d5267935af1475b8d5ae602Eric Laurent        mHwModules.add(module);
424770c236c9290732782d5267935af1475b8d5ae602Eric Laurent    } else {
424870c236c9290732782d5267935af1475b8d5ae602Eric Laurent        delete module;
42495ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
42505ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
42515ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
42525ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadHwModules(cnode *root)
42535ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
42545ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    cnode *node = config_find(root, AUDIO_HW_MODULE_TAG);
42555ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (node == NULL) {
42565ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return;
42575ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
42585ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
42595ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    node = node->first_child;
42605ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (node) {
42615ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        ALOGV("loadHwModules() loading module %s", node->name);
42625ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        loadHwModule(node);
42635ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        node = node->next;
42645ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
42655ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
42665ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
42675ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentvoid AudioPolicyManagerBase::loadGlobalConfig(cnode *root)
42685ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
42695ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    cnode *node = config_find(root, GLOBAL_CONFIG_TAG);
42705ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (node == NULL) {
42715ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return;
42725ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
42735ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    node = node->first_child;
42745ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    while (node) {
42755ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        if (strcmp(ATTACHED_OUTPUT_DEVICES_TAG, node->name) == 0) {
42765ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            mAttachedOutputDevices = parseDeviceNames((char *)node->value);
4277ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            ALOGW_IF(mAttachedOutputDevices == AUDIO_DEVICE_NONE,
4278c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                    "loadGlobalConfig() no attached output devices");
42795ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadGlobalConfig() mAttachedOutputDevices %04x", mAttachedOutputDevices);
42805ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(DEFAULT_OUTPUT_DEVICE_TAG, node->name) == 0) {
42815ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            mDefaultOutputDevice = (audio_devices_t)stringToEnum(sDeviceNameToEnumTable,
42825ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                              ARRAY_SIZE(sDeviceNameToEnumTable),
42835ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent                                              (char *)node->value);
4284ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            ALOGW_IF(mDefaultOutputDevice == AUDIO_DEVICE_NONE,
4285c8101f5b14e745b348592c5609e55f121a11a6bfJean-Michel Trivi                    "loadGlobalConfig() default device not specified");
42865ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadGlobalConfig() mDefaultOutputDevice %04x", mDefaultOutputDevice);
42875ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        } else if (strcmp(ATTACHED_INPUT_DEVICES_TAG, node->name) == 0) {
4288ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent            mAvailableInputDevices = parseDeviceNames((char *)node->value) & ~AUDIO_DEVICE_BIT_IN;
42895ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent            ALOGV("loadGlobalConfig() mAvailableInputDevices %04x", mAvailableInputDevices);
429018fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi        } else if (strcmp(SPEAKER_DRC_ENABLED_TAG, node->name) == 0) {
429118fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi            mSpeakerDrcEnabled = stringToBool((char *)node->value);
429218fc094c0ed41851be3d746423c6695dd28d48e1Jean-Michel Trivi            ALOGV("loadGlobalConfig() mSpeakerDrcEnabled = %d", mSpeakerDrcEnabled);
42935ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        }
42945ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        node = node->next;
42955ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
42965ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
42975ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
42985ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurentstatus_t AudioPolicyManagerBase::loadAudioPolicyConfig(const char *path)
42995ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent{
43005ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    cnode *root;
43015ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    char *data;
43025ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
43035ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    data = (char *)load_file(path, NULL);
43045ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    if (data == NULL) {
43055ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent        return -ENODEV;
43065ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    }
43075ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    root = config_node("", "");
43085ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    config_load(root, data);
43095ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
43105ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    loadGlobalConfig(root);
43115ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    loadHwModules(root);
43125ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
43135ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    config_free(root);
43145ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    free(root);
43155ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    free(data);
43165ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent
43175ec145df7708564d385fd3fb764085321cf4c253Dima Zavin    ALOGI("loadAudioPolicyConfig() loaded %s\n", path);
43185ec145df7708564d385fd3fb764085321cf4c253Dima Zavin
43195ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent    return NO_ERROR;
43205ccdf14a85ed66ac54036fb393acc06ea7acfed6Eric Laurent}
4321f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
4322739022f26a7127ba76a98dda65411496086114a7Dima Zavinvoid AudioPolicyManagerBase::defaultAudioPolicyConfig(void)
4323739022f26a7127ba76a98dda65411496086114a7Dima Zavin{
4324739022f26a7127ba76a98dda65411496086114a7Dima Zavin    HwModule *module;
4325739022f26a7127ba76a98dda65411496086114a7Dima Zavin    IOProfile *profile;
4326739022f26a7127ba76a98dda65411496086114a7Dima Zavin
4327739022f26a7127ba76a98dda65411496086114a7Dima Zavin    mDefaultOutputDevice = AUDIO_DEVICE_OUT_SPEAKER;
4328739022f26a7127ba76a98dda65411496086114a7Dima Zavin    mAttachedOutputDevices = AUDIO_DEVICE_OUT_SPEAKER;
4329ed8f62d4faa53bbd53a358c5f494b653a09285e4Eric Laurent    mAvailableInputDevices = AUDIO_DEVICE_IN_BUILTIN_MIC & ~AUDIO_DEVICE_BIT_IN;
4330739022f26a7127ba76a98dda65411496086114a7Dima Zavin
4331739022f26a7127ba76a98dda65411496086114a7Dima Zavin    module = new HwModule("primary");
4332739022f26a7127ba76a98dda65411496086114a7Dima Zavin
4333739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile = new IOProfile(module);
4334739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mSamplingRates.add(44100);
4335739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
4336739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mChannelMasks.add(AUDIO_CHANNEL_OUT_STEREO);
4337739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mSupportedDevices = AUDIO_DEVICE_OUT_SPEAKER;
4338739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mFlags = AUDIO_OUTPUT_FLAG_PRIMARY;
4339739022f26a7127ba76a98dda65411496086114a7Dima Zavin    module->mOutputProfiles.add(profile);
4340739022f26a7127ba76a98dda65411496086114a7Dima Zavin
4341739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile = new IOProfile(module);
4342739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mSamplingRates.add(8000);
4343739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mFormats.add(AUDIO_FORMAT_PCM_16_BIT);
4344739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mChannelMasks.add(AUDIO_CHANNEL_IN_MONO);
4345739022f26a7127ba76a98dda65411496086114a7Dima Zavin    profile->mSupportedDevices = AUDIO_DEVICE_IN_BUILTIN_MIC;
4346739022f26a7127ba76a98dda65411496086114a7Dima Zavin    module->mInputProfiles.add(profile);
4347739022f26a7127ba76a98dda65411496086114a7Dima Zavin
4348739022f26a7127ba76a98dda65411496086114a7Dima Zavin    mHwModules.add(module);
4349739022f26a7127ba76a98dda65411496086114a7Dima Zavin}
4350f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin
4351f01215993dda68b6b52111d754bd0c7c2d5bcfa3Dima Zavin}; // namespace android
4352