AudioPolicyService.cpp revision de80105c3f2db0eabd47640c49387ea3b44d4782
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
20153b9fe667e6e78e0218ff0159353097428c7657Glenn Kasten#include "Configuration.h"
2165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#undef __STRICT_ANSI__
2265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#define __STDINT_LIMITS
2365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#define __STDC_LIMIT_MACROS
2465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <stdint.h>
2565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
2665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <sys/time.h>
2765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <binder/IServiceManager.h>
2865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <utils/Log.h>
2965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <cutils/properties.h>
3065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <binder/IPCThreadState.h>
3165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <utils/String16.h>
3265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <utils/threads.h>
3365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include "AudioPolicyService.h"
3444deb053252a3bd2f57a007ab9560f4924f62394Glenn Kasten#include "ServiceUtilities.h"
3565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <hardware_legacy/power.h>
367c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent#include <media/AudioEffect.h>
377c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent#include <media/EffectsFactoryApi.h>
38c84d9d235679a4d48245b316b7b2e4c0b19413e7Chih-Hung Hsieh#include <media/AudioParameter.h>
3965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
40fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin#include <hardware/hardware.h>
4164760240f931714858a59c1579f07264d7182ba2Dima Zavin#include <system/audio.h>
427394a4f358fa9908a9f0a7c954b65c399f4268e6Dima Zavin#include <system/audio_policy.h>
43fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin#include <hardware/audio_policy.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
530ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurentstatic const nsecs_t kAudioCommandTimeoutNs = seconds(3); // 3 seconds
545fa8c4bf81275d5e1f5ef71bc66fc22e3152eeb0Christer Fletcher
55fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavinnamespace {
56fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    extern struct audio_policy_service_ops aps_ops;
57fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin};
58fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
5965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// ----------------------------------------------------------------------------
6065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
6165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias AgopianAudioPolicyService::AudioPolicyService()
62dce54a1492c410ad0d93253b341fb33305337505Eric Laurent    : BnAudioPolicyService(), mpAudioPolicyDev(NULL), mpAudioPolicy(NULL),
63bb6c9a05840d924b502ce0f1868fca4881ada1edEric Laurent      mAudioPolicyManager(NULL), mAudioPolicyClient(NULL), mPhoneState(AUDIO_MODE_INVALID)
6465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
65f5ada6edcc83bb1b313abcd4c4fbdc4f7724f04dEric Laurent}
66f5ada6edcc83bb1b313abcd4c4fbdc4f7724f04dEric Laurent
67f5ada6edcc83bb1b313abcd4c4fbdc4f7724f04dEric Laurentvoid AudioPolicyService::onFirstRef()
68f5ada6edcc83bb1b313abcd4c4fbdc4f7724f04dEric Laurent{
6965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    char value[PROPERTY_VALUE_MAX];
70fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    const struct hw_module_t *module;
71fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    int forced_val;
72fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    int rc;
7365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
748b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent    {
758b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent        Mutex::Autolock _l(mLock);
76935752053ef2691dbb6d5a6d149e0e362c6e3c74Eric Laurent
778b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent        // start tone playback thread
788b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent        mTonePlaybackThread = new AudioCommandThread(String8("ApmTone"), this);
798b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent        // start audio commands thread
808b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent        mAudioCommandThread = new AudioCommandThread(String8("ApmAudio"), this);
818b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent        // start output activity command thread
828b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent        mOutputCommandThread = new AudioCommandThread(String8("ApmOutput"), this);
83dce54a1492c410ad0d93253b341fb33305337505Eric Laurent
84dce54a1492c410ad0d93253b341fb33305337505Eric Laurent#ifdef USE_LEGACY_AUDIO_POLICY
858b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent        ALOGI("AudioPolicyService CSTOR in legacy mode");
868b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent
878b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent        /* instantiate the audio policy manager */
888b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent        rc = hw_get_module(AUDIO_POLICY_HARDWARE_MODULE_ID, &module);
898b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent        if (rc) {
908b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent            return;
918b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent        }
928b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent        rc = audio_policy_dev_open(module, &mpAudioPolicyDev);
938b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent        ALOGE_IF(rc, "couldn't open audio policy device (%s)", strerror(-rc));
948b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent        if (rc) {
958b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent            return;
968b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent        }
978b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent
988b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent        rc = mpAudioPolicyDev->create_audio_policy(mpAudioPolicyDev, &aps_ops, this,
998b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent                                                   &mpAudioPolicy);
1008b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent        ALOGE_IF(rc, "couldn't create audio policy (%s)", strerror(-rc));
1018b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent        if (rc) {
1028b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent            return;
1038b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent        }
1048b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent
1058b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent        rc = mpAudioPolicy->init_check(mpAudioPolicy);
1068b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent        ALOGE_IF(rc, "couldn't init_check the audio policy (%s)", strerror(-rc));
1078b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent        if (rc) {
1088b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent            return;
1098b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent        }
1108b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent        ALOGI("Loaded audio policy from %s (%s)", module->name, module->id);
111dce54a1492c410ad0d93253b341fb33305337505Eric Laurent#else
1128b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent        ALOGI("AudioPolicyService CSTOR in new mode");
113dce54a1492c410ad0d93253b341fb33305337505Eric Laurent
1148b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent        mAudioPolicyClient = new AudioPolicyClient(this);
1158b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent        mAudioPolicyManager = createAudioPolicyManager(mAudioPolicyClient);
116dce54a1492c410ad0d93253b341fb33305337505Eric Laurent#endif
1178b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent    }
118ba2b43990a7b4f0f2c425cf6cdfc63376a45772cbryant_liu    // load audio processing modules
1198b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent    sp<AudioPolicyEffects>audioPolicyEffects = new AudioPolicyEffects();
1208b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent    {
1218b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent        Mutex::Autolock _l(mLock);
1228b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent        mAudioPolicyEffects = audioPolicyEffects;
1238b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent    }
12465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
12565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
12665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias AgopianAudioPolicyService::~AudioPolicyService()
12765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
12865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mTonePlaybackThread->exit();
12965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mAudioCommandThread->exit();
130657ff61389d7316f798d4abe03efac56fd528d91Eric Laurent    mOutputCommandThread->exit();
1317c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent
132dce54a1492c410ad0d93253b341fb33305337505Eric Laurent#ifdef USE_LEGACY_AUDIO_POLICY
1336e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    if (mpAudioPolicy != NULL && mpAudioPolicyDev != NULL) {
134fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        mpAudioPolicyDev->destroy_audio_policy(mpAudioPolicyDev, mpAudioPolicy);
1356e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    }
1366e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    if (mpAudioPolicyDev != NULL) {
137fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        audio_policy_dev_close(mpAudioPolicyDev);
1386e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    }
139dce54a1492c410ad0d93253b341fb33305337505Eric Laurent#else
140f269b8e0e9ab950fc6652b9594b7a3431c81630cEric Laurent    destroyAudioPolicyManager(mAudioPolicyManager);
141dce54a1492c410ad0d93253b341fb33305337505Eric Laurent    delete mAudioPolicyClient;
142dce54a1492c410ad0d93253b341fb33305337505Eric Laurent#endif
143b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent
144b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    mNotificationClients.clear();
145ba2b43990a7b4f0f2c425cf6cdfc63376a45772cbryant_liu    mAudioPolicyEffects.clear();
146b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent}
147b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent
148b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent// A notification client is always registered by AudioSystem when the client process
149b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent// connects to AudioPolicyService.
150b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurentvoid AudioPolicyService::registerClient(const sp<IAudioPolicyServiceClient>& client)
151b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent{
152b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent
1530ebd5f95b68a3a5c9e5509f21938c9e51e74d71bEric Laurent    Mutex::Autolock _l(mNotificationClientsLock);
154b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent
155b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    uid_t uid = IPCThreadState::self()->getCallingUid();
156b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    if (mNotificationClients.indexOfKey(uid) < 0) {
157b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent        sp<NotificationClient> notificationClient = new NotificationClient(this,
158b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                                                                           client,
159b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                                                                           uid);
160b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent        ALOGV("registerClient() client %p, uid %d", client.get(), uid);
161b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent
162b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent        mNotificationClients.add(uid, notificationClient);
163b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent
164f888020c6e2735624f2b2a30e72aca24e17b8b4dMarco Nelissen        sp<IBinder> binder = IInterface::asBinder(client);
165b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent        binder->linkToDeath(notificationClient);
166b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    }
167b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent}
168b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent
169b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent// removeNotificationClient() is called when the client process dies.
170b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurentvoid AudioPolicyService::removeNotificationClient(uid_t uid)
171b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent{
1720ebd5f95b68a3a5c9e5509f21938c9e51e74d71bEric Laurent    {
1730ebd5f95b68a3a5c9e5509f21938c9e51e74d71bEric Laurent        Mutex::Autolock _l(mNotificationClientsLock);
1740ebd5f95b68a3a5c9e5509f21938c9e51e74d71bEric Laurent        mNotificationClients.removeItem(uid);
1750ebd5f95b68a3a5c9e5509f21938c9e51e74d71bEric Laurent    }
176b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent#ifndef USE_LEGACY_AUDIO_POLICY
1770ebd5f95b68a3a5c9e5509f21938c9e51e74d71bEric Laurent    {
1780ebd5f95b68a3a5c9e5509f21938c9e51e74d71bEric Laurent        Mutex::Autolock _l(mLock);
179b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent        if (mAudioPolicyManager) {
180b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent            mAudioPolicyManager->clearAudioPatches(uid);
181b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent        }
1820ebd5f95b68a3a5c9e5509f21938c9e51e74d71bEric Laurent    }
183b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent#endif
184b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent}
185b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent
186b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurentvoid AudioPolicyService::onAudioPortListUpdate()
187b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent{
188b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    mOutputCommandThread->updateAudioPortListCommand();
189b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent}
190b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent
191b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurentvoid AudioPolicyService::doOnAudioPortListUpdate()
192b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent{
1930ebd5f95b68a3a5c9e5509f21938c9e51e74d71bEric Laurent    Mutex::Autolock _l(mNotificationClientsLock);
194b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    for (size_t i = 0; i < mNotificationClients.size(); i++) {
195b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent        mNotificationClients.valueAt(i)->onAudioPortListUpdate();
196b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    }
197b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent}
198b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent
199b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurentvoid AudioPolicyService::onAudioPatchListUpdate()
200b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent{
201b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    mOutputCommandThread->updateAudioPatchListCommand();
20265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
20365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
204951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurentstatus_t AudioPolicyService::clientCreateAudioPatch(const struct audio_patch *patch,
205951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                                                audio_patch_handle_t *handle,
206951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                                                int delayMs)
207951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent{
208951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    return mAudioCommandThread->createAudioPatchCommand(patch, handle, delayMs);
209951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent}
210951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent
211951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurentstatus_t AudioPolicyService::clientReleaseAudioPatch(audio_patch_handle_t handle,
212951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                                                 int delayMs)
213951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent{
214951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    return mAudioCommandThread->releaseAudioPatchCommand(handle, delayMs);
215951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent}
216951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent
217b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurentvoid AudioPolicyService::doOnAudioPatchListUpdate()
218b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent{
2190ebd5f95b68a3a5c9e5509f21938c9e51e74d71bEric Laurent    Mutex::Autolock _l(mNotificationClientsLock);
220b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    for (size_t i = 0; i < mNotificationClients.size(); i++) {
221b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent        mNotificationClients.valueAt(i)->onAudioPatchListUpdate();
222b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    }
223b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent}
224b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent
225de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivivoid AudioPolicyService::onDynamicPolicyMixStateUpdate(String8 regId, int32_t state)
226de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi{
227de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi    ALOGV("AudioPolicyService::onDynamicPolicyMixStateUpdate(%s, %d)",
228de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi            regId.string(), state);
229de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi    mOutputCommandThread->dynamicPolicyMixStateUpdateCommand(regId, state);
230de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi}
231de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi
232de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivivoid AudioPolicyService::doOnDynamicPolicyMixStateUpdate(String8 regId, int32_t state)
233de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi{
234de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi    Mutex::Autolock _l(mNotificationClientsLock);
235de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi    for (size_t i = 0; i < mNotificationClients.size(); i++) {
236de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi        mNotificationClients.valueAt(i)->onDynamicPolicyMixStateUpdate(regId, state);
237de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi    }
238de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi}
239de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi
240e1715a465a29db625da9d0ea365edf371e39e201Eric Laurentstatus_t AudioPolicyService::clientSetAudioPortConfig(const struct audio_port_config *config,
241e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent                                                      int delayMs)
242e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent{
243e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent    return mAudioCommandThread->setAudioPortConfigCommand(config, delayMs);
244e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent}
245e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent
246b52c152d553556b2d227ffc943489de0c60b4b02Eric LaurentAudioPolicyService::NotificationClient::NotificationClient(const sp<AudioPolicyService>& service,
247b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                                                     const sp<IAudioPolicyServiceClient>& client,
248b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                                                     uid_t uid)
249b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    : mService(service), mUid(uid), mAudioPolicyServiceClient(client)
250b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent{
251b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent}
252b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent
253b52c152d553556b2d227ffc943489de0c60b4b02Eric LaurentAudioPolicyService::NotificationClient::~NotificationClient()
254b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent{
255b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent}
256b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent
257b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurentvoid AudioPolicyService::NotificationClient::binderDied(const wp<IBinder>& who __unused)
258b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent{
259b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    sp<NotificationClient> keep(this);
260b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    sp<AudioPolicyService> service = mService.promote();
261b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    if (service != 0) {
262b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent        service->removeNotificationClient(mUid);
263b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    }
264b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent}
265b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent
266b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurentvoid AudioPolicyService::NotificationClient::onAudioPortListUpdate()
267b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent{
268b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    if (mAudioPolicyServiceClient != 0) {
269b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent        mAudioPolicyServiceClient->onAudioPortListUpdate();
270b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    }
271b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent}
272b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent
273b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurentvoid AudioPolicyService::NotificationClient::onAudioPatchListUpdate()
274b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent{
275b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    if (mAudioPolicyServiceClient != 0) {
276b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent        mAudioPolicyServiceClient->onAudioPatchListUpdate();
277b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    }
278b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent}
27957dae99c9fcd6becd2b5ed0c53f277ba4d7dbcfcEric Laurent
280de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivivoid AudioPolicyService::NotificationClient::onDynamicPolicyMixStateUpdate(
281de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi        String8 regId, int32_t state)
282de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi{
283de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi    if (mAudioPolicyServiceClient != 0) {
284de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi            mAudioPolicyServiceClient->onDynamicPolicyMixStateUpdate(regId, state);
285de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi    }
286de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi}
287de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi
28865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid AudioPolicyService::binderDied(const wp<IBinder>& who) {
289411e447c4b90298f5ff635429c53f94fbce4fff9Glenn Kasten    ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
290de070137f11d346fba77605bd76a44c040a618fcEric Laurent            IPCThreadState::self()->getCallingPid());
29165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
29265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
29365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatic bool tryLock(Mutex& mutex)
29465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
29565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    bool locked = false;
29665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    for (int i = 0; i < kDumpLockRetries; ++i) {
29765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (mutex.tryLock() == NO_ERROR) {
29865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            locked = true;
29965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            break;
30065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
30122ecc912a87099cff8cadc424cd12f85c118673fGlenn Kasten        usleep(kDumpLockSleepUs);
30265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
30365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return locked;
30465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
30565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
30665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioPolicyService::dumpInternals(int fd)
30765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
30865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    const size_t SIZE = 256;
30965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    char buffer[SIZE];
31065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    String8 result;
31165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
312dce54a1492c410ad0d93253b341fb33305337505Eric Laurent#ifdef USE_LEGACY_AUDIO_POLICY
313fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    snprintf(buffer, SIZE, "PolicyManager Interface: %p\n", mpAudioPolicy);
314dce54a1492c410ad0d93253b341fb33305337505Eric Laurent#else
315dce54a1492c410ad0d93253b341fb33305337505Eric Laurent    snprintf(buffer, SIZE, "AudioPolicyManager: %p\n", mAudioPolicyManager);
316dce54a1492c410ad0d93253b341fb33305337505Eric Laurent#endif
31765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    result.append(buffer);
31865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
31965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    result.append(buffer);
32065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    snprintf(buffer, SIZE, "Tones Thread: %p\n", mTonePlaybackThread.get());
32165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    result.append(buffer);
32265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
32365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    write(fd, result.string(), result.size());
32465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return NO_ERROR;
32565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
32665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
3270f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenstatus_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused)
32865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
32944deb053252a3bd2f57a007ab9560f4924f62394Glenn Kasten    if (!dumpAllowed()) {
33065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        dumpPermissionDenial(fd);
33165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    } else {
33265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        bool locked = tryLock(mLock);
33365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (!locked) {
33465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            String8 result(kDeadlockedString);
33565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            write(fd, result.string(), result.size());
33665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
33765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
33865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        dumpInternals(fd);
3399d1f02d74fd395ec4de6861147da289423f0ab6fGlenn Kasten        if (mAudioCommandThread != 0) {
34065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            mAudioCommandThread->dump(fd);
34165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
3429d1f02d74fd395ec4de6861147da289423f0ab6fGlenn Kasten        if (mTonePlaybackThread != 0) {
34365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            mTonePlaybackThread->dump(fd);
34465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
34565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
346dce54a1492c410ad0d93253b341fb33305337505Eric Laurent#ifdef USE_LEGACY_AUDIO_POLICY
347fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        if (mpAudioPolicy) {
348fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin            mpAudioPolicy->dump(mpAudioPolicy, fd);
34965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
350dce54a1492c410ad0d93253b341fb33305337505Eric Laurent#else
351dce54a1492c410ad0d93253b341fb33305337505Eric Laurent        if (mAudioPolicyManager) {
352dce54a1492c410ad0d93253b341fb33305337505Eric Laurent            mAudioPolicyManager->dump(fd);
353dce54a1492c410ad0d93253b341fb33305337505Eric Laurent        }
354dce54a1492c410ad0d93253b341fb33305337505Eric Laurent#endif
35565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
35665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (locked) mLock.unlock();
35765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
35865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return NO_ERROR;
35965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
36065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
36165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioPolicyService::dumpPermissionDenial(int fd)
36265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
36365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    const size_t SIZE = 256;
36465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    char buffer[SIZE];
36565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    String8 result;
36665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    snprintf(buffer, SIZE, "Permission Denial: "
36765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            "can't dump AudioPolicyService from pid=%d, uid=%d\n",
36865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            IPCThreadState::self()->getCallingPid(),
36965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            IPCThreadState::self()->getCallingUid());
37065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    result.append(buffer);
37165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    write(fd, result.string(), result.size());
37265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return NO_ERROR;
37365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
37465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
37565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioPolicyService::onTransact(
37665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
37765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
37865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return BnAudioPolicyService::onTransact(code, data, reply, flags);
37965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
38065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
38165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
38265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// -----------  AudioPolicyService::AudioCommandThread implementation ----------
38365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
384bfb1b832079bbb9426f72f3863199a54aefd02daEric LaurentAudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
385bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                                                           const wp<AudioPolicyService>& service)
386bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    : Thread(false), mName(name), mService(service)
38765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
38865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mpToneGenerator = NULL;
38965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
39065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
39165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
39265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias AgopianAudioPolicyService::AudioCommandThread::~AudioCommandThread()
39365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
394bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    if (!mAudioCommands.isEmpty()) {
39565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        release_wake_lock(mName.string());
39665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
39765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mAudioCommands.clear();
398e9dd0176933d6233916c84e18f3e8c0d644ca05dGlenn Kasten    delete mpToneGenerator;
39965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
40065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
40165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid AudioPolicyService::AudioCommandThread::onFirstRef()
40265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
403bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    run(mName.string(), ANDROID_PRIORITY_AUDIO);
40465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
40565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
40665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianbool AudioPolicyService::AudioCommandThread::threadLoop()
40765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
40865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    nsecs_t waitTime = INT64_MAX;
40965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
41065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mLock.lock();
41165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    while (!exitPending())
41265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    {
41359a8923027d2d6a155c0fa434ae65559318da4b6Eric Laurent        sp<AudioPolicyService> svc;
41459a8923027d2d6a155c0fa434ae65559318da4b6Eric Laurent        while (!mAudioCommands.isEmpty() && !exitPending()) {
41565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            nsecs_t curTime = systemTime();
41665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            // commands are sorted by increasing time stamp: execute them from index 0 and up
41765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            if (mAudioCommands[0]->mTime <= curTime) {
4180ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent                sp<AudioCommand> command = mAudioCommands[0];
41965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                mAudioCommands.removeAt(0);
4200ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent                mLastCommand = command;
42165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
42265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                switch (command->mCommand) {
42365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                case START_TONE: {
42465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    mLock.unlock();
4250ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent                    ToneData *data = (ToneData *)command->mParam.get();
4263856b090cd04ba5dd4a59a12430ed724d5995909Steve Block                    ALOGV("AudioCommandThread() processing start tone %d on stream %d",
42765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                            data->mType, data->mStream);
428e9dd0176933d6233916c84e18f3e8c0d644ca05dGlenn Kasten                    delete mpToneGenerator;
42965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    mpToneGenerator = new ToneGenerator(data->mStream, 1.0);
43065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    mpToneGenerator->startTone(data->mType);
43165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    mLock.lock();
43265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    }break;
43365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                case STOP_TONE: {
43465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    mLock.unlock();
4353856b090cd04ba5dd4a59a12430ed724d5995909Steve Block                    ALOGV("AudioCommandThread() processing stop tone");
43665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    if (mpToneGenerator != NULL) {
43765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                        mpToneGenerator->stopTone();
43865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                        delete mpToneGenerator;
43965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                        mpToneGenerator = NULL;
44065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    }
44165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    mLock.lock();
44265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    }break;
44365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                case SET_VOLUME: {
4440ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent                    VolumeData *data = (VolumeData *)command->mParam.get();
4453856b090cd04ba5dd4a59a12430ed724d5995909Steve Block                    ALOGV("AudioCommandThread() processing set volume stream %d, \
446de070137f11d346fba77605bd76a44c040a618fcEric Laurent                            volume %f, output %d", data->mStream, data->mVolume, data->mIO);
447de070137f11d346fba77605bd76a44c040a618fcEric Laurent                    command->mStatus = AudioSystem::setStreamVolume(data->mStream,
448de070137f11d346fba77605bd76a44c040a618fcEric Laurent                                                                    data->mVolume,
449de070137f11d346fba77605bd76a44c040a618fcEric Laurent                                                                    data->mIO);
45065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    }break;
45165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                case SET_PARAMETERS: {
4520ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent                    ParametersData *data = (ParametersData *)command->mParam.get();
453e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten                    ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
454e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten                            data->mKeyValuePairs.string(), data->mIO);
455e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten                    command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
456e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten                    }break;
45765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                case SET_VOICE_VOLUME: {
4580ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent                    VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
4593856b090cd04ba5dd4a59a12430ed724d5995909Steve Block                    ALOGV("AudioCommandThread() processing set voice volume volume %f",
460de070137f11d346fba77605bd76a44c040a618fcEric Laurent                            data->mVolume);
46165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
46265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    }break;
463bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                case STOP_OUTPUT: {
4640ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent                    StopOutputData *data = (StopOutputData *)command->mParam.get();
465bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    ALOGV("AudioCommandThread() processing stop output %d",
466bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                            data->mIO);
46759a8923027d2d6a155c0fa434ae65559318da4b6Eric Laurent                    svc = mService.promote();
468bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    if (svc == 0) {
469bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                        break;
470bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    }
471bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    mLock.unlock();
472bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    svc->doStopOutput(data->mIO, data->mStream, data->mSession);
473bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    mLock.lock();
474bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    }break;
475bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                case RELEASE_OUTPUT: {
4760ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent                    ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get();
477bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    ALOGV("AudioCommandThread() processing release output %d",
478bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                            data->mIO);
47959a8923027d2d6a155c0fa434ae65559318da4b6Eric Laurent                    svc = mService.promote();
480bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    if (svc == 0) {
481bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                        break;
482bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    }
483bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    mLock.unlock();
484e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent                    svc->doReleaseOutput(data->mIO, data->mStream, data->mSession);
485bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    mLock.lock();
486bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    }break;
487951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                case CREATE_AUDIO_PATCH: {
488951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                    CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get();
489951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                    ALOGV("AudioCommandThread() processing create audio patch");
490951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                    sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
491951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                    if (af == 0) {
492951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                        command->mStatus = PERMISSION_DENIED;
493951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                    } else {
494951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                        command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle);
495951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                    }
496951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                    } break;
497951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                case RELEASE_AUDIO_PATCH: {
498951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                    ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get();
499951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                    ALOGV("AudioCommandThread() processing release audio patch");
500951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                    sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
501951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                    if (af == 0) {
502951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                        command->mStatus = PERMISSION_DENIED;
503951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                    } else {
504951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                        command->mStatus = af->releaseAudioPatch(data->mHandle);
505951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                    }
506951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                    } break;
507b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                case UPDATE_AUDIOPORT_LIST: {
508b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                    ALOGV("AudioCommandThread() processing update audio port list");
50959a8923027d2d6a155c0fa434ae65559318da4b6Eric Laurent                    svc = mService.promote();
510b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                    if (svc == 0) {
511b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                        break;
512b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                    }
513b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                    mLock.unlock();
514b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                    svc->doOnAudioPortListUpdate();
515b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                    mLock.lock();
516b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                    }break;
517b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                case UPDATE_AUDIOPATCH_LIST: {
518b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                    ALOGV("AudioCommandThread() processing update audio patch list");
51959a8923027d2d6a155c0fa434ae65559318da4b6Eric Laurent                    svc = mService.promote();
520b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                    if (svc == 0) {
521b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                        break;
522b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                    }
523b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                    mLock.unlock();
524b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                    svc->doOnAudioPatchListUpdate();
525b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                    mLock.lock();
526b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                    }break;
527e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent                case SET_AUDIOPORT_CONFIG: {
528e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent                    SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get();
529e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent                    ALOGV("AudioCommandThread() processing set port config");
530e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent                    sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
531e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent                    if (af == 0) {
532e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent                        command->mStatus = PERMISSION_DENIED;
533e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent                    } else {
534e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent                        command->mStatus = af->setAudioPortConfig(&data->mConfig);
535e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent                    }
536e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent                    } break;
537de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi                case DYN_POLICY_MIX_STATE_UPDATE: {
538de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi                    DynPolicyMixStateUpdateData *data =
539de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi                            (DynPolicyMixStateUpdateData *)command->mParam.get();
540de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi                    //###ALOGV("AudioCommandThread() processing dyn policy mix state update");
541de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi                    ALOGV("AudioCommandThread() processing dyn policy mix state update %s %d",
542de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi                            data->mRegId.string(), data->mState);
543de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi                    svc = mService.promote();
544de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi                    if (svc == 0) {
545de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi                        break;
546de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi                    }
547de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi                    mLock.unlock();
548de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi                    svc->doOnDynamicPolicyMixStateUpdate(data->mRegId, data->mState);
549de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi                    mLock.lock();
550de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi                    } break;
55165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                default:
5525ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block                    ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
55365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                }
5540ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent                {
5550ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent                    Mutex::Autolock _l(command->mLock);
5560ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent                    if (command->mWaitStatus) {
5570ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent                        command->mWaitStatus = false;
5580ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent                        command->mCond.signal();
5590ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent                    }
5600ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent                }
56165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                waitTime = INT64_MAX;
56265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            } else {
56365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                waitTime = mAudioCommands[0]->mTime - curTime;
56465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                break;
56565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            }
56665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
56759a8923027d2d6a155c0fa434ae65559318da4b6Eric Laurent        // release mLock before releasing strong reference on the service as
56859a8923027d2d6a155c0fa434ae65559318da4b6Eric Laurent        // AudioPolicyService destructor calls AudioCommandThread::exit() which acquires mLock.
56959a8923027d2d6a155c0fa434ae65559318da4b6Eric Laurent        mLock.unlock();
57059a8923027d2d6a155c0fa434ae65559318da4b6Eric Laurent        svc.clear();
57159a8923027d2d6a155c0fa434ae65559318da4b6Eric Laurent        mLock.lock();
572884de21ca32e4a47f77e541a7284d02054ff3349Wally Yau        if (!exitPending() && (mAudioCommands.isEmpty() || waitTime != INT64_MAX)) {
57305f2fdc1070c1c07af9e21760d4d2e06bebde55aRicardo Garcia            // release delayed commands wake lock
57405f2fdc1070c1c07af9e21760d4d2e06bebde55aRicardo Garcia            release_wake_lock(mName.string());
57559a8923027d2d6a155c0fa434ae65559318da4b6Eric Laurent            ALOGV("AudioCommandThread() going to sleep");
57659a8923027d2d6a155c0fa434ae65559318da4b6Eric Laurent            mWaitWorkCV.waitRelative(mLock, waitTime);
57759a8923027d2d6a155c0fa434ae65559318da4b6Eric Laurent            ALOGV("AudioCommandThread() waking up");
57859a8923027d2d6a155c0fa434ae65559318da4b6Eric Laurent        }
57965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
58005f2fdc1070c1c07af9e21760d4d2e06bebde55aRicardo Garcia    // release delayed commands wake lock before quitting
58105f2fdc1070c1c07af9e21760d4d2e06bebde55aRicardo Garcia    if (!mAudioCommands.isEmpty()) {
58205f2fdc1070c1c07af9e21760d4d2e06bebde55aRicardo Garcia        release_wake_lock(mName.string());
58305f2fdc1070c1c07af9e21760d4d2e06bebde55aRicardo Garcia    }
58465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mLock.unlock();
58565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return false;
58665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
58765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
58865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioPolicyService::AudioCommandThread::dump(int fd)
58965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
59065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    const size_t SIZE = 256;
59165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    char buffer[SIZE];
59265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    String8 result;
59365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
59465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
59565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    result.append(buffer);
59665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    write(fd, result.string(), result.size());
59765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
59865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    bool locked = tryLock(mLock);
59965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (!locked) {
60065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        String8 result2(kCmdDeadlockedString);
60165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        write(fd, result2.string(), result2.size());
60265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
60365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
60465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    snprintf(buffer, SIZE, "- Commands:\n");
60565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    result = String8(buffer);
60665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    result.append("   Command Time        Wait pParam\n");
6078d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten    for (size_t i = 0; i < mAudioCommands.size(); i++) {
60865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        mAudioCommands[i]->dump(buffer, SIZE);
60965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        result.append(buffer);
61065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
61165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    result.append("  Last Command\n");
6120ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    if (mLastCommand != 0) {
6130ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent        mLastCommand->dump(buffer, SIZE);
6140ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent        result.append(buffer);
6150ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    } else {
6160ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent        result.append("     none\n");
6170ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    }
61865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
61965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    write(fd, result.string(), result.size());
62065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
62165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (locked) mLock.unlock();
62265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
62365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return NO_ERROR;
62465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
62565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
6263d2f877c1cb4e4ae4ddde7f57f4353de9341f11bGlenn Kastenvoid AudioPolicyService::AudioCommandThread::startToneCommand(ToneGenerator::tone_type type,
6273d2f877c1cb4e4ae4ddde7f57f4353de9341f11bGlenn Kasten        audio_stream_type_t stream)
62865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
6290ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    sp<AudioCommand> command = new AudioCommand();
63065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    command->mCommand = START_TONE;
6310ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    sp<ToneData> data = new ToneData();
63265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    data->mType = type;
63365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    data->mStream = stream;
63448412dc92c4cc92a3f4821616e12290f5eeabd07Jesper Tragardh    command->mParam = data;
6353856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream);
6360ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    sendCommand(command);
63765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
63865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
63965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid AudioPolicyService::AudioCommandThread::stopToneCommand()
64065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
6410ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    sp<AudioCommand> command = new AudioCommand();
64265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    command->mCommand = STOP_TONE;
6433856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("AudioCommandThread() adding tone stop");
6440ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    sendCommand(command);
64565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
64665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
647fff6d715a8db0daf08a50634f242c40268de3d49Glenn Kastenstatus_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
648de070137f11d346fba77605bd76a44c040a618fcEric Laurent                                                               float volume,
64972ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten                                                               audio_io_handle_t output,
650de070137f11d346fba77605bd76a44c040a618fcEric Laurent                                                               int delayMs)
65165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
6520ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    sp<AudioCommand> command = new AudioCommand();
65365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    command->mCommand = SET_VOLUME;
6540ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    sp<VolumeData> data = new VolumeData();
65565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    data->mStream = stream;
65665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    data->mVolume = volume;
65765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    data->mIO = output;
65865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    command->mParam = data;
6590ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    command->mWaitStatus = true;
6603856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
661de070137f11d346fba77605bd76a44c040a618fcEric Laurent            stream, volume, output);
6620ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    return sendCommand(command, delayMs);
66365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
66465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
66572ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenstatus_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
666fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                                                   const char *keyValuePairs,
667de070137f11d346fba77605bd76a44c040a618fcEric Laurent                                                                   int delayMs)
66865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
6690ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    sp<AudioCommand> command = new AudioCommand();
67065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    command->mCommand = SET_PARAMETERS;
6710ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    sp<ParametersData> data = new ParametersData();
67265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    data->mIO = ioHandle;
673fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    data->mKeyValuePairs = String8(keyValuePairs);
67465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    command->mParam = data;
6750ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    command->mWaitStatus = true;
6763856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
677fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin            keyValuePairs, ioHandle, delayMs);
6780ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    return sendCommand(command, delayMs);
67965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
68065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
68165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
68265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
6830ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    sp<AudioCommand> command = new AudioCommand();
68465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    command->mCommand = SET_VOICE_VOLUME;
6850ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    sp<VoiceVolumeData> data = new VoiceVolumeData();
68665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    data->mVolume = volume;
68765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    command->mParam = data;
6880ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    command->mWaitStatus = true;
6893856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
6900ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    return sendCommand(command, delayMs);
69165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
69265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
693bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_io_handle_t output,
694bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                                                               audio_stream_type_t stream,
695e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent                                                               audio_session_t session)
696bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{
6970ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    sp<AudioCommand> command = new AudioCommand();
698bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    command->mCommand = STOP_OUTPUT;
6990ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    sp<StopOutputData> data = new StopOutputData();
700bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    data->mIO = output;
701bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    data->mStream = stream;
702bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    data->mSession = session;
70348412dc92c4cc92a3f4821616e12290f5eeabd07Jesper Tragardh    command->mParam = data;
704bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    ALOGV("AudioCommandThread() adding stop output %d", output);
7050ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    sendCommand(command);
706bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}
707bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
708e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurentvoid AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_io_handle_t output,
709e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent                                                                  audio_stream_type_t stream,
710e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent                                                                  audio_session_t session)
711bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{
7120ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    sp<AudioCommand> command = new AudioCommand();
713bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    command->mCommand = RELEASE_OUTPUT;
7140ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    sp<ReleaseOutputData> data = new ReleaseOutputData();
715bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    data->mIO = output;
716e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent    data->mStream = stream;
717e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent    data->mSession = session;
71848412dc92c4cc92a3f4821616e12290f5eeabd07Jesper Tragardh    command->mParam = data;
719bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    ALOGV("AudioCommandThread() adding release output %d", output);
7200ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    sendCommand(command);
7210ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent}
7220ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent
723951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurentstatus_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
724951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                                                const struct audio_patch *patch,
725951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                                                audio_patch_handle_t *handle,
726951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                                                int delayMs)
727951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent{
728951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    status_t status = NO_ERROR;
729951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent
730951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    sp<AudioCommand> command = new AudioCommand();
731951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    command->mCommand = CREATE_AUDIO_PATCH;
732951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    CreateAudioPatchData *data = new CreateAudioPatchData();
733951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    data->mPatch = *patch;
734951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    data->mHandle = *handle;
735951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    command->mParam = data;
736951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    command->mWaitStatus = true;
737951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
738951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    status = sendCommand(command, delayMs);
739951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    if (status == NO_ERROR) {
740951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent        *handle = data->mHandle;
741951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    }
742951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    return status;
743951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent}
744951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent
745951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurentstatus_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
746951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                                                 int delayMs)
747951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent{
748951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    sp<AudioCommand> command = new AudioCommand();
749951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    command->mCommand = RELEASE_AUDIO_PATCH;
750951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
751951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    data->mHandle = handle;
752951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    command->mParam = data;
753951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    command->mWaitStatus = true;
754951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
755951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    return sendCommand(command, delayMs);
756951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent}
757951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent
758b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurentvoid AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
759b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent{
760b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    sp<AudioCommand> command = new AudioCommand();
761b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    command->mCommand = UPDATE_AUDIOPORT_LIST;
762b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    ALOGV("AudioCommandThread() adding update audio port list");
763b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    sendCommand(command);
764b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent}
765b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent
766b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurentvoid AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
767b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent{
768b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    sp<AudioCommand>command = new AudioCommand();
769b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    command->mCommand = UPDATE_AUDIOPATCH_LIST;
770b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    ALOGV("AudioCommandThread() adding update audio patch list");
771b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    sendCommand(command);
772b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent}
773b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent
774e1715a465a29db625da9d0ea365edf371e39e201Eric Laurentstatus_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand(
775e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent                                            const struct audio_port_config *config, int delayMs)
776e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent{
777e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent    sp<AudioCommand> command = new AudioCommand();
778e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent    command->mCommand = SET_AUDIOPORT_CONFIG;
779e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent    SetAudioPortConfigData *data = new SetAudioPortConfigData();
780e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent    data->mConfig = *config;
781e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent    command->mParam = data;
782e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent    command->mWaitStatus = true;
783e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent    ALOGV("AudioCommandThread() adding set port config delay %d", delayMs);
784e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent    return sendCommand(command, delayMs);
785e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent}
786e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent
787de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivivoid AudioPolicyService::AudioCommandThread::dynamicPolicyMixStateUpdateCommand(
788de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi        String8 regId, int32_t state)
789de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi{
790de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi    sp<AudioCommand> command = new AudioCommand();
791de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi    command->mCommand = DYN_POLICY_MIX_STATE_UPDATE;
792de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi    DynPolicyMixStateUpdateData *data = new DynPolicyMixStateUpdateData();
793de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi    data->mRegId = regId;
794de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi    data->mState = state;
795de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi    command->mParam = data;
796de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi    ALOGV("AudioCommandThread() sending dynamic policy mix (id=%s) state update to %d",
797de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi            regId.string(), state);
798de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi    sendCommand(command);
799de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi}
800de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi
8010ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurentstatus_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
8020ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent{
8030ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    {
8040ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent        Mutex::Autolock _l(mLock);
8050ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent        insertCommand_l(command, delayMs);
8060ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent        mWaitWorkCV.signal();
8070ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    }
8080ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    Mutex::Autolock _l(command->mLock);
8090ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    while (command->mWaitStatus) {
8100ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent        nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
8110ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent        if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
8120ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent            command->mStatus = TIMED_OUT;
8130ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent            command->mWaitStatus = false;
8140ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent        }
8150ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    }
8160ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    return command->mStatus;
817bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}
818bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
81965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// insertCommand_l() must be called with mLock held
8200ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurentvoid AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
82165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
8228d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten    ssize_t i;  // not size_t because i will count down to -1
8230ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    Vector < sp<AudioCommand> > removedCommands;
82465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    command->mTime = systemTime() + milliseconds(delayMs);
82565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
82665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // acquire wake lock to make sure delayed commands are processed
827bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    if (mAudioCommands.isEmpty()) {
82865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
82965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
83065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
83165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // check same pending commands with later time stamps and eliminate them
83265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    for (i = mAudioCommands.size()-1; i >= 0; i--) {
8330ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent        sp<AudioCommand> command2 = mAudioCommands[i];
83465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
83565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (command2->mTime <= command->mTime) break;
836e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent
837e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent        // create audio patch or release audio patch commands are equivalent
838e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent        // with regard to filtering
839e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent        if ((command->mCommand == CREATE_AUDIO_PATCH) ||
840e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent                (command->mCommand == RELEASE_AUDIO_PATCH)) {
841e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent            if ((command2->mCommand != CREATE_AUDIO_PATCH) &&
842e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent                    (command2->mCommand != RELEASE_AUDIO_PATCH)) {
843e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent                continue;
844e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent            }
845e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent        } else if (command2->mCommand != command->mCommand) continue;
84665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
84765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        switch (command->mCommand) {
84865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        case SET_PARAMETERS: {
8490ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent            ParametersData *data = (ParametersData *)command->mParam.get();
8500ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent            ParametersData *data2 = (ParametersData *)command2->mParam.get();
85165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            if (data->mIO != data2->mIO) break;
8523856b090cd04ba5dd4a59a12430ed724d5995909Steve Block            ALOGV("Comparing parameter command %s to new command %s",
853de070137f11d346fba77605bd76a44c040a618fcEric Laurent                    data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
85465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            AudioParameter param = AudioParameter(data->mKeyValuePairs);
85565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
85665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            for (size_t j = 0; j < param.size(); j++) {
857e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten                String8 key;
858e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten                String8 value;
859e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten                param.getAt(j, key, value);
860e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten                for (size_t k = 0; k < param2.size(); k++) {
861e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten                    String8 key2;
862e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten                    String8 value2;
863e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten                    param2.getAt(k, key2, value2);
864e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten                    if (key2 == key) {
865e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten                        param2.remove(key2);
866e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten                        ALOGV("Filtering out parameter %s", key2.string());
867e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten                        break;
868e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten                    }
869e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten                }
87065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            }
87165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            // if all keys have been filtered out, remove the command.
87265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            // otherwise, update the key value pairs
87365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            if (param2.size() == 0) {
87465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                removedCommands.add(command2);
87565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            } else {
87665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                data2->mKeyValuePairs = param2.toString();
87765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            }
87821e5456821e3c107d09b0629adbf23c9e06e7c0bEric Laurent            command->mTime = command2->mTime;
87921e5456821e3c107d09b0629adbf23c9e06e7c0bEric Laurent            // force delayMs to non 0 so that code below does not request to wait for
88021e5456821e3c107d09b0629adbf23c9e06e7c0bEric Laurent            // command status as the command is now delayed
88121e5456821e3c107d09b0629adbf23c9e06e7c0bEric Laurent            delayMs = 1;
88265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        } break;
88365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
88465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        case SET_VOLUME: {
8850ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent            VolumeData *data = (VolumeData *)command->mParam.get();
8860ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent            VolumeData *data2 = (VolumeData *)command2->mParam.get();
88765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            if (data->mIO != data2->mIO) break;
88865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            if (data->mStream != data2->mStream) break;
8893856b090cd04ba5dd4a59a12430ed724d5995909Steve Block            ALOGV("Filtering out volume command on output %d for stream %d",
890de070137f11d346fba77605bd76a44c040a618fcEric Laurent                    data->mIO, data->mStream);
89165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            removedCommands.add(command2);
89221e5456821e3c107d09b0629adbf23c9e06e7c0bEric Laurent            command->mTime = command2->mTime;
89321e5456821e3c107d09b0629adbf23c9e06e7c0bEric Laurent            // force delayMs to non 0 so that code below does not request to wait for
89421e5456821e3c107d09b0629adbf23c9e06e7c0bEric Laurent            // command status as the command is now delayed
89521e5456821e3c107d09b0629adbf23c9e06e7c0bEric Laurent            delayMs = 1;
89665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        } break;
897e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent
898e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent        case CREATE_AUDIO_PATCH:
899e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent        case RELEASE_AUDIO_PATCH: {
900e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent            audio_patch_handle_t handle;
901a2d4a6dac432f0c4f543d79b9a63c55ae91f81d6Haynes Mathew George            struct audio_patch patch;
902e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent            if (command->mCommand == CREATE_AUDIO_PATCH) {
903e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent                handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle;
904a2d4a6dac432f0c4f543d79b9a63c55ae91f81d6Haynes Mathew George                patch = ((CreateAudioPatchData *)command->mParam.get())->mPatch;
905e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent            } else {
906e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent                handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle;
907e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent            }
908e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent            audio_patch_handle_t handle2;
909a2d4a6dac432f0c4f543d79b9a63c55ae91f81d6Haynes Mathew George            struct audio_patch patch2;
910e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent            if (command2->mCommand == CREATE_AUDIO_PATCH) {
911e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent                handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle;
912a2d4a6dac432f0c4f543d79b9a63c55ae91f81d6Haynes Mathew George                patch2 = ((CreateAudioPatchData *)command2->mParam.get())->mPatch;
913e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent            } else {
914e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent                handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle;
915e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent            }
916e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent            if (handle != handle2) break;
917a2d4a6dac432f0c4f543d79b9a63c55ae91f81d6Haynes Mathew George            /* Filter CREATE_AUDIO_PATCH commands only when they are issued for
918a2d4a6dac432f0c4f543d79b9a63c55ae91f81d6Haynes Mathew George               same output. */
919a2d4a6dac432f0c4f543d79b9a63c55ae91f81d6Haynes Mathew George            if( (command->mCommand == CREATE_AUDIO_PATCH) &&
920a2d4a6dac432f0c4f543d79b9a63c55ae91f81d6Haynes Mathew George                (command2->mCommand == CREATE_AUDIO_PATCH) ) {
921a2d4a6dac432f0c4f543d79b9a63c55ae91f81d6Haynes Mathew George                bool isOutputDiff = false;
922a2d4a6dac432f0c4f543d79b9a63c55ae91f81d6Haynes Mathew George                if (patch.num_sources == patch2.num_sources) {
923a2d4a6dac432f0c4f543d79b9a63c55ae91f81d6Haynes Mathew George                    for (unsigned count = 0; count < patch.num_sources; count++) {
924a2d4a6dac432f0c4f543d79b9a63c55ae91f81d6Haynes Mathew George                        if (patch.sources[count].id != patch2.sources[count].id) {
925a2d4a6dac432f0c4f543d79b9a63c55ae91f81d6Haynes Mathew George                            isOutputDiff = true;
926a2d4a6dac432f0c4f543d79b9a63c55ae91f81d6Haynes Mathew George                            break;
927a2d4a6dac432f0c4f543d79b9a63c55ae91f81d6Haynes Mathew George                        }
928a2d4a6dac432f0c4f543d79b9a63c55ae91f81d6Haynes Mathew George                    }
929a2d4a6dac432f0c4f543d79b9a63c55ae91f81d6Haynes Mathew George                    if (isOutputDiff)
930a2d4a6dac432f0c4f543d79b9a63c55ae91f81d6Haynes Mathew George                       break;
931a2d4a6dac432f0c4f543d79b9a63c55ae91f81d6Haynes Mathew George                }
932a2d4a6dac432f0c4f543d79b9a63c55ae91f81d6Haynes Mathew George            }
933e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent            ALOGV("Filtering out %s audio patch command for handle %d",
934e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent                  (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle);
935e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent            removedCommands.add(command2);
936e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent            command->mTime = command2->mTime;
937e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent            // force delayMs to non 0 so that code below does not request to wait for
938e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent            // command status as the command is now delayed
939e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent            delayMs = 1;
940e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent        } break;
941e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent
942de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi        case DYN_POLICY_MIX_STATE_UPDATE: {
943de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi
944de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi        } break;
945de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi
94665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        case START_TONE:
94765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        case STOP_TONE:
94865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        default:
94965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            break;
95065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
95165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
95265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
95365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // remove filtered commands
95465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    for (size_t j = 0; j < removedCommands.size(); j++) {
95565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        // removed commands always have time stamps greater than current command
95665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
9570ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent            if (mAudioCommands[k].get() == removedCommands[j].get()) {
9583856b090cd04ba5dd4a59a12430ed724d5995909Steve Block                ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
95965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                mAudioCommands.removeAt(k);
96065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                break;
96165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            }
96265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
96365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
96465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    removedCommands.clear();
96565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
966aa79befb301bc3451a19021b3fc9e9e6a1f231e8Eric Laurent    // Disable wait for status if delay is not 0.
967aa79befb301bc3451a19021b3fc9e9e6a1f231e8Eric Laurent    // Except for create audio patch command because the returned patch handle
968aa79befb301bc3451a19021b3fc9e9e6a1f231e8Eric Laurent    // is needed by audio policy manager
969aa79befb301bc3451a19021b3fc9e9e6a1f231e8Eric Laurent    if (delayMs != 0 && command->mCommand != CREATE_AUDIO_PATCH) {
970cec4abb2a39eafa75cf6894170a200322aa7226eEric Laurent        command->mWaitStatus = false;
971cec4abb2a39eafa75cf6894170a200322aa7226eEric Laurent    }
972cec4abb2a39eafa75cf6894170a200322aa7226eEric Laurent
97365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // insert command at the right place according to its time stamp
9741e693b55d888b9d3e0a2ce770ae2b72b59c1a317Eric Laurent    ALOGV("inserting command: %d at index %zd, num commands %zu",
9751e693b55d888b9d3e0a2ce770ae2b72b59c1a317Eric Laurent            command->mCommand, i+1, mAudioCommands.size());
97665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mAudioCommands.insertAt(command, i + 1);
97765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
97865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
97965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid AudioPolicyService::AudioCommandThread::exit()
98065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
9813856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("AudioCommandThread::exit");
98265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    {
98365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        AutoMutex _l(mLock);
98465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        requestExit();
98565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        mWaitWorkCV.signal();
98665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
98765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    requestExitAndWait();
98865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
98965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
99065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
99165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
99265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    snprintf(buffer, size, "   %02d      %06d.%03d  %01u    %p\n",
99365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            mCommand,
99465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            (int)ns2s(mTime),
99565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            (int)ns2ms(mTime)%1000,
99665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            mWaitStatus,
9970ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent            mParam.get());
99865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
99965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1000fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin/******* helpers for the service_ops callbacks defined below *********/
1001fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavinvoid AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
1002fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                       const char *keyValuePairs,
1003fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                       int delayMs)
1004fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin{
100572ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten    mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
1006fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                           delayMs);
1007fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin}
1008fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
1009fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavinint AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
1010fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                        float volume,
1011fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                        audio_io_handle_t output,
1012fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                        int delayMs)
1013fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin{
1014fff6d715a8db0daf08a50634f242c40268de3d49Glenn Kasten    return (int)mAudioCommandThread->volumeCommand(stream, volume,
101572ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten                                                   output, delayMs);
1016fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin}
1017fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
1018fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavinint AudioPolicyService::startTone(audio_policy_tone_t tone,
1019fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                  audio_stream_type_t stream)
1020fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin{
10216e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION) {
102229357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block        ALOGE("startTone: illegal tone requested (%d)", tone);
10236e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    }
10246e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    if (stream != AUDIO_STREAM_VOICE_CALL) {
102529357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block        ALOGE("startTone: illegal stream (%d) requested for tone %d", stream,
1026e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten            tone);
10276e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    }
1028fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING,
1029fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                          AUDIO_STREAM_VOICE_CALL);
1030fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    return 0;
1031fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin}
1032fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
1033fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavinint AudioPolicyService::stopTone()
1034fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin{
1035fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    mTonePlaybackThread->stopToneCommand();
1036fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    return 0;
1037fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin}
1038fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
1039fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavinint AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1040fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin{
1041fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1042fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin}
1043fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
1044fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavinextern "C" {
10452d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurentaudio_module_handle_t aps_load_hw_module(void *service __unused,
10462d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurent                                             const char *name);
10472d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurentaudio_io_handle_t aps_open_output(void *service __unused,
1048a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                                         audio_devices_t *pDevices,
1049a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                                         uint32_t *pSamplingRate,
1050a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                                         audio_format_t *pFormat,
1051a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                                         audio_channel_mask_t *pChannelMask,
1052a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                                         uint32_t *pLatencyMs,
10532d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurent                                         audio_output_flags_t flags);
1054fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
10552d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurentaudio_io_handle_t aps_open_output_on_module(void *service __unused,
1056a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                                                   audio_module_handle_t module,
1057a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                                                   audio_devices_t *pDevices,
1058a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                                                   uint32_t *pSamplingRate,
1059a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                                                   audio_format_t *pFormat,
1060a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                                                   audio_channel_mask_t *pChannelMask,
1061a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                                                   uint32_t *pLatencyMs,
1062ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald                                                   audio_output_flags_t flags,
10632d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurent                                                   const audio_offload_info_t *offloadInfo);
10642d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurentaudio_io_handle_t aps_open_dup_output(void *service __unused,
1065fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                                 audio_io_handle_t output1,
10662d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurent                                                 audio_io_handle_t output2);
10672d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurentint aps_close_output(void *service __unused, audio_io_handle_t output);
10682d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurentint aps_suspend_output(void *service __unused, audio_io_handle_t output);
10692d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurentint aps_restore_output(void *service __unused, audio_io_handle_t output);
10702d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurentaudio_io_handle_t aps_open_input(void *service __unused,
1071a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                                        audio_devices_t *pDevices,
1072a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                                        uint32_t *pSamplingRate,
1073a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                                        audio_format_t *pFormat,
1074a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                                        audio_channel_mask_t *pChannelMask,
10752d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurent                                        audio_in_acoustics_t acoustics __unused);
10762d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurentaudio_io_handle_t aps_open_input_on_module(void *service __unused,
1077a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                                                  audio_module_handle_t module,
1078a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                                                  audio_devices_t *pDevices,
1079a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                                                  uint32_t *pSamplingRate,
1080a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                                                  audio_format_t *pFormat,
10812d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurent                                                  audio_channel_mask_t *pChannelMask);
10822d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurentint aps_close_input(void *service __unused, audio_io_handle_t input);
10832d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurentint aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
10842d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurentint aps_move_effects(void *service __unused, int session,
1085fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                audio_io_handle_t src_output,
10862d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurent                                audio_io_handle_t dst_output);
10872d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurentchar * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
10882d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurent                                     const char *keys);
10892d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurentvoid aps_set_parameters(void *service, audio_io_handle_t io_handle,
10902d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurent                                   const char *kv_pairs, int delay_ms);
10912d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurentint aps_set_stream_volume(void *service, audio_stream_type_t stream,
1092fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                     float volume, audio_io_handle_t output,
10932d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurent                                     int delay_ms);
10942d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurentint aps_start_tone(void *service, audio_policy_tone_t tone,
10952d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurent                              audio_stream_type_t stream);
10962d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurentint aps_stop_tone(void *service);
10972d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurentint aps_set_voice_volume(void *service, float volume, int delay_ms);
10982d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurent};
1099fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
1100fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavinnamespace {
1101fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    struct audio_policy_service_ops aps_ops = {
110201d3acba9de861cb2b718338e787cff3566fc5ecGlenn Kasten        .open_output           = aps_open_output,
110301d3acba9de861cb2b718338e787cff3566fc5ecGlenn Kasten        .open_duplicate_output = aps_open_dup_output,
110401d3acba9de861cb2b718338e787cff3566fc5ecGlenn Kasten        .close_output          = aps_close_output,
110501d3acba9de861cb2b718338e787cff3566fc5ecGlenn Kasten        .suspend_output        = aps_suspend_output,
110601d3acba9de861cb2b718338e787cff3566fc5ecGlenn Kasten        .restore_output        = aps_restore_output,
110701d3acba9de861cb2b718338e787cff3566fc5ecGlenn Kasten        .open_input            = aps_open_input,
110801d3acba9de861cb2b718338e787cff3566fc5ecGlenn Kasten        .close_input           = aps_close_input,
110901d3acba9de861cb2b718338e787cff3566fc5ecGlenn Kasten        .set_stream_volume     = aps_set_stream_volume,
1110d2304db2fcb5112292105a0949a55986a4c9875fGlenn Kasten        .invalidate_stream     = aps_invalidate_stream,
111101d3acba9de861cb2b718338e787cff3566fc5ecGlenn Kasten        .set_parameters        = aps_set_parameters,
111201d3acba9de861cb2b718338e787cff3566fc5ecGlenn Kasten        .get_parameters        = aps_get_parameters,
111301d3acba9de861cb2b718338e787cff3566fc5ecGlenn Kasten        .start_tone            = aps_start_tone,
111401d3acba9de861cb2b718338e787cff3566fc5ecGlenn Kasten        .stop_tone             = aps_stop_tone,
111501d3acba9de861cb2b718338e787cff3566fc5ecGlenn Kasten        .set_voice_volume      = aps_set_voice_volume,
111601d3acba9de861cb2b718338e787cff3566fc5ecGlenn Kasten        .move_effects          = aps_move_effects,
111701d3acba9de861cb2b718338e787cff3566fc5ecGlenn Kasten        .load_hw_module        = aps_load_hw_module,
111801d3acba9de861cb2b718338e787cff3566fc5ecGlenn Kasten        .open_output_on_module = aps_open_output_on_module,
111901d3acba9de861cb2b718338e787cff3566fc5ecGlenn Kasten        .open_input_on_module  = aps_open_input_on_module,
1120fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    };
1121fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin}; // namespace <unnamed>
1122fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
112365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}; // namespace android
1124