Threads.cpp revision e83b55dc29ca16092ba02f36f55fa6e0e37fd78c
181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent/*
281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent**
381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** Copyright 2012, The Android Open Source Project
481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent**
581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** Licensed under the Apache License, Version 2.0 (the "License");
681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** you may not use this file except in compliance with the License.
781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** You may obtain a copy of the License at
881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent**
981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent**     http://www.apache.org/licenses/LICENSE-2.0
1081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent**
1181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** Unless required by applicable law or agreed to in writing, software
1281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** distributed under the License is distributed on an "AS IS" BASIS,
1381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** See the License for the specific language governing permissions and
1581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent** limitations under the License.
1681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent*/
1781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#define LOG_TAG "AudioFlinger"
2081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent//#define LOG_NDEBUG 0
21371eb9756c32109ea572b91216b19bb623f6d3fdAlex Ray#define ATRACE_TAG ATRACE_TAG_AUDIO
2281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
23153b9fe667e6e78e0218ff0159353097428c7657Glenn Kasten#include "Configuration.h"
2481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <math.h>
2581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <fcntl.h>
2681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <sys/stat.h>
2781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <cutils/properties.h>
281ab85ec401801ef9a9184650d0f5a1639b45eeb9Glenn Kasten#include <media/AudioParameter.h>
29cd04484f4837b8ca0041d118286ab6a98e84fc75Andy Hung#include <media/AudioResamplerPublic.h>
3081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <utils/Log.h>
31371eb9756c32109ea572b91216b19bb623f6d3fdAlex Ray#include <utils/Trace.h>
3281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
3381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <private/media/AudioTrackShared.h>
3481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <hardware/audio.h>
3581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <audio_effects/effect_ns.h>
3681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <audio_effects/effect_aec.h>
3781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <audio_utils/primitives.h>
3898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung#include <audio_utils/format.h>
39c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten#include <audio_utils/minifloat.h>
4081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
4181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// NBAIO implementations
426dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#include <media/nbaio/AudioStreamInSource.h>
4381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <media/nbaio/AudioStreamOutSink.h>
4481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <media/nbaio/MonoPipe.h>
4581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <media/nbaio/MonoPipeReader.h>
4681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <media/nbaio/Pipe.h>
4781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <media/nbaio/PipeReader.h>
4881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <media/nbaio/SourceAudioBufferProvider.h>
4981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
5081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <powermanager/PowerManager.h>
5181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
5281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <common_time/cc_helper.h>
5381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <common_time/local_clock.h>
5481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
5581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include "AudioFlinger.h"
5681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include "AudioMixer.h"
5781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include "FastMixer.h"
586dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#include "FastCapture.h"
5981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include "ServiceUtilities.h"
6081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include "SchedulingPolicyService.h"
6181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
6281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef ADD_BATTERY_DATA
6381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <media/IMediaPlayerService.h>
6481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <media/IMediaDeathNotifier.h>
6581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif
6681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
6781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef DEBUG_CPU_USAGE
6881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <cpustats/CentralTendencyStatistics.h>
6981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#include <cpustats/ThreadCpuUsage.h>
7081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif
7181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
7281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
7381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
7481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// Note: the following macro is used for extremely verbose logging message.  In
7581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// order to run with ALOG_ASSERT turned on, we need to have LOG_NDEBUG set to
7681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// 0; but one side effect of this is to turn all LOGV's as well.  Some messages
7781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// are so verbose that we want to suppress them even when we have ALOG_ASSERT
7881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// turned on.  Do not uncomment the #def below unless you really know what you
7981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// are doing and want to see all of the extremely verbose messages.
8081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent//#define VERY_VERY_VERBOSE_LOGGING
8181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef VERY_VERY_VERBOSE_LOGGING
8281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#define ALOGVV ALOGV
8381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#else
8481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#define ALOGVV(a...) do { } while(0)
8581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif
8681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
8749d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten#define max(a, b) ((a) > (b) ? (a) : (b))
8849d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten
8981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentnamespace android {
9081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
9181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// retry counts for buffer fill timeout
9281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// 50 * ~20msecs = 1 second
9381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatic const int8_t kMaxTrackRetries = 50;
9481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatic const int8_t kMaxTrackStartupRetries = 50;
9581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// allow less retry attempts on direct output thread.
9681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// direct outputs can be a scarce resource in audio hardware and should
9781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// be released as quickly as possible.
9881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatic const int8_t kMaxTrackRetriesDirect = 2;
9981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
10081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// don't warn about blocked writes or record buffer overflows more often than this
10181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatic const nsecs_t kWarningThrottleNs = seconds(5);
10281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
10381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// RecordThread loop sleep time upon application overrun or audio HAL read error
10481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatic const int kRecordThreadSleepUs = 5000;
10581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1061035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent// maximum time to wait in sendConfigEvent_l() for a status to be received
1071035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurentstatic const nsecs_t kConfigEventTimeoutNs = seconds(2);
10881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
10981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// minimum sleep time for the mixer thread loop when tracks are active but in underrun
11081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatic const uint32_t kMinThreadSleepTimeUs = 5000;
11181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// maximum divider applied to the active sleep time in the mixer thread loop
11281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatic const uint32_t kMaxThreadSleepTimeShift = 2;
11381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
11409a5007b17acb49d25cfa386a2e2534d942e8854Andy Hung// minimum normal sink buffer size, expressed in milliseconds rather than frames
11509a5007b17acb49d25cfa386a2e2534d942e8854Andy Hungstatic const uint32_t kMinNormalSinkBufferSizeMs = 20;
11609a5007b17acb49d25cfa386a2e2534d942e8854Andy Hung// maximum normal sink buffer size
11709a5007b17acb49d25cfa386a2e2534d942e8854Andy Hungstatic const uint32_t kMaxNormalSinkBufferSizeMs = 24;
11881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
119972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent// Offloaded output thread standby delay: allows track transition without going to standby
120972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurentstatic const nsecs_t kOffloadStandbyDelayNs = seconds(1);
121972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent
12281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// Whether to use fast mixer
12381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatic const enum {
12481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    FastMixer_Never,    // never initialize or use: for debugging only
12581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    FastMixer_Always,   // always initialize and use, even if not needed: for debugging only
12681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        // normal mixer multiplier is 1
12781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    FastMixer_Static,   // initialize if needed, then use all the time if initialized,
12881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        // multiplier is calculated based on min & max normal mixer buffer size
12981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    FastMixer_Dynamic,  // initialize if needed, then use dynamically depending on track load,
13081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        // multiplier is calculated based on min & max normal mixer buffer size
13181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // FIXME for FastMixer_Dynamic:
13281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    //  Supporting this option will require fixing HALs that can't handle large writes.
13381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    //  For example, one HAL implementation returns an error from a large write,
13481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    //  and another HAL implementation corrupts memory, possibly in the sample rate converter.
13581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    //  We could either fix the HAL implementations, or provide a wrapper that breaks
13681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    //  up large writes into smaller ones, and the wrapper would need to deal with scheduler.
13781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent} kUseFastMixer = FastMixer_Static;
13881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1396dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten// Whether to use fast capture
1406dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kastenstatic const enum {
1416dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    FastCapture_Never,  // never initialize or use: for debugging only
1426dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    FastCapture_Always, // always initialize and use, even if not needed: for debugging only
1436dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    FastCapture_Static, // initialize if needed, then use all the time if initialized
1446dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten} kUseFastCapture = FastCapture_Static;
1456dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten
14681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// Priorities for requestPriority
14781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatic const int kPriorityAudioApp = 2;
14881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatic const int kPriorityFastMixer = 3;
1496dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kastenstatic const int kPriorityFastCapture = 3;
15081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
15181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// IAudioFlinger::createTrack() reports back to client the total size of shared memory area
15281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// for the track.  The client then sub-divides this into smaller buffers for its use.
153b5fed68bcdd6f44424c9e4d12bfe9a3ff51bd62eGlenn Kasten// Currently the client uses N-buffering by default, but doesn't tell us about the value of N.
154b5fed68bcdd6f44424c9e4d12bfe9a3ff51bd62eGlenn Kasten// So for now we just assume that client is double-buffered for fast tracks.
155b5fed68bcdd6f44424c9e4d12bfe9a3ff51bd62eGlenn Kasten// FIXME It would be better for client to tell AudioFlinger the value of N,
156b5fed68bcdd6f44424c9e4d12bfe9a3ff51bd62eGlenn Kasten// so AudioFlinger could allocate the right amount of memory.
15781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// See the client's minBufCount and mNotificationFramesAct calculations for details.
1580349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten
1590349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten// This is the default value, if not specified by property.
160b5fed68bcdd6f44424c9e4d12bfe9a3ff51bd62eGlenn Kastenstatic const int kFastTrackMultiplier = 2;
16181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1620349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten// The minimum and maximum allowed values
1630349009fd19f89f8414c428f6b71b369f7546085Glenn Kastenstatic const int kFastTrackMultiplierMin = 1;
1640349009fd19f89f8414c428f6b71b369f7546085Glenn Kastenstatic const int kFastTrackMultiplierMax = 2;
1650349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten
1660349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten// The actual value to use, which can be specified per-device via property af.fast_track_multiplier.
1670349009fd19f89f8414c428f6b71b369f7546085Glenn Kastenstatic int sFastTrackMultiplier = kFastTrackMultiplier;
1680349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten
169b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten// See Thread::readOnlyHeap().
170b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten// Initially this heap is used to allocate client buffers for "fast" AudioRecord.
171b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten// Eventually it will be the single buffer that FastCapture writes into via HAL read(),
172b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten// and that all "fast" AudioRecord clients read from.  In either case, the size can be small.
1739f81de3452dfb2385bd57dc05456a045174a1ab1Glenn Kastenstatic const size_t kRecordThreadReadOnlyHeapSize = 0x2000;
174b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten
17581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
17681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1770349009fd19f89f8414c428f6b71b369f7546085Glenn Kastenstatic pthread_once_t sFastTrackMultiplierOnce = PTHREAD_ONCE_INIT;
1780349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten
1790349009fd19f89f8414c428f6b71b369f7546085Glenn Kastenstatic void sFastTrackMultiplierInit()
1800349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten{
1810349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten    char value[PROPERTY_VALUE_MAX];
1820349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten    if (property_get("af.fast_track_multiplier", value, NULL) > 0) {
1830349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten        char *endptr;
1840349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten        unsigned long ul = strtoul(value, &endptr, 0);
1850349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten        if (*endptr == '\0' && kFastTrackMultiplierMin <= ul && ul <= kFastTrackMultiplierMax) {
1860349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten            sFastTrackMultiplier = (int) ul;
1870349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten        }
1880349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten    }
1890349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten}
1900349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten
1910349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten// ----------------------------------------------------------------------------
1920349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten
19381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef ADD_BATTERY_DATA
19481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// To collect the amplifier usage
19581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatic void addBatteryData(uint32_t params) {
19681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<IMediaPlayerService> service = IMediaDeathNotifier::getMediaPlayerService();
19781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (service == NULL) {
19881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // it already logged
19981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return;
20081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
20181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
20281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    service->addBatteryData(params);
20381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
20481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif
20581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
20681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
20781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
20881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent//      CPU Stats
20981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
21081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
21181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentclass CpuStats {
21281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentpublic:
21381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    CpuStats();
21481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void sample(const String8 &title);
21581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef DEBUG_CPU_USAGE
21681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentprivate:
21781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ThreadCpuUsage mCpuUsage;           // instantaneous thread CPU usage in wall clock ns
21881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    CentralTendencyStatistics mWcStats; // statistics on thread CPU usage in wall clock ns
21981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
22081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    CentralTendencyStatistics mHzStats; // statistics on thread CPU usage in cycles
22181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
22281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int mCpuNum;                        // thread's current CPU number
22381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int mCpukHz;                        // frequency of thread's current CPU in kHz
22481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif
22581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent};
22681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
22781784c37c61b09289654b979567a42bf73cd2b12Eric LaurentCpuStats::CpuStats()
22881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef DEBUG_CPU_USAGE
22981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    : mCpuNum(-1), mCpukHz(-1)
23081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif
23181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
23281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
23381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
2340f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid CpuStats::sample(const String8 &title
2350f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kasten#ifndef DEBUG_CPU_USAGE
2360f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kasten                __unused
2370f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kasten#endif
2380f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kasten        ) {
23981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef DEBUG_CPU_USAGE
24081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // get current thread's delta CPU time in wall clock ns
24181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    double wcNs;
24281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool valid = mCpuUsage.sampleAndEnable(wcNs);
24381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
24481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // record sample for wall clock statistics
24581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (valid) {
24681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mWcStats.sample(wcNs);
24781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
24881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
24981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // get the current CPU number
25081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int cpuNum = sched_getcpu();
25181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
25281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // get the current CPU frequency in kHz
25381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int cpukHz = mCpuUsage.getCpukHz(cpuNum);
25481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
25581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // check if either CPU number or frequency changed
25681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (cpuNum != mCpuNum || cpukHz != mCpukHz) {
25781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mCpuNum = cpuNum;
25881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mCpukHz = cpukHz;
25981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // ignore sample for purposes of cycles
26081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        valid = false;
26181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
26281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
26381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // if no change in CPU number or frequency, then record sample for cycle statistics
26481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (valid && mCpukHz > 0) {
26581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        double cycles = wcNs * cpukHz * 0.000001;
26681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mHzStats.sample(cycles);
26781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
26881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
26981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    unsigned n = mWcStats.n();
27081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // mCpuUsage.elapsed() is expensive, so don't call it every loop
27181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if ((n & 127) == 1) {
27281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        long long elapsed = mCpuUsage.elapsed();
27381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (elapsed >= DEBUG_CPU_USAGE * 1000000000LL) {
27481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            double perLoop = elapsed / (double) n;
27581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            double perLoop100 = perLoop * 0.01;
27681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            double perLoop1k = perLoop * 0.001;
27781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            double mean = mWcStats.mean();
27881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            double stddev = mWcStats.stddev();
27981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            double minimum = mWcStats.minimum();
28081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            double maximum = mWcStats.maximum();
28181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            double meanCycles = mHzStats.mean();
28281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            double stddevCycles = mHzStats.stddev();
28381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            double minCycles = mHzStats.minimum();
28481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            double maxCycles = mHzStats.maximum();
28581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mCpuUsage.resetElapsed();
28681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mWcStats.reset();
28781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mHzStats.reset();
28881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGD("CPU usage for %s over past %.1f secs\n"
28981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                "  (%u mixer loops at %.1f mean ms per loop):\n"
29081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                "  us per mix loop: mean=%.0f stddev=%.0f min=%.0f max=%.0f\n"
29181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                "  %% of wall: mean=%.1f stddev=%.1f min=%.1f max=%.1f\n"
29281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                "  MHz: mean=%.1f, stddev=%.1f, min=%.1f max=%.1f",
29381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    title.string(),
29481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    elapsed * .000000001, n, perLoop * .000001,
29581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    mean * .001,
29681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    stddev * .001,
29781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    minimum * .001,
29881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    maximum * .001,
29981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    mean / perLoop100,
30081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    stddev / perLoop100,
30181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    minimum / perLoop100,
30281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    maximum / perLoop100,
30381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    meanCycles / perLoop1k,
30481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    stddevCycles / perLoop1k,
30581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    minCycles / perLoop1k,
30681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    maxCycles / perLoop1k);
30781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
30881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
30981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
31081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif
31181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent};
31281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
31381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
31481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent//      ThreadBase
31581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
31681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
31781784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::ThreadBase::ThreadBase(const sp<AudioFlinger>& audioFlinger, audio_io_handle_t id,
31881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        audio_devices_t outDevice, audio_devices_t inDevice, type_t type)
31981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    :   Thread(false /*canCallJava*/),
32081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mType(type),
3219b58f63e45ef2fdfb839b9b9bb3411d81eb96128Glenn Kasten        mAudioFlinger(audioFlinger),
32270949c47fbae3f836d15f040551d7631be3ed7c2Glenn Kasten        // mSampleRate, mFrameCount, mChannelMask, mChannelCount, mFrameSize, mFormat, mBufferSize
323deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten        // are set by PlaybackThread::readOutputParameters_l() or
324deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten        // RecordThread::readInputParameters_l()
325fd4779740ec3e9e865d5514464df26d015354388Eric Laurent        //FIXME: mStandby should be true here. Is this some kind of hack?
32681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mStandby(false), mOutDevice(outDevice), mInDevice(inDevice),
32781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mAudioSource(AUDIO_SOURCE_DEFAULT), mId(id),
32881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // mName will be set by concrete (non-virtual) subclass
32981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mDeathRecipient(new PMDeathRecipient(this))
33081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
33181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
33281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
33381784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::ThreadBase::~ThreadBase()
33481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
335c6ae3c8a261794fd4445e4e152d1ada074a3f92fGlenn Kasten    // mConfigEvents should be empty, but just in case it isn't, free the memory it owns
336c6ae3c8a261794fd4445e4e152d1ada074a3f92fGlenn Kasten    mConfigEvents.clear();
337c6ae3c8a261794fd4445e4e152d1ada074a3f92fGlenn Kasten
33881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // do not lock the mutex in destructor
33981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    releaseWakeLock_l();
34081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (mPowerManager != 0) {
34181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<IBinder> binder = mPowerManager->asBinder();
34281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        binder->unlinkToDeath(mDeathRecipient);
34381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
34481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
34581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
346cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kastenstatus_t AudioFlinger::ThreadBase::readyToRun()
347cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten{
348cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten    status_t status = initCheck();
349cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten    if (status == NO_ERROR) {
350cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten        ALOGI("AudioFlinger's thread %p ready to run", this);
351cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten    } else {
352cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten        ALOGE("No working audio driver found.");
353cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten    }
354cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten    return status;
355cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten}
356cf04c2cb8e031acc03c1c91cb1ccab15098c89b6Glenn Kasten
35781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::exit()
35881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
35981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("ThreadBase::exit");
36081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // do any cleanup required for exit to succeed
36181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    preExit();
36281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    {
36381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // This lock prevents the following race in thread (uniprocessor for illustration):
36481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        //  if (!exitPending()) {
36581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        //      // context switch from here to exit()
36681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        //      // exit() calls requestExit(), what exitPending() observes
36781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        //      // exit() calls signal(), which is dropped since no waiters
36881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        //      // context switch back from exit() to here
36981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        //      mWaitWorkCV.wait(...);
37081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        //      // now thread is hung
37181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        //  }
37281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        AutoMutex lock(mLock);
37381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        requestExit();
37481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mWaitWorkCV.broadcast();
37581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
37681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // When Thread::requestExitAndWait is made virtual and this method is renamed to
37781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // "virtual status_t requestExitAndWait()", replace by "return Thread::requestExitAndWait();"
37881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    requestExitAndWait();
37981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
38081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
38181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::ThreadBase::setParameters(const String8& keyValuePairs)
38281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
38381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t status;
38481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
38581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("ThreadBase::setParameters() %s", keyValuePairs.string());
38681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
38781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
3881035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    return sendSetParameterConfigEvent_l(keyValuePairs);
3891035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent}
3901035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent
3911035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent// sendConfigEvent_l() must be called with ThreadBase::mLock held
3921035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent// Can temporarily release the lock if waiting for a reply from processConfigEvents_l().
3931035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurentstatus_t AudioFlinger::ThreadBase::sendConfigEvent_l(sp<ConfigEvent>& event)
3941035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent{
3951035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    status_t status = NO_ERROR;
3961035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent
3971035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    mConfigEvents.add(event);
3981035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    ALOGV("sendConfigEvent_l() num events %d event %d", mConfigEvents.size(), event->mType);
39981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mWaitWorkCV.signal();
4001035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    mLock.unlock();
4011035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    {
4021035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        Mutex::Autolock _l(event->mLock);
4031035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        while (event->mWaitStatus) {
4041035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            if (event->mCond.waitRelative(event->mLock, kConfigEventTimeoutNs) != NO_ERROR) {
4051035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                event->mStatus = TIMED_OUT;
4061035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                event->mWaitStatus = false;
4071035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            }
4081035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        }
4091035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        status = event->mStatus;
41081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
4111035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    mLock.lock();
41281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return status;
41381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
41481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
41581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::sendIoConfigEvent(int event, int param)
41681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
41781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
41881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sendIoConfigEvent_l(event, param);
41981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
42081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
42181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// sendIoConfigEvent_l() must be called with ThreadBase::mLock held
42281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::sendIoConfigEvent_l(int event, int param)
42381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
4241035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    sp<ConfigEvent> configEvent = (ConfigEvent *)new IoConfigEvent(event, param);
4251035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    sendConfigEvent_l(configEvent);
42681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
42781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
42881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// sendPrioConfigEvent_l() must be called with ThreadBase::mLock held
42981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::sendPrioConfigEvent_l(pid_t pid, pid_t tid, int32_t prio)
43081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
4311035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    sp<ConfigEvent> configEvent = (ConfigEvent *)new PrioConfigEvent(pid, tid, prio);
4321035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    sendConfigEvent_l(configEvent);
43381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
43481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
4351035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent// sendSetParameterConfigEvent_l() must be called with ThreadBase::mLock held
4361035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurentstatus_t AudioFlinger::ThreadBase::sendSetParameterConfigEvent_l(const String8& keyValuePair)
43781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
4381035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    sp<ConfigEvent> configEvent = (ConfigEvent *)new SetParameterConfigEvent(keyValuePair);
4391035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    return sendConfigEvent_l(configEvent);
440f777331418a86cd9fd709af898ef24a69967aeb4Glenn Kasten}
441f777331418a86cd9fd709af898ef24a69967aeb4Glenn Kasten
4421c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurentstatus_t AudioFlinger::ThreadBase::sendCreateAudioPatchConfigEvent(
4431c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                                        const struct audio_patch *patch,
4441c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                                        audio_patch_handle_t *handle)
4451c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent{
4461c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    Mutex::Autolock _l(mLock);
4471c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    sp<ConfigEvent> configEvent = (ConfigEvent *)new CreateAudioPatchConfigEvent(*patch, *handle);
4481c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    status_t status = sendConfigEvent_l(configEvent);
4491c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    if (status == NO_ERROR) {
4501c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        CreateAudioPatchConfigEventData *data =
4511c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                        (CreateAudioPatchConfigEventData *)configEvent->mData.get();
4521c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        *handle = data->mHandle;
4531c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    }
4541c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    return status;
4551c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent}
4561c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
4571c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurentstatus_t AudioFlinger::ThreadBase::sendReleaseAudioPatchConfigEvent(
4581c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                                                const audio_patch_handle_t handle)
4591c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent{
4601c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    Mutex::Autolock _l(mLock);
4611c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    sp<ConfigEvent> configEvent = (ConfigEvent *)new ReleaseAudioPatchConfigEvent(handle);
4621c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    return sendConfigEvent_l(configEvent);
4631c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent}
4641c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
4651c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
4662cfbf88b89854f30b295e8ae26a031edb8d712f8Glenn Kasten// post condition: mConfigEvents.isEmpty()
467021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurentvoid AudioFlinger::ThreadBase::processConfigEvents_l()
468f777331418a86cd9fd709af898ef24a69967aeb4Glenn Kasten{
4691035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    bool configChanged = false;
4701035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent
47181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    while (!mConfigEvents.isEmpty()) {
4721035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        ALOGV("processConfigEvents_l() remaining events %d", mConfigEvents.size());
4731035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        sp<ConfigEvent> event = mConfigEvents[0];
47481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mConfigEvents.removeAt(0);
4751035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        switch (event->mType) {
4763468e8a4d79cc6a7bb0f03f8382426195bed44dfGlenn Kasten        case CFG_EVENT_PRIO: {
4771035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            PrioConfigEventData *data = (PrioConfigEventData *)event->mData.get();
4781035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            // FIXME Need to understand why this has to be done asynchronously
4791035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            int err = requestPriority(data->mPid, data->mTid, data->mPrio,
4803468e8a4d79cc6a7bb0f03f8382426195bed44dfGlenn Kasten                    true /*asynchronous*/);
4813468e8a4d79cc6a7bb0f03f8382426195bed44dfGlenn Kasten            if (err != 0) {
4823468e8a4d79cc6a7bb0f03f8382426195bed44dfGlenn Kasten                ALOGW("Policy SCHED_FIFO priority %d is unavailable for pid %d tid %d; error %d",
4831035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                      data->mPrio, data->mPid, data->mTid, err);
4843468e8a4d79cc6a7bb0f03f8382426195bed44dfGlenn Kasten            }
4853468e8a4d79cc6a7bb0f03f8382426195bed44dfGlenn Kasten        } break;
4863468e8a4d79cc6a7bb0f03f8382426195bed44dfGlenn Kasten        case CFG_EVENT_IO: {
4871035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            IoConfigEventData *data = (IoConfigEventData *)event->mData.get();
488021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent            audioConfigChanged(data->mEvent, data->mParam);
4891035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        } break;
4901035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        case CFG_EVENT_SET_PARAMETER: {
4911035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            SetParameterConfigEventData *data = (SetParameterConfigEventData *)event->mData.get();
4921035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            if (checkForNewParameter_l(data->mKeyValuePairs, event->mStatus)) {
4931035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                configChanged = true;
494d5418eb594435c958d6c37fa9938161a0112adbdGlenn Kasten            }
4953468e8a4d79cc6a7bb0f03f8382426195bed44dfGlenn Kasten        } break;
4961c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        case CFG_EVENT_CREATE_AUDIO_PATCH: {
4971c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent            CreateAudioPatchConfigEventData *data =
4981c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                            (CreateAudioPatchConfigEventData *)event->mData.get();
4991c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent            event->mStatus = createAudioPatch_l(&data->mPatch, &data->mHandle);
5001c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        } break;
5011c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        case CFG_EVENT_RELEASE_AUDIO_PATCH: {
5021c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent            ReleaseAudioPatchConfigEventData *data =
5031c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                            (ReleaseAudioPatchConfigEventData *)event->mData.get();
5041c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent            event->mStatus = releaseAudioPatch_l(data->mHandle);
5051c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        } break;
5063468e8a4d79cc6a7bb0f03f8382426195bed44dfGlenn Kasten        default:
5071035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            ALOG_ASSERT(false, "processConfigEvents_l() unknown event type %d", event->mType);
5083468e8a4d79cc6a7bb0f03f8382426195bed44dfGlenn Kasten            break;
50981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
5101035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        {
5111035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            Mutex::Autolock _l(event->mLock);
5121035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            if (event->mWaitStatus) {
5131035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                event->mWaitStatus = false;
5141035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                event->mCond.signal();
5151035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            }
5161035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        }
5171035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        ALOGV_IF(mConfigEvents.isEmpty(), "processConfigEvents_l() DONE thread %p", this);
5181035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    }
5191035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent
5201035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    if (configChanged) {
5211035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        cacheParameters_l();
52281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
52381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
52481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
525b220884bf3129253cc5bc8d030bc475411ea4911Marco NelissenString8 channelMaskToString(audio_channel_mask_t mask, bool output) {
526b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    String8 s;
527b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    if (output) {
528b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        if (mask & AUDIO_CHANNEL_OUT_FRONT_LEFT) s.append("front-left, ");
529b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        if (mask & AUDIO_CHANNEL_OUT_FRONT_RIGHT) s.append("front-right, ");
530b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        if (mask & AUDIO_CHANNEL_OUT_FRONT_CENTER) s.append("front-center, ");
531b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        if (mask & AUDIO_CHANNEL_OUT_LOW_FREQUENCY) s.append("low freq, ");
532b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        if (mask & AUDIO_CHANNEL_OUT_BACK_LEFT) s.append("back-left, ");
533b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        if (mask & AUDIO_CHANNEL_OUT_BACK_RIGHT) s.append("back-right, ");
534b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        if (mask & AUDIO_CHANNEL_OUT_FRONT_LEFT_OF_CENTER) s.append("front-left-of-center, ");
535b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        if (mask & AUDIO_CHANNEL_OUT_FRONT_RIGHT_OF_CENTER) s.append("front-right-of-center, ");
536b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        if (mask & AUDIO_CHANNEL_OUT_BACK_CENTER) s.append("back-center, ");
537b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        if (mask & AUDIO_CHANNEL_OUT_SIDE_LEFT) s.append("side-left, ");
538b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        if (mask & AUDIO_CHANNEL_OUT_SIDE_RIGHT) s.append("side-right, ");
539b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        if (mask & AUDIO_CHANNEL_OUT_TOP_CENTER) s.append("top-center ,");
540b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_LEFT) s.append("top-front-left, ");
541b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_CENTER) s.append("top-front-center, ");
542b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        if (mask & AUDIO_CHANNEL_OUT_TOP_FRONT_RIGHT) s.append("top-front-right, ");
543b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_LEFT) s.append("top-back-left, ");
544b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_CENTER) s.append("top-back-center, " );
545b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        if (mask & AUDIO_CHANNEL_OUT_TOP_BACK_RIGHT) s.append("top-back-right, " );
546b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        if (mask & ~AUDIO_CHANNEL_OUT_ALL) s.append("unknown,  ");
547b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    } else {
548b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        if (mask & AUDIO_CHANNEL_IN_LEFT) s.append("left, ");
549b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        if (mask & AUDIO_CHANNEL_IN_RIGHT) s.append("right, ");
550b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        if (mask & AUDIO_CHANNEL_IN_FRONT) s.append("front, ");
551b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        if (mask & AUDIO_CHANNEL_IN_BACK) s.append("back, ");
552b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        if (mask & AUDIO_CHANNEL_IN_LEFT_PROCESSED) s.append("left-processed, ");
553b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        if (mask & AUDIO_CHANNEL_IN_RIGHT_PROCESSED) s.append("right-processed, ");
554b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        if (mask & AUDIO_CHANNEL_IN_FRONT_PROCESSED) s.append("front-processed, ");
555b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        if (mask & AUDIO_CHANNEL_IN_BACK_PROCESSED) s.append("back-processed, ");
556b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        if (mask & AUDIO_CHANNEL_IN_PRESSURE) s.append("pressure, ");
557b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        if (mask & AUDIO_CHANNEL_IN_X_AXIS) s.append("X, ");
558b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        if (mask & AUDIO_CHANNEL_IN_Y_AXIS) s.append("Y, ");
559b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        if (mask & AUDIO_CHANNEL_IN_Z_AXIS) s.append("Z, ");
560b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        if (mask & AUDIO_CHANNEL_IN_VOICE_UPLINK) s.append("voice-uplink, ");
561b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        if (mask & AUDIO_CHANNEL_IN_VOICE_DNLINK) s.append("voice-dnlink, ");
562b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        if (mask & ~AUDIO_CHANNEL_IN_ALL) s.append("unknown,  ");
563b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    }
564b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    int len = s.length();
565b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    if (s.length() > 2) {
566b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        char *str = s.lockBuffer(len);
567b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        s.unlockBuffer(len - 2);
568b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    }
569b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    return s;
570b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen}
571b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen
5720f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid AudioFlinger::ThreadBase::dumpBase(int fd, const Vector<String16>& args __unused)
57381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
57481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    const size_t SIZE = 256;
57581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    char buffer[SIZE];
57681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    String8 result;
57781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
57881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool locked = AudioFlinger::dumpTryLock(mLock);
57981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (!locked) {
58087cebadd48710e42474756fc3513df678de045ceElliott Hughes        dprintf(fd, "thread %p maybe dead locked\n", this);
581b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    }
582b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen
58387cebadd48710e42474756fc3513df678de045ceElliott Hughes    dprintf(fd, "  I/O handle: %d\n", mId);
58487cebadd48710e42474756fc3513df678de045ceElliott Hughes    dprintf(fd, "  TID: %d\n", getTid());
58587cebadd48710e42474756fc3513df678de045ceElliott Hughes    dprintf(fd, "  Standby: %s\n", mStandby ? "yes" : "no");
58687cebadd48710e42474756fc3513df678de045ceElliott Hughes    dprintf(fd, "  Sample rate: %u\n", mSampleRate);
58787cebadd48710e42474756fc3513df678de045ceElliott Hughes    dprintf(fd, "  HAL frame count: %zu\n", mFrameCount);
58887cebadd48710e42474756fc3513df678de045ceElliott Hughes    dprintf(fd, "  HAL buffer size: %u bytes\n", mBufferSize);
58987cebadd48710e42474756fc3513df678de045ceElliott Hughes    dprintf(fd, "  Channel Count: %u\n", mChannelCount);
59087cebadd48710e42474756fc3513df678de045ceElliott Hughes    dprintf(fd, "  Channel Mask: 0x%08x (%s)\n", mChannelMask,
591b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen            channelMaskToString(mChannelMask, mType != RECORD).string());
592463be250de73907965faa6a216c00312bf81e049Andy Hung    dprintf(fd, "  Format: 0x%x (%s)\n", mHALFormat, formatToString(mHALFormat));
59387cebadd48710e42474756fc3513df678de045ceElliott Hughes    dprintf(fd, "  Frame size: %zu\n", mFrameSize);
59487cebadd48710e42474756fc3513df678de045ceElliott Hughes    dprintf(fd, "  Pending config events:");
595b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    size_t numConfig = mConfigEvents.size();
596b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    if (numConfig) {
597b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        for (size_t i = 0; i < numConfig; i++) {
598b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen            mConfigEvents[i]->dump(buffer, SIZE);
59987cebadd48710e42474756fc3513df678de045ceElliott Hughes            dprintf(fd, "\n    %s", buffer);
600b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        }
60187cebadd48710e42474756fc3513df678de045ceElliott Hughes        dprintf(fd, "\n");
602b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    } else {
60387cebadd48710e42474756fc3513df678de045ceElliott Hughes        dprintf(fd, " none\n");
60481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
60581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
60681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (locked) {
60781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mLock.unlock();
60881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
60981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
61081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
61181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::dumpEffectChains(int fd, const Vector<String16>& args)
61281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
61381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    const size_t SIZE = 256;
61481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    char buffer[SIZE];
61581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    String8 result;
61681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
617b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    size_t numEffectChains = mEffectChains.size();
6181d6fa7af1288b550faabe4ec2cf98684236723dbNarayan Kamath    snprintf(buffer, SIZE, "  %zu Effect Chains\n", numEffectChains);
61981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    write(fd, buffer, strlen(buffer));
62081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
621b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    for (size_t i = 0; i < numEffectChains; ++i) {
62281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<EffectChain> chain = mEffectChains[i];
62381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (chain != 0) {
62481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            chain->dump(fd, args);
62581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
62681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
62781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
62881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
629e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissenvoid AudioFlinger::ThreadBase::acquireWakeLock(int uid)
63081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
63181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
632e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissen    acquireWakeLock_l(uid);
63381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
63481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
635014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan KamathString16 AudioFlinger::ThreadBase::getWakeLockTag()
636014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath{
637014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath    switch (mType) {
638014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath        case MIXER:
639014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath            return String16("AudioMix");
640014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath        case DIRECT:
641014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath            return String16("AudioDirectOut");
642014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath        case DUPLICATING:
643014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath            return String16("AudioDup");
644014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath        case RECORD:
645014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath            return String16("AudioIn");
646014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath        case OFFLOAD:
647014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath            return String16("AudioOffload");
648014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath        default:
649014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath            ALOG_ASSERT(false);
650014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath            return String16("AudioUnknown");
651014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath    }
652014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath}
653014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath
654e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissenvoid AudioFlinger::ThreadBase::acquireWakeLock_l(int uid)
65581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
656462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen    getPowerManager_l();
65781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (mPowerManager != 0) {
65881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<IBinder> binder = new BBinder();
659e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissen        status_t status;
660e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissen        if (uid >= 0) {
661547789d25dc6bd6561553bcf6b384fb0d4fee834Eric Laurent            status = mPowerManager->acquireWakeLockWithUid(POWERMANAGER_PARTIAL_WAKE_LOCK,
662e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissen                    binder,
663014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath                    getWakeLockTag(),
664e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissen                    String16("media"),
6653abc2ded40066f3b1df23aceb553f22d569c5cd3Glenn Kasten                    uid,
6663abc2ded40066f3b1df23aceb553f22d569c5cd3Glenn Kasten                    true /* FIXME force oneway contrary to .aidl */);
667e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissen        } else {
668547789d25dc6bd6561553bcf6b384fb0d4fee834Eric Laurent            status = mPowerManager->acquireWakeLock(POWERMANAGER_PARTIAL_WAKE_LOCK,
669e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissen                    binder,
670014e7fa2e90827d911c37bb0ce4d2e10e14d0bb3Narayan Kamath                    getWakeLockTag(),
6713abc2ded40066f3b1df23aceb553f22d569c5cd3Glenn Kasten                    String16("media"),
6723abc2ded40066f3b1df23aceb553f22d569c5cd3Glenn Kasten                    true /* FIXME force oneway contrary to .aidl */);
673e14a5d6d2cc91dd2fc09ffdf7aa670b37da0795dMarco Nelissen        }
67481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (status == NO_ERROR) {
67581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mWakeLockToken = binder;
67681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
67781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGV("acquireWakeLock_l() %s status %d", mName, status);
67881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
67981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
68081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
68181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::releaseWakeLock()
68281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
68381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
68481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    releaseWakeLock_l();
68581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
68681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
68781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::releaseWakeLock_l()
68881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
68981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (mWakeLockToken != 0) {
69081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGV("releaseWakeLock_l() %s", mName);
69181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (mPowerManager != 0) {
6923abc2ded40066f3b1df23aceb553f22d569c5cd3Glenn Kasten            mPowerManager->releaseWakeLock(mWakeLockToken, 0,
6933abc2ded40066f3b1df23aceb553f22d569c5cd3Glenn Kasten                    true /* FIXME force oneway contrary to .aidl */);
69481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
69581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mWakeLockToken.clear();
69681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
69781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
69881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
699462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissenvoid AudioFlinger::ThreadBase::updateWakeLockUids(const SortedVector<int> &uids) {
700462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen    Mutex::Autolock _l(mLock);
701462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen    updateWakeLockUids_l(uids);
702462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen}
703462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen
704462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissenvoid AudioFlinger::ThreadBase::getPowerManager_l() {
705462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen
706462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen    if (mPowerManager == 0) {
707462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen        // use checkService() to avoid blocking if power service is not up yet
708462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen        sp<IBinder> binder =
709462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen            defaultServiceManager()->checkService(String16("power"));
710462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen        if (binder == 0) {
711462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen            ALOGW("Thread %s cannot connect to the power manager service", mName);
712462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen        } else {
713462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen            mPowerManager = interface_cast<IPowerManager>(binder);
714462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen            binder->linkToDeath(mDeathRecipient);
715462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen        }
716462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen    }
717462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen}
718462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen
719462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissenvoid AudioFlinger::ThreadBase::updateWakeLockUids_l(const SortedVector<int> &uids) {
720462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen
721462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen    getPowerManager_l();
722462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen    if (mWakeLockToken == NULL) {
723462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen        ALOGE("no wake lock to update!");
724462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen        return;
725462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen    }
726462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen    if (mPowerManager != 0) {
727462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen        sp<IBinder> binder = new BBinder();
728462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen        status_t status;
7293abc2ded40066f3b1df23aceb553f22d569c5cd3Glenn Kasten        status = mPowerManager->updateWakeLockUids(mWakeLockToken, uids.size(), uids.array(),
7303abc2ded40066f3b1df23aceb553f22d569c5cd3Glenn Kasten                    true /* FIXME force oneway contrary to .aidl */);
731462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen        ALOGV("acquireWakeLock_l() %s status %d", mName, status);
732462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen    }
733462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen}
734462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen
73581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::clearPowerManager()
73681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
73781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
73881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    releaseWakeLock_l();
73981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mPowerManager.clear();
74081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
74181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
7420f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid AudioFlinger::ThreadBase::PMDeathRecipient::binderDied(const wp<IBinder>& who __unused)
74381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
74481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<ThreadBase> thread = mThread.promote();
74581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (thread != 0) {
74681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        thread->clearPowerManager();
74781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
74881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGW("power manager service died !!!");
74981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
75081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
75181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::setEffectSuspended(
75281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        const effect_uuid_t *type, bool suspend, int sessionId)
75381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
75481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
75581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    setEffectSuspended_l(type, suspend, sessionId);
75681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
75781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
75881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::setEffectSuspended_l(
75981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        const effect_uuid_t *type, bool suspend, int sessionId)
76081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
76181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<EffectChain> chain = getEffectChain_l(sessionId);
76281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (chain != 0) {
76381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (type != NULL) {
76481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            chain->setEffectSuspended_l(type, suspend);
76581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else {
76681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            chain->setEffectSuspendedAll_l(suspend);
76781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
76881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
76981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
77081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    updateSuspendedSessions_l(type, suspend, sessionId);
77181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
77281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
77381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::checkSuspendOnAddEffectChain_l(const sp<EffectChain>& chain)
77481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
77581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ssize_t index = mSuspendedSessions.indexOfKey(chain->sessionId());
77681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (index < 0) {
77781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return;
77881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
77981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
78081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    const KeyedVector <int, sp<SuspendedSessionDesc> >& sessionEffects =
78181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mSuspendedSessions.valueAt(index);
78281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
78381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < sessionEffects.size(); i++) {
78481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<SuspendedSessionDesc> desc = sessionEffects.valueAt(i);
78581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        for (int j = 0; j < desc->mRefCount; j++) {
78681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (sessionEffects.keyAt(i) == EffectChain::kKeyForSuspendAll) {
78781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                chain->setEffectSuspendedAll_l(true);
78881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            } else {
78981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGV("checkSuspendOnAddEffectChain_l() suspending effects %08x",
79081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    desc->mType.timeLow);
79181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                chain->setEffectSuspended_l(&desc->mType, true);
79281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
79381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
79481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
79581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
79681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
79781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::updateSuspendedSessions_l(const effect_uuid_t *type,
79881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                         bool suspend,
79981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                         int sessionId)
80081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
80181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ssize_t index = mSuspendedSessions.indexOfKey(sessionId);
80281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
80381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    KeyedVector <int, sp<SuspendedSessionDesc> > sessionEffects;
80481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
80581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (suspend) {
80681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (index >= 0) {
80781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sessionEffects = mSuspendedSessions.valueAt(index);
80881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else {
80981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mSuspendedSessions.add(sessionId, sessionEffects);
81081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
81181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    } else {
81281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (index < 0) {
81381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return;
81481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
81581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sessionEffects = mSuspendedSessions.valueAt(index);
81681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
81781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
81881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
81981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int key = EffectChain::kKeyForSuspendAll;
82081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (type != NULL) {
82181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        key = type->timeLow;
82281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
82381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    index = sessionEffects.indexOfKey(key);
82481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
82581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<SuspendedSessionDesc> desc;
82681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (suspend) {
82781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (index >= 0) {
82881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            desc = sessionEffects.valueAt(index);
82981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else {
83081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            desc = new SuspendedSessionDesc();
83181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (type != NULL) {
83281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                desc->mType = *type;
83381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
83481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sessionEffects.add(key, desc);
83581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGV("updateSuspendedSessions_l() suspend adding effect %08x", key);
83681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
83781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        desc->mRefCount++;
83881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    } else {
83981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (index < 0) {
84081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return;
84181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
84281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        desc = sessionEffects.valueAt(index);
84381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (--desc->mRefCount == 0) {
84481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGV("updateSuspendedSessions_l() restore removing effect %08x", key);
84581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sessionEffects.removeItemsAt(index);
84681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (sessionEffects.isEmpty()) {
84781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGV("updateSuspendedSessions_l() restore removing session %d",
84881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                 sessionId);
84981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                mSuspendedSessions.removeItem(sessionId);
85081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
85181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
85281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
85381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (!sessionEffects.isEmpty()) {
85481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mSuspendedSessions.replaceValueFor(sessionId, sessionEffects);
85581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
85681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
85781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
85881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled(const sp<EffectModule>& effect,
85981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                            bool enabled,
86081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                            int sessionId)
86181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
86281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
86381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    checkSuspendOnEffectEnabled_l(effect, enabled, sessionId);
86481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
86581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
86681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::checkSuspendOnEffectEnabled_l(const sp<EffectModule>& effect,
86781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                            bool enabled,
86881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                                            int sessionId)
86981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
87081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (mType != RECORD) {
87181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // suspend all effects in AUDIO_SESSION_OUTPUT_MIX when enabling any effect on
87281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // another session. This gives the priority to well behaved effect control panels
87381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // and applications not using global effects.
87481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // Enabling post processing in AUDIO_SESSION_OUTPUT_STAGE session does not affect
87581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // global effects
87681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if ((sessionId != AUDIO_SESSION_OUTPUT_MIX) && (sessionId != AUDIO_SESSION_OUTPUT_STAGE)) {
87781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            setEffectSuspended_l(NULL, enabled, AUDIO_SESSION_OUTPUT_MIX);
87881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
87981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
88081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
88181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<EffectChain> chain = getEffectChain_l(sessionId);
88281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (chain != 0) {
88381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        chain->checkSuspendOnEffectEnabled(effect, enabled);
88481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
88581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
88681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
88781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ThreadBase::createEffect_l() must be called with AudioFlinger::mLock held
88881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<AudioFlinger::EffectHandle> AudioFlinger::ThreadBase::createEffect_l(
88981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        const sp<AudioFlinger::Client>& client,
89081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        const sp<IEffectClient>& effectClient,
89181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        int32_t priority,
89281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        int sessionId,
89381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        effect_descriptor_t *desc,
89481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        int *enabled,
8959156ef3e11b68cc4b6d3cea77f1f63673855a6d1Glenn Kasten        status_t *status)
89681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
89781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<EffectModule> effect;
89881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<EffectHandle> handle;
89981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t lStatus;
90081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<EffectChain> chain;
90181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool chainCreated = false;
90281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool effectCreated = false;
90381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool effectRegistered = false;
90481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
90581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    lStatus = initCheck();
90681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (lStatus != NO_ERROR) {
90781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGW("createEffect_l() Audio driver not initialized.");
90881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        goto Exit;
90981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
91081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
91198ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // Reject any effect on Direct output threads for now, since the format of
91298ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    // mSinkBuffer is not guaranteed to be compatible with effect processing (PCM 16 stereo).
91398ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    if (mType == DIRECT) {
91498ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung        ALOGW("createEffect_l() Cannot add effect %s on Direct output type thread %s",
91598ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung                desc->name, mName);
91698ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung        lStatus = BAD_VALUE;
91798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung        goto Exit;
91898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    }
91998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung
920389cfdbb9a92a438a0d7710321c2964c7ad55ecaAndy Hung    // Reject any effect on mixer or duplicating multichannel sinks.
9219a59276fb465e492138e0576523b54079671e8f4Andy Hung    // TODO: fix both format and multichannel issues with effects.
922389cfdbb9a92a438a0d7710321c2964c7ad55ecaAndy Hung    if ((mType == MIXER || mType == DUPLICATING) && mChannelCount != FCC_2) {
923389cfdbb9a92a438a0d7710321c2964c7ad55ecaAndy Hung        ALOGW("createEffect_l() Cannot add effect %s for multichannel(%d) %s threads",
924389cfdbb9a92a438a0d7710321c2964c7ad55ecaAndy Hung                desc->name, mChannelCount, mType == MIXER ? "MIXER" : "DUPLICATING");
9259a59276fb465e492138e0576523b54079671e8f4Andy Hung        lStatus = BAD_VALUE;
9269a59276fb465e492138e0576523b54079671e8f4Andy Hung        goto Exit;
9279a59276fb465e492138e0576523b54079671e8f4Andy Hung    }
9289a59276fb465e492138e0576523b54079671e8f4Andy Hung
9295baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent    // Allow global effects only on offloaded and mixer threads
9305baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent    if (sessionId == AUDIO_SESSION_OUTPUT_MIX) {
9315baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        switch (mType) {
9325baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        case MIXER:
9335baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        case OFFLOAD:
9345baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent            break;
9355baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        case DIRECT:
9365baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        case DUPLICATING:
9375baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        case RECORD:
9385baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        default:
9395baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent            ALOGW("createEffect_l() Cannot add global effect %s on thread %s", desc->name, mName);
9405baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent            lStatus = BAD_VALUE;
9415baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent            goto Exit;
9425baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent        }
94381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
9445baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent
94581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Only Pre processor effects are allowed on input threads and only on input threads
94681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if ((mType == RECORD) != ((desc->flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_PRE_PROC)) {
94781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGW("createEffect_l() effect %s (flags %08x) created on wrong thread type %d",
94881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                desc->name, desc->flags, mType);
94981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        lStatus = BAD_VALUE;
95081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        goto Exit;
95181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
95281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
95381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("createEffect_l() thread %p effect %s on session %d", this, desc->name, sessionId);
95481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
95581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    { // scope for mLock
95681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        Mutex::Autolock _l(mLock);
95781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
95881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // check for existing effect chain with the requested audio session
95981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        chain = getEffectChain_l(sessionId);
96081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (chain == 0) {
96181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // create a new chain for this session
96281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGV("createEffect_l() new effect chain for session %d", sessionId);
96381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            chain = new EffectChain(this, sessionId);
96481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            addEffectChain_l(chain);
96581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            chain->setStrategy(getStrategyForSession_l(sessionId));
96681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            chainCreated = true;
96781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else {
96881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            effect = chain->getEffectFromDesc_l(desc);
96981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
97081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
97181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGV("createEffect_l() got effect %p on chain %p", effect.get(), chain.get());
97281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
97381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (effect == 0) {
97481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            int id = mAudioFlinger->nextUniqueId();
97581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // Check CPU and memory usage
97681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            lStatus = AudioSystem::registerEffect(desc, mId, chain->strategy(), sessionId, id);
97781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (lStatus != NO_ERROR) {
97881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                goto Exit;
97981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
98081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            effectRegistered = true;
98181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // create a new effect module if none present in the chain
98281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            effect = new EffectModule(this, chain, desc, id, sessionId);
98381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            lStatus = effect->status();
98481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (lStatus != NO_ERROR) {
98581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                goto Exit;
98681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
9875baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent            effect->setOffloaded(mType == OFFLOAD, mId);
9885baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent
98981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            lStatus = chain->addEffect_l(effect);
99081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (lStatus != NO_ERROR) {
99181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                goto Exit;
99281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
99381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            effectCreated = true;
99481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
99581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            effect->setDevice(mOutDevice);
99681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            effect->setDevice(mInDevice);
99781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            effect->setMode(mAudioFlinger->getMode());
99881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            effect->setAudioSource(mAudioSource);
99981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
100081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // create effect handle and connect it to effect module
100181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        handle = new EffectHandle(effect, client, effectClient, priority);
1002e75da4004b2c814987aa2adf8a76190f92d99c65Glenn Kasten        lStatus = handle->initCheck();
1003e75da4004b2c814987aa2adf8a76190f92d99c65Glenn Kasten        if (lStatus == OK) {
1004e75da4004b2c814987aa2adf8a76190f92d99c65Glenn Kasten            lStatus = effect->addHandle(handle.get());
1005e75da4004b2c814987aa2adf8a76190f92d99c65Glenn Kasten        }
100681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (enabled != NULL) {
100781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            *enabled = (int)effect->isEnabled();
100881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
100981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
101081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
101181784c37c61b09289654b979567a42bf73cd2b12Eric LaurentExit:
101281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (lStatus != NO_ERROR && lStatus != ALREADY_EXISTS) {
101381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        Mutex::Autolock _l(mLock);
101481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (effectCreated) {
101581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            chain->removeEffect_l(effect);
101681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
101781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (effectRegistered) {
101881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            AudioSystem::unregisterEffect(effect->id());
101981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
102081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (chainCreated) {
102181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            removeEffectChain_l(chain);
102281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
102381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        handle.clear();
102481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
102581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
10269156ef3e11b68cc4b6d3cea77f1f63673855a6d1Glenn Kasten    *status = lStatus;
102781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return handle;
102881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
102981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
103081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect(int sessionId, int effectId)
103181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
103281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
103381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return getEffect_l(sessionId, effectId);
103481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
103581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
103681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<AudioFlinger::EffectModule> AudioFlinger::ThreadBase::getEffect_l(int sessionId, int effectId)
103781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
103881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<EffectChain> chain = getEffectChain_l(sessionId);
103981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return chain != 0 ? chain->getEffectFromId_l(effectId) : 0;
104081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
104181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
104281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// PlaybackThread::addEffect_l() must be called with AudioFlinger::mLock and
104381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// PlaybackThread::mLock held
104481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::ThreadBase::addEffect_l(const sp<EffectModule>& effect)
104581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
104681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // check for existing effect chain with the requested audio session
104781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int sessionId = effect->sessionId();
104881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<EffectChain> chain = getEffectChain_l(sessionId);
104981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool chainCreated = false;
105081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
10515baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent    ALOGD_IF((mType == OFFLOAD) && !effect->isOffloadable(),
10525baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent             "addEffect_l() on offloaded thread %p: effect %s does not support offload flags %x",
10535baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent                    this, effect->desc().name, effect->desc().flags);
10545baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent
105581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (chain == 0) {
105681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // create a new chain for this session
105781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGV("addEffect_l() new effect chain for session %d", sessionId);
105881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        chain = new EffectChain(this, sessionId);
105981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        addEffectChain_l(chain);
106081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        chain->setStrategy(getStrategyForSession_l(sessionId));
106181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        chainCreated = true;
106281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
106381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("addEffect_l() %p chain %p effect %p", this, chain.get(), effect.get());
106481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
106581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (chain->getEffectFromId_l(effect->id()) != 0) {
106681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGW("addEffect_l() %p effect %s already present in chain %p",
106781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                this, effect->desc().name, chain.get());
106881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return BAD_VALUE;
106981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
107081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
10715baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent    effect->setOffloaded(mType == OFFLOAD, mId);
10725baf2af52cd186633b7173196c1e4a4cd3435f22Eric Laurent
107381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t status = chain->addEffect_l(effect);
107481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (status != NO_ERROR) {
107581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (chainCreated) {
107681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            removeEffectChain_l(chain);
107781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
107881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return status;
107981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
108081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
108181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    effect->setDevice(mOutDevice);
108281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    effect->setDevice(mInDevice);
108381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    effect->setMode(mAudioFlinger->getMode());
108481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    effect->setAudioSource(mAudioSource);
108581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return NO_ERROR;
108681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
108781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
108881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::removeEffect_l(const sp<EffectModule>& effect) {
108981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
109081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("removeEffect_l() %p effect %p", this, effect.get());
109181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    effect_descriptor_t desc = effect->desc();
109281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if ((desc.flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
109381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        detachAuxEffect_l(effect->id());
109481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
109581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
109681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<EffectChain> chain = effect->chain().promote();
109781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (chain != 0) {
109881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // remove effect chain if removing last effect
109981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (chain->removeEffect_l(effect) == 0) {
110081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            removeEffectChain_l(chain);
110181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
110281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    } else {
110381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGW("removeEffect_l() %p cannot promote chain for effect %p", this, effect.get());
110481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
110581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
110681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
110781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::lockEffectChains_l(
110881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        Vector< sp<AudioFlinger::EffectChain> >& effectChains)
110981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
111081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    effectChains = mEffectChains;
111181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mEffectChains.size(); i++) {
111281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mEffectChains[i]->lock();
111381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
111481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
111581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
111681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::unlockEffectChains(
111781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        const Vector< sp<AudioFlinger::EffectChain> >& effectChains)
111881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
111981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < effectChains.size(); i++) {
112081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        effectChains[i]->unlock();
112181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
112281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
112381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
112481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain(int sessionId)
112581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
112681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
112781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return getEffectChain_l(sessionId);
112881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
112981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
113081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<AudioFlinger::EffectChain> AudioFlinger::ThreadBase::getEffectChain_l(int sessionId) const
113181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
113281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    size_t size = mEffectChains.size();
113381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < size; i++) {
113481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (mEffectChains[i]->sessionId() == sessionId) {
113581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return mEffectChains[i];
113681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
113781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
113881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return 0;
113981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
114081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
114181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::ThreadBase::setMode(audio_mode_t mode)
114281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
114381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
114481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    size_t size = mEffectChains.size();
114581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < size; i++) {
114681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mEffectChains[i]->setMode_l(mode);
114781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
114881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
114981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
115083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentvoid AudioFlinger::ThreadBase::getAudioPortConfig(struct audio_port_config *config)
115183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{
115283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    config->type = AUDIO_PORT_TYPE_MIX;
115383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    config->ext.mix.handle = mId;
115483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    config->sample_rate = mSampleRate;
115583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    config->format = mFormat;
115683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    config->channel_mask = mChannelMask;
115783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    config->config_mask = AUDIO_PORT_CONFIG_SAMPLE_RATE|AUDIO_PORT_CONFIG_CHANNEL_MASK|
115883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent                            AUDIO_PORT_CONFIG_FORMAT;
115983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent}
116083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
116183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
116281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
116381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent//      Playback
116481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
116581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
116681784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::PlaybackThread::PlaybackThread(const sp<AudioFlinger>& audioFlinger,
116781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                             AudioStreamOut* output,
116881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                             audio_io_handle_t id,
116981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                             audio_devices_t device,
117081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                             type_t type)
117181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    :   ThreadBase(audioFlinger, id, device, AUDIO_DEVICE_NONE, type),
11722098f2744cedf2dc3fa36f608aa965a34602e7c0Andy Hung        mNormalFrameCount(0), mSinkBuffer(NULL),
11736146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung        mMixerBufferEnabled(AudioFlinger::kEnableExtendedPrecision),
117469aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung        mMixerBuffer(NULL),
117569aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung        mMixerBufferSize(0),
117669aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung        mMixerBufferFormat(AUDIO_FORMAT_INVALID),
117769aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung        mMixerBufferValid(false),
11786146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung        mEffectBufferEnabled(AudioFlinger::kEnableExtendedPrecision),
117998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung        mEffectBuffer(NULL),
118098ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung        mEffectBufferSize(0),
118198ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung        mEffectBufferFormat(AUDIO_FORMAT_INVALID),
118298ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung        mEffectBufferValid(false),
1183c1fac191069774c7bfcb062edbb821ea56e7dbc0Glenn Kasten        mSuspended(0), mBytesWritten(0),
1184462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen        mActiveTracksGeneration(0),
118581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // mStreamTypes[] initialized in constructor body
118681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mOutput(output),
118781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mLastWriteTime(0), mNumWrites(0), mNumDelayedWrites(0), mInWrite(false),
118881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mMixerStatus(MIXER_IDLE),
118981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mMixerStatusIgnoringFastTracks(MIXER_IDLE),
119081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        standbyDelay(AudioFlinger::mStandbyTimeInNsecs),
1191bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        mBytesRemaining(0),
1192bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        mCurrentWriteLength(0),
1193bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        mUseAsyncWrite(false),
11943b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent        mWriteAckSequence(0),
11953b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent        mDrainSequence(0),
1196ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent        mSignalPending(false),
119781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mScreenState(AudioFlinger::mScreenState),
119881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // index 0 is reserved for normal mixer's submix
1199bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten        mFastTrackAvailMask(((1 << FastMixerState::kMaxFastTracks) - 1) & ~1),
1200bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten        // mLatchD, mLatchQ,
1201bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten        mLatchDValid(false), mLatchQValid(false)
120281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
120381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    snprintf(mName, kNameLength, "AudioOut_%X", id);
12049e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    mNBLogWriter = audioFlinger->newWriter_l(kLogSize, mName);
120581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
120681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Assumes constructor is called by AudioFlinger with it's mLock held, but
120781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // it would be safer to explicitly pass initial masterVolume/masterMute as
120881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // parameter.
120981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    //
121081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // If the HAL we are using has support for master volume or master mute,
121181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // then do not attenuate or mute during mixing (just leave the volume at 1.0
121281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // and the mute set to false).
121381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mMasterVolume = audioFlinger->masterVolume_l();
121481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mMasterMute = audioFlinger->masterMute_l();
121581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (mOutput && mOutput->audioHwDev) {
121681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (mOutput->audioHwDev->canSetMasterVolume()) {
121781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mMasterVolume = 1.0;
121881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
121981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
122081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (mOutput->audioHwDev->canSetMasterMute()) {
122181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mMasterMute = false;
122281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
122381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
122481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1225deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten    readOutputParameters_l();
122681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1227223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent    // ++ operator does not compile
122866e4635cb09fadcaccf912f37c387396c428378aGlenn Kasten    for (audio_stream_type_t stream = AUDIO_STREAM_MIN; stream < AUDIO_STREAM_CNT;
122981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            stream = (audio_stream_type_t) (stream + 1)) {
123081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mStreamTypes[stream].volume = mAudioFlinger->streamVolume_l(stream);
123181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mStreamTypes[stream].mute = mAudioFlinger->streamMute_l(stream);
123281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
123381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
123481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
123581784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::PlaybackThread::~PlaybackThread()
123681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
12379e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    mAudioFlinger->unregisterWriter(mNBLogWriter);
1238010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung    free(mSinkBuffer);
123969aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    free(mMixerBuffer);
124098ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    free(mEffectBuffer);
124181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
124281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
124381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::dump(int fd, const Vector<String16>& args)
124481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
124581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    dumpInternals(fd, args);
124681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    dumpTracks(fd, args);
124781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    dumpEffectChains(fd, args);
124881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
124981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
12500f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid AudioFlinger::PlaybackThread::dumpTracks(int fd, const Vector<String16>& args __unused)
125181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
125281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    const size_t SIZE = 256;
125381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    char buffer[SIZE];
125481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    String8 result;
125581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1256b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    result.appendFormat("  Stream volumes in dB: ");
125781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (int i = 0; i < AUDIO_STREAM_CNT; ++i) {
125881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        const stream_type_t *st = &mStreamTypes[i];
125981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (i > 0) {
126081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            result.appendFormat(", ");
126181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
126281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        result.appendFormat("%d:%.2g", i, 20.0 * log10(st->volume));
126381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (st->mute) {
126481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            result.append("M");
126581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
126681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
126781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    result.append("\n");
126881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    write(fd, result.string(), result.length());
126981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    result.clear();
127081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1271b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    // These values are "raw"; they will wrap around.  See prepareTracks_l() for a better way.
1272b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    FastTrackUnderruns underruns = getFastTrackUnderruns(0);
127387cebadd48710e42474756fc3513df678de045ceElliott Hughes    dprintf(fd, "  Normal mixer raw underrun counters: partial=%u empty=%u\n",
1274b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen            underruns.mBitFields.mPartial, underruns.mBitFields.mEmpty);
1275b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen
1276b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    size_t numtracks = mTracks.size();
1277b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    size_t numactive = mActiveTracks.size();
127887cebadd48710e42474756fc3513df678de045ceElliott Hughes    dprintf(fd, "  %d Tracks", numtracks);
1279b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    size_t numactiveseen = 0;
1280b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    if (numtracks) {
128187cebadd48710e42474756fc3513df678de045ceElliott Hughes        dprintf(fd, " of which %d are active\n", numactive);
1282b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        Track::appendDumpHeader(result);
1283b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        for (size_t i = 0; i < numtracks; ++i) {
1284b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen            sp<Track> track = mTracks[i];
1285b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen            if (track != 0) {
1286b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen                bool active = mActiveTracks.indexOf(track) >= 0;
1287b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen                if (active) {
1288b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen                    numactiveseen++;
1289b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen                }
1290b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen                track->dump(buffer, SIZE, active);
1291b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen                result.append(buffer);
1292b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen            }
129381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
1294b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    } else {
1295b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        result.append("\n");
129681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
1297b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    if (numactiveseen != numactive) {
1298b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        // some tracks in the active list were not in the tracks list
1299b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        snprintf(buffer, SIZE, "  The following tracks are in the active list but"
1300b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen                " not in the track list\n");
1301b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        result.append(buffer);
1302b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        Track::appendDumpHeader(result);
1303b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        for (size_t i = 0; i < numactive; ++i) {
1304b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen            sp<Track> track = mActiveTracks[i].promote();
1305b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen            if (track != 0 && mTracks.indexOf(track) < 0) {
1306b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen                track->dump(buffer, SIZE, true);
1307b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen                result.append(buffer);
1308b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen            }
130981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
131081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
1311b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen
131281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    write(fd, result.string(), result.size());
131381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
131481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
131581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::dumpInternals(int fd, const Vector<String16>& args)
131681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
131787cebadd48710e42474756fc3513df678de045ceElliott Hughes    dprintf(fd, "\nOutput thread %p:\n", this);
131887cebadd48710e42474756fc3513df678de045ceElliott Hughes    dprintf(fd, "  Normal frame count: %zu\n", mNormalFrameCount);
131987cebadd48710e42474756fc3513df678de045ceElliott Hughes    dprintf(fd, "  Last write occurred (msecs): %llu\n", ns2ms(systemTime() - mLastWriteTime));
132087cebadd48710e42474756fc3513df678de045ceElliott Hughes    dprintf(fd, "  Total writes: %d\n", mNumWrites);
132187cebadd48710e42474756fc3513df678de045ceElliott Hughes    dprintf(fd, "  Delayed writes: %d\n", mNumDelayedWrites);
132287cebadd48710e42474756fc3513df678de045ceElliott Hughes    dprintf(fd, "  Blocked in write: %s\n", mInWrite ? "yes" : "no");
132387cebadd48710e42474756fc3513df678de045ceElliott Hughes    dprintf(fd, "  Suspend count: %d\n", mSuspended);
132487cebadd48710e42474756fc3513df678de045ceElliott Hughes    dprintf(fd, "  Sink buffer : %p\n", mSinkBuffer);
132587cebadd48710e42474756fc3513df678de045ceElliott Hughes    dprintf(fd, "  Mixer buffer: %p\n", mMixerBuffer);
132687cebadd48710e42474756fc3513df678de045ceElliott Hughes    dprintf(fd, "  Effect buffer: %p\n", mEffectBuffer);
132787cebadd48710e42474756fc3513df678de045ceElliott Hughes    dprintf(fd, "  Fast track availMask=%#x\n", mFastTrackAvailMask);
132881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
132981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    dumpBase(fd, args);
133081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
133181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
133281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// Thread virtuals
133381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
133481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::onFirstRef()
133581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
133681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    run(mName, ANDROID_PRIORITY_URGENT_AUDIO);
133781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
133881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
133981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ThreadBase virtuals
134081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::preExit()
134181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
134281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("  preExit()");
134381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // FIXME this is using hard-coded strings but in the future, this functionality will be
134481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    //       converted to use audio HAL extensions required to support tunneling
134581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mOutput->stream->common.set_parameters(&mOutput->stream->common, "exiting=1");
134681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
134781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
134881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// PlaybackThread::createTrack_l() must be called with AudioFlinger::mLock held
134981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsp<AudioFlinger::PlaybackThread::Track> AudioFlinger::PlaybackThread::createTrack_l(
135081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        const sp<AudioFlinger::Client>& client,
135181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        audio_stream_type_t streamType,
135281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        uint32_t sampleRate,
135381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        audio_format_t format,
135481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        audio_channel_mask_t channelMask,
135574935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten        size_t *pFrameCount,
135681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        const sp<IMemory>& sharedBuffer,
135781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        int sessionId,
135881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        IAudioFlinger::track_flags_t *flags,
135981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        pid_t tid,
1360462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen        int uid,
136181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        status_t *status)
136281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
136374935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten    size_t frameCount = *pFrameCount;
136481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<Track> track;
136581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t lStatus;
136681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
136781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool isTimed = (*flags & IAudioFlinger::TRACK_TIMED) != 0;
136881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
136981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // client expresses a preference for FAST, but we get the final say
137081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (*flags & IAudioFlinger::TRACK_FAST) {
137181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent      if (
137281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // not timed
137381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            (!isTimed) &&
137481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // either of these use cases:
137581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            (
137681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent              // use case 1: shared buffer with any frame count
137781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent              (
137881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                (sharedBuffer != 0)
137981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent              ) ||
138081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent              // use case 2: callback handler and frame count is default or at least as large as HAL
138181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent              (
138281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                (tid != -1) &&
138381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ((frameCount == 0) ||
1384b5fed68bcdd6f44424c9e4d12bfe9a3ff51bd62eGlenn Kasten                (frameCount >= mFrameCount))
138581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent              )
138681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ) &&
138781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // PCM data
138881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            audio_is_linear_pcm(format) &&
13899a59276fb465e492138e0576523b54079671e8f4Andy Hung            // identical channel mask to sink, or mono in and stereo sink
13909a59276fb465e492138e0576523b54079671e8f4Andy Hung            (channelMask == mChannelMask ||
13919a59276fb465e492138e0576523b54079671e8f4Andy Hung                    (channelMask == AUDIO_CHANNEL_OUT_MONO &&
13929a59276fb465e492138e0576523b54079671e8f4Andy Hung                            mChannelMask == AUDIO_CHANNEL_OUT_STEREO)) &&
139381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // hardware sample rate
139481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            (sampleRate == mSampleRate) &&
139581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // normal mixer has an associated fast mixer
139681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            hasFastMixer() &&
139781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // there are sufficient fast track slots available
139881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            (mFastTrackAvailMask != 0)
139981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // FIXME test that MixerThread for this fast track has a capable output HAL
140081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // FIXME add a permission test also?
140181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ) {
140281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // if frameCount not specified, then it defaults to fast mixer (HAL) frame count
140381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (frameCount == 0) {
14040349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten            // read the fast track multiplier property the first time it is needed
14050349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten            int ok = pthread_once(&sFastTrackMultiplierOnce, sFastTrackMultiplierInit);
14060349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten            if (ok != 0) {
14070349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten                ALOGE("%s pthread_once failed: %d", __func__, ok);
14080349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten            }
14090349009fd19f89f8414c428f6b71b369f7546085Glenn Kasten            frameCount = mFrameCount * sFastTrackMultiplier;
141081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
141181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGV("AUDIO_OUTPUT_FLAG_FAST accepted: frameCount=%d mFrameCount=%d",
141281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                frameCount, mFrameCount);
141381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent      } else {
141481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGV("AUDIO_OUTPUT_FLAG_FAST denied: isTimed=%d sharedBuffer=%p frameCount=%d "
14156146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung                "mFrameCount=%d format=%#x mFormat=%#x isLinear=%d channelMask=%#x "
14166146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung                "sampleRate=%u mSampleRate=%u "
141781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                "hasFastMixer=%d tid=%d fastTrackAvailMask=%#x",
14186146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung                isTimed, sharedBuffer.get(), frameCount, mFrameCount, format, mFormat,
141981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                audio_is_linear_pcm(format),
142081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                channelMask, sampleRate, mSampleRate, hasFastMixer(), tid, mFastTrackAvailMask);
142181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        *flags &= ~IAudioFlinger::TRACK_FAST;
142281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // For compatibility with AudioTrack calculation, buffer depth is forced
142381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // to be at least 2 x the normal mixer frame count and cover audio hardware latency.
142481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // This is probably too conservative, but legacy application code may depend on it.
142581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // If you change this calculation, also review the start threshold which is related.
142681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        uint32_t latencyMs = mOutput->stream->get_latency(mOutput->stream);
142781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        uint32_t minBufCount = latencyMs / ((1000 * mNormalFrameCount) / mSampleRate);
142881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (minBufCount < 2) {
142981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            minBufCount = 2;
143081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
143181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        size_t minFrameCount = mNormalFrameCount * minBufCount;
143281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (frameCount < minFrameCount) {
143381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            frameCount = minFrameCount;
143481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
143581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent      }
143681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
143774935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten    *pFrameCount = frameCount;
143881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1439c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten    switch (mType) {
1440c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten
1441c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten    case DIRECT:
1442993fa0603707e94ce259e95e56838a85b5ccbdc5Glenn Kasten        if (audio_is_linear_pcm(format)) {
144381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) {
1444cac3daa6332bf6d1f7d26adc4a9915f3d7992dd9Glenn Kasten                ALOGE("createTrack_l() Bad parameter: sampleRate %u format %#x, channelMask 0x%08x "
1445cac3daa6332bf6d1f7d26adc4a9915f3d7992dd9Glenn Kasten                        "for output %p with format %#x",
144681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        sampleRate, format, channelMask, mOutput, mFormat);
144781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                lStatus = BAD_VALUE;
144881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                goto Exit;
144981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
145081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
1451c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten        break;
1452c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten
1453c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten    case OFFLOAD:
1454bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        if (sampleRate != mSampleRate || format != mFormat || channelMask != mChannelMask) {
1455cac3daa6332bf6d1f7d26adc4a9915f3d7992dd9Glenn Kasten            ALOGE("createTrack_l() Bad parameter: sampleRate %d format %#x, channelMask 0x%08x \""
1456cac3daa6332bf6d1f7d26adc4a9915f3d7992dd9Glenn Kasten                    "for output %p with format %#x",
1457bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    sampleRate, format, channelMask, mOutput, mFormat);
1458bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            lStatus = BAD_VALUE;
1459bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            goto Exit;
1460bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        }
1461c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten        break;
1462c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten
1463c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten    default:
1464993fa0603707e94ce259e95e56838a85b5ccbdc5Glenn Kasten        if (!audio_is_linear_pcm(format)) {
1465cac3daa6332bf6d1f7d26adc4a9915f3d7992dd9Glenn Kasten                ALOGE("createTrack_l() Bad parameter: format %#x \""
1466cac3daa6332bf6d1f7d26adc4a9915f3d7992dd9Glenn Kasten                        "for output %p with format %#x",
1467bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                        format, mOutput, mFormat);
1468bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                lStatus = BAD_VALUE;
1469bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                goto Exit;
1470bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        }
1471cd04484f4837b8ca0041d118286ab6a98e84fc75Andy Hung        if (sampleRate > mSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX) {
147281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGE("Sample rate out of range: %u mSampleRate %u", sampleRate, mSampleRate);
147381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            lStatus = BAD_VALUE;
147481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            goto Exit;
147581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
1476c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten        break;
1477c3df838434b37d8400eea2438083cc01a4c1cc71Glenn Kasten
147881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
147981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
148081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    lStatus = initCheck();
148181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (lStatus != NO_ERROR) {
148215e5798908ccac14e10c84834eaf08c42931bd06Glenn Kasten        ALOGE("createTrack_l() audio driver not initialized");
148381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        goto Exit;
148481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
148581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
148681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    { // scope for mLock
148781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        Mutex::Autolock _l(mLock);
148881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
148981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // all tracks in same audio session must share the same routing strategy otherwise
149081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // conflicts will happen when tracks are moved from one output to another by audio policy
149181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // manager
149281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        uint32_t strategy = AudioSystem::getStrategyForStream(streamType);
149381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        for (size_t i = 0; i < mTracks.size(); ++i) {
149481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sp<Track> t = mTracks[i];
149583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent            if (t != 0 && t->isExternalTrack()) {
149681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                uint32_t actual = AudioSystem::getStrategyForStream(t->streamType());
149781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (sessionId == t->sessionId() && strategy != actual) {
149881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    ALOGE("createTrack_l() mismatched strategy; expected %u but found %u",
149981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                            strategy, actual);
150081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    lStatus = BAD_VALUE;
150181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    goto Exit;
150281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
150381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
150481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
150581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
150681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (!isTimed) {
150781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            track = new Track(this, client, streamType, sampleRate, format,
150883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent                              channelMask, frameCount, NULL, sharedBuffer,
150983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent                              sessionId, uid, *flags, TrackBase::TYPE_DEFAULT);
151081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else {
151181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            track = TimedTrack::create(this, client, streamType, sampleRate, format,
1512462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen                    channelMask, frameCount, sharedBuffer, sessionId, uid);
151381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
1514030033342a6ea17003e6af38a56c7edc6d2ead01Glenn Kasten
1515030033342a6ea17003e6af38a56c7edc6d2ead01Glenn Kasten        // new Track always returns non-NULL,
1516030033342a6ea17003e6af38a56c7edc6d2ead01Glenn Kasten        // but TimedTrack::create() is a factory that could fail by returning NULL
1517030033342a6ea17003e6af38a56c7edc6d2ead01Glenn Kasten        lStatus = track != 0 ? track->initCheck() : (status_t) NO_MEMORY;
1518030033342a6ea17003e6af38a56c7edc6d2ead01Glenn Kasten        if (lStatus != NO_ERROR) {
15190cde076ddb283c84c3801a2df4cc3df99bd1577fGlenn Kasten            ALOGE("createTrack_l() initCheck failed %d; no control block?", lStatus);
152003e9e83c47ab4a518da0a1f36b8f702f59221c95Haynes Mathew George            // track must be cleared from the caller as the caller has the AF lock
152181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            goto Exit;
152281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
152381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mTracks.add(track);
152481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
152581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<EffectChain> chain = getEffectChain_l(sessionId);
152681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (chain != 0) {
152781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGV("createTrack_l() setting main buffer %p", chain->inBuffer());
152881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            track->setMainBuffer(chain->inBuffer());
152981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            chain->setStrategy(AudioSystem::getStrategyForStream(track->streamType()));
153081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            chain->incTrackCnt();
153181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
153281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
153381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if ((*flags & IAudioFlinger::TRACK_FAST) && (tid != -1)) {
153481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            pid_t callingPid = IPCThreadState::self()->getCallingPid();
153581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful,
153681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // so ask activity manager to do this on our behalf
153781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp);
153881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
153981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
154081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
154181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    lStatus = NO_ERROR;
154281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
154381784c37c61b09289654b979567a42bf73cd2b12Eric LaurentExit:
15449156ef3e11b68cc4b6d3cea77f1f63673855a6d1Glenn Kasten    *status = lStatus;
154581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return track;
154681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
154781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
154881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::PlaybackThread::correctLatency_l(uint32_t latency) const
154981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
155081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return latency;
155181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
155281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
155381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::PlaybackThread::latency() const
155481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
155581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
155681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return latency_l();
155781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
155881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::PlaybackThread::latency_l() const
155981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
156081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (initCheck() == NO_ERROR) {
156181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return correctLatency_l(mOutput->stream->get_latency(mOutput->stream));
156281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    } else {
156381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return 0;
156481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
156581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
156681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
156781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::setMasterVolume(float value)
156881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
156981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
157081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Don't apply master volume in SW if our HAL can do it for us.
157181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (mOutput && mOutput->audioHwDev &&
157281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mOutput->audioHwDev->canSetMasterVolume()) {
157381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mMasterVolume = 1.0;
157481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    } else {
157581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mMasterVolume = value;
157681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
157781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
157881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
157981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::setMasterMute(bool muted)
158081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
158181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
158281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Don't apply master mute in SW if our HAL can do it for us.
158381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (mOutput && mOutput->audioHwDev &&
158481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mOutput->audioHwDev->canSetMasterMute()) {
158581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mMasterMute = false;
158681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    } else {
158781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mMasterMute = muted;
158881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
158981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
159081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
159181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::setStreamVolume(audio_stream_type_t stream, float value)
159281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
159381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
159481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mStreamTypes[stream].volume = value;
1595ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent    broadcast_l();
159681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
159781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
159881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::setStreamMute(audio_stream_type_t stream, bool muted)
159981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
160081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
160181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mStreamTypes[stream].mute = muted;
1602ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent    broadcast_l();
160381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
160481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
160581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentfloat AudioFlinger::PlaybackThread::streamVolume(audio_stream_type_t stream) const
160681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
160781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
160881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return mStreamTypes[stream].volume;
160981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
161081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
161181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// addTrack_l() must be called with ThreadBase::mLock held
161281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::PlaybackThread::addTrack_l(const sp<Track>& track)
161381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
161481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t status = ALREADY_EXISTS;
161581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
161681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // set retry count for buffer fill
161781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    track->mRetryCount = kMaxTrackStartupRetries;
161881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (mActiveTracks.indexOf(track) < 0) {
161981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // the track is newly added, make sure it fills up all its
162081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // buffers before playing. This is to ensure the client will
162181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // effectively get the latency it requested.
162283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent        if (track->isExternalTrack()) {
1623bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            TrackBase::track_state state = track->mState;
1624bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            mLock.unlock();
1625e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent            status = AudioSystem::startOutput(mId, track->streamType(),
1626e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent                                              (audio_session_t)track->sessionId());
1627bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            mLock.lock();
1628bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            // abort track was stopped/paused while we released the lock
1629bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            if (state != track->mState) {
1630bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                if (status == NO_ERROR) {
1631bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    mLock.unlock();
1632e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent                    AudioSystem::stopOutput(mId, track->streamType(),
1633e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent                                            (audio_session_t)track->sessionId());
1634bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    mLock.lock();
1635bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                }
1636bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                return INVALID_OPERATION;
1637bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            }
1638bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            // abort if start is rejected by audio policy manager
1639bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            if (status != NO_ERROR) {
1640bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                return PERMISSION_DENIED;
1641bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            }
1642bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent#ifdef ADD_BATTERY_DATA
1643bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            // to track the speaker usage
1644bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStart);
1645bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent#endif
1646bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        }
1647bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
16489f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        track->mFillingUpStatus = track->sharedBuffer() != 0 ? Track::FS_FILLED : Track::FS_FILLING;
164981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        track->mResetDone = false;
165081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        track->mPresentationCompleteFrames = 0;
165181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mActiveTracks.add(track);
1652462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen        mWakeLockUids.add(track->uid());
1653462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen        mActiveTracksGeneration++;
1654fd4779740ec3e9e865d5514464df26d015354388Eric Laurent        mLatestActiveTrack = track;
1655d0107bcd44fe608b0c00a8843d19fb6356c4cb69Eric Laurent        sp<EffectChain> chain = getEffectChain_l(track->sessionId());
1656d0107bcd44fe608b0c00a8843d19fb6356c4cb69Eric Laurent        if (chain != 0) {
1657d0107bcd44fe608b0c00a8843d19fb6356c4cb69Eric Laurent            ALOGV("addTrack_l() starting track on chain %p for session %d", chain.get(),
1658d0107bcd44fe608b0c00a8843d19fb6356c4cb69Eric Laurent                    track->sessionId());
1659d0107bcd44fe608b0c00a8843d19fb6356c4cb69Eric Laurent            chain->incActiveTrackCnt();
166081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
166181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
166281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        status = NO_ERROR;
166381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
166481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
16654c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George    onAddNewTrack_l();
166681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return status;
166781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
166881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1669bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentbool AudioFlinger::PlaybackThread::destroyTrack_l(const sp<Track>& track)
167081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
1671bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    track->terminate();
167281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // active tracks are removed by threadLoop()
1673bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    bool trackActive = (mActiveTracks.indexOf(track) >= 0);
1674bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    track->mState = TrackBase::STOPPED;
1675bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    if (!trackActive) {
167681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        removeTrack_l(track);
1677ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent    } else if (track->isFastTrack() || track->isOffloaded() || track->isDirect()) {
1678bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        track->mState = TrackBase::STOPPING_1;
167981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
1680bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
1681bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    return trackActive;
168281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
168381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
168481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::removeTrack_l(const sp<Track>& track)
168581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
168681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    track->triggerEvents(AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE);
168781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mTracks.remove(track);
168881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    deleteTrackName_l(track->name());
168981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // redundant as track is about to be destroyed, for dumpsys only
169081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    track->mName = -1;
169181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (track->isFastTrack()) {
169281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        int index = track->mFastIndex;
169381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOG_ASSERT(0 < index && index < (int)FastMixerState::kMaxFastTracks);
169481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOG_ASSERT(!(mFastTrackAvailMask & (1 << index)));
169581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mFastTrackAvailMask |= 1 << index;
169681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // redundant as track is about to be destroyed, for dumpsys only
169781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        track->mFastIndex = -1;
169881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
169981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<EffectChain> chain = getEffectChain_l(track->sessionId());
170081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (chain != 0) {
170181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        chain->decTrackCnt();
170281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
170381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
170481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1705ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurentvoid AudioFlinger::PlaybackThread::broadcast_l()
1706bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{
1707bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    // Thread could be blocked waiting for async
1708bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    // so signal it to handle state changes immediately
1709bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    // If threadLoop is currently unlocked a signal of mWaitWorkCV will
1710bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    // be lost so we also flag to prevent it blocking on mWaitWorkCV
1711bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    mSignalPending = true;
1712ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent    mWaitWorkCV.broadcast();
1713bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}
1714bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
171581784c37c61b09289654b979567a42bf73cd2b12Eric LaurentString8 AudioFlinger::PlaybackThread::getParameters(const String8& keys)
171681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
171781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
171881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (initCheck() != NO_ERROR) {
1719d8ea699dc8e7dac58bb32e9cdb31b0758da25817Glenn Kasten        return String8();
172081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
172181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1722d8ea699dc8e7dac58bb32e9cdb31b0758da25817Glenn Kasten    char *s = mOutput->stream->common.get_parameters(&mOutput->stream->common, keys.string());
1723d8ea699dc8e7dac58bb32e9cdb31b0758da25817Glenn Kasten    const String8 out_s8(s);
172481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    free(s);
172581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return out_s8;
172681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
172781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1728021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurentvoid AudioFlinger::PlaybackThread::audioConfigChanged(int event, int param) {
172981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    AudioSystem::OutputDescriptor desc;
173081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    void *param2 = NULL;
173181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1732021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent    ALOGV("PlaybackThread::audioConfigChanged, thread %p, event %d, param %d", this, event,
173381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            param);
173481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
173581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    switch (event) {
173681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    case AudioSystem::OUTPUT_OPENED:
173781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    case AudioSystem::OUTPUT_CONFIG_CHANGED:
1738fad226abd12435dbcd232f7de396f1a097b2bd5fGlenn Kasten        desc.channelMask = mChannelMask;
173981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        desc.samplingRate = mSampleRate;
174081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        desc.format = mFormat;
174181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        desc.frameCount = mNormalFrameCount; // FIXME see
174281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                             // AudioFlinger::frameCount(audio_io_handle_t)
17431035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        desc.latency = latency_l();
174481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        param2 = &desc;
174581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        break;
174681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
174781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    case AudioSystem::STREAM_CONFIG_CHANGED:
174881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        param2 = &param;
174981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    case AudioSystem::OUTPUT_CLOSED:
175081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    default:
175181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        break;
175281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
1753021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent    mAudioFlinger->audioConfigChanged(event, mId, param2);
175481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
175581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1756bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioFlinger::PlaybackThread::writeCallback()
1757bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{
1758bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    ALOG_ASSERT(mCallbackThread != 0);
17593b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    mCallbackThread->resetWriteBlocked();
1760bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}
1761bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
1762bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioFlinger::PlaybackThread::drainCallback()
1763bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{
1764bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    ALOG_ASSERT(mCallbackThread != 0);
17653b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    mCallbackThread->resetDraining();
1766bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}
1767bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
17683b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurentvoid AudioFlinger::PlaybackThread::resetWriteBlocked(uint32_t sequence)
1769bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{
1770bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    Mutex::Autolock _l(mLock);
17713b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // reject out of sequence requests
17723b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    if ((mWriteAckSequence & 1) && (sequence == mWriteAckSequence)) {
17733b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent        mWriteAckSequence &= ~1;
1774bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        mWaitWorkCV.signal();
1775bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    }
1776bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}
1777bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
17783b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurentvoid AudioFlinger::PlaybackThread::resetDraining(uint32_t sequence)
1779bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{
1780bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    Mutex::Autolock _l(mLock);
17813b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // reject out of sequence requests
17823b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    if ((mDrainSequence & 1) && (sequence == mDrainSequence)) {
17833b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent        mDrainSequence &= ~1;
1784bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        mWaitWorkCV.signal();
1785bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    }
1786bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}
1787bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
1788bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent// static
1789bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentint AudioFlinger::PlaybackThread::asyncCallback(stream_callback_event_t event,
17900f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kasten                                                void *param __unused,
1791bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                                                void *cookie)
1792bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{
1793bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    AudioFlinger::PlaybackThread *me = (AudioFlinger::PlaybackThread *)cookie;
1794bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    ALOGV("asyncCallback() event %d", event);
1795bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    switch (event) {
1796bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    case STREAM_CBK_EVENT_WRITE_READY:
1797bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        me->writeCallback();
1798bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        break;
1799bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    case STREAM_CBK_EVENT_DRAIN_READY:
1800bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        me->drainCallback();
1801bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        break;
1802bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    default:
1803bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        ALOGW("asyncCallback() unknown event %d", event);
1804bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        break;
1805bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    }
1806bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    return 0;
1807bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}
1808bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
1809deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kastenvoid AudioFlinger::PlaybackThread::readOutputParameters_l()
181081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
1811adad3d7d935da176ff24941b4ae9edf7340e9b96Glenn Kasten    // unfortunately we have no way of recovering from errors here, hence the LOG_ALWAYS_FATAL
181281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mSampleRate = mOutput->stream->common.get_sample_rate(&mOutput->stream->common);
181381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mChannelMask = mOutput->stream->common.get_channels(&mOutput->stream->common);
18147fc97ba08e2850f3f16db704b78ce78e3dbe1ff0Glenn Kasten    if (!audio_is_output_channel(mChannelMask)) {
1815adad3d7d935da176ff24941b4ae9edf7340e9b96Glenn Kasten        LOG_ALWAYS_FATAL("HAL channel mask %#x not valid for output", mChannelMask);
18167fc97ba08e2850f3f16db704b78ce78e3dbe1ff0Glenn Kasten    }
18179a59276fb465e492138e0576523b54079671e8f4Andy Hung    if ((mType == MIXER || mType == DUPLICATING)
18189a59276fb465e492138e0576523b54079671e8f4Andy Hung            && !isValidPcmSinkChannelMask(mChannelMask)) {
18199a59276fb465e492138e0576523b54079671e8f4Andy Hung        LOG_ALWAYS_FATAL("HAL channel mask %#x not supported for mixed output",
18209a59276fb465e492138e0576523b54079671e8f4Andy Hung                mChannelMask);
18217fc97ba08e2850f3f16db704b78ce78e3dbe1ff0Glenn Kasten    }
1822e541269be94f3a1072932d51537905b120ef4733Andy Hung    mChannelCount = audio_channel_count_from_out_mask(mChannelMask);
1823463be250de73907965faa6a216c00312bf81e049Andy Hung    mHALFormat = mOutput->stream->common.get_format(&mOutput->stream->common);
1824463be250de73907965faa6a216c00312bf81e049Andy Hung    mFormat = mHALFormat;
18257fc97ba08e2850f3f16db704b78ce78e3dbe1ff0Glenn Kasten    if (!audio_is_valid_format(mFormat)) {
1826adad3d7d935da176ff24941b4ae9edf7340e9b96Glenn Kasten        LOG_ALWAYS_FATAL("HAL format %#x not valid for output", mFormat);
18277fc97ba08e2850f3f16db704b78ce78e3dbe1ff0Glenn Kasten    }
18286146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung    if ((mType == MIXER || mType == DUPLICATING)
18296146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung            && !isValidPcmSinkFormat(mFormat)) {
18306146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung        LOG_FATAL("HAL format %#x not supported for mixed output",
18316146c08f0c3dd8b9e5788063aa433f304a810602Andy Hung                mFormat);
18327fc97ba08e2850f3f16db704b78ce78e3dbe1ff0Glenn Kasten    }
1833665470b36f202bcc8ee2f7417f68fd2608dd07c1Eric Laurent    mFrameSize = audio_stream_out_frame_size(mOutput->stream);
183470949c47fbae3f836d15f040551d7631be3ed7c2Glenn Kasten    mBufferSize = mOutput->stream->common.get_buffer_size(&mOutput->stream->common);
183570949c47fbae3f836d15f040551d7631be3ed7c2Glenn Kasten    mFrameCount = mBufferSize / mFrameSize;
183681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (mFrameCount & 15) {
183781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGW("HAL output buffer size is %u frames but AudioMixer requires multiples of 16 frames",
183881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                mFrameCount);
183981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
184081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1841bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    if ((mOutput->flags & AUDIO_OUTPUT_FLAG_NON_BLOCKING) &&
1842bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            (mOutput->stream->set_callback != NULL)) {
1843bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        if (mOutput->stream->set_callback(mOutput->stream,
1844bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                                      AudioFlinger::PlaybackThread::asyncCallback, this) == 0) {
1845bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            mUseAsyncWrite = true;
18464de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent            mCallbackThread = new AudioFlinger::AsyncCallbackThread(this);
1847bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        }
1848bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    }
1849bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
185009a5007b17acb49d25cfa386a2e2534d942e8854Andy Hung    // Calculate size of normal sink buffer relative to the HAL output buffer size
185181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    double multiplier = 1.0;
185281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (mType == MIXER && (kUseFastMixer == FastMixer_Static ||
185381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            kUseFastMixer == FastMixer_Dynamic)) {
185409a5007b17acb49d25cfa386a2e2534d942e8854Andy Hung        size_t minNormalFrameCount = (kMinNormalSinkBufferSizeMs * mSampleRate) / 1000;
185509a5007b17acb49d25cfa386a2e2534d942e8854Andy Hung        size_t maxNormalFrameCount = (kMaxNormalSinkBufferSizeMs * mSampleRate) / 1000;
185681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // round up minimum and round down maximum to nearest 16 frames to satisfy AudioMixer
185781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        minNormalFrameCount = (minNormalFrameCount + 15) & ~15;
185881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        maxNormalFrameCount = maxNormalFrameCount & ~15;
185981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (maxNormalFrameCount < minNormalFrameCount) {
186081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            maxNormalFrameCount = minNormalFrameCount;
186181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
186281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        multiplier = (double) minNormalFrameCount / (double) mFrameCount;
186381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (multiplier <= 1.0) {
186481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            multiplier = 1.0;
186581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else if (multiplier <= 2.0) {
186681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (2 * mFrameCount <= maxNormalFrameCount) {
186781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                multiplier = 2.0;
186881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            } else {
186981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                multiplier = (double) maxNormalFrameCount / (double) mFrameCount;
187081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
187181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else {
187281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // prefer an even multiplier, for compatibility with doubling of fast tracks due to HAL
187309a5007b17acb49d25cfa386a2e2534d942e8854Andy Hung            // SRC (it would be unusual for the normal sink buffer size to not be a multiple of fast
187481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // track, but we sometimes have to do this to satisfy the maximum frame count
187581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // constraint)
187681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // FIXME this rounding up should not be done if no HAL SRC
187781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            uint32_t truncMult = (uint32_t) multiplier;
187881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if ((truncMult & 1)) {
187981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if ((truncMult + 1) * mFrameCount <= maxNormalFrameCount) {
188081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    ++truncMult;
188181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
188281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
188381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            multiplier = (double) truncMult;
188481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
188581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
188681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mNormalFrameCount = multiplier * mFrameCount;
188781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // round up to nearest 16 frames to satisfy AudioMixer
1888ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent    if (mType == MIXER || mType == DUPLICATING) {
1889ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent        mNormalFrameCount = (mNormalFrameCount + 15) & ~15;
1890ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent    }
189109a5007b17acb49d25cfa386a2e2534d942e8854Andy Hung    ALOGI("HAL output buffer size %u frames, normal sink buffer size %u frames", mFrameCount,
189281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mNormalFrameCount);
189381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1894010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung    // mSinkBuffer is the sink buffer.  Size is always multiple-of-16 frames.
1895010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung    // Originally this was int16_t[] array, need to remove legacy implications.
1896010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung    free(mSinkBuffer);
1897010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung    mSinkBuffer = NULL;
18985b10a2037a835e790994b9ebec3c2e55052f1f3bAndy Hung    // For sink buffer size, we use the frame size from the downstream sink to avoid problems
18995b10a2037a835e790994b9ebec3c2e55052f1f3bAndy Hung    // with non PCM formats for compressed music, e.g. AAC, and Offload threads.
19005b10a2037a835e790994b9ebec3c2e55052f1f3bAndy Hung    const size_t sinkBufferSize = mNormalFrameCount * mFrameSize;
1901010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung    (void)posix_memalign(&mSinkBuffer, 32, sinkBufferSize);
190281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
190369aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // We resize the mMixerBuffer according to the requirements of the sink buffer which
190469aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // drives the output.
190569aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    free(mMixerBuffer);
190669aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    mMixerBuffer = NULL;
190769aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    if (mMixerBufferEnabled) {
190869aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung        mMixerBufferFormat = AUDIO_FORMAT_PCM_FLOAT; // also valid: AUDIO_FORMAT_PCM_16_BIT.
190969aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung        mMixerBufferSize = mNormalFrameCount * mChannelCount
191069aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung                * audio_bytes_per_sample(mMixerBufferFormat);
191169aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung        (void)posix_memalign(&mMixerBuffer, 32, mMixerBufferSize);
191269aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    }
191398ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    free(mEffectBuffer);
191498ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    mEffectBuffer = NULL;
191598ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    if (mEffectBufferEnabled) {
191698ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung        mEffectBufferFormat = AUDIO_FORMAT_PCM_16_BIT; // Note: Effects support 16b only
191798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung        mEffectBufferSize = mNormalFrameCount * mChannelCount
191898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung                * audio_bytes_per_sample(mEffectBufferFormat);
191998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung        (void)posix_memalign(&mEffectBuffer, 32, mEffectBufferSize);
192098ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    }
192169aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung
192281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // force reconfiguration of effect chains and engines to take new buffer size and audio
192381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // parameters into account
1924deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten    // Note that mLock is not held when readOutputParameters_l() is called from the constructor
192581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // but in this case nothing is done below as no audio sessions have effect yet so it doesn't
192681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // matter.
192781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // create a copy of mEffectChains as calling moveEffectChain_l() can reorder some effect chains
192881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Vector< sp<EffectChain> > effectChains = mEffectChains;
192981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < effectChains.size(); i ++) {
193081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mAudioFlinger->moveEffectChain_l(effectChains[i]->sessionId(), this, this, false);
193181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
193281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
193381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
193481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
1935377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETITstatus_t AudioFlinger::PlaybackThread::getRenderPosition(uint32_t *halFrames, uint32_t *dspFrames)
193681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
193781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (halFrames == NULL || dspFrames == NULL) {
193881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return BAD_VALUE;
193981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
194081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
194181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (initCheck() != NO_ERROR) {
194281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return INVALID_OPERATION;
194381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
194481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    size_t framesWritten = mBytesWritten / mFrameSize;
194581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    *halFrames = framesWritten;
194681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
194781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (isSuspended()) {
194881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // return an estimation of rendered frames when the output is suspended
194981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        size_t latencyFrames = (latency_l() * mSampleRate) / 1000;
195081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        *dspFrames = framesWritten >= latencyFrames ? framesWritten - latencyFrames : 0;
195181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return NO_ERROR;
195281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    } else {
1953377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT        status_t status;
1954377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT        uint32_t frames;
1955377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT        status = mOutput->stream->get_render_position(mOutput->stream, &frames);
1956377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT        *dspFrames = (size_t)frames;
1957377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT        return status;
195881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
195981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
196081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
196181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::PlaybackThread::hasAudioSession(int sessionId) const
196281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
196381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
196481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t result = 0;
196581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (getEffectChain_l(sessionId) != 0) {
196681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        result = EFFECT_SESSION;
196781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
196881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
196981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mTracks.size(); ++i) {
197081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<Track> track = mTracks[i];
19715736c35b841de56ce394b4879389f669b61425e6Glenn Kasten        if (sessionId == track->sessionId() && !track->isInvalid()) {
197281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            result |= TRACK_SESSION;
197381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            break;
197481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
197581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
197681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
197781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return result;
197881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
197981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
198081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::PlaybackThread::getStrategyForSession_l(int sessionId)
198181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
198281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // session AUDIO_SESSION_OUTPUT_MIX is placed in same strategy as MUSIC stream so that
198381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // it is moved to correct output by audio policy manager when A2DP is connected or disconnected
198481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (sessionId == AUDIO_SESSION_OUTPUT_MIX) {
198581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
198681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
198781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mTracks.size(); i++) {
198881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<Track> track = mTracks[i];
19895736c35b841de56ce394b4879389f669b61425e6Glenn Kasten        if (sessionId == track->sessionId() && !track->isInvalid()) {
199081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return AudioSystem::getStrategyForStream(track->streamType());
199181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
199281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
199381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return AudioSystem::getStrategyForStream(AUDIO_STREAM_MUSIC);
199481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
199581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
199681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
199781784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::AudioStreamOut* AudioFlinger::PlaybackThread::getOutput() const
199881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
199981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
200081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return mOutput;
200181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
200281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
200381784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::AudioStreamOut* AudioFlinger::PlaybackThread::clearOutput()
200481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
200581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
200681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    AudioStreamOut *output = mOutput;
200781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mOutput = NULL;
200881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // FIXME FastMixer might also have a raw ptr to mOutputSink;
200981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    //       must push a NULL and wait for ack
201081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mOutputSink.clear();
201181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mPipeSink.clear();
201281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mNormalSink.clear();
201381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return output;
201481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
201581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
201681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// this method must always be called either with ThreadBase mLock held or inside the thread loop
201781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentaudio_stream_t* AudioFlinger::PlaybackThread::stream() const
201881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
201981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (mOutput == NULL) {
202081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return NULL;
202181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
202281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return &mOutput->stream->common;
202381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
202481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
202581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::PlaybackThread::activeSleepTimeUs() const
202681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
202781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return (uint32_t)((uint32_t)((mNormalFrameCount * 1000) / mSampleRate) * 1000);
202881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
202981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
203081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::PlaybackThread::setSyncEvent(const sp<SyncEvent>& event)
203181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
203281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (!isValidSyncEvent(event)) {
203381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return BAD_VALUE;
203481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
203581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
203681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
203781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
203881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mTracks.size(); ++i) {
203981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<Track> track = mTracks[i];
204081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (event->triggerSession() == track->sessionId()) {
204181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            (void) track->setSyncEvent(event);
204281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return NO_ERROR;
204381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
204481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
204581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
204681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return NAME_NOT_FOUND;
204781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
204881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
204981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentbool AudioFlinger::PlaybackThread::isValidSyncEvent(const sp<SyncEvent>& event) const
205081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
205181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return event->type() == AudioSystem::SYNC_EVENT_PRESENTATION_COMPLETE;
205281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
205381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
205481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::threadLoop_removeTracks(
205581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        const Vector< sp<Track> >& tracksToRemove)
205681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
205781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    size_t count = tracksToRemove.size();
205834fca34606b448e6b71c2942f63cb13a0aebd620Glenn Kasten    if (count > 0) {
205981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        for (size_t i = 0 ; i < count ; i++) {
206081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            const sp<Track>& track = tracksToRemove.itemAt(i);
206183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent            if (track->isExternalTrack()) {
2062e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent                AudioSystem::stopOutput(mId, track->streamType(),
2063e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent                                        (audio_session_t)track->sessionId());
2064bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent#ifdef ADD_BATTERY_DATA
2065bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                // to track the speaker usage
2066bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                addBatteryData(IMediaPlayerService::kBatteryDataAudioFlingerStop);
2067bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent#endif
2068bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                if (track->isTerminated()) {
2069e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent                    AudioSystem::releaseOutput(mId, track->streamType(),
2070e83b55dc29ca16092ba02f36f55fa6e0e37fd78cEric Laurent                                               (audio_session_t)track->sessionId());
2071bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                }
207281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
207381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
207481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
207581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
207681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
207781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::checkSilentMode_l()
207881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
207981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (!mMasterMute) {
208081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        char value[PROPERTY_VALUE_MAX];
208181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (property_get("ro.audio.silent", value, "0") > 0) {
208281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            char *endptr;
208381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            unsigned long ul = strtoul(value, &endptr, 0);
208481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (*endptr == '\0' && ul != 0) {
208581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGD("Silence is golden");
208681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // The setprop command will not allow a property to be changed after
208781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // the first time it is set, so we don't have to worry about un-muting.
208881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                setMasterMute_l(true);
208981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
209081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
209181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
209281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
209381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
209481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// shared by MIXER and DIRECT, overridden by DUPLICATING
2095bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentssize_t AudioFlinger::PlaybackThread::threadLoop_write()
209681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
209781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // FIXME rewrite to reduce number of system calls
209881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mLastWriteTime = systemTime();
209981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mInWrite = true;
2100bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    ssize_t bytesWritten;
2101010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung    const size_t offset = mCurrentWriteLength - mBytesRemaining;
210281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
210381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // If an NBAIO sink is present, use it to write the normal mixer's submix
210481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (mNormalSink != 0) {
21054c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten
2106010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung        const size_t count = mBytesRemaining / mFrameSize;
2107010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung
21082d590964aa58e137d17a43e095e6443dd0fe2e98Simon Wilson        ATRACE_BEGIN("write");
210981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // update the setpoint when AudioFlinger::mScreenState changes
211081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        uint32_t screenState = AudioFlinger::mScreenState;
211181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (screenState != mScreenState) {
211281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mScreenState = screenState;
211381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            MonoPipe *pipe = (MonoPipe *)mPipeSink.get();
211481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (pipe != NULL) {
211581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                pipe->setAvgFrames((mScreenState & 1) ?
211681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        (pipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2);
211781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
211881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
2119010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung        ssize_t framesWritten = mNormalSink->write((char *)mSinkBuffer + offset, count);
21202d590964aa58e137d17a43e095e6443dd0fe2e98Simon Wilson        ATRACE_END();
212181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (framesWritten > 0) {
2122010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung            bytesWritten = framesWritten * mFrameSize;
212381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else {
212481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            bytesWritten = framesWritten;
212581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
2126767094dd98b01baf21de2ad09c27b3c98776cf73Glenn Kasten        status_t status = mNormalSink->getTimestamp(mLatchD.mTimestamp);
2127bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten        if (status == NO_ERROR) {
2128bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten            size_t totalFramesWritten = mNormalSink->framesWritten();
2129bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten            if (totalFramesWritten >= mLatchD.mTimestamp.mPosition) {
2130bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten                mLatchD.mUnpresentedFrames = totalFramesWritten - mLatchD.mTimestamp.mPosition;
21314c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten                // mLatchD.mFramesReleased is set immediately before D is clocked into Q
2132bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten                mLatchDValid = true;
2133bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten            }
2134bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten        }
213581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // otherwise use the HAL / AudioStreamOut directly
213681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    } else {
2137bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        // Direct output and offload threads
2138010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung
2139bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        if (mUseAsyncWrite) {
21403b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent            ALOGW_IF(mWriteAckSequence & 1, "threadLoop_write(): out of sequence write request");
21413b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent            mWriteAckSequence += 2;
21423b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent            mWriteAckSequence |= 1;
2143bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            ALOG_ASSERT(mCallbackThread != 0);
21443b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent            mCallbackThread->setWriteBlocked(mWriteAckSequence);
2145bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        }
2146767094dd98b01baf21de2ad09c27b3c98776cf73Glenn Kasten        // FIXME We should have an implementation of timestamps for direct output threads.
2147767094dd98b01baf21de2ad09c27b3c98776cf73Glenn Kasten        // They are used e.g for multichannel PCM playback over HDMI.
2148bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        bytesWritten = mOutput->stream->write(mOutput->stream,
21492098f2744cedf2dc3fa36f608aa965a34602e7c0Andy Hung                                                   (char *)mSinkBuffer + offset, mBytesRemaining);
2150bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        if (mUseAsyncWrite &&
2151bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                ((bytesWritten < 0) || (bytesWritten == (ssize_t)mBytesRemaining))) {
2152bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            // do not wait for async callback in case of error of full write
21533b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent            mWriteAckSequence &= ~1;
2154bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            ALOG_ASSERT(mCallbackThread != 0);
21553b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent            mCallbackThread->setWriteBlocked(mWriteAckSequence);
2156bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        }
215781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
215881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
215981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mNumWrites++;
216081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mInWrite = false;
2161fd4779740ec3e9e865d5514464df26d015354388Eric Laurent    mStandby = false;
2162bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    return bytesWritten;
2163bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}
2164bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
2165bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioFlinger::PlaybackThread::threadLoop_drain()
2166bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{
2167bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    if (mOutput->stream->drain) {
2168bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        ALOGV("draining %s", (mMixerStatus == MIXER_DRAIN_TRACK) ? "early" : "full");
2169bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        if (mUseAsyncWrite) {
21703b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent            ALOGW_IF(mDrainSequence & 1, "threadLoop_drain(): out of sequence drain request");
21713b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent            mDrainSequence |= 1;
2172bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            ALOG_ASSERT(mCallbackThread != 0);
21733b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent            mCallbackThread->setDraining(mDrainSequence);
2174bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        }
2175bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        mOutput->stream->drain(mOutput->stream,
2176bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            (mMixerStatus == MIXER_DRAIN_TRACK) ? AUDIO_DRAIN_EARLY_NOTIFY
2177bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                                                : AUDIO_DRAIN_ALL);
2178bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    }
2179bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}
2180bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
2181bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioFlinger::PlaybackThread::threadLoop_exit()
2182bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{
2183bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    // Default implementation has nothing to do
218481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
218581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
218681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent/*
218781784c37c61b09289654b979567a42bf73cd2b12Eric LaurentThe derived values that are cached:
218825c2dac12114699e90deb1c579cadebce7b91a97Andy Hung - mSinkBufferSize from frame count * frame size
218981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - activeSleepTime from activeSleepTimeUs()
219081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - idleSleepTime from idleSleepTimeUs()
219181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - standbyDelay from mActiveSleepTimeUs (DIRECT only)
219281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - maxPeriod from frame count and sample rate (MIXER only)
219381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
219481784c37c61b09289654b979567a42bf73cd2b12Eric LaurentThe parameters that affect these derived values are:
219581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - frame count
219681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - frame size
219781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - sample rate
219881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - device type: A2DP or not
219981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - device latency
220081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - format: PCM or not
220181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - active sleep time
220281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent - idle sleep time
220381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent*/
220481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
220581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::cacheParameters_l()
220681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
220725c2dac12114699e90deb1c579cadebce7b91a97Andy Hung    mSinkBufferSize = mNormalFrameCount * mFrameSize;
220881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    activeSleepTime = activeSleepTimeUs();
220981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    idleSleepTime = idleSleepTimeUs();
221081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
221181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
221281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::invalidateTracks(audio_stream_type_t streamType)
221381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
22147c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten    ALOGV("MixerThread::invalidateTracks() mixer %p, streamType %d, mTracks.size %d",
221581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            this,  streamType, mTracks.size());
221681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
221781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
221881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    size_t size = mTracks.size();
221981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < size; i++) {
222081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<Track> t = mTracks[i];
222181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (t->streamType() == streamType) {
22225736c35b841de56ce394b4879389f669b61425e6Glenn Kasten            t->invalidate();
222381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
222481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
222581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
222681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
222781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::PlaybackThread::addEffectChain_l(const sp<EffectChain>& chain)
222881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
222981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int session = chain->sessionId();
2230010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung    int16_t* buffer = reinterpret_cast<int16_t*>(mEffectBufferEnabled
2231010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung            ? mEffectBuffer : mSinkBuffer);
223281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool ownsBuffer = false;
223381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
223481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("addEffectChain_l() %p on thread %p for session %d", chain.get(), this, session);
223581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (session > 0) {
223681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // Only one effect chain can be present in direct output thread and it uses
22372098f2744cedf2dc3fa36f608aa965a34602e7c0Andy Hung        // the sink buffer as input
223881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (mType != DIRECT) {
223981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            size_t numSamples = mNormalFrameCount * mChannelCount;
224081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            buffer = new int16_t[numSamples];
224181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            memset(buffer, 0, numSamples * sizeof(int16_t));
224281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGV("addEffectChain_l() creating new input buffer %p session %d", buffer, session);
224381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ownsBuffer = true;
224481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
224581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
224681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // Attach all tracks with same session ID to this chain.
224781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        for (size_t i = 0; i < mTracks.size(); ++i) {
224881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sp<Track> track = mTracks[i];
224981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (session == track->sessionId()) {
225081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGV("addEffectChain_l() track->setMainBuffer track %p buffer %p", track.get(),
225181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        buffer);
225281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                track->setMainBuffer(buffer);
225381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                chain->incTrackCnt();
225481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
225581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
225681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
225781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // indicate all active tracks in the chain
225881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) {
225981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sp<Track> track = mActiveTracks[i].promote();
226081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (track == 0) {
226181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                continue;
226281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
226381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (session == track->sessionId()) {
226481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGV("addEffectChain_l() activating track %p on session %d", track.get(), session);
226581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                chain->incActiveTrackCnt();
226681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
226781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
226881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
2269aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    chain->setThread(this);
227081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    chain->setInBuffer(buffer, ownsBuffer);
2271010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung    chain->setOutBuffer(reinterpret_cast<int16_t*>(mEffectBufferEnabled
2272010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung            ? mEffectBuffer : mSinkBuffer));
227381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Effect chain for session AUDIO_SESSION_OUTPUT_STAGE is inserted at end of effect
227481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // chains list in order to be processed last as it contains output stage effects
227581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Effect chain for session AUDIO_SESSION_OUTPUT_MIX is inserted before
227681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // session AUDIO_SESSION_OUTPUT_STAGE to be processed
227781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // after track specific effects and before output stage
227881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // It is therefore mandatory that AUDIO_SESSION_OUTPUT_MIX == 0 and
227981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // that AUDIO_SESSION_OUTPUT_STAGE < AUDIO_SESSION_OUTPUT_MIX
228081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Effect chain for other sessions are inserted at beginning of effect
228181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // chains list to be processed before output mix effects. Relative order between other
228281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // sessions is not important
228381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    size_t size = mEffectChains.size();
228481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    size_t i = 0;
228581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (i = 0; i < size; i++) {
228681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (mEffectChains[i]->sessionId() < session) {
228781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            break;
228881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
228981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
229081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mEffectChains.insertAt(chain, i);
229181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    checkSuspendOnAddEffectChain_l(chain);
229281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
229381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return NO_ERROR;
229481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
229581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
229681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsize_t AudioFlinger::PlaybackThread::removeEffectChain_l(const sp<EffectChain>& chain)
229781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
229881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int session = chain->sessionId();
229981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
230081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("removeEffectChain_l() %p from thread %p for session %d", chain.get(), this, session);
230181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
230281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mEffectChains.size(); i++) {
230381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (chain == mEffectChains[i]) {
230481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mEffectChains.removeAt(i);
230581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // detach all active tracks from the chain
230681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            for (size_t i = 0 ; i < mActiveTracks.size() ; ++i) {
230781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sp<Track> track = mActiveTracks[i].promote();
230881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (track == 0) {
230981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    continue;
231081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
231181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (session == track->sessionId()) {
231281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    ALOGV("removeEffectChain_l(): stopping track on chain %p for session Id: %d",
231381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                            chain.get(), session);
231481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    chain->decActiveTrackCnt();
231581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
231681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
231781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
231881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // detach all tracks with same session ID from this chain
231981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            for (size_t i = 0; i < mTracks.size(); ++i) {
232081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sp<Track> track = mTracks[i];
232181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (session == track->sessionId()) {
2322010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung                    track->setMainBuffer(reinterpret_cast<int16_t*>(mSinkBuffer));
232381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    chain->decTrackCnt();
232481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
232581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
232681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            break;
232781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
232881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
232981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return mEffectChains.size();
233081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
233181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
233281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::PlaybackThread::attachAuxEffect(
233381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId)
233481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
233581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
233681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return attachAuxEffect_l(track, EffectId);
233781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
233881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
233981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::PlaybackThread::attachAuxEffect_l(
234081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        const sp<AudioFlinger::PlaybackThread::Track> track, int EffectId)
234181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
234281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t status = NO_ERROR;
234381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
234481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (EffectId == 0) {
234581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        track->setAuxBuffer(0, NULL);
234681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    } else {
234781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // Auxiliary effects are always in audio session AUDIO_SESSION_OUTPUT_MIX
234881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<EffectModule> effect = getEffect_l(AUDIO_SESSION_OUTPUT_MIX, EffectId);
234981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (effect != 0) {
235081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if ((effect->desc().flags & EFFECT_FLAG_TYPE_MASK) == EFFECT_FLAG_TYPE_AUXILIARY) {
235181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                track->setAuxBuffer(EffectId, (int32_t *)effect->inBuffer());
235281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            } else {
235381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                status = INVALID_OPERATION;
235481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
235581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else {
235681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            status = BAD_VALUE;
235781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
235881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
235981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return status;
236081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
236181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
236281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::detachAuxEffect_l(int effectId)
236381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
236481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mTracks.size(); ++i) {
236581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<Track> track = mTracks[i];
236681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (track->auxEffectId() == effectId) {
236781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            attachAuxEffect_l(track, 0);
236881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
236981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
237081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
237181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
237281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentbool AudioFlinger::PlaybackThread::threadLoop()
237381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
237481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Vector< sp<Track> > tracksToRemove;
237581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
237681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    standbyTime = systemTime();
237781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
237881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // MIXER
237981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    nsecs_t lastWarning = 0;
238081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
238181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // DUPLICATING
238281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // FIXME could this be made local to while loop?
238381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    writeFrames = 0;
238481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
2385462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen    int lastGeneration = 0;
2386462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen
238781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    cacheParameters_l();
238881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sleepTime = idleSleepTime;
238981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
239081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (mType == MIXER) {
239181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sleepTimeShift = 0;
239281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
239381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
239481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    CpuStats cpuStats;
239581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    const String8 myName(String8::format("thread %p type %d TID %d", this, mType, gettid()));
239681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
239781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    acquireWakeLock();
239881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
23999e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    // mNBLogWriter->log can only be called while thread mutex mLock is held.
24009e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    // So if you need to log when mutex is unlocked, set logString to a non-NULL string,
24019e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    // and then that string will be logged at the next convenient opportunity.
24029e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    const char *logString = NULL;
24039e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten
2404664539d25180ab8f77e0521533ea2821cf28985fEric Laurent    checkSilentMode_l();
2405664539d25180ab8f77e0521533ea2821cf28985fEric Laurent
240681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    while (!exitPending())
240781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    {
240881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        cpuStats.sample(myName);
240981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
241081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        Vector< sp<EffectChain> > effectChains;
241181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
241281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        { // scope for mLock
241381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
241481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            Mutex::Autolock _l(mLock);
241581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
2416021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent            processConfigEvents_l();
24171035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent
24189e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten            if (logString != NULL) {
24199e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten                mNBLogWriter->logTimestamp();
24209e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten                mNBLogWriter->log(logString);
24219e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten                logString = NULL;
24229e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten            }
24239e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten
24244c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten            // Gather the framesReleased counters for all active tracks,
24254c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten            // and latch them atomically with the timestamp.
24264c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten            // FIXME We're using raw pointers as indices. A unique track ID would be a better index.
24274c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten            mLatchD.mFramesReleased.clear();
24284c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten            size_t size = mActiveTracks.size();
24294c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten            for (size_t i = 0; i < size; i++) {
24304c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten                sp<Track> t = mActiveTracks[i].promote();
24314c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten                if (t != 0) {
24324c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten                    mLatchD.mFramesReleased.add(t.get(),
24334c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten                            t->mAudioTrackServerProxy->framesReleased());
24344c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten                }
24354c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten            }
2436bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten            if (mLatchDValid) {
2437bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten                mLatchQ = mLatchD;
2438bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten                mLatchDValid = false;
2439bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten                mLatchQValid = true;
2440bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten            }
2441bd096fd9d8e5fc0e62f98807f4818a06f70d0812Glenn Kasten
244281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            saveOutputTracks();
2443bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            if (mSignalPending) {
2444bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                // A signal was raised while we were unlocked
2445bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                mSignalPending = false;
2446bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            } else if (waitingAsyncCallback_l()) {
2447bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                if (exitPending()) {
2448bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    break;
2449bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                }
2450bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                releaseWakeLock_l();
2451462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen                mWakeLockUids.clear();
2452462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen                mActiveTracksGeneration++;
2453bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                ALOGV("wait async completion");
2454bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                mWaitWorkCV.wait(mLock);
2455bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                ALOGV("async completion/wake");
2456bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                acquireWakeLock_l();
2457972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent                standbyTime = systemTime() + standbyDelay;
2458972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent                sleepTime = 0;
2459ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent
2460ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent                continue;
2461ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent            }
2462ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent            if ((!mActiveTracks.size() && systemTime() > standbyTime) ||
2463bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                                   isSuspended()) {
2464bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                // put audio hardware into standby after short delay
2465bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                if (shouldStandby_l()) {
246681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
246781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    threadLoop_standby();
246881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
246981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    mStandby = true;
247081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
247181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
247281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (!mActiveTracks.size() && mConfigEvents.isEmpty()) {
247381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    // we're about to wait, flush the binder command buffer
247481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    IPCThreadState::self()->flushCommands();
247581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
247681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    clearOutputTracks();
247781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
247881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    if (exitPending()) {
247981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        break;
248081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    }
248181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
248281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    releaseWakeLock_l();
2483462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen                    mWakeLockUids.clear();
2484462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen                    mActiveTracksGeneration++;
248581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    // wait until we have something to do...
248681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    ALOGV("%s going to sleep", myName.string());
248781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    mWaitWorkCV.wait(mLock);
248881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    ALOGV("%s waking up", myName.string());
248981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    acquireWakeLock_l();
249081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
249181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    mMixerStatus = MIXER_IDLE;
249281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    mMixerStatusIgnoringFastTracks = MIXER_IDLE;
249381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    mBytesWritten = 0;
2494bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    mBytesRemaining = 0;
249581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    checkSilentMode_l();
249681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
249781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    standbyTime = systemTime() + standbyDelay;
249881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    sleepTime = idleSleepTime;
249981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    if (mType == MIXER) {
250081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        sleepTimeShift = 0;
250181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    }
250281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
250381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    continue;
250481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
250581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
250681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // mMixerStatusIgnoringFastTracks is also updated internally
250781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mMixerStatus = prepareTracks_l(&tracksToRemove);
250881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
2509462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen            // compare with previously applied list
2510462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen            if (lastGeneration != mActiveTracksGeneration) {
2511462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen                // update wakelock
2512462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen                updateWakeLockUids_l(mWakeLockUids);
2513462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen                lastGeneration = mActiveTracksGeneration;
2514462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen            }
2515462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen
251681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // prevent any changes in effect chain list and in each effect chain
251781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // during mixing and effect process as the audio buffers could be deleted
251881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // or modified if an effect is created or deleted
251981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            lockEffectChains_l(effectChains);
2520462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen        } // mLock scope ends
252181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
2522bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        if (mBytesRemaining == 0) {
2523bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            mCurrentWriteLength = 0;
2524bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            if (mMixerStatus == MIXER_TRACKS_READY) {
2525bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                // threadLoop_mix() sets mCurrentWriteLength
2526bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                threadLoop_mix();
2527bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            } else if ((mMixerStatus != MIXER_DRAIN_TRACK)
2528bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                        && (mMixerStatus != MIXER_DRAIN_ALL)) {
2529bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                // threadLoop_sleepTime sets sleepTime to 0 if data
2530bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                // must be written to HAL
2531bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                threadLoop_sleepTime();
2532bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                if (sleepTime == 0) {
253325c2dac12114699e90deb1c579cadebce7b91a97Andy Hung                    mCurrentWriteLength = mSinkBufferSize;
2534bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                }
2535bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            }
253698ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung            // Either threadLoop_mix() or threadLoop_sleepTime() should have set
253798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung            // mMixerBuffer with data if mMixerBufferValid is true and sleepTime == 0.
253898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung            // Merge mMixerBuffer data into mEffectBuffer (if any effects are valid)
253998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung            // or mSinkBuffer (if there are no effects).
254098ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung            //
254198ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung            // This is done pre-effects computation; if effects change to
254298ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung            // support higher precision, this needs to move.
254398ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung            //
254498ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung            // mMixerBufferValid is only set true by MixerThread::prepareTracks_l().
254598ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung            // TODO use sleepTime == 0 as an additional condition.
254698ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung            if (mMixerBufferValid) {
254798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung                void *buffer = mEffectBufferValid ? mEffectBuffer : mSinkBuffer;
254898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung                audio_format_t format = mEffectBufferValid ? mEffectBufferFormat : mFormat;
254998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung
255098ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung                memcpy_by_audio_format(buffer, format, mMixerBuffer, mMixerBufferFormat,
255198ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung                        mNormalFrameCount * mChannelCount);
255298ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung            }
255398ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung
2554bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            mBytesRemaining = mCurrentWriteLength;
2555bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            if (isSuspended()) {
2556bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                sleepTime = suspendSleepTimeUs();
2557bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                // simulate write to HAL when suspended
255825c2dac12114699e90deb1c579cadebce7b91a97Andy Hung                mBytesWritten += mSinkBufferSize;
2559bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                mBytesRemaining = 0;
2560bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            }
256181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
2562bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            // only process effects if we're going to write
256359fe010bcc072597852454a2ec53d7b0a2002a3bEric Laurent            if (sleepTime == 0 && mType != OFFLOAD) {
2564bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                for (size_t i = 0; i < effectChains.size(); i ++) {
2565bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    effectChains[i]->process_l();
2566bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                }
256781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
256881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
256959fe010bcc072597852454a2ec53d7b0a2002a3bEric Laurent        // Process effect chains for offloaded thread even if no audio
257059fe010bcc072597852454a2ec53d7b0a2002a3bEric Laurent        // was read from audio track: process only updates effect state
257159fe010bcc072597852454a2ec53d7b0a2002a3bEric Laurent        // and thus does have to be synchronized with audio writes but may have
257259fe010bcc072597852454a2ec53d7b0a2002a3bEric Laurent        // to be called while waiting for async write callback
257359fe010bcc072597852454a2ec53d7b0a2002a3bEric Laurent        if (mType == OFFLOAD) {
257459fe010bcc072597852454a2ec53d7b0a2002a3bEric Laurent            for (size_t i = 0; i < effectChains.size(); i ++) {
257559fe010bcc072597852454a2ec53d7b0a2002a3bEric Laurent                effectChains[i]->process_l();
257659fe010bcc072597852454a2ec53d7b0a2002a3bEric Laurent            }
257759fe010bcc072597852454a2ec53d7b0a2002a3bEric Laurent        }
257881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
257998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung        // Only if the Effects buffer is enabled and there is data in the
258098ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung        // Effects buffer (buffer valid), we need to
258198ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung        // copy into the sink buffer.
258298ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung        // TODO use sleepTime == 0 as an additional condition.
258398ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung        if (mEffectBufferValid) {
258498ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung            //ALOGV("writing effect buffer to sink buffer format %#x", mFormat);
258598ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung            memcpy_by_audio_format(mSinkBuffer, mFormat, mEffectBuffer, mEffectBufferFormat,
258698ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung                    mNormalFrameCount * mChannelCount);
258798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung        }
258898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung
258981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // enable changes in effect chain
259081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        unlockEffectChains(effectChains);
259181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
2592bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        if (!waitingAsyncCallback()) {
2593bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            // sleepTime == 0 means we must write to audio hardware
2594bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            if (sleepTime == 0) {
2595bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                if (mBytesRemaining) {
2596bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    ssize_t ret = threadLoop_write();
2597bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    if (ret < 0) {
2598bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                        mBytesRemaining = 0;
2599bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    } else {
2600bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                        mBytesWritten += ret;
2601bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                        mBytesRemaining -= ret;
2602bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    }
2603bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                } else if ((mMixerStatus == MIXER_DRAIN_TRACK) ||
2604bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                        (mMixerStatus == MIXER_DRAIN_ALL)) {
2605bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    threadLoop_drain();
2606bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                }
26074944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten                if (mType == MIXER) {
26084944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten                    // write blocked detection
26094944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten                    nsecs_t now = systemTime();
26104944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten                    nsecs_t delta = now - mLastWriteTime;
26114944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten                    if (!mStandby && delta > maxPeriod) {
26124944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten                        mNumDelayedWrites++;
26134944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten                        if ((now - lastWarning) > kWarningThrottleNs) {
26144944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten                            ATRACE_NAME("underrun");
26154944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten                            ALOGW("write blocked for %llu msecs, %d delayed writes, thread %p",
26164944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten                                    ns2ms(delta), mNumDelayedWrites, this);
26174944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten                            lastWarning = now;
26184944acb7355b3aa25748fd25945a363a69d65444Glenn Kasten                        }
2619bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    }
262081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
262181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
2622bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            } else {
2623bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                usleep(sleepTime);
2624bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            }
262581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
262681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
262781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // Finally let go of removed track(s), without the lock held
262881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // since we can't guarantee the destructors won't acquire that
262981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // same lock.  This will also mutate and push a new fast mixer state.
263081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        threadLoop_removeTracks(tracksToRemove);
263181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        tracksToRemove.clear();
263281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
263381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // FIXME I don't understand the need for this here;
263481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        //       it was in the original code but maybe the
263581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        //       assignment in saveOutputTracks() makes this unnecessary?
263681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        clearOutputTracks();
263781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
263881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // Effect chains will be actually deleted here if they were removed from
263981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // mEffectChains list during mixing or effects processing
264081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        effectChains.clear();
264181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
264281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // FIXME Note that the above .clear() is no longer necessary since effectChains
264381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // is now local to this block, but will keep it for now (at least until merge done).
264481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
264581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
2646bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    threadLoop_exit();
2647bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
2648cf817a2330936947df94c11859f48771f5596a59Eric Laurent    if (!mStandby) {
2649cf817a2330936947df94c11859f48771f5596a59Eric Laurent        threadLoop_standby();
2650cf817a2330936947df94c11859f48771f5596a59Eric Laurent        mStandby = true;
265181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
265281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
265381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    releaseWakeLock();
2654462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen    mWakeLockUids.clear();
2655462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen    mActiveTracksGeneration++;
265681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
265781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("Thread %p type %d exiting", this, mType);
265881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return false;
265981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
266081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
2661bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent// removeTracks_l() must be called with ThreadBase::mLock held
2662bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioFlinger::PlaybackThread::removeTracks_l(const Vector< sp<Track> >& tracksToRemove)
2663bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{
2664bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    size_t count = tracksToRemove.size();
266534fca34606b448e6b71c2942f63cb13a0aebd620Glenn Kasten    if (count > 0) {
2666bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        for (size_t i=0 ; i<count ; i++) {
2667bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            const sp<Track>& track = tracksToRemove.itemAt(i);
2668bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            mActiveTracks.remove(track);
2669462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen            mWakeLockUids.remove(track->uid());
2670462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen            mActiveTracksGeneration++;
2671bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            ALOGV("removeTracks_l removing track on session %d", track->sessionId());
2672bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            sp<EffectChain> chain = getEffectChain_l(track->sessionId());
2673bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            if (chain != 0) {
2674bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                ALOGV("stopping track on chain %p for session Id: %d", chain.get(),
2675bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                        track->sessionId());
2676bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                chain->decActiveTrackCnt();
2677bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            }
2678bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            if (track->isTerminated()) {
2679bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                removeTrack_l(track);
2680bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            }
2681bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        }
2682bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    }
2683bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
2684bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}
268581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
2686accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurentstatus_t AudioFlinger::PlaybackThread::getTimestamp_l(AudioTimestamp& timestamp)
2687accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent{
2688accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent    if (mNormalSink != 0) {
2689accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent        return mNormalSink->getTimestamp(timestamp);
2690accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent    }
2691ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent    if ((mType == OFFLOAD || mType == DIRECT) && mOutput->stream->get_presentation_position) {
2692accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent        uint64_t position64;
2693accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent        int ret = mOutput->stream->get_presentation_position(
2694accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent                                                mOutput->stream, &position64, &timestamp.mTime);
2695accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent        if (ret == 0) {
2696accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent            timestamp.mPosition = (uint32_t)position64;
2697accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent            return NO_ERROR;
2698accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent        }
2699accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent    }
2700accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent    return INVALID_OPERATION;
2701accc147666bfd37fc8b4ef745f18a8c751555ec2Eric Laurent}
27021c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
27031c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurentstatus_t AudioFlinger::PlaybackThread::createAudioPatch_l(const struct audio_patch *patch,
27041c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                                          audio_patch_handle_t *handle)
27051c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent{
27061c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    status_t status = NO_ERROR;
27071c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    if (mOutput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) {
27081c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        // store new device and send to effects
27091c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        audio_devices_t type = AUDIO_DEVICE_NONE;
27101c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        for (unsigned int i = 0; i < patch->num_sinks; i++) {
27111c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent            type |= patch->sinks[i].ext.device.type;
27121c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        }
27131c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        mOutDevice = type;
27141c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        for (size_t i = 0; i < mEffectChains.size(); i++) {
27151c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent            mEffectChains[i]->setDevice_l(mOutDevice);
27161c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        }
27171c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
27181c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        audio_hw_device_t *hwDevice = mOutput->audioHwDev->hwDevice();
27191c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        status = hwDevice->create_audio_patch(hwDevice,
27201c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                               patch->num_sources,
27211c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                               patch->sources,
27221c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                               patch->num_sinks,
27231c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                               patch->sinks,
27241c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                               handle);
27251c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    } else {
27261c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        ALOG_ASSERT(false, "createAudioPatch_l() called on a pre 3.0 HAL");
27271c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    }
27281c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    return status;
27291c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent}
27301c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
27311c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurentstatus_t AudioFlinger::PlaybackThread::releaseAudioPatch_l(const audio_patch_handle_t handle)
27321c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent{
27331c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    status_t status = NO_ERROR;
27341c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    if (mOutput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) {
27351c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        audio_hw_device_t *hwDevice = mOutput->audioHwDev->hwDevice();
27361c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        status = hwDevice->release_audio_patch(hwDevice, handle);
27371c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    } else {
27381c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        ALOG_ASSERT(false, "releaseAudioPatch_l() called on a pre 3.0 HAL");
27391c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    }
27401c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    return status;
27411c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent}
27421c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
274383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentvoid AudioFlinger::PlaybackThread::addPatchTrack(const sp<PatchTrack>& track)
274483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{
274583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    Mutex::Autolock _l(mLock);
274683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    mTracks.add(track);
274783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent}
274883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
274983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentvoid AudioFlinger::PlaybackThread::deletePatchTrack(const sp<PatchTrack>& track)
275083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{
275183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    Mutex::Autolock _l(mLock);
275283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    destroyTrack_l(track);
275383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent}
275483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
275583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentvoid AudioFlinger::PlaybackThread::getAudioPortConfig(struct audio_port_config *config)
275683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{
275783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    ThreadBase::getAudioPortConfig(config);
275883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    config->role = AUDIO_PORT_ROLE_SOURCE;
275983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    config->ext.mix.hw_module = mOutput->audioHwDev->handle();
276083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    config->ext.mix.usecase.stream = AUDIO_STREAM_DEFAULT;
276183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent}
276283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
276381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
276481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
276581784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::MixerThread::MixerThread(const sp<AudioFlinger>& audioFlinger, AudioStreamOut* output,
276681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        audio_io_handle_t id, audio_devices_t device, type_t type)
276781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    :   PlaybackThread(audioFlinger, output, id, device, type),
276881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // mAudioMixer below
276981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // mFastMixer below
277081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mFastMixerFutex(0)
277181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // mOutputSink below
277281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // mPipeSink below
277381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // mNormalSink below
277481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
277581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("MixerThread() id=%d device=%#x type=%d", id, device, type);
2776f6ed423af92a56ef54bba23eba883b1f21448b54Glenn Kasten    ALOGV("mSampleRate=%u, mChannelMask=%#x, mChannelCount=%u, mFormat=%d, mFrameSize=%u, "
277781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            "mFrameCount=%d, mNormalFrameCount=%d",
277881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mSampleRate, mChannelMask, mChannelCount, mFormat, mFrameSize, mFrameCount,
277981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mNormalFrameCount);
278081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate);
278181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
278281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // create an NBAIO sink for the HAL output stream, and negotiate
278381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mOutputSink = new AudioStreamOutSink(output->stream);
278481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    size_t numCounterOffers = 0;
2785f69f9869514730aebe5724c461768507084dfff7Glenn Kasten    const NBAIO_Format offers[1] = {Format_from_SR_C(mSampleRate, mChannelCount, mFormat)};
278681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ssize_t index = mOutputSink->negotiate(offers, 1, NULL, numCounterOffers);
278781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOG_ASSERT(index == 0);
278881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
278981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // initialize fast mixer depending on configuration
279081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool initFastMixer;
279181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    switch (kUseFastMixer) {
279281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    case FastMixer_Never:
279381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        initFastMixer = false;
279481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        break;
279581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    case FastMixer_Always:
279681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        initFastMixer = true;
279781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        break;
279881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    case FastMixer_Static:
279981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    case FastMixer_Dynamic:
280081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        initFastMixer = mFrameCount < mNormalFrameCount;
280181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        break;
280281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
280381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (initFastMixer) {
28041258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung        audio_format_t fastMixerFormat;
28051258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung        if (mMixerBufferEnabled && mEffectBufferEnabled) {
28061258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung            fastMixerFormat = AUDIO_FORMAT_PCM_FLOAT;
28071258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung        } else {
28081258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung            fastMixerFormat = AUDIO_FORMAT_PCM_16_BIT;
28091258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung        }
28101258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung        if (mFormat != fastMixerFormat) {
28111258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung            // change our Sink format to accept our intermediate precision
28121258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung            mFormat = fastMixerFormat;
28131258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung            free(mSinkBuffer);
28141258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung            mFrameSize = mChannelCount * audio_bytes_per_sample(mFormat);
28151258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung            const size_t sinkBufferSize = mNormalFrameCount * mFrameSize;
28161258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung            (void)posix_memalign(&mSinkBuffer, 32, sinkBufferSize);
28171258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung        }
281881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
281981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // create a MonoPipe to connect our submix to FastMixer
282081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        NBAIO_Format format = mOutputSink->format();
2821ba0b34c18da93681c0813ecdab19b0e215b6d261Glenn Kasten        NBAIO_Format origformat = format;
28221258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung        // adjust format to match that of the Fast Mixer
28231258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung        format.mFormat = fastMixerFormat;
28241258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung        format.mFrameSize = audio_bytes_per_sample(format.mFormat) * format.mChannelCount;
28251258c1ab592a899fabb1e31eb5db2ef413b6f38aAndy Hung
282681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // This pipe depth compensates for scheduling latency of the normal mixer thread.
282781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // When it wakes up after a maximum latency, it runs a few cycles quickly before
282881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // finally blocking.  Note the pipe implementation rounds up the request to a power of 2.
282981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        MonoPipe *monoPipe = new MonoPipe(mNormalFrameCount * 4, format, true /*writeCanBlock*/);
283081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        const NBAIO_Format offers[1] = {format};
283181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        size_t numCounterOffers = 0;
283281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ssize_t index = monoPipe->negotiate(offers, 1, NULL, numCounterOffers);
283381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOG_ASSERT(index == 0);
283481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        monoPipe->setAvgFrames((mScreenState & 1) ?
283581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                (monoPipe->maxFrames() * 7) / 8 : mNormalFrameCount * 2);
283681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mPipeSink = monoPipe;
283781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
283846909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK
2839da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        if (mTeeSinkOutputEnabled) {
2840da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            // create a Pipe to archive a copy of FastMixer's output for dumpsys
2841ba0b34c18da93681c0813ecdab19b0e215b6d261Glenn Kasten            Pipe *teeSink = new Pipe(mTeeSinkOutputFrames, origformat);
2842ba0b34c18da93681c0813ecdab19b0e215b6d261Glenn Kasten            const NBAIO_Format offers2[1] = {origformat};
2843da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            numCounterOffers = 0;
2844ba0b34c18da93681c0813ecdab19b0e215b6d261Glenn Kasten            index = teeSink->negotiate(offers2, 1, NULL, numCounterOffers);
2845da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            ALOG_ASSERT(index == 0);
2846da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            mTeeSink = teeSink;
2847da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            PipeReader *teeSource = new PipeReader(*teeSink);
2848da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            numCounterOffers = 0;
2849ba0b34c18da93681c0813ecdab19b0e215b6d261Glenn Kasten            index = teeSource->negotiate(offers2, 1, NULL, numCounterOffers);
2850da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            ALOG_ASSERT(index == 0);
2851da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten            mTeeSource = teeSource;
2852da6ef1320d0161b1640dc84d7a9c5a25860c3619Glenn Kasten        }
285346909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif
285481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
285581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // create fast mixer and configure it initially with just one fast track for our submix
285681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mFastMixer = new FastMixer();
285781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        FastMixerStateQueue *sq = mFastMixer->sq();
285881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef STATE_QUEUE_DUMP
285981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sq->setObserverDump(&mStateQueueObserverDump);
286081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sq->setMutatorDump(&mStateQueueMutatorDump);
286181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif
286281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        FastMixerState *state = sq->begin();
286381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        FastTrack *fastTrack = &state->mFastTracks[0];
286481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // wrap the source side of the MonoPipe to make it an AudioBufferProvider
286581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        fastTrack->mBufferProvider = new SourceAudioBufferProvider(new MonoPipeReader(monoPipe));
286681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        fastTrack->mVolumeProvider = NULL;
2867e8a1ced4da17dc6c07803dc2af8060f62a8389c1Andy Hung        fastTrack->mChannelMask = mChannelMask; // mPipeSink channel mask for audio to FastMixer
2868e8a1ced4da17dc6c07803dc2af8060f62a8389c1Andy Hung        fastTrack->mFormat = mFormat; // mPipeSink format for audio to FastMixer
286981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        fastTrack->mGeneration++;
287081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        state->mFastTracksGen++;
287181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        state->mTrackMask = 1;
287281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // fast mixer will use the HAL output sink
287381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        state->mOutputSink = mOutputSink.get();
287481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        state->mOutputSinkGen++;
287581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        state->mFrameCount = mFrameCount;
287681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        state->mCommand = FastMixerState::COLD_IDLE;
287781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // already done in constructor initialization list
287881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        //mFastMixerFutex = 0;
287981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        state->mColdFutexAddr = &mFastMixerFutex;
288081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        state->mColdGen++;
288181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        state->mDumpState = &mFastMixerDumpState;
288246909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK
288381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        state->mTeeSink = mTeeSink.get();
288446909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif
28859e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten        mFastMixerNBLogWriter = audioFlinger->newWriter_l(kFastMixerLogSize, "FastMixer");
28869e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten        state->mNBLogWriter = mFastMixerNBLogWriter.get();
288781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sq->end();
288881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED);
288981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
289081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // start the fast mixer
289181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mFastMixer->run("FastMixer", PRIORITY_URGENT_AUDIO);
289281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        pid_t tid = mFastMixer->getTid();
289381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        int err = requestPriority(getpid_cached, tid, kPriorityFastMixer);
289481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (err != 0) {
289581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGW("Policy SCHED_FIFO priority %d is unavailable for pid %d tid %d; error %d",
289681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    kPriorityFastMixer, getpid_cached, tid, err);
289781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
289881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
289981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef AUDIO_WATCHDOG
290081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // create and start the watchdog
290181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mAudioWatchdog = new AudioWatchdog();
290281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mAudioWatchdog->setDump(&mAudioWatchdogDump);
290381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mAudioWatchdog->run("AudioWatchdog", PRIORITY_URGENT_AUDIO);
290481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        tid = mAudioWatchdog->getTid();
290581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        err = requestPriority(getpid_cached, tid, kPriorityFastMixer);
290681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (err != 0) {
290781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGW("Policy SCHED_FIFO priority %d is unavailable for pid %d tid %d; error %d",
290881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    kPriorityFastMixer, getpid_cached, tid, err);
290981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
291081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif
291181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
291281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
291381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
291481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    switch (kUseFastMixer) {
291581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    case FastMixer_Never:
291681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    case FastMixer_Dynamic:
291781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mNormalSink = mOutputSink;
291881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        break;
291981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    case FastMixer_Always:
292081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mNormalSink = mPipeSink;
292181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        break;
292281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    case FastMixer_Static:
292381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mNormalSink = initFastMixer ? mPipeSink : mOutputSink;
292481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        break;
292581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
292681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
292781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
292881784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::MixerThread::~MixerThread()
292981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
29304d23ca370dd0ce584f49a80ef9dfcdbb75ba2c8eGlenn Kasten    if (mFastMixer != 0) {
293181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        FastMixerStateQueue *sq = mFastMixer->sq();
293281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        FastMixerState *state = sq->begin();
293381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (state->mCommand == FastMixerState::COLD_IDLE) {
293481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            int32_t old = android_atomic_inc(&mFastMixerFutex);
293581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (old == -1) {
2936ee499291404a192b059f2e04c5afc65aa6cdd74cElliott Hughes                (void) syscall(__NR_futex, &mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1);
293781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
293881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
293981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        state->mCommand = FastMixerState::EXIT;
294081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sq->end();
294181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED);
294281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mFastMixer->join();
294381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // Though the fast mixer thread has exited, it's state queue is still valid.
294481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // We'll use that extract the final state which contains one remaining fast track
294581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // corresponding to our sub-mix.
294681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        state = sq->begin();
294781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOG_ASSERT(state->mTrackMask == 1);
294881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        FastTrack *fastTrack = &state->mFastTracks[0];
294981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOG_ASSERT(fastTrack->mBufferProvider != NULL);
295081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        delete fastTrack->mBufferProvider;
295181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sq->end(false /*didModify*/);
29524d23ca370dd0ce584f49a80ef9dfcdbb75ba2c8eGlenn Kasten        mFastMixer.clear();
295381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef AUDIO_WATCHDOG
295481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (mAudioWatchdog != 0) {
295581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mAudioWatchdog->requestExit();
295681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mAudioWatchdog->requestExitAndWait();
295781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mAudioWatchdog.clear();
295881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
295981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif
296081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
29619e58b552f51b00b3b674102876bd6c77ef3da806Glenn Kasten    mAudioFlinger->unregisterWriter(mFastMixerNBLogWriter);
296281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    delete mAudioMixer;
296381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
296481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
296581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
296681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::MixerThread::correctLatency_l(uint32_t latency) const
296781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
29684d23ca370dd0ce584f49a80ef9dfcdbb75ba2c8eGlenn Kasten    if (mFastMixer != 0) {
296981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        MonoPipe *pipe = (MonoPipe *)mPipeSink.get();
297081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        latency += (pipe->getAvgFrames() * 1000) / mSampleRate;
297181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
297281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return latency;
297381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
297481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
297581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
297681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::MixerThread::threadLoop_removeTracks(const Vector< sp<Track> >& tracksToRemove)
297781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
297881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread::threadLoop_removeTracks(tracksToRemove);
297981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
298081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
2981bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentssize_t AudioFlinger::MixerThread::threadLoop_write()
298281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
298381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // FIXME we should only do one push per cycle; confirm this is true
298481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Start the fast mixer if it's not already running
29854d23ca370dd0ce584f49a80ef9dfcdbb75ba2c8eGlenn Kasten    if (mFastMixer != 0) {
298681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        FastMixerStateQueue *sq = mFastMixer->sq();
298781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        FastMixerState *state = sq->begin();
298881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (state->mCommand != FastMixerState::MIX_WRITE &&
298981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)) {
299081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (state->mCommand == FastMixerState::COLD_IDLE) {
299181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                int32_t old = android_atomic_inc(&mFastMixerFutex);
299281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (old == -1) {
2993ee499291404a192b059f2e04c5afc65aa6cdd74cElliott Hughes                    (void) syscall(__NR_futex, &mFastMixerFutex, FUTEX_WAKE_PRIVATE, 1);
299481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
299581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef AUDIO_WATCHDOG
299681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (mAudioWatchdog != 0) {
299781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    mAudioWatchdog->resume();
299881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
299981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif
300081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
300181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            state->mCommand = FastMixerState::MIX_WRITE;
30024182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten            mFastMixerDumpState.increaseSamplingN(mAudioFlinger->isLowRamDevice() ?
30034182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten                    FastMixerDumpState::kSamplingNforLowRamDevice : FastMixerDumpState::kSamplingN);
300481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sq->end();
300581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED);
300681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (kUseFastMixer == FastMixer_Dynamic) {
300781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                mNormalSink = mPipeSink;
300881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
300981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else {
301081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sq->end(false /*didModify*/);
301181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
301281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
3013bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    return PlaybackThread::threadLoop_write();
301481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
301581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
301681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::MixerThread::threadLoop_standby()
301781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
301881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Idle the fast mixer if it's currently running
30194d23ca370dd0ce584f49a80ef9dfcdbb75ba2c8eGlenn Kasten    if (mFastMixer != 0) {
302081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        FastMixerStateQueue *sq = mFastMixer->sq();
302181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        FastMixerState *state = sq->begin();
302281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (!(state->mCommand & FastMixerState::IDLE)) {
302381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            state->mCommand = FastMixerState::COLD_IDLE;
302481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            state->mColdFutexAddr = &mFastMixerFutex;
302581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            state->mColdGen++;
302681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mFastMixerFutex = 0;
302781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sq->end();
302881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now
302981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED);
303081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (kUseFastMixer == FastMixer_Dynamic) {
303181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                mNormalSink = mOutputSink;
303281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
303381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef AUDIO_WATCHDOG
303481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (mAudioWatchdog != 0) {
303581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                mAudioWatchdog->pause();
303681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
303781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif
303881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else {
303981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sq->end(false /*didModify*/);
304081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
304181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
304281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread::threadLoop_standby();
304381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
304481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
3045bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentbool AudioFlinger::PlaybackThread::waitingAsyncCallback_l()
3046bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{
3047bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    return false;
3048bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}
3049bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
3050bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentbool AudioFlinger::PlaybackThread::shouldStandby_l()
3051bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{
3052bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    return !mStandby;
3053bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}
3054bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
3055bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentbool AudioFlinger::PlaybackThread::waitingAsyncCallback()
3056bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{
3057bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    Mutex::Autolock _l(mLock);
3058bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    return waitingAsyncCallback_l();
3059bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}
3060bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
306181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// shared by MIXER and DIRECT, overridden by DUPLICATING
306281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::PlaybackThread::threadLoop_standby()
306381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
306481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("Audio hardware entering standby, mixer %p, suspend count %d", this, mSuspended);
306581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mOutput->stream->common.standby(&mOutput->stream->common);
3066bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    if (mUseAsyncWrite != 0) {
30673b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent        // discard any pending drain or write ack by incrementing sequence
30683b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent        mWriteAckSequence = (mWriteAckSequence + 2) & ~1;
30693b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent        mDrainSequence = (mDrainSequence + 2) & ~1;
3070bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        ALOG_ASSERT(mCallbackThread != 0);
30713b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent        mCallbackThread->setWriteBlocked(mWriteAckSequence);
30723b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent        mCallbackThread->setDraining(mDrainSequence);
3073bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    }
307481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
307581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
30764c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew Georgevoid AudioFlinger::PlaybackThread::onAddNewTrack_l()
30774c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George{
30784c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George    ALOGV("signal playback thread");
30794c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George    broadcast_l();
30804c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George}
30814c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George
308281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::MixerThread::threadLoop_mix()
308381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
308481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // obtain the presentation timestamp of the next output buffer
308581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int64_t pts;
308681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t status = INVALID_OPERATION;
308781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
308881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (mNormalSink != 0) {
308981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        status = mNormalSink->getNextWriteTimestamp(&pts);
309081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    } else {
309181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        status = mOutputSink->getNextWriteTimestamp(&pts);
309281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
309381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
309481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (status != NO_ERROR) {
309581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        pts = AudioBufferProvider::kInvalidPTS;
309681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
309781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
309881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // mix buffers...
309981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mAudioMixer->process(pts);
310025c2dac12114699e90deb1c579cadebce7b91a97Andy Hung    mCurrentWriteLength = mSinkBufferSize;
310181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // increase sleep time progressively when application underrun condition clears.
310281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Only increase sleep time if the mixer is ready for two consecutive times to avoid
310381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // that a steady state of alternating ready/not ready conditions keeps the sleep time
310481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // such that we would underrun the audio HAL.
310581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if ((sleepTime == 0) && (sleepTimeShift > 0)) {
310681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sleepTimeShift--;
310781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
310881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sleepTime = 0;
310981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    standbyTime = systemTime() + standbyDelay;
311081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    //TODO: delay standby when effects have a tail
31114c053ea158b29fa2cdd4c6f39d3c8da4ee5a7a02Glenn Kasten
311281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
311381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
311481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::MixerThread::threadLoop_sleepTime()
311581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
311681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // If no tracks are ready, sleep once for the duration of an output
311781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // buffer size, then write 0s to the output
311881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (sleepTime == 0) {
311981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (mMixerStatus == MIXER_TRACKS_ENABLED) {
312081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sleepTime = activeSleepTime >> sleepTimeShift;
312181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (sleepTime < kMinThreadSleepTimeUs) {
312281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sleepTime = kMinThreadSleepTimeUs;
312381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
312481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // reduce sleep time in case of consecutive application underruns to avoid
312581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // starving the audio HAL. As activeSleepTimeUs() is larger than a buffer
312681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // duration we would end up writing less data than needed by the audio HAL if
312781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // the condition persists.
312881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (sleepTimeShift < kMaxThreadSleepTimeShift) {
312981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                sleepTimeShift++;
313081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
313181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else {
313281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sleepTime = idleSleepTime;
313381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
313481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    } else if (mBytesWritten != 0 || (mMixerStatus == MIXER_TRACKS_ENABLED)) {
313598ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung        // clear out mMixerBuffer or mSinkBuffer, to ensure buffers are cleared
313698ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung        // before effects processing or output.
313798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung        if (mMixerBufferValid) {
313898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung            memset(mMixerBuffer, 0, mMixerBufferSize);
313998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung        } else {
314098ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung            memset(mSinkBuffer, 0, mSinkBufferSize);
314198ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung        }
314281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sleepTime = 0;
314381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGV_IF(mBytesWritten == 0 && (mMixerStatus == MIXER_TRACKS_ENABLED),
314481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                "anticipated start");
314581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
314681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // TODO add standby time extension fct of effect tail
314781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
314881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
314981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// prepareTracks_l() must be called with ThreadBase::mLock held
315081784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::PlaybackThread::mixer_state AudioFlinger::MixerThread::prepareTracks_l(
315181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        Vector< sp<Track> > *tracksToRemove)
315281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
315381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
315481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mixer_state mixerStatus = MIXER_IDLE;
315581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // find out which tracks need to be processed
315681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    size_t count = mActiveTracks.size();
315781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    size_t mixedTracks = 0;
315881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    size_t tracksWithEffect = 0;
315981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // counts only _active_ fast tracks
316081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    size_t fastTracks = 0;
316181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t resetMask = 0; // bit mask of fast tracks that need to be reset
316281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
316381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    float masterVolume = mMasterVolume;
316481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool masterMute = mMasterMute;
316581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
316681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (masterMute) {
316781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        masterVolume = 0;
316881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
316981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Delegate master volume control to effect in output mix effect chain if needed
317081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<EffectChain> chain = getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX);
317181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (chain != 0) {
317281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        uint32_t v = (uint32_t)(masterVolume * (1 << 24));
317381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        chain->setVolume_l(&v, &v);
317481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        masterVolume = (float)((v + (1 << 23)) >> 24);
317581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        chain.clear();
317681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
317781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
317881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // prepare a new state to push
317981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    FastMixerStateQueue *sq = NULL;
318081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    FastMixerState *state = NULL;
318181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool didModify = false;
318281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    FastMixerStateQueue::block_t block = FastMixerStateQueue::BLOCK_UNTIL_PUSHED;
31834d23ca370dd0ce584f49a80ef9dfcdbb75ba2c8eGlenn Kasten    if (mFastMixer != 0) {
318481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sq = mFastMixer->sq();
318581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        state = sq->begin();
318681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
318781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
318869aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    mMixerBufferValid = false;  // mMixerBuffer has no valid data until appropriate tracks found.
318998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung    mEffectBufferValid = false; // mEffectBuffer has no valid data until tracks found.
319069aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung
319181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i=0 ; i<count ; i++) {
31929fdcb0a9497ca290bcf364b10868587b6bde3a34Glenn Kasten        const sp<Track> t = mActiveTracks[i].promote();
319381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (t == 0) {
319481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            continue;
319581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
319681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
319781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // this const just means the local variable doesn't change
319881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        Track* const track = t.get();
319981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
320081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // process fast tracks
320181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (track->isFastTrack()) {
320281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
320381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // It's theoretically possible (though unlikely) for a fast track to be created
320481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // and then removed within the same normal mix cycle.  This is not a problem, as
320581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // the track never becomes active so it's fast mixer slot is never touched.
320681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // The converse, of removing an (active) track and then creating a new track
320781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // at the identical fast mixer slot within the same normal mix cycle,
320881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // is impossible because the slot isn't marked available until the end of each cycle.
320981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            int j = track->mFastIndex;
321081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOG_ASSERT(0 < j && j < (int)FastMixerState::kMaxFastTracks);
321181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOG_ASSERT(!(mFastTrackAvailMask & (1 << j)));
321281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            FastTrack *fastTrack = &state->mFastTracks[j];
321381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
321481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // Determine whether the track is currently in underrun condition,
321581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // and whether it had a recent underrun.
321681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            FastTrackDump *ftDump = &mFastMixerDumpState.mTracks[j];
321781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            FastTrackUnderruns underruns = ftDump->mUnderruns;
321881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            uint32_t recentFull = (underruns.mBitFields.mFull -
321981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    track->mObservedUnderruns.mBitFields.mFull) & UNDERRUN_MASK;
322081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            uint32_t recentPartial = (underruns.mBitFields.mPartial -
322181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    track->mObservedUnderruns.mBitFields.mPartial) & UNDERRUN_MASK;
322281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            uint32_t recentEmpty = (underruns.mBitFields.mEmpty -
322381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    track->mObservedUnderruns.mBitFields.mEmpty) & UNDERRUN_MASK;
322481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            uint32_t recentUnderruns = recentPartial + recentEmpty;
322581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            track->mObservedUnderruns = underruns;
322681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // don't count underruns that occur while stopping or pausing
322781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // or stopped which can occur when flush() is called while active
322882aaf94a5b18939e4d790bbc752031f3070704a3Glenn Kasten            if (!(track->isStopping() || track->isPausing() || track->isStopped()) &&
322982aaf94a5b18939e4d790bbc752031f3070704a3Glenn Kasten                    recentUnderruns > 0) {
323082aaf94a5b18939e4d790bbc752031f3070704a3Glenn Kasten                // FIXME fast mixer will pull & mix partial buffers, but we count as a full underrun
323182aaf94a5b18939e4d790bbc752031f3070704a3Glenn Kasten                track->mAudioTrackServerProxy->tallyUnderrunFrames(recentUnderruns * mFrameCount);
323281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
323381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
323481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // This is similar to the state machine for normal tracks,
323581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // with a few modifications for fast tracks.
323681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            bool isActive = true;
323781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            switch (track->mState) {
323881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            case TrackBase::STOPPING_1:
323981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // track stays active in STOPPING_1 state until first underrun
3240bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                if (recentUnderruns > 0 || track->isTerminated()) {
324181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    track->mState = TrackBase::STOPPING_2;
324281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
324381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                break;
324481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            case TrackBase::PAUSING:
324581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // ramp down is not yet implemented
324681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                track->setPaused();
324781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                break;
324881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            case TrackBase::RESUMING:
324981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // ramp up is not yet implemented
325081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                track->mState = TrackBase::ACTIVE;
325181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                break;
325281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            case TrackBase::ACTIVE:
325381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (recentFull > 0 || recentPartial > 0) {
325481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    // track has provided at least some frames recently: reset retry count
325581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    track->mRetryCount = kMaxTrackRetries;
325681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
325781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (recentUnderruns == 0) {
325881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    // no recent underruns: stay active
325981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    break;
326081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
326181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // there has recently been an underrun of some kind
326281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (track->sharedBuffer() == 0) {
326381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    // were any of the recent underruns "empty" (no frames available)?
326481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    if (recentEmpty == 0) {
326581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        // no, then ignore the partial underruns as they are allowed indefinitely
326681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        break;
326781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    }
326881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    // there has recently been an "empty" underrun: decrement the retry counter
326981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    if (--(track->mRetryCount) > 0) {
327081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        break;
327181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    }
327281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    // indicate to client process that the track was disabled because of underrun;
327381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    // it will then automatically call start() when data is available
327496f60d8f04432a1ed503b3e24d5736d28c63c9a2Glenn Kasten                    android_atomic_or(CBLK_DISABLED, &track->mCblk->mFlags);
327581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    // remove from active list, but state remains ACTIVE [confusing but true]
327681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    isActive = false;
327781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    break;
327881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
327981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // fall through
328081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            case TrackBase::STOPPING_2:
328181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            case TrackBase::PAUSED:
328281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            case TrackBase::STOPPED:
328381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            case TrackBase::FLUSHED:   // flush() while active
328481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // Check for presentation complete if track is inactive
328581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // We have consumed all the buffers of this track.
328681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // This would be incomplete if we auto-paused on underrun
328781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                {
328881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    size_t audioHALFrames =
328981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                            (mOutput->stream->get_latency(mOutput->stream)*mSampleRate) / 1000;
329081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    size_t framesWritten = mBytesWritten / mFrameSize;
329181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    if (!(mStandby || track->presentationComplete(framesWritten, audioHALFrames))) {
329281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        // track stays in active list until presentation is complete
329381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        break;
329481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    }
329581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
329681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (track->isStopping_2()) {
329781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    track->mState = TrackBase::STOPPED;
329881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
329981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (track->isStopped()) {
330081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    // Can't reset directly, as fast mixer is still polling this track
330181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    //   track->reset();
330281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    // So instead mark this track as needing to be reset after push with ack
330381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    resetMask |= 1 << i;
330481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
330581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                isActive = false;
330681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                break;
330781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            case TrackBase::IDLE:
330881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            default:
3309adad3d7d935da176ff24941b4ae9edf7340e9b96Glenn Kasten                LOG_ALWAYS_FATAL("unexpected track state %d", track->mState);
331081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
331181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
331281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (isActive) {
331381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // was it previously inactive?
331481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (!(state->mTrackMask & (1 << j))) {
331581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    ExtendedAudioBufferProvider *eabp = track;
331681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    VolumeProvider *vp = track;
331781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    fastTrack->mBufferProvider = eabp;
331881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    fastTrack->mVolumeProvider = vp;
331981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    fastTrack->mChannelMask = track->mChannelMask;
3320e8a1ced4da17dc6c07803dc2af8060f62a8389c1Andy Hung                    fastTrack->mFormat = track->mFormat;
332181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    fastTrack->mGeneration++;
332281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    state->mTrackMask |= 1 << j;
332381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    didModify = true;
332481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    // no acknowledgement required for newly active tracks
332581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
332681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // cache the combined master volume and stream type volume for fast mixer; this
332781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // lacks any synchronization or barrier so VolumeProvider may read a stale value
3328e4756fe3a387615acb63c6a05788c8db9b5786cbGlenn Kasten                track->mCachedVolume = masterVolume * mStreamTypes[track->streamType()].volume;
332981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ++fastTracks;
333081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            } else {
333181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // was it previously active?
333281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (state->mTrackMask & (1 << j)) {
333381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    fastTrack->mBufferProvider = NULL;
333481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    fastTrack->mGeneration++;
333581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    state->mTrackMask &= ~(1 << j);
333681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    didModify = true;
333781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    // If any fast tracks were removed, we must wait for acknowledgement
333881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    // because we're about to decrement the last sp<> on those tracks.
333981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    block = FastMixerStateQueue::BLOCK_UNTIL_ACKED;
334081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                } else {
3341adad3d7d935da176ff24941b4ae9edf7340e9b96Glenn Kasten                    LOG_ALWAYS_FATAL("fast track %d should have been active", j);
334281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
334381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                tracksToRemove->add(track);
334481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // Avoids a misleading display in dumpsys
334581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                track->mObservedUnderruns.mBitFields.mMostRecent = UNDERRUN_FULL;
334681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
334781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            continue;
334881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
334981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
335081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        {   // local variable scope to avoid goto warning
335181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
335281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        audio_track_cblk_t* cblk = track->cblk();
335381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
335481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // The first time a track is added we wait
335581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // for all its buffers to be filled before processing it
335681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        int name = track->name();
335781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // make sure that we have enough frames to mix one full buffer.
335881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // enforce this condition only once to enable draining the buffer in case the client
335981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // app does not call stop() and relies on underrun to stop:
336081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // hence the test on (mMixerStatus == MIXER_TRACKS_READY) meaning the track was mixed
336181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // during last round
33629f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        size_t desiredFrames;
33639fdcb0a9497ca290bcf364b10868587b6bde3a34Glenn Kasten        uint32_t sr = track->sampleRate();
33649fdcb0a9497ca290bcf364b10868587b6bde3a34Glenn Kasten        if (sr == mSampleRate) {
33659f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            desiredFrames = mNormalFrameCount;
33669f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        } else {
33679f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            // +1 for rounding and +1 for additional sample needed for interpolation
33689fdcb0a9497ca290bcf364b10868587b6bde3a34Glenn Kasten            desiredFrames = (mNormalFrameCount * sr) / mSampleRate + 1 + 1;
33699f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            // add frames already consumed but not yet released by the resampler
33702fc14730e4697a6f456b4631549c9981f6b0b115Glenn Kasten            // because mAudioTrackServerProxy->framesReady() will include these frames
33719f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            desiredFrames += mAudioMixer->getUnreleasedFrames(track->name());
337274935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten#if 0
33739f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            // the minimum track buffer size is normally twice the number of frames necessary
33749f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            // to fill one buffer and the resampler should not leave more than one buffer worth
33759f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            // of unreleased frames after each pass, but just in case...
33769f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            ALOG_ASSERT(desiredFrames <= cblk->frameCount_);
337774935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten#endif
33789f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        }
337981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        uint32_t minFrames = 1;
338081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if ((track->sharedBuffer() == 0) && !track->isStopped() && !track->isPausing() &&
338181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY)) {
33829f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            minFrames = desiredFrames;
33839f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        }
338413e4c960ea3db03a43e084fbd85d52aa77f7b871Eric Laurent
338513e4c960ea3db03a43e084fbd85d52aa77f7b871Eric Laurent        size_t framesReady = track->framesReady();
33869f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten        if ((framesReady >= minFrames) && track->isReady() &&
338781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                !track->isPaused() && !track->isTerminated())
338881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        {
3389f20e1d8df84c5fbeeace0052d100982ae39bb7a4Glenn Kasten            ALOGVV("track %d s=%08x [OK] on thread %p", name, cblk->mServer, this);
339081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
339181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mixedTracks++;
339281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
339369aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung            // track->mainBuffer() != mSinkBuffer or mMixerBuffer means
339469aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung            // there is an effect chain connected to the track
339581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            chain.clear();
339669aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung            if (track->mainBuffer() != mSinkBuffer &&
339769aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung                    track->mainBuffer() != mMixerBuffer) {
339898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung                if (mEffectBufferEnabled) {
339998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung                    mEffectBufferValid = true; // Later can set directly.
340098ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung                }
340181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                chain = getEffectChain_l(track->sessionId());
340281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // Delegate volume control to effect in track effect chain if needed
340381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (chain != 0) {
340481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    tracksWithEffect++;
340581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                } else {
340681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    ALOGW("prepareTracks_l(): track %d attached to effect but no chain found on "
340781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                            "session %d",
340881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                            name, track->sessionId());
340981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
341081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
341181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
341281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
341381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            int param = AudioMixer::VOLUME;
341481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (track->mFillingUpStatus == Track::FS_FILLED) {
341581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // no ramp for the first volume setting
341681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                track->mFillingUpStatus = Track::FS_ACTIVE;
341781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (track->mState == TrackBase::RESUMING) {
341881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    track->mState = TrackBase::ACTIVE;
341981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    param = AudioMixer::RAMP_VOLUME;
342081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
342181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                mAudioMixer->setParameter(name, AudioMixer::RESAMPLE, AudioMixer::RESET, NULL);
3422f20e1d8df84c5fbeeace0052d100982ae39bb7a4Glenn Kasten            // FIXME should not make a decision based on mServer
3423f20e1d8df84c5fbeeace0052d100982ae39bb7a4Glenn Kasten            } else if (cblk->mServer != 0) {
342481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // If the track is stopped before the first frame was mixed,
342581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // do not apply ramp
342681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                param = AudioMixer::RAMP_VOLUME;
342781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
342881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
342981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // compute volume for this track
34306be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung            uint32_t vl, vr;       // in U8.24 integer format
34316be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung            float vlf, vrf, vaf;   // in [0.0, 1.0] float format
3432e4756fe3a387615acb63c6a05788c8db9b5786cbGlenn Kasten            if (track->isPausing() || mStreamTypes[track->streamType()].mute) {
34336be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung                vl = vr = 0;
34346be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung                vlf = vrf = vaf = 0.;
343581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (track->isPausing()) {
343681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    track->setPaused();
343781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
343881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            } else {
343981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
344081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // read original volumes with volume control
344181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                float typeVolume = mStreamTypes[track->streamType()].volume;
344281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                float v = masterVolume * typeVolume;
34439f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten                AudioTrackServerProxy *proxy = track->mAudioTrackServerProxy;
3444c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten                gain_minifloat_packed_t vlr = proxy->getVolumeLR();
34456be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung                vlf = float_from_gain(gain_minifloat_unpack_left(vlr));
34466be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung                vrf = float_from_gain(gain_minifloat_unpack_right(vlr));
344781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // track volumes come from shared memory, so can't be trusted and must be clamped
3448c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten                if (vlf > GAIN_FLOAT_UNITY) {
3449c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten                    ALOGV("Track left volume out of range: %.3g", vlf);
3450c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten                    vlf = GAIN_FLOAT_UNITY;
345181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
3452c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten                if (vrf > GAIN_FLOAT_UNITY) {
3453c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten                    ALOGV("Track right volume out of range: %.3g", vrf);
3454c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten                    vrf = GAIN_FLOAT_UNITY;
345581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
345681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // now apply the master volume and stream type volume
34576be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung                vlf *= v;
34586be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung                vrf *= v;
345981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // assuming master volume and stream type volume each go up to 1.0,
34606be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung                // then derive vl and vr as U8.24 versions for the effect chain
34616be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung                const float scaleto8_24 = MAX_GAIN_INT * MAX_GAIN_INT;
34626be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung                vl = (uint32_t) (scaleto8_24 * vlf);
34636be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung                vr = (uint32_t) (scaleto8_24 * vrf);
34646be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung                // vl and vr are now in U8.24 format
3465e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten                uint16_t sendLevel = proxy->getSendLevel_U4_12();
346681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // send level comes from shared memory and so may be corrupt
346781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (sendLevel > MAX_GAIN_INT) {
346881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    ALOGV("Track send level out of range: %04X", sendLevel);
346981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    sendLevel = MAX_GAIN_INT;
347081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
34716be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung                // vaf is represented as [0.0, 1.0] float by rescaling sendLevel
34726be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung                vaf = v * sendLevel * (1. / MAX_GAIN_INT);
347381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
3474bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
347581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // Delegate volume control to effect in track effect chain if needed
347681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (chain != 0 && chain->setVolume_l(&vl, &vr)) {
347781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // Do not ramp volume if volume is controlled by effect
347881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                param = AudioMixer::VOLUME;
3479b6be7f22a82ee3bad8bcc709d21e72fc4727da09Bryant Liu                // Update remaining floating point volume levels
3480b6be7f22a82ee3bad8bcc709d21e72fc4727da09Bryant Liu                vlf = (float)vl / (1 << 24);
3481b6be7f22a82ee3bad8bcc709d21e72fc4727da09Bryant Liu                vrf = (float)vr / (1 << 24);
348281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                track->mHasVolumeController = true;
348381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            } else {
348481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // force no volume ramp when volume controller was just disabled or removed
348581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // from effect chain to avoid volume spike
348681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (track->mHasVolumeController) {
348781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    param = AudioMixer::VOLUME;
348881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
348981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                track->mHasVolumeController = false;
349081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
349181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
349281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // XXX: these things DON'T need to be done each time
349381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mAudioMixer->setBufferProvider(name, track);
349481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mAudioMixer->enable(name);
349581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
34966be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung            mAudioMixer->setParameter(name, param, AudioMixer::VOLUME0, &vlf);
34976be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung            mAudioMixer->setParameter(name, param, AudioMixer::VOLUME1, &vrf);
34986be494077f8d7970f3a88129c5d139c5a0c88f6dAndy Hung            mAudioMixer->setParameter(name, param, AudioMixer::AUXLEVEL, &vaf);
349981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mAudioMixer->setParameter(
350081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                name,
350181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                AudioMixer::TRACK,
350281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                AudioMixer::FORMAT, (void *)track->format());
350381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mAudioMixer->setParameter(
350481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                name,
350581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                AudioMixer::TRACK,
3506377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT                AudioMixer::CHANNEL_MASK, (void *)(uintptr_t)track->channelMask());
35079a59276fb465e492138e0576523b54079671e8f4Andy Hung            mAudioMixer->setParameter(
35089a59276fb465e492138e0576523b54079671e8f4Andy Hung                name,
35099a59276fb465e492138e0576523b54079671e8f4Andy Hung                AudioMixer::TRACK,
35109a59276fb465e492138e0576523b54079671e8f4Andy Hung                AudioMixer::MIXER_CHANNEL_MASK, (void *)(uintptr_t)mChannelMask);
3511e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten            // limit track sample rate to 2 x output sample rate, which changes at re-configuration
3512cd04484f4837b8ca0041d118286ab6a98e84fc75Andy Hung            uint32_t maxSampleRate = mSampleRate * AUDIO_RESAMPLER_DOWN_RATIO_MAX;
35139f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            uint32_t reqSampleRate = track->mAudioTrackServerProxy->getSampleRate();
3514e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten            if (reqSampleRate == 0) {
3515e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten                reqSampleRate = mSampleRate;
3516e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten            } else if (reqSampleRate > maxSampleRate) {
3517e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten                reqSampleRate = maxSampleRate;
3518e3aa659e9cee7df5c12a80d285cc29ab3b2cbb39Glenn Kasten            }
351981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mAudioMixer->setParameter(
352081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                name,
352181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                AudioMixer::RESAMPLE,
352281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                AudioMixer::SAMPLE_RATE,
3523377b2ec9a2885f9b6405b07ba900a9e3f4349c38Kévin PETIT                (void *)(uintptr_t)reqSampleRate);
352469aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung            /*
352569aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung             * Select the appropriate output buffer for the track.
352669aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung             *
352798ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung             * Tracks with effects go into their own effects chain buffer
352898ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung             * and from there into either mEffectBuffer or mSinkBuffer.
352969aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung             *
353069aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung             * Other tracks can use mMixerBuffer for higher precision
353169aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung             * channel accumulation.  If this buffer is enabled
353269aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung             * (mMixerBufferEnabled true), then selected tracks will accumulate
353369aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung             * into it.
353469aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung             *
353569aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung             */
353669aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung            if (mMixerBufferEnabled
353769aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung                    && (track->mainBuffer() == mSinkBuffer
353869aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung                            || track->mainBuffer() == mMixerBuffer)) {
353969aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung                mAudioMixer->setParameter(
354069aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung                        name,
354169aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung                        AudioMixer::TRACK,
3542788207057ed4b8df4719ed8089f376ef52de9ca1Andy Hung                        AudioMixer::MIXER_FORMAT, (void *)mMixerBufferFormat);
354369aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung                mAudioMixer->setParameter(
354469aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung                        name,
354569aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung                        AudioMixer::TRACK,
354669aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung                        AudioMixer::MAIN_BUFFER, (void *)mMixerBuffer);
354769aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung                // TODO: override track->mainBuffer()?
354869aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung                mMixerBufferValid = true;
354969aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung            } else {
355069aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung                mAudioMixer->setParameter(
355169aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung                        name,
355269aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung                        AudioMixer::TRACK,
3553788207057ed4b8df4719ed8089f376ef52de9ca1Andy Hung                        AudioMixer::MIXER_FORMAT, (void *)AUDIO_FORMAT_PCM_16_BIT);
355469aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung                mAudioMixer->setParameter(
355569aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung                        name,
355669aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung                        AudioMixer::TRACK,
355769aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung                        AudioMixer::MAIN_BUFFER, (void *)track->mainBuffer());
355869aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung            }
355981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mAudioMixer->setParameter(
356081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                name,
356181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                AudioMixer::TRACK,
356281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                AudioMixer::AUX_BUFFER, (void *)track->auxBuffer());
356381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
356481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // reset retry count
356581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            track->mRetryCount = kMaxTrackRetries;
356681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
356781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // If one track is ready, set the mixer ready if:
356881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            //  - the mixer was not ready during previous round OR
356981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            //  - no other track is not ready
357081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (mMixerStatusIgnoringFastTracks != MIXER_TRACKS_READY ||
357181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    mixerStatus != MIXER_TRACKS_ENABLED) {
357281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                mixerStatus = MIXER_TRACKS_READY;
357381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
357481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else {
35759f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            if (framesReady < desiredFrames && !track->isStopped() && !track->isPaused()) {
357682aaf94a5b18939e4d790bbc752031f3070704a3Glenn Kasten                track->mAudioTrackServerProxy->tallyUnderrunFrames(desiredFrames);
35779f80dd223d83d9bb9077fb6baee056cee4eaf7e5Glenn Kasten            }
357881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // clear effect chain input buffer if an active track underruns to avoid sending
357981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // previous audio buffer again to effects
358081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            chain = getEffectChain_l(track->sessionId());
358181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (chain != 0) {
358281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                chain->clearInputBuffer();
358381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
358481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
3585f20e1d8df84c5fbeeace0052d100982ae39bb7a4Glenn Kasten            ALOGVV("track %d s=%08x [NOT READY] on thread %p", name, cblk->mServer, this);
358681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if ((track->sharedBuffer() != 0) || track->isTerminated() ||
358781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    track->isStopped() || track->isPaused()) {
358881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // We have consumed all the buffers of this track.
358981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // Remove it from the list of active tracks.
359081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // TODO: use actual buffer filling status instead of latency when available from
359181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // audio HAL
359281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                size_t audioHALFrames = (latency_l() * mSampleRate) / 1000;
359381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                size_t framesWritten = mBytesWritten / mFrameSize;
359481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (mStandby || track->presentationComplete(framesWritten, audioHALFrames)) {
359581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    if (track->isStopped()) {
359681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        track->reset();
359781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    }
359881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    tracksToRemove->add(track);
359981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
360081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            } else {
360181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // No buffers for this track. Give it a few chances to
360281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // fill a buffer, then remove it from active list.
360381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (--(track->mRetryCount) <= 0) {
3604c9b2e20f7c9a71e07ef398152709c76079decbcdGlenn Kasten                    ALOGI("BUFFER TIMEOUT: remove(%d) from active list on thread %p", name, this);
360581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    tracksToRemove->add(track);
360681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    // indicate to client process that the track was disabled because of underrun;
360781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    // it will then automatically call start() when data is available
360896f60d8f04432a1ed503b3e24d5736d28c63c9a2Glenn Kasten                    android_atomic_or(CBLK_DISABLED, &cblk->mFlags);
360981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // If one track is not ready, mark the mixer also not ready if:
361081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                //  - the mixer was ready during previous round OR
361181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                //  - no other track is ready
361281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                } else if (mMixerStatusIgnoringFastTracks == MIXER_TRACKS_READY ||
361381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                mixerStatus != MIXER_TRACKS_READY) {
361481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    mixerStatus = MIXER_TRACKS_ENABLED;
361581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
361681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
361781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mAudioMixer->disable(name);
361881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
361981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
362081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }   // local variable scope to avoid goto warning
362181784c37c61b09289654b979567a42bf73cd2b12Eric Laurenttrack_is_ready: ;
362281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
362381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
362481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
362581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Push the new FastMixer state if necessary
362681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool pauseAudioWatchdog = false;
362781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (didModify) {
362881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        state->mFastTracksGen++;
362981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // if the fast mixer was active, but now there are no fast tracks, then put it in cold idle
363081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (kUseFastMixer == FastMixer_Dynamic &&
363181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                state->mCommand == FastMixerState::MIX_WRITE && state->mTrackMask <= 1) {
363281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            state->mCommand = FastMixerState::COLD_IDLE;
363381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            state->mColdFutexAddr = &mFastMixerFutex;
363481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            state->mColdGen++;
363581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mFastMixerFutex = 0;
363681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (kUseFastMixer == FastMixer_Dynamic) {
363781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                mNormalSink = mOutputSink;
363881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
363981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // If we go into cold idle, need to wait for acknowledgement
364081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // so that fast mixer stops doing I/O.
364181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            block = FastMixerStateQueue::BLOCK_UNTIL_ACKED;
364281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            pauseAudioWatchdog = true;
364381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
364481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
364581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (sq != NULL) {
364681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sq->end(didModify);
364781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sq->push(block);
364881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
364981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef AUDIO_WATCHDOG
365081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (pauseAudioWatchdog && mAudioWatchdog != 0) {
365181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mAudioWatchdog->pause();
365281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
365381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif
365481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
365581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Now perform the deferred reset on fast tracks that have stopped
365681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    while (resetMask != 0) {
365781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        size_t i = __builtin_ctz(resetMask);
365881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOG_ASSERT(i < count);
365981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        resetMask &= ~(1 << i);
366081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<Track> t = mActiveTracks[i].promote();
366181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (t == 0) {
366281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            continue;
366381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
366481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        Track* track = t.get();
366581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOG_ASSERT(track->isFastTrack() && track->isStopped());
366681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        track->reset();
366781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
366881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
366981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // remove all the tracks that need to be...
3670bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    removeTracks_l(*tracksToRemove);
367181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
367297d547da43c9c41711d1ed1e3f4fa87c2ee3cb9aEric Laurent    if (getEffectChain_l(AUDIO_SESSION_OUTPUT_MIX) != 0) {
367397d547da43c9c41711d1ed1e3f4fa87c2ee3cb9aEric Laurent        mEffectBufferValid = true;
3674ac302143551a8b964f026385a524dda9ff8ea5baMarco Nelissen    }
3675ac302143551a8b964f026385a524dda9ff8ea5baMarco Nelissen
3676ac302143551a8b964f026385a524dda9ff8ea5baMarco Nelissen    if (mEffectBufferValid) {
367757088b5c8e76855b99b3e6b3e410de5b6382670eMarco Nelissen        // as long as there are effects we should clear the effects buffer, to avoid
367857088b5c8e76855b99b3e6b3e410de5b6382670eMarco Nelissen        // passing a non-clean buffer to the effect chain
367957088b5c8e76855b99b3e6b3e410de5b6382670eMarco Nelissen        memset(mEffectBuffer, 0, mEffectBufferSize);
368097d547da43c9c41711d1ed1e3f4fa87c2ee3cb9aEric Laurent    }
368169aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // sink or mix buffer must be cleared if all tracks are connected to an
368269aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // effect chain as in this case the mixer will not write to the sink or mix buffer
368369aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung    // and track effects will accumulate into it
3684bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    if ((mBytesRemaining == 0) && ((mixedTracks != 0 && mixedTracks == tracksWithEffect) ||
3685bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            (mixedTracks == 0 && fastTracks > 0))) {
368681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // FIXME as a performance optimization, should remember previous zero status
368769aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung        if (mMixerBufferValid) {
368869aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung            memset(mMixerBuffer, 0, mMixerBufferSize);
368969aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung            // TODO: In testing, mSinkBuffer below need not be cleared because
369069aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung            // the PlaybackThread::threadLoop() copies mMixerBuffer into mSinkBuffer
369169aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung            // after mixing.
369269aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung            //
369369aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung            // To enforce this guarantee:
369469aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung            // ((mixedTracks != 0 && mixedTracks == tracksWithEffect) ||
369569aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung            // (mixedTracks == 0 && fastTracks > 0))
369669aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung            // must imply MIXER_TRACKS_READY.
369769aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung            // Later, we may clear buffers regardless, and skip much of this logic.
369869aed5f0f4a3be3996d1e78a0473e1a72c1547daAndy Hung        }
369998ef978df4e928f486d244c4d7f7ad9f13111e98Andy Hung        // FIXME as a performance optimization, should remember previous zero status
37005567aaf4818007cd8e77329683a91c0f5d7a8837Andy Hung        memset(mSinkBuffer, 0, mNormalFrameCount * mFrameSize);
370181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
370281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
370381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // if any fast tracks, then status is ready
370481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mMixerStatusIgnoringFastTracks = mixerStatus;
370581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (fastTracks > 0) {
370681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mixerStatus = MIXER_TRACKS_READY;
370781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
370881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return mixerStatus;
370981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
371081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
371181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// getTrackName_l() must be called with ThreadBase::mLock held
3712e8a1ced4da17dc6c07803dc2af8060f62a8389c1Andy Hungint AudioFlinger::MixerThread::getTrackName_l(audio_channel_mask_t channelMask,
3713e8a1ced4da17dc6c07803dc2af8060f62a8389c1Andy Hung        audio_format_t format, int sessionId)
371481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
3715e8a1ced4da17dc6c07803dc2af8060f62a8389c1Andy Hung    return mAudioMixer->getTrackName(channelMask, format, sessionId);
371681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
371781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
371881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// deleteTrackName_l() must be called with ThreadBase::mLock held
371981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::MixerThread::deleteTrackName_l(int name)
372081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
372181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("remove track (%d) and delete from mixer", name);
372281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mAudioMixer->deleteTrackName(name);
372381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
372481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
37251035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent// checkForNewParameter_l() must be called with ThreadBase::mLock held
37261035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurentbool AudioFlinger::MixerThread::checkForNewParameter_l(const String8& keyValuePair,
37271035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                                                       status_t& status)
372881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
372981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool reconfig = false;
373081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
37311035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    status = NO_ERROR;
373281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
37331035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    // if !&IDLE, holds the FastMixer state to restore after new parameters processed
37341035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    FastMixerState::Command previousCommand = FastMixerState::HOT_IDLE;
37354d23ca370dd0ce584f49a80ef9dfcdbb75ba2c8eGlenn Kasten    if (mFastMixer != 0) {
37361035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        FastMixerStateQueue *sq = mFastMixer->sq();
37371035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        FastMixerState *state = sq->begin();
37381035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        if (!(state->mCommand & FastMixerState::IDLE)) {
37391035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            previousCommand = state->mCommand;
37401035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            state->mCommand = FastMixerState::HOT_IDLE;
37411035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            sq->end();
37421035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            sq->push(FastMixerStateQueue::BLOCK_UNTIL_ACKED);
37431035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        } else {
37441035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            sq->end(false /*didModify*/);
374581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
37461035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    }
374781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
37481035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    AudioParameter param = AudioParameter(keyValuePair);
37491035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    int value;
37501035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) {
37511035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        reconfig = true;
37521035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    }
37531035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) {
37549a59276fb465e492138e0576523b54079671e8f4Andy Hung        if (!isValidPcmSinkFormat((audio_format_t) value)) {
37551035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            status = BAD_VALUE;
37561035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        } else {
37571035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            // no need to save value, since it's constant
375881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            reconfig = true;
375981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
37601035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    }
37611035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) {
37629a59276fb465e492138e0576523b54079671e8f4Andy Hung        if (!isValidPcmSinkChannelMask((audio_channel_mask_t) value)) {
37631035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            status = BAD_VALUE;
37641035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        } else {
37651035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            // no need to save value, since it's constant
37661035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            reconfig = true;
376781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
37681035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    }
37691035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) {
37701035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        // do not accept frame count changes if tracks are open as the track buffer
37711035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        // size depends on frame count and correct behavior would not be guaranteed
37721035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        // if frame count is changed after track creation
37731035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        if (!mTracks.isEmpty()) {
37741035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            status = INVALID_OPERATION;
37751035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        } else {
37761035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            reconfig = true;
377781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
37781035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    }
37791035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) {
378081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef ADD_BATTERY_DATA
37811035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        // when changing the audio output device, call addBatteryData to notify
37821035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        // the change
37831035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        if (mOutDevice != value) {
37841035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            uint32_t params = 0;
37851035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            // check whether speaker is on
37861035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            if (value & AUDIO_DEVICE_OUT_SPEAKER) {
37871035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                params |= IMediaPlayerService::kBatteryDataSpeakerOn;
37881035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            }
378981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
37901035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            audio_devices_t deviceWithoutSpeaker
37911035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                = AUDIO_DEVICE_OUT_ALL & ~AUDIO_DEVICE_OUT_SPEAKER;
37921035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            // check if any other device (except speaker) is on
37931035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            if (value & deviceWithoutSpeaker ) {
37941035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                params |= IMediaPlayerService::kBatteryDataOtherAudioDeviceOn;
37951035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            }
379681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
37971035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            if (params != 0) {
37981035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                addBatteryData(params);
379981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
38001035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        }
380181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif
380281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
38031035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        // forward device change to effects that have requested to be
38041035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        // aware of attached audio device.
38051035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        if (value != AUDIO_DEVICE_NONE) {
38061035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            mOutDevice = value;
38071035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            for (size_t i = 0; i < mEffectChains.size(); i++) {
38081035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                mEffectChains[i]->setDevice_l(mOutDevice);
380981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
381081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
38111035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    }
381281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
38131035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    if (status == NO_ERROR) {
38141035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        status = mOutput->stream->common.set_parameters(&mOutput->stream->common,
38151035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                                                keyValuePair.string());
38161035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        if (!mStandby && status == INVALID_OPERATION) {
38171035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            mOutput->stream->common.standby(&mOutput->stream->common);
38181035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            mStandby = true;
38191035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            mBytesWritten = 0;
382081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            status = mOutput->stream->common.set_parameters(&mOutput->stream->common,
38211035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                                                   keyValuePair.string());
38221035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        }
38231035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        if (status == NO_ERROR && reconfig) {
38241035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            readOutputParameters_l();
38251035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            delete mAudioMixer;
38261035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            mAudioMixer = new AudioMixer(mNormalFrameCount, mSampleRate);
38271035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            for (size_t i = 0; i < mTracks.size() ; i++) {
3828e8a1ced4da17dc6c07803dc2af8060f62a8389c1Andy Hung                int name = getTrackName_l(mTracks[i]->mChannelMask,
3829e8a1ced4da17dc6c07803dc2af8060f62a8389c1Andy Hung                        mTracks[i]->mFormat, mTracks[i]->mSessionId);
38301035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                if (name < 0) {
38311035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                    break;
383281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
38331035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                mTracks[i]->mName = name;
383481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
38351035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            sendIoConfigEvent_l(AudioSystem::OUTPUT_CONFIG_CHANGED);
383681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
383781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
383881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
383981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (!(previousCommand & FastMixerState::IDLE)) {
38404d23ca370dd0ce584f49a80ef9dfcdbb75ba2c8eGlenn Kasten        ALOG_ASSERT(mFastMixer != 0);
384181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        FastMixerStateQueue *sq = mFastMixer->sq();
384281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        FastMixerState *state = sq->begin();
384381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOG_ASSERT(state->mCommand == FastMixerState::HOT_IDLE);
384481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        state->mCommand = previousCommand;
384581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sq->end();
384681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sq->push(FastMixerStateQueue::BLOCK_UNTIL_PUSHED);
384781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
384881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
384981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return reconfig;
385081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
385181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
385281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
385381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::MixerThread::dumpInternals(int fd, const Vector<String16>& args)
385481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
385581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    const size_t SIZE = 256;
385681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    char buffer[SIZE];
385781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    String8 result;
385881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
385981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread::dumpInternals(fd, args);
386081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
386187cebadd48710e42474756fc3513df678de045ceElliott Hughes    dprintf(fd, "  AudioMixer tracks: 0x%08x\n", mAudioMixer->trackNames());
386281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
386381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Make a non-atomic copy of fast mixer dump state so it won't change underneath us
38644182c4e2a07e2441fcd5c22eaff0ddfe7f826f61Glenn Kasten    const FastMixerDumpState copy(mFastMixerDumpState);
386581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    copy.dump(fd);
386681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
386781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef STATE_QUEUE_DUMP
386881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Similar for state queue
386981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    StateQueueObserverDump observerCopy = mStateQueueObserverDump;
387081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    observerCopy.dump(fd);
387181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    StateQueueMutatorDump mutatorCopy = mStateQueueMutatorDump;
387281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mutatorCopy.dump(fd);
387381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif
387481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
387546909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK
387681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Write the tee output to a .wav file
387781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    dumpTee(fd, mTeeSource, mId);
387846909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif
387981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
388081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#ifdef AUDIO_WATCHDOG
388181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (mAudioWatchdog != 0) {
388281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // Make a non-atomic copy of audio watchdog dump so it won't change underneath us
388381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        AudioWatchdogDump wdCopy = mAudioWatchdogDump;
388481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        wdCopy.dump(fd);
388581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
388681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif
388781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
388881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
388981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::MixerThread::idleSleepTimeUs() const
389081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
389181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000) / 2;
389281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
389381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
389481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::MixerThread::suspendSleepTimeUs() const
389581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
389681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return (uint32_t)(((mNormalFrameCount * 1000) / mSampleRate) * 1000);
389781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
389881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
389981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::MixerThread::cacheParameters_l()
390081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
390181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread::cacheParameters_l();
390281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
390381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // FIXME: Relaxed timing because of a certain device that can't meet latency
390481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // Should be reduced to 2x after the vendor fixes the driver issue
390581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // increase threshold again due to low power audio mode. The way this warning
390681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // threshold is calculated and its usefulness should be reconsidered anyway.
390781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    maxPeriod = seconds(mNormalFrameCount) / mSampleRate * 15;
390881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
390981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
391081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
391181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
391281784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger,
391381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        AudioStreamOut* output, audio_io_handle_t id, audio_devices_t device)
391481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    :   PlaybackThread(audioFlinger, output, id, device, DIRECT)
391581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // mLeftVolFloat, mRightVolFloat
391681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
391781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
391881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
3919bfb1b832079bbb9426f72f3863199a54aefd02daEric LaurentAudioFlinger::DirectOutputThread::DirectOutputThread(const sp<AudioFlinger>& audioFlinger,
3920bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        AudioStreamOut* output, audio_io_handle_t id, uint32_t device,
3921bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        ThreadBase::type_t type)
3922bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    :   PlaybackThread(audioFlinger, output, id, device, type)
3923bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        // mLeftVolFloat, mRightVolFloat
3924bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{
3925bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}
3926bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
392781784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::DirectOutputThread::~DirectOutputThread()
392881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
392981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
393081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
3931bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioFlinger::DirectOutputThread::processVolume_l(Track *track, bool lastTrack)
3932bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{
3933bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    audio_track_cblk_t* cblk = track->cblk();
3934bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    float left, right;
3935bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
3936bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    if (mMasterMute || mStreamTypes[track->streamType()].mute) {
3937bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        left = right = 0;
3938bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    } else {
3939bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        float typeVolume = mStreamTypes[track->streamType()].volume;
3940bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        float v = mMasterVolume * typeVolume;
3941bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        AudioTrackServerProxy *proxy = track->mAudioTrackServerProxy;
3942c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten        gain_minifloat_packed_t vlr = proxy->getVolumeLR();
3943c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten        left = float_from_gain(gain_minifloat_unpack_left(vlr));
3944c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten        if (left > GAIN_FLOAT_UNITY) {
3945c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten            left = GAIN_FLOAT_UNITY;
3946c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten        }
3947c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten        left *= v;
3948c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten        right = float_from_gain(gain_minifloat_unpack_right(vlr));
3949c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten        if (right > GAIN_FLOAT_UNITY) {
3950c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten            right = GAIN_FLOAT_UNITY;
3951c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten        }
3952c56f3426099a3cf2d07ccff8886050c7fbce140fGlenn Kasten        right *= v;
3953bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    }
3954bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
3955bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    if (lastTrack) {
3956bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        if (left != mLeftVolFloat || right != mRightVolFloat) {
3957bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            mLeftVolFloat = left;
3958bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            mRightVolFloat = right;
3959bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
3960bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            // Convert volumes from float to 8.24
3961bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            uint32_t vl = (uint32_t)(left * (1 << 24));
3962bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            uint32_t vr = (uint32_t)(right * (1 << 24));
3963bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
3964bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            // Delegate volume control to effect in track effect chain if needed
3965bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            // only one effect chain can be present on DirectOutputThread, so if
3966bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            // there is one, the track is connected to it
3967bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            if (!mEffectChains.isEmpty()) {
3968bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                mEffectChains[0]->setVolume_l(&vl, &vr);
3969bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                left = (float)vl / (1 << 24);
3970bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                right = (float)vr / (1 << 24);
3971bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            }
3972bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            if (mOutput->stream->set_volume) {
3973bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                mOutput->stream->set_volume(mOutput->stream, left, right);
3974bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            }
3975bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        }
3976bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    }
3977bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}
3978bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
3979bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
398081784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::PlaybackThread::mixer_state AudioFlinger::DirectOutputThread::prepareTracks_l(
398181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Vector< sp<Track> > *tracksToRemove
398281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent)
398381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
3984d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent    size_t count = mActiveTracks.size();
398581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mixer_state mixerStatus = MIXER_IDLE;
398681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
398781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // find out which tracks need to be processed
3988d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent    for (size_t i = 0; i < count; i++) {
3989d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent        sp<Track> t = mActiveTracks[i].promote();
399081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // The track died recently
399181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (t == 0) {
3992d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent            continue;
399381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
399481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
399581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        Track* const track = t.get();
399681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        audio_track_cblk_t* cblk = track->cblk();
3997fd4779740ec3e9e865d5514464df26d015354388Eric Laurent        // Only consider last track started for volume and mixer state control.
3998fd4779740ec3e9e865d5514464df26d015354388Eric Laurent        // In theory an older track could underrun and restart after the new one starts
3999fd4779740ec3e9e865d5514464df26d015354388Eric Laurent        // but as we only care about the transition phase between two tracks on a
4000fd4779740ec3e9e865d5514464df26d015354388Eric Laurent        // direct output, it is not a problem to ignore the underrun case.
4001fd4779740ec3e9e865d5514464df26d015354388Eric Laurent        sp<Track> l = mLatestActiveTrack.promote();
4002fd4779740ec3e9e865d5514464df26d015354388Eric Laurent        bool last = l.get() == track;
400381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
400481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // The first time a track is added we wait
400581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // for all its buffers to be filled before processing it
400681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        uint32_t minFrames;
4007ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent        if ((track->sharedBuffer() == 0) && !track->isStopping_1() && !track->isPausing()) {
400881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            minFrames = mNormalFrameCount;
400981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else {
401081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            minFrames = 1;
401181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
4012bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
4013ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent        if ((track->framesReady() >= minFrames) && track->isReady() && !track->isPaused() &&
4014ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent                !track->isStopping_2() && !track->isStopped())
401581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        {
4016f20e1d8df84c5fbeeace0052d100982ae39bb7a4Glenn Kasten            ALOGVV("track %d s=%08x [OK]", track->name(), cblk->mServer);
401781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
401881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (track->mFillingUpStatus == Track::FS_FILLED) {
401981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                track->mFillingUpStatus = Track::FS_ACTIVE;
40201abbdb4429479975718421c4fef3f79ce7c820e3Eric Laurent                // make sure processVolume_l() will apply new volume even if 0
40211abbdb4429479975718421c4fef3f79ce7c820e3Eric Laurent                mLeftVolFloat = mRightVolFloat = -1.0;
402281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (track->mState == TrackBase::RESUMING) {
402381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    track->mState = TrackBase::ACTIVE;
402481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
402581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
402681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
402781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // compute volume for this track
4028bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            processVolume_l(track, last);
4029bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            if (last) {
4030d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent                // reset retry count
4031d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent                track->mRetryCount = kMaxTrackRetriesDirect;
4032d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent                mActiveTrack = t;
4033d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent                mixerStatus = MIXER_TRACKS_READY;
4034d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent            }
403581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else {
4036d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent            // clear effect chain input buffer if the last active track started underruns
4037d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent            // to avoid sending previous audio buffer again to effects
4038fd4779740ec3e9e865d5514464df26d015354388Eric Laurent            if (!mEffectChains.isEmpty() && last) {
403981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                mEffectChains[0]->clearInputBuffer();
404081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
4041ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent            if (track->isStopping_1()) {
4042ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent                track->mState = TrackBase::STOPPING_2;
4043ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent            }
4044ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent            if ((track->sharedBuffer() != 0) || track->isStopped() ||
4045ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent                    track->isStopping_2() || track->isPaused()) {
404681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // We have consumed all the buffers of this track.
404781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // Remove it from the list of active tracks.
4048ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent                size_t audioHALFrames;
4049ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent                if (audio_is_linear_pcm(mFormat)) {
4050ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent                    audioHALFrames = (latency_l() * mSampleRate) / 1000;
4051ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent                } else {
4052ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent                    audioHALFrames = 0;
4053ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent                }
4054ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent
405581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                size_t framesWritten = mBytesWritten / mFrameSize;
4056fd4779740ec3e9e865d5514464df26d015354388Eric Laurent                if (mStandby || !last ||
4057fd4779740ec3e9e865d5514464df26d015354388Eric Laurent                        track->presentationComplete(framesWritten, audioHALFrames)) {
4058ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent                    if (track->isStopping_2()) {
4059ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent                        track->mState = TrackBase::STOPPED;
4060ab5cdbaf65ca509681d2726aacdf3ac8bfb6b3faEric Laurent                    }
406181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    if (track->isStopped()) {
4062e659ef420dae0caae84ab78f9df8952acb9ad3a0Eric Laurent                        if (track->mState == TrackBase::FLUSHED) {
4063e659ef420dae0caae84ab78f9df8952acb9ad3a0Eric Laurent                            flushHw_l();
4064e659ef420dae0caae84ab78f9df8952acb9ad3a0Eric Laurent                        }
406581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        track->reset();
406681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    }
4067d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent                    tracksToRemove->add(track);
406881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
406981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            } else {
407081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // No buffers for this track. Give it a few chances to
407181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // fill a buffer, then remove it from active list.
4072d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent                // Only consider last track started for mixer state control
407381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                if (--(track->mRetryCount) <= 0) {
407481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    ALOGV("BUFFER TIMEOUT: remove(%d) from active list", track->name());
4075d595b7c858c481a07745674ce2d8a6690e980e74Eric Laurent                    tracksToRemove->add(track);
4076a23f17ac334ff20a11ee63dd177cb1080e44c483Eric Laurent                    // indicate to client process that the track was disabled because of underrun;
4077a23f17ac334ff20a11ee63dd177cb1080e44c483Eric Laurent                    // it will then automatically call start() when data is available
4078a23f17ac334ff20a11ee63dd177cb1080e44c483Eric Laurent                    android_atomic_or(CBLK_DISABLED, &cblk->mFlags);
4079bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                } else if (last) {
408081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    mixerStatus = MIXER_TRACKS_ENABLED;
408181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
408281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
408381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
408481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
408581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
408681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // remove all the tracks that need to be...
4087bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    removeTracks_l(*tracksToRemove);
408881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
408981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return mixerStatus;
409081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
409181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
409281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DirectOutputThread::threadLoop_mix()
409381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
409481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    size_t frameCount = mFrameCount;
40952098f2744cedf2dc3fa36f608aa965a34602e7c0Andy Hung    int8_t *curBuf = (int8_t *)mSinkBuffer;
409681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // output audio to hardware
409781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    while (frameCount) {
409834542acfa25c6413c87a94b6f7cc315a0c496277Glenn Kasten        AudioBufferProvider::Buffer buffer;
409981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        buffer.frameCount = frameCount;
410081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mActiveTrack->getNextBuffer(&buffer);
4101fa319e6d918b84f93fb5457af5d1cca6421ac517Glenn Kasten        if (buffer.raw == NULL) {
410281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            memset(curBuf, 0, frameCount * mFrameSize);
410381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            break;
410481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
410581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        memcpy(curBuf, buffer.raw, buffer.frameCount * mFrameSize);
410681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        frameCount -= buffer.frameCount;
410781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        curBuf += buffer.frameCount * mFrameSize;
410881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mActiveTrack->releaseBuffer(&buffer);
410981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
41102098f2744cedf2dc3fa36f608aa965a34602e7c0Andy Hung    mCurrentWriteLength = curBuf - (int8_t *)mSinkBuffer;
411181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sleepTime = 0;
411281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    standbyTime = systemTime() + standbyDelay;
411381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mActiveTrack.clear();
411481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
411581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
411681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DirectOutputThread::threadLoop_sleepTime()
411781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
411881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (sleepTime == 0) {
411981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (mMixerStatus == MIXER_TRACKS_ENABLED) {
412081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sleepTime = activeSleepTime;
412181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else {
412281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sleepTime = idleSleepTime;
412381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
412481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    } else if (mBytesWritten != 0 && audio_is_linear_pcm(mFormat)) {
41252098f2744cedf2dc3fa36f608aa965a34602e7c0Andy Hung        memset(mSinkBuffer, 0, mFrameCount * mFrameSize);
412681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sleepTime = 0;
412781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
412881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
412981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
413081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// getTrackName_l() must be called with ThreadBase::mLock held
41310f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenint AudioFlinger::DirectOutputThread::getTrackName_l(audio_channel_mask_t channelMask __unused,
4132e8a1ced4da17dc6c07803dc2af8060f62a8389c1Andy Hung        audio_format_t format __unused, int sessionId __unused)
413381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
413481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return 0;
413581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
413681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
413781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// deleteTrackName_l() must be called with ThreadBase::mLock held
41380f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid AudioFlinger::DirectOutputThread::deleteTrackName_l(int name __unused)
413981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
414081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
414181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
41421035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent// checkForNewParameter_l() must be called with ThreadBase::mLock held
41431035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurentbool AudioFlinger::DirectOutputThread::checkForNewParameter_l(const String8& keyValuePair,
41441035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                                                              status_t& status)
414581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
414681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool reconfig = false;
414781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
41481035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    status = NO_ERROR;
41491035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent
41501035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    AudioParameter param = AudioParameter(keyValuePair);
41511035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    int value;
41521035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) {
41531035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        // forward device change to effects that have requested to be
41541035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        // aware of attached audio device.
41551035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        if (value != AUDIO_DEVICE_NONE) {
41561035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            mOutDevice = value;
41571035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            for (size_t i = 0; i < mEffectChains.size(); i++) {
41581035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                mEffectChains[i]->setDevice_l(mOutDevice);
4159c125f38cd0ae35409a01b98a99e483550daa1313Glenn Kasten            }
4160c125f38cd0ae35409a01b98a99e483550daa1313Glenn Kasten        }
41611035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    }
41621035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) {
41631035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        // do not accept frame count changes if tracks are open as the track buffer
41641035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        // size depends on frame count and correct behavior would not be garantied
41651035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        // if frame count is changed after track creation
41661035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        if (!mTracks.isEmpty()) {
41671035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            status = INVALID_OPERATION;
41681035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        } else {
41691035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            reconfig = true;
417081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
41711035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    }
41721035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    if (status == NO_ERROR) {
41731035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        status = mOutput->stream->common.set_parameters(&mOutput->stream->common,
41741035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                                                keyValuePair.string());
41751035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        if (!mStandby && status == INVALID_OPERATION) {
41761035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            mOutput->stream->common.standby(&mOutput->stream->common);
41771035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            mStandby = true;
41781035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            mBytesWritten = 0;
417981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            status = mOutput->stream->common.set_parameters(&mOutput->stream->common,
41801035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                                                   keyValuePair.string());
41811035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        }
41821035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        if (status == NO_ERROR && reconfig) {
41831035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            readOutputParameters_l();
41841035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            sendIoConfigEvent_l(AudioSystem::OUTPUT_CONFIG_CHANGED);
418581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
418681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
41871035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent
418881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return reconfig;
418981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
419081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
419181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::DirectOutputThread::activeSleepTimeUs() const
419281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
419381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t time;
419481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (audio_is_linear_pcm(mFormat)) {
419581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        time = PlaybackThread::activeSleepTimeUs();
419681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    } else {
419781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        time = 10000;
419881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
419981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return time;
420081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
420181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
420281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::DirectOutputThread::idleSleepTimeUs() const
420381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
420481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t time;
420581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (audio_is_linear_pcm(mFormat)) {
420681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000) / 2;
420781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    } else {
420881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        time = 10000;
420981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
421081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return time;
421181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
421281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
421381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::DirectOutputThread::suspendSleepTimeUs() const
421481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
421581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t time;
421681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (audio_is_linear_pcm(mFormat)) {
421781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        time = (uint32_t)(((mFrameCount * 1000) / mSampleRate) * 1000);
421881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    } else {
421981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        time = 10000;
422081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
422181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return time;
422281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
422381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
422481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DirectOutputThread::cacheParameters_l()
422581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
422681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    PlaybackThread::cacheParameters_l();
422781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
422881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // use shorter standby delay as on normal output to release
422981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // hardware resources as soon as possible
4230972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent    if (audio_is_linear_pcm(mFormat)) {
4231972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent        standbyDelay = microseconds(activeSleepTime*2);
4232972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent    } else {
4233972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent        standbyDelay = kOffloadStandbyDelayNs;
4234972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent    }
423581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
423681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
4237e659ef420dae0caae84ab78f9df8952acb9ad3a0Eric Laurentvoid AudioFlinger::DirectOutputThread::flushHw_l()
4238e659ef420dae0caae84ab78f9df8952acb9ad3a0Eric Laurent{
4239e659ef420dae0caae84ab78f9df8952acb9ad3a0Eric Laurent    if (mOutput->stream->flush != NULL)
4240e659ef420dae0caae84ab78f9df8952acb9ad3a0Eric Laurent        mOutput->stream->flush(mOutput->stream);
4241e659ef420dae0caae84ab78f9df8952acb9ad3a0Eric Laurent}
4242e659ef420dae0caae84ab78f9df8952acb9ad3a0Eric Laurent
424381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
424481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
4245bfb1b832079bbb9426f72f3863199a54aefd02daEric LaurentAudioFlinger::AsyncCallbackThread::AsyncCallbackThread(
42464de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent        const wp<AudioFlinger::PlaybackThread>& playbackThread)
4247bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    :   Thread(false /*canCallJava*/),
42484de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent        mPlaybackThread(playbackThread),
42493b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent        mWriteAckSequence(0),
42503b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent        mDrainSequence(0)
4251bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{
4252bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}
4253bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
4254bfb1b832079bbb9426f72f3863199a54aefd02daEric LaurentAudioFlinger::AsyncCallbackThread::~AsyncCallbackThread()
4255bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{
4256bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}
4257bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
4258bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioFlinger::AsyncCallbackThread::onFirstRef()
4259bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{
4260bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    run("Offload Cbk", ANDROID_PRIORITY_URGENT_AUDIO);
4261bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}
4262bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
4263bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentbool AudioFlinger::AsyncCallbackThread::threadLoop()
4264bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{
4265bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    while (!exitPending()) {
42663b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent        uint32_t writeAckSequence;
42673b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent        uint32_t drainSequence;
4268bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
4269bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        {
4270bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            Mutex::Autolock _l(mLock);
427124a325d6f8c4bbf9330e6ce0c769d46e04266ffcHaynes Mathew George            while (!((mWriteAckSequence & 1) ||
427224a325d6f8c4bbf9330e6ce0c769d46e04266ffcHaynes Mathew George                     (mDrainSequence & 1) ||
427324a325d6f8c4bbf9330e6ce0c769d46e04266ffcHaynes Mathew George                     exitPending())) {
427424a325d6f8c4bbf9330e6ce0c769d46e04266ffcHaynes Mathew George                mWaitWorkCV.wait(mLock);
427524a325d6f8c4bbf9330e6ce0c769d46e04266ffcHaynes Mathew George            }
427624a325d6f8c4bbf9330e6ce0c769d46e04266ffcHaynes Mathew George
4277bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            if (exitPending()) {
4278bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                break;
4279bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            }
42803b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent            ALOGV("AsyncCallbackThread mWriteAckSequence %d mDrainSequence %d",
42813b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent                  mWriteAckSequence, mDrainSequence);
42823b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent            writeAckSequence = mWriteAckSequence;
42833b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent            mWriteAckSequence &= ~1;
42843b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent            drainSequence = mDrainSequence;
42853b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent            mDrainSequence &= ~1;
4286bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        }
4287bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        {
42884de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent            sp<AudioFlinger::PlaybackThread> playbackThread = mPlaybackThread.promote();
42894de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent            if (playbackThread != 0) {
42903b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent                if (writeAckSequence & 1) {
42914de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent                    playbackThread->resetWriteBlocked(writeAckSequence >> 1);
4292bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                }
42933b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent                if (drainSequence & 1) {
42944de95592980dba88a35b3dc8f3fd045588387a4fEric Laurent                    playbackThread->resetDraining(drainSequence >> 1);
4295bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                }
4296bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            }
4297bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        }
4298bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    }
4299bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    return false;
4300bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}
4301bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
4302bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioFlinger::AsyncCallbackThread::exit()
4303bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{
4304bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    ALOGV("AsyncCallbackThread::exit");
4305bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    Mutex::Autolock _l(mLock);
4306bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    requestExit();
4307bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    mWaitWorkCV.broadcast();
4308bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}
4309bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
43103b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurentvoid AudioFlinger::AsyncCallbackThread::setWriteBlocked(uint32_t sequence)
4311bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{
4312bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    Mutex::Autolock _l(mLock);
43133b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // bit 0 is cleared
43143b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    mWriteAckSequence = sequence << 1;
43153b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent}
43163b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent
43173b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurentvoid AudioFlinger::AsyncCallbackThread::resetWriteBlocked()
43183b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent{
43193b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    Mutex::Autolock _l(mLock);
43203b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // ignore unexpected callbacks
43213b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    if (mWriteAckSequence & 2) {
43223b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent        mWriteAckSequence |= 1;
4323bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        mWaitWorkCV.signal();
4324bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    }
4325bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}
4326bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
43273b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurentvoid AudioFlinger::AsyncCallbackThread::setDraining(uint32_t sequence)
43283b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent{
43293b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    Mutex::Autolock _l(mLock);
43303b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // bit 0 is cleared
43313b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    mDrainSequence = sequence << 1;
43323b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent}
43333b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent
43343b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurentvoid AudioFlinger::AsyncCallbackThread::resetDraining()
4335bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{
4336bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    Mutex::Autolock _l(mLock);
43373b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    // ignore unexpected callbacks
43383b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    if (mDrainSequence & 2) {
43393b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent        mDrainSequence |= 1;
4340bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        mWaitWorkCV.signal();
4341bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    }
4342bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}
4343bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
4344bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
4345bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent// ----------------------------------------------------------------------------
4346bfb1b832079bbb9426f72f3863199a54aefd02daEric LaurentAudioFlinger::OffloadThread::OffloadThread(const sp<AudioFlinger>& audioFlinger,
4347bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        AudioStreamOut* output, audio_io_handle_t id, uint32_t device)
4348bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    :   DirectOutputThread(audioFlinger, output, id, device, OFFLOAD),
4349bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        mHwPaused(false),
4350ea0fadeb5d81ef3cb7f9db458c9033d628bdb86aEric Laurent        mFlushPending(false),
4351d7e59228caad3867794d847f6bf163c6495e9506Eric Laurent        mPausedBytesRemaining(0)
4352bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{
4353fd4779740ec3e9e865d5514464df26d015354388Eric Laurent    //FIXME: mStandby should be set to true by ThreadBase constructor
4354fd4779740ec3e9e865d5514464df26d015354388Eric Laurent    mStandby = true;
4355bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}
4356bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
4357bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioFlinger::OffloadThread::threadLoop_exit()
4358bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{
4359bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    if (mFlushPending || mHwPaused) {
4360bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        // If a flush is pending or track was paused, just discard buffered data
4361bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        flushHw_l();
4362bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    } else {
4363bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        mMixerStatus = MIXER_DRAIN_ALL;
4364bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        threadLoop_drain();
4365bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    }
436656604aa3a56dc8e15532597a0a74b3c7b165e006Uday Gupta    if (mUseAsyncWrite) {
436756604aa3a56dc8e15532597a0a74b3c7b165e006Uday Gupta        ALOG_ASSERT(mCallbackThread != 0);
436856604aa3a56dc8e15532597a0a74b3c7b165e006Uday Gupta        mCallbackThread->exit();
436956604aa3a56dc8e15532597a0a74b3c7b165e006Uday Gupta    }
4370bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    PlaybackThread::threadLoop_exit();
4371bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}
4372bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
4373bfb1b832079bbb9426f72f3863199a54aefd02daEric LaurentAudioFlinger::PlaybackThread::mixer_state AudioFlinger::OffloadThread::prepareTracks_l(
4374bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    Vector< sp<Track> > *tracksToRemove
4375bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent)
4376bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{
4377bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    size_t count = mActiveTracks.size();
4378bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
4379bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    mixer_state mixerStatus = MIXER_IDLE;
4380972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent    bool doHwPause = false;
4381972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent    bool doHwResume = false;
4382972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent
4383ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent    ALOGV("OffloadThread::prepareTracks_l active tracks %d", count);
4384ede6c3b8b1147bc425f7b923882f559a513fe23bEric Laurent
4385bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    // find out which tracks need to be processed
4386bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    for (size_t i = 0; i < count; i++) {
4387bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        sp<Track> t = mActiveTracks[i].promote();
4388bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        // The track died recently
4389bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        if (t == 0) {
4390bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            continue;
4391bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        }
4392bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        Track* const track = t.get();
4393bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        audio_track_cblk_t* cblk = track->cblk();
4394fd4779740ec3e9e865d5514464df26d015354388Eric Laurent        // Only consider last track started for volume and mixer state control.
4395fd4779740ec3e9e865d5514464df26d015354388Eric Laurent        // In theory an older track could underrun and restart after the new one starts
4396fd4779740ec3e9e865d5514464df26d015354388Eric Laurent        // but as we only care about the transition phase between two tracks on a
4397fd4779740ec3e9e865d5514464df26d015354388Eric Laurent        // direct output, it is not a problem to ignore the underrun case.
4398fd4779740ec3e9e865d5514464df26d015354388Eric Laurent        sp<Track> l = mLatestActiveTrack.promote();
4399fd4779740ec3e9e865d5514464df26d015354388Eric Laurent        bool last = l.get() == track;
4400fd4779740ec3e9e865d5514464df26d015354388Eric Laurent
44017844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George        if (track->isInvalid()) {
44027844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George            ALOGW("An invalidated track shouldn't be in active list");
44037844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George            tracksToRemove->add(track);
44047844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George            continue;
44057844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George        }
44067844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George
44077844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George        if (track->mState == TrackBase::IDLE) {
44087844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George            ALOGW("An idle track shouldn't be in active list");
44097844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George            continue;
44107844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George        }
44117844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George
4412bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        if (track->isPausing()) {
4413bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            track->setPaused();
4414bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            if (last) {
4415bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                if (!mHwPaused) {
4416972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent                    doHwPause = true;
4417bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    mHwPaused = true;
4418bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                }
4419bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                // If we were part way through writing the mixbuffer to
4420bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                // the HAL we must save this until we resume
4421bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                // BUG - this will be wrong if a different track is made active,
4422bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                // in that case we want to discard the pending data in the
4423bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                // mixbuffer and tell the client to present it again when the
4424bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                // track is resumed
4425bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                mPausedWriteLength = mCurrentWriteLength;
4426bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                mPausedBytesRemaining = mBytesRemaining;
4427bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                mBytesRemaining = 0;    // stop writing
4428bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            }
4429bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            tracksToRemove->add(track);
44307844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George        } else if (track->isFlushPending()) {
44317844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George            track->flushAck();
44327844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George            if (last) {
44337844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George                mFlushPending = true;
44347844f679be8d94c5cdf017f53754cb68ee2f00daHaynes Mathew George            }
44352d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George        } else if (track->isResumePending()){
44362d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George            track->resumeAck();
44372d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George            if (last) {
44382d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George                if (mPausedBytesRemaining) {
44392d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George                    // Need to continue write that was interrupted
44402d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George                    mCurrentWriteLength = mPausedWriteLength;
44412d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George                    mBytesRemaining = mPausedBytesRemaining;
44422d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George                    mPausedBytesRemaining = 0;
44432d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George                }
44442d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George                if (mHwPaused) {
44452d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George                    doHwResume = true;
44462d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George                    mHwPaused = false;
44472d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George                    // threadLoop_mix() will handle the case that we need to
44482d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George                    // resume an interrupted write
44492d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George                }
44502d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George                // enable write to audio HAL
44512d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George                sleepTime = 0;
44522d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George
44532d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George                // Do not handle new data in this iteration even if track->framesReady()
44542d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George                mixerStatus = MIXER_TRACKS_ENABLED;
44552d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George            }
44562d3ca68363f723fbe269d3ce52dab4985dfc7154Haynes Mathew George        }  else if (track->framesReady() && track->isReady() &&
44573b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent                !track->isPaused() && !track->isTerminated() && !track->isStopping_2()) {
4458f20e1d8df84c5fbeeace0052d100982ae39bb7a4Glenn Kasten            ALOGVV("OffloadThread: track %d s=%08x [OK]", track->name(), cblk->mServer);
4459bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            if (track->mFillingUpStatus == Track::FS_FILLED) {
4460bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                track->mFillingUpStatus = Track::FS_ACTIVE;
44611abbdb4429479975718421c4fef3f79ce7c820e3Eric Laurent                // make sure processVolume_l() will apply new volume even if 0
44621abbdb4429479975718421c4fef3f79ce7c820e3Eric Laurent                mLeftVolFloat = mRightVolFloat = -1.0;
4463bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            }
4464bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
4465bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            if (last) {
4466d7e59228caad3867794d847f6bf163c6495e9506Eric Laurent                sp<Track> previousTrack = mPreviousTrack.promote();
4467d7e59228caad3867794d847f6bf163c6495e9506Eric Laurent                if (previousTrack != 0) {
4468d7e59228caad3867794d847f6bf163c6495e9506Eric Laurent                    if (track != previousTrack.get()) {
44699da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent                        // Flush any data still being written from last track
44709da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent                        mBytesRemaining = 0;
44719da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent                        if (mPausedBytesRemaining) {
44729da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent                            // Last track was paused so we also need to flush saved
44739da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent                            // mixbuffer state and invalidate track so that it will
44749da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent                            // re-submit that unwritten data when it is next resumed
44759da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent                            mPausedBytesRemaining = 0;
44769da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent                            // Invalidate is a bit drastic - would be more efficient
44779da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent                            // to have a flag to tell client that some of the
44789da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent                            // previously written data was lost
4479d7e59228caad3867794d847f6bf163c6495e9506Eric Laurent                            previousTrack->invalidate();
44809da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent                        }
44819da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent                        // flush data already sent to the DSP if changing audio session as audio
44829da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent                        // comes from a different source. Also invalidate previous track to force a
44839da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent                        // seek when resuming.
4484d7e59228caad3867794d847f6bf163c6495e9506Eric Laurent                        if (previousTrack->sessionId() != track->sessionId()) {
4485d7e59228caad3867794d847f6bf163c6495e9506Eric Laurent                            previousTrack->invalidate();
44869da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent                        }
44879da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent                    }
44889da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent                }
44899da3d9573a18ffe08365557c706cf52f09118d1cEric Laurent                mPreviousTrack = track;
4490bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                // reset retry count
4491bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                track->mRetryCount = kMaxTrackRetriesOffload;
4492bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                mActiveTrack = t;
4493bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                mixerStatus = MIXER_TRACKS_READY;
4494bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            }
4495bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        } else {
4496f20e1d8df84c5fbeeace0052d100982ae39bb7a4Glenn Kasten            ALOGVV("OffloadThread: track %d s=%08x [NOT READY]", track->name(), cblk->mServer);
4497bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            if (track->isStopping_1()) {
4498bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                // Hardware buffer can hold a large amount of audio so we must
4499bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                // wait for all current track's data to drain before we say
4500bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                // that the track is stopped.
4501bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                if (mBytesRemaining == 0) {
4502bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    // Only start draining when all data in mixbuffer
4503bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    // has been written
4504bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    ALOGV("OffloadThread: underrun and STOPPING_1 -> draining, STOPPING_2");
4505bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    track->mState = TrackBase::STOPPING_2; // so presentation completes after drain
45066a51d7ed7062536ccc892c8850a34ed55cbc8d5cEric Laurent                    // do not drain if no data was ever sent to HAL (mStandby == true)
45076a51d7ed7062536ccc892c8850a34ed55cbc8d5cEric Laurent                    if (last && !mStandby) {
45081b9f9b134e732a48198e51f16424f330cbf03143Eric Laurent                        // do not modify drain sequence if we are already draining. This happens
45091b9f9b134e732a48198e51f16424f330cbf03143Eric Laurent                        // when resuming from pause after drain.
45101b9f9b134e732a48198e51f16424f330cbf03143Eric Laurent                        if ((mDrainSequence & 1) == 0) {
45111b9f9b134e732a48198e51f16424f330cbf03143Eric Laurent                            sleepTime = 0;
45121b9f9b134e732a48198e51f16424f330cbf03143Eric Laurent                            standbyTime = systemTime() + standbyDelay;
45131b9f9b134e732a48198e51f16424f330cbf03143Eric Laurent                            mixerStatus = MIXER_DRAIN_TRACK;
45141b9f9b134e732a48198e51f16424f330cbf03143Eric Laurent                            mDrainSequence += 2;
45151b9f9b134e732a48198e51f16424f330cbf03143Eric Laurent                        }
4516bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                        if (mHwPaused) {
4517bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                            // It is possible to move from PAUSED to STOPPING_1 without
4518bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                            // a resume so we must ensure hardware is running
45191b9f9b134e732a48198e51f16424f330cbf03143Eric Laurent                            doHwResume = true;
4520bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                            mHwPaused = false;
4521bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                        }
4522bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    }
4523bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                }
4524bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            } else if (track->isStopping_2()) {
45256a51d7ed7062536ccc892c8850a34ed55cbc8d5cEric Laurent                // Drain has completed or we are in standby, signal presentation complete
45266a51d7ed7062536ccc892c8850a34ed55cbc8d5cEric Laurent                if (!(mDrainSequence & 1) || !last || mStandby) {
4527bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    track->mState = TrackBase::STOPPED;
4528bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    size_t audioHALFrames =
4529bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                            (mOutput->stream->get_latency(mOutput->stream)*mSampleRate) / 1000;
4530bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    size_t framesWritten =
4531665470b36f202bcc8ee2f7417f68fd2608dd07c1Eric Laurent                            mBytesWritten / audio_stream_out_frame_size(mOutput->stream);
4532bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    track->presentationComplete(framesWritten, audioHALFrames);
4533bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    track->reset();
4534bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    tracksToRemove->add(track);
4535bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                }
4536bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            } else {
4537bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                // No buffers for this track. Give it a few chances to
4538bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                // fill a buffer, then remove it from active list.
4539bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                if (--(track->mRetryCount) <= 0) {
4540bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    ALOGV("OffloadThread: BUFFER TIMEOUT: remove(%d) from active list",
4541bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                          track->name());
4542bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    tracksToRemove->add(track);
4543a23f17ac334ff20a11ee63dd177cb1080e44c483Eric Laurent                    // indicate to client process that the track was disabled because of underrun;
4544a23f17ac334ff20a11ee63dd177cb1080e44c483Eric Laurent                    // it will then automatically call start() when data is available
4545a23f17ac334ff20a11ee63dd177cb1080e44c483Eric Laurent                    android_atomic_or(CBLK_DISABLED, &cblk->mFlags);
4546bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                } else if (last){
4547bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                    mixerStatus = MIXER_TRACKS_ENABLED;
4548bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent                }
4549bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent            }
4550bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        }
4551bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        // compute volume for this track
4552bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        processVolume_l(track, last);
4553bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    }
45546bf9ae20b3bd2dbb8f2e89ee167a6785222301cfEric Laurent
4555ea0fadeb5d81ef3cb7f9db458c9033d628bdb86aEric Laurent    // make sure the pause/flush/resume sequence is executed in the right order.
4556ea0fadeb5d81ef3cb7f9db458c9033d628bdb86aEric Laurent    // If a flush is pending and a track is active but the HW is not paused, force a HW pause
4557ea0fadeb5d81ef3cb7f9db458c9033d628bdb86aEric Laurent    // before flush and then resume HW. This can happen in case of pause/flush/resume
4558ea0fadeb5d81ef3cb7f9db458c9033d628bdb86aEric Laurent    // if resume is received before pause is executed.
4559fd4779740ec3e9e865d5514464df26d015354388Eric Laurent    if (!mStandby && (doHwPause || (mFlushPending && !mHwPaused && (count != 0)))) {
4560972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent        mOutput->stream->pause(mOutput->stream);
4561972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent    }
45626bf9ae20b3bd2dbb8f2e89ee167a6785222301cfEric Laurent    if (mFlushPending) {
45636bf9ae20b3bd2dbb8f2e89ee167a6785222301cfEric Laurent        flushHw_l();
45646bf9ae20b3bd2dbb8f2e89ee167a6785222301cfEric Laurent        mFlushPending = false;
45656bf9ae20b3bd2dbb8f2e89ee167a6785222301cfEric Laurent    }
4566fd4779740ec3e9e865d5514464df26d015354388Eric Laurent    if (!mStandby && doHwResume) {
4567972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent        mOutput->stream->resume(mOutput->stream);
4568972a173d7d1de1a3b5a617aae3e2abb6e05ae02dEric Laurent    }
45696bf9ae20b3bd2dbb8f2e89ee167a6785222301cfEric Laurent
4570bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    // remove all the tracks that need to be...
4571bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    removeTracks_l(*tracksToRemove);
4572bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
4573bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    return mixerStatus;
4574bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}
4575bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
4576bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent// must be called with thread mutex locked
4577bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentbool AudioFlinger::OffloadThread::waitingAsyncCallback_l()
4578bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{
45793b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    ALOGVV("waitingAsyncCallback_l mWriteAckSequence %d mDrainSequence %d",
45803b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent          mWriteAckSequence, mDrainSequence);
45813b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent    if (mUseAsyncWrite && ((mWriteAckSequence & 1) || (mDrainSequence & 1))) {
4582bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        return true;
4583bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    }
4584bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    return false;
4585bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}
4586bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
4587bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent// must be called with thread mutex locked
4588bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentbool AudioFlinger::OffloadThread::shouldStandby_l()
4589bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{
4590e6f35b13567d920af19f0b8dc35f9120b7cf1dc9Glenn Kasten    bool trackPaused = false;
4591bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
4592bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    // do not put the HAL in standby when paused. AwesomePlayer clear the offloaded AudioTrack
4593bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    // after a timeout and we will enter standby then.
4594bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    if (mTracks.size() > 0) {
4595e6f35b13567d920af19f0b8dc35f9120b7cf1dc9Glenn Kasten        trackPaused = mTracks[mTracks.size() - 1]->isPaused();
4596bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    }
4597bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
4598e6f35b13567d920af19f0b8dc35f9120b7cf1dc9Glenn Kasten    return !mStandby && !trackPaused;
4599bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}
4600bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
4601bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
4602bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentbool AudioFlinger::OffloadThread::waitingAsyncCallback()
4603bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{
4604bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    Mutex::Autolock _l(mLock);
4605bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    return waitingAsyncCallback_l();
4606bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}
4607bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
4608bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentvoid AudioFlinger::OffloadThread::flushHw_l()
4609bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent{
4610e659ef420dae0caae84ab78f9df8952acb9ad3a0Eric Laurent    DirectOutputThread::flushHw_l();
4611bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    // Flush anything still waiting in the mixbuffer
4612bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    mCurrentWriteLength = 0;
4613bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    mBytesRemaining = 0;
4614bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    mPausedWriteLength = 0;
4615bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    mPausedBytesRemaining = 0;
46160f02f265123b7ef2fd6ac09ff70cde26eb5559adHaynes Mathew George    mHwPaused = false;
46170f02f265123b7ef2fd6ac09ff70cde26eb5559adHaynes Mathew George
4618bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    if (mUseAsyncWrite) {
46193b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent        // discard any pending drain or write ack by incrementing sequence
46203b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent        mWriteAckSequence = (mWriteAckSequence + 2) & ~1;
46213b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent        mDrainSequence = (mDrainSequence + 2) & ~1;
4622bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent        ALOG_ASSERT(mCallbackThread != 0);
46233b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent        mCallbackThread->setWriteBlocked(mWriteAckSequence);
46243b4529e03c5fc7a44c22f9091ad15a269bfca3a8Eric Laurent        mCallbackThread->setDraining(mDrainSequence);
4625bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    }
4626bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent}
4627bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
46284c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew Georgevoid AudioFlinger::OffloadThread::onAddNewTrack_l()
46294c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George{
46304c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George    sp<Track> previousTrack = mPreviousTrack.promote();
46314c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George    sp<Track> latestTrack = mLatestActiveTrack.promote();
46324c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George
46334c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George    if (previousTrack != 0 && latestTrack != 0 &&
46344c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George        (previousTrack->sessionId() != latestTrack->sessionId())) {
46354c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George        mFlushPending = true;
46364c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George    }
46374c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George    PlaybackThread::onAddNewTrack_l();
46384c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George}
46394c6a433d74d5ae8b9bc0557207e3ced43bf34a25Haynes Mathew George
4640bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent// ----------------------------------------------------------------------------
4641bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent
464281784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::DuplicatingThread::DuplicatingThread(const sp<AudioFlinger>& audioFlinger,
464381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        AudioFlinger::MixerThread* mainThread, audio_io_handle_t id)
464481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    :   MixerThread(audioFlinger, mainThread->getOutput(), id, mainThread->outDevice(),
464581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                DUPLICATING),
464681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mWaitTimeMs(UINT_MAX)
464781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
464881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    addOutputTrack(mainThread);
464981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
465081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
465181784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::DuplicatingThread::~DuplicatingThread()
465281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
465381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mOutputTracks.size(); i++) {
465481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mOutputTracks[i]->destroy();
465581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
465681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
465781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
465881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DuplicatingThread::threadLoop_mix()
465981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
466081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // mix buffers...
466181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (outputsReady(outputTracks)) {
466281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mAudioMixer->process(AudioBufferProvider::kInvalidPTS);
466381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    } else {
466402b5708776ba2a9b4ff8c09008483aef7dbe38c7Eric Laurent        if (mMixerBufferValid) {
466502b5708776ba2a9b4ff8c09008483aef7dbe38c7Eric Laurent            memset(mMixerBuffer, 0, mMixerBufferSize);
466602b5708776ba2a9b4ff8c09008483aef7dbe38c7Eric Laurent        } else {
466702b5708776ba2a9b4ff8c09008483aef7dbe38c7Eric Laurent            memset(mSinkBuffer, 0, mSinkBufferSize);
466802b5708776ba2a9b4ff8c09008483aef7dbe38c7Eric Laurent        }
466981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
467081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sleepTime = 0;
467181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    writeFrames = mNormalFrameCount;
467225c2dac12114699e90deb1c579cadebce7b91a97Andy Hung    mCurrentWriteLength = mSinkBufferSize;
467381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    standbyTime = systemTime() + standbyDelay;
467481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
467581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
467681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DuplicatingThread::threadLoop_sleepTime()
467781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
467881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (sleepTime == 0) {
467981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (mMixerStatus == MIXER_TRACKS_ENABLED) {
468081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sleepTime = activeSleepTime;
468181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else {
468281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            sleepTime = idleSleepTime;
468381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
468481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    } else if (mBytesWritten != 0) {
468581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (mMixerStatus == MIXER_TRACKS_ENABLED) {
468681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            writeFrames = mNormalFrameCount;
468725c2dac12114699e90deb1c579cadebce7b91a97Andy Hung            memset(mSinkBuffer, 0, mSinkBufferSize);
468881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else {
468981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // flush remaining overflow buffers in output tracks
469081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            writeFrames = 0;
469181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
469281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sleepTime = 0;
469381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
469481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
469581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
4696bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurentssize_t AudioFlinger::DuplicatingThread::threadLoop_write()
469781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
4698ec0eeafa8a7fd882e8206e9cf8c4c7e1db81857bHaynes Mathew George    // We convert the duplicating thread format to AUDIO_FORMAT_PCM_16_BIT
4699ec0eeafa8a7fd882e8206e9cf8c4c7e1db81857bHaynes Mathew George    // for delivery downstream as needed. This in-place conversion is safe as
4700ec0eeafa8a7fd882e8206e9cf8c4c7e1db81857bHaynes Mathew George    // AUDIO_FORMAT_PCM_16_BIT is smaller than any other supported format
4701ec0eeafa8a7fd882e8206e9cf8c4c7e1db81857bHaynes Mathew George    // (AUDIO_FORMAT_PCM_8_BIT is not allowed here).
4702ec0eeafa8a7fd882e8206e9cf8c4c7e1db81857bHaynes Mathew George    if (mFormat != AUDIO_FORMAT_PCM_16_BIT) {
4703ec0eeafa8a7fd882e8206e9cf8c4c7e1db81857bHaynes Mathew George        memcpy_by_audio_format(mSinkBuffer, AUDIO_FORMAT_PCM_16_BIT,
4704ec0eeafa8a7fd882e8206e9cf8c4c7e1db81857bHaynes Mathew George                               mSinkBuffer, mFormat, writeFrames * mChannelCount);
4705ec0eeafa8a7fd882e8206e9cf8c4c7e1db81857bHaynes Mathew George    }
470681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < outputTracks.size(); i++) {
4707010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung        outputTracks[i]->write(reinterpret_cast<int16_t*>(mSinkBuffer), writeFrames);
470881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
47092c3740f01acca69c3e0bcc5e01bb0edc51b6777fEric Laurent    mStandby = false;
471025c2dac12114699e90deb1c579cadebce7b91a97Andy Hung    return (ssize_t)mSinkBufferSize;
471181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
471281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
471381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DuplicatingThread::threadLoop_standby()
471481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
471581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // DuplicatingThread implements standby by stopping all tracks
471681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < outputTracks.size(); i++) {
471781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        outputTracks[i]->stop();
471881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
471981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
472081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
472181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DuplicatingThread::saveOutputTracks()
472281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
472381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    outputTracks = mOutputTracks;
472481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
472581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
472681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DuplicatingThread::clearOutputTracks()
472781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
472881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    outputTracks.clear();
472981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
473081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
473181784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DuplicatingThread::addOutputTrack(MixerThread *thread)
473281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
473381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
473481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // FIXME explain this formula
473581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    size_t frameCount = (3 * mNormalFrameCount * mSampleRate) / thread->sampleRate();
4736010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung    // OutputTrack is forced to AUDIO_FORMAT_PCM_16_BIT regardless of mFormat
4737010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung    // due to current usage case and restrictions on the AudioBufferProvider.
4738010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung    // Actual buffer conversion is done in threadLoop_write().
4739010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung    //
4740010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung    // TODO: This may change in the future, depending on multichannel
4741010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung    // (and non int16_t*) support on AF::PlaybackThread::OutputTrack
474281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    OutputTrack *outputTrack = new OutputTrack(thread,
474381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                            this,
474481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                            mSampleRate,
4745010a1a1a552cdaad362cea8a0333b8906402dbcbAndy Hung                                            AUDIO_FORMAT_PCM_16_BIT,
474681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                            mChannelMask,
4747462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen                                            frameCount,
4748462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen                                            IPCThreadState::self()->getCallingUid());
474981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (outputTrack->cblk() != NULL) {
4750223fd5c9738e9665e495904d37d4632414b68c1eEric Laurent        thread->setStreamVolume(AUDIO_STREAM_PATCH, 1.0f);
475181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mOutputTracks.add(outputTrack);
475281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGV("addOutputTrack() track %p, on thread %p", outputTrack, thread);
475381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        updateWaitTime_l();
475481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
475581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
475681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
475781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DuplicatingThread::removeOutputTrack(MixerThread *thread)
475881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
475981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
476081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mOutputTracks.size(); i++) {
476181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (mOutputTracks[i]->thread() == thread) {
476281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mOutputTracks[i]->destroy();
476381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            mOutputTracks.removeAt(i);
476481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            updateWaitTime_l();
476581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return;
476681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
476781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
476881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("removeOutputTrack(): unkonwn thread: %p", thread);
476981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
477081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
477181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// caller must hold mLock
477281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DuplicatingThread::updateWaitTime_l()
477381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
477481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mWaitTimeMs = UINT_MAX;
477581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mOutputTracks.size(); i++) {
477681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<ThreadBase> strong = mOutputTracks[i]->thread().promote();
477781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (strong != 0) {
477881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            uint32_t waitTimeMs = (strong->frameCount() * 2 * 1000) / strong->sampleRate();
477981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            if (waitTimeMs < mWaitTimeMs) {
478081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                mWaitTimeMs = waitTimeMs;
478181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
478281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
478381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
478481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
478581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
478681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
478781784c37c61b09289654b979567a42bf73cd2b12Eric Laurentbool AudioFlinger::DuplicatingThread::outputsReady(
478881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        const SortedVector< sp<OutputTrack> > &outputTracks)
478981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
479081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < outputTracks.size(); i++) {
479181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<ThreadBase> thread = outputTracks[i]->thread().promote();
479281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (thread == 0) {
479381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGW("DuplicatingThread::outputsReady() could not promote thread on output track %p",
479481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    outputTracks[i].get());
479581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return false;
479681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
479781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        PlaybackThread *playbackThread = (PlaybackThread *)thread.get();
479881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // see note at standby() declaration
479981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (playbackThread->standby() && !playbackThread->isSuspended()) {
480081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGV("DuplicatingThread output track %p on thread %p Not Ready", outputTracks[i].get(),
480181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    thread.get());
480281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return false;
480381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
480481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
480581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return true;
480681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
480781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
480881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::DuplicatingThread::activeSleepTimeUs() const
480981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
481081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return (mWaitTimeMs * 1000) / 2;
481181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
481281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
481381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::DuplicatingThread::cacheParameters_l()
481481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
481581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // updateWaitTime_l() sets mWaitTimeMs, which affects activeSleepTimeUs(), so call it first
481681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    updateWaitTime_l();
481781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
481881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    MixerThread::cacheParameters_l();
481981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
482081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
482181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
482281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent//      Record
482381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// ----------------------------------------------------------------------------
482481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
482581784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::RecordThread::RecordThread(const sp<AudioFlinger>& audioFlinger,
482681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                         AudioStreamIn *input,
482781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                         audio_io_handle_t id,
4828d3922f72601d82c6fc067a98916fda0bd1291c5fEric Laurent                                         audio_devices_t outDevice,
482946909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten                                         audio_devices_t inDevice
483046909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK
483146909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten                                         , const sp<NBAIO_Sink>& teeSink
483246909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif
483346909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten                                         ) :
4834d3922f72601d82c6fc067a98916fda0bd1291c5fEric Laurent    ThreadBase(audioFlinger, id, outDevice, inDevice, RECORD),
48356dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    mInput(input), mActiveTracksGen(0), mRsmpInBuffer(NULL),
4836deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten    // mRsmpInFrames and mRsmpInFramesP2 are set by readInputParameters_l()
48374cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten    mRsmpInRear(0)
483846909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#ifdef TEE_SINK
483946909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten    , mTeeSink(teeSink)
484046909e7eb074ce1b95b8a411eb71154f53f84f77Glenn Kasten#endif
4841b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten    , mReadOnlyHeap(new MemoryDealer(kRecordThreadReadOnlyHeapSize,
4842b880f5e5fc07397ddd09a94ba18bdf4fa62aae00Glenn Kasten            "RecordThreadRO", MemoryHeapBase::READ_ONLY))
48436dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    // mFastCapture below
48446dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    , mFastCaptureFutex(0)
48456dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    // mInputSource
48466dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    // mPipeSink
48476dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    // mPipeSource
48486dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    , mPipeFramesP2(0)
48496dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    // mPipeMemory
48506dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    // mFastCaptureNBLogWriter
48516e6704c06d61bc356e30c164081e5bcffb37920cGlenn Kasten    , mFastTrackAvail(false)
485281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
485381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    snprintf(mName, kNameLength, "AudioIn_%X", id);
4854481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    mNBLogWriter = audioFlinger->newWriter_l(kLogSize, mName);
485581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
4856deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kasten    readInputParameters_l();
48576dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten
48586dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    // create an NBAIO source for the HAL input stream, and negotiate
48596dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    mInputSource = new AudioStreamInSource(input->stream);
48606dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    size_t numCounterOffers = 0;
48616dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    const NBAIO_Format offers[1] = {Format_from_SR_C(mSampleRate, mChannelCount, mFormat)};
48626dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    ssize_t index = mInputSource->negotiate(offers, 1, NULL, numCounterOffers);
48636dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    ALOG_ASSERT(index == 0);
48646dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten
48656dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    // initialize fast capture depending on configuration
48666dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    bool initFastCapture;
48676dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    switch (kUseFastCapture) {
48686dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    case FastCapture_Never:
48696dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        initFastCapture = false;
48706dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        break;
48716dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    case FastCapture_Always:
48726dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        initFastCapture = true;
48736dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        break;
48746dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    case FastCapture_Static:
48756dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        uint32_t primaryOutputSampleRate;
48766dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        {
48776dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            AutoMutex _l(audioFlinger->mHardwareLock);
48786dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            primaryOutputSampleRate = audioFlinger->mPrimaryOutputSampleRate;
48796dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        }
48806dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        initFastCapture =
48816dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                // either capture sample rate is same as (a reasonable) primary output sample rate
48826dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                (((primaryOutputSampleRate == 44100 || primaryOutputSampleRate == 48000) &&
48836dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                    (mSampleRate == primaryOutputSampleRate)) ||
48846dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                // or primary output sample rate is unknown, and capture sample rate is reasonable
48856dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                ((primaryOutputSampleRate == 0) &&
48866dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                    ((mSampleRate == 44100 || mSampleRate == 48000)))) &&
48879f81de3452dfb2385bd57dc05456a045174a1ab1Glenn Kasten                // and the buffer size is < 12 ms
48889f81de3452dfb2385bd57dc05456a045174a1ab1Glenn Kasten                (mFrameCount * 1000) / mSampleRate < 12;
48896dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        break;
48906dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    // case FastCapture_Dynamic:
48916dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    }
48926dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten
48936dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    if (initFastCapture) {
48946dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        // create a Pipe for FastMixer to write to, and for us and fast tracks to read from
48956dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        NBAIO_Format format = mInputSource->format();
489649d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten        size_t pipeFramesP2 = roundup(mSampleRate / 25);    // double-buffering of 20 ms each
48976dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        size_t pipeSize = pipeFramesP2 * Format_frameSize(format);
48986dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        void *pipeBuffer;
48996dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        const sp<MemoryDealer> roHeap(readOnlyHeap());
49006dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        sp<IMemory> pipeMemory;
49016dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        if ((roHeap == 0) ||
49026dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                (pipeMemory = roHeap->allocate(pipeSize)) == 0 ||
49036dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                (pipeBuffer = pipeMemory->pointer()) == NULL) {
49046dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            ALOGE("not enough memory for pipe buffer size=%zu", pipeSize);
49056dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            goto failed;
49066dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        }
49076dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        // pipe will be shared directly with fast clients, so clear to avoid leaking old information
49086dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        memset(pipeBuffer, 0, pipeSize);
49096dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        Pipe *pipe = new Pipe(pipeFramesP2, format, pipeBuffer);
49106dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        const NBAIO_Format offers[1] = {format};
49116dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        size_t numCounterOffers = 0;
49126dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        ssize_t index = pipe->negotiate(offers, 1, NULL, numCounterOffers);
49136dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        ALOG_ASSERT(index == 0);
49146dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        mPipeSink = pipe;
49156dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        PipeReader *pipeReader = new PipeReader(*pipe);
49166dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        numCounterOffers = 0;
49176dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        index = pipeReader->negotiate(offers, 1, NULL, numCounterOffers);
49186dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        ALOG_ASSERT(index == 0);
49196dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        mPipeSource = pipeReader;
49206dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        mPipeFramesP2 = pipeFramesP2;
49216dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        mPipeMemory = pipeMemory;
49226dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten
49236dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        // create fast capture
49246dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        mFastCapture = new FastCapture();
49256dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        FastCaptureStateQueue *sq = mFastCapture->sq();
49266dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#ifdef STATE_QUEUE_DUMP
49276dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        // FIXME
49286dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#endif
49296dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        FastCaptureState *state = sq->begin();
49306dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        state->mCblk = NULL;
49316dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        state->mInputSource = mInputSource.get();
49326dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        state->mInputSourceGen++;
49336dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        state->mPipeSink = pipe;
49346dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        state->mPipeSinkGen++;
49356dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        state->mFrameCount = mFrameCount;
49366dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        state->mCommand = FastCaptureState::COLD_IDLE;
49376dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        // already done in constructor initialization list
49386dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        //mFastCaptureFutex = 0;
49396dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        state->mColdFutexAddr = &mFastCaptureFutex;
49406dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        state->mColdGen++;
49416dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        state->mDumpState = &mFastCaptureDumpState;
49426dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#ifdef TEE_SINK
49436dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        // FIXME
49446dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#endif
49456dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        mFastCaptureNBLogWriter = audioFlinger->newWriter_l(kFastCaptureLogSize, "FastCapture");
49466dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        state->mNBLogWriter = mFastCaptureNBLogWriter.get();
49476dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        sq->end();
49486dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        sq->push(FastCaptureStateQueue::BLOCK_UNTIL_PUSHED);
49496dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten
49506dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        // start the fast capture
49516dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        mFastCapture->run("FastCapture", ANDROID_PRIORITY_URGENT_AUDIO);
49526dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        pid_t tid = mFastCapture->getTid();
49536dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        int err = requestPriority(getpid_cached, tid, kPriorityFastMixer);
49546dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        if (err != 0) {
49556dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            ALOGW("Policy SCHED_FIFO priority %d is unavailable for pid %d tid %d; error %d",
49566dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                    kPriorityFastCapture, getpid_cached, tid, err);
49576dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        }
49586dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten
49596dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#ifdef AUDIO_WATCHDOG
49606dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        // FIXME
49616dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#endif
49626dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten
49636e6704c06d61bc356e30c164081e5bcffb37920cGlenn Kasten        mFastTrackAvail = true;
49646dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    }
49656dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kastenfailed: ;
49666dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten
49676dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    // FIXME mNormalSource
496881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
496981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
497081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
497181784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::RecordThread::~RecordThread()
497281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
49736dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    if (mFastCapture != 0) {
49746dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        FastCaptureStateQueue *sq = mFastCapture->sq();
49756dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        FastCaptureState *state = sq->begin();
49766dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        if (state->mCommand == FastCaptureState::COLD_IDLE) {
49776dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            int32_t old = android_atomic_inc(&mFastCaptureFutex);
49786dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            if (old == -1) {
49796dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                (void) syscall(__NR_futex, &mFastCaptureFutex, FUTEX_WAKE_PRIVATE, 1);
49806dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            }
49816dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        }
49826dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        state->mCommand = FastCaptureState::EXIT;
49836dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        sq->end();
49846dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        sq->push(FastCaptureStateQueue::BLOCK_UNTIL_PUSHED);
49856dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        mFastCapture->join();
49866dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        mFastCapture.clear();
49876dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    }
49886dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    mAudioFlinger->unregisterWriter(mFastCaptureNBLogWriter);
4989481fb67a595f23c5b7f5be84b06db9b84a41a42fGlenn Kasten    mAudioFlinger->unregisterWriter(mNBLogWriter);
499081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    delete[] mRsmpInBuffer;
499181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
499281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
499381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::RecordThread::onFirstRef()
499481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
499581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    run(mName, PRIORITY_URGENT_AUDIO);
499681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
499781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
499881784c37c61b09289654b979567a42bf73cd2b12Eric Laurentbool AudioFlinger::RecordThread::threadLoop()
499981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
500081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    nsecs_t lastWarning = 0;
500181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
500281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    inputStandBy();
500381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
5004f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kastenreacquire_wakelock:
5005f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten    sp<RecordTrack> activeTrack;
50062b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten    int activeTracksGen;
5007f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten    {
5008f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten        Mutex::Autolock _l(mLock);
50092b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten        size_t size = mActiveTracks.size();
50102b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten        activeTracksGen = mActiveTracksGen;
50112b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten        if (size > 0) {
50122b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten            // FIXME an arbitrary choice
50132b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten            activeTrack = mActiveTracks[0];
50142b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten            acquireWakeLock_l(activeTrack->uid());
50152b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten            if (size > 1) {
50162b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten                SortedVector<int> tmp;
50172b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten                for (size_t i = 0; i < size; i++) {
50182b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten                    tmp.add(mActiveTracks[i]->uid());
50192b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten                }
50202b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten                updateWakeLockUids_l(tmp);
50212b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten            }
50222b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten        } else {
50232b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten            acquireWakeLock_l(-1);
50242b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten        }
5025f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten    }
5026f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten
50276dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    // used to request a deferred sleep, to be executed later while mutex is unlocked
50286dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    uint32_t sleepUs = 0;
50296dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten
50306dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    // loop while there is work to do
50314ef0b463a56c19bad9197aa9f90d792090461429Glenn Kasten    for (;;) {
5032c527a7c2b1bfd26e8f3086e1b653d56e521379d9Glenn Kasten        Vector< sp<EffectChain> > effectChains;
50332cfbf88b89854f30b295e8ae26a031edb8d712f8Glenn Kasten
50345edadd46c76c5ff1c3edabf2ea943c2278e82e1cGlenn Kasten        // sleep with mutex unlocked
50356dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        if (sleepUs > 0) {
50366dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            usleep(sleepUs);
50376dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            sleepUs = 0;
50385edadd46c76c5ff1c3edabf2ea943c2278e82e1cGlenn Kasten        }
50395edadd46c76c5ff1c3edabf2ea943c2278e82e1cGlenn Kasten
50406dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        // activeTracks accumulates a copy of a subset of mActiveTracks
50416dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        Vector< sp<RecordTrack> > activeTracks;
50426dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten
5043735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten        // reference to the (first and only) active fast track
50446dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        sp<RecordTrack> fastTrack;
50451035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent
5046735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten        // reference to a fast track which is about to be removed
5047735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten        sp<RecordTrack> fastTrackToRemove;
5048735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten
504981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        { // scope for mLock
505081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            Mutex::Autolock _l(mLock);
5051000a4193dd82549192277fd4b9bb571d8a4c262fEric Laurent
5052021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent            processConfigEvents_l();
5053f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten
5054000a4193dd82549192277fd4b9bb571d8a4c262fEric Laurent            // check exitPending here because checkForNewParameters_l() and
5055000a4193dd82549192277fd4b9bb571d8a4c262fEric Laurent            // checkForNewParameters_l() can temporarily release mLock
5056000a4193dd82549192277fd4b9bb571d8a4c262fEric Laurent            if (exitPending()) {
5057000a4193dd82549192277fd4b9bb571d8a4c262fEric Laurent                break;
5058000a4193dd82549192277fd4b9bb571d8a4c262fEric Laurent            }
5059000a4193dd82549192277fd4b9bb571d8a4c262fEric Laurent
50602b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten            // if no active track(s), then standby and release wakelock
50612b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten            size_t size = mActiveTracks.size();
50622b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten            if (size == 0) {
506393e471f620454f7de73d190521568b1e25879767Glenn Kasten                standbyIfNotAlreadyInStandby();
50644ef0b463a56c19bad9197aa9f90d792090461429Glenn Kasten                // exitPending() can't become true here
506581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                releaseWakeLock_l();
506681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGV("RecordThread: loop stopping");
506781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                // go to sleep
506881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                mWaitWorkCV.wait(mLock);
506981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                ALOGV("RecordThread: loop starting");
5070f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten                goto reacquire_wakelock;
5071f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten            }
5072f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten
50732b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten            if (mActiveTracksGen != activeTracksGen) {
50742b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten                activeTracksGen = mActiveTracksGen;
5075f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten                SortedVector<int> tmp;
50762b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten                for (size_t i = 0; i < size; i++) {
50772b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten                    tmp.add(mActiveTracks[i]->uid());
50782b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten                }
5079f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten                updateWakeLockUids_l(tmp);
508081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
50819e98235a9e9bb870e1c76911e3b4d00386a52c39Glenn Kasten
50826dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            bool doBroadcast = false;
50836dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            for (size_t i = 0; i < size; ) {
50849e98235a9e9bb870e1c76911e3b4d00386a52c39Glenn Kasten
50856dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                activeTrack = mActiveTracks[i];
50866dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                if (activeTrack->isTerminated()) {
5087735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten                    if (activeTrack->isFastTrack()) {
5088735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten                        ALOG_ASSERT(fastTrackToRemove == 0);
5089735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten                        fastTrackToRemove = activeTrack;
5090735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten                    }
50916dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    removeTrack_l(activeTrack);
50922b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten                    mActiveTracks.remove(activeTrack);
50932b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten                    mActiveTracksGen++;
50946dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    size--;
50959e98235a9e9bb870e1c76911e3b4d00386a52c39Glenn Kasten                    continue;
50969e98235a9e9bb870e1c76911e3b4d00386a52c39Glenn Kasten                }
50976dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten
50986dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                TrackBase::track_state activeTrackState = activeTrack->mState;
50996dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                switch (activeTrackState) {
51006dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten
51016dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                case TrackBase::PAUSING:
51026dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    mActiveTracks.remove(activeTrack);
51036dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    mActiveTracksGen++;
51046dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    doBroadcast = true;
51056dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    size--;
51066dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    continue;
51076dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten
51086dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                case TrackBase::STARTING_1:
51096dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    sleepUs = 10000;
51106dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    i++;
51116dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    continue;
51126dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten
51136dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                case TrackBase::STARTING_2:
51146dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    doBroadcast = true;
51156dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    mStandby = false;
51169e98235a9e9bb870e1c76911e3b4d00386a52c39Glenn Kasten                    activeTrack->mState = TrackBase::ACTIVE;
51176dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    break;
51186dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten
51196dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                case TrackBase::ACTIVE:
51206dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    break;
51216dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten
51226dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                case TrackBase::IDLE:
51236dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    i++;
51246dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    continue;
51256dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten
51266dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                default:
5127adad3d7d935da176ff24941b4ae9edf7340e9b96Glenn Kasten                    LOG_ALWAYS_FATAL("Unexpected activeTrackState %d", activeTrackState);
51289e98235a9e9bb870e1c76911e3b4d00386a52c39Glenn Kasten                }
51292d94426cd3302cb1215c92c5f1c4b90c24ceb72bGlenn Kasten
51306dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                activeTracks.add(activeTrack);
51316dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                i++;
51322d94426cd3302cb1215c92c5f1c4b90c24ceb72bGlenn Kasten
51336dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                if (activeTrack->isFastTrack()) {
51346dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                    ALOG_ASSERT(!mFastTrackAvail);
51356dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                    ALOG_ASSERT(fastTrack == 0);
51366dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                    fastTrack = activeTrack;
51376dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                }
51386dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            }
51396dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            if (doBroadcast) {
51406dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                mStartStopCond.broadcast();
51416dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            }
5142d9fc34fb0fcfcc739f868b116edf50c62af19d5eGlenn Kasten
51436dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            // sleep if there are no active tracks to process
51446dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            if (activeTracks.size() == 0) {
51456dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                if (sleepUs == 0) {
51466dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    sleepUs = kRecordThreadSleepUs;
51476dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                }
51486dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                continue;
514981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
51506dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            sleepUs = 0;
51519e98235a9e9bb870e1c76911e3b4d00386a52c39Glenn Kasten
515281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            lockEffectChains_l(effectChains);
515381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
515481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
51556dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        // thread mutex is now unlocked, mActiveTracks unknown, activeTracks.size() > 0
51567165268ffa6c7b6b405b6afad82e2a346500e8eeGlenn Kasten
51576dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        size_t size = effectChains.size();
51586dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        for (size_t i = 0; i < size; i++) {
51591ba19cd7fcdf18ab6efab2a1b831affab9a46157Glenn Kasten            // thread mutex is not locked, but effect chain is locked
51601ba19cd7fcdf18ab6efab2a1b831affab9a46157Glenn Kasten            effectChains[i]->process_l();
51611ba19cd7fcdf18ab6efab2a1b831affab9a46157Glenn Kasten        }
516281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
5163735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten        // Push a new fast capture state if fast capture is not already running, or cblk change
51646dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        if (mFastCapture != 0) {
51656dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            FastCaptureStateQueue *sq = mFastCapture->sq();
51666dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            FastCaptureState *state = sq->begin();
5167735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten            bool didModify = false;
5168735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten            FastCaptureStateQueue::block_t block = FastCaptureStateQueue::BLOCK_UNTIL_PUSHED;
51696dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            if (state->mCommand != FastCaptureState::READ_WRITE /* FIXME &&
51706dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                    (kUseFastMixer != FastMixer_Dynamic || state->mTrackMask > 1)*/) {
51716dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                if (state->mCommand == FastCaptureState::COLD_IDLE) {
51726dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                    int32_t old = android_atomic_inc(&mFastCaptureFutex);
51736dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                    if (old == -1) {
51746dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                        (void) syscall(__NR_futex, &mFastCaptureFutex, FUTEX_WAKE_PRIVATE, 1);
51756dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                    }
51766dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                }
51776dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                state->mCommand = FastCaptureState::READ_WRITE;
51786dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#if 0   // FIXME
51796dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                mFastCaptureDumpState.increaseSamplingN(mAudioFlinger->isLowRamDevice() ?
51806dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                        FastCaptureDumpState::kSamplingNforLowRamDevice : FastMixerDumpState::kSamplingN);
51816dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#endif
5182735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten                didModify = true;
5183735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten            }
5184735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten            audio_track_cblk_t *cblkOld = state->mCblk;
5185735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten            audio_track_cblk_t *cblkNew = fastTrack != 0 ? fastTrack->cblk() : NULL;
5186735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten            if (cblkNew != cblkOld) {
5187735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten                state->mCblk = cblkNew;
5188735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten                // block until acked if removing a fast track
5189735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten                if (cblkOld != NULL) {
5190735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten                    block = FastCaptureStateQueue::BLOCK_UNTIL_ACKED;
5191735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten                }
5192735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten                didModify = true;
5193735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten            }
5194735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten            sq->end(didModify);
5195735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten            if (didModify) {
5196735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten                sq->push(block);
51976dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#if 0
51986dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                if (kUseFastCapture == FastCapture_Dynamic) {
51996dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                    mNormalSource = mPipeSource;
52006dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                }
52016dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#endif
52026dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            }
52036dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        }
52046dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten
5205735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten        // now run the fast track destructor with thread mutex unlocked
5206735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten        fastTrackToRemove.clear();
5207735f45fbc37d7905ffb722f40727edbed82319b7Glenn Kasten
52086dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        // Read from HAL to keep up with fastest client if multiple active tracks, not slowest one.
52096dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        // Only the client(s) that are too slow will overrun. But if even the fastest client is too
52106dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        // slow, then this RecordThread will overrun by not calling HAL read often enough.
52116dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        // If destination is non-contiguous, first read past the nominal end of buffer, then
52126dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        // copy to the right place.  Permitted because mRsmpInBuffer was over-allocated.
52136dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten
52146dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        int32_t rear = mRsmpInRear & (mRsmpInFramesP2 - 1);
52156dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        ssize_t framesRead;
52166dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten
52176dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        // If an NBAIO source is present, use it to read the normal capture's data
52186dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        if (mPipeSource != 0) {
52196dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            size_t framesToRead = mBufferSize / mFrameSize;
52206dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            framesRead = mPipeSource->read(&mRsmpInBuffer[rear * mChannelCount],
52216dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                    framesToRead, AudioBufferProvider::kInvalidPTS);
52226dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            if (framesRead == 0) {
52236dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                // since pipe is non-blocking, simulate blocking input
52246dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                sleepUs = (framesToRead * 1000000LL) / mSampleRate;
52256dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            }
52266dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        // otherwise use the HAL / AudioStreamIn directly
52276dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        } else {
52286dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            ssize_t bytesRead = mInput->stream->read(mInput->stream,
52296dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                    &mRsmpInBuffer[rear * mChannelCount], mBufferSize);
52306dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            if (bytesRead < 0) {
52316dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                framesRead = bytesRead;
52326dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            } else {
52336dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                framesRead = bytesRead / mFrameSize;
52346dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            }
52356dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        }
52366dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten
52376dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        if (framesRead < 0 || (framesRead == 0 && mPipeSource == 0)) {
52386dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            ALOGE("read failed: framesRead=%d", framesRead);
52396dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            // Force input into standby so that it tries to recover at next read attempt
52406dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            inputStandBy();
52416dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            sleepUs = kRecordThreadSleepUs;
52426dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        }
52436dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        if (framesRead <= 0) {
52443d61bc1ffc8afc8d7be3b0d4205c9b5ba6daf2e8Glenn Kasten            goto unlock;
52456dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        }
52466dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        ALOG_ASSERT(framesRead > 0);
52476dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten
52486dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        if (mTeeSink != 0) {
52496dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            (void) mTeeSink->write(&mRsmpInBuffer[rear * mChannelCount], framesRead);
52506dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        }
52516dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        // If destination is non-contiguous, we now correct for reading past end of buffer.
52523d61bc1ffc8afc8d7be3b0d4205c9b5ba6daf2e8Glenn Kasten        {
52533d61bc1ffc8afc8d7be3b0d4205c9b5ba6daf2e8Glenn Kasten            size_t part1 = mRsmpInFramesP2 - rear;
52543d61bc1ffc8afc8d7be3b0d4205c9b5ba6daf2e8Glenn Kasten            if ((size_t) framesRead > part1) {
52553d61bc1ffc8afc8d7be3b0d4205c9b5ba6daf2e8Glenn Kasten                memcpy(mRsmpInBuffer, &mRsmpInBuffer[mRsmpInFramesP2 * mChannelCount],
52563d61bc1ffc8afc8d7be3b0d4205c9b5ba6daf2e8Glenn Kasten                        (framesRead - part1) * mFrameSize);
52573d61bc1ffc8afc8d7be3b0d4205c9b5ba6daf2e8Glenn Kasten            }
52586dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        }
52596dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        rear = mRsmpInRear += framesRead;
52606dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten
52616dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        size = activeTracks.size();
52626dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        // loop over each active track
52636dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        for (size_t i = 0; i < size; i++) {
52646dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            activeTrack = activeTracks[i];
52656dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten
52666dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // skip fast tracks, as those are handled directly by FastCapture
52676dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            if (activeTrack->isFastTrack()) {
52686dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                continue;
52696dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            }
52706dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten
52716dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            enum {
52726dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                OVERRUN_UNKNOWN,
52736dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                OVERRUN_TRUE,
52746dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                OVERRUN_FALSE
52756dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            } overrun = OVERRUN_UNKNOWN;
52766dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten
52776dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            // loop over getNextBuffer to handle circular sink
52786dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            for (;;) {
52796dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten
52806dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                activeTrack->mSink.frameCount = ~0;
52816dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                status_t status = activeTrack->getNextBuffer(&activeTrack->mSink);
52826dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                size_t framesOut = activeTrack->mSink.frameCount;
52836dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                LOG_ALWAYS_FATAL_IF((status == OK) != (framesOut > 0));
52846dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten
52856dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                int32_t front = activeTrack->mRsmpInFront;
52866dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                ssize_t filled = rear - front;
52876dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                size_t framesIn;
52886dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten
52896dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                if (filled < 0) {
52906dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    // should not happen, but treat like a massive overrun and re-sync
52916dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    framesIn = 0;
52926dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    activeTrack->mRsmpInFront = rear;
52936dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    overrun = OVERRUN_TRUE;
5294607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten                } else if ((size_t) filled <= mRsmpInFrames) {
52956dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    framesIn = (size_t) filled;
52966dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                } else {
52976dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    // client is not keeping up with server, but give it latest data
5298607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten                    framesIn = mRsmpInFrames;
5299607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten                    activeTrack->mRsmpInFront = front = rear - framesIn;
53006dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    overrun = OVERRUN_TRUE;
53016dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                }
53026dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten
53034cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten                if (framesOut == 0 || framesIn == 0) {
53044cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten                    break;
53054cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten                }
53064cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten
53076dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                if (activeTrack->mResampler == NULL) {
53086dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    // no resampling
53096dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    if (framesIn > framesOut) {
53106dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                        framesIn = framesOut;
53116dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    } else {
53126dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                        framesOut = framesIn;
53136dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    }
53146dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    int8_t *dst = activeTrack->mSink.i8;
53156dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    while (framesIn > 0) {
53166dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                        front &= mRsmpInFramesP2 - 1;
53176dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                        size_t part1 = mRsmpInFramesP2 - front;
53186dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                        if (part1 > framesIn) {
53196dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                            part1 = framesIn;
532081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        }
53216dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                        int8_t *src = (int8_t *)mRsmpInBuffer + (front * mFrameSize);
53224cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten                        if (mChannelCount == activeTrack->mChannelCount) {
53236dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                            memcpy(dst, src, part1 * mFrameSize);
53246dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                        } else if (mChannelCount == 1) {
5325cd704219d22ec51660103684a680caf2c3a12dadGlenn Kasten                            upmix_to_stereo_i16_from_mono_i16((int16_t *)dst, (const int16_t *)src,
53266dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                                    part1);
53271ba19cd7fcdf18ab6efab2a1b831affab9a46157Glenn Kasten                        } else {
5328cd704219d22ec51660103684a680caf2c3a12dadGlenn Kasten                            downmix_to_mono_i16_from_stereo_i16((int16_t *)dst, (const int16_t *)src,
53296dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                                    part1);
53301ba19cd7fcdf18ab6efab2a1b831affab9a46157Glenn Kasten                        }
53316dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                        dst += part1 * activeTrack->mFrameSize;
53326dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                        front += part1;
53336dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                        framesIn -= part1;
53341ba19cd7fcdf18ab6efab2a1b831affab9a46157Glenn Kasten                    }
53356dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    activeTrack->mRsmpInFront += framesOut;
53366dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten
53376dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                } else {
53386dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    // resampling
53396dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    // FIXME framesInNeeded should really be part of resampler API, and should
53406dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    //       depend on the SRC ratio
53416dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    //       to keep mRsmpInBuffer full so resampler always has sufficient input
53426dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    size_t framesInNeeded;
53436dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    // FIXME only re-calculate when it changes, and optimize for common ratios
53448661aaff9e9a4d4c1b57f6a68cdbcab006354ab2Andy Hung                    // Do not precompute in/out because floating point is not associative
53458661aaff9e9a4d4c1b57f6a68cdbcab006354ab2Andy Hung                    // e.g. a*b/c != a*(b/c).
53468661aaff9e9a4d4c1b57f6a68cdbcab006354ab2Andy Hung                    const double in(mSampleRate);
53478661aaff9e9a4d4c1b57f6a68cdbcab006354ab2Andy Hung                    const double out(activeTrack->mSampleRate);
53488661aaff9e9a4d4c1b57f6a68cdbcab006354ab2Andy Hung                    framesInNeeded = ceil(framesOut * in / out) + 1;
5349607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten                    ALOGV("need %u frames in to produce %u out given in/out ratio of %.4g",
53508661aaff9e9a4d4c1b57f6a68cdbcab006354ab2Andy Hung                                framesInNeeded, framesOut, in / out);
5351607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten                    // Although we theoretically have framesIn in circular buffer, some of those are
5352607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten                    // unreleased frames, and thus must be discounted for purpose of budgeting.
5353607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten                    size_t unreleased = activeTrack->mRsmpInUnrel;
5354607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten                    framesIn = framesIn > unreleased ? framesIn - unreleased : 0;
53556dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    if (framesIn < framesInNeeded) {
5356607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten                        ALOGV("not enough to resample: have %u frames in but need %u in to "
5357607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten                                "produce %u out given in/out ratio of %.4g",
53588661aaff9e9a4d4c1b57f6a68cdbcab006354ab2Andy Hung                                framesIn, framesInNeeded, framesOut, in / out);
53598661aaff9e9a4d4c1b57f6a68cdbcab006354ab2Andy Hung                        size_t newFramesOut = framesIn > 0 ? floor((framesIn - 1) * out / in) : 0;
5360607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten                        LOG_ALWAYS_FATAL_IF(newFramesOut >= framesOut);
5361607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten                        if (newFramesOut == 0) {
5362607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten                            break;
53634cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten                        }
53648661aaff9e9a4d4c1b57f6a68cdbcab006354ab2Andy Hung                        framesInNeeded = ceil(newFramesOut * in / out) + 1;
5365607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten                        ALOGV("now need %u frames in to produce %u out given out/in ratio of %.4g",
53668661aaff9e9a4d4c1b57f6a68cdbcab006354ab2Andy Hung                                framesInNeeded, newFramesOut, out / in);
5367607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten                        LOG_ALWAYS_FATAL_IF(framesIn < framesInNeeded);
5368607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten                        ALOGV("success 2: have %u frames in and need %u in to produce %u out "
5369607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten                              "given in/out ratio of %.4g",
53708661aaff9e9a4d4c1b57f6a68cdbcab006354ab2Andy Hung                              framesIn, framesInNeeded, newFramesOut, in / out);
5371607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten                        framesOut = newFramesOut;
53724cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten                    } else {
5373607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten                        ALOGV("success 1: have %u in and need %u in to produce %u out "
53744cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten                            "given in/out ratio of %.4g",
53758661aaff9e9a4d4c1b57f6a68cdbcab006354ab2Andy Hung                            framesIn, framesInNeeded, framesOut, in / out);
53768594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten                    }
53776dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten
53786dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    // reallocate mRsmpOutBuffer as needed; we will grow but never shrink
53796dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    if (activeTrack->mRsmpOutFrameCount < framesOut) {
5380607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten                        // FIXME why does each track need it's own mRsmpOutBuffer? can't they share?
53816dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                        delete[] activeTrack->mRsmpOutBuffer;
53826dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                        // resampler always outputs stereo
53836dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                        activeTrack->mRsmpOutBuffer = new int32_t[framesOut * FCC_2];
53846dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                        activeTrack->mRsmpOutFrameCount = framesOut;
53858594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten                    }
53866dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten
53876dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    // resampler accumulates, but we only have one source track
53886dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    memset(activeTrack->mRsmpOutBuffer, 0, framesOut * FCC_2 * sizeof(int32_t));
53896dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    activeTrack->mResampler->resample(activeTrack->mRsmpOutBuffer, framesOut,
5390607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten                            // FIXME how about having activeTrack implement this interface itself?
53916dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                            activeTrack->mResamplerBufferProvider
53926dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                            /*this*/ /* AudioBufferProvider* */);
53936dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    // ditherAndClamp() works as long as all buffers returned by
53946dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    // activeTrack->getNextBuffer() are 32 bit aligned which should be always true.
53954cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten                    if (activeTrack->mChannelCount == 1) {
539684a0c6e87c48f58a0d3be71961432c086a4d24ccAndy Hung                        // temporarily type pun mRsmpOutBuffer from Q4.27 to int16_t
53976dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                        ditherAndClamp(activeTrack->mRsmpOutBuffer, activeTrack->mRsmpOutBuffer,
53986dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                                framesOut);
53996dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                        // the resampler always outputs stereo samples:
54006dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                        // do post stereo to mono conversion
54016dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                        downmix_to_mono_i16_from_stereo_i16(activeTrack->mSink.i16,
5402cd704219d22ec51660103684a680caf2c3a12dadGlenn Kasten                                (const int16_t *)activeTrack->mRsmpOutBuffer, framesOut);
54036dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    } else {
54046dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                        ditherAndClamp((int32_t *)activeTrack->mSink.raw,
54056dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                                activeTrack->mRsmpOutBuffer, framesOut);
54068594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten                    }
54076dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    // now done with mRsmpOutBuffer
54088594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten
54098594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten                }
54108594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten
54116dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                if (framesOut > 0 && (overrun == OVERRUN_UNKNOWN)) {
54126dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    overrun = OVERRUN_FALSE;
54131ba19cd7fcdf18ab6efab2a1b831affab9a46157Glenn Kasten                }
541481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
54156dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                if (activeTrack->mFramesToDrop == 0) {
54166dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    if (framesOut > 0) {
54176dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                        activeTrack->mSink.frameCount = framesOut;
54186dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                        activeTrack->releaseBuffer(&activeTrack->mSink);
541981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    }
542081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                } else {
54216dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    // FIXME could do a partial drop of framesOut
54226dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    if (activeTrack->mFramesToDrop > 0) {
54236dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                        activeTrack->mFramesToDrop -= framesOut;
54246dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                        if (activeTrack->mFramesToDrop <= 0) {
542525f4aa83efaa9179e65a20583a6d441de2c3ff3fGlenn Kasten                            activeTrack->clearSyncStartEvent();
54266dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                        }
54276dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    } else {
54286dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                        activeTrack->mFramesToDrop += framesOut;
54296dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                        if (activeTrack->mFramesToDrop >= 0 || activeTrack->mSyncStartEvent == 0 ||
54306dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                                activeTrack->mSyncStartEvent->isCancelled()) {
54316dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                            ALOGW("Synced record %s, session %d, trigger session %d",
54326dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                                  (activeTrack->mFramesToDrop >= 0) ? "timed out" : "cancelled",
54336dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                                  activeTrack->sessionId(),
54346dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                                  (activeTrack->mSyncStartEvent != 0) ?
54356dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                                          activeTrack->mSyncStartEvent->triggerSession() : 0);
543625f4aa83efaa9179e65a20583a6d441de2c3ff3fGlenn Kasten                            activeTrack->clearSyncStartEvent();
54376dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                        }
543881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    }
543981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
54406dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten
54416dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                if (framesOut == 0) {
54426dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    break;
54436dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                }
544481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
54456dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten
54466dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            switch (overrun) {
54476dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            case OVERRUN_TRUE:
54486dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                // client isn't retrieving buffers fast enough
54496dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                if (!activeTrack->setOverflow()) {
54506dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    nsecs_t now = systemTime();
54516dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    // FIXME should lastWarning per track?
54526dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    if ((now - lastWarning) > kWarningThrottleNs) {
54536dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                        ALOGW("RecordThread: buffer overflow");
54546dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                        lastWarning = now;
54556dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                    }
545681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
54576dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                break;
54586dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            case OVERRUN_FALSE:
54596dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                activeTrack->clearOverflow();
54606dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                break;
54616dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            case OVERRUN_UNKNOWN:
54626dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                break;
546381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
54646dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten
546581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
54661ba19cd7fcdf18ab6efab2a1b831affab9a46157Glenn Kasten
54673d61bc1ffc8afc8d7be3b0d4205c9b5ba6daf2e8Glenn Kastenunlock:
546881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // enable changes in effect chain
546981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        unlockEffectChains(effectChains);
5470c527a7c2b1bfd26e8f3086e1b653d56e521379d9Glenn Kasten        // effectChains doesn't need to be cleared, since it is cleared by destructor at scope end
547181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
547281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
547393e471f620454f7de73d190521568b1e25879767Glenn Kasten    standbyIfNotAlreadyInStandby();
547481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
547581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    {
547681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        Mutex::Autolock _l(mLock);
54779a54bc27876acd5d8be5b1fc3dc46701fe76fbb3Eric Laurent        for (size_t i = 0; i < mTracks.size(); i++) {
54789a54bc27876acd5d8be5b1fc3dc46701fe76fbb3Eric Laurent            sp<RecordTrack> track = mTracks[i];
54799a54bc27876acd5d8be5b1fc3dc46701fe76fbb3Eric Laurent            track->invalidate();
54809a54bc27876acd5d8be5b1fc3dc46701fe76fbb3Eric Laurent        }
54812b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten        mActiveTracks.clear();
54822b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten        mActiveTracksGen++;
548381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mStartStopCond.broadcast();
548481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
548581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
548681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    releaseWakeLock();
548781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
548881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("RecordThread %p exiting", this);
548981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return false;
549081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
549181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
549293e471f620454f7de73d190521568b1e25879767Glenn Kastenvoid AudioFlinger::RecordThread::standbyIfNotAlreadyInStandby()
549381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
549481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (!mStandby) {
549581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        inputStandBy();
549681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mStandby = true;
549781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
549881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
549981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
550081784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::RecordThread::inputStandBy()
550181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
55026dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    // Idle the fast capture if it's currently running
55036dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    if (mFastCapture != 0) {
55046dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        FastCaptureStateQueue *sq = mFastCapture->sq();
55056dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        FastCaptureState *state = sq->begin();
55066dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        if (!(state->mCommand & FastCaptureState::IDLE)) {
55076dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            state->mCommand = FastCaptureState::COLD_IDLE;
55086dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            state->mColdFutexAddr = &mFastCaptureFutex;
55096dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            state->mColdGen++;
55106dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            mFastCaptureFutex = 0;
55116dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            sq->end();
55126dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // BLOCK_UNTIL_PUSHED would be insufficient, as we need it to stop doing I/O now
55136dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            sq->push(FastCaptureStateQueue::BLOCK_UNTIL_ACKED);
55146dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#if 0
55156dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            if (kUseFastCapture == FastCapture_Dynamic) {
55166dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                // FIXME
55176dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            }
55186dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#endif
55196dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#ifdef AUDIO_WATCHDOG
55206dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // FIXME
55216dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten#endif
55226dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        } else {
55236dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            sq->end(false /*didModify*/);
55246dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        }
55256dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    }
552681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mInput->stream->common.standby(&mInput->stream->common);
552781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
552881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
552905997e21af6c4517f375def6563af4b9ebe95f39Glenn Kasten// RecordThread::createRecordTrack_l() must be called with AudioFlinger::mLock held
5530e198c360d5e75a9b2097844c495c10902e7e8500Glenn Kastensp<AudioFlinger::RecordThread::RecordTrack> AudioFlinger::RecordThread::createRecordTrack_l(
553181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        const sp<AudioFlinger::Client>& client,
553281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        uint32_t sampleRate,
553381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        audio_format_t format,
553481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        audio_channel_mask_t channelMask,
553574935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten        size_t *pFrameCount,
553681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        int sessionId,
55377df8c0b799d8f52d6386e03313286dbd7d5cdc7cGlenn Kasten        size_t *notificationFrames,
5538462fd2fa9eef642b0574aa7409de0bde3fec8d43Marco Nelissen        int uid,
5539ddb0ccf3fb6fe8da8c71a6deb30561b821f3c0a2Glenn Kasten        IAudioFlinger::track_flags_t *flags,
554081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        pid_t tid,
554181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        status_t *status)
554281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
554374935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten    size_t frameCount = *pFrameCount;
554481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<RecordTrack> track;
554581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t lStatus;
554681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
554790e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten    // client expresses a preference for FAST, but we get the final say
554890e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten    if (*flags & IAudioFlinger::TRACK_FAST) {
554990e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten      if (
55507410591dad836434c72ddee66680802708b70c10Glenn Kasten            // use case: callback handler
55517410591dad836434c72ddee66680802708b70c10Glenn Kasten            (tid != -1) &&
55527410591dad836434c72ddee66680802708b70c10Glenn Kasten            // frame count is not specified, or is exactly the pipe depth
55537410591dad836434c72ddee66680802708b70c10Glenn Kasten            ((frameCount == 0) || (frameCount == mPipeFramesP2)) &&
55543a6c90aa0617666d9abc94c02b752d9eb3d64772Glenn Kasten            // PCM data
55553a6c90aa0617666d9abc94c02b752d9eb3d64772Glenn Kasten            audio_is_linear_pcm(format) &&
55566dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // native format
55576dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            (format == mFormat) &&
55586dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // native channel mask
55596dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            (channelMask == mChannelMask) &&
55606dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // native hardware sample rate
556190e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten            (sampleRate == mSampleRate) &&
55623a6c90aa0617666d9abc94c02b752d9eb3d64772Glenn Kasten            // record thread has an associated fast capture
55636dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            hasFastCapture() &&
55646dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            // there are sufficient fast track slots available
55656dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten            mFastTrackAvail
556690e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten        ) {
55677410591dad836434c72ddee66680802708b70c10Glenn Kasten        ALOGV("AUDIO_INPUT_FLAG_FAST accepted: frameCount=%u mFrameCount=%u",
556890e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten                frameCount, mFrameCount);
556990e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten      } else {
55707410591dad836434c72ddee66680802708b70c10Glenn Kasten        ALOGV("AUDIO_INPUT_FLAG_FAST denied: frameCount=%u mFrameCount=%u mPipeFramesP2=%u "
55717410591dad836434c72ddee66680802708b70c10Glenn Kasten                "format=%#x isLinear=%d channelMask=%#x sampleRate=%u mSampleRate=%u "
55726dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten                "hasFastCapture=%d tid=%d mFastTrackAvail=%d",
55737410591dad836434c72ddee66680802708b70c10Glenn Kasten                frameCount, mFrameCount, mPipeFramesP2,
55747410591dad836434c72ddee66680802708b70c10Glenn Kasten                format, audio_is_linear_pcm(format), channelMask, sampleRate, mSampleRate,
55757410591dad836434c72ddee66680802708b70c10Glenn Kasten                hasFastCapture(), tid, mFastTrackAvail);
557690e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten        *flags &= ~IAudioFlinger::TRACK_FAST;
55777410591dad836434c72ddee66680802708b70c10Glenn Kasten      }
55787410591dad836434c72ddee66680802708b70c10Glenn Kasten    }
55797410591dad836434c72ddee66680802708b70c10Glenn Kasten
55807410591dad836434c72ddee66680802708b70c10Glenn Kasten    // compute track buffer size in frames, and suggest the notification frame count
55817410591dad836434c72ddee66680802708b70c10Glenn Kasten    if (*flags & IAudioFlinger::TRACK_FAST) {
55827410591dad836434c72ddee66680802708b70c10Glenn Kasten        // fast track: frame count is exactly the pipe depth
55837410591dad836434c72ddee66680802708b70c10Glenn Kasten        frameCount = mPipeFramesP2;
55847410591dad836434c72ddee66680802708b70c10Glenn Kasten        // ignore requested notificationFrames, and always notify exactly once every HAL buffer
55857410591dad836434c72ddee66680802708b70c10Glenn Kasten        *notificationFrames = mFrameCount;
55867410591dad836434c72ddee66680802708b70c10Glenn Kasten    } else {
558749d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten        // not fast track: max notification period is resampled equivalent of one HAL buffer time
558849d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten        //                 or 20 ms if there is a fast capture
558949d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten        // TODO This could be a roundupRatio inline, and const
559049d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten        size_t maxNotificationFrames = ((int64_t) (hasFastCapture() ? mSampleRate/50 : mFrameCount)
559149d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten                * sampleRate + mSampleRate - 1) / mSampleRate;
559249d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten        // minimum number of notification periods is at least kMinNotifications,
559349d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten        // and at least kMinMs rounded up to a whole notification period (minNotificationsByMs)
559449d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten        static const size_t kMinNotifications = 3;
559549d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten        static const uint32_t kMinMs = 30;
559649d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten        // TODO This could be a roundupRatio inline
559749d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten        const size_t minFramesByMs = (sampleRate * kMinMs + 1000 - 1) / 1000;
559849d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten        // TODO This could be a roundupRatio inline
559949d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten        const size_t minNotificationsByMs = (minFramesByMs + maxNotificationFrames - 1) /
560049d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten                maxNotificationFrames;
560149d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten        const size_t minFrameCount = maxNotificationFrames *
560249d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten                max(kMinNotifications, minNotificationsByMs);
560349d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten        frameCount = max(frameCount, minFrameCount);
560449d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten        if (*notificationFrames == 0 || *notificationFrames > maxNotificationFrames) {
560549d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten            *notificationFrames = maxNotificationFrames;
56067410591dad836434c72ddee66680802708b70c10Glenn Kasten        }
560790e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten    }
560874935e44734c1ec235c2b6677db3e0dbefa5ddb8Glenn Kasten    *pFrameCount = frameCount;
560990e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten
561015e5798908ccac14e10c84834eaf08c42931bd06Glenn Kasten    lStatus = initCheck();
561115e5798908ccac14e10c84834eaf08c42931bd06Glenn Kasten    if (lStatus != NO_ERROR) {
561215e5798908ccac14e10c84834eaf08c42931bd06Glenn Kasten        ALOGE("createRecordTrack_l() audio driver not initialized");
561315e5798908ccac14e10c84834eaf08c42931bd06Glenn Kasten        goto Exit;
561415e5798908ccac14e10c84834eaf08c42931bd06Glenn Kasten    }
561581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
561681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    { // scope for mLock
561781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        Mutex::Autolock _l(mLock);
561881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
561981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        track = new RecordTrack(this, client, sampleRate,
562083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent                      format, channelMask, frameCount, NULL, sessionId, uid,
562183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent                      *flags, TrackBase::TYPE_DEFAULT);
562281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
5623030033342a6ea17003e6af38a56c7edc6d2ead01Glenn Kasten        lStatus = track->initCheck();
5624030033342a6ea17003e6af38a56c7edc6d2ead01Glenn Kasten        if (lStatus != NO_ERROR) {
562535295078ab59c8c5d143a54d5a55557c3ca62c51Glenn Kasten            ALOGE("createRecordTrack_l() initCheck failed %d; no control block?", lStatus);
562603e9e83c47ab4a518da0a1f36b8f702f59221c95Haynes Mathew George            // track must be cleared from the caller as the caller has the AF lock
562781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            goto Exit;
562881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
562981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mTracks.add(track);
563081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
563181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // disable AEC and NS if the device is a BT SCO headset supporting those pre processings
563281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        bool suspend = audio_is_bluetooth_sco_device(mInDevice) &&
563381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                        mAudioFlinger->btNrecIsOff();
563481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        setEffectSuspended_l(FX_IID_AEC, suspend, sessionId);
563581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        setEffectSuspended_l(FX_IID_NS, suspend, sessionId);
563690e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten
563790e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten        if ((*flags & IAudioFlinger::TRACK_FAST) && (tid != -1)) {
563890e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten            pid_t callingPid = IPCThreadState::self()->getCallingPid();
563990e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten            // we don't have CAP_SYS_NICE, nor do we want to have it as it's too powerful,
564090e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten            // so ask activity manager to do this on our behalf
564190e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten            sendPrioConfigEvent_l(callingPid, tid, kPriorityAudioApp);
564290e58b1fefc8caf70b34301a92bc86179580b6fcGlenn Kasten        }
564381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
564405997e21af6c4517f375def6563af4b9ebe95f39Glenn Kasten
564581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    lStatus = NO_ERROR;
564681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
564781784c37c61b09289654b979567a42bf73cd2b12Eric LaurentExit:
56489156ef3e11b68cc4b6d3cea77f1f63673855a6d1Glenn Kasten    *status = lStatus;
564981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return track;
565081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
565181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
565281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::RecordThread::start(RecordThread::RecordTrack* recordTrack,
565381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                           AudioSystem::sync_event_t event,
565481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                           int triggerSession)
565581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
565681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("RecordThread::start event %d, triggerSession %d", event, triggerSession);
565781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<ThreadBase> strongMe = this;
565881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t status = NO_ERROR;
565981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
566081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (event == AudioSystem::SYNC_EVENT_NONE) {
566125f4aa83efaa9179e65a20583a6d441de2c3ff3fGlenn Kasten        recordTrack->clearSyncStartEvent();
566281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    } else if (event != AudioSystem::SYNC_EVENT_SAME) {
56636dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        recordTrack->mSyncStartEvent = mAudioFlinger->createSyncEvent(event,
566481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                       triggerSession,
566581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                       recordTrack->sessionId(),
566681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                                       syncStartEventCallback,
56676dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                                       recordTrack);
566881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // Sync event can be cancelled by the trigger session if the track is not in a
566981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // compatible state in which case we start record immediately
56706dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        if (recordTrack->mSyncStartEvent->isCancelled()) {
567125f4aa83efaa9179e65a20583a6d441de2c3ff3fGlenn Kasten            recordTrack->clearSyncStartEvent();
567281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        } else {
567381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            // do not wait for the event for more than AudioSystem::kSyncRecordStartTimeOutMs
56746dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            recordTrack->mFramesToDrop = -
56754cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten                    ((AudioSystem::kSyncRecordStartTimeOutMs * recordTrack->mSampleRate) / 1000);
567681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
567781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
567881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
567981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    {
568047c2070b2ce8aedb7300c0aad91caccf3c383841Glenn Kasten        // This section is a rendezvous between binder thread executing start() and RecordThread
568181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        AutoMutex lock(mLock);
56826dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        if (mActiveTracks.indexOf(recordTrack) >= 0) {
56836dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            if (recordTrack->mState == TrackBase::PAUSING) {
56846dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                ALOGV("active record track PAUSING -> ACTIVE");
5685f10ffec18f930d92e1abe9200d60e746831841a7Glenn Kasten                recordTrack->mState = TrackBase::ACTIVE;
56866dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            } else {
56876dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten                ALOGV("active record track state %d", recordTrack->mState);
568881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
568981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            return status;
569081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
569181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
56924cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten        // TODO consider other ways of handling this, such as changing the state to :STARTING and
56934cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten        //      adding the track to mActiveTracks after returning from AudioSystem::startInput(),
56944cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten        //      or using a separate command thread
56956dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        recordTrack->mState = TrackBase::STARTING_1;
56962b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten        mActiveTracks.add(recordTrack);
56972b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten        mActiveTracksGen++;
569883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent        status_t status = NO_ERROR;
569983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent        if (recordTrack->isExternalTrack()) {
570083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent            mLock.unlock();
57014dc680607181e6a76f4e91a39366c4f5dfb7b03eEric Laurent            status = AudioSystem::startInput(mId, (audio_session_t)recordTrack->sessionId());
570283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent            mLock.lock();
570383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent            // FIXME should verify that recordTrack is still in mActiveTracks
570483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent            if (status != NO_ERROR) {
570583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent                mActiveTracks.remove(recordTrack);
570683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent                mActiveTracksGen++;
570783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent                recordTrack->clearSyncStartEvent();
570883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent                ALOGV("RecordThread::start error %d", status);
570983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent                return status;
571083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent            }
571181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
57126dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        // Catch up with current buffer indices if thread is already running.
57136dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        // This is what makes a new client discard all buffered data.  If the track's mRsmpInFront
57146dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        // was initialized to some value closer to the thread's mRsmpInFront, then the track could
57156dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        // see previously buffered data before it called start(), but with greater risk of overrun.
57166dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten
57176dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        recordTrack->mRsmpInFront = mRsmpInRear;
57186dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        recordTrack->mRsmpInUnrel = 0;
57196dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        // FIXME why reset?
57206dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        if (recordTrack->mResampler != NULL) {
57216dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten            recordTrack->mResampler->reset();
57226dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        }
57236dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        recordTrack->mState = TrackBase::STARTING_2;
572481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        // signal thread to start
572581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mWaitWorkCV.broadcast();
57262b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten        if (mActiveTracks.indexOf(recordTrack) < 0) {
572781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ALOGV("Record failed to start");
572881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            status = BAD_VALUE;
572981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            goto startError;
573081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
573181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return status;
573281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
57337c027248e1a4ccd5b22bc4deafb03e2d87ac8f38Glenn Kasten
573481784c37c61b09289654b979567a42bf73cd2b12Eric LaurentstartError:
573583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    if (recordTrack->isExternalTrack()) {
57364dc680607181e6a76f4e91a39366c4f5dfb7b03eEric Laurent        AudioSystem::stopInput(mId, (audio_session_t)recordTrack->sessionId());
573783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    }
573825f4aa83efaa9179e65a20583a6d441de2c3ff3fGlenn Kasten    recordTrack->clearSyncStartEvent();
57396dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    // FIXME I wonder why we do not reset the state here?
574081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return status;
574181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
574281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
574381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::RecordThread::syncStartEventCallback(const wp<SyncEvent>& event)
574481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
574581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    sp<SyncEvent> strongEvent = event.promote();
574681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
574781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (strongEvent != 0) {
57488ea16e4b0a7d398d26887c18675b3899de5d779dEric Laurent        sp<RefBase> ptr = strongEvent->cookie().promote();
57498ea16e4b0a7d398d26887c18675b3899de5d779dEric Laurent        if (ptr != 0) {
57508ea16e4b0a7d398d26887c18675b3899de5d779dEric Laurent            RecordTrack *recordTrack = (RecordTrack *)ptr.get();
57518ea16e4b0a7d398d26887c18675b3899de5d779dEric Laurent            recordTrack->handleSyncStartEvent(strongEvent);
57528ea16e4b0a7d398d26887c18675b3899de5d779dEric Laurent        }
575381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
575481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
575581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
5756a8356f663014e7d4c27869629af83d8bb3441e19Glenn Kastenbool AudioFlinger::RecordThread::stop(RecordThread::RecordTrack* recordTrack) {
575781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("RecordThread::stop");
5758a8356f663014e7d4c27869629af83d8bb3441e19Glenn Kasten    AutoMutex _l(mLock);
57592b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten    if (mActiveTracks.indexOf(recordTrack) != 0 || recordTrack->mState == TrackBase::PAUSING) {
576081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return false;
576181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
576247c2070b2ce8aedb7300c0aad91caccf3c383841Glenn Kasten    // note that threadLoop may still be processing the track at this point [without lock]
576381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    recordTrack->mState = TrackBase::PAUSING;
576481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // do not wait for mStartStopCond if exiting
576581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (exitPending()) {
576681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return true;
576781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
576847c2070b2ce8aedb7300c0aad91caccf3c383841Glenn Kasten    // FIXME incorrect usage of wait: no explicit predicate or loop
576981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mStartStopCond.wait(mLock);
57702b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten    // if we have been restarted, recordTrack is in mActiveTracks here
57712b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten    if (exitPending() || mActiveTracks.indexOf(recordTrack) != 0) {
577281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        ALOGV("Record stopped OK");
577381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return true;
577481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
577581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return false;
577681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
577781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
57780f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenbool AudioFlinger::RecordThread::isValidSyncEvent(const sp<SyncEvent>& event __unused) const
577981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
578081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return false;
578181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
578281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
57830f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenstatus_t AudioFlinger::RecordThread::setSyncEvent(const sp<SyncEvent>& event __unused)
578481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
578581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#if 0   // This branch is currently dead code, but is preserved in case it will be needed in future
578681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (!isValidSyncEvent(event)) {
578781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return BAD_VALUE;
578881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
578981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
579081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    int eventSession = event->triggerSession();
579181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    status_t ret = NAME_NOT_FOUND;
579281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
579381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
579481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
579581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mTracks.size(); i++) {
579681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<RecordTrack> track = mTracks[i];
579781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (eventSession == track->sessionId()) {
579881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            (void) track->setSyncEvent(event);
579981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ret = NO_ERROR;
580081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
580181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
580281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return ret;
580381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#else
580481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return BAD_VALUE;
580581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent#endif
580681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
580781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
580881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// destroyTrack_l() must be called with ThreadBase::mLock held
580981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::RecordThread::destroyTrack_l(const sp<RecordTrack>& track)
581081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
5811bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    track->terminate();
5812bfb1b832079bbb9426f72f3863199a54aefd02daEric Laurent    track->mState = TrackBase::STOPPED;
581381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // active tracks are removed by threadLoop()
58142b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten    if (mActiveTracks.indexOf(track) < 0) {
581581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        removeTrack_l(track);
581681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
581781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
581881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
581981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::RecordThread::removeTrack_l(const sp<RecordTrack>& track)
582081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
582181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mTracks.remove(track);
582281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // need anything related to effects here?
58236dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    if (track->isFastTrack()) {
58246dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        ALOG_ASSERT(!mFastTrackAvail);
58256dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten        mFastTrackAvail = true;
58266dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    }
582781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
582881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
582981784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::RecordThread::dump(int fd, const Vector<String16>& args)
583081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
583181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    dumpInternals(fd, args);
583281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    dumpTracks(fd, args);
583381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    dumpEffectChains(fd, args);
583481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
583581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
583681784c37c61b09289654b979567a42bf73cd2b12Eric Laurentvoid AudioFlinger::RecordThread::dumpInternals(int fd, const Vector<String16>& args)
583781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
583887cebadd48710e42474756fc3513df678de045ceElliott Hughes    dprintf(fd, "\nInput thread %p:\n", this);
583981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
58402b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten    if (mActiveTracks.size() > 0) {
584187cebadd48710e42474756fc3513df678de045ceElliott Hughes        dprintf(fd, "  Buffer size: %zu bytes\n", mBufferSize);
584281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    } else {
584387cebadd48710e42474756fc3513df678de045ceElliott Hughes        dprintf(fd, "  No active record clients\n");
584481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
58456e6704c06d61bc356e30c164081e5bcffb37920cGlenn Kasten    dprintf(fd, "  Fast capture thread: %s\n", hasFastCapture() ? "yes" : "no");
58466dbb5e3336cfff1ad51d429fcb847307c06efd61Glenn Kasten    dprintf(fd, "  Fast track available: %s\n", mFastTrackAvail ? "yes" : "no");
584781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
584881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    dumpBase(fd, args);
584981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
585081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
58510f11b51a57bc9062c4fe8af73747319cedabc5d6Glenn Kastenvoid AudioFlinger::RecordThread::dumpTracks(int fd, const Vector<String16>& args __unused)
585281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
585381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    const size_t SIZE = 256;
585481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    char buffer[SIZE];
585581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    String8 result;
585681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
5857b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    size_t numtracks = mTracks.size();
5858b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    size_t numactive = mActiveTracks.size();
5859b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    size_t numactiveseen = 0;
586087cebadd48710e42474756fc3513df678de045ceElliott Hughes    dprintf(fd, "  %d Tracks", numtracks);
5861b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    if (numtracks) {
586287cebadd48710e42474756fc3513df678de045ceElliott Hughes        dprintf(fd, " of which %d are active\n", numactive);
5863b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        RecordTrack::appendDumpHeader(result);
5864b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        for (size_t i = 0; i < numtracks ; ++i) {
5865b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen            sp<RecordTrack> track = mTracks[i];
5866b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen            if (track != 0) {
5867b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen                bool active = mActiveTracks.indexOf(track) >= 0;
5868b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen                if (active) {
5869b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen                    numactiveseen++;
5870b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen                }
5871b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen                track->dump(buffer, SIZE, active);
5872b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen                result.append(buffer);
5873b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen            }
587481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
5875b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    } else {
587687cebadd48710e42474756fc3513df678de045ceElliott Hughes        dprintf(fd, "\n");
587781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
587881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
5879b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen    if (numactiveseen != numactive) {
5880b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        snprintf(buffer, SIZE, "  The following tracks are in the active list but"
5881b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen                " not in the track list\n");
588281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        result.append(buffer);
588381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        RecordTrack::appendDumpHeader(result);
5884b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen        for (size_t i = 0; i < numactive; ++i) {
58852b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten            sp<RecordTrack> track = mActiveTracks[i];
5886b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen            if (mTracks.indexOf(track) < 0) {
5887b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen                track->dump(buffer, SIZE, true);
5888b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen                result.append(buffer);
5889b220884bf3129253cc5bc8d030bc475411ea4911Marco Nelissen            }
58902b806406ac1ec680b6fe3aaa84c54bdc4e43ad8dGlenn Kasten        }
589181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
589281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
589381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    write(fd, result.string(), result.size());
589481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
589581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
589681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// AudioBufferProvider interface
58976dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kastenstatus_t AudioFlinger::RecordThread::ResamplerBufferProvider::getNextBuffer(
58986dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        AudioBufferProvider::Buffer* buffer, int64_t pts __unused)
589981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
59006dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    RecordTrack *activeTrack = mRecordTrack;
59016dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    sp<ThreadBase> threadBase = activeTrack->mThread.promote();
59026dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    if (threadBase == 0) {
59036dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        buffer->frameCount = 0;
5904607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten        buffer->raw = NULL;
59056dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        return NOT_ENOUGH_DATA;
59066dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    }
59076dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    RecordThread *recordThread = (RecordThread *) threadBase.get();
59086dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    int32_t rear = recordThread->mRsmpInRear;
59096dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    int32_t front = activeTrack->mRsmpInFront;
59108594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten    ssize_t filled = rear - front;
59116dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    // FIXME should not be P2 (don't want to increase latency)
59126dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    // FIXME if client not keeping up, discard
5913607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten    LOG_ALWAYS_FATAL_IF(!(0 <= filled && (size_t) filled <= recordThread->mRsmpInFrames));
59148594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten    // 'filled' may be non-contiguous, so return only the first contiguous chunk
59156dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    front &= recordThread->mRsmpInFramesP2 - 1;
59166dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    size_t part1 = recordThread->mRsmpInFramesP2 - front;
59178594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten    if (part1 > (size_t) filled) {
59188594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten        part1 = filled;
59198594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten    }
59208594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten    size_t ask = buffer->frameCount;
59218594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten    ALOG_ASSERT(ask > 0);
59228594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten    if (part1 > ask) {
59238594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten        part1 = ask;
59248594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten    }
59258594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten    if (part1 == 0) {
59268594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten        // Higher-level should keep mRsmpInBuffer full, and not call resampler if empty
5927607fa3e928de696eba49f198af72d68e4591ca1bGlenn Kasten        LOG_ALWAYS_FATAL("RecordThread::getNextBuffer() starved");
59288594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten        buffer->raw = NULL;
59298594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten        buffer->frameCount = 0;
59306dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        activeTrack->mRsmpInUnrel = 0;
59318594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten        return NOT_ENOUGH_DATA;
59328594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten    }
59338594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten
59346dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    buffer->raw = recordThread->mRsmpInBuffer + front * recordThread->mChannelCount;
59358594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten    buffer->frameCount = part1;
59366dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    activeTrack->mRsmpInUnrel = part1;
593781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return NO_ERROR;
593881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
593981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
594081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// AudioBufferProvider interface
59416dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kastenvoid AudioFlinger::RecordThread::ResamplerBufferProvider::releaseBuffer(
59426dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten        AudioBufferProvider::Buffer* buffer)
594381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
59446dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    RecordTrack *activeTrack = mRecordTrack;
59458594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten    size_t stepCount = buffer->frameCount;
59468594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten    if (stepCount == 0) {
59478594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten        return;
59488594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten    }
59496dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    ALOG_ASSERT(stepCount <= activeTrack->mRsmpInUnrel);
59506dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    activeTrack->mRsmpInUnrel -= stepCount;
59516dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    activeTrack->mRsmpInFront += stepCount;
59528594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten    buffer->raw = NULL;
595381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    buffer->frameCount = 0;
595481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
595581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
59561035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurentbool AudioFlinger::RecordThread::checkForNewParameter_l(const String8& keyValuePair,
59571035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                                                        status_t& status)
595881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
595981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    bool reconfig = false;
596081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
59611035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    status = NO_ERROR;
59621035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent
59631035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    audio_format_t reqFormat = mFormat;
59641035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    uint32_t samplingRate = mSampleRate;
59651035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    audio_channel_mask_t channelMask = audio_channel_in_mask_from_count(mChannelCount);
59661035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent
59671035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    AudioParameter param = AudioParameter(keyValuePair);
59681035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    int value;
59691035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    // TODO Investigate when this code runs. Check with audio policy when a sample rate and
59701035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    //      channel count change can be requested. Do we mandate the first client defines the
59711035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    //      HAL sampling rate and channel count or do we allow changes on the fly?
59721035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    if (param.getInt(String8(AudioParameter::keySamplingRate), value) == NO_ERROR) {
59731035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        samplingRate = value;
59741035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        reconfig = true;
59751035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    }
59761035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    if (param.getInt(String8(AudioParameter::keyFormat), value) == NO_ERROR) {
59771035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        if ((audio_format_t) value != AUDIO_FORMAT_PCM_16_BIT) {
59781035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            status = BAD_VALUE;
59791035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        } else {
59801035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            reqFormat = (audio_format_t) value;
598181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            reconfig = true;
598281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
59831035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    }
59841035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    if (param.getInt(String8(AudioParameter::keyChannels), value) == NO_ERROR) {
59851035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        audio_channel_mask_t mask = (audio_channel_mask_t) value;
59861035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        if (mask != AUDIO_CHANNEL_IN_MONO && mask != AUDIO_CHANNEL_IN_STEREO) {
59871035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            status = BAD_VALUE;
59881035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        } else {
59891035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            channelMask = mask;
59901035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            reconfig = true;
599181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
59921035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    }
59931035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    if (param.getInt(String8(AudioParameter::keyFrameCount), value) == NO_ERROR) {
59941035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        // do not accept frame count changes if tracks are open as the track buffer
59951035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        // size depends on frame count and correct behavior would not be guaranteed
59961035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        // if frame count is changed after track creation
59971035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        if (mActiveTracks.size() > 0) {
59981035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            status = INVALID_OPERATION;
59991035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        } else {
60001035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            reconfig = true;
600181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
60021035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    }
60031035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    if (param.getInt(String8(AudioParameter::keyRouting), value) == NO_ERROR) {
60041035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        // forward device change to effects that have requested to be
60051035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        // aware of attached audio device.
60061035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        for (size_t i = 0; i < mEffectChains.size(); i++) {
60071035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            mEffectChains[i]->setDevice_l(value);
600881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
600981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
60101035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        // store input device and output device but do not forward output device to audio HAL.
60111035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        // Note that status is ignored by the caller for output device
60121035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        // (see AudioFlinger::setParameters()
60131035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        if (audio_is_output_devices(value)) {
60141035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            mOutDevice = value;
60151035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            status = BAD_VALUE;
60161035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        } else {
60171035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            mInDevice = value;
60181035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            // disable AEC and NS if the device is a BT SCO headset supporting those
60191035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            // pre processings
60201035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            if (mTracks.size() > 0) {
60211035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                bool suspend = audio_is_bluetooth_sco_device(mInDevice) &&
60221035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                                    mAudioFlinger->btNrecIsOff();
60231035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                for (size_t i = 0; i < mTracks.size(); i++) {
60241035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                    sp<RecordTrack> track = mTracks[i];
60251035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                    setEffectSuspended_l(FX_IID_AEC, suspend, track->sessionId());
60261035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                    setEffectSuspended_l(FX_IID_NS, suspend, track->sessionId());
602781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                }
602881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
602981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
60301035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    }
60311035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    if (param.getInt(String8(AudioParameter::keyInputSource), value) == NO_ERROR &&
60321035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            mAudioSource != (audio_source_t)value) {
60331035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        // forward device change to effects that have requested to be
60341035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        // aware of attached audio device.
60351035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        for (size_t i = 0; i < mEffectChains.size(); i++) {
60361035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            mEffectChains[i]->setAudioSource_l((audio_source_t)value);
603781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
60381035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        mAudioSource = (audio_source_t)value;
60391035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    }
6040e198c360d5e75a9b2097844c495c10902e7e8500Glenn Kasten
60411035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent    if (status == NO_ERROR) {
60421035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        status = mInput->stream->common.set_parameters(&mInput->stream->common,
60431035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                keyValuePair.string());
60441035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        if (status == INVALID_OPERATION) {
60451035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            inputStandBy();
604681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            status = mInput->stream->common.set_parameters(&mInput->stream->common,
604781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent                    keyValuePair.string());
60481035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        }
60491035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent        if (reconfig) {
60501035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            if (status == BAD_VALUE &&
60511035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                reqFormat == mInput->stream->common.get_format(&mInput->stream->common) &&
60521035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                reqFormat == AUDIO_FORMAT_PCM_16_BIT &&
60531035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                (mInput->stream->common.get_sample_rate(&mInput->stream->common)
60541035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                        <= (2 * samplingRate)) &&
6055e541269be94f3a1072932d51537905b120ef4733Andy Hung                audio_channel_count_from_in_mask(
6056e541269be94f3a1072932d51537905b120ef4733Andy Hung                        mInput->stream->common.get_channels(&mInput->stream->common)) <= FCC_2 &&
60571035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                (channelMask == AUDIO_CHANNEL_IN_MONO ||
60581035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                        channelMask == AUDIO_CHANNEL_IN_STEREO)) {
60591035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                status = NO_ERROR;
606081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
60611035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent            if (status == NO_ERROR) {
60621035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                readInputParameters_l();
60631035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent                sendIoConfigEvent_l(AudioSystem::INPUT_CONFIG_CHANGED);
606481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            }
606581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
606681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
60671035194cee4fbd57e35ea15c56e66cd09b63d56eEric Laurent
606881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return reconfig;
606981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
607081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
607181784c37c61b09289654b979567a42bf73cd2b12Eric LaurentString8 AudioFlinger::RecordThread::getParameters(const String8& keys)
607281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
607381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
607481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (initCheck() != NO_ERROR) {
6075d8ea699dc8e7dac58bb32e9cdb31b0758da25817Glenn Kasten        return String8();
607681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
607781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
6078d8ea699dc8e7dac58bb32e9cdb31b0758da25817Glenn Kasten    char *s = mInput->stream->common.get_parameters(&mInput->stream->common, keys.string());
6079d8ea699dc8e7dac58bb32e9cdb31b0758da25817Glenn Kasten    const String8 out_s8(s);
608081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    free(s);
608181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return out_s8;
608281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
608381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
6084021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurentvoid AudioFlinger::RecordThread::audioConfigChanged(int event, int param __unused) {
608581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    AudioSystem::OutputDescriptor desc;
6086b2737d0b33c17e408d96d6f9eeaa3381479c94c7Glenn Kasten    const void *param2 = NULL;
608781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
608881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    switch (event) {
608981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    case AudioSystem::INPUT_OPENED:
609081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    case AudioSystem::INPUT_CONFIG_CHANGED:
6091fad226abd12435dbcd232f7de396f1a097b2bd5fGlenn Kasten        desc.channelMask = mChannelMask;
609281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        desc.samplingRate = mSampleRate;
609381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        desc.format = mFormat;
609481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        desc.frameCount = mFrameCount;
609581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        desc.latency = 0;
609681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        param2 = &desc;
609781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        break;
609881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
609981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    case AudioSystem::INPUT_CLOSED:
610081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    default:
610181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        break;
610281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
6103021cf9634ab09c0753a40b7c9ef4ba603be5c3daEric Laurent    mAudioFlinger->audioConfigChanged(event, mId, param2);
610481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
610581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
6106deca2ae0a7cf8bc54ff3f30b7dc39bbc78b94c0dGlenn Kastenvoid AudioFlinger::RecordThread::readInputParameters_l()
610781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
610881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mSampleRate = mInput->stream->common.get_sample_rate(&mInput->stream->common);
610981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mChannelMask = mInput->stream->common.get_channels(&mInput->stream->common);
6110e541269be94f3a1072932d51537905b120ef4733Andy Hung    mChannelCount = audio_channel_count_from_in_mask(mChannelMask);
6111463be250de73907965faa6a216c00312bf81e049Andy Hung    mHALFormat = mInput->stream->common.get_format(&mInput->stream->common);
6112463be250de73907965faa6a216c00312bf81e049Andy Hung    mFormat = mHALFormat;
6113291bb6d8947c5b0c062f0895d623c529259bfa39Glenn Kasten    if (mFormat != AUDIO_FORMAT_PCM_16_BIT) {
6114cac3daa6332bf6d1f7d26adc4a9915f3d7992dd9Glenn Kasten        ALOGE("HAL format %#x not supported; must be AUDIO_FORMAT_PCM_16_BIT", mFormat);
6115291bb6d8947c5b0c062f0895d623c529259bfa39Glenn Kasten    }
6116665470b36f202bcc8ee2f7417f68fd2608dd07c1Eric Laurent    mFrameSize = audio_stream_in_frame_size(mInput->stream);
6117548efc94813c1dec6e8cf6c085ae41ccb04827f1Glenn Kasten    mBufferSize = mInput->stream->common.get_buffer_size(&mInput->stream->common);
6118548efc94813c1dec6e8cf6c085ae41ccb04827f1Glenn Kasten    mFrameCount = mBufferSize / mFrameSize;
61196dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    // This is the formula for calculating the temporary buffer size.
6120e842614837e5401adf77e90485300c288b9a7876Glenn Kasten    // With 7 HAL buffers, we can guarantee ability to down-sample the input by ratio of 6:1 to
61218594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten    // 1 full output buffer, regardless of the alignment of the available input.
6122e842614837e5401adf77e90485300c288b9a7876Glenn Kasten    // The value is somewhat arbitrary, and could probably be even larger.
61236dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    // A larger value should allow more old data to be read after a track calls start(),
61246dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    // without increasing latency.
6125e842614837e5401adf77e90485300c288b9a7876Glenn Kasten    mRsmpInFrames = mFrameCount * 7;
61268594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten    mRsmpInFramesP2 = roundup(mRsmpInFrames);
61276dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten    delete[] mRsmpInBuffer;
612849d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten
612949d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten    // TODO optimize audio capture buffer sizes ...
613049d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten    // Here we calculate the size of the sliding buffer used as a source
613149d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten    // for resampling.  mRsmpInFramesP2 is currently roundup(mFrameCount * 7).
613249d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten    // For current HAL frame counts, this is usually 2048 = 40 ms.  It would
613349d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten    // be better to have it derived from the pipe depth in the long term.
613449d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten    // The current value is higher than necessary.  However it should not add to latency.
613549d00ad9164ea5ce48c85765a2b6460d9b457d38Glenn Kasten
61368594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten    // Over-allocate beyond mRsmpInFramesP2 to permit a HAL read past end of buffer
61378594843c15b4722ced39436fe9e64f3e57e7ace4Glenn Kasten    mRsmpInBuffer = new int16_t[(mRsmpInFramesP2 + mFrameCount - 1) * mChannelCount];
61386dd62fb91d82dedcfa3ab38c02eb0940b4ba932aGlenn Kasten
61394cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten    // AudioRecord mSampleRate and mChannelCount are constant due to AudioRecord API constraints.
61404cc0a6a835c806d200ef83ef31fe5bef327c355cGlenn Kasten    // But if thread's mSampleRate or mChannelCount changes, how will that affect active tracks?
614181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
614281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
61435f972c031d4061f4f037c9fda1ea4bd9b6a756cdGlenn Kastenuint32_t AudioFlinger::RecordThread::getInputFramesLost()
614481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
614581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
614681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (initCheck() != NO_ERROR) {
614781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return 0;
614881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
614981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
615081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return mInput->stream->get_input_frames_lost(mInput->stream);
615181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
615281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
615381784c37c61b09289654b979567a42bf73cd2b12Eric Laurentuint32_t AudioFlinger::RecordThread::hasAudioSession(int sessionId) const
615481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
615581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
615681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    uint32_t result = 0;
615781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (getEffectChain_l(sessionId) != 0) {
615881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        result = EFFECT_SESSION;
615981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
616081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
616181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t i = 0; i < mTracks.size(); ++i) {
616281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (sessionId == mTracks[i]->sessionId()) {
616381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            result |= TRACK_SESSION;
616481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            break;
616581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
616681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
616781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
616881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return result;
616981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
617081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
617181784c37c61b09289654b979567a42bf73cd2b12Eric LaurentKeyedVector<int, bool> AudioFlinger::RecordThread::sessionIds() const
617281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
617381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    KeyedVector<int, bool> ids;
617481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
617581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    for (size_t j = 0; j < mTracks.size(); ++j) {
617681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        sp<RecordThread::RecordTrack> track = mTracks[j];
617781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        int sessionId = track->sessionId();
617881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        if (ids.indexOfKey(sessionId) < 0) {
617981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            ids.add(sessionId, true);
618081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        }
618181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
618281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return ids;
618381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
618481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
618581784c37c61b09289654b979567a42bf73cd2b12Eric LaurentAudioFlinger::AudioStreamIn* AudioFlinger::RecordThread::clearInput()
618681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
618781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    Mutex::Autolock _l(mLock);
618881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    AudioStreamIn *input = mInput;
618981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mInput = NULL;
619081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return input;
619181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
619281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
619381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent// this method must always be called either with ThreadBase mLock held or inside the thread loop
619481784c37c61b09289654b979567a42bf73cd2b12Eric Laurentaudio_stream_t* AudioFlinger::RecordThread::stream() const
619581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
619681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (mInput == NULL) {
619781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return NULL;
619881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
619981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return &mInput->stream->common;
620081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
620181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
620281784c37c61b09289654b979567a42bf73cd2b12Eric Laurentstatus_t AudioFlinger::RecordThread::addEffectChain_l(const sp<EffectChain>& chain)
620381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
620481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    // only one chain per input thread
620581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (mEffectChains.size() != 0) {
6206aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent        ALOGW("addEffectChain_l() already one chain %p on thread %p", chain.get(), this);
620781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        return INVALID_OPERATION;
620881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
620981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("addEffectChain_l() %p on thread %p", chain.get(), this);
6210aaa44478a373232d8416657035a9020f9c7aa7c3Eric Laurent    chain->setThread(this);
621181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    chain->setInBuffer(NULL);
621281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    chain->setOutBuffer(NULL);
621381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
621481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    checkSuspendOnAddEffectChain_l(chain);
621581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
62161b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent    // make sure enabled pre processing effects state is communicated to the HAL as we
62171b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent    // just moved them to a new input stream.
62181b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent    chain->syncHalEffectsState();
62191b92868010b5c1409692a86f6b27e4a265b64c1aEric Laurent
622081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    mEffectChains.add(chain);
622181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
622281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return NO_ERROR;
622381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
622481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
622581784c37c61b09289654b979567a42bf73cd2b12Eric Laurentsize_t AudioFlinger::RecordThread::removeEffectChain_l(const sp<EffectChain>& chain)
622681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent{
622781784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGV("removeEffectChain_l() %p from thread %p", chain.get(), this);
622881784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    ALOGW_IF(mEffectChains.size() != 1,
622981784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            "removeEffectChain_l() %p invalid chain size %d on thread %p",
623081784c37c61b09289654b979567a42bf73cd2b12Eric Laurent            chain.get(), mEffectChains.size(), this);
623181784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    if (mEffectChains.size() == 1) {
623281784c37c61b09289654b979567a42bf73cd2b12Eric Laurent        mEffectChains.removeAt(0);
623381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    }
623481784c37c61b09289654b979567a42bf73cd2b12Eric Laurent    return 0;
623581784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}
623681784c37c61b09289654b979567a42bf73cd2b12Eric Laurent
62371c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurentstatus_t AudioFlinger::RecordThread::createAudioPatch_l(const struct audio_patch *patch,
62381c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                                          audio_patch_handle_t *handle)
62391c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent{
62401c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    status_t status = NO_ERROR;
62411c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    if (mInput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) {
62421c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        // store new device and send to effects
62431c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        mInDevice = patch->sources[0].ext.device.type;
62441c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        for (size_t i = 0; i < mEffectChains.size(); i++) {
62451c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent            mEffectChains[i]->setDevice_l(mInDevice);
62461c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        }
62471c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
62481c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        // disable AEC and NS if the device is a BT SCO headset supporting those
62491c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        // pre processings
62501c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        if (mTracks.size() > 0) {
62511c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent            bool suspend = audio_is_bluetooth_sco_device(mInDevice) &&
62521c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                mAudioFlinger->btNrecIsOff();
62531c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent            for (size_t i = 0; i < mTracks.size(); i++) {
62541c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                sp<RecordTrack> track = mTracks[i];
62551c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                setEffectSuspended_l(FX_IID_AEC, suspend, track->sessionId());
62561c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                setEffectSuspended_l(FX_IID_NS, suspend, track->sessionId());
62571c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent            }
62581c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        }
62591c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
62601c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        // store new source and send to effects
62611c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        if (mAudioSource != patch->sinks[0].ext.mix.usecase.source) {
62621c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent            mAudioSource = patch->sinks[0].ext.mix.usecase.source;
62631c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent            for (size_t i = 0; i < mEffectChains.size(); i++) {
62641c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                mEffectChains[i]->setAudioSource_l(mAudioSource);
62651c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent            }
62661c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        }
62671c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
62681c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        audio_hw_device_t *hwDevice = mInput->audioHwDev->hwDevice();
62691c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        status = hwDevice->create_audio_patch(hwDevice,
62701c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                               patch->num_sources,
62711c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                               patch->sources,
62721c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                               patch->num_sinks,
62731c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                               patch->sinks,
62741c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent                                               handle);
62751c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    } else {
62761c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        ALOG_ASSERT(false, "createAudioPatch_l() called on a pre 3.0 HAL");
62771c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    }
62781c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    return status;
62791c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent}
62801c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
62811c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurentstatus_t AudioFlinger::RecordThread::releaseAudioPatch_l(const audio_patch_handle_t handle)
62821c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent{
62831c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    status_t status = NO_ERROR;
62841c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    if (mInput->audioHwDev->version() >= AUDIO_DEVICE_API_VERSION_3_0) {
62851c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        audio_hw_device_t *hwDevice = mInput->audioHwDev->hwDevice();
62861c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        status = hwDevice->release_audio_patch(hwDevice, handle);
62871c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    } else {
62881c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent        ALOG_ASSERT(false, "releaseAudioPatch_l() called on a pre 3.0 HAL");
62891c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    }
62901c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent    return status;
62911c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent}
62921c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
629383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentvoid AudioFlinger::RecordThread::addPatchRecord(const sp<PatchRecord>& record)
629483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{
629583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    Mutex::Autolock _l(mLock);
629683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    mTracks.add(record);
629783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent}
629883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
629983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentvoid AudioFlinger::RecordThread::deletePatchRecord(const sp<PatchRecord>& record)
630083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{
630183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    Mutex::Autolock _l(mLock);
630283b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    destroyTrack_l(record);
630383b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent}
630483b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent
630583b8808faad1e91690c64d7007348be8d9ebde73Eric Laurentvoid AudioFlinger::RecordThread::getAudioPortConfig(struct audio_port_config *config)
630683b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent{
630783b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    ThreadBase::getAudioPortConfig(config);
630883b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    config->role = AUDIO_PORT_ROLE_SINK;
630983b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    config->ext.mix.hw_module = mInput->audioHwDev->handle();
631083b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent    config->ext.mix.usecase.source = mAudioSource;
631183b8808faad1e91690c64d7007348be8d9ebde73Eric Laurent}
63121c333e252cbca3337c1bedbc57a005f3b7d23fdbEric Laurent
631381784c37c61b09289654b979567a42bf73cd2b12Eric Laurent}; // namespace android
6314