AudioPolicyService.cpp revision 125902529aed131abc00210c36dc9ff03d9e8120
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{
152125902529aed131abc00210c36dc9ff03d9e8120Eric Laurent    if (client == 0) {
153125902529aed131abc00210c36dc9ff03d9e8120Eric Laurent        ALOGW("%s got NULL client", __FUNCTION__);
154125902529aed131abc00210c36dc9ff03d9e8120Eric Laurent        return;
155125902529aed131abc00210c36dc9ff03d9e8120Eric Laurent    }
1560ebd5f95b68a3a5c9e5509f21938c9e51e74d71bEric Laurent    Mutex::Autolock _l(mNotificationClientsLock);
157b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent
158b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    uid_t uid = IPCThreadState::self()->getCallingUid();
159b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    if (mNotificationClients.indexOfKey(uid) < 0) {
160b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent        sp<NotificationClient> notificationClient = new NotificationClient(this,
161b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                                                                           client,
162b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                                                                           uid);
163b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent        ALOGV("registerClient() client %p, uid %d", client.get(), uid);
164b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent
165b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent        mNotificationClients.add(uid, notificationClient);
166b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent
167f888020c6e2735624f2b2a30e72aca24e17b8b4dMarco Nelissen        sp<IBinder> binder = IInterface::asBinder(client);
168b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent        binder->linkToDeath(notificationClient);
169b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    }
170b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent}
171b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent
172e8726fea8a53bf3474aa3c6deaf2f6c1f565e694Eric Laurentvoid AudioPolicyService::setAudioPortCallbacksEnabled(bool enabled)
173e8726fea8a53bf3474aa3c6deaf2f6c1f565e694Eric Laurent{
174e8726fea8a53bf3474aa3c6deaf2f6c1f565e694Eric Laurent    Mutex::Autolock _l(mNotificationClientsLock);
175e8726fea8a53bf3474aa3c6deaf2f6c1f565e694Eric Laurent
176e8726fea8a53bf3474aa3c6deaf2f6c1f565e694Eric Laurent    uid_t uid = IPCThreadState::self()->getCallingUid();
177e8726fea8a53bf3474aa3c6deaf2f6c1f565e694Eric Laurent    if (mNotificationClients.indexOfKey(uid) < 0) {
178e8726fea8a53bf3474aa3c6deaf2f6c1f565e694Eric Laurent        return;
179e8726fea8a53bf3474aa3c6deaf2f6c1f565e694Eric Laurent    }
180e8726fea8a53bf3474aa3c6deaf2f6c1f565e694Eric Laurent    mNotificationClients.valueFor(uid)->setAudioPortCallbacksEnabled(enabled);
181e8726fea8a53bf3474aa3c6deaf2f6c1f565e694Eric Laurent}
182e8726fea8a53bf3474aa3c6deaf2f6c1f565e694Eric Laurent
183b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent// removeNotificationClient() is called when the client process dies.
184b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurentvoid AudioPolicyService::removeNotificationClient(uid_t uid)
185b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent{
1860ebd5f95b68a3a5c9e5509f21938c9e51e74d71bEric Laurent    {
1870ebd5f95b68a3a5c9e5509f21938c9e51e74d71bEric Laurent        Mutex::Autolock _l(mNotificationClientsLock);
1880ebd5f95b68a3a5c9e5509f21938c9e51e74d71bEric Laurent        mNotificationClients.removeItem(uid);
1890ebd5f95b68a3a5c9e5509f21938c9e51e74d71bEric Laurent    }
190b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent#ifndef USE_LEGACY_AUDIO_POLICY
1910ebd5f95b68a3a5c9e5509f21938c9e51e74d71bEric Laurent    {
1920ebd5f95b68a3a5c9e5509f21938c9e51e74d71bEric Laurent        Mutex::Autolock _l(mLock);
193b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent        if (mAudioPolicyManager) {
1948c7e6dac6f5eb38cef627dab92eac8b38513450cEric Laurent            mAudioPolicyManager->releaseResourcesForUid(uid);
195b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent        }
1960ebd5f95b68a3a5c9e5509f21938c9e51e74d71bEric Laurent    }
197b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent#endif
198b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent}
199b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent
200b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurentvoid AudioPolicyService::onAudioPortListUpdate()
201b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent{
202b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    mOutputCommandThread->updateAudioPortListCommand();
203b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent}
204b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent
205b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurentvoid AudioPolicyService::doOnAudioPortListUpdate()
206b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent{
2070ebd5f95b68a3a5c9e5509f21938c9e51e74d71bEric Laurent    Mutex::Autolock _l(mNotificationClientsLock);
208b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    for (size_t i = 0; i < mNotificationClients.size(); i++) {
209b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent        mNotificationClients.valueAt(i)->onAudioPortListUpdate();
210b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    }
211b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent}
212b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent
213b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurentvoid AudioPolicyService::onAudioPatchListUpdate()
214b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent{
215b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    mOutputCommandThread->updateAudioPatchListCommand();
21665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
21765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
218951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurentstatus_t AudioPolicyService::clientCreateAudioPatch(const struct audio_patch *patch,
219951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                                                audio_patch_handle_t *handle,
220951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                                                int delayMs)
221951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent{
222951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    return mAudioCommandThread->createAudioPatchCommand(patch, handle, delayMs);
223951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent}
224951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent
225951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurentstatus_t AudioPolicyService::clientReleaseAudioPatch(audio_patch_handle_t handle,
226951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                                                 int delayMs)
227951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent{
228951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    return mAudioCommandThread->releaseAudioPatchCommand(handle, delayMs);
229951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent}
230951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent
231b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurentvoid AudioPolicyService::doOnAudioPatchListUpdate()
232b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent{
2330ebd5f95b68a3a5c9e5509f21938c9e51e74d71bEric Laurent    Mutex::Autolock _l(mNotificationClientsLock);
234b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    for (size_t i = 0; i < mNotificationClients.size(); i++) {
235b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent        mNotificationClients.valueAt(i)->onAudioPatchListUpdate();
236b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    }
237b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent}
238b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent
239de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivivoid AudioPolicyService::onDynamicPolicyMixStateUpdate(String8 regId, int32_t state)
240de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi{
241de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi    ALOGV("AudioPolicyService::onDynamicPolicyMixStateUpdate(%s, %d)",
242de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi            regId.string(), state);
243de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi    mOutputCommandThread->dynamicPolicyMixStateUpdateCommand(regId, state);
244de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi}
245de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi
246de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivivoid AudioPolicyService::doOnDynamicPolicyMixStateUpdate(String8 regId, int32_t state)
247de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi{
248de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi    Mutex::Autolock _l(mNotificationClientsLock);
249de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi    for (size_t i = 0; i < mNotificationClients.size(); i++) {
250de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi        mNotificationClients.valueAt(i)->onDynamicPolicyMixStateUpdate(regId, state);
251de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi    }
252de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi}
253de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi
254e1715a465a29db625da9d0ea365edf371e39e201Eric Laurentstatus_t AudioPolicyService::clientSetAudioPortConfig(const struct audio_port_config *config,
255e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent                                                      int delayMs)
256e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent{
257e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent    return mAudioCommandThread->setAudioPortConfigCommand(config, delayMs);
258e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent}
259e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent
260b52c152d553556b2d227ffc943489de0c60b4b02Eric LaurentAudioPolicyService::NotificationClient::NotificationClient(const sp<AudioPolicyService>& service,
261b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                                                     const sp<IAudioPolicyServiceClient>& client,
262b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                                                     uid_t uid)
263e8726fea8a53bf3474aa3c6deaf2f6c1f565e694Eric Laurent    : mService(service), mUid(uid), mAudioPolicyServiceClient(client),
264e8726fea8a53bf3474aa3c6deaf2f6c1f565e694Eric Laurent      mAudioPortCallbacksEnabled(false)
265b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent{
266b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent}
267b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent
268b52c152d553556b2d227ffc943489de0c60b4b02Eric LaurentAudioPolicyService::NotificationClient::~NotificationClient()
269b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent{
270b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent}
271b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent
272b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurentvoid AudioPolicyService::NotificationClient::binderDied(const wp<IBinder>& who __unused)
273b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent{
274b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    sp<NotificationClient> keep(this);
275b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    sp<AudioPolicyService> service = mService.promote();
276b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    if (service != 0) {
277b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent        service->removeNotificationClient(mUid);
278b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    }
279b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent}
280b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent
281b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurentvoid AudioPolicyService::NotificationClient::onAudioPortListUpdate()
282b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent{
283e8726fea8a53bf3474aa3c6deaf2f6c1f565e694Eric Laurent    if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
284b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent        mAudioPolicyServiceClient->onAudioPortListUpdate();
285b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    }
286b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent}
287b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent
288b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurentvoid AudioPolicyService::NotificationClient::onAudioPatchListUpdate()
289b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent{
290e8726fea8a53bf3474aa3c6deaf2f6c1f565e694Eric Laurent    if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
291b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent        mAudioPolicyServiceClient->onAudioPatchListUpdate();
292b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    }
293b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent}
29457dae99c9fcd6becd2b5ed0c53f277ba4d7dbcfcEric Laurent
295de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivivoid AudioPolicyService::NotificationClient::onDynamicPolicyMixStateUpdate(
296de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi        String8 regId, int32_t state)
297de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi{
298de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi    if (mAudioPolicyServiceClient != 0) {
299de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi            mAudioPolicyServiceClient->onDynamicPolicyMixStateUpdate(regId, state);
300de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi    }
301de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi}
302de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi
303e8726fea8a53bf3474aa3c6deaf2f6c1f565e694Eric Laurentvoid AudioPolicyService::NotificationClient::setAudioPortCallbacksEnabled(bool enabled)
304e8726fea8a53bf3474aa3c6deaf2f6c1f565e694Eric Laurent{
305e8726fea8a53bf3474aa3c6deaf2f6c1f565e694Eric Laurent    mAudioPortCallbacksEnabled = enabled;
306e8726fea8a53bf3474aa3c6deaf2f6c1f565e694Eric Laurent}
307e8726fea8a53bf3474aa3c6deaf2f6c1f565e694Eric Laurent
308e8726fea8a53bf3474aa3c6deaf2f6c1f565e694Eric Laurent
30965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid AudioPolicyService::binderDied(const wp<IBinder>& who) {
310411e447c4b90298f5ff635429c53f94fbce4fff9Glenn Kasten    ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
311de070137f11d346fba77605bd76a44c040a618fcEric Laurent            IPCThreadState::self()->getCallingPid());
31265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
31365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
31465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatic bool tryLock(Mutex& mutex)
31565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
31665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    bool locked = false;
31765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    for (int i = 0; i < kDumpLockRetries; ++i) {
31865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (mutex.tryLock() == NO_ERROR) {
31965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            locked = true;
32065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            break;
32165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
32222ecc912a87099cff8cadc424cd12f85c118673fGlenn Kasten        usleep(kDumpLockSleepUs);
32365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
32465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return locked;
32565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
32665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
32765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioPolicyService::dumpInternals(int fd)
32865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
32965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    const size_t SIZE = 256;
33065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    char buffer[SIZE];
33165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    String8 result;
33265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
333dce54a1492c410ad0d93253b341fb33305337505Eric Laurent#ifdef USE_LEGACY_AUDIO_POLICY
334fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    snprintf(buffer, SIZE, "PolicyManager Interface: %p\n", mpAudioPolicy);
335dce54a1492c410ad0d93253b341fb33305337505Eric Laurent#else
336dce54a1492c410ad0d93253b341fb33305337505Eric Laurent    snprintf(buffer, SIZE, "AudioPolicyManager: %p\n", mAudioPolicyManager);
337dce54a1492c410ad0d93253b341fb33305337505Eric Laurent#endif
33865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    result.append(buffer);
33965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
34065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    result.append(buffer);
34165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    snprintf(buffer, SIZE, "Tones Thread: %p\n", mTonePlaybackThread.get());
34265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    result.append(buffer);
34365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
34465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    write(fd, result.string(), result.size());
34565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return NO_ERROR;
34665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
34765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
3480f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenstatus_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused)
34965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
35044deb053252a3bd2f57a007ab9560f4924f62394Glenn Kasten    if (!dumpAllowed()) {
35165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        dumpPermissionDenial(fd);
35265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    } else {
35365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        bool locked = tryLock(mLock);
35465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (!locked) {
35565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            String8 result(kDeadlockedString);
35665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            write(fd, result.string(), result.size());
35765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
35865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
35965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        dumpInternals(fd);
3609d1f02d74fd395ec4de6861147da289423f0ab6fGlenn Kasten        if (mAudioCommandThread != 0) {
36165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            mAudioCommandThread->dump(fd);
36265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
3639d1f02d74fd395ec4de6861147da289423f0ab6fGlenn Kasten        if (mTonePlaybackThread != 0) {
36465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            mTonePlaybackThread->dump(fd);
36565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
36665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
367dce54a1492c410ad0d93253b341fb33305337505Eric Laurent#ifdef USE_LEGACY_AUDIO_POLICY
368fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        if (mpAudioPolicy) {
369fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin            mpAudioPolicy->dump(mpAudioPolicy, fd);
37065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
371dce54a1492c410ad0d93253b341fb33305337505Eric Laurent#else
372dce54a1492c410ad0d93253b341fb33305337505Eric Laurent        if (mAudioPolicyManager) {
373dce54a1492c410ad0d93253b341fb33305337505Eric Laurent            mAudioPolicyManager->dump(fd);
374dce54a1492c410ad0d93253b341fb33305337505Eric Laurent        }
375dce54a1492c410ad0d93253b341fb33305337505Eric Laurent#endif
37665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
37765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (locked) mLock.unlock();
37865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
37965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return NO_ERROR;
38065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
38165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
38265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioPolicyService::dumpPermissionDenial(int fd)
38365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
38465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    const size_t SIZE = 256;
38565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    char buffer[SIZE];
38665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    String8 result;
38765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    snprintf(buffer, SIZE, "Permission Denial: "
38865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            "can't dump AudioPolicyService from pid=%d, uid=%d\n",
38965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            IPCThreadState::self()->getCallingPid(),
39065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            IPCThreadState::self()->getCallingUid());
39165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    result.append(buffer);
39265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    write(fd, result.string(), result.size());
39365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return NO_ERROR;
39465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
39565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
39665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioPolicyService::onTransact(
39765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
39865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
39965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return BnAudioPolicyService::onTransact(code, data, reply, flags);
40065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
40165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
40265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
40365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// -----------  AudioPolicyService::AudioCommandThread implementation ----------
40465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
405bfb1b832079bbb9426f72f3863199a54aefd02daEric LaurentAudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
406bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                                                           const wp<AudioPolicyService>& service)
407bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    : Thread(false), mName(name), mService(service)
40865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
40965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mpToneGenerator = NULL;
41065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
41165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
41265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
41365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias AgopianAudioPolicyService::AudioCommandThread::~AudioCommandThread()
41465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
415bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    if (!mAudioCommands.isEmpty()) {
41665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        release_wake_lock(mName.string());
41765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
41865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mAudioCommands.clear();
419e9dd0176933d6233916c84e18f3e8c0d644ca05dGlenn Kasten    delete mpToneGenerator;
42065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
42165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
42265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid AudioPolicyService::AudioCommandThread::onFirstRef()
42365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
424bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    run(mName.string(), ANDROID_PRIORITY_AUDIO);
42565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
42665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
42765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianbool AudioPolicyService::AudioCommandThread::threadLoop()
42865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
42965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    nsecs_t waitTime = INT64_MAX;
43065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
43165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mLock.lock();
43265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    while (!exitPending())
43365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    {
43459a8923027d2d6a155c0fa434ae65559318da4b6Eric Laurent        sp<AudioPolicyService> svc;
43559a8923027d2d6a155c0fa434ae65559318da4b6Eric Laurent        while (!mAudioCommands.isEmpty() && !exitPending()) {
43665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            nsecs_t curTime = systemTime();
43765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            // commands are sorted by increasing time stamp: execute them from index 0 and up
43865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            if (mAudioCommands[0]->mTime <= curTime) {
4390ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent                sp<AudioCommand> command = mAudioCommands[0];
44065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                mAudioCommands.removeAt(0);
4410ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent                mLastCommand = command;
44265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
44365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                switch (command->mCommand) {
44465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                case START_TONE: {
44565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    mLock.unlock();
4460ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent                    ToneData *data = (ToneData *)command->mParam.get();
4473856b090cd04ba5dd4a59a12430ed724d5995909Steve Block                    ALOGV("AudioCommandThread() processing start tone %d on stream %d",
44865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                            data->mType, data->mStream);
449e9dd0176933d6233916c84e18f3e8c0d644ca05dGlenn Kasten                    delete mpToneGenerator;
45065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    mpToneGenerator = new ToneGenerator(data->mStream, 1.0);
45165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    mpToneGenerator->startTone(data->mType);
45265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    mLock.lock();
45365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    }break;
45465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                case STOP_TONE: {
45565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    mLock.unlock();
4563856b090cd04ba5dd4a59a12430ed724d5995909Steve Block                    ALOGV("AudioCommandThread() processing stop tone");
45765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    if (mpToneGenerator != NULL) {
45865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                        mpToneGenerator->stopTone();
45965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                        delete mpToneGenerator;
46065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                        mpToneGenerator = NULL;
46165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    }
46265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    mLock.lock();
46365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    }break;
46465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                case SET_VOLUME: {
4650ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent                    VolumeData *data = (VolumeData *)command->mParam.get();
4663856b090cd04ba5dd4a59a12430ed724d5995909Steve Block                    ALOGV("AudioCommandThread() processing set volume stream %d, \
467de070137f11d346fba77605bd76a44c040a618fcEric Laurent                            volume %f, output %d", data->mStream, data->mVolume, data->mIO);
468de070137f11d346fba77605bd76a44c040a618fcEric Laurent                    command->mStatus = AudioSystem::setStreamVolume(data->mStream,
469de070137f11d346fba77605bd76a44c040a618fcEric Laurent                                                                    data->mVolume,
470de070137f11d346fba77605bd76a44c040a618fcEric Laurent                                                                    data->mIO);
47165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    }break;
47265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                case SET_PARAMETERS: {
4730ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent                    ParametersData *data = (ParametersData *)command->mParam.get();
474e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten                    ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
475e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten                            data->mKeyValuePairs.string(), data->mIO);
476e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten                    command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
477e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten                    }break;
47865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                case SET_VOICE_VOLUME: {
4790ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent                    VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
4803856b090cd04ba5dd4a59a12430ed724d5995909Steve Block                    ALOGV("AudioCommandThread() processing set voice volume volume %f",
481de070137f11d346fba77605bd76a44c040a618fcEric Laurent                            data->mVolume);
48265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
48365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    }break;
484bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                case STOP_OUTPUT: {
4850ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent                    StopOutputData *data = (StopOutputData *)command->mParam.get();
486bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    ALOGV("AudioCommandThread() processing stop output %d",
487bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                            data->mIO);
48859a8923027d2d6a155c0fa434ae65559318da4b6Eric Laurent                    svc = mService.promote();
489bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    if (svc == 0) {
490bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                        break;
491bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    }
492bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    mLock.unlock();
493bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    svc->doStopOutput(data->mIO, data->mStream, data->mSession);
494bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    mLock.lock();
495bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    }break;
496bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                case RELEASE_OUTPUT: {
4970ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent                    ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get();
498bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    ALOGV("AudioCommandThread() processing release output %d",
499bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                            data->mIO);
50059a8923027d2d6a155c0fa434ae65559318da4b6Eric Laurent                    svc = mService.promote();
501bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    if (svc == 0) {
502bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                        break;
503bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    }
504bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    mLock.unlock();
505e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent                    svc->doReleaseOutput(data->mIO, data->mStream, data->mSession);
506bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    mLock.lock();
507bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    }break;
508951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                case CREATE_AUDIO_PATCH: {
509951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                    CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get();
510951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                    ALOGV("AudioCommandThread() processing create audio patch");
511951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                    sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
512951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                    if (af == 0) {
513951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                        command->mStatus = PERMISSION_DENIED;
514951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                    } else {
515951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                        command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle);
516951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                    }
517951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                    } break;
518951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                case RELEASE_AUDIO_PATCH: {
519951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                    ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get();
520951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                    ALOGV("AudioCommandThread() processing release audio patch");
521951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                    sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
522951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                    if (af == 0) {
523951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                        command->mStatus = PERMISSION_DENIED;
524951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                    } else {
525951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                        command->mStatus = af->releaseAudioPatch(data->mHandle);
526951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                    }
527951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                    } break;
528b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                case UPDATE_AUDIOPORT_LIST: {
529b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                    ALOGV("AudioCommandThread() processing update audio port list");
53059a8923027d2d6a155c0fa434ae65559318da4b6Eric Laurent                    svc = mService.promote();
531b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                    if (svc == 0) {
532b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                        break;
533b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                    }
534b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                    mLock.unlock();
535b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                    svc->doOnAudioPortListUpdate();
536b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                    mLock.lock();
537b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                    }break;
538b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                case UPDATE_AUDIOPATCH_LIST: {
539b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                    ALOGV("AudioCommandThread() processing update audio patch list");
54059a8923027d2d6a155c0fa434ae65559318da4b6Eric Laurent                    svc = mService.promote();
541b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                    if (svc == 0) {
542b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                        break;
543b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                    }
544b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                    mLock.unlock();
545b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                    svc->doOnAudioPatchListUpdate();
546b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                    mLock.lock();
547b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                    }break;
548e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent                case SET_AUDIOPORT_CONFIG: {
549e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent                    SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get();
550e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent                    ALOGV("AudioCommandThread() processing set port config");
551e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent                    sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
552e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent                    if (af == 0) {
553e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent                        command->mStatus = PERMISSION_DENIED;
554e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent                    } else {
555e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent                        command->mStatus = af->setAudioPortConfig(&data->mConfig);
556e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent                    }
557e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent                    } break;
558de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi                case DYN_POLICY_MIX_STATE_UPDATE: {
559de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi                    DynPolicyMixStateUpdateData *data =
560de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi                            (DynPolicyMixStateUpdateData *)command->mParam.get();
561de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi                    //###ALOGV("AudioCommandThread() processing dyn policy mix state update");
562de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi                    ALOGV("AudioCommandThread() processing dyn policy mix state update %s %d",
563de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi                            data->mRegId.string(), data->mState);
564de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi                    svc = mService.promote();
565de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi                    if (svc == 0) {
566de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi                        break;
567de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi                    }
568de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi                    mLock.unlock();
569de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi                    svc->doOnDynamicPolicyMixStateUpdate(data->mRegId, data->mState);
570de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi                    mLock.lock();
571de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi                    } break;
57265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                default:
5735ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block                    ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
57465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                }
5750ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent                {
5760ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent                    Mutex::Autolock _l(command->mLock);
5770ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent                    if (command->mWaitStatus) {
5780ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent                        command->mWaitStatus = false;
5790ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent                        command->mCond.signal();
5800ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent                    }
5810ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent                }
58265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                waitTime = INT64_MAX;
58365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            } else {
58465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                waitTime = mAudioCommands[0]->mTime - curTime;
58565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                break;
58665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            }
58765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
58859a8923027d2d6a155c0fa434ae65559318da4b6Eric Laurent        // release mLock before releasing strong reference on the service as
58959a8923027d2d6a155c0fa434ae65559318da4b6Eric Laurent        // AudioPolicyService destructor calls AudioCommandThread::exit() which acquires mLock.
59059a8923027d2d6a155c0fa434ae65559318da4b6Eric Laurent        mLock.unlock();
59159a8923027d2d6a155c0fa434ae65559318da4b6Eric Laurent        svc.clear();
59259a8923027d2d6a155c0fa434ae65559318da4b6Eric Laurent        mLock.lock();
593884de21ca32e4a47f77e541a7284d02054ff3349Wally Yau        if (!exitPending() && (mAudioCommands.isEmpty() || waitTime != INT64_MAX)) {
59405f2fdc1070c1c07af9e21760d4d2e06bebde55aRicardo Garcia            // release delayed commands wake lock
59505f2fdc1070c1c07af9e21760d4d2e06bebde55aRicardo Garcia            release_wake_lock(mName.string());
59659a8923027d2d6a155c0fa434ae65559318da4b6Eric Laurent            ALOGV("AudioCommandThread() going to sleep");
59759a8923027d2d6a155c0fa434ae65559318da4b6Eric Laurent            mWaitWorkCV.waitRelative(mLock, waitTime);
59859a8923027d2d6a155c0fa434ae65559318da4b6Eric Laurent            ALOGV("AudioCommandThread() waking up");
59959a8923027d2d6a155c0fa434ae65559318da4b6Eric Laurent        }
60065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
60105f2fdc1070c1c07af9e21760d4d2e06bebde55aRicardo Garcia    // release delayed commands wake lock before quitting
60205f2fdc1070c1c07af9e21760d4d2e06bebde55aRicardo Garcia    if (!mAudioCommands.isEmpty()) {
60305f2fdc1070c1c07af9e21760d4d2e06bebde55aRicardo Garcia        release_wake_lock(mName.string());
60405f2fdc1070c1c07af9e21760d4d2e06bebde55aRicardo Garcia    }
60565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mLock.unlock();
60665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return false;
60765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
60865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
60965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioPolicyService::AudioCommandThread::dump(int fd)
61065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
61165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    const size_t SIZE = 256;
61265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    char buffer[SIZE];
61365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    String8 result;
61465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
61565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
61665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    result.append(buffer);
61765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    write(fd, result.string(), result.size());
61865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
61965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    bool locked = tryLock(mLock);
62065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (!locked) {
62165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        String8 result2(kCmdDeadlockedString);
62265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        write(fd, result2.string(), result2.size());
62365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
62465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
62565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    snprintf(buffer, SIZE, "- Commands:\n");
62665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    result = String8(buffer);
62765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    result.append("   Command Time        Wait pParam\n");
6288d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten    for (size_t i = 0; i < mAudioCommands.size(); i++) {
62965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        mAudioCommands[i]->dump(buffer, SIZE);
63065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        result.append(buffer);
63165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
63265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    result.append("  Last Command\n");
6330ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    if (mLastCommand != 0) {
6340ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent        mLastCommand->dump(buffer, SIZE);
6350ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent        result.append(buffer);
6360ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    } else {
6370ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent        result.append("     none\n");
6380ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    }
63965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
64065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    write(fd, result.string(), result.size());
64165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
64265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (locked) mLock.unlock();
64365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
64465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return NO_ERROR;
64565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
64665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
6473d2f877c1cb4e4ae4ddde7f57f4353de9341f11bGlenn Kastenvoid AudioPolicyService::AudioCommandThread::startToneCommand(ToneGenerator::tone_type type,
6483d2f877c1cb4e4ae4ddde7f57f4353de9341f11bGlenn Kasten        audio_stream_type_t stream)
64965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
6500ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    sp<AudioCommand> command = new AudioCommand();
65165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    command->mCommand = START_TONE;
6520ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    sp<ToneData> data = new ToneData();
65365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    data->mType = type;
65465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    data->mStream = stream;
65548412dc92c4cc92a3f4821616e12290f5eeabd07Jesper Tragardh    command->mParam = data;
6563856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream);
6570ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    sendCommand(command);
65865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
65965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
66065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid AudioPolicyService::AudioCommandThread::stopToneCommand()
66165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
6620ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    sp<AudioCommand> command = new AudioCommand();
66365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    command->mCommand = STOP_TONE;
6643856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("AudioCommandThread() adding tone stop");
6650ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    sendCommand(command);
66665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
66765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
668fff6d715a8db0daf08a50634f242c40268de3d49Glenn Kastenstatus_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
669de070137f11d346fba77605bd76a44c040a618fcEric Laurent                                                               float volume,
67072ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten                                                               audio_io_handle_t output,
671de070137f11d346fba77605bd76a44c040a618fcEric Laurent                                                               int delayMs)
67265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
6730ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    sp<AudioCommand> command = new AudioCommand();
67465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    command->mCommand = SET_VOLUME;
6750ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    sp<VolumeData> data = new VolumeData();
67665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    data->mStream = stream;
67765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    data->mVolume = volume;
67865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    data->mIO = output;
67965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    command->mParam = data;
6800ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    command->mWaitStatus = true;
6813856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
682de070137f11d346fba77605bd76a44c040a618fcEric Laurent            stream, volume, output);
6830ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    return sendCommand(command, delayMs);
68465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
68565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
68672ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenstatus_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
687fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                                                   const char *keyValuePairs,
688de070137f11d346fba77605bd76a44c040a618fcEric Laurent                                                                   int delayMs)
68965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
6900ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    sp<AudioCommand> command = new AudioCommand();
69165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    command->mCommand = SET_PARAMETERS;
6920ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    sp<ParametersData> data = new ParametersData();
69365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    data->mIO = ioHandle;
694fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    data->mKeyValuePairs = String8(keyValuePairs);
69565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    command->mParam = data;
6960ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    command->mWaitStatus = true;
6973856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
698fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin            keyValuePairs, ioHandle, delayMs);
6990ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    return sendCommand(command, delayMs);
70065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
70165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
70265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
70365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
7040ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    sp<AudioCommand> command = new AudioCommand();
70565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    command->mCommand = SET_VOICE_VOLUME;
7060ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    sp<VoiceVolumeData> data = new VoiceVolumeData();
70765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    data->mVolume = volume;
70865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    command->mParam = data;
7090ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    command->mWaitStatus = true;
7103856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
7110ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    return sendCommand(command, delayMs);
71265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
71365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
714bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_io_handle_t output,
715bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                                                               audio_stream_type_t stream,
716e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent                                                               audio_session_t session)
717bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{
7180ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    sp<AudioCommand> command = new AudioCommand();
719bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    command->mCommand = STOP_OUTPUT;
7200ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    sp<StopOutputData> data = new StopOutputData();
721bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    data->mIO = output;
722bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    data->mStream = stream;
723bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    data->mSession = session;
72448412dc92c4cc92a3f4821616e12290f5eeabd07Jesper Tragardh    command->mParam = data;
725bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    ALOGV("AudioCommandThread() adding stop output %d", output);
7260ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    sendCommand(command);
727bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}
728bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
729e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurentvoid AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_io_handle_t output,
730e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent                                                                  audio_stream_type_t stream,
731e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent                                                                  audio_session_t session)
732bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{
7330ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    sp<AudioCommand> command = new AudioCommand();
734bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    command->mCommand = RELEASE_OUTPUT;
7350ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    sp<ReleaseOutputData> data = new ReleaseOutputData();
736bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    data->mIO = output;
737e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent    data->mStream = stream;
738e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent    data->mSession = session;
73948412dc92c4cc92a3f4821616e12290f5eeabd07Jesper Tragardh    command->mParam = data;
740bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    ALOGV("AudioCommandThread() adding release output %d", output);
7410ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    sendCommand(command);
7420ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent}
7430ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent
744951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurentstatus_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
745951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                                                const struct audio_patch *patch,
746951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                                                audio_patch_handle_t *handle,
747951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                                                int delayMs)
748951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent{
749951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    status_t status = NO_ERROR;
750951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent
751951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    sp<AudioCommand> command = new AudioCommand();
752951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    command->mCommand = CREATE_AUDIO_PATCH;
753951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    CreateAudioPatchData *data = new CreateAudioPatchData();
754951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    data->mPatch = *patch;
755951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    data->mHandle = *handle;
756951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    command->mParam = data;
757951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    command->mWaitStatus = true;
758951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
759951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    status = sendCommand(command, delayMs);
760951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    if (status == NO_ERROR) {
761951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent        *handle = data->mHandle;
762951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    }
763951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    return status;
764951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent}
765951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent
766951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurentstatus_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
767951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                                                 int delayMs)
768951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent{
769951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    sp<AudioCommand> command = new AudioCommand();
770951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    command->mCommand = RELEASE_AUDIO_PATCH;
771951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
772951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    data->mHandle = handle;
773951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    command->mParam = data;
774951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    command->mWaitStatus = true;
775951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
776951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    return sendCommand(command, delayMs);
777951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent}
778951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent
779b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurentvoid AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
780b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent{
781b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    sp<AudioCommand> command = new AudioCommand();
782b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    command->mCommand = UPDATE_AUDIOPORT_LIST;
783b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    ALOGV("AudioCommandThread() adding update audio port list");
784b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    sendCommand(command);
785b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent}
786b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent
787b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurentvoid AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
788b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent{
789b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    sp<AudioCommand>command = new AudioCommand();
790b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    command->mCommand = UPDATE_AUDIOPATCH_LIST;
791b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    ALOGV("AudioCommandThread() adding update audio patch list");
792b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    sendCommand(command);
793b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent}
794b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent
795e1715a465a29db625da9d0ea365edf371e39e201Eric Laurentstatus_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand(
796e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent                                            const struct audio_port_config *config, int delayMs)
797e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent{
798e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent    sp<AudioCommand> command = new AudioCommand();
799e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent    command->mCommand = SET_AUDIOPORT_CONFIG;
800e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent    SetAudioPortConfigData *data = new SetAudioPortConfigData();
801e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent    data->mConfig = *config;
802e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent    command->mParam = data;
803e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent    command->mWaitStatus = true;
804e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent    ALOGV("AudioCommandThread() adding set port config delay %d", delayMs);
805e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent    return sendCommand(command, delayMs);
806e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent}
807e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent
808de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivivoid AudioPolicyService::AudioCommandThread::dynamicPolicyMixStateUpdateCommand(
809de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi        String8 regId, int32_t state)
810de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi{
811de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi    sp<AudioCommand> command = new AudioCommand();
812de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi    command->mCommand = DYN_POLICY_MIX_STATE_UPDATE;
813de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi    DynPolicyMixStateUpdateData *data = new DynPolicyMixStateUpdateData();
814de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi    data->mRegId = regId;
815de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi    data->mState = state;
816de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi    command->mParam = data;
817de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi    ALOGV("AudioCommandThread() sending dynamic policy mix (id=%s) state update to %d",
818de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi            regId.string(), state);
819de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi    sendCommand(command);
820de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi}
821de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi
8220ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurentstatus_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
8230ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent{
8240ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    {
8250ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent        Mutex::Autolock _l(mLock);
8260ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent        insertCommand_l(command, delayMs);
8270ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent        mWaitWorkCV.signal();
8280ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    }
8290ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    Mutex::Autolock _l(command->mLock);
8300ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    while (command->mWaitStatus) {
8310ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent        nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
8320ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent        if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
8330ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent            command->mStatus = TIMED_OUT;
8340ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent            command->mWaitStatus = false;
8350ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent        }
8360ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    }
8370ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    return command->mStatus;
838bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}
839bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
84065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// insertCommand_l() must be called with mLock held
8410ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurentvoid AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
84265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
8438d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten    ssize_t i;  // not size_t because i will count down to -1
8440ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    Vector < sp<AudioCommand> > removedCommands;
84565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    command->mTime = systemTime() + milliseconds(delayMs);
84665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
84765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // acquire wake lock to make sure delayed commands are processed
848bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    if (mAudioCommands.isEmpty()) {
84965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
85065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
85165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
85265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // check same pending commands with later time stamps and eliminate them
85365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    for (i = mAudioCommands.size()-1; i >= 0; i--) {
8540ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent        sp<AudioCommand> command2 = mAudioCommands[i];
85565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
85665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (command2->mTime <= command->mTime) break;
857e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent
858e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent        // create audio patch or release audio patch commands are equivalent
859e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent        // with regard to filtering
860e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent        if ((command->mCommand == CREATE_AUDIO_PATCH) ||
861e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent                (command->mCommand == RELEASE_AUDIO_PATCH)) {
862e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent            if ((command2->mCommand != CREATE_AUDIO_PATCH) &&
863e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent                    (command2->mCommand != RELEASE_AUDIO_PATCH)) {
864e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent                continue;
865e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent            }
866e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent        } else if (command2->mCommand != command->mCommand) continue;
86765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
86865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        switch (command->mCommand) {
86965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        case SET_PARAMETERS: {
8700ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent            ParametersData *data = (ParametersData *)command->mParam.get();
8710ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent            ParametersData *data2 = (ParametersData *)command2->mParam.get();
87265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            if (data->mIO != data2->mIO) break;
8733856b090cd04ba5dd4a59a12430ed724d5995909Steve Block            ALOGV("Comparing parameter command %s to new command %s",
874de070137f11d346fba77605bd76a44c040a618fcEric Laurent                    data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
87565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            AudioParameter param = AudioParameter(data->mKeyValuePairs);
87665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
87765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            for (size_t j = 0; j < param.size(); j++) {
878e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten                String8 key;
879e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten                String8 value;
880e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten                param.getAt(j, key, value);
881e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten                for (size_t k = 0; k < param2.size(); k++) {
882e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten                    String8 key2;
883e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten                    String8 value2;
884e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten                    param2.getAt(k, key2, value2);
885e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten                    if (key2 == key) {
886e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten                        param2.remove(key2);
887e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten                        ALOGV("Filtering out parameter %s", key2.string());
888e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten                        break;
889e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten                    }
890e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten                }
89165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            }
89265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            // if all keys have been filtered out, remove the command.
89365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            // otherwise, update the key value pairs
89465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            if (param2.size() == 0) {
89565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                removedCommands.add(command2);
89665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            } else {
89765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                data2->mKeyValuePairs = param2.toString();
89865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            }
89921e5456821e3c107d09b0629adbf23c9e06e7c0bEric Laurent            command->mTime = command2->mTime;
90021e5456821e3c107d09b0629adbf23c9e06e7c0bEric Laurent            // force delayMs to non 0 so that code below does not request to wait for
90121e5456821e3c107d09b0629adbf23c9e06e7c0bEric Laurent            // command status as the command is now delayed
90221e5456821e3c107d09b0629adbf23c9e06e7c0bEric Laurent            delayMs = 1;
90365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        } break;
90465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
90565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        case SET_VOLUME: {
9060ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent            VolumeData *data = (VolumeData *)command->mParam.get();
9070ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent            VolumeData *data2 = (VolumeData *)command2->mParam.get();
90865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            if (data->mIO != data2->mIO) break;
90965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            if (data->mStream != data2->mStream) break;
9103856b090cd04ba5dd4a59a12430ed724d5995909Steve Block            ALOGV("Filtering out volume command on output %d for stream %d",
911de070137f11d346fba77605bd76a44c040a618fcEric Laurent                    data->mIO, data->mStream);
91265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            removedCommands.add(command2);
91321e5456821e3c107d09b0629adbf23c9e06e7c0bEric Laurent            command->mTime = command2->mTime;
91421e5456821e3c107d09b0629adbf23c9e06e7c0bEric Laurent            // force delayMs to non 0 so that code below does not request to wait for
91521e5456821e3c107d09b0629adbf23c9e06e7c0bEric Laurent            // command status as the command is now delayed
91621e5456821e3c107d09b0629adbf23c9e06e7c0bEric Laurent            delayMs = 1;
91765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        } break;
918e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent
919e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent        case CREATE_AUDIO_PATCH:
920e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent        case RELEASE_AUDIO_PATCH: {
921e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent            audio_patch_handle_t handle;
922a2d4a6dac432f0c4f543d79b9a63c55ae91f81d6Haynes Mathew George            struct audio_patch patch;
923e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent            if (command->mCommand == CREATE_AUDIO_PATCH) {
924e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent                handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle;
925a2d4a6dac432f0c4f543d79b9a63c55ae91f81d6Haynes Mathew George                patch = ((CreateAudioPatchData *)command->mParam.get())->mPatch;
926e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent            } else {
927e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent                handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle;
928e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent            }
929e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent            audio_patch_handle_t handle2;
930a2d4a6dac432f0c4f543d79b9a63c55ae91f81d6Haynes Mathew George            struct audio_patch patch2;
931e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent            if (command2->mCommand == CREATE_AUDIO_PATCH) {
932e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent                handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle;
933a2d4a6dac432f0c4f543d79b9a63c55ae91f81d6Haynes Mathew George                patch2 = ((CreateAudioPatchData *)command2->mParam.get())->mPatch;
934e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent            } else {
935e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent                handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle;
936f60b6b6c776a762827874f6f635c0577359a7ee4Glenn Kasten                memset(&patch2, 0, sizeof(patch2));
937e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent            }
938e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent            if (handle != handle2) break;
939a2d4a6dac432f0c4f543d79b9a63c55ae91f81d6Haynes Mathew George            /* Filter CREATE_AUDIO_PATCH commands only when they are issued for
940a2d4a6dac432f0c4f543d79b9a63c55ae91f81d6Haynes Mathew George               same output. */
941a2d4a6dac432f0c4f543d79b9a63c55ae91f81d6Haynes Mathew George            if( (command->mCommand == CREATE_AUDIO_PATCH) &&
942a2d4a6dac432f0c4f543d79b9a63c55ae91f81d6Haynes Mathew George                (command2->mCommand == CREATE_AUDIO_PATCH) ) {
943a2d4a6dac432f0c4f543d79b9a63c55ae91f81d6Haynes Mathew George                bool isOutputDiff = false;
944a2d4a6dac432f0c4f543d79b9a63c55ae91f81d6Haynes Mathew George                if (patch.num_sources == patch2.num_sources) {
945a2d4a6dac432f0c4f543d79b9a63c55ae91f81d6Haynes Mathew George                    for (unsigned count = 0; count < patch.num_sources; count++) {
946a2d4a6dac432f0c4f543d79b9a63c55ae91f81d6Haynes Mathew George                        if (patch.sources[count].id != patch2.sources[count].id) {
947a2d4a6dac432f0c4f543d79b9a63c55ae91f81d6Haynes Mathew George                            isOutputDiff = true;
948a2d4a6dac432f0c4f543d79b9a63c55ae91f81d6Haynes Mathew George                            break;
949a2d4a6dac432f0c4f543d79b9a63c55ae91f81d6Haynes Mathew George                        }
950a2d4a6dac432f0c4f543d79b9a63c55ae91f81d6Haynes Mathew George                    }
951a2d4a6dac432f0c4f543d79b9a63c55ae91f81d6Haynes Mathew George                    if (isOutputDiff)
952a2d4a6dac432f0c4f543d79b9a63c55ae91f81d6Haynes Mathew George                       break;
953a2d4a6dac432f0c4f543d79b9a63c55ae91f81d6Haynes Mathew George                }
954a2d4a6dac432f0c4f543d79b9a63c55ae91f81d6Haynes Mathew George            }
955e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent            ALOGV("Filtering out %s audio patch command for handle %d",
956e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent                  (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle);
957e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent            removedCommands.add(command2);
958e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent            command->mTime = command2->mTime;
959e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent            // force delayMs to non 0 so that code below does not request to wait for
960e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent            // command status as the command is now delayed
961e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent            delayMs = 1;
962e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent        } break;
963e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent
964de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi        case DYN_POLICY_MIX_STATE_UPDATE: {
965de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi
966de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi        } break;
967de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi
96865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        case START_TONE:
96965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        case STOP_TONE:
97065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        default:
97165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            break;
97265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
97365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
97465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
97565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // remove filtered commands
97665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    for (size_t j = 0; j < removedCommands.size(); j++) {
97765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        // removed commands always have time stamps greater than current command
97865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
9790ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent            if (mAudioCommands[k].get() == removedCommands[j].get()) {
9803856b090cd04ba5dd4a59a12430ed724d5995909Steve Block                ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
98165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                mAudioCommands.removeAt(k);
98265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                break;
98365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            }
98465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
98565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
98665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    removedCommands.clear();
98765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
988aa79befb301bc3451a19021b3fc9e9e6a1f231e8Eric Laurent    // Disable wait for status if delay is not 0.
989aa79befb301bc3451a19021b3fc9e9e6a1f231e8Eric Laurent    // Except for create audio patch command because the returned patch handle
990aa79befb301bc3451a19021b3fc9e9e6a1f231e8Eric Laurent    // is needed by audio policy manager
991aa79befb301bc3451a19021b3fc9e9e6a1f231e8Eric Laurent    if (delayMs != 0 && command->mCommand != CREATE_AUDIO_PATCH) {
992cec4abb2a39eafa75cf6894170a200322aa7226eEric Laurent        command->mWaitStatus = false;
993cec4abb2a39eafa75cf6894170a200322aa7226eEric Laurent    }
994cec4abb2a39eafa75cf6894170a200322aa7226eEric Laurent
99565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // insert command at the right place according to its time stamp
9961e693b55d888b9d3e0a2ce770ae2b72b59c1a317Eric Laurent    ALOGV("inserting command: %d at index %zd, num commands %zu",
9971e693b55d888b9d3e0a2ce770ae2b72b59c1a317Eric Laurent            command->mCommand, i+1, mAudioCommands.size());
99865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mAudioCommands.insertAt(command, i + 1);
99965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
100065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
100165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid AudioPolicyService::AudioCommandThread::exit()
100265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
10033856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("AudioCommandThread::exit");
100465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    {
100565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        AutoMutex _l(mLock);
100665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        requestExit();
100765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        mWaitWorkCV.signal();
100865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
100965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    requestExitAndWait();
101065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
101165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
101265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
101365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
101465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    snprintf(buffer, size, "   %02d      %06d.%03d  %01u    %p\n",
101565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            mCommand,
101665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            (int)ns2s(mTime),
101765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            (int)ns2ms(mTime)%1000,
101865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            mWaitStatus,
10190ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent            mParam.get());
102065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
102165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1022fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin/******* helpers for the service_ops callbacks defined below *********/
1023fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavinvoid AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
1024fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                       const char *keyValuePairs,
1025fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                       int delayMs)
1026fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin{
102772ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten    mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
1028fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                           delayMs);
1029fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin}
1030fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
1031fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavinint AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
1032fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                        float volume,
1033fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                        audio_io_handle_t output,
1034fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                        int delayMs)
1035fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin{
1036fff6d715a8db0daf08a50634f242c40268de3d49Glenn Kasten    return (int)mAudioCommandThread->volumeCommand(stream, volume,
103772ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten                                                   output, delayMs);
1038fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin}
1039fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
1040fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavinint AudioPolicyService::startTone(audio_policy_tone_t tone,
1041fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                  audio_stream_type_t stream)
1042fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin{
10436e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION) {
104429357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block        ALOGE("startTone: illegal tone requested (%d)", tone);
10456e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    }
10466e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    if (stream != AUDIO_STREAM_VOICE_CALL) {
104729357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block        ALOGE("startTone: illegal stream (%d) requested for tone %d", stream,
1048e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten            tone);
10496e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    }
1050fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING,
1051fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                          AUDIO_STREAM_VOICE_CALL);
1052fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    return 0;
1053fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin}
1054fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
1055fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavinint AudioPolicyService::stopTone()
1056fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin{
1057fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    mTonePlaybackThread->stopToneCommand();
1058fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    return 0;
1059fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin}
1060fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
1061fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavinint AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1062fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin{
1063fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1064fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin}
1065fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
1066fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavinextern "C" {
10672d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurentaudio_module_handle_t aps_load_hw_module(void *service __unused,
10682d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurent                                             const char *name);
10692d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurentaudio_io_handle_t aps_open_output(void *service __unused,
1070a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                                         audio_devices_t *pDevices,
1071a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                                         uint32_t *pSamplingRate,
1072a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                                         audio_format_t *pFormat,
1073a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                                         audio_channel_mask_t *pChannelMask,
1074a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                                         uint32_t *pLatencyMs,
10752d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurent                                         audio_output_flags_t flags);
1076fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
10772d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurentaudio_io_handle_t aps_open_output_on_module(void *service __unused,
1078a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                                                   audio_module_handle_t module,
1079a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                                                   audio_devices_t *pDevices,
1080a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                                                   uint32_t *pSamplingRate,
1081a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                                                   audio_format_t *pFormat,
1082a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                                                   audio_channel_mask_t *pChannelMask,
1083a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                                                   uint32_t *pLatencyMs,
1084ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald                                                   audio_output_flags_t flags,
10852d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurent                                                   const audio_offload_info_t *offloadInfo);
10862d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurentaudio_io_handle_t aps_open_dup_output(void *service __unused,
1087fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                                 audio_io_handle_t output1,
10882d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurent                                                 audio_io_handle_t output2);
10892d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurentint aps_close_output(void *service __unused, audio_io_handle_t output);
10902d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurentint aps_suspend_output(void *service __unused, audio_io_handle_t output);
10912d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurentint aps_restore_output(void *service __unused, audio_io_handle_t output);
10922d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurentaudio_io_handle_t aps_open_input(void *service __unused,
1093a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                                        audio_devices_t *pDevices,
1094a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                                        uint32_t *pSamplingRate,
1095a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                                        audio_format_t *pFormat,
1096a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                                        audio_channel_mask_t *pChannelMask,
10972d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurent                                        audio_in_acoustics_t acoustics __unused);
10982d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurentaudio_io_handle_t aps_open_input_on_module(void *service __unused,
1099a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                                                  audio_module_handle_t module,
1100a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                                                  audio_devices_t *pDevices,
1101a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                                                  uint32_t *pSamplingRate,
1102a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                                                  audio_format_t *pFormat,
11032d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurent                                                  audio_channel_mask_t *pChannelMask);
11042d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurentint aps_close_input(void *service __unused, audio_io_handle_t input);
11052d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurentint aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
11062d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurentint aps_move_effects(void *service __unused, int session,
1107fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                audio_io_handle_t src_output,
11082d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurent                                audio_io_handle_t dst_output);
11092d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurentchar * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
11102d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurent                                     const char *keys);
11112d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurentvoid aps_set_parameters(void *service, audio_io_handle_t io_handle,
11122d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurent                                   const char *kv_pairs, int delay_ms);
11132d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurentint aps_set_stream_volume(void *service, audio_stream_type_t stream,
1114fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                     float volume, audio_io_handle_t output,
11152d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurent                                     int delay_ms);
11162d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurentint aps_start_tone(void *service, audio_policy_tone_t tone,
11172d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurent                              audio_stream_type_t stream);
11182d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurentint aps_stop_tone(void *service);
11192d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurentint aps_set_voice_volume(void *service, float volume, int delay_ms);
11202d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurent};
1121fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
1122fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavinnamespace {
1123fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    struct audio_policy_service_ops aps_ops = {
112401d3acba9de861cb2b718338e787cff3566fc5ecGlenn Kasten        .open_output           = aps_open_output,
112501d3acba9de861cb2b718338e787cff3566fc5ecGlenn Kasten        .open_duplicate_output = aps_open_dup_output,
112601d3acba9de861cb2b718338e787cff3566fc5ecGlenn Kasten        .close_output          = aps_close_output,
112701d3acba9de861cb2b718338e787cff3566fc5ecGlenn Kasten        .suspend_output        = aps_suspend_output,
112801d3acba9de861cb2b718338e787cff3566fc5ecGlenn Kasten        .restore_output        = aps_restore_output,
112901d3acba9de861cb2b718338e787cff3566fc5ecGlenn Kasten        .open_input            = aps_open_input,
113001d3acba9de861cb2b718338e787cff3566fc5ecGlenn Kasten        .close_input           = aps_close_input,
113101d3acba9de861cb2b718338e787cff3566fc5ecGlenn Kasten        .set_stream_volume     = aps_set_stream_volume,
1132d2304db2fcb5112292105a0949a55986a4c9875fGlenn Kasten        .invalidate_stream     = aps_invalidate_stream,
113301d3acba9de861cb2b718338e787cff3566fc5ecGlenn Kasten        .set_parameters        = aps_set_parameters,
113401d3acba9de861cb2b718338e787cff3566fc5ecGlenn Kasten        .get_parameters        = aps_get_parameters,
113501d3acba9de861cb2b718338e787cff3566fc5ecGlenn Kasten        .start_tone            = aps_start_tone,
113601d3acba9de861cb2b718338e787cff3566fc5ecGlenn Kasten        .stop_tone             = aps_stop_tone,
113701d3acba9de861cb2b718338e787cff3566fc5ecGlenn Kasten        .set_voice_volume      = aps_set_voice_volume,
113801d3acba9de861cb2b718338e787cff3566fc5ecGlenn Kasten        .move_effects          = aps_move_effects,
113901d3acba9de861cb2b718338e787cff3566fc5ecGlenn Kasten        .load_hw_module        = aps_load_hw_module,
114001d3acba9de861cb2b718338e787cff3566fc5ecGlenn Kasten        .open_output_on_module = aps_open_output_on_module,
114101d3acba9de861cb2b718338e787cff3566fc5ecGlenn Kasten        .open_input_on_module  = aps_open_input_on_module,
1142fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    };
1143fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin}; // namespace <unnamed>
1144fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
114565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}; // namespace android
1146