AudioFlinger.cpp revision 5ce96d97feafc6989f6141bb2633eae3d87ddf28
1/*
2**
3** Copyright 2007, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9**     http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18
19#define LOG_TAG "AudioFlinger"
20//#define LOG_NDEBUG 0
21
22#include <math.h>
23#include <signal.h>
24#include <sys/time.h>
25#include <sys/resource.h>
26
27#include <binder/IPCThreadState.h>
28#include <binder/IServiceManager.h>
29#include <utils/Log.h>
30#include <binder/Parcel.h>
31#include <binder/IPCThreadState.h>
32#include <utils/String16.h>
33#include <utils/threads.h>
34#include <utils/Atomic.h>
35
36#include <cutils/bitops.h>
37#include <cutils/properties.h>
38#include <cutils/compiler.h>
39
40#include <media/IMediaPlayerService.h>
41#include <media/IMediaDeathNotifier.h>
42
43#include <private/media/AudioTrackShared.h>
44#include <private/media/AudioEffectShared.h>
45
46#include <system/audio.h>
47#include <hardware/audio.h>
48
49#include "AudioMixer.h"
50#include "AudioFlinger.h"
51#include "ServiceUtilities.h"
52
53#include <media/EffectsFactoryApi.h>
54#include <audio_effects/effect_visualizer.h>
55#include <audio_effects/effect_ns.h>
56#include <audio_effects/effect_aec.h>
57
58#include <audio_utils/primitives.h>
59
60#include <cpustats/ThreadCpuUsage.h>
61#include <powermanager/PowerManager.h>
62// #define DEBUG_CPU_USAGE 10  // log statistics every n wall clock seconds
63
64#include <common_time/cc_helper.h>
65#include <common_time/local_clock.h>
66
67// ----------------------------------------------------------------------------
68
69
70namespace android {
71
72static const char kDeadlockedString[] = "AudioFlinger may be deadlocked\n";
73static const char kHardwareLockedString[] = "Hardware lock is taken\n";
74
75static const float MAX_GAIN = 4096.0f;
76static const uint32_t MAX_GAIN_INT = 0x1000;
77
78// retry counts for buffer fill timeout
79// 50 * ~20msecs = 1 second
80static const int8_t kMaxTrackRetries = 50;
81static const int8_t kMaxTrackStartupRetries = 50;
82// allow less retry attempts on direct output thread.
83// direct outputs can be a scarce resource in audio hardware and should
84// be released as quickly as possible.
85static const int8_t kMaxTrackRetriesDirect = 2;
86
87static const int kDumpLockRetries = 50;
88static const int kDumpLockSleepUs = 20000;
89
90// don't warn about blocked writes or record buffer overflows more often than this
91static const nsecs_t kWarningThrottleNs = seconds(5);
92
93// RecordThread loop sleep time upon application overrun or audio HAL read error
94static const int kRecordThreadSleepUs = 5000;
95
96// maximum time to wait for setParameters to complete
97static const nsecs_t kSetParametersTimeoutNs = seconds(2);
98
99// minimum sleep time for the mixer thread loop when tracks are active but in underrun
100static const uint32_t kMinThreadSleepTimeUs = 5000;
101// maximum divider applied to the active sleep time in the mixer thread loop
102static const uint32_t kMaxThreadSleepTimeShift = 2;
103
104nsecs_t AudioFlinger::mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
105
106// ----------------------------------------------------------------------------
107
108// To collect the amplifier usage
109static void addBatteryData(uint32_t params) {
110    sp<IMediaPlayerService> service = IMediaDeathNotifier::getMediaPlayerService();
111    if (service == NULL) {
112        // it already logged
113        return;
114    }
115
116    service->addBatteryData(params);
117}
118
119static int load_audio_interface(const char *if_name, const hw_module_t **mod,
120                                audio_hw_device_t **dev)
121{
122    int rc;
123
124    rc = hw_get_module_by_class(AUDIO_HARDWARE_MODULE_ID, if_name, mod);
125    if (rc)
126        goto out;
127
128    rc = audio_hw_device_open(*mod, dev);
129    ALOGE_IF(rc, "couldn't open audio hw device in %s.%s (%s)",
130            AUDIO_HARDWARE_MODULE_ID, if_name, strerror(-rc));
131    if (rc)
132        goto out;
133
134    return 0;
135
136out:
137    *mod = NULL;
138    *dev = NULL;
139    return rc;
140}
141
142static const char * const audio_interfaces[] = {
143    "primary",
144    "a2dp",
145    "usb",
146};
147#define ARRAY_SIZE(x) (sizeof((x))/sizeof(((x)[0])))
148
149// ----------------------------------------------------------------------------
150
151AudioFlinger::AudioFlinger()
152    : BnAudioFlinger(),
153      mPrimaryHardwareDev(NULL),
154      mHardwareStatus(AUDIO_HW_IDLE), // see also onFirstRef()
155      mMasterVolume(1.0f),
156      mMasterVolumeSupportLvl(MVS_NONE),
157      mMasterMute(false),
158      mNextUniqueId(1),
159      mMode(AUDIO_MODE_INVALID),
160      mBtNrecIsOff(false)
161{
162}
163
164void AudioFlinger::onFirstRef()
165{
166    int rc = 0;
167
168    Mutex::Autolock _l(mLock);
169
170    /* TODO: move all this work into an Init() function */
171    char val_str[PROPERTY_VALUE_MAX] = { 0 };
172    if (property_get("ro.audio.flinger_standbytime_ms", val_str, NULL) >= 0) {
173        uint32_t int_val;
174        if (1 == sscanf(val_str, "%u", &int_val)) {
175            mStandbyTimeInNsecs = milliseconds(int_val);
176            ALOGI("Using %u mSec as standby time.", int_val);
177        } else {
178            mStandbyTimeInNsecs = kDefaultStandbyTimeInNsecs;
179            ALOGI("Using default %u mSec as standby time.",
180                    (uint32_t)(mStandbyTimeInNsecs / 1000000));
181        }
182    }
183
184    for (size_t i = 0; i < ARRAY_SIZE(audio_interfaces); i++) {
185        const hw_module_t *mod;
186        audio_hw_device_t *dev;
187
188        rc = load_audio_interface(audio_interfaces[i], &mod, &dev);
189        if (rc)
190            continue;
191
192        ALOGI("Loaded %s audio interface from %s (%s)", audio_interfaces[i],
193             mod->name, mod->id);
194        mAudioHwDevs.push(dev);
195
196        if (mPrimaryHardwareDev == NULL) {
197            mPrimaryHardwareDev = dev;
198            ALOGI("Using '%s' (%s.%s) as the primary audio interface",
199                 mod->name, mod->id, audio_interfaces[i]);
200        }
201    }
202
203    if (mPrimaryHardwareDev == NULL) {
204        ALOGE("Primary audio interface not found");
205        // proceed, all later accesses to mPrimaryHardwareDev verify it's safe with initCheck()
206    }
207
208    // Currently (mPrimaryHardwareDev == NULL) == (mAudioHwDevs.size() == 0), but the way the
209    // primary HW dev is selected can change so these conditions might not always be equivalent.
210    // When that happens, re-visit all the code that assumes this.
211
212    AutoMutex lock(mHardwareLock);
213
214    // Determine the level of master volume support the primary audio HAL has,
215    // and set the initial master volume at the same time.
216    float initialVolume = 1.0;
217    mMasterVolumeSupportLvl = MVS_NONE;
218    if (0 == mPrimaryHardwareDev->init_check(mPrimaryHardwareDev)) {
219        audio_hw_device_t *dev = mPrimaryHardwareDev;
220
221        mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
222        if ((NULL != dev->get_master_volume) &&
223            (NO_ERROR == dev->get_master_volume(dev, &initialVolume))) {
224            mMasterVolumeSupportLvl = MVS_FULL;
225        } else {
226            mMasterVolumeSupportLvl = MVS_SETONLY;
227            initialVolume = 1.0;
228        }
229
230        mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
231        if ((NULL == dev->set_master_volume) ||
232            (NO_ERROR != dev->set_master_volume(dev, initialVolume))) {
233            mMasterVolumeSupportLvl = MVS_NONE;
234        }
235        mHardwareStatus = AUDIO_HW_INIT;
236    }
237
238    // Set the mode for each audio HAL, and try to set the initial volume (if
239    // supported) for all of the non-primary audio HALs.
240    for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
241        audio_hw_device_t *dev = mAudioHwDevs[i];
242
243        mHardwareStatus = AUDIO_HW_INIT;
244        rc = dev->init_check(dev);
245        mHardwareStatus = AUDIO_HW_IDLE;
246        if (rc == 0) {
247            mMode = AUDIO_MODE_NORMAL;  // assigned multiple times with same value
248            mHardwareStatus = AUDIO_HW_SET_MODE;
249            dev->set_mode(dev, mMode);
250
251            if ((dev != mPrimaryHardwareDev) &&
252                (NULL != dev->set_master_volume)) {
253                mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
254                dev->set_master_volume(dev, initialVolume);
255            }
256
257            mHardwareStatus = AUDIO_HW_INIT;
258        }
259    }
260
261    mMasterVolumeSW = (MVS_NONE == mMasterVolumeSupportLvl)
262                    ? initialVolume
263                    : 1.0;
264    mMasterVolume   = initialVolume;
265    mHardwareStatus = AUDIO_HW_IDLE;
266}
267
268AudioFlinger::~AudioFlinger()
269{
270
271    while (!mRecordThreads.isEmpty()) {
272        // closeInput() will remove first entry from mRecordThreads
273        closeInput(mRecordThreads.keyAt(0));
274    }
275    while (!mPlaybackThreads.isEmpty()) {
276        // closeOutput() will remove first entry from mPlaybackThreads
277        closeOutput(mPlaybackThreads.keyAt(0));
278    }
279
280    for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
281        // no mHardwareLock needed, as there are no other references to this
282        audio_hw_device_close(mAudioHwDevs[i]);
283    }
284}
285
286audio_hw_device_t* AudioFlinger::findSuitableHwDev_l(uint32_t devices)
287{
288    /* first matching HW device is returned */
289    for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
290        audio_hw_device_t *dev = mAudioHwDevs[i];
291        if ((dev->get_supported_devices(dev) & devices) == devices)
292            return dev;
293    }
294    return NULL;
295}
296
297status_t AudioFlinger::dumpClients(int fd, const Vector<String16>& args)
298{
299    const size_t SIZE = 256;
300    char buffer[SIZE];
301    String8 result;
302
303    result.append("Clients:\n");
304    for (size_t i = 0; i < mClients.size(); ++i) {
305        sp<Client> client = mClients.valueAt(i).promote();
306        if (client != 0) {
307            snprintf(buffer, SIZE, "  pid: %d\n", client->pid());
308            result.append(buffer);
309        }
310    }
311
312    result.append("Global session refs:\n");
313    result.append(" session pid cnt\n");
314    for (size_t i = 0; i < mAudioSessionRefs.size(); i++) {
315        AudioSessionRef *r = mAudioSessionRefs[i];
316        snprintf(buffer, SIZE, " %7d %3d %3d\n", r->sessionid, r->pid, r->cnt);
317        result.append(buffer);
318    }
319    write(fd, result.string(), result.size());
320    return NO_ERROR;
321}
322
323
324status_t AudioFlinger::dumpInternals(int fd, const Vector<String16>& args)
325{
326    const size_t SIZE = 256;
327    char buffer[SIZE];
328    String8 result;
329    hardware_call_state hardwareStatus = mHardwareStatus;
330
331    snprintf(buffer, SIZE, "Hardware status: %d\n"
332                           "Standby Time mSec: %u\n",
333                            hardwareStatus,
334                            (uint32_t)(mStandbyTimeInNsecs / 1000000));
335    result.append(buffer);
336    write(fd, result.string(), result.size());
337    return NO_ERROR;
338}
339
340status_t AudioFlinger::dumpPermissionDenial(int fd, const Vector<String16>& args)
341{
342    const size_t SIZE = 256;
343    char buffer[SIZE];
344    String8 result;
345    snprintf(buffer, SIZE, "Permission Denial: "
346            "can't dump AudioFlinger from pid=%d, uid=%d\n",
347            IPCThreadState::self()->getCallingPid(),
348            IPCThreadState::self()->getCallingUid());
349    result.append(buffer);
350    write(fd, result.string(), result.size());
351    return NO_ERROR;
352}
353
354static bool tryLock(Mutex& mutex)
355{
356    bool locked = false;
357    for (int i = 0; i < kDumpLockRetries; ++i) {
358        if (mutex.tryLock() == NO_ERROR) {
359            locked = true;
360            break;
361        }
362        usleep(kDumpLockSleepUs);
363    }
364    return locked;
365}
366
367status_t AudioFlinger::dump(int fd, const Vector<String16>& args)
368{
369    if (!dumpAllowed()) {
370        dumpPermissionDenial(fd, args);
371    } else {
372        // get state of hardware lock
373        bool hardwareLocked = tryLock(mHardwareLock);
374        if (!hardwareLocked) {
375            String8 result(kHardwareLockedString);
376            write(fd, result.string(), result.size());
377        } else {
378            mHardwareLock.unlock();
379        }
380
381        bool locked = tryLock(mLock);
382
383        // failed to lock - AudioFlinger is probably deadlocked
384        if (!locked) {
385            String8 result(kDeadlockedString);
386            write(fd, result.string(), result.size());
387        }
388
389        dumpClients(fd, args);
390        dumpInternals(fd, args);
391
392        // dump playback threads
393        for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
394            mPlaybackThreads.valueAt(i)->dump(fd, args);
395        }
396
397        // dump record threads
398        for (size_t i = 0; i < mRecordThreads.size(); i++) {
399            mRecordThreads.valueAt(i)->dump(fd, args);
400        }
401
402        // dump all hardware devs
403        for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
404            audio_hw_device_t *dev = mAudioHwDevs[i];
405            dev->dump(dev, fd);
406        }
407        if (locked) mLock.unlock();
408    }
409    return NO_ERROR;
410}
411
412sp<AudioFlinger::Client> AudioFlinger::registerPid_l(pid_t pid)
413{
414    // If pid is already in the mClients wp<> map, then use that entry
415    // (for which promote() is always != 0), otherwise create a new entry and Client.
416    sp<Client> client = mClients.valueFor(pid).promote();
417    if (client == 0) {
418        client = new Client(this, pid);
419        mClients.add(pid, client);
420    }
421
422    return client;
423}
424
425// IAudioFlinger interface
426
427
428sp<IAudioTrack> AudioFlinger::createTrack(
429        pid_t pid,
430        audio_stream_type_t streamType,
431        uint32_t sampleRate,
432        audio_format_t format,
433        uint32_t channelMask,
434        int frameCount,
435        // FIXME dead, remove from IAudioFlinger
436        uint32_t flags,
437        const sp<IMemory>& sharedBuffer,
438        audio_io_handle_t output,
439        bool isTimed,
440        int *sessionId,
441        status_t *status)
442{
443    sp<PlaybackThread::Track> track;
444    sp<TrackHandle> trackHandle;
445    sp<Client> client;
446    status_t lStatus;
447    int lSessionId;
448
449    // client AudioTrack::set already implements AUDIO_STREAM_DEFAULT => AUDIO_STREAM_MUSIC,
450    // but if someone uses binder directly they could bypass that and cause us to crash
451    if (uint32_t(streamType) >= AUDIO_STREAM_CNT) {
452        ALOGE("createTrack() invalid stream type %d", streamType);
453        lStatus = BAD_VALUE;
454        goto Exit;
455    }
456
457    {
458        Mutex::Autolock _l(mLock);
459        PlaybackThread *thread = checkPlaybackThread_l(output);
460        PlaybackThread *effectThread = NULL;
461        if (thread == NULL) {
462            ALOGE("unknown output thread");
463            lStatus = BAD_VALUE;
464            goto Exit;
465        }
466
467        client = registerPid_l(pid);
468
469        ALOGV("createTrack() sessionId: %d", (sessionId == NULL) ? -2 : *sessionId);
470        if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
471            for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
472                sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
473                if (mPlaybackThreads.keyAt(i) != output) {
474                    // prevent same audio session on different output threads
475                    uint32_t sessions = t->hasAudioSession(*sessionId);
476                    if (sessions & PlaybackThread::TRACK_SESSION) {
477                        ALOGE("createTrack() session ID %d already in use", *sessionId);
478                        lStatus = BAD_VALUE;
479                        goto Exit;
480                    }
481                    // check if an effect with same session ID is waiting for a track to be created
482                    if (sessions & PlaybackThread::EFFECT_SESSION) {
483                        effectThread = t.get();
484                    }
485                }
486            }
487            lSessionId = *sessionId;
488        } else {
489            // if no audio session id is provided, create one here
490            lSessionId = nextUniqueId();
491            if (sessionId != NULL) {
492                *sessionId = lSessionId;
493            }
494        }
495        ALOGV("createTrack() lSessionId: %d", lSessionId);
496
497        track = thread->createTrack_l(client, streamType, sampleRate, format,
498                channelMask, frameCount, sharedBuffer, lSessionId, isTimed, &lStatus);
499
500        // move effect chain to this output thread if an effect on same session was waiting
501        // for a track to be created
502        if (lStatus == NO_ERROR && effectThread != NULL) {
503            Mutex::Autolock _dl(thread->mLock);
504            Mutex::Autolock _sl(effectThread->mLock);
505            moveEffectChain_l(lSessionId, effectThread, thread, true);
506        }
507    }
508    if (lStatus == NO_ERROR) {
509        trackHandle = new TrackHandle(track);
510    } else {
511        // remove local strong reference to Client before deleting the Track so that the Client
512        // destructor is called by the TrackBase destructor with mLock held
513        client.clear();
514        track.clear();
515    }
516
517Exit:
518    if(status) {
519        *status = lStatus;
520    }
521    return trackHandle;
522}
523
524uint32_t AudioFlinger::sampleRate(audio_io_handle_t output) const
525{
526    Mutex::Autolock _l(mLock);
527    PlaybackThread *thread = checkPlaybackThread_l(output);
528    if (thread == NULL) {
529        ALOGW("sampleRate() unknown thread %d", output);
530        return 0;
531    }
532    return thread->sampleRate();
533}
534
535int AudioFlinger::channelCount(audio_io_handle_t output) const
536{
537    Mutex::Autolock _l(mLock);
538    PlaybackThread *thread = checkPlaybackThread_l(output);
539    if (thread == NULL) {
540        ALOGW("channelCount() unknown thread %d", output);
541        return 0;
542    }
543    return thread->channelCount();
544}
545
546audio_format_t AudioFlinger::format(audio_io_handle_t output) const
547{
548    Mutex::Autolock _l(mLock);
549    PlaybackThread *thread = checkPlaybackThread_l(output);
550    if (thread == NULL) {
551        ALOGW("format() unknown thread %d", output);
552        return AUDIO_FORMAT_INVALID;
553    }
554    return thread->format();
555}
556
557size_t AudioFlinger::frameCount(audio_io_handle_t output) const
558{
559    Mutex::Autolock _l(mLock);
560    PlaybackThread *thread = checkPlaybackThread_l(output);
561    if (thread == NULL) {
562        ALOGW("frameCount() unknown thread %d", output);
563        return 0;
564    }
565    return thread->frameCount();
566}
567
568uint32_t AudioFlinger::latency(audio_io_handle_t output) const
569{
570    Mutex::Autolock _l(mLock);
571    PlaybackThread *thread = checkPlaybackThread_l(output);
572    if (thread == NULL) {
573        ALOGW("latency() unknown thread %d", output);
574        return 0;
575    }
576    return thread->latency();
577}
578
579status_t AudioFlinger::setMasterVolume(float value)
580{
581    status_t ret = initCheck();
582    if (ret != NO_ERROR) {
583        return ret;
584    }
585
586    // check calling permissions
587    if (!settingsAllowed()) {
588        return PERMISSION_DENIED;
589    }
590
591    float swmv = value;
592
593    // when hw supports master volume, don't scale in sw mixer
594    if (MVS_NONE != mMasterVolumeSupportLvl) {
595        for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
596            AutoMutex lock(mHardwareLock);
597            audio_hw_device_t *dev = mAudioHwDevs[i];
598
599            mHardwareStatus = AUDIO_HW_SET_MASTER_VOLUME;
600            if (NULL != dev->set_master_volume) {
601                dev->set_master_volume(dev, value);
602            }
603            mHardwareStatus = AUDIO_HW_IDLE;
604        }
605
606        swmv = 1.0;
607    }
608
609    Mutex::Autolock _l(mLock);
610    mMasterVolume   = value;
611    mMasterVolumeSW = swmv;
612    for (size_t i = 0; i < mPlaybackThreads.size(); i++)
613       mPlaybackThreads.valueAt(i)->setMasterVolume(swmv);
614
615    return NO_ERROR;
616}
617
618status_t AudioFlinger::setMode(audio_mode_t mode)
619{
620    status_t ret = initCheck();
621    if (ret != NO_ERROR) {
622        return ret;
623    }
624
625    // check calling permissions
626    if (!settingsAllowed()) {
627        return PERMISSION_DENIED;
628    }
629    if (uint32_t(mode) >= AUDIO_MODE_CNT) {
630        ALOGW("Illegal value: setMode(%d)", mode);
631        return BAD_VALUE;
632    }
633
634    { // scope for the lock
635        AutoMutex lock(mHardwareLock);
636        mHardwareStatus = AUDIO_HW_SET_MODE;
637        ret = mPrimaryHardwareDev->set_mode(mPrimaryHardwareDev, mode);
638        mHardwareStatus = AUDIO_HW_IDLE;
639    }
640
641    if (NO_ERROR == ret) {
642        Mutex::Autolock _l(mLock);
643        mMode = mode;
644        for (size_t i = 0; i < mPlaybackThreads.size(); i++)
645           mPlaybackThreads.valueAt(i)->setMode(mode);
646    }
647
648    return ret;
649}
650
651status_t AudioFlinger::setMicMute(bool state)
652{
653    status_t ret = initCheck();
654    if (ret != NO_ERROR) {
655        return ret;
656    }
657
658    // check calling permissions
659    if (!settingsAllowed()) {
660        return PERMISSION_DENIED;
661    }
662
663    AutoMutex lock(mHardwareLock);
664    mHardwareStatus = AUDIO_HW_SET_MIC_MUTE;
665    ret = mPrimaryHardwareDev->set_mic_mute(mPrimaryHardwareDev, state);
666    mHardwareStatus = AUDIO_HW_IDLE;
667    return ret;
668}
669
670bool AudioFlinger::getMicMute() const
671{
672    status_t ret = initCheck();
673    if (ret != NO_ERROR) {
674        return false;
675    }
676
677    bool state = AUDIO_MODE_INVALID;
678    AutoMutex lock(mHardwareLock);
679    mHardwareStatus = AUDIO_HW_GET_MIC_MUTE;
680    mPrimaryHardwareDev->get_mic_mute(mPrimaryHardwareDev, &state);
681    mHardwareStatus = AUDIO_HW_IDLE;
682    return state;
683}
684
685status_t AudioFlinger::setMasterMute(bool muted)
686{
687    // check calling permissions
688    if (!settingsAllowed()) {
689        return PERMISSION_DENIED;
690    }
691
692    Mutex::Autolock _l(mLock);
693    // This is an optimization, so PlaybackThread doesn't have to look at the one from AudioFlinger
694    mMasterMute = muted;
695    for (size_t i = 0; i < mPlaybackThreads.size(); i++)
696       mPlaybackThreads.valueAt(i)->setMasterMute(muted);
697
698    return NO_ERROR;
699}
700
701float AudioFlinger::masterVolume() const
702{
703    Mutex::Autolock _l(mLock);
704    return masterVolume_l();
705}
706
707float AudioFlinger::masterVolumeSW() const
708{
709    Mutex::Autolock _l(mLock);
710    return masterVolumeSW_l();
711}
712
713bool AudioFlinger::masterMute() const
714{
715    Mutex::Autolock _l(mLock);
716    return masterMute_l();
717}
718
719float AudioFlinger::masterVolume_l() const
720{
721    if (MVS_FULL == mMasterVolumeSupportLvl) {
722        float ret_val;
723        AutoMutex lock(mHardwareLock);
724
725        mHardwareStatus = AUDIO_HW_GET_MASTER_VOLUME;
726        assert(NULL != mPrimaryHardwareDev);
727        assert(NULL != mPrimaryHardwareDev->get_master_volume);
728
729        mPrimaryHardwareDev->get_master_volume(mPrimaryHardwareDev, &ret_val);
730        mHardwareStatus = AUDIO_HW_IDLE;
731        return ret_val;
732    }
733
734    return mMasterVolume;
735}
736
737status_t AudioFlinger::setStreamVolume(audio_stream_type_t stream, float value,
738        audio_io_handle_t output)
739{
740    // check calling permissions
741    if (!settingsAllowed()) {
742        return PERMISSION_DENIED;
743    }
744
745    if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
746        ALOGE("setStreamVolume() invalid stream %d", stream);
747        return BAD_VALUE;
748    }
749
750    AutoMutex lock(mLock);
751    PlaybackThread *thread = NULL;
752    if (output) {
753        thread = checkPlaybackThread_l(output);
754        if (thread == NULL) {
755            return BAD_VALUE;
756        }
757    }
758
759    mStreamTypes[stream].volume = value;
760
761    if (thread == NULL) {
762        for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
763           mPlaybackThreads.valueAt(i)->setStreamVolume(stream, value);
764        }
765    } else {
766        thread->setStreamVolume(stream, value);
767    }
768
769    return NO_ERROR;
770}
771
772status_t AudioFlinger::setStreamMute(audio_stream_type_t stream, bool muted)
773{
774    // check calling permissions
775    if (!settingsAllowed()) {
776        return PERMISSION_DENIED;
777    }
778
779    if (uint32_t(stream) >= AUDIO_STREAM_CNT ||
780        uint32_t(stream) == AUDIO_STREAM_ENFORCED_AUDIBLE) {
781        ALOGE("setStreamMute() invalid stream %d", stream);
782        return BAD_VALUE;
783    }
784
785    AutoMutex lock(mLock);
786    mStreamTypes[stream].mute = muted;
787    for (uint32_t i = 0; i < mPlaybackThreads.size(); i++)
788       mPlaybackThreads.valueAt(i)->setStreamMute(stream, muted);
789
790    return NO_ERROR;
791}
792
793float AudioFlinger::streamVolume(audio_stream_type_t stream, audio_io_handle_t output) const
794{
795    if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
796        return 0.0f;
797    }
798
799    AutoMutex lock(mLock);
800    float volume;
801    if (output) {
802        PlaybackThread *thread = checkPlaybackThread_l(output);
803        if (thread == NULL) {
804            return 0.0f;
805        }
806        volume = thread->streamVolume(stream);
807    } else {
808        volume = streamVolume_l(stream);
809    }
810
811    return volume;
812}
813
814bool AudioFlinger::streamMute(audio_stream_type_t stream) const
815{
816    if (uint32_t(stream) >= AUDIO_STREAM_CNT) {
817        return true;
818    }
819
820    AutoMutex lock(mLock);
821    return streamMute_l(stream);
822}
823
824status_t AudioFlinger::setParameters(audio_io_handle_t ioHandle, const String8& keyValuePairs)
825{
826    status_t result;
827
828    ALOGV("setParameters(): io %d, keyvalue %s, tid %d, calling pid %d",
829            ioHandle, keyValuePairs.string(), gettid(), IPCThreadState::self()->getCallingPid());
830    // check calling permissions
831    if (!settingsAllowed()) {
832        return PERMISSION_DENIED;
833    }
834
835    // ioHandle == 0 means the parameters are global to the audio hardware interface
836    if (ioHandle == 0) {
837        AutoMutex lock(mHardwareLock);
838        mHardwareStatus = AUDIO_SET_PARAMETER;
839        status_t final_result = NO_ERROR;
840        for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
841            audio_hw_device_t *dev = mAudioHwDevs[i];
842            result = dev->set_parameters(dev, keyValuePairs.string());
843            final_result = result ?: final_result;
844        }
845        mHardwareStatus = AUDIO_HW_IDLE;
846        // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
847        AudioParameter param = AudioParameter(keyValuePairs);
848        String8 value;
849        if (param.get(String8(AUDIO_PARAMETER_KEY_BT_NREC), value) == NO_ERROR) {
850            Mutex::Autolock _l(mLock);
851            bool btNrecIsOff = (value == AUDIO_PARAMETER_VALUE_OFF);
852            if (mBtNrecIsOff != btNrecIsOff) {
853                for (size_t i = 0; i < mRecordThreads.size(); i++) {
854                    sp<RecordThread> thread = mRecordThreads.valueAt(i);
855                    RecordThread::RecordTrack *track = thread->track();
856                    if (track != NULL) {
857                        audio_devices_t device = (audio_devices_t)(
858                                thread->device() & AUDIO_DEVICE_IN_ALL);
859                        bool suspend = audio_is_bluetooth_sco_device(device) && btNrecIsOff;
860                        thread->setEffectSuspended(FX_IID_AEC,
861                                                   suspend,
862                                                   track->sessionId());
863                        thread->setEffectSuspended(FX_IID_NS,
864                                                   suspend,
865                                                   track->sessionId());
866                    }
867                }
868                mBtNrecIsOff = btNrecIsOff;
869            }
870        }
871        return final_result;
872    }
873
874    // hold a strong ref on thread in case closeOutput() or closeInput() is called
875    // and the thread is exited once the lock is released
876    sp<ThreadBase> thread;
877    {
878        Mutex::Autolock _l(mLock);
879        thread = checkPlaybackThread_l(ioHandle);
880        if (thread == NULL) {
881            thread = checkRecordThread_l(ioHandle);
882        } else if (thread == primaryPlaybackThread_l()) {
883            // indicate output device change to all input threads for pre processing
884            AudioParameter param = AudioParameter(keyValuePairs);
885            int value;
886            if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) {
887                for (size_t i = 0; i < mRecordThreads.size(); i++) {
888                    mRecordThreads.valueAt(i)->setParameters(keyValuePairs);
889                }
890            }
891        }
892    }
893    if (thread != 0) {
894        return thread->setParameters(keyValuePairs);
895    }
896    return BAD_VALUE;
897}
898
899String8 AudioFlinger::getParameters(audio_io_handle_t ioHandle, const String8& keys) const
900{
901//    ALOGV("getParameters() io %d, keys %s, tid %d, calling pid %d",
902//            ioHandle, keys.string(), gettid(), IPCThreadState::self()->getCallingPid());
903
904    if (ioHandle == 0) {
905        String8 out_s8;
906
907        for (size_t i = 0; i < mAudioHwDevs.size(); i++) {
908            audio_hw_device_t *dev = mAudioHwDevs[i];
909            char *s = dev->get_parameters(dev, keys.string());
910            out_s8 += String8(s ? s : "");
911            free(s);
912        }
913        return out_s8;
914    }
915
916    Mutex::Autolock _l(mLock);
917
918    PlaybackThread *playbackThread = checkPlaybackThread_l(ioHandle);
919    if (playbackThread != NULL) {
920        return playbackThread->getParameters(keys);
921    }
922    RecordThread *recordThread = checkRecordThread_l(ioHandle);
923    if (recordThread != NULL) {
924        return recordThread->getParameters(keys);
925    }
926    return String8("");
927}
928
929size_t AudioFlinger::getInputBufferSize(uint32_t sampleRate, audio_format_t format, int channelCount) const
930{
931    status_t ret = initCheck();
932    if (ret != NO_ERROR) {
933        return 0;
934    }
935
936    AutoMutex lock(mHardwareLock);
937    mHardwareStatus = AUDIO_HW_GET_INPUT_BUFFER_SIZE;
938    size_t size = mPrimaryHardwareDev->get_input_buffer_size(mPrimaryHardwareDev, sampleRate, format, channelCount);
939    mHardwareStatus = AUDIO_HW_IDLE;
940    return size;
941}
942
943unsigned int AudioFlinger::getInputFramesLost(audio_io_handle_t ioHandle) const
944{
945    if (ioHandle == 0) {
946        return 0;
947    }
948
949    Mutex::Autolock _l(mLock);
950
951    RecordThread *recordThread = checkRecordThread_l(ioHandle);
952    if (recordThread != NULL) {
953        return recordThread->getInputFramesLost();
954    }
955    return 0;
956}
957
958status_t AudioFlinger::setVoiceVolume(float value)
959{
960    status_t ret = initCheck();
961    if (ret != NO_ERROR) {
962        return ret;
963    }
964
965    // check calling permissions
966    if (!settingsAllowed()) {
967        return PERMISSION_DENIED;
968    }
969
970    AutoMutex lock(mHardwareLock);
971    mHardwareStatus = AUDIO_SET_VOICE_VOLUME;
972    ret = mPrimaryHardwareDev->set_voice_volume(mPrimaryHardwareDev, value);
973    mHardwareStatus = AUDIO_HW_IDLE;
974
975    return ret;
976}
977
978status_t AudioFlinger::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames,
979        audio_io_handle_t output) const
980{
981    status_t status;
982
983    Mutex::Autolock _l(mLock);
984
985    PlaybackThread *playbackThread = checkPlaybackThread_l(output);
986    if (playbackThread != NULL) {
987        return playbackThread->getRenderPosition(halFrames, dspFrames);
988    }
989
990    return BAD_VALUE;
991}
992
993void AudioFlinger::registerClient(const sp<IAudioFlingerClient>& client)
994{
995
996    Mutex::Autolock _l(mLock);
997
998    pid_t pid = IPCThreadState::self()->getCallingPid();
999    if (mNotificationClients.indexOfKey(pid) < 0) {
1000        sp<NotificationClient> notificationClient = new NotificationClient(this,
1001                                                                            client,
1002                                                                            pid);
1003        ALOGV("registerClient() client %p, pid %d", notificationClient.get(), pid);
1004
1005        mNotificationClients.add(pid, notificationClient);
1006
1007        sp<IBinder> binder = client->asBinder();
1008        binder->linkToDeath(notificationClient);
1009
1010        // the config change is always sent from playback or record threads to avoid deadlock
1011        // with AudioSystem::gLock
1012        for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
1013            mPlaybackThreads.valueAt(i)->sendConfigEvent(AudioSystem::OUTPUT_OPENED);
1014        }
1015
1016        for (size_t i = 0; i < mRecordThreads.size(); i++) {
1017            mRecordThreads.valueAt(i)->sendConfigEvent(AudioSystem::INPUT_OPENED);
1018        }
1019    }
1020}
1021
1022void AudioFlinger::removeNotificationClient(pid_t pid)
1023{
1024    Mutex::Autolock _l(mLock);
1025
1026    ssize_t index = mNotificationClients.indexOfKey(pid);
1027    if (index >= 0) {
1028        sp <NotificationClient> client = mNotificationClients.valueFor(pid);
1029        ALOGV("removeNotificationClient() %p, pid %d", client.get(), pid);
1030        mNotificationClients.removeItem(pid);
1031    }
1032
1033    ALOGV("%d died, releasing its sessions", pid);
1034    size_t num = mAudioSessionRefs.size();
1035    bool removed = false;
1036    for (size_t i = 0; i< num; ) {
1037        AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
1038        ALOGV(" pid %d @ %d", ref->pid, i);
1039        if (ref->pid == pid) {
1040            ALOGV(" removing entry for pid %d session %d", pid, ref->sessionid);
1041            mAudioSessionRefs.removeAt(i);
1042            delete ref;
1043            removed = true;
1044            num--;
1045        } else {
1046            i++;
1047        }
1048    }
1049    if (removed) {
1050        purgeStaleEffects_l();
1051    }
1052}
1053
1054// audioConfigChanged_l() must be called with AudioFlinger::mLock held
1055void AudioFlinger::audioConfigChanged_l(int event, audio_io_handle_t ioHandle, void *param2)
1056{
1057    size_t size = mNotificationClients.size();
1058    for (size_t i = 0; i < size; i++) {
1059        mNotificationClients.valueAt(i)->audioFlingerClient()->ioConfigChanged(event, ioHandle,
1060                                                                               param2);
1061    }
1062}
1063
1064// removeClient_l() must be called with AudioFlinger::mLock held
1065void AudioFlinger::removeClient_l(pid_t pid)
1066{
1067    ALOGV("removeClient_l() pid %d, tid %d, calling tid %d", pid, gettid(), IPCThreadState::self()->getCallingPid());
1068    mClients.removeItem(pid);
1069}
1070
1071
1072// ----------------------------------------------------------------------------
1073
1074AudioFlinger::ThreadBase::ThreadBase(const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id,
1075        uint32_t device, type_t type)
1076    :   Thread(false),
1077        mType(type),
1078        mAudioFlinger(audioFlinger), mSampleRate(0), mFrameCount(0),
1079        // mChannelMask
1080        mChannelCount(0),
1081        mFrameSize(1), mFormat(AUDIO_FORMAT_INVALID),
1082        mParamStatus(NO_ERROR),
1083        mStandby(false), mId(id),
1084        mDevice(device),
1085        mDeathRecipient(new PMDeathRecipient(this))
1086{
1087}
1088
1089AudioFlinger::ThreadBase::~ThreadBase()
1090{
1091    mParamCond.broadcast();
1092    // do not lock the mutex in destructor
1093    releaseWakeLock_l();
1094    if (mPowerManager != 0) {
1095        sp<IBinder> binder = mPowerManager->asBinder();
1096        binder->unlinkToDeath(mDeathRecipient);
1097    }
1098}
1099
1100void AudioFlinger::ThreadBase::exit()
1101{
1102    ALOGV("ThreadBase::exit");
1103    {
1104        // This lock prevents the following race in thread (uniprocessor for illustration):
1105        //  if (!exitPending()) {
1106        //      // context switch from here to exit()
1107        //      // exit() calls requestExit(), what exitPending() observes
1108        //      // exit() calls signal(), which is dropped since no waiters
1109        //      // context switch back from exit() to here
1110        //      mWaitWorkCV.wait(...);
1111        //      // now thread is hung
1112        //  }
1113        AutoMutex lock(mLock);
1114        requestExit();
1115        mWaitWorkCV.signal();
1116    }
1117    // When Thread::requestExitAndWait is made virtual and this method is renamed to
1118    // "virtual status_t requestExitAndWait()", replace by "return Thread::requestExitAndWait();"
1119    requestExitAndWait();
1120}
1121
1122status_t AudioFlinger::ThreadBase::setParameters(const String8& keyValuePairs)
1123{
1124    status_t status;
1125
1126    ALOGV("ThreadBase::setParameters() %s", keyValuePairs.string());
1127    Mutex::Autolock _l(mLock);
1128
1129    mNewParameters.add(keyValuePairs);
1130    mWaitWorkCV.signal();
1131    // wait condition with timeout in case the thread loop has exited
1132    // before the request could be processed
1133    if (mParamCond.waitRelative(mLock, kSetParametersTimeoutNs) == NO_ERROR) {
1134        status = mParamStatus;
1135        mWaitWorkCV.signal();
1136    } else {
1137        status = TIMED_OUT;
1138    }
1139    return status;
1140}
1141
1142void AudioFlinger::ThreadBase::sendConfigEvent(int event, int param)
1143{
1144    Mutex::Autolock _l(mLock);
1145    sendConfigEvent_l(event, param);
1146}
1147
1148// sendConfigEvent_l() must be called with ThreadBase::mLock held
1149void AudioFlinger::ThreadBase::sendConfigEvent_l(int event, int param)
1150{
1151    ConfigEvent configEvent;
1152    configEvent.mEvent = event;
1153    configEvent.mParam = param;
1154    mConfigEvents.add(configEvent);
1155    ALOGV("sendConfigEvent() num events %d event %d, param %d", mConfigEvents.size(), event, param);
1156    mWaitWorkCV.signal();
1157}
1158
1159void AudioFlinger::ThreadBase::processConfigEvents()
1160{
1161    mLock.lock();
1162    while(!mConfigEvents.isEmpty()) {
1163        ALOGV("processConfigEvents() remaining events %d", mConfigEvents.size());
1164        ConfigEvent configEvent = mConfigEvents[0];
1165        mConfigEvents.removeAt(0);
1166        // release mLock before locking AudioFlinger mLock: lock order is always
1167        // AudioFlinger then ThreadBase to avoid cross deadlock
1168        mLock.unlock();
1169        mAudioFlinger->mLock.lock();
1170        audioConfigChanged_l(configEvent.mEvent, configEvent.mParam);
1171        mAudioFlinger->mLock.unlock();
1172        mLock.lock();
1173    }
1174    mLock.unlock();
1175}
1176
1177status_t AudioFlinger::ThreadBase::dumpBase(int fd, const Vector<String16>& args)
1178{
1179    const size_t SIZE = 256;
1180    char buffer[SIZE];
1181    String8 result;
1182
1183    bool locked = tryLock(mLock);
1184    if (!locked) {
1185        snprintf(buffer, SIZE, "thread %p maybe dead locked\n", this);
1186        write(fd, buffer, strlen(buffer));
1187    }
1188
1189    snprintf(buffer, SIZE, "standby: %d\n", mStandby);
1190    result.append(buffer);
1191    snprintf(buffer, SIZE, "Sample rate: %d\n", mSampleRate);
1192    result.append(buffer);
1193    snprintf(buffer, SIZE, "Frame count: %d\n", mFrameCount);
1194    result.append(buffer);
1195    snprintf(buffer, SIZE, "Channel Count: %d\n", mChannelCount);
1196    result.append(buffer);
1197    snprintf(buffer, SIZE, "Channel Mask: 0x%08x\n", mChannelMask);
1198    result.append(buffer);
1199    snprintf(buffer, SIZE, "Format: %d\n", mFormat);
1200    result.append(buffer);
1201    snprintf(buffer, SIZE, "Frame size: %u\n", mFrameSize);
1202    result.append(buffer);
1203
1204    snprintf(buffer, SIZE, "\nPending setParameters commands: \n");
1205    result.append(buffer);
1206    result.append(" Index Command");
1207    for (size_t i = 0; i < mNewParameters.size(); ++i) {
1208        snprintf(buffer, SIZE, "\n %02d    ", i);
1209        result.append(buffer);
1210        result.append(mNewParameters[i]);
1211    }
1212
1213    snprintf(buffer, SIZE, "\n\nPending config events: \n");
1214    result.append(buffer);
1215    snprintf(buffer, SIZE, " Index event param\n");
1216    result.append(buffer);
1217    for (size_t i = 0; i < mConfigEvents.size(); i++) {
1218        snprintf(buffer, SIZE, " %02d    %02d    %d\n", i, mConfigEvents[i].mEvent, mConfigEvents[i].mParam);
1219        result.append(buffer);
1220    }
1221    result.append("\n");
1222
1223    write(fd, result.string(), result.size());
1224
1225    if (locked) {
1226        mLock.unlock();
1227    }
1228    return NO_ERROR;
1229}
1230
1231status_t AudioFlinger::ThreadBase::dumpEffectChains(int fd, const Vector<String16>& args)
1232{
1233    const size_t SIZE = 256;
1234    char buffer[SIZE];
1235    String8 result;
1236
1237    snprintf(buffer, SIZE, "\n- %d Effect Chains:\n", mEffectChains.size());
1238    write(fd, buffer, strlen(buffer));
1239
1240    for (size_t i = 0; i < mEffectChains.size(); ++i) {
1241        sp<EffectChain> chain = mEffectChains[i];
1242        if (chain != 0) {
1243            chain->dump(fd, args);
1244        }
1245    }
1246    return NO_ERROR;
1247}
1248
1249void AudioFlinger::ThreadBase::acquireWakeLock()
1250{
1251    Mutex::Autolock _l(mLock);
1252    acquireWakeLock_l();
1253}
1254
1255void AudioFlinger::ThreadBase::acquireWakeLock_l()
1256{
1257    if (mPowerManager == 0) {
1258        // use checkService() to avoid blocking if power service is not up yet
1259        sp<IBinder> binder =
1260            defaultServiceManager()->checkService(String16("power"));
1261        if (binder == 0) {
1262            ALOGW("Thread %s cannot connect to the power manager service", mName);
1263        } else {
1264            mPowerManager = interface_cast<IPowerManager>(binder);
1265            binder->linkToDeath(mDeathRecipient);
1266        }
1267    }
1268    if (mPowerManager != 0) {
1269        sp<IBinder> binder = new BBinder();
1270        status_t status = mPowerManager->acquireWakeLock(POWERMANAGER_PARTIAL_WAKE_LOCK,
1271                                                         binder,
1272                                                         String16(mName));
1273        if (status == NO_ERROR) {
1274            mWakeLockToken = binder;
1275        }
1276        ALOGV("acquireWakeLock_l() %s status %d", mName, status);
1277    }
1278}
1279
1280void AudioFlinger::ThreadBase::releaseWakeLock()
1281{
1282    Mutex::Autolock _l(mLock);
1283    releaseWakeLock_l();
1284}
1285
1286void AudioFlinger::ThreadBase::releaseWakeLock_l()
1287{
1288    if (mWakeLockToken != 0) {
1289        ALOGV("releaseWakeLock_l() %s", mName);
1290        if (mPowerManager != 0) {
1291            mPowerManager->releaseWakeLock(mWakeLockToken, 0);
1292        }
1293        mWakeLockToken.clear();
1294    }
1295}
1296
1297void AudioFlinger::ThreadBase::clearPowerManager()
1298{
1299    Mutex::Autolock _l(mLock);
1300    releaseWakeLock_l();
1301    mPowerManager.clear();
1302}
1303
1304void AudioFlinger::ThreadBase::PMDeathRecipient::binderDied(const wp<IBinder>& who)
1305{
1306    sp<ThreadBase> thread = mThread.promote();
1307    if (thread != 0) {
1308        thread->clearPowerManager();
1309    }
1310    ALOGW("power manager service died !!!");
1311}
1312
1313void AudioFlinger::ThreadBase::setEffectSuspended(
1314        const effect_uuid_t *type, bool suspend, int sessionId)
1315{
1316    Mutex::Autolock _l(mLock);
1317    setEffectSuspended_l(type, suspend, sessionId);
1318}
1319
1320void AudioFlinger::ThreadBase::setEffectSuspended_l(
1321        const effect_uuid_t *type, bool suspend, int sessionId)
1322{
1323    sp<EffectChain> chain = getEffectChain_l(sessionId);
1324    if (chain != 0) {
1325        if (type != NULL) {
1326            chain->setEffectSuspended_l(type, suspend);
1327        } else {
1328            chain->setEffectSuspendedAll_l(suspend);
1329        }
1330    }
1331
1332    updateSuspendedSessions_l(type, suspend, sessionId);
1333}
1334
1335void AudioFlinger::ThreadBase::checkSuspendOnAddEffectChain_l(const sp<EffectChain>& chain)
1336{
1337    ssize_t index = mSuspendedSessions.indexOfKey(chain->sessionId());
1338    if (index < 0) {
1339        return;
1340    }
1341
1342    KeyedVector <int, sp<SuspendedSessionDesc> > sessionEffects =
1343            mSuspendedSessions.editValueAt(index);
1344
1345    for (size_t i = 0; i < sessionEffects.size(); i++) {
1346        sp <SuspendedSessionDesc> desc = sessionEffects.valueAt(i);
1347        for (int j = 0; j < desc->mRefCount; j++) {
1348            if (sessionEffects.keyAt(i) == EffectChain::kKeyForSuspendAll) {
1349                chain->setEffectSuspendedAll_l(true);
1350            } else {
1351                ALOGV("checkSuspendOnAddEffectChain_l() suspending effects %08x",
1352                     desc->mType.timeLow);
1353                chain->setEffectSuspended_l(&desc->mType, true);
1354            }
1355        }
1356    }
1357}
1358
1359void AudioFlinger::ThreadBase::updateSuspendedSessions_l(const effect_uuid_t *type,
1360                                                         bool suspend,
1361                                                         int sessionId)
1362{
1363    ssize_t index = mSuspendedSessions.indexOfKey(sessionId);
1364
1365    KeyedVector <int, sp<SuspendedSessionDesc> > sessionEffects;
1366
1367    if (suspend) {
1368        if (index >= 0) {
1369            sessionEffects = mSuspendedSessions.editValueAt(index);
1370        } else {
1371            mSuspendedSessions.add(sessionId, sessionEffects);
1372        }
1373    } else {
1374        if (index < 0) {
1375            return;
1376        }
1377        sessionEffects = mSuspendedSessions.editValueAt(index);
1378    }
1379
1380
1381    int key = EffectChain::kKeyForSuspendAll;
1382    if (type != NULL) {
1383        key = type->timeLow;
1384    }
1385    index = sessionEffects.indexOfKey(key);
1386
1387    sp <SuspendedSessionDesc> desc;
1388    if (suspend) {
1389        if (index >= 0) {
1390            desc = sessionEffects.valueAt(index);
1391        } else {
1392            desc = new SuspendedSessionDesc();
1393            if (type != NULL) {
1394                memcpy(&desc->mType, type, sizeof(effect_uuid_t));
1395            }
1396            sessionEffects.add(key, desc);
1397            ALOGV("updateSuspendedSessions_l() suspend adding effect %08x", key);
1398        }
1399        desc->mRefCount++;
1400    } else {
1401        if (index < 0) {
1402            return;
1403        }
1404        desc = sessionEffects.valueAt(index);
1405        if (--desc->mRefCount == 0) {
1406            ALOGV("updateSuspendedSessions_l() restore removing effect %08x", key);
1407            sessionEffects.removeItemsAt(index);
1408            if (sessionEffects.isEmpty()) {
1409                ALOGV("updateSuspendedSessions_l() restore removing session %d",
1410                                 sessionId);
1411                mSuspendedSessions.removeItem(sessionId);
1412            }
1413        }
1414    }
1415    if (!sessionEffects.isEmpty()) {
1416        mSuspendedSessions.replaceValueFor(sessionId, sessionEffects);
1417    }
1418}
1419
1420void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
1421                                                            bool enabled,
1422                                                            int sessionId)
1423{
1424    Mutex::Autolock _l(mLock);
1425    checkSuspendOnEffectEnabled_l(effect, enabled, sessionId);
1426}
1427
1428void AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled_l(const sp<EffectModule>& effect,
1429                                                            bool enabled,
1430                                                            int sessionId)
1431{
1432    if (mType != RECORD) {
1433        // suspend all effects in AUDIO_SESSION_OUTPUT_MIX when enabling any effect on
1434        // another session. This gives the priority to well behaved effect control panels
1435        // and applications not using global effects.
1436        if (sessionId != AUDIO_SESSION_OUTPUT_MIX) {
1437            setEffectSuspended_l(NULL, enabled, AUDIO_SESSION_OUTPUT_MIX);
1438        }
1439    }
1440
1441    sp<EffectChain> chain = getEffectChain_l(sessionId);
1442    if (chain != 0) {
1443        chain->checkSuspendOnEffectEnabled(effect, enabled);
1444    }
1445}
1446
1447// ----------------------------------------------------------------------------
1448
1449AudioFlinger::PlaybackThread::PlaybackThread(const sp<AudioFlinger>& audioFlinger,
1450                                             AudioStreamOut* output,
1451                                             audio_io_handle_t id,
1452                                             uint32_t device,
1453                                             type_t type)
1454    :   ThreadBase(audioFlinger, id, device, type),
1455        mMixBuffer(NULL), mSuspended(0), mBytesWritten(0),
1456        // Assumes constructor is called by AudioFlinger with it's mLock held,
1457        // but it would be safer to explicitly pass initial masterMute as parameter
1458        mMasterMute(audioFlinger->masterMute_l()),
1459        // mStreamTypes[] initialized in constructor body
1460        mOutput(output),
1461        // Assumes constructor is called by AudioFlinger with it's mLock held,
1462        // but it would be safer to explicitly pass initial masterVolume as parameter
1463        mMasterVolume(audioFlinger->masterVolumeSW_l()),
1464        mLastWriteTime(0), mNumWrites(0), mNumDelayedWrites(0), mInWrite(false)
1465{
1466    snprintf(mName, kNameLength, "AudioOut_%d", id);
1467
1468    readOutputParameters();
1469
1470    // mStreamTypes[AUDIO_STREAM_CNT] is initialized by stream_type_t default constructor
1471    // There is no AUDIO_STREAM_MIN, and ++ operator does not compile
1472    for (audio_stream_type_t stream = (audio_stream_type_t) 0; stream < AUDIO_STREAM_CNT;
1473            stream = (audio_stream_type_t) (stream + 1)) {
1474        mStreamTypes[stream].volume = mAudioFlinger->streamVolume_l(stream);
1475        mStreamTypes[stream].mute = mAudioFlinger->streamMute_l(stream);
1476        // initialized by stream_type_t default constructor
1477        // mStreamTypes[stream].valid = true;
1478    }
1479    // mStreamTypes[AUDIO_STREAM_CNT] exists but isn't explicitly initialized here,
1480    // because mAudioFlinger doesn't have one to copy from
1481}
1482
1483AudioFlinger::PlaybackThread::~PlaybackThread()
1484{
1485    delete [] mMixBuffer;
1486}
1487
1488status_t AudioFlinger::PlaybackThread::dump(int fd, const Vector<String16>& args)
1489{
1490    dumpInternals(fd, args);
1491    dumpTracks(fd, args);
1492    dumpEffectChains(fd, args);
1493    return NO_ERROR;
1494}
1495
1496status_t AudioFlinger::PlaybackThread::dumpTracks(int fd, const Vector<String16>& args)
1497{
1498    const size_t SIZE = 256;
1499    char buffer[SIZE];
1500    String8 result;
1501
1502    snprintf(buffer, SIZE, "Output thread %p tracks\n", this);
1503    result.append(buffer);
1504    result.append("   Name  Clien Typ Fmt Chn mask   Session Buf  S M F SRate LeftV RighV  Serv       User       Main buf   Aux Buf\n");
1505    for (size_t i = 0; i < mTracks.size(); ++i) {
1506        sp<Track> track = mTracks[i];
1507        if (track != 0) {
1508            track->dump(buffer, SIZE);
1509            result.append(buffer);
1510        }
1511    }
1512
1513    snprintf(buffer, SIZE, "Output thread %p active tracks\n", this);
1514    result.append(buffer);
1515    result.append("   Name  Clien Typ Fmt Chn mask   Session Buf  S M F SRate LeftV RighV  Serv       User       Main buf   Aux Buf\n");
1516    for (size_t i = 0; i < mActiveTracks.size(); ++i) {
1517        sp<Track> track = mActiveTracks[i].promote();
1518        if (track != 0) {
1519            track->dump(buffer, SIZE);
1520            result.append(buffer);
1521        }
1522    }
1523    write(fd, result.string(), result.size());
1524    return NO_ERROR;
1525}
1526
1527status_t AudioFlinger::PlaybackThread::dumpInternals(int fd, const Vector<String16>& args)
1528{
1529    const size_t SIZE = 256;
1530    char buffer[SIZE];
1531    String8 result;
1532
1533    snprintf(buffer, SIZE, "\nOutput thread %p internals\n", this);
1534    result.append(buffer);
1535    snprintf(buffer, SIZE, "last write occurred (msecs): %llu\n", ns2ms(systemTime() - mLastWriteTime));
1536    result.append(buffer);
1537    snprintf(buffer, SIZE, "total writes: %d\n", mNumWrites);
1538    result.append(buffer);
1539    snprintf(buffer, SIZE, "delayed writes: %d\n", mNumDelayedWrites);
1540    result.append(buffer);
1541    snprintf(buffer, SIZE, "blocked in write: %d\n", mInWrite);
1542    result.append(buffer);
1543    snprintf(buffer, SIZE, "suspend count: %d\n", mSuspended);
1544    result.append(buffer);
1545    snprintf(buffer, SIZE, "mix buffer : %p\n", mMixBuffer);
1546    result.append(buffer);
1547    write(fd, result.string(), result.size());
1548
1549    dumpBase(fd, args);
1550
1551    return NO_ERROR;
1552}
1553
1554// Thread virtuals
1555status_t AudioFlinger::PlaybackThread::readyToRun()
1556{
1557    status_t status = initCheck();
1558    if (status == NO_ERROR) {
1559        ALOGI("AudioFlinger's thread %p ready to run", this);
1560    } else {
1561        ALOGE("No working audio driver found.");
1562    }
1563    return status;
1564}
1565
1566void AudioFlinger::PlaybackThread::onFirstRef()
1567{
1568    run(mName, ANDROID_PRIORITY_URGENT_AUDIO);
1569}
1570
1571// PlaybackThread::createTrack_l() must be called with AudioFlinger::mLock held
1572sp<AudioFlinger::PlaybackThread::Track>  AudioFlinger::PlaybackThread::createTrack_l(
1573        const sp<AudioFlinger::Client>& client,
1574        audio_stream_type_t streamType,
1575        uint32_t sampleRate,
1576        audio_format_t format,
1577        uint32_t channelMask,
1578        int frameCount,
1579        const sp<IMemory>& sharedBuffer,
1580        int sessionId,
1581        bool isTimed,
1582        status_t *status)
1583{
1584    sp<Track> track;
1585    status_t lStatus;
1586
1587    if (mType == DIRECT) {
1588        if ((format & AUDIO_FORMAT_MAIN_MASK) == AUDIO_FORMAT_PCM) {
1589            if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) {
1590                ALOGE("createTrack_l() Bad parameter: sampleRate %d format %d, channelMask 0x%08x \""
1591                        "for output %p with format %d",
1592                        sampleRate, format, channelMask, mOutput, mFormat);
1593                lStatus = BAD_VALUE;
1594                goto Exit;
1595            }
1596        }
1597    } else {
1598        // Resampler implementation limits input sampling rate to 2 x output sampling rate.
1599        if (sampleRate > mSampleRate*2) {
1600            ALOGE("Sample rate out of range: %d mSampleRate %d", sampleRate, mSampleRate);
1601            lStatus = BAD_VALUE;
1602            goto Exit;
1603        }
1604    }
1605
1606    lStatus = initCheck();
1607    if (lStatus != NO_ERROR) {
1608        ALOGE("Audio driver not initialized.");
1609        goto Exit;
1610    }
1611
1612    { // scope for mLock
1613        Mutex::Autolock _l(mLock);
1614
1615        // all tracks in same audio session must share the same routing strategy otherwise
1616        // conflicts will happen when tracks are moved from one output to another by audio policy
1617        // manager
1618        uint32_t strategy = AudioSystem::getStrategyForStream(streamType);
1619        for (size_t i = 0; i < mTracks.size(); ++i) {
1620            sp<Track> t = mTracks[i];
1621            if (t != 0) {
1622                uint32_t actual = AudioSystem::getStrategyForStream(t->streamType());
1623                if (sessionId == t->sessionId() && strategy != actual) {
1624                    ALOGE("createTrack_l() mismatched strategy; expected %u but found %u",
1625                            strategy, actual);
1626                    lStatus = BAD_VALUE;
1627                    goto Exit;
1628                }
1629            }
1630        }
1631
1632        if (!isTimed) {
1633            track = new Track(this, client, streamType, sampleRate, format,
1634                    channelMask, frameCount, sharedBuffer, sessionId);
1635        } else {
1636            track = TimedTrack::create(this, client, streamType, sampleRate, format,
1637                    channelMask, frameCount, sharedBuffer, sessionId);
1638        }
1639        if (track == NULL || track->getCblk() == NULL || track->name() < 0) {
1640            lStatus = NO_MEMORY;
1641            goto Exit;
1642        }
1643        mTracks.add(track);
1644
1645        sp<EffectChain> chain = getEffectChain_l(sessionId);
1646        if (chain != 0) {
1647            ALOGV("createTrack_l() setting main buffer %p", chain->inBuffer());
1648            track->setMainBuffer(chain->inBuffer());
1649            chain->setStrategy(AudioSystem::getStrategyForStream(track->streamType()));
1650            chain->incTrackCnt();
1651        }
1652
1653        // invalidate track immediately if the stream type was moved to another thread since
1654        // createTrack() was called by the client process.
1655        if (!mStreamTypes[streamType].valid) {
1656            ALOGW("createTrack_l() on thread %p: invalidating track on stream %d",
1657                 this, streamType);
1658            android_atomic_or(CBLK_INVALID_ON, &track->mCblk->flags);
1659        }
1660    }
1661    lStatus = NO_ERROR;
1662
1663Exit:
1664    if(status) {
1665        *status = lStatus;
1666    }
1667    return track;
1668}
1669
1670uint32_t AudioFlinger::PlaybackThread::latency() const
1671{
1672    Mutex::Autolock _l(mLock);
1673    if (initCheck() == NO_ERROR) {
1674        return mOutput->stream->get_latency(mOutput->stream);
1675    } else {
1676        return 0;
1677    }
1678}
1679
1680void AudioFlinger::PlaybackThread::setMasterVolume(float value)
1681{
1682    Mutex::Autolock _l(mLock);
1683    mMasterVolume = value;
1684}
1685
1686void AudioFlinger::PlaybackThread::setMasterMute(bool muted)
1687{
1688    Mutex::Autolock _l(mLock);
1689    setMasterMute_l(muted);
1690}
1691
1692void AudioFlinger::PlaybackThread::setStreamVolume(audio_stream_type_t stream, float value)
1693{
1694    Mutex::Autolock _l(mLock);
1695    mStreamTypes[stream].volume = value;
1696}
1697
1698void AudioFlinger::PlaybackThread::setStreamMute(audio_stream_type_t stream, bool muted)
1699{
1700    Mutex::Autolock _l(mLock);
1701    mStreamTypes[stream].mute = muted;
1702}
1703
1704float AudioFlinger::PlaybackThread::streamVolume(audio_stream_type_t stream) const
1705{
1706    Mutex::Autolock _l(mLock);
1707    return mStreamTypes[stream].volume;
1708}
1709
1710// addTrack_l() must be called with ThreadBase::mLock held
1711status_t AudioFlinger::PlaybackThread::addTrack_l(const sp<Track>& track)
1712{
1713    status_t status = ALREADY_EXISTS;
1714
1715    // set retry count for buffer fill
1716    track->mRetryCount = kMaxTrackStartupRetries;
1717    if (mActiveTracks.indexOf(track) < 0) {
1718        // the track is newly added, make sure it fills up all its
1719        // buffers before playing. This is to ensure the client will
1720        // effectively get the latency it requested.
1721        track->mFillingUpStatus = Track::FS_FILLING;
1722        track->mResetDone = false;
1723        mActiveTracks.add(track);
1724        if (track->mainBuffer() != mMixBuffer) {
1725            sp<EffectChain> chain = getEffectChain_l(track->sessionId());
1726            if (chain != 0) {
1727                ALOGV("addTrack_l() starting track on chain %p for session %d", chain.get(), track->sessionId());
1728                chain->incActiveTrackCnt();
1729            }
1730        }
1731
1732        status = NO_ERROR;
1733    }
1734
1735    ALOGV("mWaitWorkCV.broadcast");
1736    mWaitWorkCV.broadcast();
1737
1738    return status;
1739}
1740
1741// destroyTrack_l() must be called with ThreadBase::mLock held
1742void AudioFlinger::PlaybackThread::destroyTrack_l(const sp<Track>& track)
1743{
1744    track->mState = TrackBase::TERMINATED;
1745    if (mActiveTracks.indexOf(track) < 0) {
1746        removeTrack_l(track);
1747    }
1748}
1749
1750void AudioFlinger::PlaybackThread::removeTrack_l(const sp<Track>& track)
1751{
1752    mTracks.remove(track);
1753    deleteTrackName_l(track->name());
1754    sp<EffectChain> chain = getEffectChain_l(track->sessionId());
1755    if (chain != 0) {
1756        chain->decTrackCnt();
1757    }
1758}
1759
1760String8 AudioFlinger::PlaybackThread::getParameters(const String8& keys)
1761{
1762    String8 out_s8 = String8("");
1763    char *s;
1764
1765    Mutex::Autolock _l(mLock);
1766    if (initCheck() != NO_ERROR) {
1767        return out_s8;
1768    }
1769
1770    s = mOutput->stream->common.get_parameters(&mOutput->stream->common, keys.string());
1771    out_s8 = String8(s);
1772    free(s);
1773    return out_s8;
1774}
1775
1776// audioConfigChanged_l() must be called with AudioFlinger::mLock held
1777void AudioFlinger::PlaybackThread::audioConfigChanged_l(int event, int param) {
1778    AudioSystem::OutputDescriptor desc;
1779    void *param2 = NULL;
1780
1781    ALOGV("PlaybackThread::audioConfigChanged_l, thread %p, event %d, param %d", this, event, param);
1782
1783    switch (event) {
1784    case AudioSystem::OUTPUT_OPENED:
1785    case AudioSystem::OUTPUT_CONFIG_CHANGED:
1786        desc.channels = mChannelMask;
1787        desc.samplingRate = mSampleRate;
1788        desc.format = mFormat;
1789        desc.frameCount = mFrameCount;
1790        desc.latency = latency();
1791        param2 = &desc;
1792        break;
1793
1794    case AudioSystem::STREAM_CONFIG_CHANGED:
1795        param2 = &param;
1796    case AudioSystem::OUTPUT_CLOSED:
1797    default:
1798        break;
1799    }
1800    mAudioFlinger->audioConfigChanged_l(event, mId, param2);
1801}
1802
1803void AudioFlinger::PlaybackThread::readOutputParameters()
1804{
1805    mSampleRate = mOutput->stream->common.get_sample_rate(&mOutput->stream->common);
1806    mChannelMask = mOutput->stream->common.get_channels(&mOutput->stream->common);
1807    mChannelCount = (uint16_t)popcount(mChannelMask);
1808    mFormat = mOutput->stream->common.get_format(&mOutput->stream->common);
1809    mFrameSize = audio_stream_frame_size(&mOutput->stream->common);
1810    mFrameCount = mOutput->stream->common.get_buffer_size(&mOutput->stream->common) / mFrameSize;
1811
1812    // FIXME - Current mixer implementation only supports stereo output: Always
1813    // Allocate a stereo buffer even if HW output is mono.
1814    delete[] mMixBuffer;
1815    mMixBuffer = new int16_t[mFrameCount * 2];
1816    memset(mMixBuffer, 0, mFrameCount * 2 * sizeof(int16_t));
1817
1818    // force reconfiguration of effect chains and engines to take new buffer size and audio
1819    // parameters into account
1820    // Note that mLock is not held when readOutputParameters() is called from the constructor
1821    // but in this case nothing is done below as no audio sessions have effect yet so it doesn't
1822    // matter.
1823    // create a copy of mEffectChains as calling moveEffectChain_l() can reorder some effect chains
1824    Vector< sp<EffectChain> > effectChains = mEffectChains;
1825    for (size_t i = 0; i < effectChains.size(); i ++) {
1826        mAudioFlinger->moveEffectChain_l(effectChains[i]->sessionId(), this, this, false);
1827    }
1828}
1829
1830status_t AudioFlinger::PlaybackThread::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames)
1831{
1832    if (halFrames == NULL || dspFrames == NULL) {
1833        return BAD_VALUE;
1834    }
1835    Mutex::Autolock _l(mLock);
1836    if (initCheck() != NO_ERROR) {
1837        return INVALID_OPERATION;
1838    }
1839    *halFrames = mBytesWritten / audio_stream_frame_size(&mOutput->stream->common);
1840
1841    return mOutput->stream->get_render_position(mOutput->stream, dspFrames);
1842}
1843
1844uint32_t AudioFlinger::PlaybackThread::hasAudioSession(int sessionId)
1845{
1846    Mutex::Autolock _l(mLock);
1847    uint32_t result = 0;
1848    if (getEffectChain_l(sessionId) != 0) {
1849        result = EFFECT_SESSION;
1850    }
1851
1852    for (size_t i = 0; i < mTracks.size(); ++i) {
1853        sp<Track> track = mTracks[i];
1854        if (sessionId == track->sessionId() &&
1855                !(track->mCblk->flags & CBLK_INVALID_MSK)) {
1856            result |= TRACK_SESSION;
1857            break;
1858        }
1859    }
1860
1861    return result;
1862}
1863
1864uint32_t AudioFlinger::PlaybackThread::getStrategyForSession_l(int sessionId)
1865{
1866    // session AUDIO_SESSION_OUTPUT_MIX is placed in same strategy as MUSIC stream so that
1867    // it is moved to correct output by audio policy manager when A2DP is connected or disconnected
1868    if (sessionId == AUDIO_SESSION_OUTPUT_MIX) {
1869        return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
1870    }
1871    for (size_t i = 0; i < mTracks.size(); i++) {
1872        sp<Track> track = mTracks[i];
1873        if (sessionId == track->sessionId() &&
1874                !(track->mCblk->flags & CBLK_INVALID_MSK)) {
1875            return AudioSystem::getStrategyForStream(track->streamType());
1876        }
1877    }
1878    return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
1879}
1880
1881
1882AudioFlinger::AudioStreamOut* AudioFlinger::PlaybackThread::getOutput() const
1883{
1884    Mutex::Autolock _l(mLock);
1885    return mOutput;
1886}
1887
1888AudioFlinger::AudioStreamOut* AudioFlinger::PlaybackThread::clearOutput()
1889{
1890    Mutex::Autolock _l(mLock);
1891    AudioStreamOut *output = mOutput;
1892    mOutput = NULL;
1893    return output;
1894}
1895
1896// this method must always be called either with ThreadBase mLock held or inside the thread loop
1897audio_stream_t* AudioFlinger::PlaybackThread::stream()
1898{
1899    if (mOutput == NULL) {
1900        return NULL;
1901    }
1902    return &mOutput->stream->common;
1903}
1904
1905uint32_t AudioFlinger::PlaybackThread::activeSleepTimeUs()
1906{
1907    // A2DP output latency is not due only to buffering capacity. It also reflects encoding,
1908    // decoding and transfer time. So sleeping for half of the latency would likely cause
1909    // underruns
1910    if (audio_is_a2dp_device((audio_devices_t)mDevice)) {
1911        return (uint32_t)((uint32_t)((mFrameCount * 1000) / mSampleRate) * 1000);
1912    } else {
1913        return (uint32_t)(mOutput->stream->get_latency(mOutput->stream) * 1000) / 2;
1914    }
1915}
1916
1917// ----------------------------------------------------------------------------
1918
1919AudioFlinger::MixerThread::MixerThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output,
1920        audio_io_handle_t id, uint32_t device, type_t type)
1921    :   PlaybackThread(audioFlinger, output, id, device, type),
1922        mAudioMixer(new AudioMixer(mFrameCount, mSampleRate)),
1923        mPrevMixerStatus(MIXER_IDLE)
1924{
1925    // FIXME - Current mixer implementation only supports stereo output
1926    if (mChannelCount == 1) {
1927        ALOGE("Invalid audio hardware channel count");
1928    }
1929}
1930
1931AudioFlinger::MixerThread::~MixerThread()
1932{
1933    delete mAudioMixer;
1934}
1935
1936class CpuStats {
1937public:
1938    void sample();
1939#ifdef DEBUG_CPU_USAGE
1940private:
1941    ThreadCpuUsage mCpu;
1942#endif
1943};
1944
1945void CpuStats::sample() {
1946#ifdef DEBUG_CPU_USAGE
1947    const CentralTendencyStatistics& stats = mCpu.statistics();
1948    mCpu.sampleAndEnable();
1949    unsigned n = stats.n();
1950    // mCpu.elapsed() is expensive, so don't call it every loop
1951    if ((n & 127) == 1) {
1952        long long elapsed = mCpu.elapsed();
1953        if (elapsed >= DEBUG_CPU_USAGE * 1000000000LL) {
1954            double perLoop = elapsed / (double) n;
1955            double perLoop100 = perLoop * 0.01;
1956            double mean = stats.mean();
1957            double stddev = stats.stddev();
1958            double minimum = stats.minimum();
1959            double maximum = stats.maximum();
1960            mCpu.resetStatistics();
1961            ALOGI("CPU usage over past %.1f secs (%u mixer loops at %.1f mean ms per loop):\n  us per mix loop: mean=%.0f stddev=%.0f min=%.0f max=%.0f\n  %% of wall: mean=%.1f stddev=%.1f min=%.1f max=%.1f",
1962                    elapsed * .000000001, n, perLoop * .000001,
1963                    mean * .001,
1964                    stddev * .001,
1965                    minimum * .001,
1966                    maximum * .001,
1967                    mean / perLoop100,
1968                    stddev / perLoop100,
1969                    minimum / perLoop100,
1970                    maximum / perLoop100);
1971        }
1972    }
1973#endif
1974};
1975
1976bool AudioFlinger::MixerThread::threadLoop()
1977{
1978    Vector< sp<Track> > tracksToRemove;
1979    mixer_state mixerStatus = MIXER_IDLE;
1980    nsecs_t standbyTime = systemTime();
1981    size_t mixBufferSize = mFrameCount * mFrameSize;
1982    // FIXME: Relaxed timing because of a certain device that can't meet latency
1983    // Should be reduced to 2x after the vendor fixes the driver issue
1984    // increase threshold again due to low power audio mode. The way this warning threshold is
1985    // calculated and its usefulness should be reconsidered anyway.
1986    nsecs_t maxPeriod = seconds(mFrameCount) / mSampleRate * 15;
1987    nsecs_t lastWarning = 0;
1988    bool longStandbyExit = false;
1989    uint32_t activeSleepTime = activeSleepTimeUs();
1990    uint32_t idleSleepTime = idleSleepTimeUs();
1991    uint32_t sleepTime = idleSleepTime;
1992    uint32_t sleepTimeShift = 0;
1993    Vector< sp<EffectChain> > effectChains;
1994    CpuStats cpuStats;
1995
1996    acquireWakeLock();
1997
1998    while (!exitPending())
1999    {
2000        cpuStats.sample();
2001        processConfigEvents();
2002
2003        mixerStatus = MIXER_IDLE;
2004        { // scope for mLock
2005
2006            Mutex::Autolock _l(mLock);
2007
2008            if (checkForNewParameters_l()) {
2009                mixBufferSize = mFrameCount * mFrameSize;
2010                // FIXME: Relaxed timing because of a certain device that can't meet latency
2011                // Should be reduced to 2x after the vendor fixes the driver issue
2012                // increase threshold again due to low power audio mode. The way this warning
2013                // threshold is calculated and its usefulness should be reconsidered anyway.
2014                maxPeriod = seconds(mFrameCount) / mSampleRate * 15;
2015                activeSleepTime = activeSleepTimeUs();
2016                idleSleepTime = idleSleepTimeUs();
2017            }
2018
2019            const SortedVector< wp<Track> >& activeTracks = mActiveTracks;
2020
2021            // put audio hardware into standby after short delay
2022            if (CC_UNLIKELY((!activeTracks.size() && systemTime() > standbyTime) ||
2023                        mSuspended)) {
2024                if (!mStandby) {
2025                    ALOGV("Audio hardware entering standby, mixer %p, mSuspended %d", this, mSuspended);
2026                    mOutput->stream->common.standby(&mOutput->stream->common);
2027                    mStandby = true;
2028                    mBytesWritten = 0;
2029                }
2030
2031                if (!activeTracks.size() && mConfigEvents.isEmpty()) {
2032                    // we're about to wait, flush the binder command buffer
2033                    IPCThreadState::self()->flushCommands();
2034
2035                    if (exitPending()) break;
2036
2037                    releaseWakeLock_l();
2038                    // wait until we have something to do...
2039                    ALOGV("MixerThread %p TID %d going to sleep", this, gettid());
2040                    mWaitWorkCV.wait(mLock);
2041                    ALOGV("MixerThread %p TID %d waking up", this, gettid());
2042                    acquireWakeLock_l();
2043
2044                    mPrevMixerStatus = MIXER_IDLE;
2045                    if (!mMasterMute) {
2046                        char value[PROPERTY_VALUE_MAX];
2047                        property_get("ro.audio.silent", value, "0");
2048                        if (atoi(value)) {
2049                            ALOGD("Silence is golden");
2050                            setMasterMute_l(true);
2051                        }
2052                    }
2053
2054                    standbyTime = systemTime() + mStandbyTimeInNsecs;
2055                    sleepTime = idleSleepTime;
2056                    sleepTimeShift = 0;
2057                    continue;
2058                }
2059            }
2060
2061            mixerStatus = prepareTracks_l(activeTracks, &tracksToRemove);
2062
2063            // prevent any changes in effect chain list and in each effect chain
2064            // during mixing and effect process as the audio buffers could be deleted
2065            // or modified if an effect is created or deleted
2066            lockEffectChains_l(effectChains);
2067        }
2068
2069        if (CC_LIKELY(mixerStatus == MIXER_TRACKS_READY)) {
2070            // obtain the presentation timestamp of the next output buffer
2071            int64_t pts;
2072            status_t status = INVALID_OPERATION;
2073
2074            if (NULL != mOutput->stream->get_next_write_timestamp) {
2075                status = mOutput->stream->get_next_write_timestamp(
2076                        mOutput->stream, &pts);
2077            }
2078
2079            if (status != NO_ERROR) {
2080                pts = AudioBufferProvider::kInvalidPTS;
2081            }
2082
2083            // mix buffers...
2084            mAudioMixer->process(pts);
2085            // increase sleep time progressively when application underrun condition clears.
2086            // Only increase sleep time if the mixer is ready for two consecutive times to avoid
2087            // that a steady state of alternating ready/not ready conditions keeps the sleep time
2088            // such that we would underrun the audio HAL.
2089            if ((sleepTime == 0) && (sleepTimeShift > 0)) {
2090                sleepTimeShift--;
2091            }
2092            sleepTime = 0;
2093            standbyTime = systemTime() + mStandbyTimeInNsecs;
2094            //TODO: delay standby when effects have a tail
2095        } else {
2096            // If no tracks are ready, sleep once for the duration of an output
2097            // buffer size, then write 0s to the output
2098            if (sleepTime == 0) {
2099                if (mixerStatus == MIXER_TRACKS_ENABLED) {
2100                    sleepTime = activeSleepTime >> sleepTimeShift;
2101                    if (sleepTime < kMinThreadSleepTimeUs) {
2102                        sleepTime = kMinThreadSleepTimeUs;
2103                    }
2104                    // reduce sleep time in case of consecutive application underruns to avoid
2105                    // starving the audio HAL. As activeSleepTimeUs() is larger than a buffer
2106                    // duration we would end up writing less data than needed by the audio HAL if
2107                    // the condition persists.
2108                    if (sleepTimeShift < kMaxThreadSleepTimeShift) {
2109                        sleepTimeShift++;
2110                    }
2111                } else {
2112                    sleepTime = idleSleepTime;
2113                }
2114            } else if (mBytesWritten != 0 ||
2115                       (mixerStatus == MIXER_TRACKS_ENABLED && longStandbyExit)) {
2116                memset (mMixBuffer, 0, mixBufferSize);
2117                sleepTime = 0;
2118                ALOGV_IF((mBytesWritten == 0 && (mixerStatus == MIXER_TRACKS_ENABLED && longStandbyExit)), "anticipated start");
2119            }
2120            // TODO add standby time extension fct of effect tail
2121        }
2122
2123        if (mSuspended) {
2124            sleepTime = suspendSleepTimeUs();
2125        }
2126        // sleepTime == 0 means we must write to audio hardware
2127        if (sleepTime == 0) {
2128            for (size_t i = 0; i < effectChains.size(); i ++) {
2129                effectChains[i]->process_l();
2130            }
2131            // enable changes in effect chain
2132            unlockEffectChains(effectChains);
2133            mLastWriteTime = systemTime();
2134            mInWrite = true;
2135            mBytesWritten += mixBufferSize;
2136
2137            int bytesWritten = (int)mOutput->stream->write(mOutput->stream, mMixBuffer, mixBufferSize);
2138            if (bytesWritten < 0) mBytesWritten -= mixBufferSize;
2139            mNumWrites++;
2140            mInWrite = false;
2141            nsecs_t now = systemTime();
2142            nsecs_t delta = now - mLastWriteTime;
2143            if (!mStandby && delta > maxPeriod) {
2144                mNumDelayedWrites++;
2145                if ((now - lastWarning) > kWarningThrottleNs) {
2146                    ALOGW("write blocked for %llu msecs, %d delayed writes, thread %p",
2147                            ns2ms(delta), mNumDelayedWrites, this);
2148                    lastWarning = now;
2149                }
2150                if (mStandby) {
2151                    longStandbyExit = true;
2152                }
2153            }
2154            mStandby = false;
2155        } else {
2156            // enable changes in effect chain
2157            unlockEffectChains(effectChains);
2158            usleep(sleepTime);
2159        }
2160
2161        // finally let go of all our tracks, without the lock held
2162        // since we can't guarantee the destructors won't acquire that
2163        // same lock.
2164        tracksToRemove.clear();
2165
2166        // Effect chains will be actually deleted here if they were removed from
2167        // mEffectChains list during mixing or effects processing
2168        effectChains.clear();
2169    }
2170
2171    if (!mStandby) {
2172        mOutput->stream->common.standby(&mOutput->stream->common);
2173    }
2174
2175    releaseWakeLock();
2176
2177    ALOGV("MixerThread %p exiting", this);
2178    return false;
2179}
2180
2181// prepareTracks_l() must be called with ThreadBase::mLock held
2182AudioFlinger::PlaybackThread::mixer_state AudioFlinger::MixerThread::prepareTracks_l(
2183        const SortedVector< wp<Track> >& activeTracks, Vector< sp<Track> > *tracksToRemove)
2184{
2185
2186    mixer_state mixerStatus = MIXER_IDLE;
2187    // find out which tracks need to be processed
2188    size_t count = activeTracks.size();
2189    size_t mixedTracks = 0;
2190    size_t tracksWithEffect = 0;
2191
2192    float masterVolume = mMasterVolume;
2193    bool  masterMute = mMasterMute;
2194
2195    if (masterMute) {
2196        masterVolume = 0;
2197    }
2198    // Delegate master volume control to effect in output mix effect chain if needed
2199    sp<EffectChain> chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
2200    if (chain != 0) {
2201        uint32_t v = (uint32_t)(masterVolume * (1 << 24));
2202        chain->setVolume_l(&v, &v);
2203        masterVolume = (float)((v + (1 << 23)) >> 24);
2204        chain.clear();
2205    }
2206
2207    for (size_t i=0 ; i<count ; i++) {
2208        sp<Track> t = activeTracks[i].promote();
2209        if (t == 0) continue;
2210
2211        // this const just means the local variable doesn't change
2212        Track* const track = t.get();
2213        audio_track_cblk_t* cblk = track->cblk();
2214
2215        // The first time a track is added we wait
2216        // for all its buffers to be filled before processing it
2217        int name = track->name();
2218        // make sure that we have enough frames to mix one full buffer.
2219        // enforce this condition only once to enable draining the buffer in case the client
2220        // app does not call stop() and relies on underrun to stop:
2221        // hence the test on (mPrevMixerStatus == MIXER_TRACKS_READY) meaning the track was mixed
2222        // during last round
2223        uint32_t minFrames = 1;
2224        if (!track->isStopped() && !track->isPausing() &&
2225                (mPrevMixerStatus == MIXER_TRACKS_READY)) {
2226            if (t->sampleRate() == (int)mSampleRate) {
2227                minFrames = mFrameCount;
2228            } else {
2229                // +1 for rounding and +1 for additional sample needed for interpolation
2230                minFrames = (mFrameCount * t->sampleRate()) / mSampleRate + 1 + 1;
2231                // add frames already consumed but not yet released by the resampler
2232                // because cblk->framesReady() will  include these frames
2233                minFrames += mAudioMixer->getUnreleasedFrames(track->name());
2234                // the minimum track buffer size is normally twice the number of frames necessary
2235                // to fill one buffer and the resampler should not leave more than one buffer worth
2236                // of unreleased frames after each pass, but just in case...
2237                ALOG_ASSERT(minFrames <= cblk->frameCount);
2238            }
2239        }
2240        if ((track->framesReady() >= minFrames) && track->isReady() &&
2241                !track->isPaused() && !track->isTerminated())
2242        {
2243            //ALOGV("track %d u=%08x, s=%08x [OK] on thread %p", name, cblk->user, cblk->server, this);
2244
2245            mixedTracks++;
2246
2247            // track->mainBuffer() != mMixBuffer means there is an effect chain
2248            // connected to the track
2249            chain.clear();
2250            if (track->mainBuffer() != mMixBuffer) {
2251                chain = getEffectChain_l(track->sessionId());
2252                // Delegate volume control to effect in track effect chain if needed
2253                if (chain != 0) {
2254                    tracksWithEffect++;
2255                } else {
2256                    ALOGW("prepareTracks_l(): track %d attached to effect but no chain found on session %d",
2257                            name, track->sessionId());
2258                }
2259            }
2260
2261
2262            int param = AudioMixer::VOLUME;
2263            if (track->mFillingUpStatus == Track::FS_FILLED) {
2264                // no ramp for the first volume setting
2265                track->mFillingUpStatus = Track::FS_ACTIVE;
2266                if (track->mState == TrackBase::RESUMING) {
2267                    track->mState = TrackBase::ACTIVE;
2268                    param = AudioMixer::RAMP_VOLUME;
2269                }
2270                mAudioMixer->setParameter(name, AudioMixer::RESAMPLE, AudioMixer::RESET, NULL);
2271            } else if (cblk->server != 0) {
2272                // If the track is stopped before the first frame was mixed,
2273                // do not apply ramp
2274                param = AudioMixer::RAMP_VOLUME;
2275            }
2276
2277            // compute volume for this track
2278            uint32_t vl, vr, va;
2279            if (track->isMuted() || track->isPausing() ||
2280                mStreamTypes[track->streamType()].mute) {
2281                vl = vr = va = 0;
2282                if (track->isPausing()) {
2283                    track->setPaused();
2284                }
2285            } else {
2286
2287                // read original volumes with volume control
2288                float typeVolume = mStreamTypes[track->streamType()].volume;
2289                float v = masterVolume * typeVolume;
2290                uint32_t vlr = cblk->getVolumeLR();
2291                vl = vlr & 0xFFFF;
2292                vr = vlr >> 16;
2293                // track volumes come from shared memory, so can't be trusted and must be clamped
2294                if (vl > MAX_GAIN_INT) {
2295                    ALOGV("Track left volume out of range: %04X", vl);
2296                    vl = MAX_GAIN_INT;
2297                }
2298                if (vr > MAX_GAIN_INT) {
2299                    ALOGV("Track right volume out of range: %04X", vr);
2300                    vr = MAX_GAIN_INT;
2301                }
2302                // now apply the master volume and stream type volume
2303                vl = (uint32_t)(v * vl) << 12;
2304                vr = (uint32_t)(v * vr) << 12;
2305                // assuming master volume and stream type volume each go up to 1.0,
2306                // vl and vr are now in 8.24 format
2307
2308                uint16_t sendLevel = cblk->getSendLevel_U4_12();
2309                // send level comes from shared memory and so may be corrupt
2310                if (sendLevel > MAX_GAIN_INT) {
2311                    ALOGV("Track send level out of range: %04X", sendLevel);
2312                    sendLevel = MAX_GAIN_INT;
2313                }
2314                va = (uint32_t)(v * sendLevel);
2315            }
2316            // Delegate volume control to effect in track effect chain if needed
2317            if (chain != 0 && chain->setVolume_l(&vl, &vr)) {
2318                // Do not ramp volume if volume is controlled by effect
2319                param = AudioMixer::VOLUME;
2320                track->mHasVolumeController = true;
2321            } else {
2322                // force no volume ramp when volume controller was just disabled or removed
2323                // from effect chain to avoid volume spike
2324                if (track->mHasVolumeController) {
2325                    param = AudioMixer::VOLUME;
2326                }
2327                track->mHasVolumeController = false;
2328            }
2329
2330            // Convert volumes from 8.24 to 4.12 format
2331            // This additional clamping is needed in case chain->setVolume_l() overshot
2332            vl = (vl + (1 << 11)) >> 12;
2333            if (vl > MAX_GAIN_INT) vl = MAX_GAIN_INT;
2334            vr = (vr + (1 << 11)) >> 12;
2335            if (vr > MAX_GAIN_INT) vr = MAX_GAIN_INT;
2336
2337            if (va > MAX_GAIN_INT) va = MAX_GAIN_INT;   // va is uint32_t, so no need to check for -
2338
2339            // XXX: these things DON'T need to be done each time
2340            mAudioMixer->setBufferProvider(name, track);
2341            mAudioMixer->enable(name);
2342
2343            mAudioMixer->setParameter(name, param, AudioMixer::VOLUME0, (void *)vl);
2344            mAudioMixer->setParameter(name, param, AudioMixer::VOLUME1, (void *)vr);
2345            mAudioMixer->setParameter(name, param, AudioMixer::AUXLEVEL, (void *)va);
2346            mAudioMixer->setParameter(
2347                name,
2348                AudioMixer::TRACK,
2349                AudioMixer::FORMAT, (void *)track->format());
2350            mAudioMixer->setParameter(
2351                name,
2352                AudioMixer::TRACK,
2353                AudioMixer::CHANNEL_MASK, (void *)track->channelMask());
2354            mAudioMixer->setParameter(
2355                name,
2356                AudioMixer::RESAMPLE,
2357                AudioMixer::SAMPLE_RATE,
2358                (void *)(cblk->sampleRate));
2359            mAudioMixer->setParameter(
2360                name,
2361                AudioMixer::TRACK,
2362                AudioMixer::MAIN_BUFFER, (void *)track->mainBuffer());
2363            mAudioMixer->setParameter(
2364                name,
2365                AudioMixer::TRACK,
2366                AudioMixer::AUX_BUFFER, (void *)track->auxBuffer());
2367
2368            // reset retry count
2369            track->mRetryCount = kMaxTrackRetries;
2370            // If one track is ready, set the mixer ready if:
2371            //  - the mixer was not ready during previous round OR
2372            //  - no other track is not ready
2373            if (mPrevMixerStatus != MIXER_TRACKS_READY ||
2374                    mixerStatus != MIXER_TRACKS_ENABLED) {
2375                mixerStatus = MIXER_TRACKS_READY;
2376            }
2377        } else {
2378            //ALOGV("track %d u=%08x, s=%08x [NOT READY] on thread %p", name, cblk->user, cblk->server, this);
2379            if (track->isStopped()) {
2380                track->reset();
2381            }
2382            if (track->isTerminated() || track->isStopped() || track->isPaused()) {
2383                // We have consumed all the buffers of this track.
2384                // Remove it from the list of active tracks.
2385                tracksToRemove->add(track);
2386            } else {
2387                // No buffers for this track. Give it a few chances to
2388                // fill a buffer, then remove it from active list.
2389                if (--(track->mRetryCount) <= 0) {
2390                    ALOGV("BUFFER TIMEOUT: remove(%d) from active list on thread %p", name, this);
2391                    tracksToRemove->add(track);
2392                    // indicate to client process that the track was disabled because of underrun
2393                    android_atomic_or(CBLK_DISABLED_ON, &cblk->flags);
2394                // If one track is not ready, mark the mixer also not ready if:
2395                //  - the mixer was ready during previous round OR
2396                //  - no other track is ready
2397                } else if (mPrevMixerStatus == MIXER_TRACKS_READY ||
2398                                mixerStatus != MIXER_TRACKS_READY) {
2399                    mixerStatus = MIXER_TRACKS_ENABLED;
2400                }
2401            }
2402            mAudioMixer->disable(name);
2403        }
2404    }
2405
2406    // remove all the tracks that need to be...
2407    count = tracksToRemove->size();
2408    if (CC_UNLIKELY(count)) {
2409        for (size_t i=0 ; i<count ; i++) {
2410            const sp<Track>& track = tracksToRemove->itemAt(i);
2411            mActiveTracks.remove(track);
2412            if (track->mainBuffer() != mMixBuffer) {
2413                chain = getEffectChain_l(track->sessionId());
2414                if (chain != 0) {
2415                    ALOGV("stopping track on chain %p for session Id: %d", chain.get(), track->sessionId());
2416                    chain->decActiveTrackCnt();
2417                }
2418            }
2419            if (track->isTerminated()) {
2420                removeTrack_l(track);
2421            }
2422        }
2423    }
2424
2425    // mix buffer must be cleared if all tracks are connected to an
2426    // effect chain as in this case the mixer will not write to
2427    // mix buffer and track effects will accumulate into it
2428    if (mixedTracks != 0 && mixedTracks == tracksWithEffect) {
2429        memset(mMixBuffer, 0, mFrameCount * mChannelCount * sizeof(int16_t));
2430    }
2431
2432    mPrevMixerStatus = mixerStatus;
2433    return mixerStatus;
2434}
2435
2436void AudioFlinger::MixerThread::invalidateTracks(audio_stream_type_t streamType)
2437{
2438    ALOGV ("MixerThread::invalidateTracks() mixer %p, streamType %d, mTracks.size %d",
2439            this,  streamType, mTracks.size());
2440    Mutex::Autolock _l(mLock);
2441
2442    size_t size = mTracks.size();
2443    for (size_t i = 0; i < size; i++) {
2444        sp<Track> t = mTracks[i];
2445        if (t->streamType() == streamType) {
2446            android_atomic_or(CBLK_INVALID_ON, &t->mCblk->flags);
2447            t->mCblk->cv.signal();
2448        }
2449    }
2450}
2451
2452void AudioFlinger::PlaybackThread::setStreamValid(audio_stream_type_t streamType, bool valid)
2453{
2454    ALOGV ("PlaybackThread::setStreamValid() thread %p, streamType %d, valid %d",
2455            this,  streamType, valid);
2456    Mutex::Autolock _l(mLock);
2457
2458    mStreamTypes[streamType].valid = valid;
2459}
2460
2461// getTrackName_l() must be called with ThreadBase::mLock held
2462int AudioFlinger::MixerThread::getTrackName_l()
2463{
2464    return mAudioMixer->getTrackName();
2465}
2466
2467// deleteTrackName_l() must be called with ThreadBase::mLock held
2468void AudioFlinger::MixerThread::deleteTrackName_l(int name)
2469{
2470    ALOGV("remove track (%d) and delete from mixer", name);
2471    mAudioMixer->deleteTrackName(name);
2472}
2473
2474// checkForNewParameters_l() must be called with ThreadBase::mLock held
2475bool AudioFlinger::MixerThread::checkForNewParameters_l()
2476{
2477    bool reconfig = false;
2478
2479    while (!mNewParameters.isEmpty()) {
2480        status_t status = NO_ERROR;
2481        String8 keyValuePair = mNewParameters[0];
2482        AudioParameter param = AudioParameter(keyValuePair);
2483        int value;
2484
2485        if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) {
2486            reconfig = true;
2487        }
2488        if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) {
2489            if ((audio_format_t) value != AUDIO_FORMAT_PCM_16_BIT) {
2490                status = BAD_VALUE;
2491            } else {
2492                reconfig = true;
2493            }
2494        }
2495        if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) {
2496            if (value != AUDIO_CHANNEL_OUT_STEREO) {
2497                status = BAD_VALUE;
2498            } else {
2499                reconfig = true;
2500            }
2501        }
2502        if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) {
2503            // do not accept frame count changes if tracks are open as the track buffer
2504            // size depends on frame count and correct behavior would not be guaranteed
2505            // if frame count is changed after track creation
2506            if (!mTracks.isEmpty()) {
2507                status = INVALID_OPERATION;
2508            } else {
2509                reconfig = true;
2510            }
2511        }
2512        if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) {
2513            // when changing the audio output device, call addBatteryData to notify
2514            // the change
2515            if ((int)mDevice != value) {
2516                uint32_t params = 0;
2517                // check whether speaker is on
2518                if (value & AUDIO_DEVICE_OUT_SPEAKER) {
2519                    params |= IMediaPlayerService::kBatteryDataSpeakerOn;
2520                }
2521
2522                int deviceWithoutSpeaker
2523                    = AUDIO_DEVICE_OUT_ALL & ~AUDIO_DEVICE_OUT_SPEAKER;
2524                // check if any other device (except speaker) is on
2525                if (value & deviceWithoutSpeaker ) {
2526                    params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn;
2527                }
2528
2529                if (params != 0) {
2530                    addBatteryData(params);
2531                }
2532            }
2533
2534            // forward device change to effects that have requested to be
2535            // aware of attached audio device.
2536            mDevice = (uint32_t)value;
2537            for (size_t i = 0; i < mEffectChains.size(); i++) {
2538                mEffectChains[i]->setDevice_l(mDevice);
2539            }
2540        }
2541
2542        if (status == NO_ERROR) {
2543            status = mOutput->stream->common.set_parameters(&mOutput->stream->common,
2544                                                    keyValuePair.string());
2545            if (!mStandby && status == INVALID_OPERATION) {
2546               mOutput->stream->common.standby(&mOutput->stream->common);
2547               mStandby = true;
2548               mBytesWritten = 0;
2549               status = mOutput->stream->common.set_parameters(&mOutput->stream->common,
2550                                                       keyValuePair.string());
2551            }
2552            if (status == NO_ERROR && reconfig) {
2553                delete mAudioMixer;
2554                // for safety in case readOutputParameters() accesses mAudioMixer (it doesn't)
2555                mAudioMixer = NULL;
2556                readOutputParameters();
2557                mAudioMixer = new AudioMixer(mFrameCount, mSampleRate);
2558                for (size_t i = 0; i < mTracks.size() ; i++) {
2559                    int name = getTrackName_l();
2560                    if (name < 0) break;
2561                    mTracks[i]->mName = name;
2562                    // limit track sample rate to 2 x new output sample rate
2563                    if (mTracks[i]->mCblk->sampleRate > 2 * sampleRate()) {
2564                        mTracks[i]->mCblk->sampleRate = 2 * sampleRate();
2565                    }
2566                }
2567                sendConfigEvent_l(AudioSystem::OUTPUT_CONFIG_CHANGED);
2568            }
2569        }
2570
2571        mNewParameters.removeAt(0);
2572
2573        mParamStatus = status;
2574        mParamCond.signal();
2575        // wait for condition with time out in case the thread calling ThreadBase::setParameters()
2576        // already timed out waiting for the status and will never signal the condition.
2577        mWaitWorkCV.waitRelative(mLock, kSetParametersTimeoutNs);
2578    }
2579    return reconfig;
2580}
2581
2582status_t AudioFlinger::MixerThread::dumpInternals(int fd, const Vector<String16>& args)
2583{
2584    const size_t SIZE = 256;
2585    char buffer[SIZE];
2586    String8 result;
2587
2588    PlaybackThread::dumpInternals(fd, args);
2589
2590    snprintf(buffer, SIZE, "AudioMixer tracks: %08x\n", mAudioMixer->trackNames());
2591    result.append(buffer);
2592    write(fd, result.string(), result.size());
2593    return NO_ERROR;
2594}
2595
2596uint32_t AudioFlinger::MixerThread::idleSleepTimeUs()
2597{
2598    return (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000) / 2;
2599}
2600
2601uint32_t AudioFlinger::MixerThread::suspendSleepTimeUs()
2602{
2603    return (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000);
2604}
2605
2606// ----------------------------------------------------------------------------
2607AudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger,
2608        AudioStreamOut* output, audio_io_handle_t id, uint32_t device)
2609    :   PlaybackThread(audioFlinger, output, id, device, DIRECT)
2610        // mLeftVolFloat, mRightVolFloat
2611        // mLeftVolShort, mRightVolShort
2612{
2613}
2614
2615AudioFlinger::DirectOutputThread::~DirectOutputThread()
2616{
2617}
2618
2619void AudioFlinger::DirectOutputThread::applyVolume(uint16_t leftVol, uint16_t rightVol, bool ramp)
2620{
2621    // Do not apply volume on compressed audio
2622    if (!audio_is_linear_pcm(mFormat)) {
2623        return;
2624    }
2625
2626    // convert to signed 16 bit before volume calculation
2627    if (mFormat == AUDIO_FORMAT_PCM_8_BIT) {
2628        size_t count = mFrameCount * mChannelCount;
2629        uint8_t *src = (uint8_t *)mMixBuffer + count-1;
2630        int16_t *dst = mMixBuffer + count-1;
2631        while(count--) {
2632            *dst-- = (int16_t)(*src--^0x80) << 8;
2633        }
2634    }
2635
2636    size_t frameCount = mFrameCount;
2637    int16_t *out = mMixBuffer;
2638    if (ramp) {
2639        if (mChannelCount == 1) {
2640            int32_t d = ((int32_t)leftVol - (int32_t)mLeftVolShort) << 16;
2641            int32_t vlInc = d / (int32_t)frameCount;
2642            int32_t vl = ((int32_t)mLeftVolShort << 16);
2643            do {
2644                out[0] = clamp16(mul(out[0], vl >> 16) >> 12);
2645                out++;
2646                vl += vlInc;
2647            } while (--frameCount);
2648
2649        } else {
2650            int32_t d = ((int32_t)leftVol - (int32_t)mLeftVolShort) << 16;
2651            int32_t vlInc = d / (int32_t)frameCount;
2652            d = ((int32_t)rightVol - (int32_t)mRightVolShort) << 16;
2653            int32_t vrInc = d / (int32_t)frameCount;
2654            int32_t vl = ((int32_t)mLeftVolShort << 16);
2655            int32_t vr = ((int32_t)mRightVolShort << 16);
2656            do {
2657                out[0] = clamp16(mul(out[0], vl >> 16) >> 12);
2658                out[1] = clamp16(mul(out[1], vr >> 16) >> 12);
2659                out += 2;
2660                vl += vlInc;
2661                vr += vrInc;
2662            } while (--frameCount);
2663        }
2664    } else {
2665        if (mChannelCount == 1) {
2666            do {
2667                out[0] = clamp16(mul(out[0], leftVol) >> 12);
2668                out++;
2669            } while (--frameCount);
2670        } else {
2671            do {
2672                out[0] = clamp16(mul(out[0], leftVol) >> 12);
2673                out[1] = clamp16(mul(out[1], rightVol) >> 12);
2674                out += 2;
2675            } while (--frameCount);
2676        }
2677    }
2678
2679    // convert back to unsigned 8 bit after volume calculation
2680    if (mFormat == AUDIO_FORMAT_PCM_8_BIT) {
2681        size_t count = mFrameCount * mChannelCount;
2682        int16_t *src = mMixBuffer;
2683        uint8_t *dst = (uint8_t *)mMixBuffer;
2684        while(count--) {
2685            *dst++ = (uint8_t)(((int32_t)*src++ + (1<<7)) >> 8)^0x80;
2686        }
2687    }
2688
2689    mLeftVolShort = leftVol;
2690    mRightVolShort = rightVol;
2691}
2692
2693bool AudioFlinger::DirectOutputThread::threadLoop()
2694{
2695    mixer_state mixerStatus = MIXER_IDLE;
2696    sp<Track> trackToRemove;
2697    sp<Track> activeTrack;
2698    nsecs_t standbyTime = systemTime();
2699    size_t mixBufferSize = mFrameCount*mFrameSize;
2700    uint32_t activeSleepTime = activeSleepTimeUs();
2701    uint32_t idleSleepTime = idleSleepTimeUs();
2702    uint32_t sleepTime = idleSleepTime;
2703    // use shorter standby delay as on normal output to release
2704    // hardware resources as soon as possible
2705    nsecs_t standbyDelay = microseconds(activeSleepTime*2);
2706
2707    acquireWakeLock();
2708
2709    while (!exitPending())
2710    {
2711        bool rampVolume;
2712        uint16_t leftVol;
2713        uint16_t rightVol;
2714        Vector< sp<EffectChain> > effectChains;
2715
2716        processConfigEvents();
2717
2718        mixerStatus = MIXER_IDLE;
2719
2720        { // scope for the mLock
2721
2722            Mutex::Autolock _l(mLock);
2723
2724            if (checkForNewParameters_l()) {
2725                mixBufferSize = mFrameCount*mFrameSize;
2726                activeSleepTime = activeSleepTimeUs();
2727                idleSleepTime = idleSleepTimeUs();
2728                standbyDelay = microseconds(activeSleepTime*2);
2729            }
2730
2731            // put audio hardware into standby after short delay
2732            if (CC_UNLIKELY((!mActiveTracks.size() && systemTime() > standbyTime) ||
2733                        mSuspended)) {
2734                // wait until we have something to do...
2735                if (!mStandby) {
2736                    ALOGV("Audio hardware entering standby, mixer %p", this);
2737                    mOutput->stream->common.standby(&mOutput->stream->common);
2738                    mStandby = true;
2739                    mBytesWritten = 0;
2740                }
2741
2742                if (!mActiveTracks.size() && mConfigEvents.isEmpty()) {
2743                    // we're about to wait, flush the binder command buffer
2744                    IPCThreadState::self()->flushCommands();
2745
2746                    if (exitPending()) break;
2747
2748                    releaseWakeLock_l();
2749                    ALOGV("DirectOutputThread %p TID %d going to sleep", this, gettid());
2750                    mWaitWorkCV.wait(mLock);
2751                    ALOGV("DirectOutputThread %p TID %d waking up in active mode", this, gettid());
2752                    acquireWakeLock_l();
2753
2754                    if (!mMasterMute) {
2755                        char value[PROPERTY_VALUE_MAX];
2756                        property_get("ro.audio.silent", value, "0");
2757                        if (atoi(value)) {
2758                            ALOGD("Silence is golden");
2759                            setMasterMute_l(true);
2760                        }
2761                    }
2762
2763                    standbyTime = systemTime() + standbyDelay;
2764                    sleepTime = idleSleepTime;
2765                    continue;
2766                }
2767            }
2768
2769            effectChains = mEffectChains;
2770
2771            // find out which tracks need to be processed
2772            if (mActiveTracks.size() != 0) {
2773                sp<Track> t = mActiveTracks[0].promote();
2774                if (t == 0) continue;
2775
2776                Track* const track = t.get();
2777                audio_track_cblk_t* cblk = track->cblk();
2778
2779                // The first time a track is added we wait
2780                // for all its buffers to be filled before processing it
2781                if (cblk->framesReady() && track->isReady() &&
2782                        !track->isPaused() && !track->isTerminated())
2783                {
2784                    //ALOGV("track %d u=%08x, s=%08x [OK]", track->name(), cblk->user, cblk->server);
2785
2786                    if (track->mFillingUpStatus == Track::FS_FILLED) {
2787                        track->mFillingUpStatus = Track::FS_ACTIVE;
2788                        mLeftVolFloat = mRightVolFloat = 0;
2789                        mLeftVolShort = mRightVolShort = 0;
2790                        if (track->mState == TrackBase::RESUMING) {
2791                            track->mState = TrackBase::ACTIVE;
2792                            rampVolume = true;
2793                        }
2794                    } else if (cblk->server != 0) {
2795                        // If the track is stopped before the first frame was mixed,
2796                        // do not apply ramp
2797                        rampVolume = true;
2798                    }
2799                    // compute volume for this track
2800                    float left, right;
2801                    if (track->isMuted() || mMasterMute || track->isPausing() ||
2802                        mStreamTypes[track->streamType()].mute) {
2803                        left = right = 0;
2804                        if (track->isPausing()) {
2805                            track->setPaused();
2806                        }
2807                    } else {
2808                        float typeVolume = mStreamTypes[track->streamType()].volume;
2809                        float v = mMasterVolume * typeVolume;
2810                        uint32_t vlr = cblk->getVolumeLR();
2811                        float v_clamped = v * (vlr & 0xFFFF);
2812                        if (v_clamped > MAX_GAIN) v_clamped = MAX_GAIN;
2813                        left = v_clamped/MAX_GAIN;
2814                        v_clamped = v * (vlr >> 16);
2815                        if (v_clamped > MAX_GAIN) v_clamped = MAX_GAIN;
2816                        right = v_clamped/MAX_GAIN;
2817                    }
2818
2819                    if (left != mLeftVolFloat || right != mRightVolFloat) {
2820                        mLeftVolFloat = left;
2821                        mRightVolFloat = right;
2822
2823                        // If audio HAL implements volume control,
2824                        // force software volume to nominal value
2825                        if (mOutput->stream->set_volume(mOutput->stream, left, right) == NO_ERROR) {
2826                            left = 1.0f;
2827                            right = 1.0f;
2828                        }
2829
2830                        // Convert volumes from float to 8.24
2831                        uint32_t vl = (uint32_t)(left * (1 << 24));
2832                        uint32_t vr = (uint32_t)(right * (1 << 24));
2833
2834                        // Delegate volume control to effect in track effect chain if needed
2835                        // only one effect chain can be present on DirectOutputThread, so if
2836                        // there is one, the track is connected to it
2837                        if (!effectChains.isEmpty()) {
2838                            // Do not ramp volume if volume is controlled by effect
2839                            if(effectChains[0]->setVolume_l(&vl, &vr)) {
2840                                rampVolume = false;
2841                            }
2842                        }
2843
2844                        // Convert volumes from 8.24 to 4.12 format
2845                        uint32_t v_clamped = (vl + (1 << 11)) >> 12;
2846                        if (v_clamped > MAX_GAIN_INT) v_clamped = MAX_GAIN_INT;
2847                        leftVol = (uint16_t)v_clamped;
2848                        v_clamped = (vr + (1 << 11)) >> 12;
2849                        if (v_clamped > MAX_GAIN_INT) v_clamped = MAX_GAIN_INT;
2850                        rightVol = (uint16_t)v_clamped;
2851                    } else {
2852                        leftVol = mLeftVolShort;
2853                        rightVol = mRightVolShort;
2854                        rampVolume = false;
2855                    }
2856
2857                    // reset retry count
2858                    track->mRetryCount = kMaxTrackRetriesDirect;
2859                    activeTrack = t;
2860                    mixerStatus = MIXER_TRACKS_READY;
2861                } else {
2862                    //ALOGV("track %d u=%08x, s=%08x [NOT READY]", track->name(), cblk->user, cblk->server);
2863                    if (track->isStopped()) {
2864                        track->reset();
2865                    }
2866                    if (track->isTerminated() || track->isStopped() || track->isPaused()) {
2867                        // We have consumed all the buffers of this track.
2868                        // Remove it from the list of active tracks.
2869                        trackToRemove = track;
2870                    } else {
2871                        // No buffers for this track. Give it a few chances to
2872                        // fill a buffer, then remove it from active list.
2873                        if (--(track->mRetryCount) <= 0) {
2874                            ALOGV("BUFFER TIMEOUT: remove(%d) from active list", track->name());
2875                            trackToRemove = track;
2876                        } else {
2877                            mixerStatus = MIXER_TRACKS_ENABLED;
2878                        }
2879                    }
2880                }
2881            }
2882
2883            // remove all the tracks that need to be...
2884            if (CC_UNLIKELY(trackToRemove != 0)) {
2885                mActiveTracks.remove(trackToRemove);
2886                if (!effectChains.isEmpty()) {
2887                    ALOGV("stopping track on chain %p for session Id: %d", effectChains[0].get(),
2888                            trackToRemove->sessionId());
2889                    effectChains[0]->decActiveTrackCnt();
2890                }
2891                if (trackToRemove->isTerminated()) {
2892                    removeTrack_l(trackToRemove);
2893                }
2894            }
2895
2896            lockEffectChains_l(effectChains);
2897       }
2898
2899        if (CC_LIKELY(mixerStatus == MIXER_TRACKS_READY)) {
2900            AudioBufferProvider::Buffer buffer;
2901            size_t frameCount = mFrameCount;
2902            int8_t *curBuf = (int8_t *)mMixBuffer;
2903            // output audio to hardware
2904            while (frameCount) {
2905                buffer.frameCount = frameCount;
2906                activeTrack->getNextBuffer(&buffer,
2907                                           AudioBufferProvider::kInvalidPTS);
2908                if (CC_UNLIKELY(buffer.raw == NULL)) {
2909                    memset(curBuf, 0, frameCount * mFrameSize);
2910                    break;
2911                }
2912                memcpy(curBuf, buffer.raw, buffer.frameCount * mFrameSize);
2913                frameCount -= buffer.frameCount;
2914                curBuf += buffer.frameCount * mFrameSize;
2915                activeTrack->releaseBuffer(&buffer);
2916            }
2917            sleepTime = 0;
2918            standbyTime = systemTime() + standbyDelay;
2919        } else {
2920            if (sleepTime == 0) {
2921                if (mixerStatus == MIXER_TRACKS_ENABLED) {
2922                    sleepTime = activeSleepTime;
2923                } else {
2924                    sleepTime = idleSleepTime;
2925                }
2926            } else if (mBytesWritten != 0 && audio_is_linear_pcm(mFormat)) {
2927                memset (mMixBuffer, 0, mFrameCount * mFrameSize);
2928                sleepTime = 0;
2929            }
2930        }
2931
2932        if (mSuspended) {
2933            sleepTime = suspendSleepTimeUs();
2934        }
2935        // sleepTime == 0 means we must write to audio hardware
2936        if (sleepTime == 0) {
2937            if (mixerStatus == MIXER_TRACKS_READY) {
2938                applyVolume(leftVol, rightVol, rampVolume);
2939            }
2940            for (size_t i = 0; i < effectChains.size(); i ++) {
2941                effectChains[i]->process_l();
2942            }
2943            unlockEffectChains(effectChains);
2944
2945            mLastWriteTime = systemTime();
2946            mInWrite = true;
2947            mBytesWritten += mixBufferSize;
2948            int bytesWritten = (int)mOutput->stream->write(mOutput->stream, mMixBuffer, mixBufferSize);
2949            if (bytesWritten < 0) mBytesWritten -= mixBufferSize;
2950            mNumWrites++;
2951            mInWrite = false;
2952            mStandby = false;
2953        } else {
2954            unlockEffectChains(effectChains);
2955            usleep(sleepTime);
2956        }
2957
2958        // finally let go of removed track, without the lock held
2959        // since we can't guarantee the destructors won't acquire that
2960        // same lock.
2961        trackToRemove.clear();
2962        activeTrack.clear();
2963
2964        // Effect chains will be actually deleted here if they were removed from
2965        // mEffectChains list during mixing or effects processing
2966        effectChains.clear();
2967    }
2968
2969    if (!mStandby) {
2970        mOutput->stream->common.standby(&mOutput->stream->common);
2971    }
2972
2973    releaseWakeLock();
2974
2975    ALOGV("DirectOutputThread %p exiting", this);
2976    return false;
2977}
2978
2979// getTrackName_l() must be called with ThreadBase::mLock held
2980int AudioFlinger::DirectOutputThread::getTrackName_l()
2981{
2982    return 0;
2983}
2984
2985// deleteTrackName_l() must be called with ThreadBase::mLock held
2986void AudioFlinger::DirectOutputThread::deleteTrackName_l(int name)
2987{
2988}
2989
2990// checkForNewParameters_l() must be called with ThreadBase::mLock held
2991bool AudioFlinger::DirectOutputThread::checkForNewParameters_l()
2992{
2993    bool reconfig = false;
2994
2995    while (!mNewParameters.isEmpty()) {
2996        status_t status = NO_ERROR;
2997        String8 keyValuePair = mNewParameters[0];
2998        AudioParameter param = AudioParameter(keyValuePair);
2999        int value;
3000
3001        if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) {
3002            // do not accept frame count changes if tracks are open as the track buffer
3003            // size depends on frame count and correct behavior would not be garantied
3004            // if frame count is changed after track creation
3005            if (!mTracks.isEmpty()) {
3006                status = INVALID_OPERATION;
3007            } else {
3008                reconfig = true;
3009            }
3010        }
3011        if (status == NO_ERROR) {
3012            status = mOutput->stream->common.set_parameters(&mOutput->stream->common,
3013                                                    keyValuePair.string());
3014            if (!mStandby && status == INVALID_OPERATION) {
3015               mOutput->stream->common.standby(&mOutput->stream->common);
3016               mStandby = true;
3017               mBytesWritten = 0;
3018               status = mOutput->stream->common.set_parameters(&mOutput->stream->common,
3019                                                       keyValuePair.string());
3020            }
3021            if (status == NO_ERROR && reconfig) {
3022                readOutputParameters();
3023                sendConfigEvent_l(AudioSystem::OUTPUT_CONFIG_CHANGED);
3024            }
3025        }
3026
3027        mNewParameters.removeAt(0);
3028
3029        mParamStatus = status;
3030        mParamCond.signal();
3031        // wait for condition with time out in case the thread calling ThreadBase::setParameters()
3032        // already timed out waiting for the status and will never signal the condition.
3033        mWaitWorkCV.waitRelative(mLock, kSetParametersTimeoutNs);
3034    }
3035    return reconfig;
3036}
3037
3038uint32_t AudioFlinger::DirectOutputThread::activeSleepTimeUs()
3039{
3040    uint32_t time;
3041    if (audio_is_linear_pcm(mFormat)) {
3042        time = PlaybackThread::activeSleepTimeUs();
3043    } else {
3044        time = 10000;
3045    }
3046    return time;
3047}
3048
3049uint32_t AudioFlinger::DirectOutputThread::idleSleepTimeUs()
3050{
3051    uint32_t time;
3052    if (audio_is_linear_pcm(mFormat)) {
3053        time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000) / 2;
3054    } else {
3055        time = 10000;
3056    }
3057    return time;
3058}
3059
3060uint32_t AudioFlinger::DirectOutputThread::suspendSleepTimeUs()
3061{
3062    uint32_t time;
3063    if (audio_is_linear_pcm(mFormat)) {
3064        time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000);
3065    } else {
3066        time = 10000;
3067    }
3068    return time;
3069}
3070
3071
3072// ----------------------------------------------------------------------------
3073
3074AudioFlinger::DuplicatingThread::DuplicatingThread(const sp<AudioFlinger>& audioFlinger,
3075        AudioFlinger::MixerThread* mainThread, audio_io_handle_t id)
3076    :   MixerThread(audioFlinger, mainThread->getOutput(), id, mainThread->device(), DUPLICATING),
3077        mWaitTimeMs(UINT_MAX)
3078{
3079    addOutputTrack(mainThread);
3080}
3081
3082AudioFlinger::DuplicatingThread::~DuplicatingThread()
3083{
3084    for (size_t i = 0; i < mOutputTracks.size(); i++) {
3085        mOutputTracks[i]->destroy();
3086    }
3087}
3088
3089bool AudioFlinger::DuplicatingThread::threadLoop()
3090{
3091    Vector< sp<Track> > tracksToRemove;
3092    mixer_state mixerStatus = MIXER_IDLE;
3093    nsecs_t standbyTime = systemTime();
3094    size_t mixBufferSize = mFrameCount*mFrameSize;
3095    SortedVector< sp<OutputTrack> > outputTracks;
3096    uint32_t writeFrames = 0;
3097    uint32_t activeSleepTime = activeSleepTimeUs();
3098    uint32_t idleSleepTime = idleSleepTimeUs();
3099    uint32_t sleepTime = idleSleepTime;
3100    Vector< sp<EffectChain> > effectChains;
3101
3102    acquireWakeLock();
3103
3104    while (!exitPending())
3105    {
3106        processConfigEvents();
3107
3108        mixerStatus = MIXER_IDLE;
3109        { // scope for the mLock
3110
3111            Mutex::Autolock _l(mLock);
3112
3113            if (checkForNewParameters_l()) {
3114                mixBufferSize = mFrameCount*mFrameSize;
3115                updateWaitTime();
3116                activeSleepTime = activeSleepTimeUs();
3117                idleSleepTime = idleSleepTimeUs();
3118            }
3119
3120            const SortedVector< wp<Track> >& activeTracks = mActiveTracks;
3121
3122            for (size_t i = 0; i < mOutputTracks.size(); i++) {
3123                outputTracks.add(mOutputTracks[i]);
3124            }
3125
3126            // put audio hardware into standby after short delay
3127            if (CC_UNLIKELY((!activeTracks.size() && systemTime() > standbyTime) ||
3128                         mSuspended)) {
3129                if (!mStandby) {
3130                    for (size_t i = 0; i < outputTracks.size(); i++) {
3131                        outputTracks[i]->stop();
3132                    }
3133                    mStandby = true;
3134                    mBytesWritten = 0;
3135                }
3136
3137                if (!activeTracks.size() && mConfigEvents.isEmpty()) {
3138                    // we're about to wait, flush the binder command buffer
3139                    IPCThreadState::self()->flushCommands();
3140                    outputTracks.clear();
3141
3142                    if (exitPending()) break;
3143
3144                    releaseWakeLock_l();
3145                    ALOGV("DuplicatingThread %p TID %d going to sleep", this, gettid());
3146                    mWaitWorkCV.wait(mLock);
3147                    ALOGV("DuplicatingThread %p TID %d waking up", this, gettid());
3148                    acquireWakeLock_l();
3149
3150                    mPrevMixerStatus = MIXER_IDLE;
3151                    if (!mMasterMute) {
3152                        char value[PROPERTY_VALUE_MAX];
3153                        property_get("ro.audio.silent", value, "0");
3154                        if (atoi(value)) {
3155                            ALOGD("Silence is golden");
3156                            setMasterMute_l(true);
3157                        }
3158                    }
3159
3160                    standbyTime = systemTime() + mStandbyTimeInNsecs;
3161                    sleepTime = idleSleepTime;
3162                    continue;
3163                }
3164            }
3165
3166            mixerStatus = prepareTracks_l(activeTracks, &tracksToRemove);
3167
3168            // prevent any changes in effect chain list and in each effect chain
3169            // during mixing and effect process as the audio buffers could be deleted
3170            // or modified if an effect is created or deleted
3171            lockEffectChains_l(effectChains);
3172        }
3173
3174        if (CC_LIKELY(mixerStatus == MIXER_TRACKS_READY)) {
3175            // mix buffers...
3176            if (outputsReady(outputTracks)) {
3177                mAudioMixer->process(AudioBufferProvider::kInvalidPTS);
3178            } else {
3179                memset(mMixBuffer, 0, mixBufferSize);
3180            }
3181            sleepTime = 0;
3182            writeFrames = mFrameCount;
3183        } else {
3184            if (sleepTime == 0) {
3185                if (mixerStatus == MIXER_TRACKS_ENABLED) {
3186                    sleepTime = activeSleepTime;
3187                } else {
3188                    sleepTime = idleSleepTime;
3189                }
3190            } else if (mBytesWritten != 0) {
3191                // flush remaining overflow buffers in output tracks
3192                for (size_t i = 0; i < outputTracks.size(); i++) {
3193                    if (outputTracks[i]->isActive()) {
3194                        sleepTime = 0;
3195                        writeFrames = 0;
3196                        memset(mMixBuffer, 0, mixBufferSize);
3197                        break;
3198                    }
3199                }
3200            }
3201        }
3202
3203        if (mSuspended) {
3204            sleepTime = suspendSleepTimeUs();
3205        }
3206        // sleepTime == 0 means we must write to audio hardware
3207        if (sleepTime == 0) {
3208            for (size_t i = 0; i < effectChains.size(); i ++) {
3209                effectChains[i]->process_l();
3210            }
3211            // enable changes in effect chain
3212            unlockEffectChains(effectChains);
3213
3214            standbyTime = systemTime() + mStandbyTimeInNsecs;
3215            for (size_t i = 0; i < outputTracks.size(); i++) {
3216                outputTracks[i]->write(mMixBuffer, writeFrames);
3217            }
3218            mStandby = false;
3219            mBytesWritten += mixBufferSize;
3220        } else {
3221            // enable changes in effect chain
3222            unlockEffectChains(effectChains);
3223            usleep(sleepTime);
3224        }
3225
3226        // finally let go of all our tracks, without the lock held
3227        // since we can't guarantee the destructors won't acquire that
3228        // same lock.
3229        tracksToRemove.clear();
3230        outputTracks.clear();
3231
3232        // Effect chains will be actually deleted here if they were removed from
3233        // mEffectChains list during mixing or effects processing
3234        effectChains.clear();
3235    }
3236
3237    releaseWakeLock();
3238
3239    return false;
3240}
3241
3242void AudioFlinger::DuplicatingThread::addOutputTrack(MixerThread *thread)
3243{
3244    // FIXME explain this formula
3245    int frameCount = (3 * mFrameCount * mSampleRate) / thread->sampleRate();
3246    OutputTrack *outputTrack = new OutputTrack(thread,
3247                                            this,
3248                                            mSampleRate,
3249                                            mFormat,
3250                                            mChannelMask,
3251                                            frameCount);
3252    if (outputTrack->cblk() != NULL) {
3253        thread->setStreamVolume(AUDIO_STREAM_CNT, 1.0f);
3254        mOutputTracks.add(outputTrack);
3255        ALOGV("addOutputTrack() track %p, on thread %p", outputTrack, thread);
3256        updateWaitTime();
3257    }
3258}
3259
3260void AudioFlinger::DuplicatingThread::removeOutputTrack(MixerThread *thread)
3261{
3262    Mutex::Autolock _l(mLock);
3263    for (size_t i = 0; i < mOutputTracks.size(); i++) {
3264        if (mOutputTracks[i]->thread() == thread) {
3265            mOutputTracks[i]->destroy();
3266            mOutputTracks.removeAt(i);
3267            updateWaitTime();
3268            return;
3269        }
3270    }
3271    ALOGV("removeOutputTrack(): unkonwn thread: %p", thread);
3272}
3273
3274void AudioFlinger::DuplicatingThread::updateWaitTime()
3275{
3276    mWaitTimeMs = UINT_MAX;
3277    for (size_t i = 0; i < mOutputTracks.size(); i++) {
3278        sp<ThreadBase> strong = mOutputTracks[i]->thread().promote();
3279        if (strong != 0) {
3280            uint32_t waitTimeMs = (strong->frameCount() * 2 * 1000) / strong->sampleRate();
3281            if (waitTimeMs < mWaitTimeMs) {
3282                mWaitTimeMs = waitTimeMs;
3283            }
3284        }
3285    }
3286}
3287
3288
3289bool AudioFlinger::DuplicatingThread::outputsReady(SortedVector< sp<OutputTrack> > &outputTracks)
3290{
3291    for (size_t i = 0; i < outputTracks.size(); i++) {
3292        sp <ThreadBase> thread = outputTracks[i]->thread().promote();
3293        if (thread == 0) {
3294            ALOGW("DuplicatingThread::outputsReady() could not promote thread on output track %p", outputTracks[i].get());
3295            return false;
3296        }
3297        PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
3298        if (playbackThread->standby() && !playbackThread->isSuspended()) {
3299            ALOGV("DuplicatingThread output track %p on thread %p Not Ready", outputTracks[i].get(), thread.get());
3300            return false;
3301        }
3302    }
3303    return true;
3304}
3305
3306uint32_t AudioFlinger::DuplicatingThread::activeSleepTimeUs()
3307{
3308    return (mWaitTimeMs * 1000) / 2;
3309}
3310
3311// ----------------------------------------------------------------------------
3312
3313// TrackBase constructor must be called with AudioFlinger::mLock held
3314AudioFlinger::ThreadBase::TrackBase::TrackBase(
3315            ThreadBase *thread,
3316            const sp<Client>& client,
3317            uint32_t sampleRate,
3318            audio_format_t format,
3319            uint32_t channelMask,
3320            int frameCount,
3321            const sp<IMemory>& sharedBuffer,
3322            int sessionId)
3323    :   RefBase(),
3324        mThread(thread),
3325        mClient(client),
3326        mCblk(NULL),
3327        // mBuffer
3328        // mBufferEnd
3329        mFrameCount(0),
3330        mState(IDLE),
3331        mFormat(format),
3332        mStepServerFailed(false),
3333        mSessionId(sessionId)
3334        // mChannelCount
3335        // mChannelMask
3336{
3337    ALOGV_IF(sharedBuffer != 0, "sharedBuffer: %p, size: %d", sharedBuffer->pointer(), sharedBuffer->size());
3338
3339    // ALOGD("Creating track with %d buffers @ %d bytes", bufferCount, bufferSize);
3340   size_t size = sizeof(audio_track_cblk_t);
3341   uint8_t channelCount = popcount(channelMask);
3342   size_t bufferSize = frameCount*channelCount*sizeof(int16_t);
3343   if (sharedBuffer == 0) {
3344       size += bufferSize;
3345   }
3346
3347   if (client != NULL) {
3348        mCblkMemory = client->heap()->allocate(size);
3349        if (mCblkMemory != 0) {
3350            mCblk = static_cast<audio_track_cblk_t *>(mCblkMemory->pointer());
3351            if (mCblk != NULL) { // construct the shared structure in-place.
3352                new(mCblk) audio_track_cblk_t();
3353                // clear all buffers
3354                mCblk->frameCount = frameCount;
3355                mCblk->sampleRate = sampleRate;
3356                mChannelCount = channelCount;
3357                mChannelMask = channelMask;
3358                if (sharedBuffer == 0) {
3359                    mBuffer = (char*)mCblk + sizeof(audio_track_cblk_t);
3360                    memset(mBuffer, 0, frameCount*channelCount*sizeof(int16_t));
3361                    // Force underrun condition to avoid false underrun callback until first data is
3362                    // written to buffer (other flags are cleared)
3363                    mCblk->flags = CBLK_UNDERRUN_ON;
3364                } else {
3365                    mBuffer = sharedBuffer->pointer();
3366                }
3367                mBufferEnd = (uint8_t *)mBuffer + bufferSize;
3368            }
3369        } else {
3370            ALOGE("not enough memory for AudioTrack size=%u", size);
3371            client->heap()->dump("AudioTrack");
3372            return;
3373        }
3374   } else {
3375       mCblk = (audio_track_cblk_t *)(new uint8_t[size]);
3376           // construct the shared structure in-place.
3377           new(mCblk) audio_track_cblk_t();
3378           // clear all buffers
3379           mCblk->frameCount = frameCount;
3380           mCblk->sampleRate = sampleRate;
3381           mChannelCount = channelCount;
3382           mChannelMask = channelMask;
3383           mBuffer = (char*)mCblk + sizeof(audio_track_cblk_t);
3384           memset(mBuffer, 0, frameCount*channelCount*sizeof(int16_t));
3385           // Force underrun condition to avoid false underrun callback until first data is
3386           // written to buffer (other flags are cleared)
3387           mCblk->flags = CBLK_UNDERRUN_ON;
3388           mBufferEnd = (uint8_t *)mBuffer + bufferSize;
3389   }
3390}
3391
3392AudioFlinger::ThreadBase::TrackBase::~TrackBase()
3393{
3394    if (mCblk != NULL) {
3395        if (mClient == 0) {
3396            delete mCblk;
3397        } else {
3398            mCblk->~audio_track_cblk_t();   // destroy our shared-structure.
3399        }
3400    }
3401    mCblkMemory.clear();    // free the shared memory before releasing the heap it belongs to
3402    if (mClient != 0) {
3403        // Client destructor must run with AudioFlinger mutex locked
3404        Mutex::Autolock _l(mClient->audioFlinger()->mLock);
3405        // If the client's reference count drops to zero, the associated destructor
3406        // must run with AudioFlinger lock held. Thus the explicit clear() rather than
3407        // relying on the automatic clear() at end of scope.
3408        mClient.clear();
3409    }
3410}
3411
3412void AudioFlinger::ThreadBase::TrackBase::releaseBuffer(AudioBufferProvider::Buffer* buffer)
3413{
3414    buffer->raw = NULL;
3415    mFrameCount = buffer->frameCount;
3416    step();
3417    buffer->frameCount = 0;
3418}
3419
3420bool AudioFlinger::ThreadBase::TrackBase::step() {
3421    bool result;
3422    audio_track_cblk_t* cblk = this->cblk();
3423
3424    result = cblk->stepServer(mFrameCount);
3425    if (!result) {
3426        ALOGV("stepServer failed acquiring cblk mutex");
3427        mStepServerFailed = true;
3428    }
3429    return result;
3430}
3431
3432void AudioFlinger::ThreadBase::TrackBase::reset() {
3433    audio_track_cblk_t* cblk = this->cblk();
3434
3435    cblk->user = 0;
3436    cblk->server = 0;
3437    cblk->userBase = 0;
3438    cblk->serverBase = 0;
3439    mStepServerFailed = false;
3440    ALOGV("TrackBase::reset");
3441}
3442
3443int AudioFlinger::ThreadBase::TrackBase::sampleRate() const {
3444    return (int)mCblk->sampleRate;
3445}
3446
3447void* AudioFlinger::ThreadBase::TrackBase::getBuffer(uint32_t offset, uint32_t frames) const {
3448    audio_track_cblk_t* cblk = this->cblk();
3449    size_t frameSize = cblk->frameSize;
3450    int8_t *bufferStart = (int8_t *)mBuffer + (offset-cblk->serverBase)*frameSize;
3451    int8_t *bufferEnd = bufferStart + frames * frameSize;
3452
3453    // Check validity of returned pointer in case the track control block would have been corrupted.
3454    if (bufferStart < mBuffer || bufferStart > bufferEnd || bufferEnd > mBufferEnd ||
3455        ((unsigned long)bufferStart & (unsigned long)(frameSize - 1))) {
3456        ALOGE("TrackBase::getBuffer buffer out of range:\n    start: %p, end %p , mBuffer %p mBufferEnd %p\n    \
3457                server %d, serverBase %d, user %d, userBase %d",
3458                bufferStart, bufferEnd, mBuffer, mBufferEnd,
3459                cblk->server, cblk->serverBase, cblk->user, cblk->userBase);
3460        return NULL;
3461    }
3462
3463    return bufferStart;
3464}
3465
3466// ----------------------------------------------------------------------------
3467
3468// Track constructor must be called with AudioFlinger::mLock and ThreadBase::mLock held
3469AudioFlinger::PlaybackThread::Track::Track(
3470            PlaybackThread *thread,
3471            const sp<Client>& client,
3472            audio_stream_type_t streamType,
3473            uint32_t sampleRate,
3474            audio_format_t format,
3475            uint32_t channelMask,
3476            int frameCount,
3477            const sp<IMemory>& sharedBuffer,
3478            int sessionId)
3479    :   TrackBase(thread, client, sampleRate, format, channelMask, frameCount, sharedBuffer, sessionId),
3480    mMute(false), mSharedBuffer(sharedBuffer), mName(-1), mMainBuffer(NULL), mAuxBuffer(NULL),
3481    mAuxEffectId(0), mHasVolumeController(false)
3482{
3483    if (mCblk != NULL) {
3484        if (thread != NULL) {
3485            mName = thread->getTrackName_l();
3486            mMainBuffer = thread->mixBuffer();
3487        }
3488        ALOGV("Track constructor name %d, calling pid %d", mName, IPCThreadState::self()->getCallingPid());
3489        if (mName < 0) {
3490            ALOGE("no more track names available");
3491        }
3492        mStreamType = streamType;
3493        // NOTE: audio_track_cblk_t::frameSize for 8 bit PCM data is based on a sample size of
3494        // 16 bit because data is converted to 16 bit before being stored in buffer by AudioTrack
3495        mCblk->frameSize = audio_is_linear_pcm(format) ? mChannelCount * sizeof(int16_t) : sizeof(uint8_t);
3496    }
3497}
3498
3499AudioFlinger::PlaybackThread::Track::~Track()
3500{
3501    ALOGV("PlaybackThread::Track destructor");
3502    sp<ThreadBase> thread = mThread.promote();
3503    if (thread != 0) {
3504        Mutex::Autolock _l(thread->mLock);
3505        mState = TERMINATED;
3506    }
3507}
3508
3509void AudioFlinger::PlaybackThread::Track::destroy()
3510{
3511    // NOTE: destroyTrack_l() can remove a strong reference to this Track
3512    // by removing it from mTracks vector, so there is a risk that this Tracks's
3513    // destructor is called. As the destructor needs to lock mLock,
3514    // we must acquire a strong reference on this Track before locking mLock
3515    // here so that the destructor is called only when exiting this function.
3516    // On the other hand, as long as Track::destroy() is only called by
3517    // TrackHandle destructor, the TrackHandle still holds a strong ref on
3518    // this Track with its member mTrack.
3519    sp<Track> keep(this);
3520    { // scope for mLock
3521        sp<ThreadBase> thread = mThread.promote();
3522        if (thread != 0) {
3523            if (!isOutputTrack()) {
3524                if (mState == ACTIVE || mState == RESUMING) {
3525                    AudioSystem::stopOutput(thread->id(), mStreamType, mSessionId);
3526
3527                    // to track the speaker usage
3528                    addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop);
3529                }
3530                AudioSystem::releaseOutput(thread->id());
3531            }
3532            Mutex::Autolock _l(thread->mLock);
3533            PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
3534            playbackThread->destroyTrack_l(this);
3535        }
3536    }
3537}
3538
3539void AudioFlinger::PlaybackThread::Track::dump(char* buffer, size_t size)
3540{
3541    uint32_t vlr = mCblk->getVolumeLR();
3542    snprintf(buffer, size, "   %05d %05d %03u %03u 0x%08x %05u   %04u %1d %1d %1d %05u %05u %05u  0x%08x 0x%08x 0x%08x 0x%08x\n",
3543            mName - AudioMixer::TRACK0,
3544            (mClient == 0) ? getpid_cached : mClient->pid(),
3545            mStreamType,
3546            mFormat,
3547            mChannelMask,
3548            mSessionId,
3549            mFrameCount,
3550            mState,
3551            mMute,
3552            mFillingUpStatus,
3553            mCblk->sampleRate,
3554            vlr & 0xFFFF,
3555            vlr >> 16,
3556            mCblk->server,
3557            mCblk->user,
3558            (int)mMainBuffer,
3559            (int)mAuxBuffer);
3560}
3561
3562status_t AudioFlinger::PlaybackThread::Track::getNextBuffer(
3563    AudioBufferProvider::Buffer* buffer, int64_t pts)
3564{
3565     audio_track_cblk_t* cblk = this->cblk();
3566     uint32_t framesReady;
3567     uint32_t framesReq = buffer->frameCount;
3568
3569     // Check if last stepServer failed, try to step now
3570     if (mStepServerFailed) {
3571         if (!step())  goto getNextBuffer_exit;
3572         ALOGV("stepServer recovered");
3573         mStepServerFailed = false;
3574     }
3575
3576     framesReady = cblk->framesReady();
3577
3578     if (CC_LIKELY(framesReady)) {
3579        uint32_t s = cblk->server;
3580        uint32_t bufferEnd = cblk->serverBase + cblk->frameCount;
3581
3582        bufferEnd = (cblk->loopEnd < bufferEnd) ? cblk->loopEnd : bufferEnd;
3583        if (framesReq > framesReady) {
3584            framesReq = framesReady;
3585        }
3586        if (s + framesReq > bufferEnd) {
3587            framesReq = bufferEnd - s;
3588        }
3589
3590         buffer->raw = getBuffer(s, framesReq);
3591         if (buffer->raw == NULL) goto getNextBuffer_exit;
3592
3593         buffer->frameCount = framesReq;
3594        return NO_ERROR;
3595     }
3596
3597getNextBuffer_exit:
3598     buffer->raw = NULL;
3599     buffer->frameCount = 0;
3600     ALOGV("getNextBuffer() no more data for track %d on thread %p", mName, mThread.unsafe_get());
3601     return NOT_ENOUGH_DATA;
3602}
3603
3604uint32_t AudioFlinger::PlaybackThread::Track::framesReady() const{
3605    return mCblk->framesReady();
3606}
3607
3608bool AudioFlinger::PlaybackThread::Track::isReady() const {
3609    if (mFillingUpStatus != FS_FILLING || isStopped() || isPausing()) return true;
3610
3611    if (framesReady() >= mCblk->frameCount ||
3612            (mCblk->flags & CBLK_FORCEREADY_MSK)) {
3613        mFillingUpStatus = FS_FILLED;
3614        android_atomic_and(~CBLK_FORCEREADY_MSK, &mCblk->flags);
3615        return true;
3616    }
3617    return false;
3618}
3619
3620status_t AudioFlinger::PlaybackThread::Track::start(pid_t tid)
3621{
3622    status_t status = NO_ERROR;
3623    ALOGV("start(%d), calling pid %d session %d tid %d",
3624            mName, IPCThreadState::self()->getCallingPid(), mSessionId, tid);
3625    sp<ThreadBase> thread = mThread.promote();
3626    if (thread != 0) {
3627        Mutex::Autolock _l(thread->mLock);
3628        track_state state = mState;
3629        // here the track could be either new, or restarted
3630        // in both cases "unstop" the track
3631        if (mState == PAUSED) {
3632            mState = TrackBase::RESUMING;
3633            ALOGV("PAUSED => RESUMING (%d) on thread %p", mName, this);
3634        } else {
3635            mState = TrackBase::ACTIVE;
3636            ALOGV("? => ACTIVE (%d) on thread %p", mName, this);
3637        }
3638
3639        if (!isOutputTrack() && state != ACTIVE && state != RESUMING) {
3640            thread->mLock.unlock();
3641            status = AudioSystem::startOutput(thread->id(), mStreamType, mSessionId);
3642            thread->mLock.lock();
3643
3644            // to track the speaker usage
3645            if (status == NO_ERROR) {
3646                addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStart);
3647            }
3648        }
3649        if (status == NO_ERROR) {
3650            PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
3651            playbackThread->addTrack_l(this);
3652        } else {
3653            mState = state;
3654        }
3655    } else {
3656        status = BAD_VALUE;
3657    }
3658    return status;
3659}
3660
3661void AudioFlinger::PlaybackThread::Track::stop()
3662{
3663    ALOGV("stop(%d), calling pid %d", mName, IPCThreadState::self()->getCallingPid());
3664    sp<ThreadBase> thread = mThread.promote();
3665    if (thread != 0) {
3666        Mutex::Autolock _l(thread->mLock);
3667        track_state state = mState;
3668        if (mState > STOPPED) {
3669            mState = STOPPED;
3670            // If the track is not active (PAUSED and buffers full), flush buffers
3671            PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
3672            if (playbackThread->mActiveTracks.indexOf(this) < 0) {
3673                reset();
3674            }
3675            ALOGV("(> STOPPED) => STOPPED (%d) on thread %p", mName, playbackThread);
3676        }
3677        if (!isOutputTrack() && (state == ACTIVE || state == RESUMING)) {
3678            thread->mLock.unlock();
3679            AudioSystem::stopOutput(thread->id(), mStreamType, mSessionId);
3680            thread->mLock.lock();
3681
3682            // to track the speaker usage
3683            addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop);
3684        }
3685    }
3686}
3687
3688void AudioFlinger::PlaybackThread::Track::pause()
3689{
3690    ALOGV("pause(%d), calling pid %d", mName, IPCThreadState::self()->getCallingPid());
3691    sp<ThreadBase> thread = mThread.promote();
3692    if (thread != 0) {
3693        Mutex::Autolock _l(thread->mLock);
3694        if (mState == ACTIVE || mState == RESUMING) {
3695            mState = PAUSING;
3696            ALOGV("ACTIVE/RESUMING => PAUSING (%d) on thread %p", mName, thread.get());
3697            if (!isOutputTrack()) {
3698                thread->mLock.unlock();
3699                AudioSystem::stopOutput(thread->id(), mStreamType, mSessionId);
3700                thread->mLock.lock();
3701
3702                // to track the speaker usage
3703                addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop);
3704            }
3705        }
3706    }
3707}
3708
3709void AudioFlinger::PlaybackThread::Track::flush()
3710{
3711    ALOGV("flush(%d)", mName);
3712    sp<ThreadBase> thread = mThread.promote();
3713    if (thread != 0) {
3714        Mutex::Autolock _l(thread->mLock);
3715        if (mState != STOPPED && mState != PAUSED && mState != PAUSING) {
3716            return;
3717        }
3718        // No point remaining in PAUSED state after a flush => go to
3719        // STOPPED state
3720        mState = STOPPED;
3721
3722        // do not reset the track if it is still in the process of being stopped or paused.
3723        // this will be done by prepareTracks_l() when the track is stopped.
3724        PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
3725        if (playbackThread->mActiveTracks.indexOf(this) < 0) {
3726            reset();
3727        }
3728    }
3729}
3730
3731void AudioFlinger::PlaybackThread::Track::reset()
3732{
3733    // Do not reset twice to avoid discarding data written just after a flush and before
3734    // the audioflinger thread detects the track is stopped.
3735    if (!mResetDone) {
3736        TrackBase::reset();
3737        // Force underrun condition to avoid false underrun callback until first data is
3738        // written to buffer
3739        android_atomic_and(~CBLK_FORCEREADY_MSK, &mCblk->flags);
3740        android_atomic_or(CBLK_UNDERRUN_ON, &mCblk->flags);
3741        mFillingUpStatus = FS_FILLING;
3742        mResetDone = true;
3743    }
3744}
3745
3746void AudioFlinger::PlaybackThread::Track::mute(bool muted)
3747{
3748    mMute = muted;
3749}
3750
3751status_t AudioFlinger::PlaybackThread::Track::attachAuxEffect(int EffectId)
3752{
3753    status_t status = DEAD_OBJECT;
3754    sp<ThreadBase> thread = mThread.promote();
3755    if (thread != 0) {
3756       PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
3757       status = playbackThread->attachAuxEffect(this, EffectId);
3758    }
3759    return status;
3760}
3761
3762void AudioFlinger::PlaybackThread::Track::setAuxBuffer(int EffectId, int32_t *buffer)
3763{
3764    mAuxEffectId = EffectId;
3765    mAuxBuffer = buffer;
3766}
3767
3768// timed audio tracks
3769
3770sp<AudioFlinger::PlaybackThread::TimedTrack>
3771AudioFlinger::PlaybackThread::TimedTrack::create(
3772            PlaybackThread *thread,
3773            const sp<Client>& client,
3774            audio_stream_type_t streamType,
3775            uint32_t sampleRate,
3776            audio_format_t format,
3777            uint32_t channelMask,
3778            int frameCount,
3779            const sp<IMemory>& sharedBuffer,
3780            int sessionId) {
3781    if (!client->reserveTimedTrack())
3782        return NULL;
3783
3784    sp<TimedTrack> track = new TimedTrack(
3785        thread, client, streamType, sampleRate, format, channelMask, frameCount,
3786        sharedBuffer, sessionId);
3787
3788    if (track == NULL) {
3789        client->releaseTimedTrack();
3790        return NULL;
3791    }
3792
3793    return track;
3794}
3795
3796AudioFlinger::PlaybackThread::TimedTrack::TimedTrack(
3797            PlaybackThread *thread,
3798            const sp<Client>& client,
3799            audio_stream_type_t streamType,
3800            uint32_t sampleRate,
3801            audio_format_t format,
3802            uint32_t channelMask,
3803            int frameCount,
3804            const sp<IMemory>& sharedBuffer,
3805            int sessionId)
3806    : Track(thread, client, streamType, sampleRate, format, channelMask,
3807            frameCount, sharedBuffer, sessionId),
3808      mTimedSilenceBuffer(NULL),
3809      mTimedSilenceBufferSize(0),
3810      mTimedAudioOutputOnTime(false),
3811      mMediaTimeTransformValid(false)
3812{
3813    LocalClock lc;
3814    mLocalTimeFreq = lc.getLocalFreq();
3815
3816    mLocalTimeToSampleTransform.a_zero = 0;
3817    mLocalTimeToSampleTransform.b_zero = 0;
3818    mLocalTimeToSampleTransform.a_to_b_numer = sampleRate;
3819    mLocalTimeToSampleTransform.a_to_b_denom = mLocalTimeFreq;
3820    LinearTransform::reduce(&mLocalTimeToSampleTransform.a_to_b_numer,
3821                            &mLocalTimeToSampleTransform.a_to_b_denom);
3822}
3823
3824AudioFlinger::PlaybackThread::TimedTrack::~TimedTrack() {
3825    mClient->releaseTimedTrack();
3826    delete [] mTimedSilenceBuffer;
3827}
3828
3829status_t AudioFlinger::PlaybackThread::TimedTrack::allocateTimedBuffer(
3830    size_t size, sp<IMemory>* buffer) {
3831
3832    Mutex::Autolock _l(mTimedBufferQueueLock);
3833
3834    trimTimedBufferQueue_l();
3835
3836    // lazily initialize the shared memory heap for timed buffers
3837    if (mTimedMemoryDealer == NULL) {
3838        const int kTimedBufferHeapSize = 512 << 10;
3839
3840        mTimedMemoryDealer = new MemoryDealer(kTimedBufferHeapSize,
3841                                              "AudioFlingerTimed");
3842        if (mTimedMemoryDealer == NULL)
3843            return NO_MEMORY;
3844    }
3845
3846    sp<IMemory> newBuffer = mTimedMemoryDealer->allocate(size);
3847    if (newBuffer == NULL) {
3848        newBuffer = mTimedMemoryDealer->allocate(size);
3849        if (newBuffer == NULL)
3850            return NO_MEMORY;
3851    }
3852
3853    *buffer = newBuffer;
3854    return NO_ERROR;
3855}
3856
3857// caller must hold mTimedBufferQueueLock
3858void AudioFlinger::PlaybackThread::TimedTrack::trimTimedBufferQueue_l() {
3859    int64_t mediaTimeNow;
3860    {
3861        Mutex::Autolock mttLock(mMediaTimeTransformLock);
3862        if (!mMediaTimeTransformValid)
3863            return;
3864
3865        int64_t targetTimeNow;
3866        status_t res = (mMediaTimeTransformTarget == TimedAudioTrack::COMMON_TIME)
3867            ? mCCHelper.getCommonTime(&targetTimeNow)
3868            : mCCHelper.getLocalTime(&targetTimeNow);
3869
3870        if (OK != res)
3871            return;
3872
3873        if (!mMediaTimeTransform.doReverseTransform(targetTimeNow,
3874                                                    &mediaTimeNow)) {
3875            return;
3876        }
3877    }
3878
3879    size_t trimIndex;
3880    for (trimIndex = 0; trimIndex < mTimedBufferQueue.size(); trimIndex++) {
3881        if (mTimedBufferQueue[trimIndex].pts() > mediaTimeNow)
3882            break;
3883    }
3884
3885    if (trimIndex) {
3886        mTimedBufferQueue.removeItemsAt(0, trimIndex);
3887    }
3888}
3889
3890status_t AudioFlinger::PlaybackThread::TimedTrack::queueTimedBuffer(
3891    const sp<IMemory>& buffer, int64_t pts) {
3892
3893    {
3894        Mutex::Autolock mttLock(mMediaTimeTransformLock);
3895        if (!mMediaTimeTransformValid)
3896            return INVALID_OPERATION;
3897    }
3898
3899    Mutex::Autolock _l(mTimedBufferQueueLock);
3900
3901    mTimedBufferQueue.add(TimedBuffer(buffer, pts));
3902
3903    return NO_ERROR;
3904}
3905
3906status_t AudioFlinger::PlaybackThread::TimedTrack::setMediaTimeTransform(
3907    const LinearTransform& xform, TimedAudioTrack::TargetTimeline target) {
3908
3909    ALOGV("%s az=%lld bz=%lld n=%d d=%u tgt=%d", __PRETTY_FUNCTION__,
3910         xform.a_zero, xform.b_zero, xform.a_to_b_numer, xform.a_to_b_denom,
3911         target);
3912
3913    if (!(target == TimedAudioTrack::LOCAL_TIME ||
3914          target == TimedAudioTrack::COMMON_TIME)) {
3915        return BAD_VALUE;
3916    }
3917
3918    Mutex::Autolock lock(mMediaTimeTransformLock);
3919    mMediaTimeTransform = xform;
3920    mMediaTimeTransformTarget = target;
3921    mMediaTimeTransformValid = true;
3922
3923    return NO_ERROR;
3924}
3925
3926#define min(a, b) ((a) < (b) ? (a) : (b))
3927
3928// implementation of getNextBuffer for tracks whose buffers have timestamps
3929status_t AudioFlinger::PlaybackThread::TimedTrack::getNextBuffer(
3930    AudioBufferProvider::Buffer* buffer, int64_t pts)
3931{
3932    if (pts == AudioBufferProvider::kInvalidPTS) {
3933        buffer->raw = 0;
3934        buffer->frameCount = 0;
3935        return INVALID_OPERATION;
3936    }
3937
3938    Mutex::Autolock _l(mTimedBufferQueueLock);
3939
3940    while (true) {
3941
3942        // if we have no timed buffers, then fail
3943        if (mTimedBufferQueue.isEmpty()) {
3944            buffer->raw = 0;
3945            buffer->frameCount = 0;
3946            return NOT_ENOUGH_DATA;
3947        }
3948
3949        TimedBuffer& head = mTimedBufferQueue.editItemAt(0);
3950
3951        // calculate the PTS of the head of the timed buffer queue expressed in
3952        // local time
3953        int64_t headLocalPTS;
3954        {
3955            Mutex::Autolock mttLock(mMediaTimeTransformLock);
3956
3957            assert(mMediaTimeTransformValid);
3958
3959            if (mMediaTimeTransform.a_to_b_denom == 0) {
3960                // the transform represents a pause, so yield silence
3961                timedYieldSilence(buffer->frameCount, buffer);
3962                return NO_ERROR;
3963            }
3964
3965            int64_t transformedPTS;
3966            if (!mMediaTimeTransform.doForwardTransform(head.pts(),
3967                                                        &transformedPTS)) {
3968                // the transform failed.  this shouldn't happen, but if it does
3969                // then just drop this buffer
3970                ALOGW("timedGetNextBuffer transform failed");
3971                buffer->raw = 0;
3972                buffer->frameCount = 0;
3973                mTimedBufferQueue.removeAt(0);
3974                return NO_ERROR;
3975            }
3976
3977            if (mMediaTimeTransformTarget == TimedAudioTrack::COMMON_TIME) {
3978                if (OK != mCCHelper.commonTimeToLocalTime(transformedPTS,
3979                                                          &headLocalPTS)) {
3980                    buffer->raw = 0;
3981                    buffer->frameCount = 0;
3982                    return INVALID_OPERATION;
3983                }
3984            } else {
3985                headLocalPTS = transformedPTS;
3986            }
3987        }
3988
3989        // adjust the head buffer's PTS to reflect the portion of the head buffer
3990        // that has already been consumed
3991        int64_t effectivePTS = headLocalPTS +
3992                ((head.position() / mCblk->frameSize) * mLocalTimeFreq / sampleRate());
3993
3994        // Calculate the delta in samples between the head of the input buffer
3995        // queue and the start of the next output buffer that will be written.
3996        // If the transformation fails because of over or underflow, it means
3997        // that the sample's position in the output stream is so far out of
3998        // whack that it should just be dropped.
3999        int64_t sampleDelta;
4000        if (llabs(effectivePTS - pts) >= (static_cast<int64_t>(1) << 31)) {
4001            ALOGV("*** head buffer is too far from PTS: dropped buffer");
4002            mTimedBufferQueue.removeAt(0);
4003            continue;
4004        }
4005        if (!mLocalTimeToSampleTransform.doForwardTransform(
4006                (effectivePTS - pts) << 32, &sampleDelta)) {
4007            ALOGV("*** too late during sample rate transform: dropped buffer");
4008            mTimedBufferQueue.removeAt(0);
4009            continue;
4010        }
4011
4012        ALOGV("*** %s head.pts=%lld head.pos=%d pts=%lld sampleDelta=[%d.%08x]",
4013             __PRETTY_FUNCTION__, head.pts(), head.position(), pts,
4014             static_cast<int32_t>((sampleDelta >= 0 ? 0 : 1) + (sampleDelta >> 32)),
4015             static_cast<uint32_t>(sampleDelta & 0xFFFFFFFF));
4016
4017        // if the delta between the ideal placement for the next input sample and
4018        // the current output position is within this threshold, then we will
4019        // concatenate the next input samples to the previous output
4020        const int64_t kSampleContinuityThreshold =
4021                (static_cast<int64_t>(sampleRate()) << 32) / 10;
4022
4023        // if this is the first buffer of audio that we're emitting from this track
4024        // then it should be almost exactly on time.
4025        const int64_t kSampleStartupThreshold = 1LL << 32;
4026
4027        if ((mTimedAudioOutputOnTime && llabs(sampleDelta) <= kSampleContinuityThreshold) ||
4028            (!mTimedAudioOutputOnTime && llabs(sampleDelta) <= kSampleStartupThreshold)) {
4029            // the next input is close enough to being on time, so concatenate it
4030            // with the last output
4031            timedYieldSamples(buffer);
4032
4033            ALOGV("*** on time: head.pos=%d frameCount=%u", head.position(), buffer->frameCount);
4034            return NO_ERROR;
4035        } else if (sampleDelta > 0) {
4036            // the gap between the current output position and the proper start of
4037            // the next input sample is too big, so fill it with silence
4038            uint32_t framesUntilNextInput = (sampleDelta + 0x80000000) >> 32;
4039
4040            timedYieldSilence(framesUntilNextInput, buffer);
4041            ALOGV("*** silence: frameCount=%u", buffer->frameCount);
4042            return NO_ERROR;
4043        } else {
4044            // the next input sample is late
4045            uint32_t lateFrames = static_cast<uint32_t>(-((sampleDelta + 0x80000000) >> 32));
4046            size_t onTimeSamplePosition =
4047                    head.position() + lateFrames * mCblk->frameSize;
4048
4049            if (onTimeSamplePosition > head.buffer()->size()) {
4050                // all the remaining samples in the head are too late, so
4051                // drop it and move on
4052                ALOGV("*** too late: dropped buffer");
4053                mTimedBufferQueue.removeAt(0);
4054                continue;
4055            } else {
4056                // skip over the late samples
4057                head.setPosition(onTimeSamplePosition);
4058
4059                // yield the available samples
4060                timedYieldSamples(buffer);
4061
4062                ALOGV("*** late: head.pos=%d frameCount=%u", head.position(), buffer->frameCount);
4063                return NO_ERROR;
4064            }
4065        }
4066    }
4067}
4068
4069// Yield samples from the timed buffer queue head up to the given output
4070// buffer's capacity.
4071//
4072// Caller must hold mTimedBufferQueueLock
4073void AudioFlinger::PlaybackThread::TimedTrack::timedYieldSamples(
4074    AudioBufferProvider::Buffer* buffer) {
4075
4076    const TimedBuffer& head = mTimedBufferQueue[0];
4077
4078    buffer->raw = (static_cast<uint8_t*>(head.buffer()->pointer()) +
4079                   head.position());
4080
4081    uint32_t framesLeftInHead = ((head.buffer()->size() - head.position()) /
4082                                 mCblk->frameSize);
4083    size_t framesRequested = buffer->frameCount;
4084    buffer->frameCount = min(framesLeftInHead, framesRequested);
4085
4086    mTimedAudioOutputOnTime = true;
4087}
4088
4089// Yield samples of silence up to the given output buffer's capacity
4090//
4091// Caller must hold mTimedBufferQueueLock
4092void AudioFlinger::PlaybackThread::TimedTrack::timedYieldSilence(
4093    uint32_t numFrames, AudioBufferProvider::Buffer* buffer) {
4094
4095    // lazily allocate a buffer filled with silence
4096    if (mTimedSilenceBufferSize < numFrames * mCblk->frameSize) {
4097        delete [] mTimedSilenceBuffer;
4098        mTimedSilenceBufferSize = numFrames * mCblk->frameSize;
4099        mTimedSilenceBuffer = new uint8_t[mTimedSilenceBufferSize];
4100        memset(mTimedSilenceBuffer, 0, mTimedSilenceBufferSize);
4101    }
4102
4103    buffer->raw = mTimedSilenceBuffer;
4104    size_t framesRequested = buffer->frameCount;
4105    buffer->frameCount = min(numFrames, framesRequested);
4106
4107    mTimedAudioOutputOnTime = false;
4108}
4109
4110void AudioFlinger::PlaybackThread::TimedTrack::releaseBuffer(
4111    AudioBufferProvider::Buffer* buffer) {
4112
4113    Mutex::Autolock _l(mTimedBufferQueueLock);
4114
4115    // If the buffer which was just released is part of the buffer at the head
4116    // of the queue, be sure to update the amt of the buffer which has been
4117    // consumed.  If the buffer being returned is not part of the head of the
4118    // queue, its either because the buffer is part of the silence buffer, or
4119    // because the head of the timed queue was trimmed after the mixer called
4120    // getNextBuffer but before the mixer called releaseBuffer.
4121    if ((buffer->raw != mTimedSilenceBuffer) && mTimedBufferQueue.size()) {
4122        TimedBuffer& head = mTimedBufferQueue.editItemAt(0);
4123
4124        void* start = head.buffer()->pointer();
4125        void* end   = (char *) head.buffer()->pointer() + head.buffer()->size();
4126
4127        if ((buffer->raw >= start) && (buffer->raw <= end)) {
4128            head.setPosition(head.position() +
4129                    (buffer->frameCount * mCblk->frameSize));
4130            if (static_cast<size_t>(head.position()) >= head.buffer()->size()) {
4131                mTimedBufferQueue.removeAt(0);
4132            }
4133        }
4134    }
4135
4136    buffer->raw = 0;
4137    buffer->frameCount = 0;
4138}
4139
4140uint32_t AudioFlinger::PlaybackThread::TimedTrack::framesReady() const {
4141    Mutex::Autolock _l(mTimedBufferQueueLock);
4142
4143    uint32_t frames = 0;
4144    for (size_t i = 0; i < mTimedBufferQueue.size(); i++) {
4145        const TimedBuffer& tb = mTimedBufferQueue[i];
4146        frames += (tb.buffer()->size() - tb.position())  / mCblk->frameSize;
4147    }
4148
4149    return frames;
4150}
4151
4152AudioFlinger::PlaybackThread::TimedTrack::TimedBuffer::TimedBuffer()
4153        : mPTS(0), mPosition(0) {}
4154
4155AudioFlinger::PlaybackThread::TimedTrack::TimedBuffer::TimedBuffer(
4156    const sp<IMemory>& buffer, int64_t pts)
4157        : mBuffer(buffer), mPTS(pts), mPosition(0) {}
4158
4159// ----------------------------------------------------------------------------
4160
4161// RecordTrack constructor must be called with AudioFlinger::mLock held
4162AudioFlinger::RecordThread::RecordTrack::RecordTrack(
4163            RecordThread *thread,
4164            const sp<Client>& client,
4165            uint32_t sampleRate,
4166            audio_format_t format,
4167            uint32_t channelMask,
4168            int frameCount,
4169            int sessionId)
4170    :   TrackBase(thread, client, sampleRate, format,
4171                  channelMask, frameCount, 0 /*sharedBuffer*/, sessionId),
4172        mOverflow(false)
4173{
4174    if (mCblk != NULL) {
4175       ALOGV("RecordTrack constructor, size %d", (int)mBufferEnd - (int)mBuffer);
4176       if (format == AUDIO_FORMAT_PCM_16_BIT) {
4177           mCblk->frameSize = mChannelCount * sizeof(int16_t);
4178       } else if (format == AUDIO_FORMAT_PCM_8_BIT) {
4179           mCblk->frameSize = mChannelCount * sizeof(int8_t);
4180       } else {
4181           mCblk->frameSize = sizeof(int8_t);
4182       }
4183    }
4184}
4185
4186AudioFlinger::RecordThread::RecordTrack::~RecordTrack()
4187{
4188    sp<ThreadBase> thread = mThread.promote();
4189    if (thread != 0) {
4190        AudioSystem::releaseInput(thread->id());
4191    }
4192}
4193
4194status_t AudioFlinger::RecordThread::RecordTrack::getNextBuffer(AudioBufferProvider::Buffer* buffer, int64_t pts)
4195{
4196    audio_track_cblk_t* cblk = this->cblk();
4197    uint32_t framesAvail;
4198    uint32_t framesReq = buffer->frameCount;
4199
4200     // Check if last stepServer failed, try to step now
4201    if (mStepServerFailed) {
4202        if (!step()) goto getNextBuffer_exit;
4203        ALOGV("stepServer recovered");
4204        mStepServerFailed = false;
4205    }
4206
4207    framesAvail = cblk->framesAvailable_l();
4208
4209    if (CC_LIKELY(framesAvail)) {
4210        uint32_t s = cblk->server;
4211        uint32_t bufferEnd = cblk->serverBase + cblk->frameCount;
4212
4213        if (framesReq > framesAvail) {
4214            framesReq = framesAvail;
4215        }
4216        if (s + framesReq > bufferEnd) {
4217            framesReq = bufferEnd - s;
4218        }
4219
4220        buffer->raw = getBuffer(s, framesReq);
4221        if (buffer->raw == NULL) goto getNextBuffer_exit;
4222
4223        buffer->frameCount = framesReq;
4224        return NO_ERROR;
4225    }
4226
4227getNextBuffer_exit:
4228    buffer->raw = NULL;
4229    buffer->frameCount = 0;
4230    return NOT_ENOUGH_DATA;
4231}
4232
4233status_t AudioFlinger::RecordThread::RecordTrack::start(pid_t tid)
4234{
4235    sp<ThreadBase> thread = mThread.promote();
4236    if (thread != 0) {
4237        RecordThread *recordThread = (RecordThread *)thread.get();
4238        return recordThread->start(this, tid);
4239    } else {
4240        return BAD_VALUE;
4241    }
4242}
4243
4244void AudioFlinger::RecordThread::RecordTrack::stop()
4245{
4246    sp<ThreadBase> thread = mThread.promote();
4247    if (thread != 0) {
4248        RecordThread *recordThread = (RecordThread *)thread.get();
4249        recordThread->stop(this);
4250        TrackBase::reset();
4251        // Force overerrun condition to avoid false overrun callback until first data is
4252        // read from buffer
4253        android_atomic_or(CBLK_UNDERRUN_ON, &mCblk->flags);
4254    }
4255}
4256
4257void AudioFlinger::RecordThread::RecordTrack::dump(char* buffer, size_t size)
4258{
4259    snprintf(buffer, size, "   %05d %03u 0x%08x %05d   %04u %01d %05u  %08x %08x\n",
4260            (mClient == 0) ? getpid_cached : mClient->pid(),
4261            mFormat,
4262            mChannelMask,
4263            mSessionId,
4264            mFrameCount,
4265            mState,
4266            mCblk->sampleRate,
4267            mCblk->server,
4268            mCblk->user);
4269}
4270
4271
4272// ----------------------------------------------------------------------------
4273
4274AudioFlinger::PlaybackThread::OutputTrack::OutputTrack(
4275            PlaybackThread *playbackThread,
4276            DuplicatingThread *sourceThread,
4277            uint32_t sampleRate,
4278            audio_format_t format,
4279            uint32_t channelMask,
4280            int frameCount)
4281    :   Track(playbackThread, NULL, AUDIO_STREAM_CNT, sampleRate, format, channelMask, frameCount, NULL, 0),
4282    mActive(false), mSourceThread(sourceThread)
4283{
4284
4285    if (mCblk != NULL) {
4286        mCblk->flags |= CBLK_DIRECTION_OUT;
4287        mCblk->buffers = (char*)mCblk + sizeof(audio_track_cblk_t);
4288        mOutBuffer.frameCount = 0;
4289        playbackThread->mTracks.add(this);
4290        ALOGV("OutputTrack constructor mCblk %p, mBuffer %p, mCblk->buffers %p, " \
4291                "mCblk->frameCount %d, mCblk->sampleRate %d, mChannelMask 0x%08x mBufferEnd %p",
4292                mCblk, mBuffer, mCblk->buffers,
4293                mCblk->frameCount, mCblk->sampleRate, mChannelMask, mBufferEnd);
4294    } else {
4295        ALOGW("Error creating output track on thread %p", playbackThread);
4296    }
4297}
4298
4299AudioFlinger::PlaybackThread::OutputTrack::~OutputTrack()
4300{
4301    clearBufferQueue();
4302}
4303
4304status_t AudioFlinger::PlaybackThread::OutputTrack::start(pid_t tid)
4305{
4306    status_t status = Track::start(tid);
4307    if (status != NO_ERROR) {
4308        return status;
4309    }
4310
4311    mActive = true;
4312    mRetryCount = 127;
4313    return status;
4314}
4315
4316void AudioFlinger::PlaybackThread::OutputTrack::stop()
4317{
4318    Track::stop();
4319    clearBufferQueue();
4320    mOutBuffer.frameCount = 0;
4321    mActive = false;
4322}
4323
4324bool AudioFlinger::PlaybackThread::OutputTrack::write(int16_t* data, uint32_t frames)
4325{
4326    Buffer *pInBuffer;
4327    Buffer inBuffer;
4328    uint32_t channelCount = mChannelCount;
4329    bool outputBufferFull = false;
4330    inBuffer.frameCount = frames;
4331    inBuffer.i16 = data;
4332
4333    uint32_t waitTimeLeftMs = mSourceThread->waitTimeMs();
4334
4335    if (!mActive && frames != 0) {
4336        start(0);
4337        sp<ThreadBase> thread = mThread.promote();
4338        if (thread != 0) {
4339            MixerThread *mixerThread = (MixerThread *)thread.get();
4340            if (mCblk->frameCount > frames){
4341                if (mBufferQueue.size() < kMaxOverFlowBuffers) {
4342                    uint32_t startFrames = (mCblk->frameCount - frames);
4343                    pInBuffer = new Buffer;
4344                    pInBuffer->mBuffer = new int16_t[startFrames * channelCount];
4345                    pInBuffer->frameCount = startFrames;
4346                    pInBuffer->i16 = pInBuffer->mBuffer;
4347                    memset(pInBuffer->raw, 0, startFrames * channelCount * sizeof(int16_t));
4348                    mBufferQueue.add(pInBuffer);
4349                } else {
4350                    ALOGW ("OutputTrack::write() %p no more buffers in queue", this);
4351                }
4352            }
4353        }
4354    }
4355
4356    while (waitTimeLeftMs) {
4357        // First write pending buffers, then new data
4358        if (mBufferQueue.size()) {
4359            pInBuffer = mBufferQueue.itemAt(0);
4360        } else {
4361            pInBuffer = &inBuffer;
4362        }
4363
4364        if (pInBuffer->frameCount == 0) {
4365            break;
4366        }
4367
4368        if (mOutBuffer.frameCount == 0) {
4369            mOutBuffer.frameCount = pInBuffer->frameCount;
4370            nsecs_t startTime = systemTime();
4371            if (obtainBuffer(&mOutBuffer, waitTimeLeftMs) == (status_t)NO_MORE_BUFFERS) {
4372                ALOGV ("OutputTrack::write() %p thread %p no more output buffers", this, mThread.unsafe_get());
4373                outputBufferFull = true;
4374                break;
4375            }
4376            uint32_t waitTimeMs = (uint32_t)ns2ms(systemTime() - startTime);
4377            if (waitTimeLeftMs >= waitTimeMs) {
4378                waitTimeLeftMs -= waitTimeMs;
4379            } else {
4380                waitTimeLeftMs = 0;
4381            }
4382        }
4383
4384        uint32_t outFrames = pInBuffer->frameCount > mOutBuffer.frameCount ? mOutBuffer.frameCount : pInBuffer->frameCount;
4385        memcpy(mOutBuffer.raw, pInBuffer->raw, outFrames * channelCount * sizeof(int16_t));
4386        mCblk->stepUser(outFrames);
4387        pInBuffer->frameCount -= outFrames;
4388        pInBuffer->i16 += outFrames * channelCount;
4389        mOutBuffer.frameCount -= outFrames;
4390        mOutBuffer.i16 += outFrames * channelCount;
4391
4392        if (pInBuffer->frameCount == 0) {
4393            if (mBufferQueue.size()) {
4394                mBufferQueue.removeAt(0);
4395                delete [] pInBuffer->mBuffer;
4396                delete pInBuffer;
4397                ALOGV("OutputTrack::write() %p thread %p released overflow buffer %d", this, mThread.unsafe_get(), mBufferQueue.size());
4398            } else {
4399                break;
4400            }
4401        }
4402    }
4403
4404    // If we could not write all frames, allocate a buffer and queue it for next time.
4405    if (inBuffer.frameCount) {
4406        sp<ThreadBase> thread = mThread.promote();
4407        if (thread != 0 && !thread->standby()) {
4408            if (mBufferQueue.size() < kMaxOverFlowBuffers) {
4409                pInBuffer = new Buffer;
4410                pInBuffer->mBuffer = new int16_t[inBuffer.frameCount * channelCount];
4411                pInBuffer->frameCount = inBuffer.frameCount;
4412                pInBuffer->i16 = pInBuffer->mBuffer;
4413                memcpy(pInBuffer->raw, inBuffer.raw, inBuffer.frameCount * channelCount * sizeof(int16_t));
4414                mBufferQueue.add(pInBuffer);
4415                ALOGV("OutputTrack::write() %p thread %p adding overflow buffer %d", this, mThread.unsafe_get(), mBufferQueue.size());
4416            } else {
4417                ALOGW("OutputTrack::write() %p thread %p no more overflow buffers", mThread.unsafe_get(), this);
4418            }
4419        }
4420    }
4421
4422    // Calling write() with a 0 length buffer, means that no more data will be written:
4423    // If no more buffers are pending, fill output track buffer to make sure it is started
4424    // by output mixer.
4425    if (frames == 0 && mBufferQueue.size() == 0) {
4426        if (mCblk->user < mCblk->frameCount) {
4427            frames = mCblk->frameCount - mCblk->user;
4428            pInBuffer = new Buffer;
4429            pInBuffer->mBuffer = new int16_t[frames * channelCount];
4430            pInBuffer->frameCount = frames;
4431            pInBuffer->i16 = pInBuffer->mBuffer;
4432            memset(pInBuffer->raw, 0, frames * channelCount * sizeof(int16_t));
4433            mBufferQueue.add(pInBuffer);
4434        } else if (mActive) {
4435            stop();
4436        }
4437    }
4438
4439    return outputBufferFull;
4440}
4441
4442status_t AudioFlinger::PlaybackThread::OutputTrack::obtainBuffer(AudioBufferProvider::Buffer* buffer, uint32_t waitTimeMs)
4443{
4444    int active;
4445    status_t result;
4446    audio_track_cblk_t* cblk = mCblk;
4447    uint32_t framesReq = buffer->frameCount;
4448
4449//    ALOGV("OutputTrack::obtainBuffer user %d, server %d", cblk->user, cblk->server);
4450    buffer->frameCount  = 0;
4451
4452    uint32_t framesAvail = cblk->framesAvailable();
4453
4454
4455    if (framesAvail == 0) {
4456        Mutex::Autolock _l(cblk->lock);
4457        goto start_loop_here;
4458        while (framesAvail == 0) {
4459            active = mActive;
4460            if (CC_UNLIKELY(!active)) {
4461                ALOGV("Not active and NO_MORE_BUFFERS");
4462                return NO_MORE_BUFFERS;
4463            }
4464            result = cblk->cv.waitRelative(cblk->lock, milliseconds(waitTimeMs));
4465            if (result != NO_ERROR) {
4466                return NO_MORE_BUFFERS;
4467            }
4468            // read the server count again
4469        start_loop_here:
4470            framesAvail = cblk->framesAvailable_l();
4471        }
4472    }
4473
4474//    if (framesAvail < framesReq) {
4475//        return NO_MORE_BUFFERS;
4476//    }
4477
4478    if (framesReq > framesAvail) {
4479        framesReq = framesAvail;
4480    }
4481
4482    uint32_t u = cblk->user;
4483    uint32_t bufferEnd = cblk->userBase + cblk->frameCount;
4484
4485    if (u + framesReq > bufferEnd) {
4486        framesReq = bufferEnd - u;
4487    }
4488
4489    buffer->frameCount  = framesReq;
4490    buffer->raw         = (void *)cblk->buffer(u);
4491    return NO_ERROR;
4492}
4493
4494
4495void AudioFlinger::PlaybackThread::OutputTrack::clearBufferQueue()
4496{
4497    size_t size = mBufferQueue.size();
4498
4499    for (size_t i = 0; i < size; i++) {
4500        Buffer *pBuffer = mBufferQueue.itemAt(i);
4501        delete [] pBuffer->mBuffer;
4502        delete pBuffer;
4503    }
4504    mBufferQueue.clear();
4505}
4506
4507// ----------------------------------------------------------------------------
4508
4509AudioFlinger::Client::Client(const sp<AudioFlinger>& audioFlinger, pid_t pid)
4510    :   RefBase(),
4511        mAudioFlinger(audioFlinger),
4512        // FIXME should be a "k" constant not hard-coded, in .h or ro. property, see 4 lines below
4513        mMemoryDealer(new MemoryDealer(1024*1024, "AudioFlinger::Client")),
4514        mPid(pid),
4515        mTimedTrackCount(0)
4516{
4517    // 1 MB of address space is good for 32 tracks, 8 buffers each, 4 KB/buffer
4518}
4519
4520// Client destructor must be called with AudioFlinger::mLock held
4521AudioFlinger::Client::~Client()
4522{
4523    mAudioFlinger->removeClient_l(mPid);
4524}
4525
4526sp<MemoryDealer> AudioFlinger::Client::heap() const
4527{
4528    return mMemoryDealer;
4529}
4530
4531// Reserve one of the limited slots for a timed audio track associated
4532// with this client
4533bool AudioFlinger::Client::reserveTimedTrack()
4534{
4535    const int kMaxTimedTracksPerClient = 4;
4536
4537    Mutex::Autolock _l(mTimedTrackLock);
4538
4539    if (mTimedTrackCount >= kMaxTimedTracksPerClient) {
4540        ALOGW("can not create timed track - pid %d has exceeded the limit",
4541             mPid);
4542        return false;
4543    }
4544
4545    mTimedTrackCount++;
4546    return true;
4547}
4548
4549// Release a slot for a timed audio track
4550void AudioFlinger::Client::releaseTimedTrack()
4551{
4552    Mutex::Autolock _l(mTimedTrackLock);
4553    mTimedTrackCount--;
4554}
4555
4556// ----------------------------------------------------------------------------
4557
4558AudioFlinger::NotificationClient::NotificationClient(const sp<AudioFlinger>& audioFlinger,
4559                                                     const sp<IAudioFlingerClient>& client,
4560                                                     pid_t pid)
4561    : mAudioFlinger(audioFlinger), mPid(pid), mAudioFlingerClient(client)
4562{
4563}
4564
4565AudioFlinger::NotificationClient::~NotificationClient()
4566{
4567}
4568
4569void AudioFlinger::NotificationClient::binderDied(const wp<IBinder>& who)
4570{
4571    sp<NotificationClient> keep(this);
4572    mAudioFlinger->removeNotificationClient(mPid);
4573}
4574
4575// ----------------------------------------------------------------------------
4576
4577AudioFlinger::TrackHandle::TrackHandle(const sp<AudioFlinger::PlaybackThread::Track>& track)
4578    : BnAudioTrack(),
4579      mTrack(track)
4580{
4581}
4582
4583AudioFlinger::TrackHandle::~TrackHandle() {
4584    // just stop the track on deletion, associated resources
4585    // will be freed from the main thread once all pending buffers have
4586    // been played. Unless it's not in the active track list, in which
4587    // case we free everything now...
4588    mTrack->destroy();
4589}
4590
4591sp<IMemory> AudioFlinger::TrackHandle::getCblk() const {
4592    return mTrack->getCblk();
4593}
4594
4595status_t AudioFlinger::TrackHandle::start(pid_t tid) {
4596    return mTrack->start(tid);
4597}
4598
4599void AudioFlinger::TrackHandle::stop() {
4600    mTrack->stop();
4601}
4602
4603void AudioFlinger::TrackHandle::flush() {
4604    mTrack->flush();
4605}
4606
4607void AudioFlinger::TrackHandle::mute(bool e) {
4608    mTrack->mute(e);
4609}
4610
4611void AudioFlinger::TrackHandle::pause() {
4612    mTrack->pause();
4613}
4614
4615status_t AudioFlinger::TrackHandle::attachAuxEffect(int EffectId)
4616{
4617    return mTrack->attachAuxEffect(EffectId);
4618}
4619
4620status_t AudioFlinger::TrackHandle::allocateTimedBuffer(size_t size,
4621                                                         sp<IMemory>* buffer) {
4622    if (!mTrack->isTimedTrack())
4623        return INVALID_OPERATION;
4624
4625    PlaybackThread::TimedTrack* tt =
4626            reinterpret_cast<PlaybackThread::TimedTrack*>(mTrack.get());
4627    return tt->allocateTimedBuffer(size, buffer);
4628}
4629
4630status_t AudioFlinger::TrackHandle::queueTimedBuffer(const sp<IMemory>& buffer,
4631                                                     int64_t pts) {
4632    if (!mTrack->isTimedTrack())
4633        return INVALID_OPERATION;
4634
4635    PlaybackThread::TimedTrack* tt =
4636            reinterpret_cast<PlaybackThread::TimedTrack*>(mTrack.get());
4637    return tt->queueTimedBuffer(buffer, pts);
4638}
4639
4640status_t AudioFlinger::TrackHandle::setMediaTimeTransform(
4641    const LinearTransform& xform, int target) {
4642
4643    if (!mTrack->isTimedTrack())
4644        return INVALID_OPERATION;
4645
4646    PlaybackThread::TimedTrack* tt =
4647            reinterpret_cast<PlaybackThread::TimedTrack*>(mTrack.get());
4648    return tt->setMediaTimeTransform(
4649        xform, static_cast<TimedAudioTrack::TargetTimeline>(target));
4650}
4651
4652status_t AudioFlinger::TrackHandle::onTransact(
4653    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
4654{
4655    return BnAudioTrack::onTransact(code, data, reply, flags);
4656}
4657
4658// ----------------------------------------------------------------------------
4659
4660sp<IAudioRecord> AudioFlinger::openRecord(
4661        pid_t pid,
4662        audio_io_handle_t input,
4663        uint32_t sampleRate,
4664        audio_format_t format,
4665        uint32_t channelMask,
4666        int frameCount,
4667        // FIXME dead, remove from IAudioFlinger
4668        uint32_t flags,
4669        int *sessionId,
4670        status_t *status)
4671{
4672    sp<RecordThread::RecordTrack> recordTrack;
4673    sp<RecordHandle> recordHandle;
4674    sp<Client> client;
4675    status_t lStatus;
4676    RecordThread *thread;
4677    size_t inFrameCount;
4678    int lSessionId;
4679
4680    // check calling permissions
4681    if (!recordingAllowed()) {
4682        lStatus = PERMISSION_DENIED;
4683        goto Exit;
4684    }
4685
4686    // add client to list
4687    { // scope for mLock
4688        Mutex::Autolock _l(mLock);
4689        thread = checkRecordThread_l(input);
4690        if (thread == NULL) {
4691            lStatus = BAD_VALUE;
4692            goto Exit;
4693        }
4694
4695        client = registerPid_l(pid);
4696
4697        // If no audio session id is provided, create one here
4698        if (sessionId != NULL && *sessionId != AUDIO_SESSION_OUTPUT_MIX) {
4699            lSessionId = *sessionId;
4700        } else {
4701            lSessionId = nextUniqueId();
4702            if (sessionId != NULL) {
4703                *sessionId = lSessionId;
4704            }
4705        }
4706        // create new record track. The record track uses one track in mHardwareMixerThread by convention.
4707        recordTrack = thread->createRecordTrack_l(client,
4708                                                sampleRate,
4709                                                format,
4710                                                channelMask,
4711                                                frameCount,
4712                                                lSessionId,
4713                                                &lStatus);
4714    }
4715    if (lStatus != NO_ERROR) {
4716        // remove local strong reference to Client before deleting the RecordTrack so that the Client
4717        // destructor is called by the TrackBase destructor with mLock held
4718        client.clear();
4719        recordTrack.clear();
4720        goto Exit;
4721    }
4722
4723    // return to handle to client
4724    recordHandle = new RecordHandle(recordTrack);
4725    lStatus = NO_ERROR;
4726
4727Exit:
4728    if (status) {
4729        *status = lStatus;
4730    }
4731    return recordHandle;
4732}
4733
4734// ----------------------------------------------------------------------------
4735
4736AudioFlinger::RecordHandle::RecordHandle(const sp<AudioFlinger::RecordThread::RecordTrack>& recordTrack)
4737    : BnAudioRecord(),
4738    mRecordTrack(recordTrack)
4739{
4740}
4741
4742AudioFlinger::RecordHandle::~RecordHandle() {
4743    stop();
4744}
4745
4746sp<IMemory> AudioFlinger::RecordHandle::getCblk() const {
4747    return mRecordTrack->getCblk();
4748}
4749
4750status_t AudioFlinger::RecordHandle::start(pid_t tid) {
4751    ALOGV("RecordHandle::start()");
4752    return mRecordTrack->start(tid);
4753}
4754
4755void AudioFlinger::RecordHandle::stop() {
4756    ALOGV("RecordHandle::stop()");
4757    mRecordTrack->stop();
4758}
4759
4760status_t AudioFlinger::RecordHandle::onTransact(
4761    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
4762{
4763    return BnAudioRecord::onTransact(code, data, reply, flags);
4764}
4765
4766// ----------------------------------------------------------------------------
4767
4768AudioFlinger::RecordThread::RecordThread(const sp<AudioFlinger>& audioFlinger,
4769                                         AudioStreamIn *input,
4770                                         uint32_t sampleRate,
4771                                         uint32_t channels,
4772                                         audio_io_handle_t id,
4773                                         uint32_t device) :
4774    ThreadBase(audioFlinger, id, device, RECORD),
4775    mInput(input), mTrack(NULL), mResampler(NULL), mRsmpOutBuffer(NULL), mRsmpInBuffer(NULL),
4776    // mRsmpInIndex and mInputBytes set by readInputParameters()
4777    mReqChannelCount(popcount(channels)),
4778    mReqSampleRate(sampleRate)
4779    // mBytesRead is only meaningful while active, and so is cleared in start()
4780    // (but might be better to also clear here for dump?)
4781{
4782    snprintf(mName, kNameLength, "AudioIn_%d", id);
4783
4784    readInputParameters();
4785}
4786
4787
4788AudioFlinger::RecordThread::~RecordThread()
4789{
4790    delete[] mRsmpInBuffer;
4791    delete mResampler;
4792    delete[] mRsmpOutBuffer;
4793}
4794
4795void AudioFlinger::RecordThread::onFirstRef()
4796{
4797    run(mName, PRIORITY_URGENT_AUDIO);
4798}
4799
4800status_t AudioFlinger::RecordThread::readyToRun()
4801{
4802    status_t status = initCheck();
4803    ALOGW_IF(status != NO_ERROR,"RecordThread %p could not initialize", this);
4804    return status;
4805}
4806
4807bool AudioFlinger::RecordThread::threadLoop()
4808{
4809    AudioBufferProvider::Buffer buffer;
4810    sp<RecordTrack> activeTrack;
4811    Vector< sp<EffectChain> > effectChains;
4812
4813    nsecs_t lastWarning = 0;
4814
4815    acquireWakeLock();
4816
4817    // start recording
4818    while (!exitPending()) {
4819
4820        processConfigEvents();
4821
4822        { // scope for mLock
4823            Mutex::Autolock _l(mLock);
4824            checkForNewParameters_l();
4825            if (mActiveTrack == 0 && mConfigEvents.isEmpty()) {
4826                if (!mStandby) {
4827                    mInput->stream->common.standby(&mInput->stream->common);
4828                    mStandby = true;
4829                }
4830
4831                if (exitPending()) break;
4832
4833                releaseWakeLock_l();
4834                ALOGV("RecordThread: loop stopping");
4835                // go to sleep
4836                mWaitWorkCV.wait(mLock);
4837                ALOGV("RecordThread: loop starting");
4838                acquireWakeLock_l();
4839                continue;
4840            }
4841            if (mActiveTrack != 0) {
4842                if (mActiveTrack->mState == TrackBase::PAUSING) {
4843                    if (!mStandby) {
4844                        mInput->stream->common.standby(&mInput->stream->common);
4845                        mStandby = true;
4846                    }
4847                    mActiveTrack.clear();
4848                    mStartStopCond.broadcast();
4849                } else if (mActiveTrack->mState == TrackBase::RESUMING) {
4850                    if (mReqChannelCount != mActiveTrack->channelCount()) {
4851                        mActiveTrack.clear();
4852                        mStartStopCond.broadcast();
4853                    } else if (mBytesRead != 0) {
4854                        // record start succeeds only if first read from audio input
4855                        // succeeds
4856                        if (mBytesRead > 0) {
4857                            mActiveTrack->mState = TrackBase::ACTIVE;
4858                        } else {
4859                            mActiveTrack.clear();
4860                        }
4861                        mStartStopCond.broadcast();
4862                    }
4863                    mStandby = false;
4864                }
4865            }
4866            lockEffectChains_l(effectChains);
4867        }
4868
4869        if (mActiveTrack != 0) {
4870            if (mActiveTrack->mState != TrackBase::ACTIVE &&
4871                mActiveTrack->mState != TrackBase::RESUMING) {
4872                unlockEffectChains(effectChains);
4873                usleep(kRecordThreadSleepUs);
4874                continue;
4875            }
4876            for (size_t i = 0; i < effectChains.size(); i ++) {
4877                effectChains[i]->process_l();
4878            }
4879
4880            buffer.frameCount = mFrameCount;
4881            if (CC_LIKELY(mActiveTrack->getNextBuffer(
4882                    &buffer, AudioBufferProvider::kInvalidPTS) == NO_ERROR)) {
4883                size_t framesOut = buffer.frameCount;
4884                if (mResampler == NULL) {
4885                    // no resampling
4886                    while (framesOut) {
4887                        size_t framesIn = mFrameCount - mRsmpInIndex;
4888                        if (framesIn) {
4889                            int8_t *src = (int8_t *)mRsmpInBuffer + mRsmpInIndex * mFrameSize;
4890                            int8_t *dst = buffer.i8 + (buffer.frameCount - framesOut) * mActiveTrack->mCblk->frameSize;
4891                            if (framesIn > framesOut)
4892                                framesIn = framesOut;
4893                            mRsmpInIndex += framesIn;
4894                            framesOut -= framesIn;
4895                            if ((int)mChannelCount == mReqChannelCount ||
4896                                mFormat != AUDIO_FORMAT_PCM_16_BIT) {
4897                                memcpy(dst, src, framesIn * mFrameSize);
4898                            } else {
4899                                int16_t *src16 = (int16_t *)src;
4900                                int16_t *dst16 = (int16_t *)dst;
4901                                if (mChannelCount == 1) {
4902                                    while (framesIn--) {
4903                                        *dst16++ = *src16;
4904                                        *dst16++ = *src16++;
4905                                    }
4906                                } else {
4907                                    while (framesIn--) {
4908                                        *dst16++ = (int16_t)(((int32_t)*src16 + (int32_t)*(src16 + 1)) >> 1);
4909                                        src16 += 2;
4910                                    }
4911                                }
4912                            }
4913                        }
4914                        if (framesOut && mFrameCount == mRsmpInIndex) {
4915                            if (framesOut == mFrameCount &&
4916                                ((int)mChannelCount == mReqChannelCount || mFormat != AUDIO_FORMAT_PCM_16_BIT)) {
4917                                mBytesRead = mInput->stream->read(mInput->stream, buffer.raw, mInputBytes);
4918                                framesOut = 0;
4919                            } else {
4920                                mBytesRead = mInput->stream->read(mInput->stream, mRsmpInBuffer, mInputBytes);
4921                                mRsmpInIndex = 0;
4922                            }
4923                            if (mBytesRead < 0) {
4924                                ALOGE("Error reading audio input");
4925                                if (mActiveTrack->mState == TrackBase::ACTIVE) {
4926                                    // Force input into standby so that it tries to
4927                                    // recover at next read attempt
4928                                    mInput->stream->common.standby(&mInput->stream->common);
4929                                    usleep(kRecordThreadSleepUs);
4930                                }
4931                                mRsmpInIndex = mFrameCount;
4932                                framesOut = 0;
4933                                buffer.frameCount = 0;
4934                            }
4935                        }
4936                    }
4937                } else {
4938                    // resampling
4939
4940                    memset(mRsmpOutBuffer, 0, framesOut * 2 * sizeof(int32_t));
4941                    // alter output frame count as if we were expecting stereo samples
4942                    if (mChannelCount == 1 && mReqChannelCount == 1) {
4943                        framesOut >>= 1;
4944                    }
4945                    mResampler->resample(mRsmpOutBuffer, framesOut, this);
4946                    // ditherAndClamp() works as long as all buffers returned by mActiveTrack->getNextBuffer()
4947                    // are 32 bit aligned which should be always true.
4948                    if (mChannelCount == 2 && mReqChannelCount == 1) {
4949                        ditherAndClamp(mRsmpOutBuffer, mRsmpOutBuffer, framesOut);
4950                        // the resampler always outputs stereo samples: do post stereo to mono conversion
4951                        int16_t *src = (int16_t *)mRsmpOutBuffer;
4952                        int16_t *dst = buffer.i16;
4953                        while (framesOut--) {
4954                            *dst++ = (int16_t)(((int32_t)*src + (int32_t)*(src + 1)) >> 1);
4955                            src += 2;
4956                        }
4957                    } else {
4958                        ditherAndClamp((int32_t *)buffer.raw, mRsmpOutBuffer, framesOut);
4959                    }
4960
4961                }
4962                mActiveTrack->releaseBuffer(&buffer);
4963                mActiveTrack->overflow();
4964            }
4965            // client isn't retrieving buffers fast enough
4966            else {
4967                if (!mActiveTrack->setOverflow()) {
4968                    nsecs_t now = systemTime();
4969                    if ((now - lastWarning) > kWarningThrottleNs) {
4970                        ALOGW("RecordThread: buffer overflow");
4971                        lastWarning = now;
4972                    }
4973                }
4974                // Release the processor for a while before asking for a new buffer.
4975                // This will give the application more chance to read from the buffer and
4976                // clear the overflow.
4977                usleep(kRecordThreadSleepUs);
4978            }
4979        }
4980        // enable changes in effect chain
4981        unlockEffectChains(effectChains);
4982        effectChains.clear();
4983    }
4984
4985    if (!mStandby) {
4986        mInput->stream->common.standby(&mInput->stream->common);
4987    }
4988    mActiveTrack.clear();
4989
4990    mStartStopCond.broadcast();
4991
4992    releaseWakeLock();
4993
4994    ALOGV("RecordThread %p exiting", this);
4995    return false;
4996}
4997
4998
4999sp<AudioFlinger::RecordThread::RecordTrack>  AudioFlinger::RecordThread::createRecordTrack_l(
5000        const sp<AudioFlinger::Client>& client,
5001        uint32_t sampleRate,
5002        audio_format_t format,
5003        int channelMask,
5004        int frameCount,
5005        int sessionId,
5006        status_t *status)
5007{
5008    sp<RecordTrack> track;
5009    status_t lStatus;
5010
5011    lStatus = initCheck();
5012    if (lStatus != NO_ERROR) {
5013        ALOGE("Audio driver not initialized.");
5014        goto Exit;
5015    }
5016
5017    { // scope for mLock
5018        Mutex::Autolock _l(mLock);
5019
5020        track = new RecordTrack(this, client, sampleRate,
5021                      format, channelMask, frameCount, sessionId);
5022
5023        if (track->getCblk() == 0) {
5024            lStatus = NO_MEMORY;
5025            goto Exit;
5026        }
5027
5028        mTrack = track.get();
5029        // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
5030        bool suspend = audio_is_bluetooth_sco_device(
5031                (audio_devices_t)(mDevice & AUDIO_DEVICE_IN_ALL)) && mAudioFlinger->btNrecIsOff();
5032        setEffectSuspended_l(FX_IID_AEC, suspend, sessionId);
5033        setEffectSuspended_l(FX_IID_NS, suspend, sessionId);
5034    }
5035    lStatus = NO_ERROR;
5036
5037Exit:
5038    if (status) {
5039        *status = lStatus;
5040    }
5041    return track;
5042}
5043
5044status_t AudioFlinger::RecordThread::start(RecordThread::RecordTrack* recordTrack, pid_t tid)
5045{
5046    ALOGV("RecordThread::start tid=%d", tid);
5047    sp <ThreadBase> strongMe = this;
5048    status_t status = NO_ERROR;
5049    {
5050        AutoMutex lock(mLock);
5051        if (mActiveTrack != 0) {
5052            if (recordTrack != mActiveTrack.get()) {
5053                status = -EBUSY;
5054            } else if (mActiveTrack->mState == TrackBase::PAUSING) {
5055                mActiveTrack->mState = TrackBase::ACTIVE;
5056            }
5057            return status;
5058        }
5059
5060        recordTrack->mState = TrackBase::IDLE;
5061        mActiveTrack = recordTrack;
5062        mLock.unlock();
5063        status_t status = AudioSystem::startInput(mId);
5064        mLock.lock();
5065        if (status != NO_ERROR) {
5066            mActiveTrack.clear();
5067            return status;
5068        }
5069        mRsmpInIndex = mFrameCount;
5070        mBytesRead = 0;
5071        if (mResampler != NULL) {
5072            mResampler->reset();
5073        }
5074        mActiveTrack->mState = TrackBase::RESUMING;
5075        // signal thread to start
5076        ALOGV("Signal record thread");
5077        mWaitWorkCV.signal();
5078        // do not wait for mStartStopCond if exiting
5079        if (exitPending()) {
5080            mActiveTrack.clear();
5081            status = INVALID_OPERATION;
5082            goto startError;
5083        }
5084        mStartStopCond.wait(mLock);
5085        if (mActiveTrack == 0) {
5086            ALOGV("Record failed to start");
5087            status = BAD_VALUE;
5088            goto startError;
5089        }
5090        ALOGV("Record started OK");
5091        return status;
5092    }
5093startError:
5094    AudioSystem::stopInput(mId);
5095    return status;
5096}
5097
5098void AudioFlinger::RecordThread::stop(RecordThread::RecordTrack* recordTrack) {
5099    ALOGV("RecordThread::stop");
5100    sp <ThreadBase> strongMe = this;
5101    {
5102        AutoMutex lock(mLock);
5103        if (mActiveTrack != 0 && recordTrack == mActiveTrack.get()) {
5104            mActiveTrack->mState = TrackBase::PAUSING;
5105            // do not wait for mStartStopCond if exiting
5106            if (exitPending()) {
5107                return;
5108            }
5109            mStartStopCond.wait(mLock);
5110            // if we have been restarted, recordTrack == mActiveTrack.get() here
5111            if (mActiveTrack == 0 || recordTrack != mActiveTrack.get()) {
5112                mLock.unlock();
5113                AudioSystem::stopInput(mId);
5114                mLock.lock();
5115                ALOGV("Record stopped OK");
5116            }
5117        }
5118    }
5119}
5120
5121status_t AudioFlinger::RecordThread::dump(int fd, const Vector<String16>& args)
5122{
5123    const size_t SIZE = 256;
5124    char buffer[SIZE];
5125    String8 result;
5126
5127    snprintf(buffer, SIZE, "\nInput thread %p internals\n", this);
5128    result.append(buffer);
5129
5130    if (mActiveTrack != 0) {
5131        result.append("Active Track:\n");
5132        result.append("   Clien Fmt Chn mask   Session Buf  S SRate  Serv     User\n");
5133        mActiveTrack->dump(buffer, SIZE);
5134        result.append(buffer);
5135
5136        snprintf(buffer, SIZE, "In index: %d\n", mRsmpInIndex);
5137        result.append(buffer);
5138        snprintf(buffer, SIZE, "In size: %d\n", mInputBytes);
5139        result.append(buffer);
5140        snprintf(buffer, SIZE, "Resampling: %d\n", (mResampler != NULL));
5141        result.append(buffer);
5142        snprintf(buffer, SIZE, "Out channel count: %d\n", mReqChannelCount);
5143        result.append(buffer);
5144        snprintf(buffer, SIZE, "Out sample rate: %d\n", mReqSampleRate);
5145        result.append(buffer);
5146
5147
5148    } else {
5149        result.append("No record client\n");
5150    }
5151    write(fd, result.string(), result.size());
5152
5153    dumpBase(fd, args);
5154    dumpEffectChains(fd, args);
5155
5156    return NO_ERROR;
5157}
5158
5159status_t AudioFlinger::RecordThread::getNextBuffer(AudioBufferProvider::Buffer* buffer, int64_t pts)
5160{
5161    size_t framesReq = buffer->frameCount;
5162    size_t framesReady = mFrameCount - mRsmpInIndex;
5163    int channelCount;
5164
5165    if (framesReady == 0) {
5166        mBytesRead = mInput->stream->read(mInput->stream, mRsmpInBuffer, mInputBytes);
5167        if (mBytesRead < 0) {
5168            ALOGE("RecordThread::getNextBuffer() Error reading audio input");
5169            if (mActiveTrack->mState == TrackBase::ACTIVE) {
5170                // Force input into standby so that it tries to
5171                // recover at next read attempt
5172                mInput->stream->common.standby(&mInput->stream->common);
5173                usleep(kRecordThreadSleepUs);
5174            }
5175            buffer->raw = NULL;
5176            buffer->frameCount = 0;
5177            return NOT_ENOUGH_DATA;
5178        }
5179        mRsmpInIndex = 0;
5180        framesReady = mFrameCount;
5181    }
5182
5183    if (framesReq > framesReady) {
5184        framesReq = framesReady;
5185    }
5186
5187    if (mChannelCount == 1 && mReqChannelCount == 2) {
5188        channelCount = 1;
5189    } else {
5190        channelCount = 2;
5191    }
5192    buffer->raw = mRsmpInBuffer + mRsmpInIndex * channelCount;
5193    buffer->frameCount = framesReq;
5194    return NO_ERROR;
5195}
5196
5197void AudioFlinger::RecordThread::releaseBuffer(AudioBufferProvider::Buffer* buffer)
5198{
5199    mRsmpInIndex += buffer->frameCount;
5200    buffer->frameCount = 0;
5201}
5202
5203bool AudioFlinger::RecordThread::checkForNewParameters_l()
5204{
5205    bool reconfig = false;
5206
5207    while (!mNewParameters.isEmpty()) {
5208        status_t status = NO_ERROR;
5209        String8 keyValuePair = mNewParameters[0];
5210        AudioParameter param = AudioParameter(keyValuePair);
5211        int value;
5212        audio_format_t reqFormat = mFormat;
5213        int reqSamplingRate = mReqSampleRate;
5214        int reqChannelCount = mReqChannelCount;
5215
5216        if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) {
5217            reqSamplingRate = value;
5218            reconfig = true;
5219        }
5220        if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) {
5221            reqFormat = (audio_format_t) value;
5222            reconfig = true;
5223        }
5224        if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) {
5225            reqChannelCount = popcount(value);
5226            reconfig = true;
5227        }
5228        if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) {
5229            // do not accept frame count changes if tracks are open as the track buffer
5230            // size depends on frame count and correct behavior would not be guaranteed
5231            // if frame count is changed after track creation
5232            if (mActiveTrack != 0) {
5233                status = INVALID_OPERATION;
5234            } else {
5235                reconfig = true;
5236            }
5237        }
5238        if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) {
5239            // forward device change to effects that have requested to be
5240            // aware of attached audio device.
5241            for (size_t i = 0; i < mEffectChains.size(); i++) {
5242                mEffectChains[i]->setDevice_l(value);
5243            }
5244            // store input device and output device but do not forward output device to audio HAL.
5245            // Note that status is ignored by the caller for output device
5246            // (see AudioFlinger::setParameters()
5247            if (value & AUDIO_DEVICE_OUT_ALL) {
5248                mDevice &= (uint32_t)~(value & AUDIO_DEVICE_OUT_ALL);
5249                status = BAD_VALUE;
5250            } else {
5251                mDevice &= (uint32_t)~(value & AUDIO_DEVICE_IN_ALL);
5252                // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
5253                if (mTrack != NULL) {
5254                    bool suspend = audio_is_bluetooth_sco_device(
5255                            (audio_devices_t)value) && mAudioFlinger->btNrecIsOff();
5256                    setEffectSuspended_l(FX_IID_AEC, suspend, mTrack->sessionId());
5257                    setEffectSuspended_l(FX_IID_NS, suspend, mTrack->sessionId());
5258                }
5259            }
5260            mDevice |= (uint32_t)value;
5261        }
5262        if (status == NO_ERROR) {
5263            status = mInput->stream->common.set_parameters(&mInput->stream->common, keyValuePair.string());
5264            if (status == INVALID_OPERATION) {
5265               mInput->stream->common.standby(&mInput->stream->common);
5266               status = mInput->stream->common.set_parameters(&mInput->stream->common, keyValuePair.string());
5267            }
5268            if (reconfig) {
5269                if (status == BAD_VALUE &&
5270                    reqFormat == mInput->stream->common.get_format(&mInput->stream->common) &&
5271                    reqFormat == AUDIO_FORMAT_PCM_16_BIT &&
5272                    ((int)mInput->stream->common.get_sample_rate(&mInput->stream->common) <= (2 * reqSamplingRate)) &&
5273                    (popcount(mInput->stream->common.get_channels(&mInput->stream->common)) < 3) &&
5274                    (reqChannelCount < 3)) {
5275                    status = NO_ERROR;
5276                }
5277                if (status == NO_ERROR) {
5278                    readInputParameters();
5279                    sendConfigEvent_l(AudioSystem::INPUT_CONFIG_CHANGED);
5280                }
5281            }
5282        }
5283
5284        mNewParameters.removeAt(0);
5285
5286        mParamStatus = status;
5287        mParamCond.signal();
5288        // wait for condition with time out in case the thread calling ThreadBase::setParameters()
5289        // already timed out waiting for the status and will never signal the condition.
5290        mWaitWorkCV.waitRelative(mLock, kSetParametersTimeoutNs);
5291    }
5292    return reconfig;
5293}
5294
5295String8 AudioFlinger::RecordThread::getParameters(const String8& keys)
5296{
5297    char *s;
5298    String8 out_s8 = String8();
5299
5300    Mutex::Autolock _l(mLock);
5301    if (initCheck() != NO_ERROR) {
5302        return out_s8;
5303    }
5304
5305    s = mInput->stream->common.get_parameters(&mInput->stream->common, keys.string());
5306    out_s8 = String8(s);
5307    free(s);
5308    return out_s8;
5309}
5310
5311void AudioFlinger::RecordThread::audioConfigChanged_l(int event, int param) {
5312    AudioSystem::OutputDescriptor desc;
5313    void *param2 = NULL;
5314
5315    switch (event) {
5316    case AudioSystem::INPUT_OPENED:
5317    case AudioSystem::INPUT_CONFIG_CHANGED:
5318        desc.channels = mChannelMask;
5319        desc.samplingRate = mSampleRate;
5320        desc.format = mFormat;
5321        desc.frameCount = mFrameCount;
5322        desc.latency = 0;
5323        param2 = &desc;
5324        break;
5325
5326    case AudioSystem::INPUT_CLOSED:
5327    default:
5328        break;
5329    }
5330    mAudioFlinger->audioConfigChanged_l(event, mId, param2);
5331}
5332
5333void AudioFlinger::RecordThread::readInputParameters()
5334{
5335    delete mRsmpInBuffer;
5336    // mRsmpInBuffer is always assigned a new[] below
5337    delete mRsmpOutBuffer;
5338    mRsmpOutBuffer = NULL;
5339    delete mResampler;
5340    mResampler = NULL;
5341
5342    mSampleRate = mInput->stream->common.get_sample_rate(&mInput->stream->common);
5343    mChannelMask = mInput->stream->common.get_channels(&mInput->stream->common);
5344    mChannelCount = (uint16_t)popcount(mChannelMask);
5345    mFormat = mInput->stream->common.get_format(&mInput->stream->common);
5346    mFrameSize = audio_stream_frame_size(&mInput->stream->common);
5347    mInputBytes = mInput->stream->common.get_buffer_size(&mInput->stream->common);
5348    mFrameCount = mInputBytes / mFrameSize;
5349    mRsmpInBuffer = new int16_t[mFrameCount * mChannelCount];
5350
5351    if (mSampleRate != mReqSampleRate && mChannelCount < 3 && mReqChannelCount < 3)
5352    {
5353        int channelCount;
5354         // optmization: if mono to mono, use the resampler in stereo to stereo mode to avoid
5355         // stereo to mono post process as the resampler always outputs stereo.
5356        if (mChannelCount == 1 && mReqChannelCount == 2) {
5357            channelCount = 1;
5358        } else {
5359            channelCount = 2;
5360        }
5361        mResampler = AudioResampler::create(16, channelCount, mReqSampleRate);
5362        mResampler->setSampleRate(mSampleRate);
5363        mResampler->setVolume(AudioMixer::UNITY_GAIN, AudioMixer::UNITY_GAIN);
5364        mRsmpOutBuffer = new int32_t[mFrameCount * 2];
5365
5366        // optmization: if mono to mono, alter input frame count as if we were inputing stereo samples
5367        if (mChannelCount == 1 && mReqChannelCount == 1) {
5368            mFrameCount >>= 1;
5369        }
5370
5371    }
5372    mRsmpInIndex = mFrameCount;
5373}
5374
5375unsigned int AudioFlinger::RecordThread::getInputFramesLost()
5376{
5377    Mutex::Autolock _l(mLock);
5378    if (initCheck() != NO_ERROR) {
5379        return 0;
5380    }
5381
5382    return mInput->stream->get_input_frames_lost(mInput->stream);
5383}
5384
5385uint32_t AudioFlinger::RecordThread::hasAudioSession(int sessionId)
5386{
5387    Mutex::Autolock _l(mLock);
5388    uint32_t result = 0;
5389    if (getEffectChain_l(sessionId) != 0) {
5390        result = EFFECT_SESSION;
5391    }
5392
5393    if (mTrack != NULL && sessionId == mTrack->sessionId()) {
5394        result |= TRACK_SESSION;
5395    }
5396
5397    return result;
5398}
5399
5400AudioFlinger::RecordThread::RecordTrack* AudioFlinger::RecordThread::track()
5401{
5402    Mutex::Autolock _l(mLock);
5403    return mTrack;
5404}
5405
5406AudioFlinger::AudioStreamIn* AudioFlinger::RecordThread::getInput() const
5407{
5408    Mutex::Autolock _l(mLock);
5409    return mInput;
5410}
5411
5412AudioFlinger::AudioStreamIn* AudioFlinger::RecordThread::clearInput()
5413{
5414    Mutex::Autolock _l(mLock);
5415    AudioStreamIn *input = mInput;
5416    mInput = NULL;
5417    return input;
5418}
5419
5420// this method must always be called either with ThreadBase mLock held or inside the thread loop
5421audio_stream_t* AudioFlinger::RecordThread::stream()
5422{
5423    if (mInput == NULL) {
5424        return NULL;
5425    }
5426    return &mInput->stream->common;
5427}
5428
5429
5430// ----------------------------------------------------------------------------
5431
5432audio_io_handle_t AudioFlinger::openOutput(uint32_t *pDevices,
5433                                uint32_t *pSamplingRate,
5434                                audio_format_t *pFormat,
5435                                uint32_t *pChannels,
5436                                uint32_t *pLatencyMs,
5437                                uint32_t flags)
5438{
5439    status_t status;
5440    PlaybackThread *thread = NULL;
5441    mHardwareStatus = AUDIO_HW_OUTPUT_OPEN;
5442    uint32_t samplingRate = pSamplingRate ? *pSamplingRate : 0;
5443    audio_format_t format = pFormat ? *pFormat : AUDIO_FORMAT_DEFAULT;
5444    uint32_t channels = pChannels ? *pChannels : 0;
5445    uint32_t latency = pLatencyMs ? *pLatencyMs : 0;
5446    audio_stream_out_t *outStream;
5447    audio_hw_device_t *outHwDev;
5448
5449    ALOGV("openOutput(), Device %x, SamplingRate %d, Format %d, Channels %x, flags %x",
5450            pDevices ? *pDevices : 0,
5451            samplingRate,
5452            format,
5453            channels,
5454            flags);
5455
5456    if (pDevices == NULL || *pDevices == 0) {
5457        return 0;
5458    }
5459
5460    Mutex::Autolock _l(mLock);
5461
5462    outHwDev = findSuitableHwDev_l(*pDevices);
5463    if (outHwDev == NULL)
5464        return 0;
5465
5466    status = outHwDev->open_output_stream(outHwDev, *pDevices, &format,
5467                                          &channels, &samplingRate, &outStream);
5468    ALOGV("openOutput() openOutputStream returned output %p, SamplingRate %d, Format %d, Channels %x, status %d",
5469            outStream,
5470            samplingRate,
5471            format,
5472            channels,
5473            status);
5474
5475    mHardwareStatus = AUDIO_HW_IDLE;
5476    if (outStream != NULL) {
5477        AudioStreamOut *output = new AudioStreamOut(outHwDev, outStream);
5478        audio_io_handle_t id = nextUniqueId();
5479
5480        if ((flags & AUDIO_POLICY_OUTPUT_FLAG_DIRECT) ||
5481            (format != AUDIO_FORMAT_PCM_16_BIT) ||
5482            (channels != AUDIO_CHANNEL_OUT_STEREO)) {
5483            thread = new DirectOutputThread(this, output, id, *pDevices);
5484            ALOGV("openOutput() created direct output: ID %d thread %p", id, thread);
5485        } else {
5486            thread = new MixerThread(this, output, id, *pDevices);
5487            ALOGV("openOutput() created mixer output: ID %d thread %p", id, thread);
5488        }
5489        mPlaybackThreads.add(id, thread);
5490
5491        if (pSamplingRate != NULL) *pSamplingRate = samplingRate;
5492        if (pFormat != NULL) *pFormat = format;
5493        if (pChannels != NULL) *pChannels = channels;
5494        if (pLatencyMs != NULL) *pLatencyMs = thread->latency();
5495
5496        // notify client processes of the new output creation
5497        thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
5498        return id;
5499    }
5500
5501    return 0;
5502}
5503
5504audio_io_handle_t AudioFlinger::openDuplicateOutput(audio_io_handle_t output1,
5505        audio_io_handle_t output2)
5506{
5507    Mutex::Autolock _l(mLock);
5508    MixerThread *thread1 = checkMixerThread_l(output1);
5509    MixerThread *thread2 = checkMixerThread_l(output2);
5510
5511    if (thread1 == NULL || thread2 == NULL) {
5512        ALOGW("openDuplicateOutput() wrong output mixer type for output %d or %d", output1, output2);
5513        return 0;
5514    }
5515
5516    audio_io_handle_t id = nextUniqueId();
5517    DuplicatingThread *thread = new DuplicatingThread(this, thread1, id);
5518    thread->addOutputTrack(thread2);
5519    mPlaybackThreads.add(id, thread);
5520    // notify client processes of the new output creation
5521    thread->audioConfigChanged_l(AudioSystem::OUTPUT_OPENED);
5522    return id;
5523}
5524
5525status_t AudioFlinger::closeOutput(audio_io_handle_t output)
5526{
5527    // keep strong reference on the playback thread so that
5528    // it is not destroyed while exit() is executed
5529    sp <PlaybackThread> thread;
5530    {
5531        Mutex::Autolock _l(mLock);
5532        thread = checkPlaybackThread_l(output);
5533        if (thread == NULL) {
5534            return BAD_VALUE;
5535        }
5536
5537        ALOGV("closeOutput() %d", output);
5538
5539        if (thread->type() == ThreadBase::MIXER) {
5540            for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
5541                if (mPlaybackThreads.valueAt(i)->type() == ThreadBase::DUPLICATING) {
5542                    DuplicatingThread *dupThread = (DuplicatingThread *)mPlaybackThreads.valueAt(i).get();
5543                    dupThread->removeOutputTrack((MixerThread *)thread.get());
5544                }
5545            }
5546        }
5547        audioConfigChanged_l(AudioSystem::OUTPUT_CLOSED, output, NULL);
5548        mPlaybackThreads.removeItem(output);
5549    }
5550    thread->exit();
5551    // The thread entity (active unit of execution) is no longer running here,
5552    // but the ThreadBase container still exists.
5553
5554    if (thread->type() != ThreadBase::DUPLICATING) {
5555        AudioStreamOut *out = thread->clearOutput();
5556        assert(out != NULL);
5557        // from now on thread->mOutput is NULL
5558        out->hwDev->close_output_stream(out->hwDev, out->stream);
5559        delete out;
5560    }
5561    return NO_ERROR;
5562}
5563
5564status_t AudioFlinger::suspendOutput(audio_io_handle_t output)
5565{
5566    Mutex::Autolock _l(mLock);
5567    PlaybackThread *thread = checkPlaybackThread_l(output);
5568
5569    if (thread == NULL) {
5570        return BAD_VALUE;
5571    }
5572
5573    ALOGV("suspendOutput() %d", output);
5574    thread->suspend();
5575
5576    return NO_ERROR;
5577}
5578
5579status_t AudioFlinger::restoreOutput(audio_io_handle_t output)
5580{
5581    Mutex::Autolock _l(mLock);
5582    PlaybackThread *thread = checkPlaybackThread_l(output);
5583
5584    if (thread == NULL) {
5585        return BAD_VALUE;
5586    }
5587
5588    ALOGV("restoreOutput() %d", output);
5589
5590    thread->restore();
5591
5592    return NO_ERROR;
5593}
5594
5595audio_io_handle_t AudioFlinger::openInput(uint32_t *pDevices,
5596                                uint32_t *pSamplingRate,
5597                                audio_format_t *pFormat,
5598                                uint32_t *pChannels,
5599                                audio_in_acoustics_t acoustics)
5600{
5601    status_t status;
5602    RecordThread *thread = NULL;
5603    uint32_t samplingRate = pSamplingRate ? *pSamplingRate : 0;
5604    audio_format_t format = pFormat ? *pFormat : AUDIO_FORMAT_DEFAULT;
5605    uint32_t channels = pChannels ? *pChannels : 0;
5606    uint32_t reqSamplingRate = samplingRate;
5607    audio_format_t reqFormat = format;
5608    uint32_t reqChannels = channels;
5609    audio_stream_in_t *inStream;
5610    audio_hw_device_t *inHwDev;
5611
5612    if (pDevices == NULL || *pDevices == 0) {
5613        return 0;
5614    }
5615
5616    Mutex::Autolock _l(mLock);
5617
5618    inHwDev = findSuitableHwDev_l(*pDevices);
5619    if (inHwDev == NULL)
5620        return 0;
5621
5622    status = inHwDev->open_input_stream(inHwDev, *pDevices, &format,
5623                                        &channels, &samplingRate,
5624                                        acoustics,
5625                                        &inStream);
5626    ALOGV("openInput() openInputStream returned input %p, SamplingRate %d, Format %d, Channels %x, acoustics %x, status %d",
5627            inStream,
5628            samplingRate,
5629            format,
5630            channels,
5631            acoustics,
5632            status);
5633
5634    // If the input could not be opened with the requested parameters and we can handle the conversion internally,
5635    // try to open again with the proposed parameters. The AudioFlinger can resample the input and do mono to stereo
5636    // or stereo to mono conversions on 16 bit PCM inputs.
5637    if (inStream == NULL && status == BAD_VALUE &&
5638        reqFormat == format && format == AUDIO_FORMAT_PCM_16_BIT &&
5639        (samplingRate <= 2 * reqSamplingRate) &&
5640        (popcount(channels) < 3) && (popcount(reqChannels) < 3)) {
5641        ALOGV("openInput() reopening with proposed sampling rate and channels");
5642        status = inHwDev->open_input_stream(inHwDev, *pDevices, &format,
5643                                            &channels, &samplingRate,
5644                                            acoustics,
5645                                            &inStream);
5646    }
5647
5648    if (inStream != NULL) {
5649        AudioStreamIn *input = new AudioStreamIn(inHwDev, inStream);
5650
5651        audio_io_handle_t id = nextUniqueId();
5652        // Start record thread
5653        // RecorThread require both input and output device indication to forward to audio
5654        // pre processing modules
5655        uint32_t device = (*pDevices) | primaryOutputDevice_l();
5656        thread = new RecordThread(this,
5657                                  input,
5658                                  reqSamplingRate,
5659                                  reqChannels,
5660                                  id,
5661                                  device);
5662        mRecordThreads.add(id, thread);
5663        ALOGV("openInput() created record thread: ID %d thread %p", id, thread);
5664        if (pSamplingRate != NULL) *pSamplingRate = reqSamplingRate;
5665        if (pFormat != NULL) *pFormat = format;
5666        if (pChannels != NULL) *pChannels = reqChannels;
5667
5668        input->stream->common.standby(&input->stream->common);
5669
5670        // notify client processes of the new input creation
5671        thread->audioConfigChanged_l(AudioSystem::INPUT_OPENED);
5672        return id;
5673    }
5674
5675    return 0;
5676}
5677
5678status_t AudioFlinger::closeInput(audio_io_handle_t input)
5679{
5680    // keep strong reference on the record thread so that
5681    // it is not destroyed while exit() is executed
5682    sp <RecordThread> thread;
5683    {
5684        Mutex::Autolock _l(mLock);
5685        thread = checkRecordThread_l(input);
5686        if (thread == NULL) {
5687            return BAD_VALUE;
5688        }
5689
5690        ALOGV("closeInput() %d", input);
5691        audioConfigChanged_l(AudioSystem::INPUT_CLOSED, input, NULL);
5692        mRecordThreads.removeItem(input);
5693    }
5694    thread->exit();
5695    // The thread entity (active unit of execution) is no longer running here,
5696    // but the ThreadBase container still exists.
5697
5698    AudioStreamIn *in = thread->clearInput();
5699    assert(in != NULL);
5700    // from now on thread->mInput is NULL
5701    in->hwDev->close_input_stream(in->hwDev, in->stream);
5702    delete in;
5703
5704    return NO_ERROR;
5705}
5706
5707status_t AudioFlinger::setStreamOutput(audio_stream_type_t stream, audio_io_handle_t output)
5708{
5709    Mutex::Autolock _l(mLock);
5710    MixerThread *dstThread = checkMixerThread_l(output);
5711    if (dstThread == NULL) {
5712        ALOGW("setStreamOutput() bad output id %d", output);
5713        return BAD_VALUE;
5714    }
5715
5716    ALOGV("setStreamOutput() stream %d to output %d", stream, output);
5717    audioConfigChanged_l(AudioSystem::STREAM_CONFIG_CHANGED, output, &stream);
5718
5719    dstThread->setStreamValid(stream, true);
5720
5721    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
5722        PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
5723        if (thread != dstThread && thread->type() != ThreadBase::DIRECT) {
5724            MixerThread *srcThread = (MixerThread *)thread;
5725            srcThread->setStreamValid(stream, false);
5726            srcThread->invalidateTracks(stream);
5727        }
5728    }
5729
5730    return NO_ERROR;
5731}
5732
5733
5734int AudioFlinger::newAudioSessionId()
5735{
5736    return nextUniqueId();
5737}
5738
5739void AudioFlinger::acquireAudioSessionId(int audioSession)
5740{
5741    Mutex::Autolock _l(mLock);
5742    pid_t caller = IPCThreadState::self()->getCallingPid();
5743    ALOGV("acquiring %d from %d", audioSession, caller);
5744    size_t num = mAudioSessionRefs.size();
5745    for (size_t i = 0; i< num; i++) {
5746        AudioSessionRef *ref = mAudioSessionRefs.editItemAt(i);
5747        if (ref->sessionid == audioSession && ref->pid == caller) {
5748            ref->cnt++;
5749            ALOGV(" incremented refcount to %d", ref->cnt);
5750            return;
5751        }
5752    }
5753    mAudioSessionRefs.push(new AudioSessionRef(audioSession, caller));
5754    ALOGV(" added new entry for %d", audioSession);
5755}
5756
5757void AudioFlinger::releaseAudioSessionId(int audioSession)
5758{
5759    Mutex::Autolock _l(mLock);
5760    pid_t caller = IPCThreadState::self()->getCallingPid();
5761    ALOGV("releasing %d from %d", audioSession, caller);
5762    size_t num = mAudioSessionRefs.size();
5763    for (size_t i = 0; i< num; i++) {
5764        AudioSessionRef *ref = mAudioSessionRefs.itemAt(i);
5765        if (ref->sessionid == audioSession && ref->pid == caller) {
5766            ref->cnt--;
5767            ALOGV(" decremented refcount to %d", ref->cnt);
5768            if (ref->cnt == 0) {
5769                mAudioSessionRefs.removeAt(i);
5770                delete ref;
5771                purgeStaleEffects_l();
5772            }
5773            return;
5774        }
5775    }
5776    ALOGW("session id %d not found for pid %d", audioSession, caller);
5777}
5778
5779void AudioFlinger::purgeStaleEffects_l() {
5780
5781    ALOGV("purging stale effects");
5782
5783    Vector< sp<EffectChain> > chains;
5784
5785    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
5786        sp<PlaybackThread> t = mPlaybackThreads.valueAt(i);
5787        for (size_t j = 0; j < t->mEffectChains.size(); j++) {
5788            sp<EffectChain> ec = t->mEffectChains[j];
5789            if (ec->sessionId() > AUDIO_SESSION_OUTPUT_MIX) {
5790                chains.push(ec);
5791            }
5792        }
5793    }
5794    for (size_t i = 0; i < mRecordThreads.size(); i++) {
5795        sp<RecordThread> t = mRecordThreads.valueAt(i);
5796        for (size_t j = 0; j < t->mEffectChains.size(); j++) {
5797            sp<EffectChain> ec = t->mEffectChains[j];
5798            chains.push(ec);
5799        }
5800    }
5801
5802    for (size_t i = 0; i < chains.size(); i++) {
5803        sp<EffectChain> ec = chains[i];
5804        int sessionid = ec->sessionId();
5805        sp<ThreadBase> t = ec->mThread.promote();
5806        if (t == 0) {
5807            continue;
5808        }
5809        size_t numsessionrefs = mAudioSessionRefs.size();
5810        bool found = false;
5811        for (size_t k = 0; k < numsessionrefs; k++) {
5812            AudioSessionRef *ref = mAudioSessionRefs.itemAt(k);
5813            if (ref->sessionid == sessionid) {
5814                ALOGV(" session %d still exists for %d with %d refs",
5815                     sessionid, ref->pid, ref->cnt);
5816                found = true;
5817                break;
5818            }
5819        }
5820        if (!found) {
5821            // remove all effects from the chain
5822            while (ec->mEffects.size()) {
5823                sp<EffectModule> effect = ec->mEffects[0];
5824                effect->unPin();
5825                Mutex::Autolock _l (t->mLock);
5826                t->removeEffect_l(effect);
5827                for (size_t j = 0; j < effect->mHandles.size(); j++) {
5828                    sp<EffectHandle> handle = effect->mHandles[j].promote();
5829                    if (handle != 0) {
5830                        handle->mEffect.clear();
5831                        if (handle->mHasControl && handle->mEnabled) {
5832                            t->checkSuspendOnEffectEnabled_l(effect, false, effect->sessionId());
5833                        }
5834                    }
5835                }
5836                AudioSystem::unregisterEffect(effect->id());
5837            }
5838        }
5839    }
5840    return;
5841}
5842
5843// checkPlaybackThread_l() must be called with AudioFlinger::mLock held
5844AudioFlinger::PlaybackThread *AudioFlinger::checkPlaybackThread_l(audio_io_handle_t output) const
5845{
5846    return mPlaybackThreads.valueFor(output).get();
5847}
5848
5849// checkMixerThread_l() must be called with AudioFlinger::mLock held
5850AudioFlinger::MixerThread *AudioFlinger::checkMixerThread_l(audio_io_handle_t output) const
5851{
5852    PlaybackThread *thread = checkPlaybackThread_l(output);
5853    return thread != NULL && thread->type() != ThreadBase::DIRECT ? (MixerThread *) thread : NULL;
5854}
5855
5856// checkRecordThread_l() must be called with AudioFlinger::mLock held
5857AudioFlinger::RecordThread *AudioFlinger::checkRecordThread_l(audio_io_handle_t input) const
5858{
5859    return mRecordThreads.valueFor(input).get();
5860}
5861
5862uint32_t AudioFlinger::nextUniqueId()
5863{
5864    return android_atomic_inc(&mNextUniqueId);
5865}
5866
5867AudioFlinger::PlaybackThread *AudioFlinger::primaryPlaybackThread_l()
5868{
5869    for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
5870        PlaybackThread *thread = mPlaybackThreads.valueAt(i).get();
5871        AudioStreamOut *output = thread->getOutput();
5872        if (output != NULL && output->hwDev == mPrimaryHardwareDev) {
5873            return thread;
5874        }
5875    }
5876    return NULL;
5877}
5878
5879uint32_t AudioFlinger::primaryOutputDevice_l()
5880{
5881    PlaybackThread *thread = primaryPlaybackThread_l();
5882
5883    if (thread == NULL) {
5884        return 0;
5885    }
5886
5887    return thread->device();
5888}
5889
5890
5891// ----------------------------------------------------------------------------
5892//  Effect management
5893// ----------------------------------------------------------------------------
5894
5895
5896status_t AudioFlinger::queryNumberEffects(uint32_t *numEffects) const
5897{
5898    Mutex::Autolock _l(mLock);
5899    return EffectQueryNumberEffects(numEffects);
5900}
5901
5902status_t AudioFlinger::queryEffect(uint32_t index, effect_descriptor_t *descriptor) const
5903{
5904    Mutex::Autolock _l(mLock);
5905    return EffectQueryEffect(index, descriptor);
5906}
5907
5908status_t AudioFlinger::getEffectDescriptor(const effect_uuid_t *pUuid,
5909        effect_descriptor_t *descriptor) const
5910{
5911    Mutex::Autolock _l(mLock);
5912    return EffectGetDescriptor(pUuid, descriptor);
5913}
5914
5915
5916sp<IEffect> AudioFlinger::createEffect(pid_t pid,
5917        effect_descriptor_t *pDesc,
5918        const sp<IEffectClient>& effectClient,
5919        int32_t priority,
5920        audio_io_handle_t io,
5921        int sessionId,
5922        status_t *status,
5923        int *id,
5924        int *enabled)
5925{
5926    status_t lStatus = NO_ERROR;
5927    sp<EffectHandle> handle;
5928    effect_descriptor_t desc;
5929
5930    ALOGV("createEffect pid %d, effectClient %p, priority %d, sessionId %d, io %d",
5931            pid, effectClient.get(), priority, sessionId, io);
5932
5933    if (pDesc == NULL) {
5934        lStatus = BAD_VALUE;
5935        goto Exit;
5936    }
5937
5938    // check audio settings permission for global effects
5939    if (sessionId == AUDIO_SESSION_OUTPUT_MIX && !settingsAllowed()) {
5940        lStatus = PERMISSION_DENIED;
5941        goto Exit;
5942    }
5943
5944    // Session AUDIO_SESSION_OUTPUT_STAGE is reserved for output stage effects
5945    // that can only be created by audio policy manager (running in same process)
5946    if (sessionId == AUDIO_SESSION_OUTPUT_STAGE && getpid_cached != pid) {
5947        lStatus = PERMISSION_DENIED;
5948        goto Exit;
5949    }
5950
5951    if (io == 0) {
5952        if (sessionId == AUDIO_SESSION_OUTPUT_STAGE) {
5953            // output must be specified by AudioPolicyManager when using session
5954            // AUDIO_SESSION_OUTPUT_STAGE
5955            lStatus = BAD_VALUE;
5956            goto Exit;
5957        } else if (sessionId == AUDIO_SESSION_OUTPUT_MIX) {
5958            // if the output returned by getOutputForEffect() is removed before we lock the
5959            // mutex below, the call to checkPlaybackThread_l(io) below will detect it
5960            // and we will exit safely
5961            io = AudioSystem::getOutputForEffect(&desc);
5962        }
5963    }
5964
5965    {
5966        Mutex::Autolock _l(mLock);
5967
5968
5969        if (!EffectIsNullUuid(&pDesc->uuid)) {
5970            // if uuid is specified, request effect descriptor
5971            lStatus = EffectGetDescriptor(&pDesc->uuid, &desc);
5972            if (lStatus < 0) {
5973                ALOGW("createEffect() error %d from EffectGetDescriptor", lStatus);
5974                goto Exit;
5975            }
5976        } else {
5977            // if uuid is not specified, look for an available implementation
5978            // of the required type in effect factory
5979            if (EffectIsNullUuid(&pDesc->type)) {
5980                ALOGW("createEffect() no effect type");
5981                lStatus = BAD_VALUE;
5982                goto Exit;
5983            }
5984            uint32_t numEffects = 0;
5985            effect_descriptor_t d;
5986            d.flags = 0; // prevent compiler warning
5987            bool found = false;
5988
5989            lStatus = EffectQueryNumberEffects(&numEffects);
5990            if (lStatus < 0) {
5991                ALOGW("createEffect() error %d from EffectQueryNumberEffects", lStatus);
5992                goto Exit;
5993            }
5994            for (uint32_t i = 0; i < numEffects; i++) {
5995                lStatus = EffectQueryEffect(i, &desc);
5996                if (lStatus < 0) {
5997                    ALOGW("createEffect() error %d from EffectQueryEffect", lStatus);
5998                    continue;
5999                }
6000                if (memcmp(&desc.type, &pDesc->type, sizeof(effect_uuid_t)) == 0) {
6001                    // If matching type found save effect descriptor. If the session is
6002                    // 0 and the effect is not auxiliary, continue enumeration in case
6003                    // an auxiliary version of this effect type is available
6004                    found = true;
6005                    memcpy(&d, &desc, sizeof(effect_descriptor_t));
6006                    if (sessionId != AUDIO_SESSION_OUTPUT_MIX ||
6007                            (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
6008                        break;
6009                    }
6010                }
6011            }
6012            if (!found) {
6013                lStatus = BAD_VALUE;
6014                ALOGW("createEffect() effect not found");
6015                goto Exit;
6016            }
6017            // For same effect type, chose auxiliary version over insert version if
6018            // connect to output mix (Compliance to OpenSL ES)
6019            if (sessionId == AUDIO_SESSION_OUTPUT_MIX &&
6020                    (d.flags & EFFECT_FLAG_TYPE_MASK) != EFFECT_FLAG_TYPE_AUXILIARY) {
6021                memcpy(&desc, &d, sizeof(effect_descriptor_t));
6022            }
6023        }
6024
6025        // Do not allow auxiliary effects on a session different from 0 (output mix)
6026        if (sessionId != AUDIO_SESSION_OUTPUT_MIX &&
6027             (desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
6028            lStatus = INVALID_OPERATION;
6029            goto Exit;
6030        }
6031
6032        // check recording permission for visualizer
6033        if ((memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) &&
6034            !recordingAllowed()) {
6035            lStatus = PERMISSION_DENIED;
6036            goto Exit;
6037        }
6038
6039        // return effect descriptor
6040        memcpy(pDesc, &desc, sizeof(effect_descriptor_t));
6041
6042        // If output is not specified try to find a matching audio session ID in one of the
6043        // output threads.
6044        // If output is 0 here, sessionId is neither SESSION_OUTPUT_STAGE nor SESSION_OUTPUT_MIX
6045        // because of code checking output when entering the function.
6046        // Note: io is never 0 when creating an effect on an input
6047        if (io == 0) {
6048             // look for the thread where the specified audio session is present
6049            for (size_t i = 0; i < mPlaybackThreads.size(); i++) {
6050                if (mPlaybackThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
6051                    io = mPlaybackThreads.keyAt(i);
6052                    break;
6053                }
6054            }
6055            if (io == 0) {
6056               for (size_t i = 0; i < mRecordThreads.size(); i++) {
6057                   if (mRecordThreads.valueAt(i)->hasAudioSession(sessionId) != 0) {
6058                       io = mRecordThreads.keyAt(i);
6059                       break;
6060                   }
6061               }
6062            }
6063            // If no output thread contains the requested session ID, default to
6064            // first output. The effect chain will be moved to the correct output
6065            // thread when a track with the same session ID is created
6066            if (io == 0 && mPlaybackThreads.size()) {
6067                io = mPlaybackThreads.keyAt(0);
6068            }
6069            ALOGV("createEffect() got io %d for effect %s", io, desc.name);
6070        }
6071        ThreadBase *thread = checkRecordThread_l(io);
6072        if (thread == NULL) {
6073            thread = checkPlaybackThread_l(io);
6074            if (thread == NULL) {
6075                ALOGE("createEffect() unknown output thread");
6076                lStatus = BAD_VALUE;
6077                goto Exit;
6078            }
6079        }
6080
6081        sp<Client> client = registerPid_l(pid);
6082
6083        // create effect on selected output thread
6084        handle = thread->createEffect_l(client, effectClient, priority, sessionId,
6085                &desc, enabled, &lStatus);
6086        if (handle != 0 && id != NULL) {
6087            *id = handle->id();
6088        }
6089    }
6090
6091Exit:
6092    if(status) {
6093        *status = lStatus;
6094    }
6095    return handle;
6096}
6097
6098status_t AudioFlinger::moveEffects(int sessionId, audio_io_handle_t srcOutput,
6099        audio_io_handle_t dstOutput)
6100{
6101    ALOGV("moveEffects() session %d, srcOutput %d, dstOutput %d",
6102            sessionId, srcOutput, dstOutput);
6103    Mutex::Autolock _l(mLock);
6104    if (srcOutput == dstOutput) {
6105        ALOGW("moveEffects() same dst and src outputs %d", dstOutput);
6106        return NO_ERROR;
6107    }
6108    PlaybackThread *srcThread = checkPlaybackThread_l(srcOutput);
6109    if (srcThread == NULL) {
6110        ALOGW("moveEffects() bad srcOutput %d", srcOutput);
6111        return BAD_VALUE;
6112    }
6113    PlaybackThread *dstThread = checkPlaybackThread_l(dstOutput);
6114    if (dstThread == NULL) {
6115        ALOGW("moveEffects() bad dstOutput %d", dstOutput);
6116        return BAD_VALUE;
6117    }
6118
6119    Mutex::Autolock _dl(dstThread->mLock);
6120    Mutex::Autolock _sl(srcThread->mLock);
6121    moveEffectChain_l(sessionId, srcThread, dstThread, false);
6122
6123    return NO_ERROR;
6124}
6125
6126// moveEffectChain_l must be called with both srcThread and dstThread mLocks held
6127status_t AudioFlinger::moveEffectChain_l(int sessionId,
6128                                   AudioFlinger::PlaybackThread *srcThread,
6129                                   AudioFlinger::PlaybackThread *dstThread,
6130                                   bool reRegister)
6131{
6132    ALOGV("moveEffectChain_l() session %d from thread %p to thread %p",
6133            sessionId, srcThread, dstThread);
6134
6135    sp<EffectChain> chain = srcThread->getEffectChain_l(sessionId);
6136    if (chain == 0) {
6137        ALOGW("moveEffectChain_l() effect chain for session %d not on source thread %p",
6138                sessionId, srcThread);
6139        return INVALID_OPERATION;
6140    }
6141
6142    // remove chain first. This is useful only if reconfiguring effect chain on same output thread,
6143    // so that a new chain is created with correct parameters when first effect is added. This is
6144    // otherwise unnecessary as removeEffect_l() will remove the chain when last effect is
6145    // removed.
6146    srcThread->removeEffectChain_l(chain);
6147
6148    // transfer all effects one by one so that new effect chain is created on new thread with
6149    // correct buffer sizes and audio parameters and effect engines reconfigured accordingly
6150    audio_io_handle_t dstOutput = dstThread->id();
6151    sp<EffectChain> dstChain;
6152    uint32_t strategy = 0; // prevent compiler warning
6153    sp<EffectModule> effect = chain->getEffectFromId_l(0);
6154    while (effect != 0) {
6155        srcThread->removeEffect_l(effect);
6156        dstThread->addEffect_l(effect);
6157        // removeEffect_l() has stopped the effect if it was active so it must be restarted
6158        if (effect->state() == EffectModule::ACTIVE ||
6159                effect->state() == EffectModule::STOPPING) {
6160            effect->start();
6161        }
6162        // if the move request is not received from audio policy manager, the effect must be
6163        // re-registered with the new strategy and output
6164        if (dstChain == 0) {
6165            dstChain = effect->chain().promote();
6166            if (dstChain == 0) {
6167                ALOGW("moveEffectChain_l() cannot get chain from effect %p", effect.get());
6168                srcThread->addEffect_l(effect);
6169                return NO_INIT;
6170            }
6171            strategy = dstChain->strategy();
6172        }
6173        if (reRegister) {
6174            AudioSystem::unregisterEffect(effect->id());
6175            AudioSystem::registerEffect(&effect->desc(),
6176                                        dstOutput,
6177                                        strategy,
6178                                        sessionId,
6179                                        effect->id());
6180        }
6181        effect = chain->getEffectFromId_l(0);
6182    }
6183
6184    return NO_ERROR;
6185}
6186
6187
6188// PlaybackThread::createEffect_l() must be called with AudioFlinger::mLock held
6189sp<AudioFlinger::EffectHandle> AudioFlinger::ThreadBase::createEffect_l(
6190        const sp<AudioFlinger::Client>& client,
6191        const sp<IEffectClient>& effectClient,
6192        int32_t priority,
6193        int sessionId,
6194        effect_descriptor_t *desc,
6195        int *enabled,
6196        status_t *status
6197        )
6198{
6199    sp<EffectModule> effect;
6200    sp<EffectHandle> handle;
6201    status_t lStatus;
6202    sp<EffectChain> chain;
6203    bool chainCreated = false;
6204    bool effectCreated = false;
6205    bool effectRegistered = false;
6206
6207    lStatus = initCheck();
6208    if (lStatus != NO_ERROR) {
6209        ALOGW("createEffect_l() Audio driver not initialized.");
6210        goto Exit;
6211    }
6212
6213    // Do not allow effects with session ID 0 on direct output or duplicating threads
6214    // TODO: add rule for hw accelerated effects on direct outputs with non PCM format
6215    if (sessionId == AUDIO_SESSION_OUTPUT_MIX && mType != MIXER) {
6216        ALOGW("createEffect_l() Cannot add auxiliary effect %s to session %d",
6217                desc->name, sessionId);
6218        lStatus = BAD_VALUE;
6219        goto Exit;
6220    }
6221    // Only Pre processor effects are allowed on input threads and only on input threads
6222    if ((mType == RECORD) != ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC)) {
6223        ALOGW("createEffect_l() effect %s (flags %08x) created on wrong thread type %d",
6224                desc->name, desc->flags, mType);
6225        lStatus = BAD_VALUE;
6226        goto Exit;
6227    }
6228
6229    ALOGV("createEffect_l() thread %p effect %s on session %d", this, desc->name, sessionId);
6230
6231    { // scope for mLock
6232        Mutex::Autolock _l(mLock);
6233
6234        // check for existing effect chain with the requested audio session
6235        chain = getEffectChain_l(sessionId);
6236        if (chain == 0) {
6237            // create a new chain for this session
6238            ALOGV("createEffect_l() new effect chain for session %d", sessionId);
6239            chain = new EffectChain(this, sessionId);
6240            addEffectChain_l(chain);
6241            chain->setStrategy(getStrategyForSession_l(sessionId));
6242            chainCreated = true;
6243        } else {
6244            effect = chain->getEffectFromDesc_l(desc);
6245        }
6246
6247        ALOGV("createEffect_l() got effect %p on chain %p", effect.get(), chain.get());
6248
6249        if (effect == 0) {
6250            int id = mAudioFlinger->nextUniqueId();
6251            // Check CPU and memory usage
6252            lStatus = AudioSystem::registerEffect(desc, mId, chain->strategy(), sessionId, id);
6253            if (lStatus != NO_ERROR) {
6254                goto Exit;
6255            }
6256            effectRegistered = true;
6257            // create a new effect module if none present in the chain
6258            effect = new EffectModule(this, chain, desc, id, sessionId);
6259            lStatus = effect->status();
6260            if (lStatus != NO_ERROR) {
6261                goto Exit;
6262            }
6263            lStatus = chain->addEffect_l(effect);
6264            if (lStatus != NO_ERROR) {
6265                goto Exit;
6266            }
6267            effectCreated = true;
6268
6269            effect->setDevice(mDevice);
6270            effect->setMode(mAudioFlinger->getMode());
6271        }
6272        // create effect handle and connect it to effect module
6273        handle = new EffectHandle(effect, client, effectClient, priority);
6274        lStatus = effect->addHandle(handle);
6275        if (enabled != NULL) {
6276            *enabled = (int)effect->isEnabled();
6277        }
6278    }
6279
6280Exit:
6281    if (lStatus != NO_ERROR && lStatus != ALREADY_EXISTS) {
6282        Mutex::Autolock _l(mLock);
6283        if (effectCreated) {
6284            chain->removeEffect_l(effect);
6285        }
6286        if (effectRegistered) {
6287            AudioSystem::unregisterEffect(effect->id());
6288        }
6289        if (chainCreated) {
6290            removeEffectChain_l(chain);
6291        }
6292        handle.clear();
6293    }
6294
6295    if(status) {
6296        *status = lStatus;
6297    }
6298    return handle;
6299}
6300
6301sp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect_l(int sessionId, int effectId)
6302{
6303    sp<EffectChain> chain = getEffectChain_l(sessionId);
6304    return chain != 0 ? chain->getEffectFromId_l(effectId) : 0;
6305}
6306
6307// PlaybackThread::addEffect_l() must be called with AudioFlinger::mLock and
6308// PlaybackThread::mLock held
6309status_t AudioFlinger::ThreadBase::addEffect_l(const sp<EffectModule>& effect)
6310{
6311    // check for existing effect chain with the requested audio session
6312    int sessionId = effect->sessionId();
6313    sp<EffectChain> chain = getEffectChain_l(sessionId);
6314    bool chainCreated = false;
6315
6316    if (chain == 0) {
6317        // create a new chain for this session
6318        ALOGV("addEffect_l() new effect chain for session %d", sessionId);
6319        chain = new EffectChain(this, sessionId);
6320        addEffectChain_l(chain);
6321        chain->setStrategy(getStrategyForSession_l(sessionId));
6322        chainCreated = true;
6323    }
6324    ALOGV("addEffect_l() %p chain %p effect %p", this, chain.get(), effect.get());
6325
6326    if (chain->getEffectFromId_l(effect->id()) != 0) {
6327        ALOGW("addEffect_l() %p effect %s already present in chain %p",
6328                this, effect->desc().name, chain.get());
6329        return BAD_VALUE;
6330    }
6331
6332    status_t status = chain->addEffect_l(effect);
6333    if (status != NO_ERROR) {
6334        if (chainCreated) {
6335            removeEffectChain_l(chain);
6336        }
6337        return status;
6338    }
6339
6340    effect->setDevice(mDevice);
6341    effect->setMode(mAudioFlinger->getMode());
6342    return NO_ERROR;
6343}
6344
6345void AudioFlinger::ThreadBase::removeEffect_l(const sp<EffectModule>& effect) {
6346
6347    ALOGV("removeEffect_l() %p effect %p", this, effect.get());
6348    effect_descriptor_t desc = effect->desc();
6349    if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
6350        detachAuxEffect_l(effect->id());
6351    }
6352
6353    sp<EffectChain> chain = effect->chain().promote();
6354    if (chain != 0) {
6355        // remove effect chain if removing last effect
6356        if (chain->removeEffect_l(effect) == 0) {
6357            removeEffectChain_l(chain);
6358        }
6359    } else {
6360        ALOGW("removeEffect_l() %p cannot promote chain for effect %p", this, effect.get());
6361    }
6362}
6363
6364void AudioFlinger::ThreadBase::lockEffectChains_l(
6365        Vector<sp <AudioFlinger::EffectChain> >& effectChains)
6366{
6367    effectChains = mEffectChains;
6368    for (size_t i = 0; i < mEffectChains.size(); i++) {
6369        mEffectChains[i]->lock();
6370    }
6371}
6372
6373void AudioFlinger::ThreadBase::unlockEffectChains(
6374        Vector<sp <AudioFlinger::EffectChain> >& effectChains)
6375{
6376    for (size_t i = 0; i < effectChains.size(); i++) {
6377        effectChains[i]->unlock();
6378    }
6379}
6380
6381sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain(int sessionId)
6382{
6383    Mutex::Autolock _l(mLock);
6384    return getEffectChain_l(sessionId);
6385}
6386
6387sp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain_l(int sessionId)
6388{
6389    size_t size = mEffectChains.size();
6390    for (size_t i = 0; i < size; i++) {
6391        if (mEffectChains[i]->sessionId() == sessionId) {
6392            return mEffectChains[i];
6393        }
6394    }
6395    return 0;
6396}
6397
6398void AudioFlinger::ThreadBase::setMode(audio_mode_t mode)
6399{
6400    Mutex::Autolock _l(mLock);
6401    size_t size = mEffectChains.size();
6402    for (size_t i = 0; i < size; i++) {
6403        mEffectChains[i]->setMode_l(mode);
6404    }
6405}
6406
6407void AudioFlinger::ThreadBase::disconnectEffect(const sp<EffectModule>& effect,
6408                                                    const wp<EffectHandle>& handle,
6409                                                    bool unpinIfLast) {
6410
6411    Mutex::Autolock _l(mLock);
6412    ALOGV("disconnectEffect() %p effect %p", this, effect.get());
6413    // delete the effect module if removing last handle on it
6414    if (effect->removeHandle(handle) == 0) {
6415        if (!effect->isPinned() || unpinIfLast) {
6416            removeEffect_l(effect);
6417            AudioSystem::unregisterEffect(effect->id());
6418        }
6419    }
6420}
6421
6422status_t AudioFlinger::PlaybackThread::addEffectChain_l(const sp<EffectChain>& chain)
6423{
6424    int session = chain->sessionId();
6425    int16_t *buffer = mMixBuffer;
6426    bool ownsBuffer = false;
6427
6428    ALOGV("addEffectChain_l() %p on thread %p for session %d", chain.get(), this, session);
6429    if (session > 0) {
6430        // Only one effect chain can be present in direct output thread and it uses
6431        // the mix buffer as input
6432        if (mType != DIRECT) {
6433            size_t numSamples = mFrameCount * mChannelCount;
6434            buffer = new int16_t[numSamples];
6435            memset(buffer, 0, numSamples * sizeof(int16_t));
6436            ALOGV("addEffectChain_l() creating new input buffer %p session %d", buffer, session);
6437            ownsBuffer = true;
6438        }
6439
6440        // Attach all tracks with same session ID to this chain.
6441        for (size_t i = 0; i < mTracks.size(); ++i) {
6442            sp<Track> track = mTracks[i];
6443            if (session == track->sessionId()) {
6444                ALOGV("addEffectChain_l() track->setMainBuffer track %p buffer %p", track.get(), buffer);
6445                track->setMainBuffer(buffer);
6446                chain->incTrackCnt();
6447            }
6448        }
6449
6450        // indicate all active tracks in the chain
6451        for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) {
6452            sp<Track> track = mActiveTracks[i].promote();
6453            if (track == 0) continue;
6454            if (session == track->sessionId()) {
6455                ALOGV("addEffectChain_l() activating track %p on session %d", track.get(), session);
6456                chain->incActiveTrackCnt();
6457            }
6458        }
6459    }
6460
6461    chain->setInBuffer(buffer, ownsBuffer);
6462    chain->setOutBuffer(mMixBuffer);
6463    // Effect chain for session AUDIO_SESSION_OUTPUT_STAGE is inserted at end of effect
6464    // chains list in order to be processed last as it contains output stage effects
6465    // Effect chain for session AUDIO_SESSION_OUTPUT_MIX is inserted before
6466    // session AUDIO_SESSION_OUTPUT_STAGE to be processed
6467    // after track specific effects and before output stage
6468    // It is therefore mandatory that AUDIO_SESSION_OUTPUT_MIX == 0 and
6469    // that AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX
6470    // Effect chain for other sessions are inserted at beginning of effect
6471    // chains list to be processed before output mix effects. Relative order between other
6472    // sessions is not important
6473    size_t size = mEffectChains.size();
6474    size_t i = 0;
6475    for (i = 0; i < size; i++) {
6476        if (mEffectChains[i]->sessionId() < session) break;
6477    }
6478    mEffectChains.insertAt(chain, i);
6479    checkSuspendOnAddEffectChain_l(chain);
6480
6481    return NO_ERROR;
6482}
6483
6484size_t AudioFlinger::PlaybackThread::removeEffectChain_l(const sp<EffectChain>& chain)
6485{
6486    int session = chain->sessionId();
6487
6488    ALOGV("removeEffectChain_l() %p from thread %p for session %d", chain.get(), this, session);
6489
6490    for (size_t i = 0; i < mEffectChains.size(); i++) {
6491        if (chain == mEffectChains[i]) {
6492            mEffectChains.removeAt(i);
6493            // detach all active tracks from the chain
6494            for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) {
6495                sp<Track> track = mActiveTracks[i].promote();
6496                if (track == 0) continue;
6497                if (session == track->sessionId()) {
6498                    ALOGV("removeEffectChain_l(): stopping track on chain %p for session Id: %d",
6499                            chain.get(), session);
6500                    chain->decActiveTrackCnt();
6501                }
6502            }
6503
6504            // detach all tracks with same session ID from this chain
6505            for (size_t i = 0; i < mTracks.size(); ++i) {
6506                sp<Track> track = mTracks[i];
6507                if (session == track->sessionId()) {
6508                    track->setMainBuffer(mMixBuffer);
6509                    chain->decTrackCnt();
6510                }
6511            }
6512            break;
6513        }
6514    }
6515    return mEffectChains.size();
6516}
6517
6518status_t AudioFlinger::PlaybackThread::attachAuxEffect(
6519        const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId)
6520{
6521    Mutex::Autolock _l(mLock);
6522    return attachAuxEffect_l(track, EffectId);
6523}
6524
6525status_t AudioFlinger::PlaybackThread::attachAuxEffect_l(
6526        const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId)
6527{
6528    status_t status = NO_ERROR;
6529
6530    if (EffectId == 0) {
6531        track->setAuxBuffer(0, NULL);
6532    } else {
6533        // Auxiliary effects are always in audio session AUDIO_SESSION_OUTPUT_MIX
6534        sp<EffectModule> effect = getEffect_l(AUDIO_SESSION_OUTPUT_MIX, EffectId);
6535        if (effect != 0) {
6536            if ((effect->desc().flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
6537                track->setAuxBuffer(EffectId, (int32_t *)effect->inBuffer());
6538            } else {
6539                status = INVALID_OPERATION;
6540            }
6541        } else {
6542            status = BAD_VALUE;
6543        }
6544    }
6545    return status;
6546}
6547
6548void AudioFlinger::PlaybackThread::detachAuxEffect_l(int effectId)
6549{
6550     for (size_t i = 0; i < mTracks.size(); ++i) {
6551        sp<Track> track = mTracks[i];
6552        if (track->auxEffectId() == effectId) {
6553            attachAuxEffect_l(track, 0);
6554        }
6555    }
6556}
6557
6558status_t AudioFlinger::RecordThread::addEffectChain_l(const sp<EffectChain>& chain)
6559{
6560    // only one chain per input thread
6561    if (mEffectChains.size() != 0) {
6562        return INVALID_OPERATION;
6563    }
6564    ALOGV("addEffectChain_l() %p on thread %p", chain.get(), this);
6565
6566    chain->setInBuffer(NULL);
6567    chain->setOutBuffer(NULL);
6568
6569    checkSuspendOnAddEffectChain_l(chain);
6570
6571    mEffectChains.add(chain);
6572
6573    return NO_ERROR;
6574}
6575
6576size_t AudioFlinger::RecordThread::removeEffectChain_l(const sp<EffectChain>& chain)
6577{
6578    ALOGV("removeEffectChain_l() %p from thread %p", chain.get(), this);
6579    ALOGW_IF(mEffectChains.size() != 1,
6580            "removeEffectChain_l() %p invalid chain size %d on thread %p",
6581            chain.get(), mEffectChains.size(), this);
6582    if (mEffectChains.size() == 1) {
6583        mEffectChains.removeAt(0);
6584    }
6585    return 0;
6586}
6587
6588// ----------------------------------------------------------------------------
6589//  EffectModule implementation
6590// ----------------------------------------------------------------------------
6591
6592#undef LOG_TAG
6593#define LOG_TAG "AudioFlinger::EffectModule"
6594
6595AudioFlinger::EffectModule::EffectModule(ThreadBase *thread,
6596                                        const wp<AudioFlinger::EffectChain>& chain,
6597                                        effect_descriptor_t *desc,
6598                                        int id,
6599                                        int sessionId)
6600    : mThread(thread), mChain(chain), mId(id), mSessionId(sessionId), mEffectInterface(NULL),
6601      mStatus(NO_INIT), mState(IDLE), mSuspended(false)
6602{
6603    ALOGV("Constructor %p", this);
6604    int lStatus;
6605    if (thread == NULL) {
6606        return;
6607    }
6608
6609    memcpy(&mDescriptor, desc, sizeof(effect_descriptor_t));
6610
6611    // create effect engine from effect factory
6612    mStatus = EffectCreate(&desc->uuid, sessionId, thread->id(), &mEffectInterface);
6613
6614    if (mStatus != NO_ERROR) {
6615        return;
6616    }
6617    lStatus = init();
6618    if (lStatus < 0) {
6619        mStatus = lStatus;
6620        goto Error;
6621    }
6622
6623    if (mSessionId > AUDIO_SESSION_OUTPUT_MIX) {
6624        mPinned = true;
6625    }
6626    ALOGV("Constructor success name %s, Interface %p", mDescriptor.name, mEffectInterface);
6627    return;
6628Error:
6629    EffectRelease(mEffectInterface);
6630    mEffectInterface = NULL;
6631    ALOGV("Constructor Error %d", mStatus);
6632}
6633
6634AudioFlinger::EffectModule::~EffectModule()
6635{
6636    ALOGV("Destructor %p", this);
6637    if (mEffectInterface != NULL) {
6638        if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
6639                (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC) {
6640            sp<ThreadBase> thread = mThread.promote();
6641            if (thread != 0) {
6642                audio_stream_t *stream = thread->stream();
6643                if (stream != NULL) {
6644                    stream->remove_audio_effect(stream, mEffectInterface);
6645                }
6646            }
6647        }
6648        // release effect engine
6649        EffectRelease(mEffectInterface);
6650    }
6651}
6652
6653status_t AudioFlinger::EffectModule::addHandle(const sp<EffectHandle>& handle)
6654{
6655    status_t status;
6656
6657    Mutex::Autolock _l(mLock);
6658    int priority = handle->priority();
6659    size_t size = mHandles.size();
6660    sp<EffectHandle> h;
6661    size_t i;
6662    for (i = 0; i < size; i++) {
6663        h = mHandles[i].promote();
6664        if (h == 0) continue;
6665        if (h->priority() <= priority) break;
6666    }
6667    // if inserted in first place, move effect control from previous owner to this handle
6668    if (i == 0) {
6669        bool enabled = false;
6670        if (h != 0) {
6671            enabled = h->enabled();
6672            h->setControl(false/*hasControl*/, true /*signal*/, enabled /*enabled*/);
6673        }
6674        handle->setControl(true /*hasControl*/, false /*signal*/, enabled /*enabled*/);
6675        status = NO_ERROR;
6676    } else {
6677        status = ALREADY_EXISTS;
6678    }
6679    ALOGV("addHandle() %p added handle %p in position %d", this, handle.get(), i);
6680    mHandles.insertAt(handle, i);
6681    return status;
6682}
6683
6684size_t AudioFlinger::EffectModule::removeHandle(const wp<EffectHandle>& handle)
6685{
6686    Mutex::Autolock _l(mLock);
6687    size_t size = mHandles.size();
6688    size_t i;
6689    for (i = 0; i < size; i++) {
6690        if (mHandles[i] == handle) break;
6691    }
6692    if (i == size) {
6693        return size;
6694    }
6695    ALOGV("removeHandle() %p removed handle %p in position %d", this, handle.unsafe_get(), i);
6696
6697    bool enabled = false;
6698    EffectHandle *hdl = handle.unsafe_get();
6699    if (hdl != NULL) {
6700        ALOGV("removeHandle() unsafe_get OK");
6701        enabled = hdl->enabled();
6702    }
6703    mHandles.removeAt(i);
6704    size = mHandles.size();
6705    // if removed from first place, move effect control from this handle to next in line
6706    if (i == 0 && size != 0) {
6707        sp<EffectHandle> h = mHandles[0].promote();
6708        if (h != 0) {
6709            h->setControl(true /*hasControl*/, true /*signal*/ , enabled /*enabled*/);
6710        }
6711    }
6712
6713    // Prevent calls to process() and other functions on effect interface from now on.
6714    // The effect engine will be released by the destructor when the last strong reference on
6715    // this object is released which can happen after next process is called.
6716    if (size == 0 && !mPinned) {
6717        mState = DESTROYED;
6718    }
6719
6720    return size;
6721}
6722
6723sp<AudioFlinger::EffectHandle> AudioFlinger::EffectModule::controlHandle()
6724{
6725    Mutex::Autolock _l(mLock);
6726    return mHandles.size() != 0 ? mHandles[0].promote() : 0;
6727}
6728
6729void AudioFlinger::EffectModule::disconnect(const wp<EffectHandle>& handle, bool unpinIfLast)
6730{
6731    ALOGV("disconnect() %p handle %p", this, handle.unsafe_get());
6732    // keep a strong reference on this EffectModule to avoid calling the
6733    // destructor before we exit
6734    sp<EffectModule> keep(this);
6735    {
6736        sp<ThreadBase> thread = mThread.promote();
6737        if (thread != 0) {
6738            thread->disconnectEffect(keep, handle, unpinIfLast);
6739        }
6740    }
6741}
6742
6743void AudioFlinger::EffectModule::updateState() {
6744    Mutex::Autolock _l(mLock);
6745
6746    switch (mState) {
6747    case RESTART:
6748        reset_l();
6749        // FALL THROUGH
6750
6751    case STARTING:
6752        // clear auxiliary effect input buffer for next accumulation
6753        if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
6754            memset(mConfig.inputCfg.buffer.raw,
6755                   0,
6756                   mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
6757        }
6758        start_l();
6759        mState = ACTIVE;
6760        break;
6761    case STOPPING:
6762        stop_l();
6763        mDisableWaitCnt = mMaxDisableWaitCnt;
6764        mState = STOPPED;
6765        break;
6766    case STOPPED:
6767        // mDisableWaitCnt is forced to 1 by process() when the engine indicates the end of the
6768        // turn off sequence.
6769        if (--mDisableWaitCnt == 0) {
6770            reset_l();
6771            mState = IDLE;
6772        }
6773        break;
6774    default: //IDLE , ACTIVE, DESTROYED
6775        break;
6776    }
6777}
6778
6779void AudioFlinger::EffectModule::process()
6780{
6781    Mutex::Autolock _l(mLock);
6782
6783    if (mState == DESTROYED || mEffectInterface == NULL ||
6784            mConfig.inputCfg.buffer.raw == NULL ||
6785            mConfig.outputCfg.buffer.raw == NULL) {
6786        return;
6787    }
6788
6789    if (isProcessEnabled()) {
6790        // do 32 bit to 16 bit conversion for auxiliary effect input buffer
6791        if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
6792            ditherAndClamp(mConfig.inputCfg.buffer.s32,
6793                                        mConfig.inputCfg.buffer.s32,
6794                                        mConfig.inputCfg.buffer.frameCount/2);
6795        }
6796
6797        // do the actual processing in the effect engine
6798        int ret = (*mEffectInterface)->process(mEffectInterface,
6799                                               &mConfig.inputCfg.buffer,
6800                                               &mConfig.outputCfg.buffer);
6801
6802        // force transition to IDLE state when engine is ready
6803        if (mState == STOPPED && ret == -ENODATA) {
6804            mDisableWaitCnt = 1;
6805        }
6806
6807        // clear auxiliary effect input buffer for next accumulation
6808        if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
6809            memset(mConfig.inputCfg.buffer.raw, 0,
6810                   mConfig.inputCfg.buffer.frameCount*sizeof(int32_t));
6811        }
6812    } else if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_INSERT &&
6813                mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
6814        // If an insert effect is idle and input buffer is different from output buffer,
6815        // accumulate input onto output
6816        sp<EffectChain> chain = mChain.promote();
6817        if (chain != 0 && chain->activeTrackCnt() != 0) {
6818            size_t frameCnt = mConfig.inputCfg.buffer.frameCount * 2;  //always stereo here
6819            int16_t *in = mConfig.inputCfg.buffer.s16;
6820            int16_t *out = mConfig.outputCfg.buffer.s16;
6821            for (size_t i = 0; i < frameCnt; i++) {
6822                out[i] = clamp16((int32_t)out[i] + (int32_t)in[i]);
6823            }
6824        }
6825    }
6826}
6827
6828void AudioFlinger::EffectModule::reset_l()
6829{
6830    if (mEffectInterface == NULL) {
6831        return;
6832    }
6833    (*mEffectInterface)->command(mEffectInterface, EFFECT_CMD_RESET, 0, NULL, 0, NULL);
6834}
6835
6836status_t AudioFlinger::EffectModule::configure()
6837{
6838    uint32_t channels;
6839    if (mEffectInterface == NULL) {
6840        return NO_INIT;
6841    }
6842
6843    sp<ThreadBase> thread = mThread.promote();
6844    if (thread == 0) {
6845        return DEAD_OBJECT;
6846    }
6847
6848    // TODO: handle configuration of effects replacing track process
6849    if (thread->channelCount() == 1) {
6850        channels = AUDIO_CHANNEL_OUT_MONO;
6851    } else {
6852        channels = AUDIO_CHANNEL_OUT_STEREO;
6853    }
6854
6855    if ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
6856        mConfig.inputCfg.channels = AUDIO_CHANNEL_OUT_MONO;
6857    } else {
6858        mConfig.inputCfg.channels = channels;
6859    }
6860    mConfig.outputCfg.channels = channels;
6861    mConfig.inputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
6862    mConfig.outputCfg.format = AUDIO_FORMAT_PCM_16_BIT;
6863    mConfig.inputCfg.samplingRate = thread->sampleRate();
6864    mConfig.outputCfg.samplingRate = mConfig.inputCfg.samplingRate;
6865    mConfig.inputCfg.bufferProvider.cookie = NULL;
6866    mConfig.inputCfg.bufferProvider.getBuffer = NULL;
6867    mConfig.inputCfg.bufferProvider.releaseBuffer = NULL;
6868    mConfig.outputCfg.bufferProvider.cookie = NULL;
6869    mConfig.outputCfg.bufferProvider.getBuffer = NULL;
6870    mConfig.outputCfg.bufferProvider.releaseBuffer = NULL;
6871    mConfig.inputCfg.accessMode = EFFECT_BUFFER_ACCESS_READ;
6872    // Insert effect:
6873    // - in session AUDIO_SESSION_OUTPUT_MIX or AUDIO_SESSION_OUTPUT_STAGE,
6874    // always overwrites output buffer: input buffer == output buffer
6875    // - in other sessions:
6876    //      last effect in the chain accumulates in output buffer: input buffer != output buffer
6877    //      other effect: overwrites output buffer: input buffer == output buffer
6878    // Auxiliary effect:
6879    //      accumulates in output buffer: input buffer != output buffer
6880    // Therefore: accumulate <=> input buffer != output buffer
6881    if (mConfig.inputCfg.buffer.raw != mConfig.outputCfg.buffer.raw) {
6882        mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_ACCUMULATE;
6883    } else {
6884        mConfig.outputCfg.accessMode = EFFECT_BUFFER_ACCESS_WRITE;
6885    }
6886    mConfig.inputCfg.mask = EFFECT_CONFIG_ALL;
6887    mConfig.outputCfg.mask = EFFECT_CONFIG_ALL;
6888    mConfig.inputCfg.buffer.frameCount = thread->frameCount();
6889    mConfig.outputCfg.buffer.frameCount = mConfig.inputCfg.buffer.frameCount;
6890
6891    ALOGV("configure() %p thread %p buffer %p framecount %d",
6892            this, thread.get(), mConfig.inputCfg.buffer.raw, mConfig.inputCfg.buffer.frameCount);
6893
6894    status_t cmdStatus;
6895    uint32_t size = sizeof(int);
6896    status_t status = (*mEffectInterface)->command(mEffectInterface,
6897                                                   EFFECT_CMD_SET_CONFIG,
6898                                                   sizeof(effect_config_t),
6899                                                   &mConfig,
6900                                                   &size,
6901                                                   &cmdStatus);
6902    if (status == 0) {
6903        status = cmdStatus;
6904    }
6905
6906    mMaxDisableWaitCnt = (MAX_DISABLE_TIME_MS * mConfig.outputCfg.samplingRate) /
6907            (1000 * mConfig.outputCfg.buffer.frameCount);
6908
6909    return status;
6910}
6911
6912status_t AudioFlinger::EffectModule::init()
6913{
6914    Mutex::Autolock _l(mLock);
6915    if (mEffectInterface == NULL) {
6916        return NO_INIT;
6917    }
6918    status_t cmdStatus;
6919    uint32_t size = sizeof(status_t);
6920    status_t status = (*mEffectInterface)->command(mEffectInterface,
6921                                                   EFFECT_CMD_INIT,
6922                                                   0,
6923                                                   NULL,
6924                                                   &size,
6925                                                   &cmdStatus);
6926    if (status == 0) {
6927        status = cmdStatus;
6928    }
6929    return status;
6930}
6931
6932status_t AudioFlinger::EffectModule::start()
6933{
6934    Mutex::Autolock _l(mLock);
6935    return start_l();
6936}
6937
6938status_t AudioFlinger::EffectModule::start_l()
6939{
6940    if (mEffectInterface == NULL) {
6941        return NO_INIT;
6942    }
6943    status_t cmdStatus;
6944    uint32_t size = sizeof(status_t);
6945    status_t status = (*mEffectInterface)->command(mEffectInterface,
6946                                                   EFFECT_CMD_ENABLE,
6947                                                   0,
6948                                                   NULL,
6949                                                   &size,
6950                                                   &cmdStatus);
6951    if (status == 0) {
6952        status = cmdStatus;
6953    }
6954    if (status == 0 &&
6955            ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
6956             (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC)) {
6957        sp<ThreadBase> thread = mThread.promote();
6958        if (thread != 0) {
6959            audio_stream_t *stream = thread->stream();
6960            if (stream != NULL) {
6961                stream->add_audio_effect(stream, mEffectInterface);
6962            }
6963        }
6964    }
6965    return status;
6966}
6967
6968status_t AudioFlinger::EffectModule::stop()
6969{
6970    Mutex::Autolock _l(mLock);
6971    return stop_l();
6972}
6973
6974status_t AudioFlinger::EffectModule::stop_l()
6975{
6976    if (mEffectInterface == NULL) {
6977        return NO_INIT;
6978    }
6979    status_t cmdStatus;
6980    uint32_t size = sizeof(status_t);
6981    status_t status = (*mEffectInterface)->command(mEffectInterface,
6982                                                   EFFECT_CMD_DISABLE,
6983                                                   0,
6984                                                   NULL,
6985                                                   &size,
6986                                                   &cmdStatus);
6987    if (status == 0) {
6988        status = cmdStatus;
6989    }
6990    if (status == 0 &&
6991            ((mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC ||
6992             (mDescriptor.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_POST_PROC)) {
6993        sp<ThreadBase> thread = mThread.promote();
6994        if (thread != 0) {
6995            audio_stream_t *stream = thread->stream();
6996            if (stream != NULL) {
6997                stream->remove_audio_effect(stream, mEffectInterface);
6998            }
6999        }
7000    }
7001    return status;
7002}
7003
7004status_t AudioFlinger::EffectModule::command(uint32_t cmdCode,
7005                                             uint32_t cmdSize,
7006                                             void *pCmdData,
7007                                             uint32_t *replySize,
7008                                             void *pReplyData)
7009{
7010    Mutex::Autolock _l(mLock);
7011//    ALOGV("command(), cmdCode: %d, mEffectInterface: %p", cmdCode, mEffectInterface);
7012
7013    if (mState == DESTROYED || mEffectInterface == NULL) {
7014        return NO_INIT;
7015    }
7016    status_t status = (*mEffectInterface)->command(mEffectInterface,
7017                                                   cmdCode,
7018                                                   cmdSize,
7019                                                   pCmdData,
7020                                                   replySize,
7021                                                   pReplyData);
7022    if (cmdCode != EFFECT_CMD_GET_PARAM && status == NO_ERROR) {
7023        uint32_t size = (replySize == NULL) ? 0 : *replySize;
7024        for (size_t i = 1; i < mHandles.size(); i++) {
7025            sp<EffectHandle> h = mHandles[i].promote();
7026            if (h != 0) {
7027                h->commandExecuted(cmdCode, cmdSize, pCmdData, size, pReplyData);
7028            }
7029        }
7030    }
7031    return status;
7032}
7033
7034status_t AudioFlinger::EffectModule::setEnabled(bool enabled)
7035{
7036
7037    Mutex::Autolock _l(mLock);
7038    ALOGV("setEnabled %p enabled %d", this, enabled);
7039
7040    if (enabled != isEnabled()) {
7041        status_t status = AudioSystem::setEffectEnabled(mId, enabled);
7042        if (enabled && status != NO_ERROR) {
7043            return status;
7044        }
7045
7046        switch (mState) {
7047        // going from disabled to enabled
7048        case IDLE:
7049            mState = STARTING;
7050            break;
7051        case STOPPED:
7052            mState = RESTART;
7053            break;
7054        case STOPPING:
7055            mState = ACTIVE;
7056            break;
7057
7058        // going from enabled to disabled
7059        case RESTART:
7060            mState = STOPPED;
7061            break;
7062        case STARTING:
7063            mState = IDLE;
7064            break;
7065        case ACTIVE:
7066            mState = STOPPING;
7067            break;
7068        case DESTROYED:
7069            return NO_ERROR; // simply ignore as we are being destroyed
7070        }
7071        for (size_t i = 1; i < mHandles.size(); i++) {
7072            sp<EffectHandle> h = mHandles[i].promote();
7073            if (h != 0) {
7074                h->setEnabled(enabled);
7075            }
7076        }
7077    }
7078    return NO_ERROR;
7079}
7080
7081bool AudioFlinger::EffectModule::isEnabled() const
7082{
7083    switch (mState) {
7084    case RESTART:
7085    case STARTING:
7086    case ACTIVE:
7087        return true;
7088    case IDLE:
7089    case STOPPING:
7090    case STOPPED:
7091    case DESTROYED:
7092    default:
7093        return false;
7094    }
7095}
7096
7097bool AudioFlinger::EffectModule::isProcessEnabled() const
7098{
7099    switch (mState) {
7100    case RESTART:
7101    case ACTIVE:
7102    case STOPPING:
7103    case STOPPED:
7104        return true;
7105    case IDLE:
7106    case STARTING:
7107    case DESTROYED:
7108    default:
7109        return false;
7110    }
7111}
7112
7113status_t AudioFlinger::EffectModule::setVolume(uint32_t *left, uint32_t *right, bool controller)
7114{
7115    Mutex::Autolock _l(mLock);
7116    status_t status = NO_ERROR;
7117
7118    // Send volume indication if EFFECT_FLAG_VOLUME_IND is set and read back altered volume
7119    // if controller flag is set (Note that controller == TRUE => EFFECT_FLAG_VOLUME_CTRL set)
7120    if (isProcessEnabled() &&
7121            ((mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL ||
7122            (mDescriptor.flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_IND)) {
7123        status_t cmdStatus;
7124        uint32_t volume[2];
7125        uint32_t *pVolume = NULL;
7126        uint32_t size = sizeof(volume);
7127        volume[0] = *left;
7128        volume[1] = *right;
7129        if (controller) {
7130            pVolume = volume;
7131        }
7132        status = (*mEffectInterface)->command(mEffectInterface,
7133                                              EFFECT_CMD_SET_VOLUME,
7134                                              size,
7135                                              volume,
7136                                              &size,
7137                                              pVolume);
7138        if (controller && status == NO_ERROR && size == sizeof(volume)) {
7139            *left = volume[0];
7140            *right = volume[1];
7141        }
7142    }
7143    return status;
7144}
7145
7146status_t AudioFlinger::EffectModule::setDevice(uint32_t device)
7147{
7148    Mutex::Autolock _l(mLock);
7149    status_t status = NO_ERROR;
7150    if (device && (mDescriptor.flags & EFFECT_FLAG_DEVICE_MASK) == EFFECT_FLAG_DEVICE_IND) {
7151        // audio pre processing modules on RecordThread can receive both output and
7152        // input device indication in the same call
7153        uint32_t dev = device & AUDIO_DEVICE_OUT_ALL;
7154        if (dev) {
7155            status_t cmdStatus;
7156            uint32_t size = sizeof(status_t);
7157
7158            status = (*mEffectInterface)->command(mEffectInterface,
7159                                                  EFFECT_CMD_SET_DEVICE,
7160                                                  sizeof(uint32_t),
7161                                                  &dev,
7162                                                  &size,
7163                                                  &cmdStatus);
7164            if (status == NO_ERROR) {
7165                status = cmdStatus;
7166            }
7167        }
7168        dev = device & AUDIO_DEVICE_IN_ALL;
7169        if (dev) {
7170            status_t cmdStatus;
7171            uint32_t size = sizeof(status_t);
7172
7173            status_t status2 = (*mEffectInterface)->command(mEffectInterface,
7174                                                  EFFECT_CMD_SET_INPUT_DEVICE,
7175                                                  sizeof(uint32_t),
7176                                                  &dev,
7177                                                  &size,
7178                                                  &cmdStatus);
7179            if (status2 == NO_ERROR) {
7180                status2 = cmdStatus;
7181            }
7182            if (status == NO_ERROR) {
7183                status = status2;
7184            }
7185        }
7186    }
7187    return status;
7188}
7189
7190status_t AudioFlinger::EffectModule::setMode(audio_mode_t mode)
7191{
7192    Mutex::Autolock _l(mLock);
7193    status_t status = NO_ERROR;
7194    if ((mDescriptor.flags & EFFECT_FLAG_AUDIO_MODE_MASK) == EFFECT_FLAG_AUDIO_MODE_IND) {
7195        status_t cmdStatus;
7196        uint32_t size = sizeof(status_t);
7197        status = (*mEffectInterface)->command(mEffectInterface,
7198                                              EFFECT_CMD_SET_AUDIO_MODE,
7199                                              sizeof(audio_mode_t),
7200                                              &mode,
7201                                              &size,
7202                                              &cmdStatus);
7203        if (status == NO_ERROR) {
7204            status = cmdStatus;
7205        }
7206    }
7207    return status;
7208}
7209
7210void AudioFlinger::EffectModule::setSuspended(bool suspended)
7211{
7212    Mutex::Autolock _l(mLock);
7213    mSuspended = suspended;
7214}
7215
7216bool AudioFlinger::EffectModule::suspended() const
7217{
7218    Mutex::Autolock _l(mLock);
7219    return mSuspended;
7220}
7221
7222status_t AudioFlinger::EffectModule::dump(int fd, const Vector<String16>& args)
7223{
7224    const size_t SIZE = 256;
7225    char buffer[SIZE];
7226    String8 result;
7227
7228    snprintf(buffer, SIZE, "\tEffect ID %d:\n", mId);
7229    result.append(buffer);
7230
7231    bool locked = tryLock(mLock);
7232    // failed to lock - AudioFlinger is probably deadlocked
7233    if (!locked) {
7234        result.append("\t\tCould not lock Fx mutex:\n");
7235    }
7236
7237    result.append("\t\tSession Status State Engine:\n");
7238    snprintf(buffer, SIZE, "\t\t%05d   %03d    %03d   0x%08x\n",
7239            mSessionId, mStatus, mState, (uint32_t)mEffectInterface);
7240    result.append(buffer);
7241
7242    result.append("\t\tDescriptor:\n");
7243    snprintf(buffer, SIZE, "\t\t- UUID: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n",
7244            mDescriptor.uuid.timeLow, mDescriptor.uuid.timeMid, mDescriptor.uuid.timeHiAndVersion,
7245            mDescriptor.uuid.clockSeq, mDescriptor.uuid.node[0], mDescriptor.uuid.node[1],mDescriptor.uuid.node[2],
7246            mDescriptor.uuid.node[3],mDescriptor.uuid.node[4],mDescriptor.uuid.node[5]);
7247    result.append(buffer);
7248    snprintf(buffer, SIZE, "\t\t- TYPE: %08X-%04X-%04X-%04X-%02X%02X%02X%02X%02X%02X\n",
7249                mDescriptor.type.timeLow, mDescriptor.type.timeMid, mDescriptor.type.timeHiAndVersion,
7250                mDescriptor.type.clockSeq, mDescriptor.type.node[0], mDescriptor.type.node[1],mDescriptor.type.node[2],
7251                mDescriptor.type.node[3],mDescriptor.type.node[4],mDescriptor.type.node[5]);
7252    result.append(buffer);
7253    snprintf(buffer, SIZE, "\t\t- apiVersion: %08X\n\t\t- flags: %08X\n",
7254            mDescriptor.apiVersion,
7255            mDescriptor.flags);
7256    result.append(buffer);
7257    snprintf(buffer, SIZE, "\t\t- name: %s\n",
7258            mDescriptor.name);
7259    result.append(buffer);
7260    snprintf(buffer, SIZE, "\t\t- implementor: %s\n",
7261            mDescriptor.implementor);
7262    result.append(buffer);
7263
7264    result.append("\t\t- Input configuration:\n");
7265    result.append("\t\t\tBuffer     Frames  Smp rate Channels Format\n");
7266    snprintf(buffer, SIZE, "\t\t\t0x%08x %05d   %05d    %08x %d\n",
7267            (uint32_t)mConfig.inputCfg.buffer.raw,
7268            mConfig.inputCfg.buffer.frameCount,
7269            mConfig.inputCfg.samplingRate,
7270            mConfig.inputCfg.channels,
7271            mConfig.inputCfg.format);
7272    result.append(buffer);
7273
7274    result.append("\t\t- Output configuration:\n");
7275    result.append("\t\t\tBuffer     Frames  Smp rate Channels Format\n");
7276    snprintf(buffer, SIZE, "\t\t\t0x%08x %05d   %05d    %08x %d\n",
7277            (uint32_t)mConfig.outputCfg.buffer.raw,
7278            mConfig.outputCfg.buffer.frameCount,
7279            mConfig.outputCfg.samplingRate,
7280            mConfig.outputCfg.channels,
7281            mConfig.outputCfg.format);
7282    result.append(buffer);
7283
7284    snprintf(buffer, SIZE, "\t\t%d Clients:\n", mHandles.size());
7285    result.append(buffer);
7286    result.append("\t\t\tPid   Priority Ctrl Locked client server\n");
7287    for (size_t i = 0; i < mHandles.size(); ++i) {
7288        sp<EffectHandle> handle = mHandles[i].promote();
7289        if (handle != 0) {
7290            handle->dump(buffer, SIZE);
7291            result.append(buffer);
7292        }
7293    }
7294
7295    result.append("\n");
7296
7297    write(fd, result.string(), result.length());
7298
7299    if (locked) {
7300        mLock.unlock();
7301    }
7302
7303    return NO_ERROR;
7304}
7305
7306// ----------------------------------------------------------------------------
7307//  EffectHandle implementation
7308// ----------------------------------------------------------------------------
7309
7310#undef LOG_TAG
7311#define LOG_TAG "AudioFlinger::EffectHandle"
7312
7313AudioFlinger::EffectHandle::EffectHandle(const sp<EffectModule>& effect,
7314                                        const sp<AudioFlinger::Client>& client,
7315                                        const sp<IEffectClient>& effectClient,
7316                                        int32_t priority)
7317    : BnEffect(),
7318    mEffect(effect), mEffectClient(effectClient), mClient(client), mCblk(NULL),
7319    mPriority(priority), mHasControl(false), mEnabled(false)
7320{
7321    ALOGV("constructor %p", this);
7322
7323    if (client == 0) {
7324        return;
7325    }
7326    int bufOffset = ((sizeof(effect_param_cblk_t) - 1) / sizeof(int) + 1) * sizeof(int);
7327    mCblkMemory = client->heap()->allocate(EFFECT_PARAM_BUFFER_SIZE + bufOffset);
7328    if (mCblkMemory != 0) {
7329        mCblk = static_cast<effect_param_cblk_t *>(mCblkMemory->pointer());
7330
7331        if (mCblk != NULL) {
7332            new(mCblk) effect_param_cblk_t();
7333            mBuffer = (uint8_t *)mCblk + bufOffset;
7334         }
7335    } else {
7336        ALOGE("not enough memory for Effect size=%u", EFFECT_PARAM_BUFFER_SIZE + sizeof(effect_param_cblk_t));
7337        return;
7338    }
7339}
7340
7341AudioFlinger::EffectHandle::~EffectHandle()
7342{
7343    ALOGV("Destructor %p", this);
7344    disconnect(false);
7345    ALOGV("Destructor DONE %p", this);
7346}
7347
7348status_t AudioFlinger::EffectHandle::enable()
7349{
7350    ALOGV("enable %p", this);
7351    if (!mHasControl) return INVALID_OPERATION;
7352    if (mEffect == 0) return DEAD_OBJECT;
7353
7354    if (mEnabled) {
7355        return NO_ERROR;
7356    }
7357
7358    mEnabled = true;
7359
7360    sp<ThreadBase> thread = mEffect->thread().promote();
7361    if (thread != 0) {
7362        thread->checkSuspendOnEffectEnabled(mEffect, true, mEffect->sessionId());
7363    }
7364
7365    // checkSuspendOnEffectEnabled() can suspend this same effect when enabled
7366    if (mEffect->suspended()) {
7367        return NO_ERROR;
7368    }
7369
7370    status_t status = mEffect->setEnabled(true);
7371    if (status != NO_ERROR) {
7372        if (thread != 0) {
7373            thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId());
7374        }
7375        mEnabled = false;
7376    }
7377    return status;
7378}
7379
7380status_t AudioFlinger::EffectHandle::disable()
7381{
7382    ALOGV("disable %p", this);
7383    if (!mHasControl) return INVALID_OPERATION;
7384    if (mEffect == 0) return DEAD_OBJECT;
7385
7386    if (!mEnabled) {
7387        return NO_ERROR;
7388    }
7389    mEnabled = false;
7390
7391    if (mEffect->suspended()) {
7392        return NO_ERROR;
7393    }
7394
7395    status_t status = mEffect->setEnabled(false);
7396
7397    sp<ThreadBase> thread = mEffect->thread().promote();
7398    if (thread != 0) {
7399        thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId());
7400    }
7401
7402    return status;
7403}
7404
7405void AudioFlinger::EffectHandle::disconnect()
7406{
7407    disconnect(true);
7408}
7409
7410void AudioFlinger::EffectHandle::disconnect(bool unpinIfLast)
7411{
7412    ALOGV("disconnect(%s)", unpinIfLast ? "true" : "false");
7413    if (mEffect == 0) {
7414        return;
7415    }
7416    mEffect->disconnect(this, unpinIfLast);
7417
7418    if (mHasControl && mEnabled) {
7419        sp<ThreadBase> thread = mEffect->thread().promote();
7420        if (thread != 0) {
7421            thread->checkSuspendOnEffectEnabled(mEffect, false, mEffect->sessionId());
7422        }
7423    }
7424
7425    // release sp on module => module destructor can be called now
7426    mEffect.clear();
7427    if (mClient != 0) {
7428        if (mCblk != NULL) {
7429            // unlike ~TrackBase(), mCblk is never a local new, so don't delete
7430            mCblk->~effect_param_cblk_t();   // destroy our shared-structure.
7431        }
7432        mCblkMemory.clear();    // free the shared memory before releasing the heap it belongs to
7433        // Client destructor must run with AudioFlinger mutex locked
7434        Mutex::Autolock _l(mClient->audioFlinger()->mLock);
7435        mClient.clear();
7436    }
7437}
7438
7439status_t AudioFlinger::EffectHandle::command(uint32_t cmdCode,
7440                                             uint32_t cmdSize,
7441                                             void *pCmdData,
7442                                             uint32_t *replySize,
7443                                             void *pReplyData)
7444{
7445//    ALOGV("command(), cmdCode: %d, mHasControl: %d, mEffect: %p",
7446//              cmdCode, mHasControl, (mEffect == 0) ? 0 : mEffect.get());
7447
7448    // only get parameter command is permitted for applications not controlling the effect
7449    if (!mHasControl && cmdCode != EFFECT_CMD_GET_PARAM) {
7450        return INVALID_OPERATION;
7451    }
7452    if (mEffect == 0) return DEAD_OBJECT;
7453    if (mClient == 0) return INVALID_OPERATION;
7454
7455    // handle commands that are not forwarded transparently to effect engine
7456    if (cmdCode == EFFECT_CMD_SET_PARAM_COMMIT) {
7457        // No need to trylock() here as this function is executed in the binder thread serving a particular client process:
7458        // no risk to block the whole media server process or mixer threads is we are stuck here
7459        Mutex::Autolock _l(mCblk->lock);
7460        if (mCblk->clientIndex > EFFECT_PARAM_BUFFER_SIZE ||
7461            mCblk->serverIndex > EFFECT_PARAM_BUFFER_SIZE) {
7462            mCblk->serverIndex = 0;
7463            mCblk->clientIndex = 0;
7464            return BAD_VALUE;
7465        }
7466        status_t status = NO_ERROR;
7467        while (mCblk->serverIndex < mCblk->clientIndex) {
7468            int reply;
7469            uint32_t rsize = sizeof(int);
7470            int *p = (int *)(mBuffer + mCblk->serverIndex);
7471            int size = *p++;
7472            if (((uint8_t *)p + size) > mBuffer + mCblk->clientIndex) {
7473                ALOGW("command(): invalid parameter block size");
7474                break;
7475            }
7476            effect_param_t *param = (effect_param_t *)p;
7477            if (param->psize == 0 || param->vsize == 0) {
7478                ALOGW("command(): null parameter or value size");
7479                mCblk->serverIndex += size;
7480                continue;
7481            }
7482            uint32_t psize = sizeof(effect_param_t) +
7483                             ((param->psize - 1) / sizeof(int) + 1) * sizeof(int) +
7484                             param->vsize;
7485            status_t ret = mEffect->command(EFFECT_CMD_SET_PARAM,
7486                                            psize,
7487                                            p,
7488                                            &rsize,
7489                                            &reply);
7490            // stop at first error encountered
7491            if (ret != NO_ERROR) {
7492                status = ret;
7493                *(int *)pReplyData = reply;
7494                break;
7495            } else if (reply != NO_ERROR) {
7496                *(int *)pReplyData = reply;
7497                break;
7498            }
7499            mCblk->serverIndex += size;
7500        }
7501        mCblk->serverIndex = 0;
7502        mCblk->clientIndex = 0;
7503        return status;
7504    } else if (cmdCode == EFFECT_CMD_ENABLE) {
7505        *(int *)pReplyData = NO_ERROR;
7506        return enable();
7507    } else if (cmdCode == EFFECT_CMD_DISABLE) {
7508        *(int *)pReplyData = NO_ERROR;
7509        return disable();
7510    }
7511
7512    return mEffect->command(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
7513}
7514
7515void AudioFlinger::EffectHandle::setControl(bool hasControl, bool signal, bool enabled)
7516{
7517    ALOGV("setControl %p control %d", this, hasControl);
7518
7519    mHasControl = hasControl;
7520    mEnabled = enabled;
7521
7522    if (signal && mEffectClient != 0) {
7523        mEffectClient->controlStatusChanged(hasControl);
7524    }
7525}
7526
7527void AudioFlinger::EffectHandle::commandExecuted(uint32_t cmdCode,
7528                                                 uint32_t cmdSize,
7529                                                 void *pCmdData,
7530                                                 uint32_t replySize,
7531                                                 void *pReplyData)
7532{
7533    if (mEffectClient != 0) {
7534        mEffectClient->commandExecuted(cmdCode, cmdSize, pCmdData, replySize, pReplyData);
7535    }
7536}
7537
7538
7539
7540void AudioFlinger::EffectHandle::setEnabled(bool enabled)
7541{
7542    if (mEffectClient != 0) {
7543        mEffectClient->enableStatusChanged(enabled);
7544    }
7545}
7546
7547status_t AudioFlinger::EffectHandle::onTransact(
7548    uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
7549{
7550    return BnEffect::onTransact(code, data, reply, flags);
7551}
7552
7553
7554void AudioFlinger::EffectHandle::dump(char* buffer, size_t size)
7555{
7556    bool locked = mCblk != NULL && tryLock(mCblk->lock);
7557
7558    snprintf(buffer, size, "\t\t\t%05d %05d    %01u    %01u      %05u  %05u\n",
7559            (mClient == 0) ? getpid_cached : mClient->pid(),
7560            mPriority,
7561            mHasControl,
7562            !locked,
7563            mCblk ? mCblk->clientIndex : 0,
7564            mCblk ? mCblk->serverIndex : 0
7565            );
7566
7567    if (locked) {
7568        mCblk->lock.unlock();
7569    }
7570}
7571
7572#undef LOG_TAG
7573#define LOG_TAG "AudioFlinger::EffectChain"
7574
7575AudioFlinger::EffectChain::EffectChain(ThreadBase *thread,
7576                                        int sessionId)
7577    : mThread(thread), mSessionId(sessionId), mActiveTrackCnt(0), mTrackCnt(0), mTailBufferCount(0),
7578      mOwnInBuffer(false), mVolumeCtrlIdx(-1), mLeftVolume(UINT_MAX), mRightVolume(UINT_MAX),
7579      mNewLeftVolume(UINT_MAX), mNewRightVolume(UINT_MAX)
7580{
7581    mStrategy = AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
7582    if (thread == NULL) {
7583        return;
7584    }
7585    mMaxTailBuffers = ((kProcessTailDurationMs * thread->sampleRate()) / 1000) /
7586                                    thread->frameCount();
7587}
7588
7589AudioFlinger::EffectChain::~EffectChain()
7590{
7591    if (mOwnInBuffer) {
7592        delete mInBuffer;
7593    }
7594
7595}
7596
7597// getEffectFromDesc_l() must be called with ThreadBase::mLock held
7598sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromDesc_l(effect_descriptor_t *descriptor)
7599{
7600    size_t size = mEffects.size();
7601
7602    for (size_t i = 0; i < size; i++) {
7603        if (memcmp(&mEffects[i]->desc().uuid, &descriptor->uuid, sizeof(effect_uuid_t)) == 0) {
7604            return mEffects[i];
7605        }
7606    }
7607    return 0;
7608}
7609
7610// getEffectFromId_l() must be called with ThreadBase::mLock held
7611sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromId_l(int id)
7612{
7613    size_t size = mEffects.size();
7614
7615    for (size_t i = 0; i < size; i++) {
7616        // by convention, return first effect if id provided is 0 (0 is never a valid id)
7617        if (id == 0 || mEffects[i]->id() == id) {
7618            return mEffects[i];
7619        }
7620    }
7621    return 0;
7622}
7623
7624// getEffectFromType_l() must be called with ThreadBase::mLock held
7625sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectFromType_l(
7626        const effect_uuid_t *type)
7627{
7628    size_t size = mEffects.size();
7629
7630    for (size_t i = 0; i < size; i++) {
7631        if (memcmp(&mEffects[i]->desc().type, type, sizeof(effect_uuid_t)) == 0) {
7632            return mEffects[i];
7633        }
7634    }
7635    return 0;
7636}
7637
7638// Must be called with EffectChain::mLock locked
7639void AudioFlinger::EffectChain::process_l()
7640{
7641    sp<ThreadBase> thread = mThread.promote();
7642    if (thread == 0) {
7643        ALOGW("process_l(): cannot promote mixer thread");
7644        return;
7645    }
7646    bool isGlobalSession = (mSessionId == AUDIO_SESSION_OUTPUT_MIX) ||
7647            (mSessionId == AUDIO_SESSION_OUTPUT_STAGE);
7648    // always process effects unless no more tracks are on the session and the effect tail
7649    // has been rendered
7650    bool doProcess = true;
7651    if (!isGlobalSession) {
7652        bool tracksOnSession = (trackCnt() != 0);
7653
7654        if (!tracksOnSession && mTailBufferCount == 0) {
7655            doProcess = false;
7656        }
7657
7658        if (activeTrackCnt() == 0) {
7659            // if no track is active and the effect tail has not been rendered,
7660            // the input buffer must be cleared here as the mixer process will not do it
7661            if (tracksOnSession || mTailBufferCount > 0) {
7662                size_t numSamples = thread->frameCount() * thread->channelCount();
7663                memset(mInBuffer, 0, numSamples * sizeof(int16_t));
7664                if (mTailBufferCount > 0) {
7665                    mTailBufferCount--;
7666                }
7667            }
7668        }
7669    }
7670
7671    size_t size = mEffects.size();
7672    if (doProcess) {
7673        for (size_t i = 0; i < size; i++) {
7674            mEffects[i]->process();
7675        }
7676    }
7677    for (size_t i = 0; i < size; i++) {
7678        mEffects[i]->updateState();
7679    }
7680}
7681
7682// addEffect_l() must be called with PlaybackThread::mLock held
7683status_t AudioFlinger::EffectChain::addEffect_l(const sp<EffectModule>& effect)
7684{
7685    effect_descriptor_t desc = effect->desc();
7686    uint32_t insertPref = desc.flags & EFFECT_FLAG_INSERT_MASK;
7687
7688    Mutex::Autolock _l(mLock);
7689    effect->setChain(this);
7690    sp<ThreadBase> thread = mThread.promote();
7691    if (thread == 0) {
7692        return NO_INIT;
7693    }
7694    effect->setThread(thread);
7695
7696    if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
7697        // Auxiliary effects are inserted at the beginning of mEffects vector as
7698        // they are processed first and accumulated in chain input buffer
7699        mEffects.insertAt(effect, 0);
7700
7701        // the input buffer for auxiliary effect contains mono samples in
7702        // 32 bit format. This is to avoid saturation in AudoMixer
7703        // accumulation stage. Saturation is done in EffectModule::process() before
7704        // calling the process in effect engine
7705        size_t numSamples = thread->frameCount();
7706        int32_t *buffer = new int32_t[numSamples];
7707        memset(buffer, 0, numSamples * sizeof(int32_t));
7708        effect->setInBuffer((int16_t *)buffer);
7709        // auxiliary effects output samples to chain input buffer for further processing
7710        // by insert effects
7711        effect->setOutBuffer(mInBuffer);
7712    } else {
7713        // Insert effects are inserted at the end of mEffects vector as they are processed
7714        //  after track and auxiliary effects.
7715        // Insert effect order as a function of indicated preference:
7716        //  if EFFECT_FLAG_INSERT_EXCLUSIVE, insert in first position or reject if
7717        //  another effect is present
7718        //  else if EFFECT_FLAG_INSERT_FIRST, insert in first position or after the
7719        //  last effect claiming first position
7720        //  else if EFFECT_FLAG_INSERT_LAST, insert in last position or before the
7721        //  first effect claiming last position
7722        //  else if EFFECT_FLAG_INSERT_ANY insert after first or before last
7723        // Reject insertion if an effect with EFFECT_FLAG_INSERT_EXCLUSIVE is
7724        // already present
7725
7726        size_t size = mEffects.size();
7727        size_t idx_insert = size;
7728        ssize_t idx_insert_first = -1;
7729        ssize_t idx_insert_last = -1;
7730
7731        for (size_t i = 0; i < size; i++) {
7732            effect_descriptor_t d = mEffects[i]->desc();
7733            uint32_t iMode = d.flags & EFFECT_FLAG_TYPE_MASK;
7734            uint32_t iPref = d.flags & EFFECT_FLAG_INSERT_MASK;
7735            if (iMode == EFFECT_FLAG_TYPE_INSERT) {
7736                // check invalid effect chaining combinations
7737                if (insertPref == EFFECT_FLAG_INSERT_EXCLUSIVE ||
7738                    iPref == EFFECT_FLAG_INSERT_EXCLUSIVE) {
7739                    ALOGW("addEffect_l() could not insert effect %s: exclusive conflict with %s", desc.name, d.name);
7740                    return INVALID_OPERATION;
7741                }
7742                // remember position of first insert effect and by default
7743                // select this as insert position for new effect
7744                if (idx_insert == size) {
7745                    idx_insert = i;
7746                }
7747                // remember position of last insert effect claiming
7748                // first position
7749                if (iPref == EFFECT_FLAG_INSERT_FIRST) {
7750                    idx_insert_first = i;
7751                }
7752                // remember position of first insert effect claiming
7753                // last position
7754                if (iPref == EFFECT_FLAG_INSERT_LAST &&
7755                    idx_insert_last == -1) {
7756                    idx_insert_last = i;
7757                }
7758            }
7759        }
7760
7761        // modify idx_insert from first position if needed
7762        if (insertPref == EFFECT_FLAG_INSERT_LAST) {
7763            if (idx_insert_last != -1) {
7764                idx_insert = idx_insert_last;
7765            } else {
7766                idx_insert = size;
7767            }
7768        } else {
7769            if (idx_insert_first != -1) {
7770                idx_insert = idx_insert_first + 1;
7771            }
7772        }
7773
7774        // always read samples from chain input buffer
7775        effect->setInBuffer(mInBuffer);
7776
7777        // if last effect in the chain, output samples to chain
7778        // output buffer, otherwise to chain input buffer
7779        if (idx_insert == size) {
7780            if (idx_insert != 0) {
7781                mEffects[idx_insert-1]->setOutBuffer(mInBuffer);
7782                mEffects[idx_insert-1]->configure();
7783            }
7784            effect->setOutBuffer(mOutBuffer);
7785        } else {
7786            effect->setOutBuffer(mInBuffer);
7787        }
7788        mEffects.insertAt(effect, idx_insert);
7789
7790        ALOGV("addEffect_l() effect %p, added in chain %p at rank %d", effect.get(), this, idx_insert);
7791    }
7792    effect->configure();
7793    return NO_ERROR;
7794}
7795
7796// removeEffect_l() must be called with PlaybackThread::mLock held
7797size_t AudioFlinger::EffectChain::removeEffect_l(const sp<EffectModule>& effect)
7798{
7799    Mutex::Autolock _l(mLock);
7800    size_t size = mEffects.size();
7801    uint32_t type = effect->desc().flags & EFFECT_FLAG_TYPE_MASK;
7802
7803    for (size_t i = 0; i < size; i++) {
7804        if (effect == mEffects[i]) {
7805            // calling stop here will remove pre-processing effect from the audio HAL.
7806            // This is safe as we hold the EffectChain mutex which guarantees that we are not in
7807            // the middle of a read from audio HAL
7808            if (mEffects[i]->state() == EffectModule::ACTIVE ||
7809                    mEffects[i]->state() == EffectModule::STOPPING) {
7810                mEffects[i]->stop();
7811            }
7812            if (type == EFFECT_FLAG_TYPE_AUXILIARY) {
7813                delete[] effect->inBuffer();
7814            } else {
7815                if (i == size - 1 && i != 0) {
7816                    mEffects[i - 1]->setOutBuffer(mOutBuffer);
7817                    mEffects[i - 1]->configure();
7818                }
7819            }
7820            mEffects.removeAt(i);
7821            ALOGV("removeEffect_l() effect %p, removed from chain %p at rank %d", effect.get(), this, i);
7822            break;
7823        }
7824    }
7825
7826    return mEffects.size();
7827}
7828
7829// setDevice_l() must be called with PlaybackThread::mLock held
7830void AudioFlinger::EffectChain::setDevice_l(uint32_t device)
7831{
7832    size_t size = mEffects.size();
7833    for (size_t i = 0; i < size; i++) {
7834        mEffects[i]->setDevice(device);
7835    }
7836}
7837
7838// setMode_l() must be called with PlaybackThread::mLock held
7839void AudioFlinger::EffectChain::setMode_l(audio_mode_t mode)
7840{
7841    size_t size = mEffects.size();
7842    for (size_t i = 0; i < size; i++) {
7843        mEffects[i]->setMode(mode);
7844    }
7845}
7846
7847// setVolume_l() must be called with PlaybackThread::mLock held
7848bool AudioFlinger::EffectChain::setVolume_l(uint32_t *left, uint32_t *right)
7849{
7850    uint32_t newLeft = *left;
7851    uint32_t newRight = *right;
7852    bool hasControl = false;
7853    int ctrlIdx = -1;
7854    size_t size = mEffects.size();
7855
7856    // first update volume controller
7857    for (size_t i = size; i > 0; i--) {
7858        if (mEffects[i - 1]->isProcessEnabled() &&
7859            (mEffects[i - 1]->desc().flags & EFFECT_FLAG_VOLUME_MASK) == EFFECT_FLAG_VOLUME_CTRL) {
7860            ctrlIdx = i - 1;
7861            hasControl = true;
7862            break;
7863        }
7864    }
7865
7866    if (ctrlIdx == mVolumeCtrlIdx && *left == mLeftVolume && *right == mRightVolume) {
7867        if (hasControl) {
7868            *left = mNewLeftVolume;
7869            *right = mNewRightVolume;
7870        }
7871        return hasControl;
7872    }
7873
7874    mVolumeCtrlIdx = ctrlIdx;
7875    mLeftVolume = newLeft;
7876    mRightVolume = newRight;
7877
7878    // second get volume update from volume controller
7879    if (ctrlIdx >= 0) {
7880        mEffects[ctrlIdx]->setVolume(&newLeft, &newRight, true);
7881        mNewLeftVolume = newLeft;
7882        mNewRightVolume = newRight;
7883    }
7884    // then indicate volume to all other effects in chain.
7885    // Pass altered volume to effects before volume controller
7886    // and requested volume to effects after controller
7887    uint32_t lVol = newLeft;
7888    uint32_t rVol = newRight;
7889
7890    for (size_t i = 0; i < size; i++) {
7891        if ((int)i == ctrlIdx) continue;
7892        // this also works for ctrlIdx == -1 when there is no volume controller
7893        if ((int)i > ctrlIdx) {
7894            lVol = *left;
7895            rVol = *right;
7896        }
7897        mEffects[i]->setVolume(&lVol, &rVol, false);
7898    }
7899    *left = newLeft;
7900    *right = newRight;
7901
7902    return hasControl;
7903}
7904
7905status_t AudioFlinger::EffectChain::dump(int fd, const Vector<String16>& args)
7906{
7907    const size_t SIZE = 256;
7908    char buffer[SIZE];
7909    String8 result;
7910
7911    snprintf(buffer, SIZE, "Effects for session %d:\n", mSessionId);
7912    result.append(buffer);
7913
7914    bool locked = tryLock(mLock);
7915    // failed to lock - AudioFlinger is probably deadlocked
7916    if (!locked) {
7917        result.append("\tCould not lock mutex:\n");
7918    }
7919
7920    result.append("\tNum fx In buffer   Out buffer   Active tracks:\n");
7921    snprintf(buffer, SIZE, "\t%02d     0x%08x  0x%08x   %d\n",
7922            mEffects.size(),
7923            (uint32_t)mInBuffer,
7924            (uint32_t)mOutBuffer,
7925            mActiveTrackCnt);
7926    result.append(buffer);
7927    write(fd, result.string(), result.size());
7928
7929    for (size_t i = 0; i < mEffects.size(); ++i) {
7930        sp<EffectModule> effect = mEffects[i];
7931        if (effect != 0) {
7932            effect->dump(fd, args);
7933        }
7934    }
7935
7936    if (locked) {
7937        mLock.unlock();
7938    }
7939
7940    return NO_ERROR;
7941}
7942
7943// must be called with ThreadBase::mLock held
7944void AudioFlinger::EffectChain::setEffectSuspended_l(
7945        const effect_uuid_t *type, bool suspend)
7946{
7947    sp<SuspendedEffectDesc> desc;
7948    // use effect type UUID timelow as key as there is no real risk of identical
7949    // timeLow fields among effect type UUIDs.
7950    ssize_t index = mSuspendedEffects.indexOfKey(type->timeLow);
7951    if (suspend) {
7952        if (index >= 0) {
7953            desc = mSuspendedEffects.valueAt(index);
7954        } else {
7955            desc = new SuspendedEffectDesc();
7956            memcpy(&desc->mType, type, sizeof(effect_uuid_t));
7957            mSuspendedEffects.add(type->timeLow, desc);
7958            ALOGV("setEffectSuspended_l() add entry for %08x", type->timeLow);
7959        }
7960        if (desc->mRefCount++ == 0) {
7961            sp<EffectModule> effect = getEffectIfEnabled(type);
7962            if (effect != 0) {
7963                desc->mEffect = effect;
7964                effect->setSuspended(true);
7965                effect->setEnabled(false);
7966            }
7967        }
7968    } else {
7969        if (index < 0) {
7970            return;
7971        }
7972        desc = mSuspendedEffects.valueAt(index);
7973        if (desc->mRefCount <= 0) {
7974            ALOGW("setEffectSuspended_l() restore refcount should not be 0 %d", desc->mRefCount);
7975            desc->mRefCount = 1;
7976        }
7977        if (--desc->mRefCount == 0) {
7978            ALOGV("setEffectSuspended_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
7979            if (desc->mEffect != 0) {
7980                sp<EffectModule> effect = desc->mEffect.promote();
7981                if (effect != 0) {
7982                    effect->setSuspended(false);
7983                    sp<EffectHandle> handle = effect->controlHandle();
7984                    if (handle != 0) {
7985                        effect->setEnabled(handle->enabled());
7986                    }
7987                }
7988                desc->mEffect.clear();
7989            }
7990            mSuspendedEffects.removeItemsAt(index);
7991        }
7992    }
7993}
7994
7995// must be called with ThreadBase::mLock held
7996void AudioFlinger::EffectChain::setEffectSuspendedAll_l(bool suspend)
7997{
7998    sp<SuspendedEffectDesc> desc;
7999
8000    ssize_t index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
8001    if (suspend) {
8002        if (index >= 0) {
8003            desc = mSuspendedEffects.valueAt(index);
8004        } else {
8005            desc = new SuspendedEffectDesc();
8006            mSuspendedEffects.add((int)kKeyForSuspendAll, desc);
8007            ALOGV("setEffectSuspendedAll_l() add entry for 0");
8008        }
8009        if (desc->mRefCount++ == 0) {
8010            Vector< sp<EffectModule> > effects;
8011            getSuspendEligibleEffects(effects);
8012            for (size_t i = 0; i < effects.size(); i++) {
8013                setEffectSuspended_l(&effects[i]->desc().type, true);
8014            }
8015        }
8016    } else {
8017        if (index < 0) {
8018            return;
8019        }
8020        desc = mSuspendedEffects.valueAt(index);
8021        if (desc->mRefCount <= 0) {
8022            ALOGW("setEffectSuspendedAll_l() restore refcount should not be 0 %d", desc->mRefCount);
8023            desc->mRefCount = 1;
8024        }
8025        if (--desc->mRefCount == 0) {
8026            Vector<const effect_uuid_t *> types;
8027            for (size_t i = 0; i < mSuspendedEffects.size(); i++) {
8028                if (mSuspendedEffects.keyAt(i) == (int)kKeyForSuspendAll) {
8029                    continue;
8030                }
8031                types.add(&mSuspendedEffects.valueAt(i)->mType);
8032            }
8033            for (size_t i = 0; i < types.size(); i++) {
8034                setEffectSuspended_l(types[i], false);
8035            }
8036            ALOGV("setEffectSuspendedAll_l() remove entry for %08x", mSuspendedEffects.keyAt(index));
8037            mSuspendedEffects.removeItem((int)kKeyForSuspendAll);
8038        }
8039    }
8040}
8041
8042
8043// The volume effect is used for automated tests only
8044#ifndef OPENSL_ES_H_
8045static const effect_uuid_t SL_IID_VOLUME_ = { 0x09e8ede0, 0xddde, 0x11db, 0xb4f6,
8046                                            { 0x00, 0x02, 0xa5, 0xd5, 0xc5, 0x1b } };
8047const effect_uuid_t * const SL_IID_VOLUME = &SL_IID_VOLUME_;
8048#endif //OPENSL_ES_H_
8049
8050bool AudioFlinger::EffectChain::isEffectEligibleForSuspend(const effect_descriptor_t& desc)
8051{
8052    // auxiliary effects and visualizer are never suspended on output mix
8053    if ((mSessionId == AUDIO_SESSION_OUTPUT_MIX) &&
8054        (((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) ||
8055         (memcmp(&desc.type, SL_IID_VISUALIZATION, sizeof(effect_uuid_t)) == 0) ||
8056         (memcmp(&desc.type, SL_IID_VOLUME, sizeof(effect_uuid_t)) == 0))) {
8057        return false;
8058    }
8059    return true;
8060}
8061
8062void AudioFlinger::EffectChain::getSuspendEligibleEffects(Vector< sp<AudioFlinger::EffectModule> > &effects)
8063{
8064    effects.clear();
8065    for (size_t i = 0; i < mEffects.size(); i++) {
8066        if (isEffectEligibleForSuspend(mEffects[i]->desc())) {
8067            effects.add(mEffects[i]);
8068        }
8069    }
8070}
8071
8072sp<AudioFlinger::EffectModule> AudioFlinger::EffectChain::getEffectIfEnabled(
8073                                                            const effect_uuid_t *type)
8074{
8075    sp<EffectModule> effect = getEffectFromType_l(type);
8076    return effect != 0 && effect->isEnabled() ? effect : 0;
8077}
8078
8079void AudioFlinger::EffectChain::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
8080                                                            bool enabled)
8081{
8082    ssize_t index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
8083    if (enabled) {
8084        if (index < 0) {
8085            // if the effect is not suspend check if all effects are suspended
8086            index = mSuspendedEffects.indexOfKey((int)kKeyForSuspendAll);
8087            if (index < 0) {
8088                return;
8089            }
8090            if (!isEffectEligibleForSuspend(effect->desc())) {
8091                return;
8092            }
8093            setEffectSuspended_l(&effect->desc().type, enabled);
8094            index = mSuspendedEffects.indexOfKey(effect->desc().type.timeLow);
8095            if (index < 0) {
8096                ALOGW("checkSuspendOnEffectEnabled() Fx should be suspended here!");
8097                return;
8098            }
8099        }
8100        ALOGV("checkSuspendOnEffectEnabled() enable suspending fx %08x",
8101             effect->desc().type.timeLow);
8102        sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
8103        // if effect is requested to suspended but was not yet enabled, supend it now.
8104        if (desc->mEffect == 0) {
8105            desc->mEffect = effect;
8106            effect->setEnabled(false);
8107            effect->setSuspended(true);
8108        }
8109    } else {
8110        if (index < 0) {
8111            return;
8112        }
8113        ALOGV("checkSuspendOnEffectEnabled() disable restoring fx %08x",
8114             effect->desc().type.timeLow);
8115        sp<SuspendedEffectDesc> desc = mSuspendedEffects.valueAt(index);
8116        desc->mEffect.clear();
8117        effect->setSuspended(false);
8118    }
8119}
8120
8121#undef LOG_TAG
8122#define LOG_TAG "AudioFlinger"
8123
8124// ----------------------------------------------------------------------------
8125
8126status_t AudioFlinger::onTransact(
8127        uint32_t code, const Parcel& data, Parcel* reply, uint32_t flags)
8128{
8129    return BnAudioFlinger::onTransact(code, data, reply, flags);
8130}
8131
8132}; // namespace android
8133