AudioPolicyService.cpp revision 44deb053252a3bd2f57a007ab9560f4924f62394
165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian/*
265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian * Copyright (C) 2009 The Android Open Source Project
365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian *
465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian * Licensed under the Apache License, Version 2.0 (the "License");
565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian * you may not use this file except in compliance with the License.
665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian * You may obtain a copy of the License at
765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian *
865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian *      http://www.apache.org/licenses/LICENSE-2.0
965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian *
1065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian * Unless required by applicable law or agreed to in writing, software
1165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian * distributed under the License is distributed on an "AS IS" BASIS,
1265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian * See the License for the specific language governing permissions and
1465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian * limitations under the License.
1565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian */
1665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#define LOG_TAG "AudioPolicyService"
1865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian//#define LOG_NDEBUG 0
1965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
2065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#undef __STRICT_ANSI__
2165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#define __STDINT_LIMITS
2265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#define __STDC_LIMIT_MACROS
2365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <stdint.h>
2465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
2565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <sys/time.h>
2665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <binder/IServiceManager.h>
2765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <utils/Log.h>
2865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <cutils/properties.h>
2965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <binder/IPCThreadState.h>
3065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <utils/String16.h>
3165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <utils/threads.h>
3265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include "AudioPolicyService.h"
3344deb053252a3bd2f57a007ab9560f4924f62394Glenn Kasten#include "ServiceUtilities.h"
3465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <cutils/properties.h>
3565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <hardware_legacy/power.h>
367c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent#include <media/AudioEffect.h>
377c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent#include <media/EffectsFactoryApi.h>
3865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
39fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin#include <hardware/hardware.h>
4064760240f931714858a59c1579f07264d7182ba2Dima Zavin#include <system/audio.h>
417394a4f358fa9908a9f0a7c954b65c399f4268e6Dima Zavin#include <system/audio_policy.h>
42fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin#include <hardware/audio_policy.h>
437c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent#include <audio_effects/audio_effects_conf.h>
44fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
4565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopiannamespace android {
4665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
478dad0e31c2366c501bd1d600261d0af35a6ca786Glenn Kastenstatic const char kDeadlockedString[] = "AudioPolicyService may be deadlocked\n";
488dad0e31c2366c501bd1d600261d0af35a6ca786Glenn Kastenstatic const char kCmdDeadlockedString[] = "AudioPolicyService command thread may be deadlocked\n";
4965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
5065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatic const int kDumpLockRetries = 50;
5122ecc912a87099cff8cadc424cd12f85c118673fGlenn Kastenstatic const int kDumpLockSleepUs = 20000;
5265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
53fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavinnamespace {
54fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    extern struct audio_policy_service_ops aps_ops;
55fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin};
56fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
5765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// ----------------------------------------------------------------------------
5865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
5965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias AgopianAudioPolicyService::AudioPolicyService()
60fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    : BnAudioPolicyService() , mpAudioPolicyDev(NULL) , mpAudioPolicy(NULL)
6165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
6265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    char value[PROPERTY_VALUE_MAX];
63fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    const struct hw_module_t *module;
64fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    int forced_val;
65fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    int rc;
6665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
67935752053ef2691dbb6d5a6d149e0e362c6e3c74Eric Laurent    Mutex::Autolock _l(mLock);
68935752053ef2691dbb6d5a6d149e0e362c6e3c74Eric Laurent
6965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // start tone playback thread
7065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mTonePlaybackThread = new AudioCommandThread(String8(""));
7165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // start audio commands thread
7265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mAudioCommandThread = new AudioCommandThread(String8("ApmCommandThread"));
7365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
74fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    /* instantiate the audio policy manager */
75fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    rc = hw_get_module(AUDIO_POLICY_HARDWARE_MODULE_ID, &module);
76fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    if (rc)
77fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        return;
7865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
79fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    rc = audio_policy_dev_open(module, &mpAudioPolicyDev);
8029357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block    ALOGE_IF(rc, "couldn't open audio policy device (%s)", strerror(-rc));
81fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    if (rc)
82fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        return;
83935752053ef2691dbb6d5a6d149e0e362c6e3c74Eric Laurent
84fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    rc = mpAudioPolicyDev->create_audio_policy(mpAudioPolicyDev, &aps_ops, this,
85fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                               &mpAudioPolicy);
8629357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block    ALOGE_IF(rc, "couldn't create audio policy (%s)", strerror(-rc));
87fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    if (rc)
88fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        return;
89fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
90fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    rc = mpAudioPolicy->init_check(mpAudioPolicy);
9129357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block    ALOGE_IF(rc, "couldn't init_check the audio policy (%s)", strerror(-rc));
92fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    if (rc)
93fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        return;
94fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
95fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    property_get("ro.camera.sound.forced", value, "0");
96fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    forced_val = strtol(value, NULL, 0);
97fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    mpAudioPolicy->set_can_mute_enforced_audible(mpAudioPolicy, !forced_val);
98fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
99df64d15042bbd5e0e4933ac49bf3c177dd94752cSteve Block    ALOGI("Loaded audio policy from %s (%s)", module->name, module->id);
1007c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent
1017c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    // load audio pre processing modules
1027c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    if (access(AUDIO_EFFECT_VENDOR_CONFIG_FILE, R_OK) == 0) {
1037c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        loadPreProcessorConfig(AUDIO_EFFECT_VENDOR_CONFIG_FILE);
1047c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    } else if (access(AUDIO_EFFECT_DEFAULT_CONFIG_FILE, R_OK) == 0) {
1057c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        loadPreProcessorConfig(AUDIO_EFFECT_DEFAULT_CONFIG_FILE);
1067c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    }
10765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
10865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
10965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias AgopianAudioPolicyService::~AudioPolicyService()
11065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
11165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mTonePlaybackThread->exit();
11265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mTonePlaybackThread.clear();
11365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mAudioCommandThread->exit();
11465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mAudioCommandThread.clear();
11565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1167c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent
1177c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    // release audio pre processing resources
1187c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    for (size_t i = 0; i < mInputSources.size(); i++) {
1197c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        InputSourceDesc *source = mInputSources.valueAt(i);
1207c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        Vector <EffectDesc *> effects = source->mEffects;
1217c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        for (size_t j = 0; j < effects.size(); j++) {
1227c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            delete effects[j]->mName;
1237c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            Vector <effect_param_t *> params = effects[j]->mParams;
1247c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            for (size_t k = 0; k < params.size(); k++) {
1257c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent                delete params[k];
1267c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            }
1277c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            params.clear();
1287c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            delete effects[j];
1297c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        }
1307c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        effects.clear();
1317c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        delete source;
1327c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    }
1337c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    mInputSources.clear();
1347c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent
1357c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    for (size_t i = 0; i < mInputs.size(); i++) {
1367c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        mInputs.valueAt(i)->mEffects.clear();
1377c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        delete mInputs.valueAt(i);
1387c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    }
1397c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    mInputs.clear();
1407c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent
141a0d68338a88c2ddb4502f95017b546d603ef1ec7Glenn Kasten    if (mpAudioPolicy != NULL && mpAudioPolicyDev != NULL)
142fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        mpAudioPolicyDev->destroy_audio_policy(mpAudioPolicyDev, mpAudioPolicy);
143a0d68338a88c2ddb4502f95017b546d603ef1ec7Glenn Kasten    if (mpAudioPolicyDev != NULL)
144fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        audio_policy_dev_close(mpAudioPolicyDev);
14565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
14665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
147fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavinstatus_t AudioPolicyService::setDeviceConnectionState(audio_devices_t device,
148fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                                  audio_policy_dev_state_t state,
14965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                                                  const char *device_address)
15065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
151fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    if (mpAudioPolicy == NULL) {
15265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return NO_INIT;
15365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
15444deb053252a3bd2f57a007ab9560f4924f62394Glenn Kasten    if (!settingsAllowed()) {
15565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return PERMISSION_DENIED;
15665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
157fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    if (!audio_is_output_device(device) && !audio_is_input_device(device)) {
15865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return BAD_VALUE;
15965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
160fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    if (state != AUDIO_POLICY_DEVICE_STATE_AVAILABLE &&
161fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin            state != AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE) {
16265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return BAD_VALUE;
16365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
16465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1653856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("setDeviceConnectionState() tid %d", gettid());
16665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
167fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    return mpAudioPolicy->set_device_connection_state(mpAudioPolicy, device,
168fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                                      state, device_address);
16965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
17065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
171fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavinaudio_policy_dev_state_t AudioPolicyService::getDeviceConnectionState(
172fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                                              audio_devices_t device,
173de070137f11d346fba77605bd76a44c040a618fcEric Laurent                                                              const char *device_address)
17465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
175fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    if (mpAudioPolicy == NULL) {
176fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        return AUDIO_POLICY_DEVICE_STATE_UNAVAILABLE;
17765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
178fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    return mpAudioPolicy->get_device_connection_state(mpAudioPolicy, device,
179fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                                      device_address);
18065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
18165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
182f78aee70d15daf4690de7e7b4983ee68b0d1381dGlenn Kastenstatus_t AudioPolicyService::setPhoneState(audio_mode_t state)
18365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
184fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    if (mpAudioPolicy == NULL) {
18565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return NO_INIT;
18665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
18744deb053252a3bd2f57a007ab9560f4924f62394Glenn Kasten    if (!settingsAllowed()) {
18865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return PERMISSION_DENIED;
18965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
190930f4caa1e311ef7ff538c421a324396157eb24fGlenn Kasten    if (uint32_t(state) >= AUDIO_MODE_CNT) {
19165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return BAD_VALUE;
19265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
19365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1943856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("setPhoneState() tid %d", gettid());
19565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
19665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // TODO: check if it is more appropriate to do it in platform specific policy manager
19765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    AudioSystem::setMode(state);
19865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
19965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
200fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    mpAudioPolicy->set_phone_state(mpAudioPolicy, state);
20165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return NO_ERROR;
20265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
20365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
204fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavinstatus_t AudioPolicyService::setForceUse(audio_policy_force_use_t usage,
205fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                         audio_policy_forced_cfg_t config)
20665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
207fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    if (mpAudioPolicy == NULL) {
20865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return NO_INIT;
20965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
21044deb053252a3bd2f57a007ab9560f4924f62394Glenn Kasten    if (!settingsAllowed()) {
21165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return PERMISSION_DENIED;
21265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
213fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) {
21465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return BAD_VALUE;
21565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
216fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    if (config < 0 || config >= AUDIO_POLICY_FORCE_CFG_CNT) {
21765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return BAD_VALUE;
21865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
2193856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("setForceUse() tid %d", gettid());
22065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
221fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    mpAudioPolicy->set_force_use(mpAudioPolicy, usage, config);
22265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return NO_ERROR;
22365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
22465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
225fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavinaudio_policy_forced_cfg_t AudioPolicyService::getForceUse(audio_policy_force_use_t usage)
22665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
227fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    if (mpAudioPolicy == NULL) {
228fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        return AUDIO_POLICY_FORCE_NONE;
22965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
230fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    if (usage < 0 || usage >= AUDIO_POLICY_FORCE_USE_CNT) {
231fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        return AUDIO_POLICY_FORCE_NONE;
23265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
233fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    return mpAudioPolicy->get_force_use(mpAudioPolicy, usage);
23465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
23565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
236fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavinaudio_io_handle_t AudioPolicyService::getOutput(audio_stream_type_t stream,
23765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                                    uint32_t samplingRate,
23858f30210ea540b6ce5aa6a46330cd3499483cb97Glenn Kasten                                    audio_format_t format,
23965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                                    uint32_t channels,
240fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                    audio_policy_output_flags_t flags)
24165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
242fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    if (mpAudioPolicy == NULL) {
24365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return 0;
24465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
2453856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("getOutput() tid %d", gettid());
24665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
247fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    return mpAudioPolicy->get_output(mpAudioPolicy, stream, samplingRate, format, channels, flags);
24865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
24965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
250de070137f11d346fba77605bd76a44c040a618fcEric Laurentstatus_t AudioPolicyService::startOutput(audio_io_handle_t output,
251fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                         audio_stream_type_t stream,
252de070137f11d346fba77605bd76a44c040a618fcEric Laurent                                         int session)
25365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
254fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    if (mpAudioPolicy == NULL) {
25565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return NO_INIT;
25665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
2573856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("startOutput() tid %d", gettid());
25865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
259fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    return mpAudioPolicy->start_output(mpAudioPolicy, output, stream, session);
26065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
26165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
262de070137f11d346fba77605bd76a44c040a618fcEric Laurentstatus_t AudioPolicyService::stopOutput(audio_io_handle_t output,
263fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                        audio_stream_type_t stream,
264de070137f11d346fba77605bd76a44c040a618fcEric Laurent                                        int session)
26565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
266fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    if (mpAudioPolicy == NULL) {
26765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return NO_INIT;
26865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
2693856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("stopOutput() tid %d", gettid());
27065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
271fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    return mpAudioPolicy->stop_output(mpAudioPolicy, output, stream, session);
27265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
27365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
27465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid AudioPolicyService::releaseOutput(audio_io_handle_t output)
27565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
276fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    if (mpAudioPolicy == NULL) {
27765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return;
27865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
2793856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("releaseOutput() tid %d", gettid());
28065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
281fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    mpAudioPolicy->release_output(mpAudioPolicy, output);
28265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
28365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
284eba51fb3a361f67a6a64d5a16eba6084fe27d60eGlenn Kastenaudio_io_handle_t AudioPolicyService::getInput(audio_source_t inputSource,
28565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                                    uint32_t samplingRate,
28658f30210ea540b6ce5aa6a46330cd3499483cb97Glenn Kasten                                    audio_format_t format,
28765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                                    uint32_t channels,
2887c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent                                    audio_in_acoustics_t acoustics,
2897c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent                                    int audioSession)
29065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
291fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    if (mpAudioPolicy == NULL) {
29265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return 0;
29365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
294eba51fb3a361f67a6a64d5a16eba6084fe27d60eGlenn Kasten    // already checked by client, but double-check in case the client wrapper is bypassed
295eba51fb3a361f67a6a64d5a16eba6084fe27d60eGlenn Kasten    if (uint32_t(inputSource) >= AUDIO_SOURCE_CNT) {
296eba51fb3a361f67a6a64d5a16eba6084fe27d60eGlenn Kasten        return 0;
297eba51fb3a361f67a6a64d5a16eba6084fe27d60eGlenn Kasten    }
29865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
2997c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    audio_io_handle_t input = mpAudioPolicy->get_input(mpAudioPolicy, inputSource, samplingRate,
3007c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent                                                       format, channels, acoustics);
3017c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent
3027c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    if (input == 0) {
3037c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        return input;
3047c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    }
3057c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    // create audio pre processors according to input source
306eba51fb3a361f67a6a64d5a16eba6084fe27d60eGlenn Kasten    ssize_t index = mInputSources.indexOfKey(inputSource);
3077c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    if (index < 0) {
3087c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        return input;
3097c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    }
3107c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    ssize_t idx = mInputs.indexOfKey(input);
3117c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    InputDesc *inputDesc;
3127c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    if (idx < 0) {
3137c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        inputDesc = new InputDesc();
3147c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        inputDesc->mSessionId = audioSession;
3157c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        mInputs.add(input, inputDesc);
3167c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    } else {
3177c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        inputDesc = mInputs.valueAt(idx);
3187c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    }
3197c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent
3207c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    Vector <EffectDesc *> effects = mInputSources.valueAt(index)->mEffects;
3217c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    for (size_t i = 0; i < effects.size(); i++) {
3227c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        EffectDesc *effect = effects[i];
3237c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        sp<AudioEffect> fx = new AudioEffect(NULL, &effect->mUuid, -1, 0, 0, audioSession, input);
3247c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        status_t status = fx->initCheck();
3257c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        if (status != NO_ERROR && status != ALREADY_EXISTS) {
3265ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block            ALOGW("Failed to create Fx %s on input %d", effect->mName, input);
3277c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            // fx goes out of scope and strong ref on AudioEffect is released
3287c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            continue;
3297c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        }
3307c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        for (size_t j = 0; j < effect->mParams.size(); j++) {
3317c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            fx->setParameter(effect->mParams[j]);
3327c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        }
3337c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        inputDesc->mEffects.add(fx);
3347c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    }
3357c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    setPreProcessorEnabled(inputDesc, true);
3367c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    return input;
33765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
33865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
33965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioPolicyService::startInput(audio_io_handle_t input)
34065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
341fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    if (mpAudioPolicy == NULL) {
34265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return NO_INIT;
34365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
34465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
3457c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent
346fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    return mpAudioPolicy->start_input(mpAudioPolicy, input);
34765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
34865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
34965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioPolicyService::stopInput(audio_io_handle_t input)
35065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
351fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    if (mpAudioPolicy == NULL) {
35265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return NO_INIT;
35365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
35465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
3557c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent
356fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    return mpAudioPolicy->stop_input(mpAudioPolicy, input);
35765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
35865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
35965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid AudioPolicyService::releaseInput(audio_io_handle_t input)
36065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
361fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    if (mpAudioPolicy == NULL) {
36265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return;
36365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
36465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
365fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    mpAudioPolicy->release_input(mpAudioPolicy, input);
3667c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent
3677c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    ssize_t index = mInputs.indexOfKey(input);
3687c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    if (index < 0) {
3697c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        return;
3707c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    }
3717c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    InputDesc *inputDesc = mInputs.valueAt(index);
3727c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    setPreProcessorEnabled(inputDesc, false);
3737c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    inputDesc->mEffects.clear();
3747c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    delete inputDesc;
3757c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    mInputs.removeItemsAt(index);
37665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
37765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
378fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavinstatus_t AudioPolicyService::initStreamVolume(audio_stream_type_t stream,
37965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                                            int indexMin,
38065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                                            int indexMax)
38165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
382fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    if (mpAudioPolicy == NULL) {
38365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return NO_INIT;
38465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
38544deb053252a3bd2f57a007ab9560f4924f62394Glenn Kasten    if (!settingsAllowed()) {
38665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return PERMISSION_DENIED;
38765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
388263709e7be37c7040aaef385bc5c9389a9b5f514Glenn Kasten    if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
38965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return BAD_VALUE;
39065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
391fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    mpAudioPolicy->init_stream_volume(mpAudioPolicy, stream, indexMin, indexMax);
39265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return NO_ERROR;
39365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
39465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
39583844cc2f95dc279015b47fd1e18c7cb4eabe9a1Eric Laurentstatus_t AudioPolicyService::setStreamVolumeIndex(audio_stream_type_t stream,
39683844cc2f95dc279015b47fd1e18c7cb4eabe9a1Eric Laurent                                                  int index,
39783844cc2f95dc279015b47fd1e18c7cb4eabe9a1Eric Laurent                                                  audio_devices_t device)
39865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
399fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    if (mpAudioPolicy == NULL) {
40065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return NO_INIT;
40165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
40244deb053252a3bd2f57a007ab9560f4924f62394Glenn Kasten    if (!settingsAllowed()) {
40365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return PERMISSION_DENIED;
40465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
405263709e7be37c7040aaef385bc5c9389a9b5f514Glenn Kasten    if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
40665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return BAD_VALUE;
40765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
40865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
40983844cc2f95dc279015b47fd1e18c7cb4eabe9a1Eric Laurent    if (mpAudioPolicy->set_stream_volume_index_for_device) {
41083844cc2f95dc279015b47fd1e18c7cb4eabe9a1Eric Laurent        return mpAudioPolicy->set_stream_volume_index_for_device(mpAudioPolicy,
41183844cc2f95dc279015b47fd1e18c7cb4eabe9a1Eric Laurent                                                                stream,
41283844cc2f95dc279015b47fd1e18c7cb4eabe9a1Eric Laurent                                                                index,
41383844cc2f95dc279015b47fd1e18c7cb4eabe9a1Eric Laurent                                                                device);
41483844cc2f95dc279015b47fd1e18c7cb4eabe9a1Eric Laurent    } else {
41583844cc2f95dc279015b47fd1e18c7cb4eabe9a1Eric Laurent        return mpAudioPolicy->set_stream_volume_index(mpAudioPolicy, stream, index);
41683844cc2f95dc279015b47fd1e18c7cb4eabe9a1Eric Laurent    }
41765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
41865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
41983844cc2f95dc279015b47fd1e18c7cb4eabe9a1Eric Laurentstatus_t AudioPolicyService::getStreamVolumeIndex(audio_stream_type_t stream,
42083844cc2f95dc279015b47fd1e18c7cb4eabe9a1Eric Laurent                                                  int *index,
42183844cc2f95dc279015b47fd1e18c7cb4eabe9a1Eric Laurent                                                  audio_devices_t device)
42265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
423fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    if (mpAudioPolicy == NULL) {
42465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return NO_INIT;
42565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
426263709e7be37c7040aaef385bc5c9389a9b5f514Glenn Kasten    if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
42765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        return BAD_VALUE;
42865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
42983844cc2f95dc279015b47fd1e18c7cb4eabe9a1Eric Laurent    if (mpAudioPolicy->get_stream_volume_index_for_device) {
43083844cc2f95dc279015b47fd1e18c7cb4eabe9a1Eric Laurent        return mpAudioPolicy->get_stream_volume_index_for_device(mpAudioPolicy,
43183844cc2f95dc279015b47fd1e18c7cb4eabe9a1Eric Laurent                                                                stream,
43283844cc2f95dc279015b47fd1e18c7cb4eabe9a1Eric Laurent                                                                index,
43383844cc2f95dc279015b47fd1e18c7cb4eabe9a1Eric Laurent                                                                device);
43483844cc2f95dc279015b47fd1e18c7cb4eabe9a1Eric Laurent    } else {
43583844cc2f95dc279015b47fd1e18c7cb4eabe9a1Eric Laurent        return mpAudioPolicy->get_stream_volume_index(mpAudioPolicy, stream, index);
43683844cc2f95dc279015b47fd1e18c7cb4eabe9a1Eric Laurent    }
43765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
43865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
439fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavinuint32_t AudioPolicyService::getStrategyForStream(audio_stream_type_t stream)
440de070137f11d346fba77605bd76a44c040a618fcEric Laurent{
441fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    if (mpAudioPolicy == NULL) {
442de070137f11d346fba77605bd76a44c040a618fcEric Laurent        return 0;
443de070137f11d346fba77605bd76a44c040a618fcEric Laurent    }
444fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    return mpAudioPolicy->get_strategy_for_stream(mpAudioPolicy, stream);
445de070137f11d346fba77605bd76a44c040a618fcEric Laurent}
446de070137f11d346fba77605bd76a44c040a618fcEric Laurent
447fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavinuint32_t AudioPolicyService::getDevicesForStream(audio_stream_type_t stream)
4486b2718c67aa7b1a8e3b0f25a73a0d5f72c59ffc3Glenn Kasten{
449fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    if (mpAudioPolicy == NULL) {
4506b2718c67aa7b1a8e3b0f25a73a0d5f72c59ffc3Glenn Kasten        return 0;
4516b2718c67aa7b1a8e3b0f25a73a0d5f72c59ffc3Glenn Kasten    }
452fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    return mpAudioPolicy->get_devices_for_stream(mpAudioPolicy, stream);
4536b2718c67aa7b1a8e3b0f25a73a0d5f72c59ffc3Glenn Kasten}
4546b2718c67aa7b1a8e3b0f25a73a0d5f72c59ffc3Glenn Kasten
455de070137f11d346fba77605bd76a44c040a618fcEric Laurentaudio_io_handle_t AudioPolicyService::getOutputForEffect(effect_descriptor_t *desc)
456de070137f11d346fba77605bd76a44c040a618fcEric Laurent{
457fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    if (mpAudioPolicy == NULL) {
458de070137f11d346fba77605bd76a44c040a618fcEric Laurent        return NO_INIT;
459de070137f11d346fba77605bd76a44c040a618fcEric Laurent    }
460de070137f11d346fba77605bd76a44c040a618fcEric Laurent    Mutex::Autolock _l(mLock);
461fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    return mpAudioPolicy->get_output_for_effect(mpAudioPolicy, desc);
462de070137f11d346fba77605bd76a44c040a618fcEric Laurent}
463de070137f11d346fba77605bd76a44c040a618fcEric Laurent
464de070137f11d346fba77605bd76a44c040a618fcEric Laurentstatus_t AudioPolicyService::registerEffect(effect_descriptor_t *desc,
4657c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent                                audio_io_handle_t io,
466de070137f11d346fba77605bd76a44c040a618fcEric Laurent                                uint32_t strategy,
467de070137f11d346fba77605bd76a44c040a618fcEric Laurent                                int session,
468de070137f11d346fba77605bd76a44c040a618fcEric Laurent                                int id)
469de070137f11d346fba77605bd76a44c040a618fcEric Laurent{
470fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    if (mpAudioPolicy == NULL) {
471de070137f11d346fba77605bd76a44c040a618fcEric Laurent        return NO_INIT;
472de070137f11d346fba77605bd76a44c040a618fcEric Laurent    }
4737c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    return mpAudioPolicy->register_effect(mpAudioPolicy, desc, io, strategy, session, id);
474de070137f11d346fba77605bd76a44c040a618fcEric Laurent}
475de070137f11d346fba77605bd76a44c040a618fcEric Laurent
476de070137f11d346fba77605bd76a44c040a618fcEric Laurentstatus_t AudioPolicyService::unregisterEffect(int id)
477de070137f11d346fba77605bd76a44c040a618fcEric Laurent{
478fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    if (mpAudioPolicy == NULL) {
479de070137f11d346fba77605bd76a44c040a618fcEric Laurent        return NO_INIT;
480de070137f11d346fba77605bd76a44c040a618fcEric Laurent    }
481fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    return mpAudioPolicy->unregister_effect(mpAudioPolicy, id);
482de070137f11d346fba77605bd76a44c040a618fcEric Laurent}
483de070137f11d346fba77605bd76a44c040a618fcEric Laurent
484db7c079f284f6e91266f6653ae0ec198b1c5006eEric Laurentstatus_t AudioPolicyService::setEffectEnabled(int id, bool enabled)
485db7c079f284f6e91266f6653ae0ec198b1c5006eEric Laurent{
486db7c079f284f6e91266f6653ae0ec198b1c5006eEric Laurent    if (mpAudioPolicy == NULL) {
487db7c079f284f6e91266f6653ae0ec198b1c5006eEric Laurent        return NO_INIT;
488db7c079f284f6e91266f6653ae0ec198b1c5006eEric Laurent    }
489db7c079f284f6e91266f6653ae0ec198b1c5006eEric Laurent    return mpAudioPolicy->set_effect_enabled(mpAudioPolicy, id, enabled);
490db7c079f284f6e91266f6653ae0ec198b1c5006eEric Laurent}
491db7c079f284f6e91266f6653ae0ec198b1c5006eEric Laurent
492fff6d715a8db0daf08a50634f242c40268de3d49Glenn Kastenbool AudioPolicyService::isStreamActive(audio_stream_type_t stream, uint32_t inPastMs) const
493eda6c364c253ba97ee45a3adeb8c2b45db1f81dbEric Laurent{
494fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    if (mpAudioPolicy == NULL) {
495eda6c364c253ba97ee45a3adeb8c2b45db1f81dbEric Laurent        return 0;
496eda6c364c253ba97ee45a3adeb8c2b45db1f81dbEric Laurent    }
497eda6c364c253ba97ee45a3adeb8c2b45db1f81dbEric Laurent    Mutex::Autolock _l(mLock);
498fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    return mpAudioPolicy->is_stream_active(mpAudioPolicy, stream, inPastMs);
499eda6c364c253ba97ee45a3adeb8c2b45db1f81dbEric Laurent}
500eda6c364c253ba97ee45a3adeb8c2b45db1f81dbEric Laurent
50157dae99c9fcd6becd2b5ed0c53f277ba4d7dbcfcEric Laurentstatus_t AudioPolicyService::queryDefaultPreProcessing(int audioSession,
50257dae99c9fcd6becd2b5ed0c53f277ba4d7dbcfcEric Laurent                                                       effect_descriptor_t *descriptors,
50357dae99c9fcd6becd2b5ed0c53f277ba4d7dbcfcEric Laurent                                                       uint32_t *count)
50457dae99c9fcd6becd2b5ed0c53f277ba4d7dbcfcEric Laurent{
50557dae99c9fcd6becd2b5ed0c53f277ba4d7dbcfcEric Laurent
50657dae99c9fcd6becd2b5ed0c53f277ba4d7dbcfcEric Laurent    if (mpAudioPolicy == NULL) {
50757dae99c9fcd6becd2b5ed0c53f277ba4d7dbcfcEric Laurent        *count = 0;
50857dae99c9fcd6becd2b5ed0c53f277ba4d7dbcfcEric Laurent        return NO_INIT;
50957dae99c9fcd6becd2b5ed0c53f277ba4d7dbcfcEric Laurent    }
51057dae99c9fcd6becd2b5ed0c53f277ba4d7dbcfcEric Laurent    Mutex::Autolock _l(mLock);
51157dae99c9fcd6becd2b5ed0c53f277ba4d7dbcfcEric Laurent    status_t status = NO_ERROR;
51257dae99c9fcd6becd2b5ed0c53f277ba4d7dbcfcEric Laurent
51357dae99c9fcd6becd2b5ed0c53f277ba4d7dbcfcEric Laurent    size_t index;
51457dae99c9fcd6becd2b5ed0c53f277ba4d7dbcfcEric Laurent    for (index = 0; index < mInputs.size(); index++) {
51557dae99c9fcd6becd2b5ed0c53f277ba4d7dbcfcEric Laurent        if (mInputs.valueAt(index)->mSessionId == audioSession) {
51657dae99c9fcd6becd2b5ed0c53f277ba4d7dbcfcEric Laurent            break;
51757dae99c9fcd6becd2b5ed0c53f277ba4d7dbcfcEric Laurent        }
51857dae99c9fcd6becd2b5ed0c53f277ba4d7dbcfcEric Laurent    }
51957dae99c9fcd6becd2b5ed0c53f277ba4d7dbcfcEric Laurent    if (index == mInputs.size()) {
52057dae99c9fcd6becd2b5ed0c53f277ba4d7dbcfcEric Laurent        *count = 0;
52157dae99c9fcd6becd2b5ed0c53f277ba4d7dbcfcEric Laurent        return BAD_VALUE;
52257dae99c9fcd6becd2b5ed0c53f277ba4d7dbcfcEric Laurent    }
52357dae99c9fcd6becd2b5ed0c53f277ba4d7dbcfcEric Laurent    Vector< sp<AudioEffect> > effects = mInputs.valueAt(index)->mEffects;
52457dae99c9fcd6becd2b5ed0c53f277ba4d7dbcfcEric Laurent
52557dae99c9fcd6becd2b5ed0c53f277ba4d7dbcfcEric Laurent    for (size_t i = 0; i < effects.size(); i++) {
52657dae99c9fcd6becd2b5ed0c53f277ba4d7dbcfcEric Laurent        effect_descriptor_t desc = effects[i]->descriptor();
52757dae99c9fcd6becd2b5ed0c53f277ba4d7dbcfcEric Laurent        if (i < *count) {
52857dae99c9fcd6becd2b5ed0c53f277ba4d7dbcfcEric Laurent            memcpy(descriptors + i, &desc, sizeof(effect_descriptor_t));
52957dae99c9fcd6becd2b5ed0c53f277ba4d7dbcfcEric Laurent        }
53057dae99c9fcd6becd2b5ed0c53f277ba4d7dbcfcEric Laurent    }
53157dae99c9fcd6becd2b5ed0c53f277ba4d7dbcfcEric Laurent    if (effects.size() > *count) {
53257dae99c9fcd6becd2b5ed0c53f277ba4d7dbcfcEric Laurent        status = NO_MEMORY;
53357dae99c9fcd6becd2b5ed0c53f277ba4d7dbcfcEric Laurent    }
53457dae99c9fcd6becd2b5ed0c53f277ba4d7dbcfcEric Laurent    *count = effects.size();
53557dae99c9fcd6becd2b5ed0c53f277ba4d7dbcfcEric Laurent    return status;
53657dae99c9fcd6becd2b5ed0c53f277ba4d7dbcfcEric Laurent}
53757dae99c9fcd6becd2b5ed0c53f277ba4d7dbcfcEric Laurent
53865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid AudioPolicyService::binderDied(const wp<IBinder>& who) {
53923d82a9bc9a43b49ba684ba40875b91db310d3b9Glenn Kasten    ALOGW("binderDied() %p, tid %d, calling pid %d", who.unsafe_get(), gettid(),
540de070137f11d346fba77605bd76a44c040a618fcEric Laurent            IPCThreadState::self()->getCallingPid());
54165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
54265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
54365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatic bool tryLock(Mutex& mutex)
54465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
54565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    bool locked = false;
54665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    for (int i = 0; i < kDumpLockRetries; ++i) {
54765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (mutex.tryLock() == NO_ERROR) {
54865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            locked = true;
54965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            break;
55065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
55122ecc912a87099cff8cadc424cd12f85c118673fGlenn Kasten        usleep(kDumpLockSleepUs);
55265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
55365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return locked;
55465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
55565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
55665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioPolicyService::dumpInternals(int fd)
55765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
55865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    const size_t SIZE = 256;
55965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    char buffer[SIZE];
56065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    String8 result;
56165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
562fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    snprintf(buffer, SIZE, "PolicyManager Interface: %p\n", mpAudioPolicy);
56365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    result.append(buffer);
56465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
56565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    result.append(buffer);
56665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    snprintf(buffer, SIZE, "Tones Thread: %p\n", mTonePlaybackThread.get());
56765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    result.append(buffer);
56865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
56965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    write(fd, result.string(), result.size());
57065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return NO_ERROR;
57165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
57265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
57365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioPolicyService::dump(int fd, const Vector<String16>& args)
57465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
57544deb053252a3bd2f57a007ab9560f4924f62394Glenn Kasten    if (!dumpAllowed()) {
57665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        dumpPermissionDenial(fd);
57765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    } else {
57865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        bool locked = tryLock(mLock);
57965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (!locked) {
58065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            String8 result(kDeadlockedString);
58165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            write(fd, result.string(), result.size());
58265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
58365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
58465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        dumpInternals(fd);
5859d1f02d74fd395ec4de6861147da289423f0ab6fGlenn Kasten        if (mAudioCommandThread != 0) {
58665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            mAudioCommandThread->dump(fd);
58765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
5889d1f02d74fd395ec4de6861147da289423f0ab6fGlenn Kasten        if (mTonePlaybackThread != 0) {
58965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            mTonePlaybackThread->dump(fd);
59065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
59165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
592fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        if (mpAudioPolicy) {
593fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin            mpAudioPolicy->dump(mpAudioPolicy, fd);
59465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
59565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
59665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (locked) mLock.unlock();
59765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
59865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return NO_ERROR;
59965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
60065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
60165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioPolicyService::dumpPermissionDenial(int fd)
60265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
60365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    const size_t SIZE = 256;
60465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    char buffer[SIZE];
60565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    String8 result;
60665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    snprintf(buffer, SIZE, "Permission Denial: "
60765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            "can't dump AudioPolicyService from pid=%d, uid=%d\n",
60865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            IPCThreadState::self()->getCallingPid(),
60965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            IPCThreadState::self()->getCallingUid());
61065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    result.append(buffer);
61165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    write(fd, result.string(), result.size());
61265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return NO_ERROR;
61365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
61465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
6157c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurentvoid AudioPolicyService::setPreProcessorEnabled(InputDesc *inputDesc, bool enabled)
6167c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent{
6177c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    Vector<sp<AudioEffect> > fxVector = inputDesc->mEffects;
6187c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    for (size_t i = 0; i < fxVector.size(); i++) {
6197c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        sp<AudioEffect> fx = fxVector.itemAt(i);
6207c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        fx->setEnabled(enabled);
6217c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    }
6227c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent}
6237c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent
62465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioPolicyService::onTransact(
62565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
62665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
62765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return BnAudioPolicyService::onTransact(code, data, reply, flags);
62865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
62965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
63065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
63165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// -----------  AudioPolicyService::AudioCommandThread implementation ----------
63265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
63365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias AgopianAudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name)
63465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    : Thread(false), mName(name)
63565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
63665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mpToneGenerator = NULL;
63765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
63865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
63965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
64065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias AgopianAudioPolicyService::AudioCommandThread::~AudioCommandThread()
64165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
64265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (mName != "" && !mAudioCommands.isEmpty()) {
64365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        release_wake_lock(mName.string());
64465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
64565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mAudioCommands.clear();
646e9dd0176933d6233916c84e18f3e8c0d644ca05dGlenn Kasten    delete mpToneGenerator;
64765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
64865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
64965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid AudioPolicyService::AudioCommandThread::onFirstRef()
65065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
65165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (mName != "") {
65265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        run(mName.string(), ANDROID_PRIORITY_AUDIO);
65365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    } else {
65465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        run("AudioCommandThread", ANDROID_PRIORITY_AUDIO);
65565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
65665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
65765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
65865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianbool AudioPolicyService::AudioCommandThread::threadLoop()
65965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
66065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    nsecs_t waitTime = INT64_MAX;
66165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
66265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mLock.lock();
66365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    while (!exitPending())
66465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    {
66565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        while(!mAudioCommands.isEmpty()) {
66665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            nsecs_t curTime = systemTime();
66765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            // commands are sorted by increasing time stamp: execute them from index 0 and up
66865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            if (mAudioCommands[0]->mTime <= curTime) {
66965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                AudioCommand *command = mAudioCommands[0];
67065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                mAudioCommands.removeAt(0);
67165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                mLastCommand = *command;
67265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
67365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                switch (command->mCommand) {
67465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                case START_TONE: {
67565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    mLock.unlock();
67665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    ToneData *data = (ToneData *)command->mParam;
6773856b090cd04ba5dd4a59a12430ed724d5995909Steve Block                    ALOGV("AudioCommandThread() processing start tone %d on stream %d",
67865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                            data->mType, data->mStream);
679e9dd0176933d6233916c84e18f3e8c0d644ca05dGlenn Kasten                    delete mpToneGenerator;
68065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    mpToneGenerator = new ToneGenerator(data->mStream, 1.0);
68165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    mpToneGenerator->startTone(data->mType);
68265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    delete data;
68365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    mLock.lock();
68465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    }break;
68565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                case STOP_TONE: {
68665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    mLock.unlock();
6873856b090cd04ba5dd4a59a12430ed724d5995909Steve Block                    ALOGV("AudioCommandThread() processing stop tone");
68865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    if (mpToneGenerator != NULL) {
68965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                        mpToneGenerator->stopTone();
69065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                        delete mpToneGenerator;
69165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                        mpToneGenerator = NULL;
69265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    }
69365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    mLock.lock();
69465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    }break;
69565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                case SET_VOLUME: {
69665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    VolumeData *data = (VolumeData *)command->mParam;
6973856b090cd04ba5dd4a59a12430ed724d5995909Steve Block                    ALOGV("AudioCommandThread() processing set volume stream %d, \
698de070137f11d346fba77605bd76a44c040a618fcEric Laurent                            volume %f, output %d", data->mStream, data->mVolume, data->mIO);
699de070137f11d346fba77605bd76a44c040a618fcEric Laurent                    command->mStatus = AudioSystem::setStreamVolume(data->mStream,
700de070137f11d346fba77605bd76a44c040a618fcEric Laurent                                                                    data->mVolume,
701de070137f11d346fba77605bd76a44c040a618fcEric Laurent                                                                    data->mIO);
70265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    if (command->mWaitStatus) {
70365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                        command->mCond.signal();
70465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                        mWaitWorkCV.wait(mLock);
70565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    }
70665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    delete data;
70765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    }break;
70865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                case SET_PARAMETERS: {
70965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                     ParametersData *data = (ParametersData *)command->mParam;
7103856b090cd04ba5dd4a59a12430ed724d5995909Steve Block                     ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
711de070137f11d346fba77605bd76a44c040a618fcEric Laurent                             data->mKeyValuePairs.string(), data->mIO);
71265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                     command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
71365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                     if (command->mWaitStatus) {
71465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                         command->mCond.signal();
71565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                         mWaitWorkCV.wait(mLock);
71665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                     }
71765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                     delete data;
71865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                     }break;
71965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                case SET_VOICE_VOLUME: {
72065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    VoiceVolumeData *data = (VoiceVolumeData *)command->mParam;
7213856b090cd04ba5dd4a59a12430ed724d5995909Steve Block                    ALOGV("AudioCommandThread() processing set voice volume volume %f",
722de070137f11d346fba77605bd76a44c040a618fcEric Laurent                            data->mVolume);
72365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
72465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    if (command->mWaitStatus) {
72565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                        command->mCond.signal();
72665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                        mWaitWorkCV.wait(mLock);
72765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    }
72865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    delete data;
72965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    }break;
73065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                default:
7315ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block                    ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
73265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                }
73365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                delete command;
73465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                waitTime = INT64_MAX;
73565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            } else {
73665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                waitTime = mAudioCommands[0]->mTime - curTime;
73765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                break;
73865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            }
73965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
74065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        // release delayed commands wake lock
74165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (mName != "" && mAudioCommands.isEmpty()) {
74265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            release_wake_lock(mName.string());
74365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
7443856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("AudioCommandThread() going to sleep");
74565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        mWaitWorkCV.waitRelative(mLock, waitTime);
7463856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("AudioCommandThread() waking up");
74765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
74865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mLock.unlock();
74965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return false;
75065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
75165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
75265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioPolicyService::AudioCommandThread::dump(int fd)
75365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
75465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    const size_t SIZE = 256;
75565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    char buffer[SIZE];
75665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    String8 result;
75765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
75865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
75965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    result.append(buffer);
76065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    write(fd, result.string(), result.size());
76165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
76265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    bool locked = tryLock(mLock);
76365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (!locked) {
76465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        String8 result2(kCmdDeadlockedString);
76565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        write(fd, result2.string(), result2.size());
76665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
76765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
76865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    snprintf(buffer, SIZE, "- Commands:\n");
76965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    result = String8(buffer);
77065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    result.append("   Command Time        Wait pParam\n");
77165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    for (int i = 0; i < (int)mAudioCommands.size(); i++) {
77265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        mAudioCommands[i]->dump(buffer, SIZE);
77365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        result.append(buffer);
77465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
77565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    result.append("  Last Command\n");
77665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mLastCommand.dump(buffer, SIZE);
77765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    result.append(buffer);
77865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
77965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    write(fd, result.string(), result.size());
78065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
78165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (locked) mLock.unlock();
78265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
78365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return NO_ERROR;
78465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
78565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
7863d2f877c1cb4e4ae4ddde7f57f4353de9341f11bGlenn Kastenvoid AudioPolicyService::AudioCommandThread::startToneCommand(ToneGenerator::tone_type type,
7873d2f877c1cb4e4ae4ddde7f57f4353de9341f11bGlenn Kasten        audio_stream_type_t stream)
78865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
78965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    AudioCommand *command = new AudioCommand();
79065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    command->mCommand = START_TONE;
79165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    ToneData *data = new ToneData();
79265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    data->mType = type;
79365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    data->mStream = stream;
79465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    command->mParam = (void *)data;
79565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    command->mWaitStatus = false;
79665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
79765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    insertCommand_l(command);
7983856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream);
79965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mWaitWorkCV.signal();
80065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
80165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
80265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid AudioPolicyService::AudioCommandThread::stopToneCommand()
80365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
80465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    AudioCommand *command = new AudioCommand();
80565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    command->mCommand = STOP_TONE;
80665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    command->mParam = NULL;
80765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    command->mWaitStatus = false;
80865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
80965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    insertCommand_l(command);
8103856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("AudioCommandThread() adding tone stop");
81165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mWaitWorkCV.signal();
81265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
81365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
814fff6d715a8db0daf08a50634f242c40268de3d49Glenn Kastenstatus_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
815de070137f11d346fba77605bd76a44c040a618fcEric Laurent                                                               float volume,
81672ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten                                                               audio_io_handle_t output,
817de070137f11d346fba77605bd76a44c040a618fcEric Laurent                                                               int delayMs)
81865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
81965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    status_t status = NO_ERROR;
82065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
82165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    AudioCommand *command = new AudioCommand();
82265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    command->mCommand = SET_VOLUME;
82365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    VolumeData *data = new VolumeData();
82465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    data->mStream = stream;
82565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    data->mVolume = volume;
82665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    data->mIO = output;
82765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    command->mParam = data;
82865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (delayMs == 0) {
82965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        command->mWaitStatus = true;
83065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    } else {
83165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        command->mWaitStatus = false;
83265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
83365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
83465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    insertCommand_l(command, delayMs);
8353856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
836de070137f11d346fba77605bd76a44c040a618fcEric Laurent            stream, volume, output);
83765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mWaitWorkCV.signal();
83865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (command->mWaitStatus) {
83965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        command->mCond.wait(mLock);
84065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        status =  command->mStatus;
84165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        mWaitWorkCV.signal();
84265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
84365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return status;
84465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
84565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
84672ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenstatus_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
847fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                                                   const char *keyValuePairs,
848de070137f11d346fba77605bd76a44c040a618fcEric Laurent                                                                   int delayMs)
84965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
85065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    status_t status = NO_ERROR;
85165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
85265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    AudioCommand *command = new AudioCommand();
85365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    command->mCommand = SET_PARAMETERS;
85465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    ParametersData *data = new ParametersData();
85565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    data->mIO = ioHandle;
856fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    data->mKeyValuePairs = String8(keyValuePairs);
85765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    command->mParam = data;
85865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (delayMs == 0) {
85965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        command->mWaitStatus = true;
86065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    } else {
86165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        command->mWaitStatus = false;
86265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
86365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
86465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    insertCommand_l(command, delayMs);
8653856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
866fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin            keyValuePairs, ioHandle, delayMs);
86765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mWaitWorkCV.signal();
86865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (command->mWaitStatus) {
86965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        command->mCond.wait(mLock);
87065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        status =  command->mStatus;
87165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        mWaitWorkCV.signal();
87265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
87365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return status;
87465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
87565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
87665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
87765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
87865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    status_t status = NO_ERROR;
87965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
88065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    AudioCommand *command = new AudioCommand();
88165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    command->mCommand = SET_VOICE_VOLUME;
88265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    VoiceVolumeData *data = new VoiceVolumeData();
88365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    data->mVolume = volume;
88465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    command->mParam = data;
88565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (delayMs == 0) {
88665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        command->mWaitStatus = true;
88765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    } else {
88865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        command->mWaitStatus = false;
88965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
89065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Mutex::Autolock _l(mLock);
89165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    insertCommand_l(command, delayMs);
8923856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
89365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mWaitWorkCV.signal();
89465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (command->mWaitStatus) {
89565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        command->mCond.wait(mLock);
89665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        status =  command->mStatus;
89765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        mWaitWorkCV.signal();
89865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
89965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return status;
90065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
90165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
90265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// insertCommand_l() must be called with mLock held
90365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid AudioPolicyService::AudioCommandThread::insertCommand_l(AudioCommand *command, int delayMs)
90465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
90565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    ssize_t i;
90665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    Vector <AudioCommand *> removedCommands;
90765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
90865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    command->mTime = systemTime() + milliseconds(delayMs);
90965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
91065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // acquire wake lock to make sure delayed commands are processed
91165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (mName != "" && mAudioCommands.isEmpty()) {
91265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
91365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
91465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
91565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // check same pending commands with later time stamps and eliminate them
91665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    for (i = mAudioCommands.size()-1; i >= 0; i--) {
91765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        AudioCommand *command2 = mAudioCommands[i];
91865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
91965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (command2->mTime <= command->mTime) break;
92065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (command2->mCommand != command->mCommand) continue;
92165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
92265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        switch (command->mCommand) {
92365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        case SET_PARAMETERS: {
92465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            ParametersData *data = (ParametersData *)command->mParam;
92565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            ParametersData *data2 = (ParametersData *)command2->mParam;
92665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            if (data->mIO != data2->mIO) break;
9273856b090cd04ba5dd4a59a12430ed724d5995909Steve Block            ALOGV("Comparing parameter command %s to new command %s",
928de070137f11d346fba77605bd76a44c040a618fcEric Laurent                    data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
92965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            AudioParameter param = AudioParameter(data->mKeyValuePairs);
93065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
93165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            for (size_t j = 0; j < param.size(); j++) {
93265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian               String8 key;
93365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian               String8 value;
93465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian               param.getAt(j, key, value);
93565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian               for (size_t k = 0; k < param2.size(); k++) {
93665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                  String8 key2;
93765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                  String8 value2;
93865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                  param2.getAt(k, key2, value2);
93965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                  if (key2 == key) {
94065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                      param2.remove(key2);
9413856b090cd04ba5dd4a59a12430ed724d5995909Steve Block                      ALOGV("Filtering out parameter %s", key2.string());
94265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                      break;
94365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                  }
94465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian               }
94565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            }
94665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            // if all keys have been filtered out, remove the command.
94765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            // otherwise, update the key value pairs
94865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            if (param2.size() == 0) {
94965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                removedCommands.add(command2);
95065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            } else {
95165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                data2->mKeyValuePairs = param2.toString();
95265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            }
95365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        } break;
95465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
95565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        case SET_VOLUME: {
95665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            VolumeData *data = (VolumeData *)command->mParam;
95765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            VolumeData *data2 = (VolumeData *)command2->mParam;
95865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            if (data->mIO != data2->mIO) break;
95965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            if (data->mStream != data2->mStream) break;
9603856b090cd04ba5dd4a59a12430ed724d5995909Steve Block            ALOGV("Filtering out volume command on output %d for stream %d",
961de070137f11d346fba77605bd76a44c040a618fcEric Laurent                    data->mIO, data->mStream);
96265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            removedCommands.add(command2);
96365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        } break;
96465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        case START_TONE:
96565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        case STOP_TONE:
96665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        default:
96765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            break;
96865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
96965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
97065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
97165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // remove filtered commands
97265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    for (size_t j = 0; j < removedCommands.size(); j++) {
97365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        // removed commands always have time stamps greater than current command
97465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
97565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            if (mAudioCommands[k] == removedCommands[j]) {
9763856b090cd04ba5dd4a59a12430ed724d5995909Steve Block                ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
97765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                mAudioCommands.removeAt(k);
97865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                break;
97965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            }
98065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
98165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
98265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    removedCommands.clear();
98365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
98465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // insert command at the right place according to its time stamp
9853856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("inserting command: %d at index %d, num commands %d",
986de070137f11d346fba77605bd76a44c040a618fcEric Laurent            command->mCommand, (int)i+1, mAudioCommands.size());
98765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mAudioCommands.insertAt(command, i + 1);
98865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
98965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
99065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid AudioPolicyService::AudioCommandThread::exit()
99165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
9923856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("AudioCommandThread::exit");
99365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    {
99465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        AutoMutex _l(mLock);
99565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        requestExit();
99665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        mWaitWorkCV.signal();
99765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
99865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    requestExitAndWait();
99965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
100065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
100165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
100265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
100365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    snprintf(buffer, size, "   %02d      %06d.%03d  %01u    %p\n",
100465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            mCommand,
100565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            (int)ns2s(mTime),
100665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            (int)ns2ms(mTime)%1000,
100765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            mWaitStatus,
100865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            mParam);
100965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
101065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1011fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin/******* helpers for the service_ops callbacks defined below *********/
1012fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavinvoid AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
1013fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                       const char *keyValuePairs,
1014fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                       int delayMs)
1015fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin{
101672ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten    mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
1017fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                           delayMs);
1018fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin}
1019fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
1020fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavinint AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
1021fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                        float volume,
1022fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                        audio_io_handle_t output,
1023fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                        int delayMs)
1024fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin{
1025fff6d715a8db0daf08a50634f242c40268de3d49Glenn Kasten    return (int)mAudioCommandThread->volumeCommand(stream, volume,
102672ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten                                                   output, delayMs);
1027fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin}
1028fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
1029fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavinint AudioPolicyService::startTone(audio_policy_tone_t tone,
1030fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                  audio_stream_type_t stream)
1031fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin{
1032fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION)
103329357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block        ALOGE("startTone: illegal tone requested (%d)", tone);
1034fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    if (stream != AUDIO_STREAM_VOICE_CALL)
103529357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block        ALOGE("startTone: illegal stream (%d) requested for tone %d", stream,
1036fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin             tone);
1037fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING,
1038fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                          AUDIO_STREAM_VOICE_CALL);
1039fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    return 0;
1040fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin}
1041fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
1042fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavinint AudioPolicyService::stopTone()
1043fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin{
1044fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    mTonePlaybackThread->stopToneCommand();
1045fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    return 0;
1046fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin}
1047fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
1048fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavinint AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1049fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin{
1050fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1051fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin}
1052fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
10537c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent// ----------------------------------------------------------------------------
10547c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent// Audio pre-processing configuration
10557c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent// ----------------------------------------------------------------------------
10567c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent
10578dad0e31c2366c501bd1d600261d0af35a6ca786Glenn Kasten/*static*/ const char * const AudioPolicyService::kInputSourceNames[AUDIO_SOURCE_CNT -1] = {
10587c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    MIC_SRC_TAG,
10597c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    VOICE_UL_SRC_TAG,
10607c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    VOICE_DL_SRC_TAG,
10617c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    VOICE_CALL_SRC_TAG,
10627c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    CAMCORDER_SRC_TAG,
10637c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    VOICE_REC_SRC_TAG,
10647c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    VOICE_COMM_SRC_TAG
10657c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent};
10667c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent
10677c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent// returns the audio_source_t enum corresponding to the input source name or
10687c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent// AUDIO_SOURCE_CNT is no match found
10697c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurentaudio_source_t AudioPolicyService::inputSourceNameToEnum(const char *name)
10707c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent{
10717c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    int i;
10727c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    for (i = AUDIO_SOURCE_MIC; i < AUDIO_SOURCE_CNT; i++) {
10737c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        if (strcmp(name, kInputSourceNames[i - AUDIO_SOURCE_MIC]) == 0) {
10743856b090cd04ba5dd4a59a12430ed724d5995909Steve Block            ALOGV("inputSourceNameToEnum found source %s %d", name, i);
10757c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            break;
10767c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        }
10777c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    }
10787c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    return (audio_source_t)i;
10797c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent}
10807c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent
10817c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurentsize_t AudioPolicyService::growParamSize(char *param,
10827c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent                                         size_t size,
10837c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent                                         size_t *curSize,
10847c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent                                         size_t *totSize)
10857c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent{
10867c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    // *curSize is at least sizeof(effect_param_t) + 2 * sizeof(int)
10877c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    size_t pos = ((*curSize - 1 ) / size + 1) * size;
10887c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent
10897c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    if (pos + size > *totSize) {
10907c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        while (pos + size > *totSize) {
10917c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            *totSize += ((*totSize + 7) / 8) * 4;
10927c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        }
10937c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        param = (char *)realloc(param, *totSize);
10947c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    }
10957c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    *curSize = pos + size;
10967c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    return pos;
10977c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent}
10987c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent
10997c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurentsize_t AudioPolicyService::readParamValue(cnode *node,
11007c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent                                          char *param,
11017c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent                                          size_t *curSize,
11027c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent                                          size_t *totSize)
11037c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent{
11047c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    if (strncmp(node->name, SHORT_TAG, sizeof(SHORT_TAG) + 1) == 0) {
11057c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        size_t pos = growParamSize(param, sizeof(short), curSize, totSize);
11067c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        *(short *)((char *)param + pos) = (short)atoi(node->value);
11073856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("readParamValue() reading short %d", *(short *)((char *)param + pos));
11087c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        return sizeof(short);
11097c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    } else if (strncmp(node->name, INT_TAG, sizeof(INT_TAG) + 1) == 0) {
11107c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        size_t pos = growParamSize(param, sizeof(int), curSize, totSize);
11117c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        *(int *)((char *)param + pos) = atoi(node->value);
11123856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("readParamValue() reading int %d", *(int *)((char *)param + pos));
11137c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        return sizeof(int);
11147c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    } else if (strncmp(node->name, FLOAT_TAG, sizeof(FLOAT_TAG) + 1) == 0) {
11157c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        size_t pos = growParamSize(param, sizeof(float), curSize, totSize);
11167c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        *(float *)((char *)param + pos) = (float)atof(node->value);
11173856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("readParamValue() reading float %f",*(float *)((char *)param + pos));
11187c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        return sizeof(float);
11197c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    } else if (strncmp(node->name, BOOL_TAG, sizeof(BOOL_TAG) + 1) == 0) {
11207c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        size_t pos = growParamSize(param, sizeof(bool), curSize, totSize);
11217c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        if (strncmp(node->value, "false", strlen("false") + 1) == 0) {
11227c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            *(bool *)((char *)param + pos) = false;
11237c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        } else {
11247c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            *(bool *)((char *)param + pos) = true;
11257c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        }
11263856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("readParamValue() reading bool %s",*(bool *)((char *)param + pos) ? "true" : "false");
11277c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        return sizeof(bool);
11287c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    } else if (strncmp(node->name, STRING_TAG, sizeof(STRING_TAG) + 1) == 0) {
11297c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        size_t len = strnlen(node->value, EFFECT_STRING_LEN_MAX);
11307c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        if (*curSize + len + 1 > *totSize) {
11317c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            *totSize = *curSize + len + 1;
11327c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            param = (char *)realloc(param, *totSize);
11337c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        }
11347c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        strncpy(param + *curSize, node->value, len);
11357c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        *curSize += len;
11367c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        param[*curSize] = '\0';
11373856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("readParamValue() reading string %s", param + *curSize - len);
11387c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        return len;
11397c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    }
11405ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block    ALOGW("readParamValue() unknown param type %s", node->name);
11417c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    return 0;
11427c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent}
11437c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent
11447c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurenteffect_param_t *AudioPolicyService::loadEffectParameter(cnode *root)
11457c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent{
11467c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    cnode *param;
11477c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    cnode *value;
11487c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    size_t curSize = sizeof(effect_param_t);
11497c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    size_t totSize = sizeof(effect_param_t) + 2 * sizeof(int);
11507c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    effect_param_t *fx_param = (effect_param_t *)malloc(totSize);
11517c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent
11527c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    param = config_find(root, PARAM_TAG);
11537c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    value = config_find(root, VALUE_TAG);
11547c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    if (param == NULL && value == NULL) {
11557c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        // try to parse simple parameter form {int int}
11567c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        param = root->first_child;
1157a0d68338a88c2ddb4502f95017b546d603ef1ec7Glenn Kasten        if (param != NULL) {
11587c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            // Note: that a pair of random strings is read as 0 0
11597c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            int *ptr = (int *)fx_param->data;
11607c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            int *ptr2 = (int *)((char *)param + sizeof(effect_param_t));
11615ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block            ALOGW("loadEffectParameter() ptr %p ptr2 %p", ptr, ptr2);
11627c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            *ptr++ = atoi(param->name);
11637c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            *ptr = atoi(param->value);
11647c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            fx_param->psize = sizeof(int);
11657c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            fx_param->vsize = sizeof(int);
11667c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            return fx_param;
11677c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        }
11687c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    }
11697c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    if (param == NULL || value == NULL) {
11705ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block        ALOGW("loadEffectParameter() invalid parameter description %s", root->name);
11717c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        goto error;
11727c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    }
11737c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent
11747c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    fx_param->psize = 0;
11757c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    param = param->first_child;
11767c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    while (param) {
11773856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("loadEffectParameter() reading param of type %s", param->name);
11787c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        size_t size = readParamValue(param, (char *)fx_param, &curSize, &totSize);
11797c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        if (size == 0) {
11807c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            goto error;
11817c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        }
11827c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        fx_param->psize += size;
11837c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        param = param->next;
11847c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    }
11857c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent
11867c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    // align start of value field on 32 bit boundary
11877c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    curSize = ((curSize - 1 ) / sizeof(int) + 1) * sizeof(int);
11887c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent
11897c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    fx_param->vsize = 0;
11907c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    value = value->first_child;
11917c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    while (value) {
11923856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("loadEffectParameter() reading value of type %s", value->name);
11937c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        size_t size = readParamValue(value, (char *)fx_param, &curSize, &totSize);
11947c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        if (size == 0) {
11957c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            goto error;
11967c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        }
11977c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        fx_param->vsize += size;
11987c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        value = value->next;
11997c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    }
12007c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent
12017c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    return fx_param;
12027c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent
12037c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurenterror:
12047c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    delete fx_param;
12057c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    return NULL;
12067c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent}
12077c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent
12087c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurentvoid AudioPolicyService::loadEffectParameters(cnode *root, Vector <effect_param_t *>& params)
12097c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent{
12107c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    cnode *node = root->first_child;
12117c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    while (node) {
12123856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("loadEffectParameters() loading param %s", node->name);
12137c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        effect_param_t *param = loadEffectParameter(node);
12147c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        if (param == NULL) {
12157c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            node = node->next;
12167c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            continue;
12177c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        }
12187c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        params.add(param);
12197c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        node = node->next;
12207c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    }
12217c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent}
12227c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent
12237c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric LaurentAudioPolicyService::InputSourceDesc *AudioPolicyService::loadInputSource(
12247c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent                                                            cnode *root,
12257c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent                                                            const Vector <EffectDesc *>& effects)
12267c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent{
12277c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    cnode *node = root->first_child;
12287c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    if (node == NULL) {
12295ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block        ALOGW("loadInputSource() empty element %s", root->name);
12307c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        return NULL;
12317c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    }
12327c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    InputSourceDesc *source = new InputSourceDesc();
12337c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    while (node) {
12347c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        size_t i;
12357c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        for (i = 0; i < effects.size(); i++) {
12367c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            if (strncmp(effects[i]->mName, node->name, EFFECT_STRING_LEN_MAX) == 0) {
12373856b090cd04ba5dd4a59a12430ed724d5995909Steve Block                ALOGV("loadInputSource() found effect %s in list", node->name);
12387c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent                break;
12397c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            }
12407c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        }
12417c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        if (i == effects.size()) {
12423856b090cd04ba5dd4a59a12430ed724d5995909Steve Block            ALOGV("loadInputSource() effect %s not in list", node->name);
12437c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            node = node->next;
12447c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            continue;
12457c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        }
12467c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        EffectDesc *effect = new EffectDesc(*effects[i]);
12477c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        loadEffectParameters(node, effect->mParams);
12483856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("loadInputSource() adding effect %s uuid %08x", effect->mName, effect->mUuid.timeLow);
12497c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        source->mEffects.add(effect);
12507c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        node = node->next;
12517c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    }
12527c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    if (source->mEffects.size() == 0) {
12535ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block        ALOGW("loadInputSource() no valid effects found in source %s", root->name);
12547c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        delete source;
12557c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        return NULL;
12567c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    }
12577c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    return source;
12587c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent}
12597c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent
12607c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurentstatus_t AudioPolicyService::loadInputSources(cnode *root, const Vector <EffectDesc *>& effects)
12617c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent{
12627c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    cnode *node = config_find(root, PREPROCESSING_TAG);
12637c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    if (node == NULL) {
12647c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        return -ENOENT;
12657c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    }
12667c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    node = node->first_child;
12677c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    while (node) {
12687c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        audio_source_t source = inputSourceNameToEnum(node->name);
12697c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        if (source == AUDIO_SOURCE_CNT) {
12705ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block            ALOGW("loadInputSources() invalid input source %s", node->name);
12717c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            node = node->next;
12727c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            continue;
12737c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        }
12743856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("loadInputSources() loading input source %s", node->name);
12757c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        InputSourceDesc *desc = loadInputSource(node, effects);
12767c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        if (desc == NULL) {
12777c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            node = node->next;
12787c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            continue;
12797c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        }
12807c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        mInputSources.add(source, desc);
12817c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        node = node->next;
12827c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    }
12837c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    return NO_ERROR;
12847c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent}
12857c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent
12867c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric LaurentAudioPolicyService::EffectDesc *AudioPolicyService::loadEffect(cnode *root)
12877c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent{
12887c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    cnode *node = config_find(root, UUID_TAG);
12897c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    if (node == NULL) {
12907c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        return NULL;
12917c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    }
12927c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    effect_uuid_t uuid;
12937c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    if (AudioEffect::stringToGuid(node->value, &uuid) != NO_ERROR) {
12945ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block        ALOGW("loadEffect() invalid uuid %s", node->value);
12957c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        return NULL;
12967c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    }
12977c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    EffectDesc *effect = new EffectDesc();
12987c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    effect->mName = strdup(root->name);
12997c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    memcpy(&effect->mUuid, &uuid, sizeof(effect_uuid_t));
13007c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent
13017c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    return effect;
13027c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent}
13037c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent
13047c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurentstatus_t AudioPolicyService::loadEffects(cnode *root, Vector <EffectDesc *>& effects)
13057c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent{
13067c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    cnode *node = config_find(root, EFFECTS_TAG);
13077c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    if (node == NULL) {
13087c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        return -ENOENT;
13097c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    }
13107c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    node = node->first_child;
13117c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    while (node) {
13123856b090cd04ba5dd4a59a12430ed724d5995909Steve Block        ALOGV("loadEffects() loading effect %s", node->name);
13137c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        EffectDesc *effect = loadEffect(node);
13147c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        if (effect == NULL) {
13157c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            node = node->next;
13167c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent            continue;
13177c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        }
13187c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        effects.add(effect);
13197c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        node = node->next;
13207c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    }
13217c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    return NO_ERROR;
13227c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent}
13237c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent
13247c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurentstatus_t AudioPolicyService::loadPreProcessorConfig(const char *path)
13257c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent{
13267c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    cnode *root;
13277c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    char *data;
13287c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent
13297c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    data = (char *)load_file(path, NULL);
13307c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    if (data == NULL) {
13317c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent        return -ENODEV;
13327c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    }
13337c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    root = config_node("", "");
13347c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    config_load(root, data);
13357c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent
13367c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    Vector <EffectDesc *> effects;
13377c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    loadEffects(root, effects);
13387c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    loadInputSources(root, effects);
13397c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent
13407c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    config_free(root);
13417c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    free(root);
13427c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    free(data);
13437c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent
13447c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent    return NO_ERROR;
13457c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent}
13467c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent
1347fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin/* implementation of the interface to the policy manager */
1348fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavinextern "C" {
1349fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
1350fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavinstatic audio_io_handle_t aps_open_output(void *service,
1351fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                             uint32_t *pDevices,
1352fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                             uint32_t *pSamplingRate,
135358f30210ea540b6ce5aa6a46330cd3499483cb97Glenn Kasten                                             audio_format_t *pFormat,
1354fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                             uint32_t *pChannels,
1355fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                             uint32_t *pLatencyMs,
1356fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                             audio_policy_output_flags_t flags)
1357fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin{
1358fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
13597378ca506e4e20c2b2d4e94a131cf1b95831adb5Glenn Kasten    if (af == 0) {
13605ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block        ALOGW("%s: could not get AudioFlinger", __func__);
1361fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        return 0;
1362fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    }
1363fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
1364fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    return af->openOutput(pDevices, pSamplingRate, pFormat, pChannels,
1365fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                          pLatencyMs, flags);
1366fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin}
1367fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
1368fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavinstatic audio_io_handle_t aps_open_dup_output(void *service,
1369fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                                 audio_io_handle_t output1,
1370fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                                 audio_io_handle_t output2)
1371fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin{
1372fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
13737378ca506e4e20c2b2d4e94a131cf1b95831adb5Glenn Kasten    if (af == 0) {
13745ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block        ALOGW("%s: could not get AudioFlinger", __func__);
1375fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        return 0;
1376fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    }
1377fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    return af->openDuplicateOutput(output1, output2);
1378fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin}
1379fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
1380fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavinstatic int aps_close_output(void *service, audio_io_handle_t output)
1381fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin{
1382fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
13837378ca506e4e20c2b2d4e94a131cf1b95831adb5Glenn Kasten    if (af == 0)
1384fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        return PERMISSION_DENIED;
1385fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
1386fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    return af->closeOutput(output);
1387fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin}
1388fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
1389fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavinstatic int aps_suspend_output(void *service, audio_io_handle_t output)
1390fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin{
1391fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
13927378ca506e4e20c2b2d4e94a131cf1b95831adb5Glenn Kasten    if (af == 0) {
13935ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block        ALOGW("%s: could not get AudioFlinger", __func__);
1394fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        return PERMISSION_DENIED;
1395fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    }
1396fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
1397fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    return af->suspendOutput(output);
1398fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin}
1399fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
1400fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavinstatic int aps_restore_output(void *service, audio_io_handle_t output)
1401fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin{
1402fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
14037378ca506e4e20c2b2d4e94a131cf1b95831adb5Glenn Kasten    if (af == 0) {
14045ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block        ALOGW("%s: could not get AudioFlinger", __func__);
1405fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        return PERMISSION_DENIED;
1406fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    }
1407fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
1408fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    return af->restoreOutput(output);
1409fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin}
1410fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
1411fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavinstatic audio_io_handle_t aps_open_input(void *service,
1412fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                            uint32_t *pDevices,
1413fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                            uint32_t *pSamplingRate,
141458f30210ea540b6ce5aa6a46330cd3499483cb97Glenn Kasten                                            audio_format_t *pFormat,
1415fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                            uint32_t *pChannels,
1416de9719b3ec71472e6bf75117152176af51d1a515Glenn Kasten                                            audio_in_acoustics_t acoustics)
1417fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin{
1418fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
14197378ca506e4e20c2b2d4e94a131cf1b95831adb5Glenn Kasten    if (af == 0) {
14205ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block        ALOGW("%s: could not get AudioFlinger", __func__);
1421fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        return 0;
1422fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    }
1423fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
1424fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    return af->openInput(pDevices, pSamplingRate, pFormat, pChannels,
1425fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                         acoustics);
1426fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin}
1427fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
1428fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavinstatic int aps_close_input(void *service, audio_io_handle_t input)
1429fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin{
1430fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
14317378ca506e4e20c2b2d4e94a131cf1b95831adb5Glenn Kasten    if (af == 0)
1432fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        return PERMISSION_DENIED;
1433fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
1434fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    return af->closeInput(input);
1435fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin}
1436fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
1437fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavinstatic int aps_set_stream_output(void *service, audio_stream_type_t stream,
1438fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                     audio_io_handle_t output)
1439fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin{
1440fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
14417378ca506e4e20c2b2d4e94a131cf1b95831adb5Glenn Kasten    if (af == 0)
1442fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        return PERMISSION_DENIED;
1443fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
1444fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    return af->setStreamOutput(stream, output);
1445fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin}
1446fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
1447fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavinstatic int aps_move_effects(void *service, int session,
1448fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                audio_io_handle_t src_output,
1449fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                audio_io_handle_t dst_output)
1450fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin{
1451fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
14527378ca506e4e20c2b2d4e94a131cf1b95831adb5Glenn Kasten    if (af == 0)
1453fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        return PERMISSION_DENIED;
1454fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
145572ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten    return af->moveEffects(session, src_output, dst_output);
1456fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin}
1457fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
1458fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavinstatic char * aps_get_parameters(void *service, audio_io_handle_t io_handle,
1459fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                     const char *keys)
1460fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin{
1461fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    String8 result = AudioSystem::getParameters(io_handle, String8(keys));
1462fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    return strdup(result.string());
1463fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin}
1464fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
1465fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavinstatic void aps_set_parameters(void *service, audio_io_handle_t io_handle,
1466fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                   const char *kv_pairs, int delay_ms)
1467fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin{
1468fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1469fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
1470fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    audioPolicyService->setParameters(io_handle, kv_pairs, delay_ms);
1471fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin}
1472fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
1473fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavinstatic int aps_set_stream_volume(void *service, audio_stream_type_t stream,
1474fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                     float volume, audio_io_handle_t output,
1475fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                     int delay_ms)
1476fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin{
1477fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1478fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
1479fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    return audioPolicyService->setStreamVolume(stream, volume, output,
1480fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                               delay_ms);
1481fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin}
1482fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
1483fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavinstatic int aps_start_tone(void *service, audio_policy_tone_t tone,
1484fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                              audio_stream_type_t stream)
1485fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin{
1486fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1487fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
1488fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    return audioPolicyService->startTone(tone, stream);
1489fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin}
1490fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
1491fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavinstatic int aps_stop_tone(void *service)
1492fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin{
1493fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1494fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
1495fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    return audioPolicyService->stopTone();
1496fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin}
1497fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
1498fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavinstatic int aps_set_voice_volume(void *service, float volume, int delay_ms)
1499fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin{
1500fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    AudioPolicyService *audioPolicyService = (AudioPolicyService *)service;
1501fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
1502fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    return audioPolicyService->setVoiceVolume(volume, delay_ms);
1503fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin}
1504fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
1505fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin}; // extern "C"
1506fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
1507fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavinnamespace {
1508fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    struct audio_policy_service_ops aps_ops = {
1509fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        open_output           : aps_open_output,
1510fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        open_duplicate_output : aps_open_dup_output,
1511fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        close_output          : aps_close_output,
1512fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        suspend_output        : aps_suspend_output,
1513fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        restore_output        : aps_restore_output,
1514fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        open_input            : aps_open_input,
1515fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        close_input           : aps_close_input,
1516fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        set_stream_volume     : aps_set_stream_volume,
1517fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        set_stream_output     : aps_set_stream_output,
1518fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        set_parameters        : aps_set_parameters,
1519fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        get_parameters        : aps_get_parameters,
1520fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        start_tone            : aps_start_tone,
1521fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        stop_tone             : aps_stop_tone,
1522fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        set_voice_volume      : aps_set_voice_volume,
1523fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        move_effects          : aps_move_effects,
1524fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    };
1525fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin}; // namespace <unnamed>
1526fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
152765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}; // namespace android
1528