AudioPolicyService.cpp revision e8726fea8a53bf3474aa3c6deaf2f6c1f565e694
165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian/*
265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian * Copyright (C) 2009 The Android Open Source Project
365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian *
465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian * Licensed under the Apache License, Version 2.0 (the "License");
565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian * you may not use this file except in compliance with the License.
665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian * You may obtain a copy of the License at
765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian *
865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian *      http://www.apache.org/licenses/LICENSE-2.0
965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian *
1065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian * Unless required by applicable law or agreed to in writing, software
1165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian * distributed under the License is distributed on an "AS IS" BASIS,
1265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian * See the License for the specific language governing permissions and
1465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian * limitations under the License.
1565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian */
1665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#define LOG_TAG "AudioPolicyService"
1865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian//#define LOG_NDEBUG 0
1965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
20153b9fe667e6e78e0218ff0159353097428c7657Glenn Kasten#include "Configuration.h"
2165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#undef __STRICT_ANSI__
2265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#define __STDINT_LIMITS
2365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#define __STDC_LIMIT_MACROS
2465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <stdint.h>
2565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
2665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <sys/time.h>
2765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <binder/IServiceManager.h>
2865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <utils/Log.h>
2965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <cutils/properties.h>
3065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <binder/IPCThreadState.h>
3165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <utils/String16.h>
3265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <utils/threads.h>
3365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include "AudioPolicyService.h"
3444deb053252a3bd2f57a007ab9560f4924f62394Glenn Kasten#include "ServiceUtilities.h"
3565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian#include <hardware_legacy/power.h>
367c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent#include <media/AudioEffect.h>
377c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent#include <media/EffectsFactoryApi.h>
38c84d9d235679a4d48245b316b7b2e4c0b19413e7Chih-Hung Hsieh#include <media/AudioParameter.h>
3965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
40fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin#include <hardware/hardware.h>
4164760240f931714858a59c1579f07264d7182ba2Dima Zavin#include <system/audio.h>
427394a4f358fa9908a9f0a7c954b65c399f4268e6Dima Zavin#include <system/audio_policy.h>
43fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin#include <hardware/audio_policy.h>
44fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
4565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopiannamespace android {
4665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
478dad0e31c2366c501bd1d600261d0af35a6ca786Glenn Kastenstatic const char kDeadlockedString[] = "AudioPolicyService may be deadlocked\n";
488dad0e31c2366c501bd1d600261d0af35a6ca786Glenn Kastenstatic const char kCmdDeadlockedString[] = "AudioPolicyService command thread may be deadlocked\n";
4965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
5065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatic const int kDumpLockRetries = 50;
5122ecc912a87099cff8cadc424cd12f85c118673fGlenn Kastenstatic const int kDumpLockSleepUs = 20000;
5265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
530ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurentstatic const nsecs_t kAudioCommandTimeoutNs = seconds(3); // 3 seconds
545fa8c4bf81275d5e1f5ef71bc66fc22e3152eeb0Christer Fletcher
55fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavinnamespace {
56fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    extern struct audio_policy_service_ops aps_ops;
57fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin};
58fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
5965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// ----------------------------------------------------------------------------
6065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
6165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias AgopianAudioPolicyService::AudioPolicyService()
62dce54a1492c410ad0d93253b341fb33305337505Eric Laurent    : BnAudioPolicyService(), mpAudioPolicyDev(NULL), mpAudioPolicy(NULL),
63bb6c9a05840d924b502ce0f1868fca4881ada1edEric Laurent      mAudioPolicyManager(NULL), mAudioPolicyClient(NULL), mPhoneState(AUDIO_MODE_INVALID)
6465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
65f5ada6edcc83bb1b313abcd4c4fbdc4f7724f04dEric Laurent}
66f5ada6edcc83bb1b313abcd4c4fbdc4f7724f04dEric Laurent
67f5ada6edcc83bb1b313abcd4c4fbdc4f7724f04dEric Laurentvoid AudioPolicyService::onFirstRef()
68f5ada6edcc83bb1b313abcd4c4fbdc4f7724f04dEric Laurent{
6965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    char value[PROPERTY_VALUE_MAX];
70fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    const struct hw_module_t *module;
71fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    int forced_val;
72fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    int rc;
7365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
748b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent    {
758b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent        Mutex::Autolock _l(mLock);
76935752053ef2691dbb6d5a6d149e0e362c6e3c74Eric Laurent
778b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent        // start tone playback thread
788b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent        mTonePlaybackThread = new AudioCommandThread(String8("ApmTone"), this);
798b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent        // start audio commands thread
808b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent        mAudioCommandThread = new AudioCommandThread(String8("ApmAudio"), this);
818b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent        // start output activity command thread
828b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent        mOutputCommandThread = new AudioCommandThread(String8("ApmOutput"), this);
83dce54a1492c410ad0d93253b341fb33305337505Eric Laurent
84dce54a1492c410ad0d93253b341fb33305337505Eric Laurent#ifdef USE_LEGACY_AUDIO_POLICY
858b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent        ALOGI("AudioPolicyService CSTOR in legacy mode");
868b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent
878b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent        /* instantiate the audio policy manager */
888b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent        rc = hw_get_module(AUDIO_POLICY_HARDWARE_MODULE_ID, &module);
898b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent        if (rc) {
908b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent            return;
918b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent        }
928b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent        rc = audio_policy_dev_open(module, &mpAudioPolicyDev);
938b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent        ALOGE_IF(rc, "couldn't open audio policy device (%s)", strerror(-rc));
948b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent        if (rc) {
958b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent            return;
968b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent        }
978b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent
988b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent        rc = mpAudioPolicyDev->create_audio_policy(mpAudioPolicyDev, &aps_ops, this,
998b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent                                                   &mpAudioPolicy);
1008b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent        ALOGE_IF(rc, "couldn't create audio policy (%s)", strerror(-rc));
1018b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent        if (rc) {
1028b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent            return;
1038b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent        }
1048b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent
1058b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent        rc = mpAudioPolicy->init_check(mpAudioPolicy);
1068b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent        ALOGE_IF(rc, "couldn't init_check the audio policy (%s)", strerror(-rc));
1078b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent        if (rc) {
1088b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent            return;
1098b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent        }
1108b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent        ALOGI("Loaded audio policy from %s (%s)", module->name, module->id);
111dce54a1492c410ad0d93253b341fb33305337505Eric Laurent#else
1128b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent        ALOGI("AudioPolicyService CSTOR in new mode");
113dce54a1492c410ad0d93253b341fb33305337505Eric Laurent
1148b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent        mAudioPolicyClient = new AudioPolicyClient(this);
1158b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent        mAudioPolicyManager = createAudioPolicyManager(mAudioPolicyClient);
116dce54a1492c410ad0d93253b341fb33305337505Eric Laurent#endif
1178b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent    }
118ba2b43990a7b4f0f2c425cf6cdfc63376a45772cbryant_liu    // load audio processing modules
1198b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent    sp<AudioPolicyEffects>audioPolicyEffects = new AudioPolicyEffects();
1208b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent    {
1218b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent        Mutex::Autolock _l(mLock);
1228b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent        mAudioPolicyEffects = audioPolicyEffects;
1238b1e80bf1e9f214feea219cfe981ba533b806003Eric Laurent    }
12465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
12565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
12665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias AgopianAudioPolicyService::~AudioPolicyService()
12765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
12865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mTonePlaybackThread->exit();
12965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mAudioCommandThread->exit();
130657ff61389d7316f798d4abe03efac56fd528d91Eric Laurent    mOutputCommandThread->exit();
1317c7f10bd4fda9a084e5e7f0eb3a040dfcbf01745Eric Laurent
132dce54a1492c410ad0d93253b341fb33305337505Eric Laurent#ifdef USE_LEGACY_AUDIO_POLICY
1336e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    if (mpAudioPolicy != NULL && mpAudioPolicyDev != NULL) {
134fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        mpAudioPolicyDev->destroy_audio_policy(mpAudioPolicyDev, mpAudioPolicy);
1356e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    }
1366e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    if (mpAudioPolicyDev != NULL) {
137fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        audio_policy_dev_close(mpAudioPolicyDev);
1386e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    }
139dce54a1492c410ad0d93253b341fb33305337505Eric Laurent#else
140f269b8e0e9ab950fc6652b9594b7a3431c81630cEric Laurent    destroyAudioPolicyManager(mAudioPolicyManager);
141dce54a1492c410ad0d93253b341fb33305337505Eric Laurent    delete mAudioPolicyClient;
142dce54a1492c410ad0d93253b341fb33305337505Eric Laurent#endif
143b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent
144b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    mNotificationClients.clear();
145ba2b43990a7b4f0f2c425cf6cdfc63376a45772cbryant_liu    mAudioPolicyEffects.clear();
146b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent}
147b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent
148b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent// A notification client is always registered by AudioSystem when the client process
149b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent// connects to AudioPolicyService.
150b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurentvoid AudioPolicyService::registerClient(const sp<IAudioPolicyServiceClient>& client)
151b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent{
152b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent
1530ebd5f95b68a3a5c9e5509f21938c9e51e74d71bEric Laurent    Mutex::Autolock _l(mNotificationClientsLock);
154b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent
155b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    uid_t uid = IPCThreadState::self()->getCallingUid();
156b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    if (mNotificationClients.indexOfKey(uid) < 0) {
157b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent        sp<NotificationClient> notificationClient = new NotificationClient(this,
158b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                                                                           client,
159b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                                                                           uid);
160b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent        ALOGV("registerClient() client %p, uid %d", client.get(), uid);
161b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent
162b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent        mNotificationClients.add(uid, notificationClient);
163b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent
164f888020c6e2735624f2b2a30e72aca24e17b8b4dMarco Nelissen        sp<IBinder> binder = IInterface::asBinder(client);
165b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent        binder->linkToDeath(notificationClient);
166b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    }
167b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent}
168b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent
169e8726fea8a53bf3474aa3c6deaf2f6c1f565e694Eric Laurentvoid AudioPolicyService::setAudioPortCallbacksEnabled(bool enabled)
170e8726fea8a53bf3474aa3c6deaf2f6c1f565e694Eric Laurent{
171e8726fea8a53bf3474aa3c6deaf2f6c1f565e694Eric Laurent    Mutex::Autolock _l(mNotificationClientsLock);
172e8726fea8a53bf3474aa3c6deaf2f6c1f565e694Eric Laurent
173e8726fea8a53bf3474aa3c6deaf2f6c1f565e694Eric Laurent    uid_t uid = IPCThreadState::self()->getCallingUid();
174e8726fea8a53bf3474aa3c6deaf2f6c1f565e694Eric Laurent    if (mNotificationClients.indexOfKey(uid) < 0) {
175e8726fea8a53bf3474aa3c6deaf2f6c1f565e694Eric Laurent        return;
176e8726fea8a53bf3474aa3c6deaf2f6c1f565e694Eric Laurent    }
177e8726fea8a53bf3474aa3c6deaf2f6c1f565e694Eric Laurent    mNotificationClients.valueFor(uid)->setAudioPortCallbacksEnabled(enabled);
178e8726fea8a53bf3474aa3c6deaf2f6c1f565e694Eric Laurent}
179e8726fea8a53bf3474aa3c6deaf2f6c1f565e694Eric Laurent
180b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent// removeNotificationClient() is called when the client process dies.
181b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurentvoid AudioPolicyService::removeNotificationClient(uid_t uid)
182b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent{
1830ebd5f95b68a3a5c9e5509f21938c9e51e74d71bEric Laurent    {
1840ebd5f95b68a3a5c9e5509f21938c9e51e74d71bEric Laurent        Mutex::Autolock _l(mNotificationClientsLock);
1850ebd5f95b68a3a5c9e5509f21938c9e51e74d71bEric Laurent        mNotificationClients.removeItem(uid);
1860ebd5f95b68a3a5c9e5509f21938c9e51e74d71bEric Laurent    }
187b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent#ifndef USE_LEGACY_AUDIO_POLICY
1880ebd5f95b68a3a5c9e5509f21938c9e51e74d71bEric Laurent    {
1890ebd5f95b68a3a5c9e5509f21938c9e51e74d71bEric Laurent        Mutex::Autolock _l(mLock);
190b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent        if (mAudioPolicyManager) {
1918c7e6dac6f5eb38cef627dab92eac8b38513450cEric Laurent            mAudioPolicyManager->releaseResourcesForUid(uid);
192b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent        }
1930ebd5f95b68a3a5c9e5509f21938c9e51e74d71bEric Laurent    }
194b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent#endif
195b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent}
196b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent
197b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurentvoid AudioPolicyService::onAudioPortListUpdate()
198b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent{
199b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    mOutputCommandThread->updateAudioPortListCommand();
200b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent}
201b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent
202b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurentvoid AudioPolicyService::doOnAudioPortListUpdate()
203b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent{
2040ebd5f95b68a3a5c9e5509f21938c9e51e74d71bEric Laurent    Mutex::Autolock _l(mNotificationClientsLock);
205b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    for (size_t i = 0; i < mNotificationClients.size(); i++) {
206b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent        mNotificationClients.valueAt(i)->onAudioPortListUpdate();
207b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    }
208b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent}
209b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent
210b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurentvoid AudioPolicyService::onAudioPatchListUpdate()
211b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent{
212b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    mOutputCommandThread->updateAudioPatchListCommand();
21365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
21465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
215951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurentstatus_t AudioPolicyService::clientCreateAudioPatch(const struct audio_patch *patch,
216951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                                                audio_patch_handle_t *handle,
217951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                                                int delayMs)
218951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent{
219951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    return mAudioCommandThread->createAudioPatchCommand(patch, handle, delayMs);
220951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent}
221951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent
222951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurentstatus_t AudioPolicyService::clientReleaseAudioPatch(audio_patch_handle_t handle,
223951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                                                 int delayMs)
224951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent{
225951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    return mAudioCommandThread->releaseAudioPatchCommand(handle, delayMs);
226951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent}
227951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent
228b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurentvoid AudioPolicyService::doOnAudioPatchListUpdate()
229b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent{
2300ebd5f95b68a3a5c9e5509f21938c9e51e74d71bEric Laurent    Mutex::Autolock _l(mNotificationClientsLock);
231b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    for (size_t i = 0; i < mNotificationClients.size(); i++) {
232b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent        mNotificationClients.valueAt(i)->onAudioPatchListUpdate();
233b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    }
234b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent}
235b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent
236de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivivoid AudioPolicyService::onDynamicPolicyMixStateUpdate(String8 regId, int32_t state)
237de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi{
238de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi    ALOGV("AudioPolicyService::onDynamicPolicyMixStateUpdate(%s, %d)",
239de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi            regId.string(), state);
240de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi    mOutputCommandThread->dynamicPolicyMixStateUpdateCommand(regId, state);
241de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi}
242de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi
243de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivivoid AudioPolicyService::doOnDynamicPolicyMixStateUpdate(String8 regId, int32_t state)
244de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi{
245de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi    Mutex::Autolock _l(mNotificationClientsLock);
246de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi    for (size_t i = 0; i < mNotificationClients.size(); i++) {
247de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi        mNotificationClients.valueAt(i)->onDynamicPolicyMixStateUpdate(regId, state);
248de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi    }
249de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi}
250de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi
251e1715a465a29db625da9d0ea365edf371e39e201Eric Laurentstatus_t AudioPolicyService::clientSetAudioPortConfig(const struct audio_port_config *config,
252e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent                                                      int delayMs)
253e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent{
254e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent    return mAudioCommandThread->setAudioPortConfigCommand(config, delayMs);
255e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent}
256e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent
257b52c152d553556b2d227ffc943489de0c60b4b02Eric LaurentAudioPolicyService::NotificationClient::NotificationClient(const sp<AudioPolicyService>& service,
258b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                                                     const sp<IAudioPolicyServiceClient>& client,
259b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                                                     uid_t uid)
260e8726fea8a53bf3474aa3c6deaf2f6c1f565e694Eric Laurent    : mService(service), mUid(uid), mAudioPolicyServiceClient(client),
261e8726fea8a53bf3474aa3c6deaf2f6c1f565e694Eric Laurent      mAudioPortCallbacksEnabled(false)
262b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent{
263b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent}
264b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent
265b52c152d553556b2d227ffc943489de0c60b4b02Eric LaurentAudioPolicyService::NotificationClient::~NotificationClient()
266b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent{
267b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent}
268b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent
269b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurentvoid AudioPolicyService::NotificationClient::binderDied(const wp<IBinder>& who __unused)
270b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent{
271b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    sp<NotificationClient> keep(this);
272b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    sp<AudioPolicyService> service = mService.promote();
273b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    if (service != 0) {
274b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent        service->removeNotificationClient(mUid);
275b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    }
276b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent}
277b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent
278b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurentvoid AudioPolicyService::NotificationClient::onAudioPortListUpdate()
279b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent{
280e8726fea8a53bf3474aa3c6deaf2f6c1f565e694Eric Laurent    if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
281b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent        mAudioPolicyServiceClient->onAudioPortListUpdate();
282b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    }
283b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent}
284b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent
285b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurentvoid AudioPolicyService::NotificationClient::onAudioPatchListUpdate()
286b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent{
287e8726fea8a53bf3474aa3c6deaf2f6c1f565e694Eric Laurent    if (mAudioPolicyServiceClient != 0 && mAudioPortCallbacksEnabled) {
288b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent        mAudioPolicyServiceClient->onAudioPatchListUpdate();
289b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    }
290b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent}
29157dae99c9fcd6becd2b5ed0c53f277ba4d7dbcfcEric Laurent
292de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivivoid AudioPolicyService::NotificationClient::onDynamicPolicyMixStateUpdate(
293de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi        String8 regId, int32_t state)
294de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi{
295de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi    if (mAudioPolicyServiceClient != 0) {
296de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi            mAudioPolicyServiceClient->onDynamicPolicyMixStateUpdate(regId, state);
297de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi    }
298de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi}
299de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi
300e8726fea8a53bf3474aa3c6deaf2f6c1f565e694Eric Laurentvoid AudioPolicyService::NotificationClient::setAudioPortCallbacksEnabled(bool enabled)
301e8726fea8a53bf3474aa3c6deaf2f6c1f565e694Eric Laurent{
302e8726fea8a53bf3474aa3c6deaf2f6c1f565e694Eric Laurent    mAudioPortCallbacksEnabled = enabled;
303e8726fea8a53bf3474aa3c6deaf2f6c1f565e694Eric Laurent}
304e8726fea8a53bf3474aa3c6deaf2f6c1f565e694Eric Laurent
305e8726fea8a53bf3474aa3c6deaf2f6c1f565e694Eric Laurent
30665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid AudioPolicyService::binderDied(const wp<IBinder>& who) {
307411e447c4b90298f5ff635429c53f94fbce4fff9Glenn Kasten    ALOGW("binderDied() %p, calling pid %d", who.unsafe_get(),
308de070137f11d346fba77605bd76a44c040a618fcEric Laurent            IPCThreadState::self()->getCallingPid());
30965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
31065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
31165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatic bool tryLock(Mutex& mutex)
31265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
31365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    bool locked = false;
31465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    for (int i = 0; i < kDumpLockRetries; ++i) {
31565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (mutex.tryLock() == NO_ERROR) {
31665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            locked = true;
31765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            break;
31865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
31922ecc912a87099cff8cadc424cd12f85c118673fGlenn Kasten        usleep(kDumpLockSleepUs);
32065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
32165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return locked;
32265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
32365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
32465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioPolicyService::dumpInternals(int fd)
32565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
32665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    const size_t SIZE = 256;
32765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    char buffer[SIZE];
32865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    String8 result;
32965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
330dce54a1492c410ad0d93253b341fb33305337505Eric Laurent#ifdef USE_LEGACY_AUDIO_POLICY
331fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    snprintf(buffer, SIZE, "PolicyManager Interface: %p\n", mpAudioPolicy);
332dce54a1492c410ad0d93253b341fb33305337505Eric Laurent#else
333dce54a1492c410ad0d93253b341fb33305337505Eric Laurent    snprintf(buffer, SIZE, "AudioPolicyManager: %p\n", mAudioPolicyManager);
334dce54a1492c410ad0d93253b341fb33305337505Eric Laurent#endif
33565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    result.append(buffer);
33665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    snprintf(buffer, SIZE, "Command Thread: %p\n", mAudioCommandThread.get());
33765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    result.append(buffer);
33865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    snprintf(buffer, SIZE, "Tones Thread: %p\n", mTonePlaybackThread.get());
33965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    result.append(buffer);
34065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
34165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    write(fd, result.string(), result.size());
34265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return NO_ERROR;
34365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
34465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
3450f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenstatus_t AudioPolicyService::dump(int fd, const Vector<String16>& args __unused)
34665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
34744deb053252a3bd2f57a007ab9560f4924f62394Glenn Kasten    if (!dumpAllowed()) {
34865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        dumpPermissionDenial(fd);
34965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    } else {
35065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        bool locked = tryLock(mLock);
35165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (!locked) {
35265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            String8 result(kDeadlockedString);
35365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            write(fd, result.string(), result.size());
35465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
35565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
35665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        dumpInternals(fd);
3579d1f02d74fd395ec4de6861147da289423f0ab6fGlenn Kasten        if (mAudioCommandThread != 0) {
35865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            mAudioCommandThread->dump(fd);
35965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
3609d1f02d74fd395ec4de6861147da289423f0ab6fGlenn Kasten        if (mTonePlaybackThread != 0) {
36165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            mTonePlaybackThread->dump(fd);
36265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
36365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
364dce54a1492c410ad0d93253b341fb33305337505Eric Laurent#ifdef USE_LEGACY_AUDIO_POLICY
365fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin        if (mpAudioPolicy) {
366fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin            mpAudioPolicy->dump(mpAudioPolicy, fd);
36765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
368dce54a1492c410ad0d93253b341fb33305337505Eric Laurent#else
369dce54a1492c410ad0d93253b341fb33305337505Eric Laurent        if (mAudioPolicyManager) {
370dce54a1492c410ad0d93253b341fb33305337505Eric Laurent            mAudioPolicyManager->dump(fd);
371dce54a1492c410ad0d93253b341fb33305337505Eric Laurent        }
372dce54a1492c410ad0d93253b341fb33305337505Eric Laurent#endif
37365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
37465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (locked) mLock.unlock();
37565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
37665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return NO_ERROR;
37765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
37865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
37965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioPolicyService::dumpPermissionDenial(int fd)
38065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
38165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    const size_t SIZE = 256;
38265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    char buffer[SIZE];
38365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    String8 result;
38465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    snprintf(buffer, SIZE, "Permission Denial: "
38565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            "can't dump AudioPolicyService from pid=%d, uid=%d\n",
38665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            IPCThreadState::self()->getCallingPid(),
38765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            IPCThreadState::self()->getCallingUid());
38865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    result.append(buffer);
38965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    write(fd, result.string(), result.size());
39065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return NO_ERROR;
39165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
39265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
39365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioPolicyService::onTransact(
39465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
39565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
39665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return BnAudioPolicyService::onTransact(code, data, reply, flags);
39765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
39865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
39965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
40065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// -----------  AudioPolicyService::AudioCommandThread implementation ----------
40165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
402bfb1b832079bbb9426f72f3863199a54aefd02daEric LaurentAudioPolicyService::AudioCommandThread::AudioCommandThread(String8 name,
403bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                                                           const wp<AudioPolicyService>& service)
404bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    : Thread(false), mName(name), mService(service)
40565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
40665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mpToneGenerator = NULL;
40765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
40865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
40965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
41065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias AgopianAudioPolicyService::AudioCommandThread::~AudioCommandThread()
41165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
412bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    if (!mAudioCommands.isEmpty()) {
41365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        release_wake_lock(mName.string());
41465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
41565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mAudioCommands.clear();
416e9dd0176933d6233916c84e18f3e8c0d644ca05dGlenn Kasten    delete mpToneGenerator;
41765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
41865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
41965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid AudioPolicyService::AudioCommandThread::onFirstRef()
42065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
421bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    run(mName.string(), ANDROID_PRIORITY_AUDIO);
42265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
42365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
42465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianbool AudioPolicyService::AudioCommandThread::threadLoop()
42565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
42665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    nsecs_t waitTime = INT64_MAX;
42765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
42865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mLock.lock();
42965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    while (!exitPending())
43065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    {
43159a8923027d2d6a155c0fa434ae65559318da4b6Eric Laurent        sp<AudioPolicyService> svc;
43259a8923027d2d6a155c0fa434ae65559318da4b6Eric Laurent        while (!mAudioCommands.isEmpty() && !exitPending()) {
43365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            nsecs_t curTime = systemTime();
43465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            // commands are sorted by increasing time stamp: execute them from index 0 and up
43565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            if (mAudioCommands[0]->mTime <= curTime) {
4360ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent                sp<AudioCommand> command = mAudioCommands[0];
43765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                mAudioCommands.removeAt(0);
4380ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent                mLastCommand = command;
43965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
44065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                switch (command->mCommand) {
44165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                case START_TONE: {
44265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    mLock.unlock();
4430ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent                    ToneData *data = (ToneData *)command->mParam.get();
4443856b090cd04ba5dd4a59a12430ed724d5995909Steve Block                    ALOGV("AudioCommandThread() processing start tone %d on stream %d",
44565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                            data->mType, data->mStream);
446e9dd0176933d6233916c84e18f3e8c0d644ca05dGlenn Kasten                    delete mpToneGenerator;
44765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    mpToneGenerator = new ToneGenerator(data->mStream, 1.0);
44865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    mpToneGenerator->startTone(data->mType);
44965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    mLock.lock();
45065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    }break;
45165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                case STOP_TONE: {
45265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    mLock.unlock();
4533856b090cd04ba5dd4a59a12430ed724d5995909Steve Block                    ALOGV("AudioCommandThread() processing stop tone");
45465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    if (mpToneGenerator != NULL) {
45565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                        mpToneGenerator->stopTone();
45665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                        delete mpToneGenerator;
45765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                        mpToneGenerator = NULL;
45865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    }
45965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    mLock.lock();
46065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    }break;
46165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                case SET_VOLUME: {
4620ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent                    VolumeData *data = (VolumeData *)command->mParam.get();
4633856b090cd04ba5dd4a59a12430ed724d5995909Steve Block                    ALOGV("AudioCommandThread() processing set volume stream %d, \
464de070137f11d346fba77605bd76a44c040a618fcEric Laurent                            volume %f, output %d", data->mStream, data->mVolume, data->mIO);
465de070137f11d346fba77605bd76a44c040a618fcEric Laurent                    command->mStatus = AudioSystem::setStreamVolume(data->mStream,
466de070137f11d346fba77605bd76a44c040a618fcEric Laurent                                                                    data->mVolume,
467de070137f11d346fba77605bd76a44c040a618fcEric Laurent                                                                    data->mIO);
46865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    }break;
46965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                case SET_PARAMETERS: {
4700ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent                    ParametersData *data = (ParametersData *)command->mParam.get();
471e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten                    ALOGV("AudioCommandThread() processing set parameters string %s, io %d",
472e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten                            data->mKeyValuePairs.string(), data->mIO);
473e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten                    command->mStatus = AudioSystem::setParameters(data->mIO, data->mKeyValuePairs);
474e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten                    }break;
47565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                case SET_VOICE_VOLUME: {
4760ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent                    VoiceVolumeData *data = (VoiceVolumeData *)command->mParam.get();
4773856b090cd04ba5dd4a59a12430ed724d5995909Steve Block                    ALOGV("AudioCommandThread() processing set voice volume volume %f",
478de070137f11d346fba77605bd76a44c040a618fcEric Laurent                            data->mVolume);
47965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    command->mStatus = AudioSystem::setVoiceVolume(data->mVolume);
48065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                    }break;
481bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                case STOP_OUTPUT: {
4820ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent                    StopOutputData *data = (StopOutputData *)command->mParam.get();
483bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    ALOGV("AudioCommandThread() processing stop output %d",
484bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                            data->mIO);
48559a8923027d2d6a155c0fa434ae65559318da4b6Eric Laurent                    svc = mService.promote();
486bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    if (svc == 0) {
487bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                        break;
488bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    }
489bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    mLock.unlock();
490bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    svc->doStopOutput(data->mIO, data->mStream, data->mSession);
491bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    mLock.lock();
492bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    }break;
493bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                case RELEASE_OUTPUT: {
4940ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent                    ReleaseOutputData *data = (ReleaseOutputData *)command->mParam.get();
495bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    ALOGV("AudioCommandThread() processing release output %d",
496bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                            data->mIO);
49759a8923027d2d6a155c0fa434ae65559318da4b6Eric Laurent                    svc = mService.promote();
498bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    if (svc == 0) {
499bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                        break;
500bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    }
501bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    mLock.unlock();
502e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent                    svc->doReleaseOutput(data->mIO, data->mStream, data->mSession);
503bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    mLock.lock();
504bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    }break;
505951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                case CREATE_AUDIO_PATCH: {
506951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                    CreateAudioPatchData *data = (CreateAudioPatchData *)command->mParam.get();
507951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                    ALOGV("AudioCommandThread() processing create audio patch");
508951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                    sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
509951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                    if (af == 0) {
510951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                        command->mStatus = PERMISSION_DENIED;
511951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                    } else {
512951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                        command->mStatus = af->createAudioPatch(&data->mPatch, &data->mHandle);
513951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                    }
514951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                    } break;
515951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                case RELEASE_AUDIO_PATCH: {
516951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                    ReleaseAudioPatchData *data = (ReleaseAudioPatchData *)command->mParam.get();
517951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                    ALOGV("AudioCommandThread() processing release audio patch");
518951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                    sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
519951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                    if (af == 0) {
520951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                        command->mStatus = PERMISSION_DENIED;
521951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                    } else {
522951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                        command->mStatus = af->releaseAudioPatch(data->mHandle);
523951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                    }
524951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                    } break;
525b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                case UPDATE_AUDIOPORT_LIST: {
526b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                    ALOGV("AudioCommandThread() processing update audio port list");
52759a8923027d2d6a155c0fa434ae65559318da4b6Eric Laurent                    svc = mService.promote();
528b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                    if (svc == 0) {
529b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                        break;
530b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                    }
531b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                    mLock.unlock();
532b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                    svc->doOnAudioPortListUpdate();
533b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                    mLock.lock();
534b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                    }break;
535b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                case UPDATE_AUDIOPATCH_LIST: {
536b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                    ALOGV("AudioCommandThread() processing update audio patch list");
53759a8923027d2d6a155c0fa434ae65559318da4b6Eric Laurent                    svc = mService.promote();
538b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                    if (svc == 0) {
539b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                        break;
540b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                    }
541b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                    mLock.unlock();
542b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                    svc->doOnAudioPatchListUpdate();
543b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                    mLock.lock();
544b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent                    }break;
545e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent                case SET_AUDIOPORT_CONFIG: {
546e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent                    SetAudioPortConfigData *data = (SetAudioPortConfigData *)command->mParam.get();
547e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent                    ALOGV("AudioCommandThread() processing set port config");
548e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent                    sp<IAudioFlinger> af = AudioSystem::get_audio_flinger();
549e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent                    if (af == 0) {
550e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent                        command->mStatus = PERMISSION_DENIED;
551e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent                    } else {
552e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent                        command->mStatus = af->setAudioPortConfig(&data->mConfig);
553e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent                    }
554e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent                    } break;
555de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi                case DYN_POLICY_MIX_STATE_UPDATE: {
556de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi                    DynPolicyMixStateUpdateData *data =
557de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi                            (DynPolicyMixStateUpdateData *)command->mParam.get();
558de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi                    //###ALOGV("AudioCommandThread() processing dyn policy mix state update");
559de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi                    ALOGV("AudioCommandThread() processing dyn policy mix state update %s %d",
560de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi                            data->mRegId.string(), data->mState);
561de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi                    svc = mService.promote();
562de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi                    if (svc == 0) {
563de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi                        break;
564de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi                    }
565de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi                    mLock.unlock();
566de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi                    svc->doOnDynamicPolicyMixStateUpdate(data->mRegId, data->mState);
567de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi                    mLock.lock();
568de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi                    } break;
56965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                default:
5705ff1dd576bb93c45b44088a51544a18fc43ebf58Steve Block                    ALOGW("AudioCommandThread() unknown command %d", command->mCommand);
57165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                }
5720ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent                {
5730ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent                    Mutex::Autolock _l(command->mLock);
5740ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent                    if (command->mWaitStatus) {
5750ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent                        command->mWaitStatus = false;
5760ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent                        command->mCond.signal();
5770ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent                    }
5780ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent                }
57965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                waitTime = INT64_MAX;
58065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            } else {
58165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                waitTime = mAudioCommands[0]->mTime - curTime;
58265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                break;
58365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            }
58465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
58559a8923027d2d6a155c0fa434ae65559318da4b6Eric Laurent        // release mLock before releasing strong reference on the service as
58659a8923027d2d6a155c0fa434ae65559318da4b6Eric Laurent        // AudioPolicyService destructor calls AudioCommandThread::exit() which acquires mLock.
58759a8923027d2d6a155c0fa434ae65559318da4b6Eric Laurent        mLock.unlock();
58859a8923027d2d6a155c0fa434ae65559318da4b6Eric Laurent        svc.clear();
58959a8923027d2d6a155c0fa434ae65559318da4b6Eric Laurent        mLock.lock();
590884de21ca32e4a47f77e541a7284d02054ff3349Wally Yau        if (!exitPending() && (mAudioCommands.isEmpty() || waitTime != INT64_MAX)) {
59105f2fdc1070c1c07af9e21760d4d2e06bebde55aRicardo Garcia            // release delayed commands wake lock
59205f2fdc1070c1c07af9e21760d4d2e06bebde55aRicardo Garcia            release_wake_lock(mName.string());
59359a8923027d2d6a155c0fa434ae65559318da4b6Eric Laurent            ALOGV("AudioCommandThread() going to sleep");
59459a8923027d2d6a155c0fa434ae65559318da4b6Eric Laurent            mWaitWorkCV.waitRelative(mLock, waitTime);
59559a8923027d2d6a155c0fa434ae65559318da4b6Eric Laurent            ALOGV("AudioCommandThread() waking up");
59659a8923027d2d6a155c0fa434ae65559318da4b6Eric Laurent        }
59765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
59805f2fdc1070c1c07af9e21760d4d2e06bebde55aRicardo Garcia    // release delayed commands wake lock before quitting
59905f2fdc1070c1c07af9e21760d4d2e06bebde55aRicardo Garcia    if (!mAudioCommands.isEmpty()) {
60005f2fdc1070c1c07af9e21760d4d2e06bebde55aRicardo Garcia        release_wake_lock(mName.string());
60105f2fdc1070c1c07af9e21760d4d2e06bebde55aRicardo Garcia    }
60265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mLock.unlock();
60365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return false;
60465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
60565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
60665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioPolicyService::AudioCommandThread::dump(int fd)
60765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
60865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    const size_t SIZE = 256;
60965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    char buffer[SIZE];
61065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    String8 result;
61165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
61265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    snprintf(buffer, SIZE, "AudioCommandThread %p Dump\n", this);
61365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    result.append(buffer);
61465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    write(fd, result.string(), result.size());
61565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
61665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    bool locked = tryLock(mLock);
61765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (!locked) {
61865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        String8 result2(kCmdDeadlockedString);
61965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        write(fd, result2.string(), result2.size());
62065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
62165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
62265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    snprintf(buffer, SIZE, "- Commands:\n");
62365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    result = String8(buffer);
62465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    result.append("   Command Time        Wait pParam\n");
6258d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten    for (size_t i = 0; i < mAudioCommands.size(); i++) {
62665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        mAudioCommands[i]->dump(buffer, SIZE);
62765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        result.append(buffer);
62865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
62965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    result.append("  Last Command\n");
6300ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    if (mLastCommand != 0) {
6310ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent        mLastCommand->dump(buffer, SIZE);
6320ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent        result.append(buffer);
6330ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    } else {
6340ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent        result.append("     none\n");
6350ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    }
63665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
63765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    write(fd, result.string(), result.size());
63865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
63965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    if (locked) mLock.unlock();
64065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
64165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    return NO_ERROR;
64265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
64365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
6443d2f877c1cb4e4ae4ddde7f57f4353de9341f11bGlenn Kastenvoid AudioPolicyService::AudioCommandThread::startToneCommand(ToneGenerator::tone_type type,
6453d2f877c1cb4e4ae4ddde7f57f4353de9341f11bGlenn Kasten        audio_stream_type_t stream)
64665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
6470ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    sp<AudioCommand> command = new AudioCommand();
64865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    command->mCommand = START_TONE;
6490ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    sp<ToneData> data = new ToneData();
65065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    data->mType = type;
65165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    data->mStream = stream;
65248412dc92c4cc92a3f4821616e12290f5eeabd07Jesper Tragardh    command->mParam = data;
6533856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("AudioCommandThread() adding tone start type %d, stream %d", type, stream);
6540ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    sendCommand(command);
65565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
65665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
65765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid AudioPolicyService::AudioCommandThread::stopToneCommand()
65865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
6590ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    sp<AudioCommand> command = new AudioCommand();
66065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    command->mCommand = STOP_TONE;
6613856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("AudioCommandThread() adding tone stop");
6620ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    sendCommand(command);
66365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
66465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
665fff6d715a8db0daf08a50634f242c40268de3d49Glenn Kastenstatus_t AudioPolicyService::AudioCommandThread::volumeCommand(audio_stream_type_t stream,
666de070137f11d346fba77605bd76a44c040a618fcEric Laurent                                                               float volume,
66772ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten                                                               audio_io_handle_t output,
668de070137f11d346fba77605bd76a44c040a618fcEric Laurent                                                               int delayMs)
66965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
6700ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    sp<AudioCommand> command = new AudioCommand();
67165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    command->mCommand = SET_VOLUME;
6720ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    sp<VolumeData> data = new VolumeData();
67365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    data->mStream = stream;
67465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    data->mVolume = volume;
67565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    data->mIO = output;
67665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    command->mParam = data;
6770ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    command->mWaitStatus = true;
6783856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("AudioCommandThread() adding set volume stream %d, volume %f, output %d",
679de070137f11d346fba77605bd76a44c040a618fcEric Laurent            stream, volume, output);
6800ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    return sendCommand(command, delayMs);
68165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
68265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
68372ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kastenstatus_t AudioPolicyService::AudioCommandThread::parametersCommand(audio_io_handle_t ioHandle,
684fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                                                   const char *keyValuePairs,
685de070137f11d346fba77605bd76a44c040a618fcEric Laurent                                                                   int delayMs)
68665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
6870ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    sp<AudioCommand> command = new AudioCommand();
68865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    command->mCommand = SET_PARAMETERS;
6890ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    sp<ParametersData> data = new ParametersData();
69065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    data->mIO = ioHandle;
691fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    data->mKeyValuePairs = String8(keyValuePairs);
69265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    command->mParam = data;
6930ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    command->mWaitStatus = true;
6943856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("AudioCommandThread() adding set parameter string %s, io %d ,delay %d",
695fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin            keyValuePairs, ioHandle, delayMs);
6960ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    return sendCommand(command, delayMs);
69765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
69865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
69965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianstatus_t AudioPolicyService::AudioCommandThread::voiceVolumeCommand(float volume, int delayMs)
70065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
7010ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    sp<AudioCommand> command = new AudioCommand();
70265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    command->mCommand = SET_VOICE_VOLUME;
7030ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    sp<VoiceVolumeData> data = new VoiceVolumeData();
70465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    data->mVolume = volume;
70565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    command->mParam = data;
7060ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    command->mWaitStatus = true;
7073856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("AudioCommandThread() adding set voice volume volume %f", volume);
7080ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    return sendCommand(command, delayMs);
70965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
71065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
711bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioPolicyService::AudioCommandThread::stopOutputCommand(audio_io_handle_t output,
712bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                                                               audio_stream_type_t stream,
713e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent                                                               audio_session_t session)
714bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{
7150ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    sp<AudioCommand> command = new AudioCommand();
716bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    command->mCommand = STOP_OUTPUT;
7170ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    sp<StopOutputData> data = new StopOutputData();
718bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    data->mIO = output;
719bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    data->mStream = stream;
720bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    data->mSession = session;
72148412dc92c4cc92a3f4821616e12290f5eeabd07Jesper Tragardh    command->mParam = data;
722bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    ALOGV("AudioCommandThread() adding stop output %d", output);
7230ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    sendCommand(command);
724bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}
725bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
726e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurentvoid AudioPolicyService::AudioCommandThread::releaseOutputCommand(audio_io_handle_t output,
727e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent                                                                  audio_stream_type_t stream,
728e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent                                                                  audio_session_t session)
729bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{
7300ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    sp<AudioCommand> command = new AudioCommand();
731bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    command->mCommand = RELEASE_OUTPUT;
7320ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    sp<ReleaseOutputData> data = new ReleaseOutputData();
733bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    data->mIO = output;
734e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent    data->mStream = stream;
735e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent    data->mSession = session;
73648412dc92c4cc92a3f4821616e12290f5eeabd07Jesper Tragardh    command->mParam = data;
737bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    ALOGV("AudioCommandThread() adding release output %d", output);
7380ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    sendCommand(command);
7390ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent}
7400ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent
741951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurentstatus_t AudioPolicyService::AudioCommandThread::createAudioPatchCommand(
742951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                                                const struct audio_patch *patch,
743951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                                                audio_patch_handle_t *handle,
744951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                                                int delayMs)
745951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent{
746951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    status_t status = NO_ERROR;
747951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent
748951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    sp<AudioCommand> command = new AudioCommand();
749951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    command->mCommand = CREATE_AUDIO_PATCH;
750951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    CreateAudioPatchData *data = new CreateAudioPatchData();
751951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    data->mPatch = *patch;
752951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    data->mHandle = *handle;
753951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    command->mParam = data;
754951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    command->mWaitStatus = true;
755951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    ALOGV("AudioCommandThread() adding create patch delay %d", delayMs);
756951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    status = sendCommand(command, delayMs);
757951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    if (status == NO_ERROR) {
758951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent        *handle = data->mHandle;
759951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    }
760951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    return status;
761951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent}
762951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent
763951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurentstatus_t AudioPolicyService::AudioCommandThread::releaseAudioPatchCommand(audio_patch_handle_t handle,
764951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent                                                 int delayMs)
765951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent{
766951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    sp<AudioCommand> command = new AudioCommand();
767951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    command->mCommand = RELEASE_AUDIO_PATCH;
768951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    ReleaseAudioPatchData *data = new ReleaseAudioPatchData();
769951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    data->mHandle = handle;
770951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    command->mParam = data;
771951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    command->mWaitStatus = true;
772951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    ALOGV("AudioCommandThread() adding release patch delay %d", delayMs);
773951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent    return sendCommand(command, delayMs);
774951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent}
775951f455566775e5f01e67c5ee26863d7d19209d7Eric Laurent
776b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurentvoid AudioPolicyService::AudioCommandThread::updateAudioPortListCommand()
777b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent{
778b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    sp<AudioCommand> command = new AudioCommand();
779b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    command->mCommand = UPDATE_AUDIOPORT_LIST;
780b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    ALOGV("AudioCommandThread() adding update audio port list");
781b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    sendCommand(command);
782b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent}
783b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent
784b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurentvoid AudioPolicyService::AudioCommandThread::updateAudioPatchListCommand()
785b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent{
786b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    sp<AudioCommand>command = new AudioCommand();
787b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    command->mCommand = UPDATE_AUDIOPATCH_LIST;
788b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    ALOGV("AudioCommandThread() adding update audio patch list");
789b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent    sendCommand(command);
790b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent}
791b52c152d553556b2d227ffc943489de0c60b4b02Eric Laurent
792e1715a465a29db625da9d0ea365edf371e39e201Eric Laurentstatus_t AudioPolicyService::AudioCommandThread::setAudioPortConfigCommand(
793e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent                                            const struct audio_port_config *config, int delayMs)
794e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent{
795e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent    sp<AudioCommand> command = new AudioCommand();
796e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent    command->mCommand = SET_AUDIOPORT_CONFIG;
797e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent    SetAudioPortConfigData *data = new SetAudioPortConfigData();
798e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent    data->mConfig = *config;
799e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent    command->mParam = data;
800e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent    command->mWaitStatus = true;
801e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent    ALOGV("AudioCommandThread() adding set port config delay %d", delayMs);
802e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent    return sendCommand(command, delayMs);
803e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent}
804e1715a465a29db625da9d0ea365edf371e39e201Eric Laurent
805de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivivoid AudioPolicyService::AudioCommandThread::dynamicPolicyMixStateUpdateCommand(
806de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi        String8 regId, int32_t state)
807de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi{
808de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi    sp<AudioCommand> command = new AudioCommand();
809de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi    command->mCommand = DYN_POLICY_MIX_STATE_UPDATE;
810de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi    DynPolicyMixStateUpdateData *data = new DynPolicyMixStateUpdateData();
811de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi    data->mRegId = regId;
812de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi    data->mState = state;
813de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi    command->mParam = data;
814de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi    ALOGV("AudioCommandThread() sending dynamic policy mix (id=%s) state update to %d",
815de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi            regId.string(), state);
816de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi    sendCommand(command);
817de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi}
818de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi
8190ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurentstatus_t AudioPolicyService::AudioCommandThread::sendCommand(sp<AudioCommand>& command, int delayMs)
8200ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent{
8210ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    {
8220ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent        Mutex::Autolock _l(mLock);
8230ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent        insertCommand_l(command, delayMs);
8240ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent        mWaitWorkCV.signal();
8250ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    }
8260ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    Mutex::Autolock _l(command->mLock);
8270ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    while (command->mWaitStatus) {
8280ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent        nsecs_t timeOutNs = kAudioCommandTimeoutNs + milliseconds(delayMs);
8290ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent        if (command->mCond.waitRelative(command->mLock, timeOutNs) != NO_ERROR) {
8300ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent            command->mStatus = TIMED_OUT;
8310ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent            command->mWaitStatus = false;
8320ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent        }
8330ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    }
8340ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    return command->mStatus;
835bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}
836bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
83765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian// insertCommand_l() must be called with mLock held
8380ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurentvoid AudioPolicyService::AudioCommandThread::insertCommand_l(sp<AudioCommand>& command, int delayMs)
83965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
8408d6a2449a91f5116d7243ab039393195ebd663feGlenn Kasten    ssize_t i;  // not size_t because i will count down to -1
8410ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent    Vector < sp<AudioCommand> > removedCommands;
84265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    command->mTime = systemTime() + milliseconds(delayMs);
84365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
84465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // acquire wake lock to make sure delayed commands are processed
845bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    if (mAudioCommands.isEmpty()) {
84665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        acquire_wake_lock(PARTIAL_WAKE_LOCK, mName.string());
84765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
84865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
84965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // check same pending commands with later time stamps and eliminate them
85065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    for (i = mAudioCommands.size()-1; i >= 0; i--) {
8510ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent        sp<AudioCommand> command2 = mAudioCommands[i];
85265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        // commands are sorted by increasing time stamp: no need to scan the rest of mAudioCommands
85365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        if (command2->mTime <= command->mTime) break;
854e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent
855e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent        // create audio patch or release audio patch commands are equivalent
856e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent        // with regard to filtering
857e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent        if ((command->mCommand == CREATE_AUDIO_PATCH) ||
858e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent                (command->mCommand == RELEASE_AUDIO_PATCH)) {
859e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent            if ((command2->mCommand != CREATE_AUDIO_PATCH) &&
860e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent                    (command2->mCommand != RELEASE_AUDIO_PATCH)) {
861e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent                continue;
862e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent            }
863e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent        } else if (command2->mCommand != command->mCommand) continue;
86465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
86565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        switch (command->mCommand) {
86665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        case SET_PARAMETERS: {
8670ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent            ParametersData *data = (ParametersData *)command->mParam.get();
8680ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent            ParametersData *data2 = (ParametersData *)command2->mParam.get();
86965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            if (data->mIO != data2->mIO) break;
8703856b090cd04ba5dd4a59a12430ed724d5995909Steve Block            ALOGV("Comparing parameter command %s to new command %s",
871de070137f11d346fba77605bd76a44c040a618fcEric Laurent                    data2->mKeyValuePairs.string(), data->mKeyValuePairs.string());
87265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            AudioParameter param = AudioParameter(data->mKeyValuePairs);
87365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            AudioParameter param2 = AudioParameter(data2->mKeyValuePairs);
87465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            for (size_t j = 0; j < param.size(); j++) {
875e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten                String8 key;
876e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten                String8 value;
877e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten                param.getAt(j, key, value);
878e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten                for (size_t k = 0; k < param2.size(); k++) {
879e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten                    String8 key2;
880e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten                    String8 value2;
881e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten                    param2.getAt(k, key2, value2);
882e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten                    if (key2 == key) {
883e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten                        param2.remove(key2);
884e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten                        ALOGV("Filtering out parameter %s", key2.string());
885e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten                        break;
886e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten                    }
887e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten                }
88865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            }
88965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            // if all keys have been filtered out, remove the command.
89065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            // otherwise, update the key value pairs
89165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            if (param2.size() == 0) {
89265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                removedCommands.add(command2);
89365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            } else {
89465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                data2->mKeyValuePairs = param2.toString();
89565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            }
89621e5456821e3c107d09b0629adbf23c9e06e7c0bEric Laurent            command->mTime = command2->mTime;
89721e5456821e3c107d09b0629adbf23c9e06e7c0bEric Laurent            // force delayMs to non 0 so that code below does not request to wait for
89821e5456821e3c107d09b0629adbf23c9e06e7c0bEric Laurent            // command status as the command is now delayed
89921e5456821e3c107d09b0629adbf23c9e06e7c0bEric Laurent            delayMs = 1;
90065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        } break;
90165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
90265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        case SET_VOLUME: {
9030ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent            VolumeData *data = (VolumeData *)command->mParam.get();
9040ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent            VolumeData *data2 = (VolumeData *)command2->mParam.get();
90565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            if (data->mIO != data2->mIO) break;
90665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            if (data->mStream != data2->mStream) break;
9073856b090cd04ba5dd4a59a12430ed724d5995909Steve Block            ALOGV("Filtering out volume command on output %d for stream %d",
908de070137f11d346fba77605bd76a44c040a618fcEric Laurent                    data->mIO, data->mStream);
90965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            removedCommands.add(command2);
91021e5456821e3c107d09b0629adbf23c9e06e7c0bEric Laurent            command->mTime = command2->mTime;
91121e5456821e3c107d09b0629adbf23c9e06e7c0bEric Laurent            // force delayMs to non 0 so that code below does not request to wait for
91221e5456821e3c107d09b0629adbf23c9e06e7c0bEric Laurent            // command status as the command is now delayed
91321e5456821e3c107d09b0629adbf23c9e06e7c0bEric Laurent            delayMs = 1;
91465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        } break;
915e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent
916e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent        case CREATE_AUDIO_PATCH:
917e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent        case RELEASE_AUDIO_PATCH: {
918e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent            audio_patch_handle_t handle;
919a2d4a6dac432f0c4f543d79b9a63c55ae91f81d6Haynes Mathew George            struct audio_patch patch;
920e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent            if (command->mCommand == CREATE_AUDIO_PATCH) {
921e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent                handle = ((CreateAudioPatchData *)command->mParam.get())->mHandle;
922a2d4a6dac432f0c4f543d79b9a63c55ae91f81d6Haynes Mathew George                patch = ((CreateAudioPatchData *)command->mParam.get())->mPatch;
923e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent            } else {
924e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent                handle = ((ReleaseAudioPatchData *)command->mParam.get())->mHandle;
925e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent            }
926e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent            audio_patch_handle_t handle2;
927a2d4a6dac432f0c4f543d79b9a63c55ae91f81d6Haynes Mathew George            struct audio_patch patch2;
928e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent            if (command2->mCommand == CREATE_AUDIO_PATCH) {
929e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent                handle2 = ((CreateAudioPatchData *)command2->mParam.get())->mHandle;
930a2d4a6dac432f0c4f543d79b9a63c55ae91f81d6Haynes Mathew George                patch2 = ((CreateAudioPatchData *)command2->mParam.get())->mPatch;
931e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent            } else {
932e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent                handle2 = ((ReleaseAudioPatchData *)command2->mParam.get())->mHandle;
933e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent            }
934e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent            if (handle != handle2) break;
935a2d4a6dac432f0c4f543d79b9a63c55ae91f81d6Haynes Mathew George            /* Filter CREATE_AUDIO_PATCH commands only when they are issued for
936a2d4a6dac432f0c4f543d79b9a63c55ae91f81d6Haynes Mathew George               same output. */
937a2d4a6dac432f0c4f543d79b9a63c55ae91f81d6Haynes Mathew George            if( (command->mCommand == CREATE_AUDIO_PATCH) &&
938a2d4a6dac432f0c4f543d79b9a63c55ae91f81d6Haynes Mathew George                (command2->mCommand == CREATE_AUDIO_PATCH) ) {
939a2d4a6dac432f0c4f543d79b9a63c55ae91f81d6Haynes Mathew George                bool isOutputDiff = false;
940a2d4a6dac432f0c4f543d79b9a63c55ae91f81d6Haynes Mathew George                if (patch.num_sources == patch2.num_sources) {
941a2d4a6dac432f0c4f543d79b9a63c55ae91f81d6Haynes Mathew George                    for (unsigned count = 0; count < patch.num_sources; count++) {
942a2d4a6dac432f0c4f543d79b9a63c55ae91f81d6Haynes Mathew George                        if (patch.sources[count].id != patch2.sources[count].id) {
943a2d4a6dac432f0c4f543d79b9a63c55ae91f81d6Haynes Mathew George                            isOutputDiff = true;
944a2d4a6dac432f0c4f543d79b9a63c55ae91f81d6Haynes Mathew George                            break;
945a2d4a6dac432f0c4f543d79b9a63c55ae91f81d6Haynes Mathew George                        }
946a2d4a6dac432f0c4f543d79b9a63c55ae91f81d6Haynes Mathew George                    }
947a2d4a6dac432f0c4f543d79b9a63c55ae91f81d6Haynes Mathew George                    if (isOutputDiff)
948a2d4a6dac432f0c4f543d79b9a63c55ae91f81d6Haynes Mathew George                       break;
949a2d4a6dac432f0c4f543d79b9a63c55ae91f81d6Haynes Mathew George                }
950a2d4a6dac432f0c4f543d79b9a63c55ae91f81d6Haynes Mathew George            }
951e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent            ALOGV("Filtering out %s audio patch command for handle %d",
952e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent                  (command->mCommand == CREATE_AUDIO_PATCH) ? "create" : "release", handle);
953e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent            removedCommands.add(command2);
954e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent            command->mTime = command2->mTime;
955e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent            // force delayMs to non 0 so that code below does not request to wait for
956e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent            // command status as the command is now delayed
957e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent            delayMs = 1;
958e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent        } break;
959e45b48aba115f4daf4a4aad31b6a95df9b79f463Eric Laurent
960de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi        case DYN_POLICY_MIX_STATE_UPDATE: {
961de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi
962de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi        } break;
963de80105c3f2db0eabd47640c49387ea3b44d4782Jean-Michel Trivi
96465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        case START_TONE:
96565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        case STOP_TONE:
96665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        default:
96765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            break;
96865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
96965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
97065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
97165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // remove filtered commands
97265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    for (size_t j = 0; j < removedCommands.size(); j++) {
97365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        // removed commands always have time stamps greater than current command
97465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        for (size_t k = i + 1; k < mAudioCommands.size(); k++) {
9750ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent            if (mAudioCommands[k].get() == removedCommands[j].get()) {
9763856b090cd04ba5dd4a59a12430ed724d5995909Steve Block                ALOGV("suppressing command: %d", mAudioCommands[k]->mCommand);
97765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                mAudioCommands.removeAt(k);
97865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian                break;
97965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            }
98065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        }
98165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
98265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    removedCommands.clear();
98365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
984aa79befb301bc3451a19021b3fc9e9e6a1f231e8Eric Laurent    // Disable wait for status if delay is not 0.
985aa79befb301bc3451a19021b3fc9e9e6a1f231e8Eric Laurent    // Except for create audio patch command because the returned patch handle
986aa79befb301bc3451a19021b3fc9e9e6a1f231e8Eric Laurent    // is needed by audio policy manager
987aa79befb301bc3451a19021b3fc9e9e6a1f231e8Eric Laurent    if (delayMs != 0 && command->mCommand != CREATE_AUDIO_PATCH) {
988cec4abb2a39eafa75cf6894170a200322aa7226eEric Laurent        command->mWaitStatus = false;
989cec4abb2a39eafa75cf6894170a200322aa7226eEric Laurent    }
990cec4abb2a39eafa75cf6894170a200322aa7226eEric Laurent
99165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    // insert command at the right place according to its time stamp
9921e693b55d888b9d3e0a2ce770ae2b72b59c1a317Eric Laurent    ALOGV("inserting command: %d at index %zd, num commands %zu",
9931e693b55d888b9d3e0a2ce770ae2b72b59c1a317Eric Laurent            command->mCommand, i+1, mAudioCommands.size());
99465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    mAudioCommands.insertAt(command, i + 1);
99565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
99665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
99765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid AudioPolicyService::AudioCommandThread::exit()
99865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
9993856b090cd04ba5dd4a59a12430ed724d5995909Steve Block    ALOGV("AudioCommandThread::exit");
100065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    {
100165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        AutoMutex _l(mLock);
100265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        requestExit();
100365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian        mWaitWorkCV.signal();
100465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    }
100565ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    requestExitAndWait();
100665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
100765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
100865ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopianvoid AudioPolicyService::AudioCommandThread::AudioCommand::dump(char* buffer, size_t size)
100965ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian{
101065ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian    snprintf(buffer, size, "   %02d      %06d.%03d  %01u    %p\n",
101165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            mCommand,
101265ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            (int)ns2s(mTime),
101365ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            (int)ns2ms(mTime)%1000,
101465ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian            mWaitStatus,
10150ede8924b98c2967be2795e8d4f9837d8d3f094cEric Laurent            mParam.get());
101665ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}
101765ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian
1018fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin/******* helpers for the service_ops callbacks defined below *********/
1019fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavinvoid AudioPolicyService::setParameters(audio_io_handle_t ioHandle,
1020fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                       const char *keyValuePairs,
1021fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                       int delayMs)
1022fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin{
102372ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten    mAudioCommandThread->parametersCommand(ioHandle, keyValuePairs,
1024fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                           delayMs);
1025fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin}
1026fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
1027fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavinint AudioPolicyService::setStreamVolume(audio_stream_type_t stream,
1028fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                        float volume,
1029fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                        audio_io_handle_t output,
1030fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                        int delayMs)
1031fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin{
1032fff6d715a8db0daf08a50634f242c40268de3d49Glenn Kasten    return (int)mAudioCommandThread->volumeCommand(stream, volume,
103372ef00de10fa95bfcb948ed88ab9b7a177ed0b48Glenn Kasten                                                   output, delayMs);
1034fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin}
1035fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
1036fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavinint AudioPolicyService::startTone(audio_policy_tone_t tone,
1037fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                  audio_stream_type_t stream)
1038fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin{
10396e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    if (tone != AUDIO_POLICY_TONE_IN_CALL_NOTIFICATION) {
104029357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block        ALOGE("startTone: illegal tone requested (%d)", tone);
10416e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    }
10426e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    if (stream != AUDIO_STREAM_VOICE_CALL) {
104329357bc2c0dd7c43ad3bd0c8e3efa4e6fd9bfd47Steve Block        ALOGE("startTone: illegal stream (%d) requested for tone %d", stream,
1044e53b9ead781c36e96d6b6f012ddffc93a3d80f0dGlenn Kasten            tone);
10456e2ebe97f2ad0a21907f20f9ee644c4eacbb7a40Glenn Kasten    }
1046fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    mTonePlaybackThread->startToneCommand(ToneGenerator::TONE_SUP_CALL_WAITING,
1047fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                          AUDIO_STREAM_VOICE_CALL);
1048fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    return 0;
1049fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin}
1050fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
1051fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavinint AudioPolicyService::stopTone()
1052fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin{
1053fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    mTonePlaybackThread->stopToneCommand();
1054fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    return 0;
1055fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin}
1056fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
1057fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavinint AudioPolicyService::setVoiceVolume(float volume, int delayMs)
1058fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin{
1059fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    return (int)mAudioCommandThread->voiceVolumeCommand(volume, delayMs);
1060fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin}
1061fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
1062fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavinextern "C" {
10632d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurentaudio_module_handle_t aps_load_hw_module(void *service __unused,
10642d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurent                                             const char *name);
10652d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurentaudio_io_handle_t aps_open_output(void *service __unused,
1066a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                                         audio_devices_t *pDevices,
1067a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                                         uint32_t *pSamplingRate,
1068a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                                         audio_format_t *pFormat,
1069a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                                         audio_channel_mask_t *pChannelMask,
1070a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                                         uint32_t *pLatencyMs,
10712d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurent                                         audio_output_flags_t flags);
1072fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
10732d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurentaudio_io_handle_t aps_open_output_on_module(void *service __unused,
1074a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                                                   audio_module_handle_t module,
1075a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                                                   audio_devices_t *pDevices,
1076a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                                                   uint32_t *pSamplingRate,
1077a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                                                   audio_format_t *pFormat,
1078a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                                                   audio_channel_mask_t *pChannelMask,
1079a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                                                   uint32_t *pLatencyMs,
1080ad3af3305f024bcbbd55c894a4995e449498e1baRichard Fitzgerald                                                   audio_output_flags_t flags,
10812d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurent                                                   const audio_offload_info_t *offloadInfo);
10822d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurentaudio_io_handle_t aps_open_dup_output(void *service __unused,
1083fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                                 audio_io_handle_t output1,
10842d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurent                                                 audio_io_handle_t output2);
10852d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurentint aps_close_output(void *service __unused, audio_io_handle_t output);
10862d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurentint aps_suspend_output(void *service __unused, audio_io_handle_t output);
10872d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurentint aps_restore_output(void *service __unused, audio_io_handle_t output);
10882d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurentaudio_io_handle_t aps_open_input(void *service __unused,
1089a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                                        audio_devices_t *pDevices,
1090a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                                        uint32_t *pSamplingRate,
1091a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                                        audio_format_t *pFormat,
1092a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                                        audio_channel_mask_t *pChannelMask,
10932d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurent                                        audio_in_acoustics_t acoustics __unused);
10942d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurentaudio_io_handle_t aps_open_input_on_module(void *service __unused,
1095a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                                                  audio_module_handle_t module,
1096a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                                                  audio_devices_t *pDevices,
1097a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                                                  uint32_t *pSamplingRate,
1098a4c5a550e2a3bc237179b8684e51718e05894492Eric Laurent                                                  audio_format_t *pFormat,
10992d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurent                                                  audio_channel_mask_t *pChannelMask);
11002d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurentint aps_close_input(void *service __unused, audio_io_handle_t input);
11012d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurentint aps_invalidate_stream(void *service __unused, audio_stream_type_t stream);
11022d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurentint aps_move_effects(void *service __unused, int session,
1103fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                audio_io_handle_t src_output,
11042d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurent                                audio_io_handle_t dst_output);
11052d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurentchar * aps_get_parameters(void *service __unused, audio_io_handle_t io_handle,
11062d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurent                                     const char *keys);
11072d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurentvoid aps_set_parameters(void *service, audio_io_handle_t io_handle,
11082d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurent                                   const char *kv_pairs, int delay_ms);
11092d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurentint aps_set_stream_volume(void *service, audio_stream_type_t stream,
1110fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin                                     float volume, audio_io_handle_t output,
11112d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurent                                     int delay_ms);
11122d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurentint aps_start_tone(void *service, audio_policy_tone_t tone,
11132d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurent                              audio_stream_type_t stream);
11142d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurentint aps_stop_tone(void *service);
11152d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurentint aps_set_voice_volume(void *service, float volume, int delay_ms);
11162d388eccc9dc085337c7a03a68467cbee6b809e1Eric Laurent};
1117fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
1118fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavinnamespace {
1119fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    struct audio_policy_service_ops aps_ops = {
112001d3acba9de861cb2b718338e787cff3566fc5ecGlenn Kasten        .open_output           = aps_open_output,
112101d3acba9de861cb2b718338e787cff3566fc5ecGlenn Kasten        .open_duplicate_output = aps_open_dup_output,
112201d3acba9de861cb2b718338e787cff3566fc5ecGlenn Kasten        .close_output          = aps_close_output,
112301d3acba9de861cb2b718338e787cff3566fc5ecGlenn Kasten        .suspend_output        = aps_suspend_output,
112401d3acba9de861cb2b718338e787cff3566fc5ecGlenn Kasten        .restore_output        = aps_restore_output,
112501d3acba9de861cb2b718338e787cff3566fc5ecGlenn Kasten        .open_input            = aps_open_input,
112601d3acba9de861cb2b718338e787cff3566fc5ecGlenn Kasten        .close_input           = aps_close_input,
112701d3acba9de861cb2b718338e787cff3566fc5ecGlenn Kasten        .set_stream_volume     = aps_set_stream_volume,
1128d2304db2fcb5112292105a0949a55986a4c9875fGlenn Kasten        .invalidate_stream     = aps_invalidate_stream,
112901d3acba9de861cb2b718338e787cff3566fc5ecGlenn Kasten        .set_parameters        = aps_set_parameters,
113001d3acba9de861cb2b718338e787cff3566fc5ecGlenn Kasten        .get_parameters        = aps_get_parameters,
113101d3acba9de861cb2b718338e787cff3566fc5ecGlenn Kasten        .start_tone            = aps_start_tone,
113201d3acba9de861cb2b718338e787cff3566fc5ecGlenn Kasten        .stop_tone             = aps_stop_tone,
113301d3acba9de861cb2b718338e787cff3566fc5ecGlenn Kasten        .set_voice_volume      = aps_set_voice_volume,
113401d3acba9de861cb2b718338e787cff3566fc5ecGlenn Kasten        .move_effects          = aps_move_effects,
113501d3acba9de861cb2b718338e787cff3566fc5ecGlenn Kasten        .load_hw_module        = aps_load_hw_module,
113601d3acba9de861cb2b718338e787cff3566fc5ecGlenn Kasten        .open_output_on_module = aps_open_output_on_module,
113701d3acba9de861cb2b718338e787cff3566fc5ecGlenn Kasten        .open_input_on_module  = aps_open_input_on_module,
1138fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin    };
1139fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin}; // namespace <unnamed>
1140fce7a473248381cc83a01855f92581077d3c9ee2Dima Zavin
114165ab47156e1c7dfcd8cc4266253a5ff30219e7f0Mathias Agopian}; // namespace android
1142